<s> """<STR_LIT>""" <EOL> import time <EOL> import sys <EOL> from nilearn import datasets <EOL> sys . stderr . write ( "<STR_LIT>" ) <EOL> t0 = time . time ( ) <EOL> miyawaki_dataset = datasets . fetch_miyawaki2008 ( ) <EOL> print ( '<STR_LIT>' % <EOL> miyawaki_dataset . func [ <NUM_LIT:0> ] ) <EOL> X_random_filenames = miyawaki_dataset . func [ <NUM_LIT:12> : ] <EOL> X_figure_filenames = miyawaki_dataset . func [ : <NUM_LIT:12> ] <EOL> y_random_filenames = miyawaki_dataset . label [ <NUM_LIT:12> : ] <EOL> y_figure_filenames = miyawaki_dataset . label [ : <NUM_LIT:12> ] <EOL> y_shape = ( <NUM_LIT:10> , <NUM_LIT:10> ) <EOL> sys . stderr . write ( "<STR_LIT>" % ( time . time ( ) - t0 ) ) <EOL> import numpy as np <EOL> from nilearn . input_data import MultiNiftiMasker <EOL> sys . stderr . write ( "<STR_LIT>" ) <EOL> t0 = time . time ( ) <EOL> masker = MultiNiftiMasker ( mask_img = miyawaki_dataset . mask , detrend = True , <EOL> standardize = False ) <EOL> masker . fit ( ) <EOL> X_train = masker . transform ( X_random_filenames ) <EOL> X_test = masker . transform ( X_figure_filenames ) <EOL> y_train = [ ] <EOL> for y in y_random_filenames : <EOL> y_train . append ( np . reshape ( np . loadtxt ( y , dtype = np . int , delimiter = '<STR_LIT:U+002C>' ) , <EOL> ( - <NUM_LIT:1> , ) + y_shape , order = '<STR_LIT:F>' ) ) <EOL> y_test = [ ] <EOL> for y in y_figure_filenames : <EOL> y_test . append ( np . reshape ( np . loadtxt ( y , dtype = np . int , delimiter = '<STR_LIT:U+002C>' ) , <EOL> ( - <NUM_LIT:1> , ) + y_shape , order = '<STR_LIT:F>' ) ) <EOL> X_train = np . vstack ( [ x [ <NUM_LIT:2> : ] for x in X_train ] ) <EOL> y_train = np . vstack ( [ y [ : - <NUM_LIT:2> ] for y in y_train ] ) . astype ( float ) <EOL> X_test = np . vstack ( [ x [ <NUM_LIT:2> : ] for x in X_test ] ) <EOL> y_test = np . vstack ( [ y [ : - <NUM_LIT:2> ] for y in y_test ] ) . astype ( float ) <EOL> n_pixels = y_train . shape [ <NUM_LIT:1> ] <EOL> n_features = X_train . shape [ <NUM_LIT:1> ] <EOL> def flatten ( list_of_2d_array ) : <EOL> flattened = [ ] <EOL> for array in list_of_2d_array : <EOL> flattened . append ( array . ravel ( ) ) <EOL> return flattened <EOL> y_cols = y_shape [ <NUM_LIT:1> ] <EOL> design_matrix = np . eye ( <NUM_LIT:100> ) <EOL> height_tf = ( np . eye ( y_cols ) + np . eye ( y_cols , k = <NUM_LIT:1> ) ) [ : y_cols - <NUM_LIT:1> ] * <NUM_LIT> <EOL> width_tf = height_tf . T <EOL> yt_tall = [ np . dot ( height_tf , m ) for m in y_train ] <EOL> yt_large = [ np . dot ( m , width_tf ) for m in y_train ] <EOL> yt_big = [ np . dot ( height_tf , np . dot ( m , width_tf ) ) for m in y_train ] <EOL> y_train = [ np . r_ [ y . ravel ( ) , t . ravel ( ) , l . ravel ( ) , b . ravel ( ) ] <EOL> for y , t , l , b in zip ( y_train , yt_tall , yt_large , yt_big ) ] <EOL> y_test = np . asarray ( flatten ( y_test ) ) <EOL> y_train = np . asarray ( y_train ) <EOL> X_train = X_train [ y_train [ : , <NUM_LIT:0> ] != - <NUM_LIT:1> ] <EOL> y_train = y_train [ y_train [ : , <NUM_LIT:0> ] != - <NUM_LIT:1> ] <EOL> X_test = X_test [ y_test [ : , <NUM_LIT:0> ] != - <NUM_LIT:1> ] <EOL> y_test = y_test [ y_test [ : , <NUM_LIT:0> ] != - <NUM_LIT:1> ] <EOL> sys . stderr . write ( "<STR_LIT>" % ( time . time ( ) - t0 ) ) <EOL> sys . stderr . write ( "<STR_LIT>" ) <EOL> t0 = time . time ( ) <EOL> from sklearn . linear_model import OrthogonalMatchingPursuit as OMP <EOL> from sklearn . feature_selection import f_classif , SelectKBest <EOL> from sklearn . pipeline import Pipeline <EOL> clfs = [ ] <EOL> n_clfs = y_train . shape [ <NUM_LIT:1> ] <EOL> for i in range ( y_train . shape [ <NUM_LIT:1> ] ) : <EOL> sys . stderr . write ( "<STR_LIT>" % ( i + <NUM_LIT:1> , n_clfs ) ) <EOL> clf = Pipeline ( [ ( '<STR_LIT>' , SelectKBest ( f_classif , <NUM_LIT> ) ) , <EOL> ( '<STR_LIT>' , OMP ( n_nonzero_coefs = <NUM_LIT:10> ) ) ] ) <EOL> clf . fit ( X_train , y_train [ : , i ] ) <EOL> clfs . append ( clf ) <EOL> sys . stderr . write ( "<STR_LIT>" % ( <EOL> n_clfs , n_clfs , time . time ( ) - t0 ) ) <EOL> sys . stderr . write ( "<STR_LIT>" ) <EOL> t0 = time . time ( ) <EOL> y_pred = [ ] <EOL> for clf in clfs : <EOL> y_pred . append ( clf . predict ( X_test ) ) <EOL> y_pred = np . asarray ( y_pred ) . T <EOL> def split_multi_scale ( y , y_shape ) : <EOL> """<STR_LIT>""" <EOL> yw , yh = y_shape <EOL> split_index = [ yw * yh ] <EOL> split_index . append ( split_index [ - <NUM_LIT:1> ] + ( yw - <NUM_LIT:1> ) * yh ) <EOL> split_index . append ( split_index [ - <NUM_LIT:1> ] + yw * ( yh - <NUM_LIT:1> ) ) <EOL> split_index . append ( split_index [ - <NUM_LIT:1> ] + ( yw - <NUM_LIT:1> ) * ( yh - <NUM_LIT:1> ) ) <EOL> y_preds = np . split ( y , split_index , axis = <NUM_LIT:1> ) <EOL> y_pred = y_preds [ <NUM_LIT:0> ] <EOL> height_tf_i = ( np . eye ( y_cols ) + np . eye ( y_cols , k = - <NUM_LIT:1> ) ) [ : , : y_cols - <NUM_LIT:1> ] * <NUM_LIT> <EOL> height_tf_i . flat [ <NUM_LIT:0> ] = <NUM_LIT:1> <EOL> height_tf_i . flat [ - <NUM_LIT:1> ] = <NUM_LIT:1> <EOL> y_pred_tall = [ np . dot ( height_tf_i , np . reshape ( m , ( yw - <NUM_LIT:1> , yh ) ) ) . flatten ( ) <EOL> for m in y_preds [ <NUM_LIT:1> ] ] <EOL> y_pred_tall = np . asarray ( y_pred_tall ) <EOL> width_tf_i = ( np . eye ( y_cols ) + np . eye ( y_cols , k = <NUM_LIT:1> ) ) [ : y_cols - <NUM_LIT:1> ] * <NUM_LIT> <EOL> width_tf_i . flat [ <NUM_LIT:0> ] = <NUM_LIT:1> <EOL> width_tf_i . flat [ - <NUM_LIT:1> ] = <NUM_LIT:1> <EOL> y_pred_large = [ np . dot ( np . reshape ( m , ( yw , yh - <NUM_LIT:1> ) ) , width_tf_i ) . flatten ( ) <EOL> for m in y_preds [ <NUM_LIT:2> ] ] <EOL> y_pred_large = np . asarray ( y_pred_large ) <EOL> y_pred_big = [ np . dot ( np . reshape ( m , ( yw - <NUM_LIT:1> , yh - <NUM_LIT:1> ) ) , width_tf_i ) <EOL> for m in y_preds [ <NUM_LIT:3> ] ] <EOL> y_pred_big = [ np . dot ( height_tf_i , np . reshape ( m , ( yw - <NUM_LIT:1> , yh ) ) ) . flatten ( ) <EOL> for m in y_pred_big ] <EOL> y_pred_big = np . asarray ( y_pred_big ) <EOL> return ( y_pred , y_pred_tall , y_pred_large , y_pred_big ) <EOL> y_pred , y_pred_tall , y_pred_large , y_pred_big = split_multi_scale ( y_pred , y_shape ) <EOL> y_pred = ( <NUM_LIT> * y_pred + <NUM_LIT> * y_pred_tall + <NUM_LIT> * y_pred_large <EOL> + <NUM_LIT> * y_pred_big ) <EOL> sys . stderr . write ( "<STR_LIT>" % ( time . time ( ) - t0 ) ) <EOL> from sklearn . metrics import ( accuracy_score , precision_score , recall_score , <EOL> f1_score ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" % np . mean ( [ <EOL> accuracy_score ( y_test [ : , i ] , y_pred [ : , i ] > <NUM_LIT> ) for i in range ( <NUM_LIT:100> ) ] ) ) <EOL> print ( "<STR_LIT>" % np . mean ( [ <EOL> precision_score ( y_test [ : , i ] , y_pred [ : , i ] > <NUM_LIT> ) for i in range ( <NUM_LIT:100> ) ] ) ) <EOL> print ( "<STR_LIT>" % np . mean ( [ <EOL> recall_score ( y_test [ : , i ] , y_pred [ : , i ] > <NUM_LIT> ) for i in range ( <NUM_LIT:100> ) ] ) ) <EOL> print ( "<STR_LIT>" % np . mean ( [ <EOL> f1_score ( y_test [ : , i ] , y_pred [ : , i ] > <NUM_LIT> ) for i in range ( <NUM_LIT:100> ) ] ) ) <EOL> from matplotlib import pyplot as plt <EOL> for i in range ( <NUM_LIT:6> ) : <EOL> j = <NUM_LIT:10> * i <EOL> fig = plt . figure ( ) <EOL> sp1 = plt . subplot ( <NUM_LIT> ) <EOL> sp1 . axis ( '<STR_LIT>' ) <EOL> plt . title ( '<STR_LIT>' ) <EOL> sp2 = plt . subplot ( <NUM_LIT> ) <EOL> sp2 . axis ( '<STR_LIT>' ) <EOL> plt . title ( '<STR_LIT>' ) <EOL> sp3 = plt . subplot ( <NUM_LIT> ) <EOL> sp3 . axis ( '<STR_LIT>' ) <EOL> plt . title ( '<STR_LIT>' ) <EOL> sp1 . imshow ( np . reshape ( y_test [ j ] , ( <NUM_LIT:10> , <NUM_LIT:10> ) ) , cmap = plt . cm . gray , <EOL> interpolation = '<STR_LIT>' ) , <EOL> sp2 . imshow ( np . reshape ( y_pred [ j ] , ( <NUM_LIT:10> , <NUM_LIT:10> ) ) , cmap = plt . cm . gray , <EOL> interpolation = '<STR_LIT>' ) , <EOL> sp3 . imshow ( np . reshape ( y_pred [ j ] > <NUM_LIT> , ( <NUM_LIT:10> , <NUM_LIT:10> ) ) , cmap = plt . cm . gray , <EOL> interpolation = '<STR_LIT>' ) <EOL> plt . savefig ( '<STR_LIT>' % i ) <EOL> plt . show ( ) </s>
