def dump2text(outfile,rtsarray,filter=Constants.DEFAULT_FLAG_FILTER): """ """ xfilter = CompositeFilter([filter,NaNFilter()]) mi = MultiIterator(rtsarray) fh = open(outfile,'w') for i in range(len(rtsarray)): fh.write('%s\n'%rtsarray[i].getName()) while not mi.atEnd(): # el = mi.getElement() fh.write(element2text(el,xfilter)) # mi.advance() fh.close()
def dump2text(outfile, rtsarray, filter=Constants.DEFAULT_FLAG_FILTER): """ """ xfilter = CompositeFilter([filter, NaNFilter()]) mi = MultiIterator(rtsarray) fh = open(outfile, 'w') for i in range(len(rtsarray)): fh.write('%s\n' % rtsarray[i].getName()) while not mi.atEnd(): # el = mi.getElement() fh.write(element2text(el, xfilter)) # mi.advance() fh.close()
def dump_patterns(inputs,outputs,outfile,calibration_percentage=0.75,skip = 365,append=0, fast_write=0): ''' dump_patterns(inputs,outputs,outfile,calibration_percentage=0.75,append=0): dumps the patterns to the given outfile. outfile.cal which contains calibration% of the points outfile.ver which contains validation% = 100 - calibration% of the points append=1 appends to the previous .cal and .ver files if they exist ''' bufsize=15000 #fast_write=0 prev_no_patterns = [0,0] if append: cfh = open(outfile+'.cal','r+',bufsize) vfh = open(outfile+'.ver','r+',bufsize) id = 0 for fh in [cfh,vfh]: while 1: line = string.strip(fh.readline()) if string.find(line,'No. of patterns') >=0: prev_no_patterns[id] = int(line[string.find(line,':')+1:]) if string.find(line,'Input pattern')>=0: break fh.seek(0) id = id + 1 else: if fast_write: cfh = PrintStream(BufferedOutputStream(FileOutputStream(outfile+'.cal'))) vfh = PrintStream(BufferedOutputStream(FileOutputStream(outfile+'.ver'))) else: cfh = open(outfile+'.cal','w',bufsize) vfh = open(outfile+'.ver','w',bufsize) # #if append: # print 'Prev # of patterns = %s'%prev_no_patterns npatterns = len(inputs[0]) - skip # skip beginning #print 'Time Window of inputs[0] %s'%str(inputs[0].getTimeWindow()) #print npatterns,len(inputs[0]),skip ncalibs = calibration_percentage*npatterns ninputs = len(inputs) noutputs = len(outputs) #print 'Ncalibs: %d & Nverse: %d'%(ncalibs,npatterns-ncalibs) if append or not fast_write: cfh.write('''SNNS pattern definition file V3.2 generated at date : %s No. of patterns : %d No. of input units : %d No. of output units : %d '''%(time.ctime(time.time()),ncalibs+prev_no_patterns[0],ninputs,noutputs)) vfh.write('''SNNS pattern definition file V3.2 generated at date : %s No. of patterns : %d No. of input units : %d No. of output units : %d '''%(time.ctime(time.time()),npatterns-ncalibs+prev_no_patterns[1],ninputs,noutputs)) else: cfh.print('''SNNS pattern definition file V3.2 generated at date : %s No. of patterns : %d No. of input units : %d No. of output units : %d '''%(time.ctime(time.time()),ncalibs+prev_no_patterns[0],ninputs,noutputs)) vfh.print('''SNNS pattern definition file V3.2 generated at date : %s No. of patterns : %d No. of input units : %d No. of output units : %d '''%(time.ctime(time.time()),npatterns-ncalibs+prev_no_patterns[1],ninputs,noutputs)) # #goto end of file if append: for fh in [cfh,vfh]: if fast_write: fh.close() else: fh.seek(0,2) if fast_write: cfh = PrintStream(BufferedOutputStream(FileOutputStream(outfile+'.cal',1))) vfh = PrintStream(BufferedOutputStream(FileOutputStream(outfile+'.ver',1))) # i=0 all = inputs + outputs iterator = MultiIterator(all) npattern = 1 nskip = 0 nprint_intvl = npatterns/10 nprints = 0 while not iterator.atEnd(): if npattern%nprint_intvl == 0: nprints = nprints + 1 print 'Done: %d%%'%(nprints*10) if nskip < skip: nskip=nskip+1 iterator.advance() continue if npattern > npatterns: break el = iterator.getElement() j=0 #print el if npattern <= ncalibs: fh = cfh pattern_no = npattern prev_pattern_no = prev_no_patterns[0] else: fh = vfh pattern_no = npattern - ncalibs prev_pattern_no = prev_no_patterns[1] if fast_write: fh.println() fh.println('# Input pattern %d:'%(pattern_no+prev_pattern_no)) else: fh.write('\n# Input pattern %d:\n'%(pattern_no+prev_pattern_no)) while j < ninputs: if fast_write: fh.print('%10.6f'%el.getY(j)) else: fh.write('%10.6f'%el.getY(j)) j=j+1 if fast_write: fh.println() fh.println('# Output pattern %d:'%(pattern_no+prev_pattern_no)) else: fh.write('\n# Output pattern %d:\n'%(pattern_no+prev_pattern_no)) while j < ninputs+noutputs: if fast_write: fh.print('%10.6f'%el.getY(j)) else: fh.write('%10.6f'%el.getY(j)) j=j+1 if fast_write: fh.println() else: fh.write('\n') npattern = npattern+1 iterator.advance() # cfh.close() vfh.close()