<s> """<STR_LIT>""" <EOL> import matplotlib <EOL> matplotlib . use ( '<STR_LIT>' ) <EOL> import matplotlib . pyplot as plt <EOL> import matplotlib . cm as cm <EOL> import numpy as np <EOL> import warnings <EOL> def is_square ( shp , n_colors = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> is_sqr = ( shp == np . round ( np . sqrt ( shp ) ) ** <NUM_LIT:2> ) <EOL> is_sqr_colors = ( shp == n_colors * np . round ( np . sqrt ( np . array ( shp ) / float ( n_colors ) ) ) ** <NUM_LIT:2> ) <EOL> return is_sqr | is_sqr_colors <EOL> def show_receptive_fields ( theta , P = None , n_colors = None , max_display = <NUM_LIT:100> , grid_wa = None ) : <EOL> """<STR_LIT>""" <EOL> shp = np . array ( theta . shape ) <EOL> if n_colors is None : <EOL> n_colors = <NUM_LIT:1> <EOL> if shp [ - <NUM_LIT:1> ] == <NUM_LIT:3> : <EOL> n_colors = <NUM_LIT:3> <EOL> if shp [ - <NUM_LIT:1> ] == n_colors : <EOL> shp [ - <NUM_LIT:2> ] *= n_colors <EOL> theta = theta . reshape ( shp [ : - <NUM_LIT:1> ] ) <EOL> shp = np . array ( theta . shape ) <EOL> if len ( shp ) > <NUM_LIT:2> : <EOL> shp [ - <NUM_LIT:2> ] *= shp [ - <NUM_LIT:1> ] <EOL> theta = theta . reshape ( shp [ : - <NUM_LIT:1> ] ) <EOL> shp = np . array ( theta . shape ) <EOL> if len ( shp ) > <NUM_LIT:2> : <EOL> theta = theta . reshape ( ( - <NUM_LIT:1> , shp [ - <NUM_LIT:1> ] ) ) <EOL> shp = np . array ( theta . shape ) <EOL> if len ( shp ) == <NUM_LIT:1> : <EOL> theta = theta . reshape ( ( - <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> shp = np . array ( theta . shape ) <EOL> is_sqr = is_square ( shp , n_colors = n_colors ) <EOL> if is_sqr [ <NUM_LIT:0> ] and is_sqr [ <NUM_LIT:1> ] : <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> elif is_sqr [ <NUM_LIT:1> ] : <EOL> theta = theta . T <EOL> elif not is_sqr [ <NUM_LIT:0> ] and not is_sqr [ <NUM_LIT:1> ] : <EOL> return False <EOL> theta = theta [ : , : max_display ] . copy ( ) <EOL> if P is None : <EOL> img_w = int ( np . ceil ( np . sqrt ( theta . shape [ <NUM_LIT:0> ] / float ( n_colors ) ) ) ) <EOL> else : <EOL> img_w = int ( np . ceil ( np . sqrt ( P . shape [ <NUM_LIT:0> ] / float ( n_colors ) ) ) ) <EOL> nf = theta . shape [ <NUM_LIT:1> ] <EOL> if grid_wa is None : <EOL> grid_wa = int ( np . ceil ( np . sqrt ( float ( nf ) ) ) ) <EOL> grid_wb = int ( np . ceil ( nf / float ( grid_wa ) ) ) <EOL> if P is not None : <EOL> theta = np . dot ( P , theta ) <EOL> vmin = np . min ( theta ) <EOL> vmax = np . max ( theta ) <EOL> for jj in range ( nf ) : <EOL> plt . subplot ( grid_wa , grid_wb , jj + <NUM_LIT:1> ) <EOL> ptch = np . zeros ( ( n_colors * img_w ** <NUM_LIT:2> , ) ) <EOL> ptch [ : theta . shape [ <NUM_LIT:0> ] ] = theta [ : , jj ] <EOL> if n_colors == <NUM_LIT:3> : <EOL> ptch = ptch . reshape ( ( n_colors , img_w , img_w ) ) <EOL> ptch = ptch . transpose ( ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:0> ) ) <EOL> else : <EOL> ptch = ptch . reshape ( ( img_w , img_w ) ) <EOL> ptch -= vmin <EOL> ptch /= vmax - vmin <EOL> plt . imshow ( ptch , interpolation = '<STR_LIT>' , cmap = cm . Greys_r ) <EOL> plt . axis ( '<STR_LIT>' ) <EOL> return True <EOL> def plot_parameter ( theta_in , base_fname_part1 , base_fname_part2 = "<STR_LIT>" , title = '<STR_LIT>' , n_colors = None ) : <EOL> """<STR_LIT>""" <EOL> theta = np . array ( theta_in . copy ( ) ) <EOL> print "<STR_LIT>" % ( <EOL> title , np . min ( theta ) , np . median ( theta ) , np . mean ( theta ) , np . max ( theta ) ) , theta . shape <EOL> theta = np . squeeze ( theta ) <EOL> if len ( theta . shape ) == <NUM_LIT:0> : <EOL> theta = np . array ( [ theta ] ) <EOL> shp = theta . shape <EOL> if len ( shp ) > <NUM_LIT:2> : <EOL> theta = theta . reshape ( ( theta . shape [ <NUM_LIT:0> ] , - <NUM_LIT:1> ) ) <EOL> shp = theta . shape <EOL> plt . figure ( figsize = [ <NUM_LIT:8> , <NUM_LIT:8> ] ) <EOL> if len ( shp ) == <NUM_LIT:1> : <EOL> plt . plot ( theta , '<STR_LIT:.>' , alpha = <NUM_LIT:0.5> ) <EOL> elif len ( shp ) == <NUM_LIT:2> : <EOL> plt . imshow ( theta , interpolation = '<STR_LIT>' , aspect = '<STR_LIT>' , cmap = cm . Greys_r ) <EOL> plt . colorbar ( ) <EOL> plt . title ( title ) <EOL> plt . savefig ( base_fname_part1 + '<STR_LIT>' + base_fname_part2 + '<STR_LIT>' ) <EOL> plt . close ( ) <EOL> if len ( shp ) >= <NUM_LIT:2> or is_square ( shp [ <NUM_LIT:0> ] ) : <EOL> if len ( shp ) == <NUM_LIT:1> : <EOL> theta = theta . reshape ( ( - <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> plt . figure ( figsize = [ <NUM_LIT:8> , <NUM_LIT:8> ] ) <EOL> if show_receptive_fields ( theta , n_colors = n_colors ) : <EOL> plt . suptitle ( title + "<STR_LIT>" ) <EOL> plt . savefig ( base_fname_part1 + '<STR_LIT>' + base_fname_part2 + '<STR_LIT>' ) <EOL> plt . close ( ) <EOL> def plot_images ( X , fname ) : <EOL> """<STR_LIT>""" <EOL> Xcol = X . reshape ( ( X . shape [ <NUM_LIT:0> ] , - <NUM_LIT:1> , ) ) . T <EOL> plt . figure ( figsize = [ <NUM_LIT:8> , <NUM_LIT:8> ] ) <EOL> if show_receptive_fields ( Xcol , n_colors = X . shape [ <NUM_LIT:1> ] ) : <EOL> plt . savefig ( fname + '<STR_LIT>' ) <EOL> else : <EOL> warnings . warn ( '<STR_LIT>' ) <EOL> plt . close ( ) <EOL> np . savez ( fname + '<STR_LIT>' , X = X ) </s>
