def imshow_filter_optimal_gratings(filters, opt_frq, opt_ang): """ Plot the filters and corresponding optimal gating pattern. :param filters: Filters to analyze. :type filters: numpy array :param opt_frq: Optimal frequencies. :type opt_frq: int :param opt_ang: Optimal frequencies. :type opt_ang: int """ # something here input_dim = filters.shape[0] output_dim = filters.shape[1] max_wavelength = int(np.sqrt(input_dim)) frqmatrix = np.tile(float(1) / opt_frq * np.pi * 2, (input_dim, 1)) thetamatrix = np.tile(opt_ang, (input_dim, 1)) vec_xy = np.array(range(0, input_dim)) vec_x = np.floor_divide(vec_xy, max_wavelength) vec_y = vec_xy + 1 - vec_x * max_wavelength xmatrix = np.tile(vec_x, (output_dim, 1)) ymatrix = np.tile(vec_y, (output_dim, 1)) gratingmatrix = np.cos(frqmatrix * (np.sin(thetamatrix) * xmatrix.transpose() + np.cos(thetamatrix ) * ymatrix.transpose())) combinedmatrix = np.concatenate((rescale_data(filters), rescale_data(gratingmatrix)), 1) imshow_matrix(tile_matrix_rows(combinedmatrix, max_wavelength, max_wavelength, 2, output_dim, border_size=1, normalized=False), 'optimal grating')
def tile_matrix_rows(matrix, tile_width, tile_height, num_tiles_x, num_tiles_y, border_size=1, normalized=True): """ Creates a matrix with tiles from rows. :param matrix: Matrix to display. :type matrix: numpy array 2D :param tile_width: Tile width dimension. :type tile_width: int :param tile_height: Tile height dimension. :type tile_height: int :param num_tiles_x: Number of tiles horizontal. :type num_tiles_x: int :param num_tiles_y: Number of tiles vertical. :type num_tiles_y: int :param border_size: Size of the border. :type border_size: int :param normalized: If true each image gets normalized to be between 0..1. :type normalized: bool :return: Matrix showing the 2D patches. :rtype: 2D numpy array """ if normalized is True: result = np.max(rescale_data(matrix)) else: result = np.max(matrix) result *= np.ones( (tile_width * num_tiles_x + (num_tiles_x - 1) * border_size, tile_height * num_tiles_y + (num_tiles_y - 1) * border_size)) for x in xrange(num_tiles_x): for y in xrange(num_tiles_y): single_image = matrix[:, (x * num_tiles_y) + y].reshape( tile_width, tile_height) if normalized is True: result[x * (tile_width + border_size):x * (tile_width + border_size) + tile_width, y * (tile_height + border_size):y * (tile_height + border_size) + tile_height] = rescale_data(single_image) else: result[x * (tile_width + border_size):x * (tile_width + border_size) + tile_width, y * (tile_height + border_size):y * (tile_height + border_size) + tile_height] = single_image return result
def tile_matrix_rows(matrix, tile_width, tile_height, num_tiles_x, num_tiles_y, border_size=1, normalized = True): ''' Creates a matrix with tiles from rows. :Parameters: matrix: Matrix to display. -type: numpy array 2D tile_width: tile width dimension. -type: int tile_height: tile height dimension. -type: int num_tiles_x: Number of tiles horizontal. -type: int num_tiles_y: Number of tiles vertical. -type: int border_size: Size of the border. -type: int normalized: If true each image gets normalized to be between 0..1. -type: bool :Returns: Image showing the 2D patches. -type: numpy array 2D ''' if(normalized==True): result = np.max(rescale_data(matrix)) else: result = np.max(matrix) result *= np.ones((tile_width*num_tiles_x+(num_tiles_x-1) *border_size, tile_height*num_tiles_y +(num_tiles_y-1)*border_size)) for x in xrange(num_tiles_x) : for y in xrange(num_tiles_y) : single_image = matrix[:,(x*num_tiles_y)+y].reshape(tile_width, tile_height) if(normalized==True): result[x*(tile_width + border_size):x*(tile_width + border_size)+tile_width, y*(tile_height + border_size ):y *(tile_height + border_size )+tile_height] = rescale_data( single_image) else: result[x*(tile_width + border_size):x*(tile_width + border_size)+tile_width, y*(tile_height + border_size ):y *(tile_height + border_size )+tile_height] = single_image return result
def test_rescale_data(self): print ('Preprocessing -> Performing rescale_data test ...') sys.stdout.flush() data = rescale_data(numx.random.randn(100, 100), new_min=-2.0, new_max=4.0) assert numx.sum(data >= -2.0) + numx.sum(data <= 4.0) == 20000 print('successfully passed!') sys.stdout.flush()