def calc_localization_Z_positions(self, cal_files, tol, maxfev): def calc_z(mol, cal, init, tol, maxfev): def fun(z, *args): ''' args = [const, A, B, c, d] ''' z0 = (z-args[3])/args[4] return args[0]+(z0)**2+\ args[1]*(z0)**3+\ args[2]*(z0)**4 ax = mol['ax'] w = mol['w'] wx = w/np.sqrt(ax) wy = w*np.sqrt(ax) const_x = 1-(wx/cal['wx0'])**2 const_y = 1-(wy/cal['wy0'])**2 xargs = (const_x, cal['Ax'], cal['Bx'], cal['gx'], cal['zrx']) yargs = (const_y, cal['Ay'], cal['By'], cal['gy'], cal['zry']) fx = optimize.fsolve(fun, [init,init,init,init], args=xargs, xtol=tol, maxfev=maxfev) fy = optimize.fsolve(fun, [init,init,init,init], args=yargs, xtol=tol, maxfev=maxfev) root_diff = np.argmin(np.abs(fx-fy)) return np.mean([fx[root_diff],fy[root_diff]]) cals = [] for file in cal_files: with open(file, 'r') as f: params = f.readlines()[0].split(';') f.close() _cal = {} for p in params: _cal[p.split('=')[0]]=float(p.split('=')[1]) cals.append(_cal) i3in = readinsight3.I3Reader(self.i3TestCombSVCBinName) num_frames = i3in.getNumberFrames() i3out = writeinsight3.I3Writer(self.i3TestZCombSVCBinNames, frames=num_frames) mols_in = i3in.getMoleculesInFrameRange(0,num_frames+1) mols_out = i3in.getMoleculesInFrameRange(0,num_frames+1) start_time = timer() n_lasers = np.sum(self.lasers) cats = [] for n in range(n_lasers): cats.append(len([mol for mol in mols_in if mol['c']==n])) # print(cats) indices = np.zeros(np.sum(self.lasers)).astype(np.uint32) diffs = [np.zeros(c) for c in cats] for idx, mol in enumerate(mols_in): if idx%10000 == 0: stop_time = timer() print('%d of %d completed in %f seconds.' % (idx, mols_in.shape[0], stop_time-start_time)) start_time = timer() z = calc_z(mol, cals[mol['c']], mol['z'], tol, maxfev) mols_out[idx]['z'] = z diffs[mol['c']][indices[mol['c']]] = z - mol['z'] indices[mol['c']]+=1 for i in range(len(cats)): print('Localization shift, ch%d (mean): %f' % (i, np.mean(diffs[i]))) print('Localization shift, ch%d (std.): %f' % (i, np.std(diffs[i]))) i3out.addMolecules(mols_out) i3out.close() return True
def merge_SVC_bin_files(self): i3multi = readinsight3.I3Reader(self.i3TestIndivSVCBinNames['multi']) i3single = readinsight3.I3Reader(self.i3TestIndivSVCBinNames['single']) num_frames = int(np.max([i3multi.getNumberFrames(),i3single.getNumberFrames()])) i3out = writeinsight3.I3Writer(self.i3TestCombSVCBinName, frames=num_frames) im1 = i3multi.getMoleculesInFrameRange(0,num_frames+1) is1 = i3single.getMoleculesInFrameRange(0,num_frames+1) n_mol0 = im1.shape[0] n_mol1 = is1.shape[0] i3new = i3dtype.createDefaultI3Data(n_mol0+n_mol1) for i in range(n_mol0): i3new[i] = im1[i] for j in range(n_mol1): i3new[n_mol0+j] = is1[j] i3new = np.sort(i3new,order=['fr','x','y']) i3out.addMoleculesWithXYZHCatFrame(i3new['x'],i3new['y'],i3new['z'],i3new['h'],i3new['c'],i3new['fr']) i3multi.close() i3single.close() i3out.close() im1 = None is1 = None i3new = None return True
def __init__(self, filename, scale=4, verbose=True): I3GGeneric.__init__(self, filename, scale=scale, verbose=verbose) self.i3_in = readinsight3.I3Reader(filename) self.i3data = self.i3_in.nextBlock() self.resetFp() # Determine film size. [image_x, image_y, self.film_l] = getFilmSize(filename, self.i3data) self.im_size = [image_x, image_y] # Determine what channels the image has. self.channels = [] for i in range(10): mask = (self.i3data['c'] == i) if mask.sum() > 0: self.channels.append(i)
def demodulate_individual_localizations(self, chunk_size=1000, f_type='single', block_size=None, lasers=None): daxIn = DAX.DAX() daxIn.open(self.nobg_DAX_name) _num_frames = daxIn.numFrames _im_size = daxIn.height if block_size == None: block_size = self.block_size if lasers == None: lasers = self.lasers _chunk_size = chunk_size*block_size _num_chunks = _num_frames//(_chunk_size) _extra_frames = _num_frames%(_chunk_size) print(_chunk_size,_num_chunks,_extra_frames,_chunk_size*_num_chunks+_extra_frames) i3in = readinsight3.I3Reader(self.i3TypeIndivBinNames[f_type]) locs = i3in.getMoleculesInFrameRange(0,_num_frames+1) demod_locs = np.zeros([locs.shape[0],4+np.sum(lasers)]) idx_dm = 0 data = np.zeros([_chunk_size+block_size, _im_size, _im_size]).astype(np.uint16) for i in range(_num_chunks): t_locs = i3dtype.maskData(locs,locs['fr']>=_chunk_size*i+1) i_locs = i3dtype.maskData(t_locs,t_locs['fr']<_chunk_size*(i+1)+1) start_time = timer() for j in range(_chunk_size+block_size): data[j] = daxIn.read(_chunk_size*i+j) curr_locs = idx_dm for frame in range(_chunk_size): _f = frame+_chunk_size*i l_locs = i3dtype.maskData(i_locs,i_locs['fr']==_f+1) l_data = data[frame:frame+block_size] temp_locs = local_dft.calc_indiv_demods_new(l_locs,l_data,lasers,_im_size) demod_locs[idx_dm:idx_dm+temp_locs.shape[0]] = temp_locs idx_dm += temp_locs.shape[0] stop_time = timer() new_locs = idx_dm - curr_locs print('Chunk %d of %d complete in %d seconds (%d localizations added).' % (i+1, _num_chunks, stop_time-start_time, new_locs)) start_time = timer() for j in range(_extra_frames): data[j] = daxIn.read(_chunk_size*_num_chunks+j) curr_locs = idx_dm i_locs = i3dtype.maskData(locs,locs['fr']>=_chunk_size*_num_chunks+1) for frame in range(_extra_frames-block_size): _f = frame+_num_chunks*_chunk_size l_locs = i3dtype.maskData(i_locs,i_locs['fr']==_f+1) l_data = data[frame:frame+block_size] temp_locs = local_dft.calc_indiv_demods_new(l_locs,l_data,lasers,_im_size) demod_locs[idx_dm:idx_dm+temp_locs.shape[0]] = temp_locs idx_dm += temp_locs.shape[0] stop_time = timer() new_locs = idx_dm - curr_locs print('Extra frames complete in %d seconds (%d localizations added).' % (stop_time-start_time, new_locs)) i3in.close() daxIn.close() i3out = writeinsight3.I3Writer(self.i3CatIndivBinNames[f_type],_num_frames-1) i3out.addMoleculesWithXYZF(demod_locs[:,0],demod_locs[:,1],demod_locs[:,2],demod_locs[:,3]) i3out.close() self.write_to_indiv_ints(demod_locs, f_type) return True
def individual_are_single_or_multi(self, block_size=None, dist_thresh=0.5): if block_size == None: block_size = self.block_size # Insight3 .bin reader for the median-subtracted localizations i3inNobg = readinsight3.I3Reader(self.i3inNobgBinName) num_frames = i3inNobg.getNumberFrames() single = np.zeros([i3inNobg.getNumMols(),4]) multi = np.zeros([i3inNobg.getNumMols()//2,4]) print('Opened',self.i3inNobgBinName,'for reading background-subtracted localizations.') start_time = timer() s_idx = 0 m_idx = 0 for frame in range(num_frames//block_size): dists_th = np.array([False]) molsNobg = i3inNobg.getMoleculesInFrameRange(block_size*frame,\ block_size*(frame+1)) molsNobg = np.sort(molsNobg, order=['fr','x','y']) molsNobg_xy = np.array([molsNobg['x'],molsNobg['y']]).T dists = distance.squareform(distance.pdist(molsNobg_xy)) dists_th = dists<dist_thresh num_mols = molsNobg.shape[0] multis = [] for i in range(num_mols): if i in multis: pass else: loc = np.array([molsNobg[i]['x'],molsNobg[i]['y']]) div = 1 for j in range(i+1,num_mols): if dists_th[i][j]: div+=1 if i not in multis: multis.append(i) if j not in multis: multis.append(j) loc = np.sum([loc,[molsNobg[j]['x'],molsNobg[j]['y']]],axis=0) loc /= div if i in multis: multi[m_idx] = [molsNobg[i]['x'],molsNobg[i]['y'],molsNobg[i]['z'],molsNobg[i]['fr']] m_idx+=1 else: single[s_idx] = [molsNobg[i]['x'],molsNobg[i]['y'],molsNobg[i]['z'],molsNobg[i]['fr']] s_idx+=1 i3inNobg.close() print('Localization classification completed in %f seconds.' % (timer()-start_time)) i3outSingle = writeinsight3.I3Writer(self.i3TypeIndivBinNames['single'], frames=num_frames-1) print('Opened',self.i3TypeIndivBinNames['single'],'for writing single-frame localizations.') i3outSingle.addMoleculesWithXYZF(single[:s_idx,0],single[:s_idx,1],single[:s_idx,2],single[:s_idx,3]) i3outSingle.close() print('Finished writing single-frame localizations.') i3outMulti = writeinsight3.I3Writer(self.i3TypeIndivBinNames['multi'], frames=num_frames-1) print('Opened',self.i3TypeIndivBinNames['multi'],'for writing multi-frame localizations.') i3outMulti.addMoleculesWithXYZF(multi[:m_idx,0],multi[:m_idx,1],multi[:m_idx,2],multi[:m_idx,3]) i3outMulti.close() print('Finished writing multi-frame localizations.') single = None multi = None print('Finished assigning localizations.\n') return True
def read_single_localization_frame(self, frame): i3in = readinsight3.I3Reader(self.i3TypeIndivBinNames['multi']) locs = i3in.getMoleculesInFrame(frame) print(locs) i3in.close() return locs