def multi_iterator(dsarray, filter=None): from vista.set import MultiIterator, Constants if filter == None: iterator = MultiIterator(dsarray) else: iterator = MultiIterator(dsarray, filter) return iterator
def _test4(): from ANN import fnet_cccec sac = vutils.RegularTimeSeries('inp1','31jan1990 2400', '1mon', [10000.0, 11000.0, 12000.0, 15000.0]) sac = sac*5.40394e-06+0.178546 sjr = vutils.RegularTimeSeries('inp1','31jan1990 2400', '1mon', [1000.0, 1100.0, 1200.0, 1500.0]) sjr = sjr*1.34396e-05+0.199247 exp = vutils.RegularTimeSeries('inp1','31jan1990 2400', '1mon', [5000.0, 6000.0, 7000.0, 8000.0]) exp = exp*-4.86697e-05+0.178537 dxc = vutils.RegularTimeSeries('inp1','31jan1990 2400', '1mon', [0.0, 0.0, 0.0, 0.0]) dxc = (1.-dxc/31)*0.6+0.2 inps = [dxc,exp,sac,sjr] inps = map(lambda x: vutils.interpolate(x,'1day'), inps) inputs = [] for i in range(len(inps)): print 'Building inputs for ',inps[i] inputs=inputs+buildinput(inps[i],7,10,7) # weekly averages print 'Built inputs' #outputs = [] #ccc_ref = vutils.findpath(g2,'//ccc/ec///%s/'%fpart)[0] #outputs.append(vutils.interpolate(DataReference.create(ccc_ref,tw).getData(),'1day')) mi = MultiIterator(inputs) import jarray ninps = len(inputs) input = jarray.zeros(ninps,'f') output = jarray.zeros(1,'f') ann = fnet_cccec() ndata = len(inputs[0]) for input_no in range(31+28+31+1): mi.advance() outdata = jarray.zeros(ndata,'f') fh = open('junk.out','w',15000) while input_no < ndata: el = mi.getElement() i=0 while i < ninps: input[i] = el.getY(i) i=i+1 ann.engine(input,output,0) fh.write('Input #: %d\n'%input_no) i=0 while i < ninps: fh.write('%13.6f'%input[i]) if i%5 == 4: fh.write('\n') i = i+1 fh.write('\nOutput #: %d\n'%input_no) fh.write('%13.6f\n'%output[0]) outdata[input_no] = output[0] mi.advance() input_no=input_no+1 fh.close()
def _test3(): from ANN import fnet_cccec fpart = 'dxc-op' g1 = vutils.opendss('anninputs.dss') g2 = vutils.opendss('annoutputs.dss') g1.filterBy(fpart) g2.filterBy(fpart) refs = g1[:] tw = vutils.timewindow('01oct1975 0000 - 01sep1991 0000') #tw = vutils.timewindow('01oct1975 0000 - 01oct1976 0000') inps = [] for i in range(len(refs)): inps.append(vutils.interpolate(DataReference.create(refs[i],tw).getData(),'1day')) inputs = [] for i in range(len(inps)): print 'Building inputs for ',inps[i] inputs=inputs+buildinput(inps[i],7,10,7) # weekly averages print 'Built inputs' outputs = [] ccc_ref = vutils.findpath(g2,'//ccc/ec///%s/'%fpart)[0] outputs.append(vutils.interpolate(DataReference.create(ccc_ref,tw).getData(),'1day')) mi = MultiIterator(inputs) import jarray ninps = len(inputs) input = jarray.zeros(ninps,'f') output = jarray.zeros(1,'f') ann = fnet_cccec() ndata = len(inputs[0]) for input_no in range(365): mi.advance() outdata = jarray.zeros(ndata,'f') while input_no < ndata: el = mi.getElement() i=0 while i < ninps: input[i] = el.getY(i) i=i+1 ann.engine(input,output,0) outdata[input_no] = output[0] mi.advance() input_no=input_no+1 # stime = inputs[0].getStartTime() ti = inputs[0].getTimeInterval() rtsout = vutils.RegularTimeSeries('/ann/ccc_out/ec///annutils/',str(stime),\ str(ti),outdata) vutils.plot(rtsout,outputs[0]) rtsout = (rtsout-0.140516)/0.000396563 vutils.writedss('annout.dss','/ann/ccc_out/ec///annutils/',rtsout)
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()
function that exists in ANN package. The usual way to do this is to convert the SNNS generated "C" network to java using snns2java script. The generated java file is put in the ANN directory and compiled using "javac ann_func.java" command. inputs is the given inputs after being scaled and shifted for memory (Use buildinputs function for that) ''' try: exec("from ANN import %s"%ann_func) in locals(), globals() except AttributeError, err: raise '''No function: %s defined Please make sure that function exists in the $CLASSPATH/ANN directory and is compiled'''%ann_func ann_obj = eval('%s()'%ann_func) mi = MultiIterator(inputs) ninps = len(inputs) input = jarray.zeros(ninps,'f') output = jarray.zeros(nouts,'f') ndata = len(inputs[0]) #fh=open('%s_junk.data'%ann_func,'w') outarray = jarray.zeros(ndata,'f') input_no = 0 nan_filter = NaNFilter() nprint_intvl = ndata/10 nprints=0 while input_no < ndata: if input_no%nprint_intvl == 0: nprints=nprints+1 print 'Done: %d%%'%(nprints*10) el = mi.getElement()