def sum_stack(fn,sw): '''Measures relative illumination between frames. Sums the values of each frame, divides by the number of pixels, and removes the mean.''' dat, fs = retrieveData(fn[0],sw) nframes = dat.shape[3] print('processing image stack: %ix%i, %i frames ...' % (dat.shape[0], dat.shape[1], nframes)) s = zeros(nframes) for k in arange(nframes): s[k]=sum(dat[:,:,0,k]) numpix = float32(dat.shape[0]*dat.shape[1]) s = s/numpix s -= s.mean() if len(s.shape)==2: s=s.reshape(-1,) h = nmpdat.newHeader(fs=fs) nd = nmpdat.newData(s, h) doc = nmp.blankDocument() doc.newElement(nd) if sw.has_key('dir'): a = io.write(doc,sw['dir'] + '/' + sw['sum_file']) else: a = io.write(doc,sw['sum_file']) if a: print "%s successfully written." % sw['sum_file'] else: print "%s failed to write." % sw['sum_file'] return s, fs
def splitstack(fn,sw): if not sw.get('numframes'): print('loading image stack ...') dat,fs = retrieveData(fn[0],sw) print('writing image stack ...') f = newFile(dat,{'StackSpacing':1./fs,'SampleType': 'image','ParentFile':fn[0]}) if sw.has_key('AOS_dir'): fname=sw['AOS_dir']+'/'+sw['AOS_file'] else: fname=sw['AOS_file'] a1=io.write(f,fname) if a1: print "Wrote %s." % fname else: print "Failed to write %s." % fname if fn[1]: print "writing stimulus file..." if sw.get('startframe'): sf[0]=sw['startframe'] else: sf[0]=0 if sw.get('nframes'): sf[1] = sw['nframes'] dat, fs=cropbin(fn[1],sf,fs) h=nmpdat.newHeader(fs=fs) b=newFile(dat,h) if sw.has_key('bin_dir'): fname=sw['bin_dir']+'/'+sw['bin_file'] else: fname=sw['bin_file'] a2=io.write(b,fname) if a2: print "Wrote %s." % fname else: print "Failed to write %s." % fname else: n=sw['numframes'] if sw.get('jumpframes'): j=sw['jumpframes'] else: j=0 if sw.get('nframes'): nf=sw['nframes'] else: nf = 23000 # > max AOS buffer at 1000 Hz if sw.get('startframe'): sf=sw['startframe'] else: sf=0 if sw.has_key('AOS_dir'): basedig=getMaxDigit(sw['AOS_dir']) else: basedig=getMaxDigit(os.getcwd()) k=1 while sf + k*n + (k-1)*j <= nf: sp= sf + (k-1)*(n + j) kk='%03d' % (k+basedig) s={'startframe':sp, 'nframes':n} try: print('loading image stack ...') dat,fs,h = retrieveData(fn[0],s,header=True) if k == 1: nf = h['OriginalNumberofFrames'] print('writing image stack %03d...' %(k-1)) f = newFile(dat,h) if sw.has_key('AOS_dir'): fname=sw['AOS_dir']+'/'+kk+'_'+sw['AOS_file'] else: fname=kk+'_'+sw['AOS_file'] a1=io.write(f,fname) if a1: print "Wrote %s." % fname else: print "Failed to write %s." % fname if fn[1]: dat, fs, sp=cropbin(fn[1],(sp,n),fs) h={'SamplesPerSecond':fs,'OriginalFile':fn[1],'SampleType':'timeseries','StartTime':sp[0]/fs} print "writing stimulus file..." b=newFile(dat,h) if sw.has_key('bin_dir'): fname=sw['bin_dir']+'/'+kk+'_'+sw['bin_file'] else: fname=kk+'_'+sw['bin_file'] a2=io.write(b,fname) if a2: print "Wrote %s." % fname else: print "Failed to write %s." % fname except: print "error while processing %s or %s" % (fn[0],fn[1]) import traceback e= sys.exc_info() apply(traceback.print_exception, e) k+=1
def comp_series(fn,sw): '''Compares illumination of movie images to stimulus generator record running the LED.''' if 'n' in sw.keys(): s, fs = retrieveData(fn[0],sw) else: s, fs = sum_stack(fn,sw) if len(s.shape)==2 and s.shape[1] == 1: s=s.reshape(-1,) b, fsb = retrieveData(fn[1],sw) if 'e' in sw.keys(): es, esind = near_event_finder(s,fs,stdnum=5,numback=6) eb, ebind = near_event_finder(b[:,0],fsb,thresh=0.1) elif 'f' in sw.keys(): es, esind = subfs_event_finder(s,fs) eb, ebind = subfs_event_finder(b[:,0],fsb,5) else: es, esind = event_finder(s,fs) eb, ebind = event_finder(b[:,0],fsb,thresh=1) df = len(eb) - len(es) if df: z=zeros(df,s.dtype) es=hstack([es,z]) tdiff = es-eb #Start: code to ensure all events are detected bb = b[:,0] z = zeros_like(bb) z[ebind] = 1 btot = vstack([bb,z]).T hb = nmpdat.newHeader(fs=fsb, l=['RawStimulus','Events']) ndb = nmpdat.newData(btot, hb) docb = nmp.blankDocument() docb.newElement(ndb) if sw.has_key('dir'): a = io.write(docb,sw['dir'] + '/' + sw['stim_file']) else: a = io.write(docb,sw['stim_file']) if a: print "%s successfully written." % sw['stim_file'] else: print "%s failed to write." % sw['stim_file'] zs = zeros_like(s) zs[esind] = 1 stot = vstack([s,zs]).T hs = nmpdat.newHeader(fs=fs, l=['CameraSum','Events']) nds = nmpdat.newData(stot, hs) docs = nmp.blankDocument() docs.newElement(nds) if sw.has_key('dir'): a = io.write(docs,sw['dir'] + '/' + sw['cam_file']) else: a = io.write(docs,sw['cam_file']) if a: print "%s successfully written." % sw['cam_file'] else: print "%s failed to write." % sw['cam_file'] #End: code to ensure all events are detected tot = vstack([es,eb,tdiff]).T h = nmpdat.newHeader(fs=1.0, l=['VideoEvents','StimulusEvents','TimeDifference']) nd = nmpdat.newData(tot, h) doc = nmp.blankDocument() doc.newElement(nd) if sw.has_key('dir'): a = io.write(doc,sw['dir'] + '/' + sw['comp_file']) else: a = io.write(doc,sw['comp_file']) if a: print "%s successfully written." % sw['comp_file'] else: print "%s failed to write." % sw['comp_file']