def OnGenShiftmapQuad(self, event): from PYME.Analysis.points import twoColour, twoColourPlot from PYME.IO.MetaDataHandler import get_camera_roi_origin pipeline = self.visFr.pipeline vs = pipeline.mdh.voxelsize_nm roi_x0, roi_y0 = get_camera_roi_origin(pipeline.mdh) x0 = (roi_x0)*vs[0] y0 = (roi_y0)*vs[1] lx = len(pipeline.filter['x']) bbox = None#[0,(pipeline.mdh['Camera.ROIWidth'] + 1)*vs[0], 0,(pipeline.mdh['Camera.ROIHeight'] + 1)*vs[1]] dx, dy, spx, spy, good = twoColour.genShiftVectorFieldQ(pipeline.filter['x']+.1*np.random.randn(lx) + x0, pipeline.filter['y']+.1*np.random.randn(lx) + y0, pipeline.filter['fitResults_dx'], pipeline.filter['fitResults_dy'], pipeline.filter['fitError_dx'], pipeline.filter['fitError_dy'], bbox=bbox) #twoColourPlot.PlotShiftField(dx, dy, spx, spy) twoColourPlot.PlotShiftField2(spx, spy, pipeline.mdh['Splitter.Channel0ROI'][2:], voxelsize=vs) twoColourPlot.PlotShiftResiduals(pipeline['x'][good] + x0, pipeline['y'][good] + y0, pipeline['fitResults_dx'][good], pipeline['fitResults_dy'][good], spx, spy) from six.moves import cPickle defFile = os.path.splitext(os.path.split(self.visFr.GetTitle())[-1])[0] + '.sf' fdialog = wx.FileDialog(None, 'Save shift field as ...', wildcard='Shift Field file (*.sf)|*.sf', style=wx.FD_SAVE, defaultDir = nameUtils.genShiftFieldDirectoryPath(), defaultFile=defFile) succ = fdialog.ShowModal() if (succ == wx.ID_OK): fpath = fdialog.GetPath() #save as a pickle containing the data and voxelsize fid = open(fpath, 'wb') cPickle.dump((spx, spy), fid, 2) fid.close()
def execute(self, namespace): from PYME.Analysis.points import twoColour from PYME.Analysis.points import multiview from PYME.IO.MetaDataHandler import NestedClassMDHandler inp = namespace[self.input_name] try: # make sure we're looking at multiview data n_chan = inp.mdh['Multiview.NumROIs'] except AttributeError: raise AttributeError('multiview metadata is missing or incomplete') # sort in frame order I = inp['tIndex'].argsort() x_sort, y_sort = inp['x'][I], inp['y'][I] chan_sort = inp['multiviewChannel'][I] clump_id, keep = multiview.pair_molecules( inp['tIndex'][I], x_sort, y_sort, chan_sort, self.search_radius_nm * np.ones_like(x_sort), appear_in=np.arange(n_chan), n_frame_sep=inp['tIndex'].max(), pix_size_nm=1e3 * inp.mdh['voxelsize.x']) # only look at the clumps which showed up in all channels x = x_sort[keep] y = y_sort[keep] chan = chan_sort[keep] clump_id = clump_id[keep] # Generate raw shift vectors (map of displacements between channels) for each channel mol_list = np.unique(clump_id) n_mols = len(mol_list) dx = np.zeros((n_chan - 1, n_mols)) dy = np.zeros_like(dx) dx_err = np.zeros_like(dx) dy_err = np.zeros_like(dx) x_clump, y_clump, x_std, y_std, x_shifted, y_shifted = [], [], [], [], [], [] shift_map_dtype = [ ('mx', '<f4'), ('mx2', '<f4'), ('mx3', '<f4'), # x terms ('my', '<f4'), ('my2', '<f4'), ('my3', '<f4'), # y terms ('mxy', '<f4'), ('mx2y', '<f4'), ('mxy2', '<f4'), # cross terms ('x0', '<f4') ] # 0th order shift shift_maps = np.zeros(2 * (n_chan - 1), dtype=shift_map_dtype) mdh = NestedClassMDHandler(inp.mdh) mdh['Multiview.shift_map.legend'] = {} for ii in range(n_chan): chan_mask = (chan == ii) x_chan = np.zeros(n_mols) y_chan = np.zeros(n_mols) x_chan_std = np.zeros(n_mols) y_chan_std = np.zeros(n_mols) for ind in range(n_mols): # merge clumps within channels clump_mask = np.where( np.logical_and(chan_mask, clump_id == mol_list[ind])) x_chan[ind] = x[clump_mask].mean() y_chan[ind] = y[clump_mask].mean() x_chan_std[ind] = x[clump_mask].std() y_chan_std[ind] = y[clump_mask].std() x_clump.append(x_chan) y_clump.append(y_chan) x_std.append(x_chan_std) y_std.append(y_chan_std) if ii > 0: dx[ii - 1, :] = x_clump[0] - x_clump[ii] dy[ii - 1, :] = y_clump[0] - y_clump[ii] dx_err[ii - 1, :] = np.sqrt(x_std[ii]**2 + x_std[0]**2) dy_err[ii - 1, :] = np.sqrt(y_std[ii]**2 + y_std[0]**2) # generate shiftmap between ii-th channel and the 0th channel dxx, dyy, spx, spy, good = twoColour.genShiftVectorFieldQ( x_clump[0], y_clump[0], dx[ii - 1, :], dy[ii - 1, :], dx_err[ii - 1, :], dy_err[ii - 1, :]) # store shiftmaps in structured array mdh['Multiview.shift_map.legend']['Chan0%s.X' % ii] = 2 * (ii - 1) mdh['Multiview.shift_map.legend']['Chan0%s.Y' % ii] = 2 * (ii - 1) + 1 for ki in range(len(shift_map_dtype)): k = shift_map_dtype[ki][0] shift_maps[2 * (ii - 1)][k] = spx.__getattribute__(k) shift_maps[2 * (ii - 1) + 1][k] = spy.__getattribute__(k) # shift_maps['Chan0%s.X' % ii], shift_maps['Chan0%s.Y' % ii] = spx.__dict__, spy.__dict__ mdh['Multiview.shift_map.model'] = '.'.join( [spx.__class__.__module__, spx.__class__.__name__]) namespace[self.output_name] = tabular.RecArraySource(shift_maps) namespace[self.output_name].mdh = mdh