<s> from __future__ import print_function <EOL> __author__ = "<STR_LIT>" <EOL> from numpy import r_ , array , isfinite <EOL> from pybrain . datasets import SequentialDataSet <EOL> def convertSequenceToTimeWindows ( DSseq , NewClass , winsize ) : <EOL> """<STR_LIT>""" <EOL> assert isinstance ( DSseq , SequentialDataSet ) <EOL> DSwin = NewClass ( DSseq . indim * winsize , DSseq . outdim ) <EOL> nsamples = <NUM_LIT:0> <EOL> nseqs = <NUM_LIT:0> <EOL> si = r_ [ DSseq [ '<STR_LIT>' ] . flatten ( ) , DSseq . endmarker [ '<STR_LIT>' ] ] <EOL> for i in range ( DSseq . getNumSequences ( ) ) : <EOL> input = DSseq [ '<STR_LIT:input>' ] [ si [ i ] : si [ i + <NUM_LIT:1> ] , : ] <EOL> target = DSseq [ '<STR_LIT:target>' ] [ si [ i ] : si [ i + <NUM_LIT:1> ] , : ] <EOL> nseqs += <NUM_LIT:1> <EOL> for k in range ( winsize , input . shape [ <NUM_LIT:0> ] , winsize ) : <EOL> inp_win = input [ k - winsize : k , : ] <EOL> tar_win = target [ k - <NUM_LIT:1> , : ] <EOL> DSwin . addSample ( inp_win . flatten ( ) , tar_win . flatten ( ) ) <EOL> nsamples += <NUM_LIT:1> <EOL> print ( ( "<STR_LIT>" , len ( DSseq ) ) ) <EOL> print ( ( "<STR_LIT>" , winsize * nsamples ) ) <EOL> print ( ( "<STR_LIT>" , len ( DSseq ) * DSseq . indim ) ) <EOL> print ( ( "<STR_LIT>" , len ( DSwin ) * DSwin . indim ) ) <EOL> return DSwin <EOL> def windowSequenceEval ( DS , winsz , result ) : <EOL> """<STR_LIT>""" <EOL> si_old = <NUM_LIT:0> <EOL> idx = <NUM_LIT:0> <EOL> x = [ ] <EOL> y = [ ] <EOL> seq_res = [ ] <EOL> for i , si in enumerate ( DS [ '<STR_LIT>' ] [ <NUM_LIT:1> : ] . astype ( int ) ) : <EOL> tar = DS [ '<STR_LIT:target>' ] [ si - <NUM_LIT:1> ] <EOL> curr_x = si_old <EOL> correct = <NUM_LIT:0.> <EOL> wrong = <NUM_LIT:0.> <EOL> while curr_x < si : <EOL> x . append ( curr_x ) <EOL> if result [ idx ] == tar : <EOL> correct += <NUM_LIT:1.> <EOL> y += [ <NUM_LIT:1.> , <NUM_LIT:1.> ] <EOL> else : <EOL> wrong += <NUM_LIT:1.> <EOL> y += [ <NUM_LIT:0.> , <NUM_LIT:0.> ] <EOL> idx += <NUM_LIT:1> <EOL> curr_x += winsz <EOL> x . append ( curr_x ) <EOL> seq_res . append ( <NUM_LIT> * correct / ( correct + wrong ) ) <EOL> print ( ( "<STR_LIT>" % ( i , seq_res [ - <NUM_LIT:1> ] ) ) ) <EOL> seq_res = array ( seq_res ) <EOL> print ( ( "<STR_LIT>" , <NUM_LIT> * float ( ( seq_res >= <NUM_LIT:0.5> ) . sum ( ) ) / seq_res . size ) ) <EOL> class DataSetNormalizer ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , fname = None , meanstd = False ) : <EOL> self . dim = <NUM_LIT:0> <EOL> self . meanstd = meanstd <EOL> if fname is not None : <EOL> self . load ( fname ) <EOL> def load ( self , fname ) : <EOL> f = open ( fname ) <EOL> c = [ ] <EOL> x = f . readline ( ) <EOL> self . meanstd = False if x == '<STR_LIT:x>' else True <EOL> bounds = array ( f . readline ( ) . split ( ) ) . astype ( float ) <EOL> for line in f : <EOL> c . append ( array ( line . split ( ) ) . astype ( float ) [ <NUM_LIT:1> : ] ) <EOL> self . dim = len ( c ) <EOL> c = array ( c ) <EOL> self . par1 = c [ : , <NUM_LIT:0> ] <EOL> self . par2 = c [ : , <NUM_LIT:1> ] <EOL> self . scale = ( bounds [ <NUM_LIT:1> ] - bounds [ <NUM_LIT:0> ] ) / ( c [ : , <NUM_LIT:1> ] - c [ : , <NUM_LIT:0> ] ) <EOL> self . newmin = bounds [ <NUM_LIT:0> ] <EOL> self . newmax = bounds [ <NUM_LIT:1> ] <EOL> def save ( self , fname ) : <EOL> f = open ( fname , "<STR_LIT>" ) <EOL> f . write ( '<STR_LIT>' ) <EOL> f . write ( '<STR_LIT>' % ( self . newmin , self . newmax ) ) <EOL> for i in range ( self . dim ) : <EOL> f . write ( '<STR_LIT>' % ( i + <NUM_LIT:1> , self . par1 [ i ] , self . par2 [ i ] ) ) <EOL> f . close ( ) <EOL> def normalizePattern ( self , y ) : <EOL> return ( y - self . par1 ) * self . scale + self . newmin <EOL> def normalize ( self , ds , field = '<STR_LIT:input>' ) : <EOL> """<STR_LIT>""" <EOL> if self . dim <= <NUM_LIT:0> : <EOL> raise IndexError ( "<STR_LIT>" ) <EOL> dsdim = ds [ field ] . shape [ <NUM_LIT:1> ] <EOL> if self . dim != dsdim : <EOL> raise IndexError ( "<STR_LIT>" ) <EOL> newfeat = ds [ field ] <EOL> if self . meanstd : <EOL> for i in range ( dsdim ) : <EOL> divisor = self . par2 [ i ] if self . par2 [ i ] > <NUM_LIT:0> else <NUM_LIT:1.0> <EOL> newfeat [ : , i ] = ( newfeat [ : , i ] - self . par1 [ i ] ) / divisor <EOL> else : <EOL> for i in range ( dsdim ) : <EOL> scale = self . scale [ i ] if isfinite ( self . scale [ i ] ) else <NUM_LIT:1.0> <EOL> newfeat [ : , i ] = ( newfeat [ : , i ] - self . par1 [ i ] ) * scale + self . newmin <EOL> ds . setField ( field , newfeat ) <EOL> def calculate ( self , ds , bounds = [ - <NUM_LIT:1> , <NUM_LIT:1> ] , field = '<STR_LIT:input>' ) : <EOL> self . dim = ds [ field ] . shape [ <NUM_LIT:1> ] <EOL> if self . meanstd : <EOL> self . par1 = ds [ field ] . mean ( axis = <NUM_LIT:0> ) <EOL> self . par2 = ds [ field ] . std ( axis = <NUM_LIT:0> ) <EOL> else : <EOL> self . par1 = ds [ field ] . min ( axis = <NUM_LIT:0> ) <EOL> self . par2 = ds [ field ] . max ( axis = <NUM_LIT:0> ) <EOL> self . scale = ( bounds [ <NUM_LIT:1> ] - bounds [ <NUM_LIT:0> ] ) / ( self . par2 - self . par1 ) <EOL> self . newmin = bounds [ <NUM_LIT:0> ] <EOL> self . newmax = bounds [ <NUM_LIT:1> ] </s>
