<s> """<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> if __name__ == '<STR_LIT:__main__>' and __package__ is None : <EOL> filePath = os . path . dirname ( os . path . dirname ( os . path . abspath ( __file__ ) ) ) <EOL> sys . path . append ( filePath ) <EOL> import yaml <EOL> from glob import glob <EOL> import numpy as np <EOL> import pandas as pd <EOL> from time import time <EOL> from sklearn . metrics import roc_auc_score <EOL> import theano <EOL> from nolearn . lasagne import BatchIterator , NeuralNet , TrainSplit <EOL> from lasagne . objectives import aggregate , binary_crossentropy <EOL> from lasagne . layers import ( InputLayer , DropoutLayer , DenseLayer , Conv1DLayer , <EOL> Conv2DLayer ) <EOL> from lasagne . updates import nesterov_momentum , adam <EOL> from theano . tensor . nnet import sigmoid <EOL> from mne import concatenate_raws , pick_types <EOL> from preprocessing . aux import creat_mne_raw_object <EOL> from preprocessing . filterBank import FilterBank <EOL> import warnings <EOL> warnings . filterwarnings ( '<STR_LIT:ignore>' , '<STR_LIT>' ) <EOL> warnings . filterwarnings ( '<STR_LIT:ignore>' , module = '<STR_LIT>' ) <EOL> warnings . filterwarnings ( '<STR_LIT:ignore>' , module = '<STR_LIT>' ) <EOL> yml = yaml . load ( open ( sys . argv [ <NUM_LIT:1> ] ) ) <EOL> fileName = yml [ '<STR_LIT:Meta>' ] [ '<STR_LIT:file>' ] <EOL> filt2Dsize = yml [ '<STR_LIT>' ] if '<STR_LIT>' in yml . keys ( ) else <NUM_LIT:0> <EOL> filters = yml [ '<STR_LIT>' ] <EOL> delay = yml [ '<STR_LIT>' ] <EOL> skip = yml [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in yml . keys ( ) : <EOL> bags = yml [ '<STR_LIT>' ] <EOL> else : <EOL> bags = <NUM_LIT:3> <EOL> mode = sys . argv [ <NUM_LIT:2> ] <EOL> if mode == '<STR_LIT>' : <EOL> test = False <EOL> elif mode == '<STR_LIT:test>' : <EOL> test = True <EOL> else : <EOL> raise ( '<STR_LIT>' ) <EOL> SUBJECTS = list ( range ( <NUM_LIT:1> , <NUM_LIT> ) ) <EOL> TRAIN_SERIES = list ( range ( <NUM_LIT:1> , <NUM_LIT:9> ) ) <EOL> TEST_SERIES = [ <NUM_LIT:9> , <NUM_LIT:10> ] <EOL> N_ELECTRODES = <NUM_LIT:32> <EOL> N_EVENTS = <NUM_LIT:6> <EOL> SAMPLE_SIZE = delay <EOL> DOWNSAMPLE = skip <EOL> TIME_POINTS = SAMPLE_SIZE // DOWNSAMPLE <EOL> TRAIN_SIZE = <NUM_LIT> <EOL> def preprocessData ( data ) : <EOL> """<STR_LIT>""" <EOL> fb = FilterBank ( filters ) <EOL> return fb . transform ( data ) <EOL> class Source : <EOL> """<STR_LIT>""" <EOL> mean = None <EOL> std = None <EOL> def load_raw_data ( self , subject , series ) : <EOL> """<STR_LIT>""" <EOL> test = series == TEST_SERIES <EOL> if not test : <EOL> fnames = [ glob ( '<STR_LIT>' % <EOL> ( subject , i ) ) for i in series ] <EOL> else : <EOL> fnames = [ glob ( '<STR_LIT>' % <EOL> ( subject , i ) ) for i in series ] <EOL> fnames = list ( np . concatenate ( fnames ) ) <EOL> fnames . sort ( ) <EOL> raw_train = [ creat_mne_raw_object ( fname , read_events = not test ) <EOL> for fname in fnames ] <EOL> raw_train = concatenate_raws ( raw_train ) <EOL> picks = pick_types ( raw_train . info , eeg = True ) <EOL> self . data = raw_train . _data [ picks ] . transpose ( ) <EOL> self . data = preprocessData ( self . data ) <EOL> if not test : <EOL> self . events = raw_train . _data [ <NUM_LIT:32> : ] . transpose ( ) <EOL> def normalize ( self ) : <EOL> """<STR_LIT>""" <EOL> self . data -= self . mean <EOL> self . data /= self . std <EOL> class TrainSource ( Source ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , subject , series_list ) : <EOL> """<STR_LIT>""" <EOL> self . load_raw_data ( subject , series_list ) <EOL> self . mean = self . data . mean ( axis = <NUM_LIT:0> ) <EOL> self . std = self . data . std ( axis = <NUM_LIT:0> ) <EOL> self . normalize ( ) <EOL> class TestSource ( Source ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , subject , series , train_source ) : <EOL> """<STR_LIT>""" <EOL> self . load_raw_data ( subject , series ) <EOL> self . mean = train_source . mean <EOL> self . std = train_source . std <EOL> self . normalize ( ) <EOL> class LayerFactory : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . layer_cnt = <NUM_LIT:0> <EOL> self . kwargs = { } <EOL> def __call__ ( self , layer , layer_name = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . layer_cnt += <NUM_LIT:1> <EOL> name = layer_name or "<STR_LIT>" . format ( self . layer_cnt ) <EOL> for k , v in kwargs . items ( ) : <EOL> self . kwargs [ "<STR_LIT>" . format ( name , k ) ] = v <EOL> return ( name , layer ) <EOL> class IndexBatchIterator ( BatchIterator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , source , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> super ( IndexBatchIterator , self ) . __init__ ( * args , ** kwargs ) <EOL> self . source = source <EOL> if source is not None : <EOL> x = source . data <EOL> input_shape = [ len ( x ) + ( SAMPLE_SIZE - <NUM_LIT:1> ) , N_ELECTRODES ] <EOL> self . augmented = np . zeros ( input_shape , dtype = np . float32 ) <EOL> self . augmented [ SAMPLE_SIZE - <NUM_LIT:1> : ] = x <EOL> self . augmented [ : SAMPLE_SIZE - <NUM_LIT:1> ] = x [ <NUM_LIT:0> ] <EOL> if filt2Dsize : <EOL> input_shape = [ self . batch_size , <NUM_LIT:1> , N_ELECTRODES , TIME_POINTS ] <EOL> self . Xbuf = np . zeros ( input_shape , np . float32 ) <EOL> else : <EOL> input_shape = [ self . batch_size , N_ELECTRODES , TIME_POINTS ] <EOL> self . Xbuf = np . zeros ( input_shape , np . float32 ) <EOL> self . Ybuf = np . zeros ( [ self . batch_size , N_EVENTS ] , np . float32 ) <EOL> def transform ( self , X_indices , y_indices ) : <EOL> """<STR_LIT>""" <EOL> X_indices , y_indices = super ( IndexBatchIterator , <EOL> self ) . transform ( X_indices , y_indices ) <EOL> [ count ] = X_indices . shape <EOL> X = self . Xbuf [ : count ] <EOL> Y = self . Ybuf [ : count ] <EOL> for i , ndx in enumerate ( X_indices ) : <EOL> if ndx == - <NUM_LIT:1> : <EOL> ndx = np . random . randint ( len ( self . source . events ) ) <EOL> sample = self . augmented [ ndx : ndx + SAMPLE_SIZE ] <EOL> if filt2Dsize : <EOL> X [ i ] [ <NUM_LIT:0> ] = sample [ : : - <NUM_LIT:1> ] [ : : DOWNSAMPLE ] . transpose ( ) <EOL> else : <EOL> X [ i ] = sample [ : : - <NUM_LIT:1> ] [ : : DOWNSAMPLE ] . transpose ( ) <EOL> if y_indices is not None : <EOL> Y [ i ] = self . source . events [ ndx ] <EOL> Y = None if ( y_indices is None ) else Y <EOL> return X , Y <EOL> def create_net ( train_source , test_source , batch_size = <NUM_LIT> , max_epochs = <NUM_LIT:100> , <EOL> train_val_split = False ) : <EOL> """<STR_LIT>""" <EOL> if train_val_split : <EOL> train_val_split = TrainSplit ( eval_size = <NUM_LIT> ) <EOL> else : <EOL> train_val_split = TrainSplit ( eval_size = False ) <EOL> batch_iter_train = IndexBatchIterator ( train_source , batch_size = batch_size ) <EOL> batch_iter_test = IndexBatchIterator ( test_source , batch_size = batch_size ) <EOL> LF = LayerFactory ( ) <EOL> dense = <NUM_LIT> <EOL> if filt2Dsize : <EOL> inputLayer = LF ( InputLayer , shape = ( None , <NUM_LIT:1> , N_ELECTRODES , TIME_POINTS ) ) <EOL> convLayer = LF ( Conv2DLayer , num_filters = <NUM_LIT:8> , filter_size = ( N_ELECTRODES , filt2Dsize ) ) <EOL> else : <EOL> inputLayer = LF ( InputLayer , shape = ( None , N_ELECTRODES , TIME_POINTS ) ) <EOL> convLayer = LF ( Conv1DLayer , num_filters = <NUM_LIT:8> , filter_size = <NUM_LIT:1> ) <EOL> layers = [ <EOL> inputLayer , <EOL> LF ( DropoutLayer , p = <NUM_LIT:0.5> ) , <EOL> convLayer , <EOL> LF ( DenseLayer , num_units = dense ) , <EOL> LF ( DropoutLayer , p = <NUM_LIT:0.5> ) , <EOL> LF ( DenseLayer , num_units = dense ) , <EOL> LF ( DropoutLayer , p = <NUM_LIT:0.5> ) , <EOL> LF ( DenseLayer , layer_name = "<STR_LIT>" , num_units = N_EVENTS , <EOL> nonlinearity = sigmoid ) <EOL> ] <EOL> def loss ( x , t ) : <EOL> return aggregate ( binary_crossentropy ( x , t ) ) <EOL> if filt2Dsize : <EOL> nnet = NeuralNet ( y_tensor_type = theano . tensor . matrix , <EOL> layers = layers , <EOL> batch_iterator_train = batch_iter_train , <EOL> batch_iterator_test = batch_iter_test , <EOL> max_epochs = max_epochs , <EOL> verbose = <NUM_LIT:0> , <EOL> update = adam , <EOL> update_learning_rate = <NUM_LIT> , <EOL> objective_loss_function = loss , <EOL> regression = True , <EOL> train_split = train_val_split , <EOL> ** LF . kwargs ) <EOL> else : <EOL> nnet = NeuralNet ( y_tensor_type = theano . tensor . matrix , <EOL> layers = layers , <EOL> batch_iterator_train = batch_iter_train , <EOL> batch_iterator_test = batch_iter_test , <EOL> max_epochs = max_epochs , <EOL> verbose = <NUM_LIT:0> , <EOL> update = nesterov_momentum , <EOL> update_learning_rate = <NUM_LIT> , <EOL> update_momentum = <NUM_LIT> , <EOL> objective_loss_function = loss , <EOL> regression = True , <EOL> train_split = train_val_split , <EOL> ** LF . kwargs ) <EOL> return nnet <EOL> print '<STR_LIT>' % ( mode , fileName ) <EOL> report = pd . DataFrame ( index = [ fileName ] ) <EOL> start_time = time ( ) <EOL> train_indices = np . zeros ( [ TRAIN_SIZE ] , dtype = int ) - <NUM_LIT:1> <EOL> np . random . seed ( <NUM_LIT> ) <EOL> valid_series = [ <NUM_LIT:7> , <NUM_LIT:8> ] <EOL> max_epochs = <NUM_LIT:100> <EOL> if test is False : <EOL> probs_bags = [ ] <EOL> for bag in range ( bags ) : <EOL> probs_tot = [ ] <EOL> lbls_tot = [ ] <EOL> for subject in range ( <NUM_LIT:1> , <NUM_LIT> ) : <EOL> tseries = sorted ( set ( TRAIN_SERIES ) - set ( valid_series ) ) <EOL> train_source = TrainSource ( subject , tseries ) <EOL> test_source = TestSource ( subject , valid_series , train_source ) <EOL> net = create_net ( train_source , test_source , max_epochs = max_epochs , <EOL> train_val_split = False ) <EOL> dummy = net . fit ( train_indices , train_indices ) <EOL> indices = np . arange ( len ( test_source . data ) ) <EOL> probs = net . predict_proba ( indices ) <EOL> auc = np . mean ( [ roc_auc_score ( trueVals , p ) for trueVals , p in <EOL> zip ( test_source . events . T , probs . T ) ] ) <EOL> print '<STR_LIT>' % ( bag , subject , auc ) <EOL> probs_tot . append ( probs ) <EOL> lbls_tot . append ( test_source . events ) <EOL> probs_tot = np . concatenate ( probs_tot ) <EOL> lbls_tot = np . concatenate ( lbls_tot ) <EOL> auc = np . mean ( [ roc_auc_score ( trueVals , p ) for trueVals , p in <EOL> zip ( lbls_tot . transpose ( ) , probs_tot . transpose ( ) ) ] ) <EOL> print auc <EOL> probs_bags . append ( probs_tot ) <EOL> probs_bags = np . mean ( probs_bags , axis = <NUM_LIT:0> ) <EOL> np . save ( '<STR_LIT>' % fileName , [ probs_bags ] ) <EOL> else : <EOL> probs_bags = [ ] <EOL> for bag in range ( bags ) : <EOL> probs_tot = [ ] <EOL> for subject in range ( <NUM_LIT:1> , <NUM_LIT> ) : <EOL> tseries = set ( TRAIN_SERIES ) <EOL> train_source = TrainSource ( subject , tseries ) <EOL> test_source = TestSource ( subject , TEST_SERIES , train_source ) <EOL> net = create_net ( train_source , test_source , max_epochs = max_epochs , <EOL> train_val_split = False ) <EOL> dummy = net . fit ( train_indices , train_indices ) <EOL> indices = np . arange ( len ( test_source . data ) ) <EOL> probs = net . predict_proba ( indices ) <EOL> print '<STR_LIT>' % ( bag , subject ) <EOL> probs_tot . append ( probs ) <EOL> probs_tot = np . concatenate ( probs_tot ) <EOL> probs_bags . append ( probs_tot ) <EOL> probs_bags = np . mean ( probs_bags , axis = <NUM_LIT:0> ) <EOL> np . save ( '<STR_LIT>' % fileName , [ probs_bags ] ) <EOL> prefix = '<STR_LIT>' if test else '<STR_LIT>' <EOL> end_time = time ( ) <EOL> report [ '<STR_LIT>' ] = end_time - start_time <EOL> report . to_csv ( "<STR_LIT>" % ( prefix , fileName ) ) <EOL> print report </s>
