def OnExtractPSF(self, event): if (len(self.PSFLocs) > 0): from PYME.Analysis.PSFEst import extractImages chnum = self.chChannel.GetSelection() psfROISize = [int(s) for s in self.tPSFROI.GetValue().split(',')] psfBlur = [float(s) for s in self.tPSFBlur.GetValue().split(',')] #print psfROISize psf, offsets = extractImages.getPSF3D( self.image.data[:, :, :, chnum], self.PSFLocs, psfROISize, psfBlur, expand_z=self.cbExpandROI.GetValue()) if self.cbBackgroundCorrect.GetValue(): #widefield image - do special background subtraction psf = extractImages.backgroundCorrectPSFWF(psf) from PYME.DSView.dsviewer import ImageStack, ViewIm3D from PYME.IO.MetaDataHandler import NestedClassMDHandler mdh = NestedClassMDHandler(self.image.mdh) self.write_metadata(mdh, 'default') mdh['ImageType'] = 'PSF' im = ImageStack(data=psf, mdh=mdh, titleStub='Extracted PSF') im.defaultExt = '*.tif' #we want to save as PSF by default ViewIm3D(im, mode='psf', parent=wx.GetTopLevelParent(self.dsviewer))
def OnSubtractBackground(self, event): from PYME.IO.image import ImageStack from PYME.DSView import ViewIm3D from PYME.Analysis.PSFEst import extractImages d_bg = extractImages.backgroundCorrectPSFWF(self.image.data[:, :, :]) im = ImageStack(d_bg, titleStub='Filtered Image') im.mdh.copyEntriesFrom(self.image.mdh) im.mdh['Parent'] = self.image.filename dv = ViewIm3D(im, mode='psf', glCanvas=self.dsviewer.glCanvas)
def OnExtractMultiviewPSF(self, event): if (len(self.PSFLocs) > 0): from PYME.Analysis.PSFEst import extractImages psfROISize = [int(s) for s in self.tPSFROI.GetValue().split(',')] psfBlur = [float(s) for s in self.tPSFBlur.GetValue().split(',')] psfs = [] for chnum in range(self.image.data.shape[3]): alignZ = (chnum > 0) and self.cbAlignZ.GetValue() #always align the first channel psf, offsets = extractImages.getPSF3D( self.image.data[:, :, :, chnum], self.PSFLocs, psfROISize, psfBlur, centreZ=alignZ, expand_z=self.cbExpandROI.GetValue()) if self.cbBackgroundCorrect.GetValue(): #widefield image - do special background subtraction psf = extractImages.backgroundCorrectPSFWF(psf) psfs.append(psf) from PYME.DSView.dsviewer import ImageStack, ViewIm3D from PYME.IO.MetaDataHandler import NestedClassMDHandler mdh = NestedClassMDHandler(self.image.mdh) self.write_metadata(mdh, 'Multiview') mdh['ImageType'] = 'PSF' im = ImageStack(data=psfs, mdh=mdh, titleStub='Extracted PSF') im.defaultExt = '*.tif' #we want to save as PSF by default ViewIm3D(im, mode='psf', parent=wx.GetTopLevelParent(self.dsviewer))
def OnExtractSplitPSF(self, event): if (len(self.PSFLocs) > 0): from PYME.Analysis.PSFEst import extractImages chnum = self.chChannel.GetSelection() psfROISize = [int(s) for s in self.tPSFROI.GetValue().split(',')] psfBlur = [float(s) for s in self.tPSFBlur.GetValue().split(',')] #print psfROISize psfs = [] offsetsAllChannel = [] #extract first channel (always aligned) psf, offsets = extractImages.getPSF3D(self.image.data[:, :, :, 0].squeeze(), self.PSFLocs, psfROISize, psfBlur, centreZ=True) if self.chType.GetSelection() == 0: #widefield image - do special background subtraction psf = extractImages.backgroundCorrectPSFWF(psf) psfs.append(psf) offsetsAllChannel.append(offsets) alignZ = self.cbAlignZ.GetValue() z_offset = offsets[2] #extract subsequent channels, aligning if necessary, otherwise offsetting by the calculated offset for the first channel for i in range(1, self.image.data.shape[3]): psf, offsets = extractImages.getPSF3D( self.image.data[:, :, :, i].squeeze(), self.PSFLocs, psfROISize, psfBlur, centreZ=alignZ, z_offset=z_offset) if self.cbBackgroundCorrect.GetValue(): #widefield image - do special background subtraction psf = extractImages.backgroundCorrectPSFWF(psf) psfs.append(psf) offsetsAllChannel.append(offsets) psf = numpy.concatenate(psfs, 0) offsetsAllChannel = numpy.asarray(offsetsAllChannel) offsetsAllChannel -= offsetsAllChannel[0] print(offsetsAllChannel) # from pylab import * # import cPickle # imshow(psf.max(2)) from PYME.DSView.dsviewer import ImageStack, ViewIm3D from PYME.IO.MetaDataHandler import NestedClassMDHandler mdh = NestedClassMDHandler(self.image.mdh) self.write_metadata(mdh, 'Split', offsetsAllChannel[:, 2]) mdh['ImageType'] = 'PSF' im = ImageStack(data=psf, mdh=mdh, titleStub='Extracted PSF') im.defaultExt = '*.tif' #we want to save as PSF by default ViewIm3D(im, mode='psf', parent=wx.GetTopLevelParent(self.dsviewer))
def OnCalibrateMultiview(self, event): """ CalibrateMultiview loops over all channels described in the metadata and runs CalibrateAstigmatism on each one. Results are stored in a library of dictionaries and are saved into a jason astigmatism map file (.am) Args: event: GUI event Returns: nothing """ # first make sure the user is calling the right function try: chanSizeX = self.image.mdh['Multiview.ROISize'][0] except KeyError: raise KeyError( 'You are not looking at multiview data, or the metadata is incomplete' ) if len(self.PSFLocs) > 0: print( 'Place cursor on bead in first multiview channel and press Calibrate Multiview Astigmatism' ) self.OnClearTags(event) return from PYME.Analysis.PSFEst import extractImages from PYME.DSView.modules import psfTools import scipy.interpolate as terp import numpy as np from PYME.IO.FileUtils import nameUtils import os import json zrange = np.nan * np.ones(2) rsx, rsy, rsz = [int(s) for s in self.tPSFROI.GetValue().split(',')] # astigDat = [] astigLib = {} for ii in range(self.numChan): # grab PSFs, currently relying on user to pick psf in first channel xmin, xmax = [(self.do.xp - rsx + ii * chanSizeX), (self.do.xp + rsx + ii * chanSizeX + 1)] # dz returns offset in units of frames, dx dy are in pixels dx, dy, dz = extractImages.getIntCenter( self.image.data[xmin:xmax, (self.do.yp - rsy):(self.do.yp + rsy + 1), :, 0]) self.PSFLocs.append((self.do.xp + dx, self.do.yp + dy, dz)) self.view.psfROIs = self.PSFLocs self.view.Refresh() psfROISize = [int(s) for s in self.tPSFROI.GetValue().split(',')] psfBlur = [float(s) for s in self.tPSFBlur.GetValue().split(',')] psf = extractImages.getPSF3D(self.image.data[:, :, :, 0], [self.PSFLocs[ii]], psfROISize, psfBlur) if self.cbBackgroundCorrect.GetValue(): #widefield image - do special background subtraction psf = extractImages.backgroundCorrectPSFWF(psf) from PYME.DSView.dsviewer import ImageStack, ViewIm3D im = ImageStack(data=psf, mdh=self.image.mdh, titleStub='Extracted PSF, Chan %i' % ii) im.defaultExt = '*.psf' #we want to save as PSF by default ViewIm3D(im, mode='psf', parent=wx.GetTopLevelParent(self.dsviewer)) calibrater = psfTools.PSFTools(self.dsviewer, im) astigLib['PSF%i' % ii] = (calibrater.OnCalibrateAstigmatism( event, plotIt=False)) # the next line is only ok if each frame is at a different z position, which it should be astigLib['PSF%i' % ii]['z'] += dz * self.image.mdh['voxelsize.z'] * 1.e3 # -- spline interpolate -- # find region of dsigma which is monotonic dsig = terp.UnivariateSpline( astigLib['PSF%i' % ii]['z'], astigLib['PSF%i' % ii]['dsigma']) #, s=1.5*len(astigDat[ii]['z'])) # mask where the sign is the same as the center zvec = np.linspace(astigLib['PSF%i' % ii]['z'].min(), astigLib['PSF%i' % ii]['z'].max(), 1000) sgn = np.sign(np.diff(dsig(zvec))) halfway = len(sgn) / 2 notmask = sgn != sgn[halfway] # find z range for spline generation lowerZ = zvec[np.where(notmask[:halfway])[0].max()] upperZ = zvec[(len(sgn) / 2 + np.where(notmask[halfway:])[0].min() - 1)] astigLib['PSF%i' % ii]['zrange'] = [lowerZ, upperZ] zrange = [ np.nanmin([lowerZ, zrange[0]]), np.nanmax([upperZ, zrange[1]]) ] #lowsubZ , upsubZ = np.absolute(astigDat[ii]['z'] - zvec[lowerZ]), np.absolute(astigDat[ii]['z'] - zvec[upperZ]) #lowZLoc = np.argmin(lowsubZ) #upZLoc = np.argmin(upsubZ) # #astigLib['sigxTerp%i' % ii] = terp.UnivariateSpline(astigLib['PSF%i' % ii]['z'], astigLib['PSF%i' % ii]['sigmax'], # bbox=[lowerZ, upperZ]) #astigLib['sigyTerp%i' % ii] = terp.UnivariateSpline(astigLib['PSF%i' % ii]['z'], astigLib['PSF%i' % ii]['sigmay'], # bbox=[lowerZ, upperZ]) astigLib['PSF%i' % ii]['z'] = astigLib['PSF%i' % ii]['z'].tolist() astigLib['zRange'] = np.round(zrange).tolist() astigLib['numChan'] = self.numChan psfTools.plotAstigCalibration(astigLib) # save to json file #defFile = os.path.splitext(os.path.split(self.visFr.GetTitle())[-1])[0] + '.am' fdialog = wx.FileDialog( None, 'Save Astigmatism Calibration as ...', wildcard='AstigMAPism file (*.am)|*.am', style=wx.FD_SAVE, defaultDir=nameUtils.genShiftFieldDirectoryPath( )) #, defaultFile=defFile) succ = fdialog.ShowModal() if (succ == wx.ID_OK): fpath = fdialog.GetPath() fid = open(fpath, 'wb') json.dump(astigLib, fid) fid.close()