<s> import numpy as np <EOL> import Image <EOL> import cPickle <EOL> from pynet . datasets . preprocessor import GCN <EOL> dir = '<STR_LIT>' <EOL> base = '<STR_LIT>' <EOL> def cal_min ( ) : <EOL> s = open ( dir + '<STR_LIT>' % base ) <EOL> s = np . fromfile ( s , dtype = '<STR_LIT>' ) <EOL> s = s . reshape ( ( s . shape [ <NUM_LIT:0> ] / <NUM_LIT> , <NUM_LIT> ) ) <EOL> m = open ( dir + '<STR_LIT>' % base ) <EOL> m = np . fromfile ( m , dtype = '<STR_LIT>' ) <EOL> m = m . reshape ( ( m . shape [ <NUM_LIT:0> ] / <NUM_LIT> , <NUM_LIT> ) ) <EOL> diff = m . shape [ <NUM_LIT:0> ] - s . shape [ <NUM_LIT:0> ] <EOL> print '<STR_LIT>' , diff <EOL> print '<STR_LIT>' , ( s ** <NUM_LIT:2> ) . mean ( ) <EOL> print '<STR_LIT>' , ( s ** <NUM_LIT:2> ) . mean ( ) <EOL> error = [ ] <EOL> min = <NUM_LIT> <EOL> min_i = <NUM_LIT:0> <EOL> if diff > <NUM_LIT:0> : <EOL> for i in range ( diff ) : <EOL> e = ( ( m [ i : i - diff ] - s ) ** <NUM_LIT:2> ) . mean ( ) <EOL> if e < min : <EOL> min = e <EOL> min_i = i <EOL> print i , e <EOL> sp = Image . fromarray ( s * <NUM_LIT:1000> ) <EOL> sp . save ( dir + '<STR_LIT>' % base ) <EOL> mgc = Image . fromarray ( m [ min_i : min_i - diff ] * <NUM_LIT:1000> ) <EOL> mgc . save ( dir + '<STR_LIT>' % base ) <EOL> elif diff < <NUM_LIT:0> : <EOL> diff = abs ( diff ) <EOL> for i in range ( diff ) : <EOL> e = ( ( s [ i : i - diff ] - m ) ** <NUM_LIT:2> ) . mean ( ) <EOL> if e < min : <EOL> min = e <EOL> min_i = i <EOL> print i , e <EOL> sp = Image . fromarray ( s [ min_i : min_i - diff ] * <NUM_LIT:1000> ) <EOL> sp . save ( dir + '<STR_LIT>' % base ) <EOL> mgc = Image . fromarray ( m * <NUM_LIT:1000> ) <EOL> mgc . save ( dir + '<STR_LIT>' % base ) <EOL> print '<STR_LIT>' , min_i , min <EOL> names = [ '<STR_LIT>' ] <EOL> def draw_gif ( ) : <EOL> for name in names : <EOL> sp_fin = open ( dir + '<STR_LIT>' % name ) <EOL> sp = np . fromfile ( sp_fin , dtype = '<STR_LIT>' ) <EOL> sp = sp . reshape ( ( sp . shape [ <NUM_LIT:0> ] / <NUM_LIT> , <NUM_LIT> ) ) <EOL> sp = Image . fromarray ( sp * <NUM_LIT:1000> ) <EOL> sp . save ( dir + '<STR_LIT>' % name ) <EOL> mgc_fin = open ( dir + '<STR_LIT>' % name ) <EOL> mgc = np . fromfile ( mgc_fin , dtype = '<STR_LIT>' ) <EOL> mgc = mgc . reshape ( ( mgc . shape [ <NUM_LIT:0> ] / <NUM_LIT> , <NUM_LIT> ) ) <EOL> mgc = Image . fromarray ( mgc * <NUM_LIT:1000> ) <EOL> mgc . save ( dir + '<STR_LIT>' % name ) <EOL> def generate_spec ( model , mlp , mgc , out_spec , fea_size ) : <EOL> fin = open ( model ) <EOL> print '<STR_LIT>' <EOL> ae = cPickle . load ( fin ) <EOL> fin . close ( ) <EOL> fin = open ( mlp ) <EOL> print '<STR_LIT>' <EOL> mlp = cPickle . load ( fin ) <EOL> fin . close ( ) <EOL> mgc_fin = open ( mgc ) <EOL> print '<STR_LIT>' , mgc <EOL> mgc = np . fromfile ( mgc_fin , dtype = '<STR_LIT>' ) <EOL> assert mgc . shape [ <NUM_LIT:0> ] % fea_size == <NUM_LIT:0> <EOL> mgc = mgc . reshape ( ( mgc . shape [ <NUM_LIT:0> ] / fea_size , fea_size ) ) <EOL> print '<STR_LIT>' , mgc . shape <EOL> decoded = ae . decode ( mgc ) <EOL> normalizer = mlp . fprop ( decoded ) <EOL> print '<STR_LIT>' , normalizer . shape <EOL> warp_spec = decoded * normalizer <EOL> fout = open ( out_spec , '<STR_LIT:wb>' ) <EOL> print '<STR_LIT>' <EOL> warp_spec . tofile ( fout , format = '<STR_LIT>' ) <EOL> mgc_fin . close ( ) <EOL> fout . close ( ) <EOL> print '<STR_LIT>' <EOL> def reconstruct_spec ( mlp , spec , outfile ) : <EOL> fin = open ( mlp ) <EOL> print '<STR_LIT>' <EOL> mlp = cPickle . load ( fin ) <EOL> fin . close ( ) <EOL> warp_spec = open ( spec ) <EOL> warp_spec = np . fromfile ( warp_spec , dtype = '<STR_LIT>' ) <EOL> assert warp_spec . shape [ <NUM_LIT:0> ] % <NUM_LIT> == <NUM_LIT:0> <EOL> warp_spec = warp_spec . reshape ( ( warp_spec . shape [ <NUM_LIT:0> ] / <NUM_LIT> , <NUM_LIT> ) ) <EOL> gcn = GCN ( ) <EOL> out = gcn . apply ( warp_spec ) <EOL> out = out * mlp . fprop ( out ) <EOL> output_file = open ( outfile , '<STR_LIT:wb>' ) <EOL> out . tofile ( output_file , format = '<STR_LIT>' ) <EOL> def mask_spec ( spec , dir ) : <EOL> warp_fin = open ( spec ) <EOL> warp_spec = np . fromfile ( warp_fin , dtype = '<STR_LIT>' ) <EOL> warp_fin . close ( ) <EOL> rd = np . random . binomial ( <NUM_LIT:1> , <NUM_LIT:0.5> , warp_spec . shape ) <EOL> mask_spec = warp_spec * rd <EOL> fout = open ( dir + '<STR_LIT>' , '<STR_LIT:wb>' ) <EOL> mask_spec = mask_spec . astype ( '<STR_LIT>' ) <EOL> mask_spec . tofile ( fout , format = '<STR_LIT>' ) <EOL> fout . close ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> spec = '<STR_LIT>' <EOL> dir = '<STR_LIT>' <EOL> mask_spec ( spec , dir ) </s>
