def diagnostics(path, maskset): ''' Compute the standard deviation in a speckle measurement ''' mask = maskset.mask rawrefpix = fdio.read(os.path.join(path, '0.rawframe')) refcube = combine.separate_pixelated(rawrefpix) rawpix = fdio.read(os.path.join(path, '1.rawframe')) phasecube = combine.separate_pixelated(rawpix) diffim = doit_speckle(os.path.join(path, '1.rawframe'), maskset, refcube, 0, output=False)[0] mdiffim = diffim[maskset.mask] sat = (rawpix > 250).sum() b0, b90, b180, b270 = refcube m0, m90, m180, m270 = phasecube Nm = m270 - m90 Dm = m0 - m180 Nb = b270 - b90 Db = b0 - b180 numerator = Nm * Db - Dm * Nb denominator = Dm * Db + Nm * Nb nmaskpoints = mask.astype(np.int32).sum() average_intensity = ( (refcube + phasecube).sum(axis=0) * mask).sum() / (2 * nmaskpoints) modulation = np.sqrt(numerator**2 + denominator**2) / 2 avg_mod = (modulation * mask).sum() / nmaskpoints return mdiffim.std(), sat, avg_mod / average_intensity
def doit_speckle(inpath, maskset, refcube, prevrefphase, windowsize=5, output=True): ''' take a single raw image, compute wrapped phase, spatially unwrap, temporally unwrap, fit zernikes, save unwrapped image, zernike results and reference point value to disk. ''' if output: print("processing ", inpath, windowsize) center = maskset.center rawframe = fdio.read(inpath) cube = combine.separate_pixelated(rawframe) phase = calcPhaseDiff(refcube, cube, windowsize) unwrapped = unwrap(phase) * maskset.mask refphase = (unwrapped * maskset.mask).sum() / maskset.mask.astype( np.int64).sum() phasecorr = temporal_correction(prevrefphase, refphase) unwrapped -= phasecorr y, x = maskset.indices radius = maskset.radius zcoeff = zernike.fit_gzernike(x, y, unwrapped[maskset.indices], 10, center, radius) return unwrapped, zcoeff, unwrapped[center[1], center[0]]
def sdiff(maskset): refcube = combine.separate_pixelated( fdio.read(os.path.join('.', '0.rawframe'))) diffim = doit_speckle(os.path.join('.', '1.rawframe'), maskset, refcube, 0, output=False)[0] return diffim
def doall_speckle_rebaseline(inpath, maskfile, rebaseline=1000): ''' Here inpath is the basic directory that contains all the associated data. /raw is appended to it to get to the raw data. Results are stored in the results subdirectory. ''' globpat = os.path.join(inpath, '*.rawframe') filelist = glob.glob(globpat) # filelist = glob.glob(os.path.join(inpath, 'raw/frame_*.dat')) # Given the variable number length, sorting is more complex filenum = [int(os.path.split(item)[1].split('.')[0]) for item in filelist] print(filenum) zlist = list(zip(filenum, filelist)) zlist.sort() filelist = [item[1] for item in zlist] print(filelist) mask = hsio.read(maskfile)[1] maskset = MaskSet(mask, center=(240, 240), radius=180) outdir = os.path.join(inpath, '000_results') if not os.path.exists(outdir): os.mkdir(outdir) print(outdir) zlist = [] niter = len(filelist) // rebaseline print('niter:', niter) tfilelist = filelist.copy() tfilelist.append('bozo') # just to allow slicing to work on the last iter for i in range(niter): for j in range(5): print(50 * '*') rfilelist = tfilelist[i * rebaseline:(i + 1) * rebaseline + 1] rawref = fdio.read(rfilelist[0]) refcube = combine.separate_pixelated(rawref) prevrefphase = 0 zmatrix = np.zeros((rebaseline + 1, 10)) zresfile = open(outdir + '/zernikes.txt', 'w') for i, rfile in enumerate(rfilelist): if rfile == 'bozo': break print(i, rfile) uim, zcoeff, rphase = doit_speckle(rfile, maskset, refcube, prevrefphase) zmatrix[i] = zcoeff zlist.append(zmatrix) # now splice segments together szmatrix = np.zeros((len(filelist) + 1, 10)) szmatrix[0:rebaseline + 1, :] = zlist[0] for i in range(niter - 1): szmatrix[(i + 1) * rebaseline:(i + 2) * rebaseline + 1, :] = szmatrix[(i + 1) * rebaseline, :] + zlist[i + 1] szresfile = open(outdir + '/szernikes.txt', 'w') for i in range(len(filelist)): szresfile.write(10 * '%e,' % tuple(szmatrix[i, :]) + '\n') szresfile.close()
def doall_speckle(inpath, maskset, range=None, suffix=''): ''' Here inpath is the basic directory that contains all the associated data. /raw is appended to it to get to the raw data. Results are stored in the results subdirectory. ''' globpat = os.path.join(inpath, '*.rawframe') filelist = glob.glob(globpat) # filelist = glob.glob(os.path.join(inpath, 'raw/frame_*.dat')) # Given the variable number length, sorting is more complex filenum = [int(os.path.split(item)[1].split('.')[0]) for item in filelist] print(filenum) zlist = list(zip(filenum, filelist)) zlist.sort() filelist = [item[1] for item in zlist] if range is not None: filelist = filelist[range[0]:range[1]] foffset = range[0] else: foffset = 0 reffile = filelist[0] print(filelist) mask = maskset.mask #mask = hsio.read(maskfile)[1] rawref = fdio.read(os.path.join(inpath, '0.rawframe')) refcube = combine.separate_pixelated(rawref) outdir = os.path.join(inpath, '000_results') if not os.path.exists(outdir): os.mkdir(outdir) print(outdir) # maskset = MaskSet(mask, center=(240, 240), radius=180) prevrefphase = 0 zmatrix = np.zeros((len(filelist), 10)) refphase = np.zeros((len(filelist), )) if range is None: zresfile = open(outdir + '/zernikes{}.txt'.format(suffix), 'w') presfile = open(outdir + '/phasetimeseries{}.txt'.format(suffix), 'w') else: zresfile = open(outdir + '/p_zernikes%s%03d.txt' % (suffix, foffset), 'w') presfile = open( outdir + '/p_phasetimeseries%s%03d.txt' % (suffix, foffset), 'w') for i, rfile in enumerate(filelist): print(i + foffset, rfile) uim, zcoeff, rphase = doit_speckle(rfile, maskset, refcube, prevrefphase) zmatrix[i] = zcoeff refphase[i] = rphase zresfile.write(10 * '%e,' % tuple(zcoeff) + '\n') presfile.write(str(rphase) + '\n') zresfile.close() presfile.close()
def doit_new(inpath, maskset, prevrefphase=None): ''' take a single raw image, compute wrapped phase, spatially unwrap, temporally unwrap, fit zernikes, save unwrapped image, zernike results and reference point value to disk. ''' print("processing ", inpath) center = maskset.center rawframe = fdio.read(inpath) unwrapped = unwrap_raw_frame(rawframe, mask=maskset.mask) refphase = unwrapped[center[1], center[0]] phasecorr = temporal_correction(prevrefphase, refphase) unwrapped -= phasecorr y, x = maskset.indices radius = maskset.radius zcoeff = zernike.fit_gzernike(x, y, unwrapped[maskset.indices], 10, center, radius) return unwrapped, zcoeff, unwrapped[center[1], center[0]]