예제 #1
0
    def OnDeconvWiener(self, event):
        #from PYME.Deconv import weiner

        decMDH = MetaDataHandler.NestedClassMDHandler(self.image.mdh)
        decMDH['Deconvolution.OriginalFile'] = self.image.filename
        decMDH['Deconvolution.Method'] = 'Wiener'

        im = numpy.zeros(self.image.data.shape, 'f4')

        decView = View3D(im,
                         'Deconvolution Result',
                         mdh=decMDH,
                         parent=self.dsviewer)

        decView.wienerPanel = WienerDeconvolver(decView, self.image,
                                                decView.image)

        self.pinfo1 = aui.AuiPaneInfo().Name("wienerPanel").Left(
        ).Caption('Wiener Filter').DestroyOnClose(True).CloseButton(
            False
        )  #.MinimizeButton(True).MinimizeMode(aui.AUI_MINIMIZE_CAPT_SMART|aui.AUI_MINIMIZE_POS_RIGHT)#.CaptionVisible(False)
        decView._mgr.AddPane(decView.wienerPanel, self.pinfo1)
        decView._mgr.Update()

        self.dsviewer.decView = decView
예제 #2
0
    def Generate(self, event=None):
        dlg = genImageDialog.GenImageDialog(self.mainWind, mode=self.mode)
        ret = dlg.ShowModal()

        bCurr = wx.BusyCursor()

        if ret == wx.ID_OK:
            mdh = MetaDataHandler.NestedClassMDHandler()
            mdh['Rendering.Method'] = self.name
            if 'imageID' in self.pipeline.mdh.getEntryNames():
                mdh['Rendering.SourceImageID'] = self.pipeline.mdh['imageID']
            mdh['Rendering.SourceFilename'] = self.pipeline.filename
            
            for cb in renderMetadataProviders:
                cb(mdh)            
            
            pixelSize = dlg.getPixelSize()

            imb = self._getImBounds()

            im = self.genIm(dlg, imb, mdh)
            img = GeneratedImage(im,imb, pixelSize, 0, ['Image'] , mdh = mdh)
            imf = ViewIm3D(img, mode='visGUI', title='Generated %s - %3.1fnm bins' % (self.name, pixelSize), glCanvas=self.visFr.glCanvas, parent=self.mainWind)
            #imf = imageView.ImageViewFrame(self.visFr,img, self.visFr.glCanvas)
            #imageView.MultiChannelImageViewFrame(self.visFr, self.visFr.glCanvas, img, title='Generated %s - %3.1fnm bins' % (self.name, pixelSize))
            #self.visFr.generatedImages.append(imf)
            #imf.Show()

            self.visFr.RefreshView()

        dlg.Destroy()
        return imf
예제 #3
0
    def OnMAquireOnePic(self, event):
        import numpy as np
        self.scope.pa.stop()
        ds2 = np.atleast_3d(
            self.scope.pa.dsa.reshape(self.scope.cam.GetPicWidth(),
                                      self.scope.cam.GetPicHeight()).copy())

        #metadata handling
        mdh = MetaDataHandler.NestedClassMDHandler()
        mdh.setEntry('StartTime', time.time())
        mdh.setEntry('AcquisitionType', 'SingleImage')

        #loop over all providers of metadata
        for mdgen in MetaDataHandler.provideStartMetadata:
            mdgen(mdh)

        im = dsviewer.ImageStack(data=ds2, mdh=mdh, titleStub='Unsaved Image')
        if not im.mode == 'graph':
            im.mode = 'lite'

        #print im.mode
        dvf = dsviewer.DSViewFrame(im, mode=im.mode, size=(500, 500))
        dvf.SetSize((500, 500))
        dvf.Show()

        self.snapNum += 1

        self.scope.pa.Prepare(True)
        self.scope.pa.start()
예제 #4
0
def getFileMetadata(filename):
    ext = os.path.splitext(filename)[1]
    #print ext

    mdh = MetaDataHandler.NestedClassMDHandler()

    try:
        if ext in ['.h5', '.h5r']:
            h5f = tables.openFile(filename)
            md = MetaDataHandler.HDFMDHandler(h5f)

            mdh = MetaDataHandler.NestedClassMDHandler(md)
            #print guess, ret

            h5f.close()
    except:
        pass

    return mdh
예제 #5
0
    def __init__(self,
                 name,
                 resultsFilename,
                 initialTasks=[],
                 onEmpty=doNix,
                 fTaskToPop=popZero):
        if resultsFilename is None:
            resultsFilename = genResultFileName(name)

        if os.path.exists(
                resultsFilename):  #bail if output file already exists
            raise RuntimeError('Output file already exists: ' +
                               resultsFilename)

        TaskQueue.__init__(self, name, initialTasks, onEmpty, fTaskToPop)
        self.resultsFilename = resultsFilename

        self.numClosedTasks = 0

        logging.info('Creating results file')

        self.h5ResultsFile = tables.openFile(self.resultsFilename, 'w')

        self.prepResultsFile()  # pass

        #self.fileResultsLock = threading.Lock()
        self.fileResultsLock = tablesLock

        logging.info('Creating results metadata')

        self.resultsMDH = MetaDataHandler.HDFMDHandler(self.h5ResultsFile)
        self.metaData = MetaDataHandler.NestedClassMDHandler()
        #self.metaData = None #MetaDataHandler.NestedClassMDHandler(self.resultsMDH)
        self.metaDataStale = True
        self.MDHCache = []

        logging.info('Creating results events table')
        with self.fileResultsLock.wlock:
            self.resultsEvents = self.h5ResultsFile.createTable(
                self.h5ResultsFile.root,
                'Events',
                SpoolEvent,
                filters=tables.Filters(complevel=5, shuffle=True))

        logging.info('Events table created')

        self.haveResultsTable = False

        self.resultsQueue = []  #Queue.Queue()
        self.resultsQueueLock = threading.Lock()
        self.lastResultsQueuePurge = time.time()

        logging.info('Results file initialised')
예제 #6
0
    def OnSplitChannels(self, event):
        try:
            names = self.image.mdh.getEntry('ChannelNames')
        except:
            names = ['%d' % d for d in range(self.image.data.shape[3])]

        for i in range(self.image.data.shape[3]):
            mdh = MetaDataHandler.NestedClassMDHandler(self.image.mdh)
            mdh.setEntry('ChannelNames', [names[i]])

            View3D(self.image.data[:, :, :, i],
                   '%s - %s' % (self.image.filename, names[i]),
                   mdh=mdh,
                   parent=wx.GetTopLevelParent(self.dsviewer))
예제 #7
0
    def __init__(self, chans, cam, shutters, _piezos, _log={}):
        PreviewAquisator.__init__(self, chans, cam, shutters, None)
        self.piezos = _piezos
        self.log = _log
        self.mdh = MetaDataHandler.NestedClassMDHandler()
        #register as a provider of metadata
        MetaDataHandler.provideStartMetadata.append(self.ProvideStackMetadata)

        self.ScanChan = 0
        self.StartMode = self.CENTRE_AND_LENGTH
        self.SeqLength = 100
        self.StepSize = 0.2
        self.startPos = 0
        self.endPos = 0

        self.direction = self.FORWARDS
예제 #8
0
    def OnFitObjects(self, event):
        import PYME.Analysis.FitFactories.Gauss3DFitR as fitMod
        from PYME.Acquire import MetaDataHandler
        chnum = self.chChannel.GetSelection()

        mdh = MetaDataHandler.NestedClassMDHandler(self.image.mdh)
        mdh['tIndex'] = 0

        self.objFitRes = {}

        for chnum in range(self.image.data.shape[3]):
            data = self.image.data[:, :, :, chnum]

            if not 'Camera.ADOffset' in mdh.getEntryNames():
                mdh['Camera.ADOffset'] = data.min()

            fitFac = fitMod.FitFactory(data, mdh)

            self.objFitRes[chnum] = numpy.empty(len(self.ofd),
                                                fitMod.FitResultsDType)
            for i in range(len(self.ofd)):
                p = self.ofd[i]
                #try:
                self.objFitRes[chnum][i] = fitFac.FromPoint(
                    round(p.x), round(p.y), round(p.z), 8)
                #except:
                #    pass

            #if self.nObjFit == None:

            vObjFit = recArrayView.recArrayPanel(
                self.dsviewer, self.objFitRes[chnum]['fitResults'])
            self.dsviewer.AddPage(vObjFit,
                                  caption='Fitted Positions %d - %d' %
                                  (chnum, self.nObjFit))
        self.nObjFit += 1
        #else:
        #    self.vObjFit.grid.SetData(self.objFitRes['fitResults'])

        self.dsviewer.update()
예제 #9
0
    def __init__(self):
        #list of tuples  of form (class, chan, name) describing the instaled piezo channels
        self.piezos = []
        self.hardwareChecks = []

        #entries should be of the form: "x" : (piezo, channel, multiplier)
        # where multiplyier is what to multiply by to get the usints to um
        self.positioning = {}
        self.joystick = None

        self.cameras = {}
        self.camControls = {}

        self.stackNum = 0

        #self.WantEventNotification = []

        self.StatusCallbacks = [
        ]  #list of functions which provide status information
        self.CleanupFunctions = []  #list of functions to be called at exit
        self.PACallbacks = [
        ]  #list of functions to be called when a new aquisator is created
        #preview
        self.saturationThreshold = 16383  #14 bit
        self.lastFrameSaturated = False
        #self.cam.saturationIntervened = False
        self.saturatedMessage = ''

        protocol.scope = self
        ccdCalibrator.setScope(self)
        self.initDone = False

        self._OpenSettingsDB()

        MetaDataHandler.provideStartMetadata.append(self.GenStartMetadata)

        #provision to set global metadata values in startup script
        self.mdh = MetaDataHandler.NestedClassMDHandler()
예제 #10
0
    def OnAlignChannels(self, event):
        #try:
        #    names = self.image.mdh.getEntry('ChannelNames')
        #except:
        #    names = ['%d' % d for d in range(self.image.data.shape[3])]
        from PYME.Analysis.DataSources import AlignDataSource
        import PYME.DSView.dataWrap

        if isinstance(self.image.data, PYME.DSView.dataWrap.ListWrap):
            nd = [
                AlignDataSource.DataSource(ds)
                for ds in self.image.data.wrapList
            ]
        else:
            nd = [
                AlignDataSource.DataSource(
                    dataWrap.Wrap(self.image.data[:, :, :, i]))
                for i in range(self.image.data.shape[3])
            ]

        mdh = MetaDataHandler.NestedClassMDHandler(self.image.mdh)

        res = View3D(nd,
                     '%s - %s' % (self.image.filename, 'align'),
                     mdh=mdh,
                     parent=wx.GetTopLevelParent(self.dsviewer))

        res.panAlign = ShiftPanel(res)

        pinfo1 = aui.AuiPaneInfo().Name("shiftPanel").Right(
        ).Caption('Alignment').DestroyOnClose(
            True
        ).CloseButton(False).MinimizeButton(True).MinimizeMode(
            aui.AUI_MINIMIZE_CAPT_SMART | aui.AUI_MINIMIZE_POS_RIGHT
        )  #.MinimizeButton(True).MinimizeMode(aui.AUI_MINIMIZE_CAPT_SMART|aui.AUI_MINIMIZE_POS_RIGHT)#.CaptionVisible(False)
        #pinfo2 = aui.AuiPaneInfo().Name("overlayPanel").Right().Caption('Overlays').CloseButton(False).MinimizeButton(True).MinimizeMode(aui.AUI_MINIMIZE_CAPT_SMART|aui.AUI_MINIMIZE_POS_RIGHT)#.CaptionVisible(False)
        res._mgr.AddPane(res.panAlign, pinfo1)
        res._mgr.Update()
예제 #11
0
    def FindAndParseMetadata(self, filename):
        '''Try and find and load a .xml or .md metadata file that might be ascociated
        with a given image filename. See the relevant metadatahandler classes
        for details.'''
        import xml.parsers.expat

        mdf = None
        xmlfn = os.path.splitext(filename)[0] + '.xml'
        xmlfnmc = os.path.splitext(filename)[0].split('__')[0] + '.xml'
        if os.path.exists(xmlfn):
            try:
                self.mdh = MetaDataHandler.NestedClassMDHandler(
                    MetaData.TIRFDefault)
                self.mdh.copyEntriesFrom(MetaDataHandler.XMLMDHandler(xmlfn))
                mdf = xmlfn
            except xml.parsers.expat.ExpatError:
                #fix for bug in which PYME .md was written with a .xml extension
                self.mdh = MetaDataHandler.NestedClassMDHandler(
                    MetaData.BareBones)
                self.mdh.copyEntriesFrom(
                    MetaDataHandler.SimpleMDHandler(xmlfn))
                mdf = xmlfn

        elif os.path.exists(
                xmlfnmc):  #this is a single colour channel of a pair
            self.mdh = MetaDataHandler.NestedClassMDHandler(
                MetaData.TIRFDefault)
            self.mdh.copyEntriesFrom(MetaDataHandler.XMLMDHandler(xmlfnmc))
            mdf = xmlfnmc
        else:
            self.mdh = MetaDataHandler.NestedClassMDHandler(MetaData.BareBones)

            #check for simple metadata (python code with an .md extension which
            #fills a dictionary called md)
            mdfn = os.path.splitext(filename)[0] + '.md'
            if os.path.exists(mdfn):
                self.mdh.copyEntriesFrom(MetaDataHandler.SimpleMDHandler(mdfn))
                mdf = mdfn
            elif filename.endswith('.lsm'):
                #read lsm metadata
                from PYME.gohlke.tifffile import TIFFfile
                tf = TIFFfile(filename)
                lsm_info = tf[0].cz_lsm_scan_information
                self.mdh['voxelsize.x'] = lsm_info['line_spacing']
                self.mdh['voxelsize.y'] = lsm_info['line_spacing']
                self.mdh['voxelsize.z'] = lsm_info['plane_spacing']

                def lsm_pop(basename, dic):
                    for k, v in dic.items():
                        if isinstance(v, list):
                            #print k, v
                            for i, l_i in enumerate(v):
                                #print i, l_i, basename
                                lsm_pop(
                                    basename + k + '.' + k[:-1] + '%i.' % i,
                                    l_i)

                        else:
                            self.mdh[basename + k] = v

                lsm_pop('LSM.', lsm_info)
            elif filename.endswith('.dbl'):  #Bewersdorf lab STED
                mdfn = filename[:-4] + '.txt'
                entrydict = {}

                try:  #try to read in extra metadata if possible
                    with open(mdfn, 'r') as mf:
                        for line in mf:
                            s = line.split(':')
                            if len(s) == 2:
                                entrydict[s[0]] = s[1]

                except IOError:
                    pass


#                vx, vy = entrydict['Pixel size (um)'].split('x')
#                self.mdh['voxelsize.x'] = float(vx)
#                self.mdh['voxelsize.y'] = float(vy)
#                self.mdh['voxelsize.z'] = 0.2 #FIXME for stacks ...
#
#                sx, sy = entrydict['Image format'].split('x')
#                self.mdh['Camera.ROIWidth'] = int(sx)
#                self.mdh['Camera.ROIHeight'] = int(sy)
#
#                self.mdh['NumImages'] = int(entrydict['# Images'])

                with open(filename) as df:
                    s = df.read(8)
                    Z, X, Y, T = numpy.fromstring(s, '>u2')
                    s = df.read(16)
                    depth, width, height, elapsed = numpy.fromstring(s, '<f4')

                    self.mdh['voxelsize.x'] = width / X
                    self.mdh['voxelsize.y'] = height / Y
                    self.mdh['voxelsize.z'] = depth

                    self.mdh['Camera.ROIWidth'] = X
                    self.mdh['Camera.ROIHeight'] = Y
                    self.mdh['NumImages'] = Z * T

                def _sanitise_key(key):
                    k = key.replace('#', 'Num')
                    k = k.replace('(%)', '')
                    k = k.replace('(', '')
                    k = k.replace(')', '')
                    k = k.replace('.', '')
                    k = k.replace('/', '')
                    k = k.replace('?', '')
                    k = k.replace(' ', '')
                    if not k[0].isalpha():
                        k = 's' + k
                    return k

                for k, v in entrydict.items():
                    self.mdh['STED.%s' % _sanitise_key(k)] = v

        if self.haveGUI and not ('voxelsize.x' in self.mdh.keys()
                                 and 'voxelsize.y' in self.mdh.keys()):
            from PYME.DSView.voxSizeDialog import VoxSizeDialog

            dlg = VoxSizeDialog(None)
            #dlg.ShowModal()

            self.mdh.setEntry('voxelsize.x', dlg.GetVoxX())
            self.mdh.setEntry('voxelsize.y', dlg.GetVoxY())
            self.mdh.setEntry('voxelsize.z', dlg.GetVoxZ())

        return mdf
예제 #12
0
    def Generate(self, event=None):
        jitVars = ['1.0']
        jitVars += self.pipeline.colourFilter.keys()

        self.genMeas = self.pipeline.GeneratedMeasures.keys()
        if not 'neighbourDistances' in self.genMeas:
            self.genMeas.append('neighbourDistances')
            
        if not 'neighbourErrorMin' in self.genMeas:
            self.genMeas.append('neighbourErrorMin')
            
        jitVars += self.genMeas
        
        
        if 'z' in self.pipeline.mapping.keys():
            zvals = self.pipeline.mapping['z']
        else:
            zvals = None

        dlg = genImageDialog.GenImageDialog(self.mainWind, mode=self.mode, defaultPixelSize=self._defaultPixelSize, colours=self.pipeline.colourFilter.getColourChans(), zvals = zvals, jitterVariables = jitVars, jitterVarDefault=self._getDefaultJitVar(jitVars), jitterVarDefaultZ=self._getDefaultZJitVar(jitVars))
        ret = dlg.ShowModal()

        bCurr = wx.BusyCursor()

        if ret == wx.ID_OK:
            mdh = MetaDataHandler.NestedClassMDHandler()
            mdh['Rendering.Method'] = self.name
            if 'imageID' in self.pipeline.mdh.getEntryNames():
                mdh['Rendering.SourceImageID'] = self.pipeline.mdh['imageID']
            mdh['Rendering.SourceFilename'] = self.pipeline.filename
            
            mdh.Source = MetaDataHandler.NestedClassMDHandler(self.pipeline.mdh)
            
            for cb in renderMetadataProviders:
                cb(mdh)           
            
            pixelSize = dlg.getPixelSize()

            status = statusLog.StatusLogger('Generating %s Image ...' % self.name)

            imb = self._getImBounds()
            
            #record the pixel origin in nm from the corner of the camera for futrue overlays            
            if 'Source.Camera.ROIPosX' in mdh.getEntryNames():
                #a rendered image with information about the source ROI
                voxx, voxy = 1e3*mdh['Source.voxelsize.x'], 1e3*mdh['Source.voxelsize.y']
                
                ox = (mdh['Source.Camera.ROIPosX'] - 1)*voxx + imb.x0
                oy = (mdh['Source.Camera.ROIPosY'] - 1)*voxy + imb.y0
                if 'Source.Positioning.PIFoc' in mdh.getEntryNames():
                    oz = mdh['Source.Positioning.PIFoc']*1e3
                else:
                    oz = 0
            else:
                ox = imb.x0
                oy = imb.y0 
                oz = 0
            
            mdh['Origin.x'] = ox
            mdh['Origin.y'] = oy
            mdh['Origin.z'] = oz
            
            colours =  dlg.getColour()
            oldC = self.pipeline.colourFilter.currentColour

            ims = []

            for c in  colours:
                self.pipeline.colourFilter.setColour(c)
                ims.append(np.atleast_3d(self.genIm(dlg, imb, mdh)))

            im = GeneratedImage(ims,imb, pixelSize,  dlg.getZSliceThickness(), colours, mdh = mdh)

            imfc = ViewIm3D(im, mode='visGUI', title='Generated %s - %3.1fnm bins' % (self.name, pixelSize), glCanvas=self.visFr.glCanvas, parent=self.mainWind)

            #imfc = imageView.MultiChannelImageViewFrame(self.visFr, self.visFr.glCanvas, im, title='Generated %s - %3.1fnm bins' % (self.name, pixelSize))

            #self.visFr.generatedImages.append(imfc)
            #imfc.Show()

            self.pipeline.colourFilter.setColour(oldC)
        else:
            imfc = None

        dlg.Destroy()
        return imfc
예제 #13
0
    def testFrame(self, detThresh=0.9, offset=0, gui=True):
        from pylab import *
        #close('all')
        print '> testFrame Begin'
        #print '> Try to print moduleName:'
        #print(self.image.dataSource.moduleName)
        if self.image.dataSource.moduleName == 'TQDataSource':  # if .tif is loaded, module name will be buffered sth.
            self.checkTQ()
        if gui:
            matplotlib.interactive(False)
            figure()
        #sq = min(self.image.mdh.getEntry('EstimatedLaserOnFrameNo') + 1000, self.image.dataSource.getNumSlices()/4)
        #zps = array(range(self.image.mdh.getEntry('EstimatedLaserOnFrameNo') + 20, self.image.mdh.getEntry('EstimatedLaserOnFrameNo') + 24)  + range(sq, sq + 4) + range(self.image.dataSource.getNumSlices()/2,self.image.dataSource.getNumSlices() /2+4))
        #zps += offset

        zp = self.do.zp
        fitMod = self.fitFactories[self.cFitType.GetSelection()]
        self.image.mdh.setEntry('Analysis.FitModule', fitMod)
        #bgFrames = int(tBackgroundFrames.GetValue())
        bgFrames = [
            int(v) for v in self.tBackgroundFrames.GetValue().split(':')
        ]
        #print zps
        bgi = range(
            max(zp + bgFrames[0],
                self.image.mdh.getEntry('EstimatedLaserOnFrameNo')),
            max(zp + bgFrames[1],
                self.image.mdh.getEntry('EstimatedLaserOnFrameNo')))
        #else:
        #    bgi = range(max(zps[i] - 10,md.EstimatedLaserOnFrameNo), zps[i])
        mn = self.image.dataSource.moduleName
        if mn == 'BufferedDataSource':
            mn = self.image.dataSource.dataSource.moduleName

        #if 'Splitter' in fitMod:
        #    ft = remFitBuf.fitTask(self.image.seriesName, zp, detThresh, MetaDataHandler.NestedClassMDHandler(self.image.mdh), 'SplitterObjFindR', bgindices=bgi, SNThreshold=True,dataSourceModule=mn)
        #else:
        #    ft = remFitBuf.fitTask(self.image.seriesName, zp, detThresh, MetaDataHandler.NestedClassMDHandler(self.image.mdh), 'LatObjFindFR', bgindices=bgi, SNThreshold=True,dataSourceModule=mn)
        ft = remFitBuf.fitTask(self.image.seriesName,
                               zp,
                               detThresh,
                               MetaDataHandler.NestedClassMDHandler(
                                   self.image.mdh),
                               fitMod,
                               bgindices=bgi,
                               SNThreshold=True,
                               dataSourceModule=mn)
        res = ft(gui=gui, taskQueue=self.tq)

        if gui:
            figure()
            try:
                d = ft.ofd.filteredData.T
                #d = ft.data.squeeze().T
                imshow(d,
                       cmap=cm.hot,
                       interpolation='nearest',
                       hold=False,
                       clim=(median(d.ravel()), d.max()))
                plot([p.x for p in ft.ofd], [p.y for p in ft.ofd],
                     'o',
                     mew=2,
                     mec='g',
                     mfc='none',
                     ms=9)
                if ft.driftEst:
                    plot([p.x for p in ft.ofdDr], [p.y for p in ft.ofdDr],
                         'o',
                         mew=2,
                         mec='b',
                         mfc='none',
                         ms=9)
                #if ft.fitModule in remFitBuf.splitterFitModules:
                #        plot([p.x for p in ft.ofd], [d.shape[0] - p.y for p in ft.ofd], 'o', mew=2, mec='g', mfc='none', ms=9)
                #axis('tight')
                xlim(0, d.shape[1])
                ylim(d.shape[0], 0)
                xticks([])
                yticks([])

                vx = 1e3 * self.image.mdh['voxelsize.x']
                vy = 1e3 * self.image.mdh['voxelsize.y']
                plot(res.results['fitResults']['x0'] / vx,
                     res.results['fitResults']['y0'] / vy,
                     '+b',
                     mew=2)

                if 'startParams' in res.results.dtype.names:
                    plot(res.results['startParams']['x0'] / vx,
                         res.results['startParams']['y0'] / vy,
                         'xc',
                         mew=2)

                if 'tIm' in dir(ft.ofd):
                    figure()
                    imshow(ft.ofd.tIm.T,
                           cmap=cm.hot,
                           interpolation='nearest',
                           hold=False)
                    #axis('tight')
                    xlim(0, d.shape[1])
                    ylim(d.shape[0], 0)
                    xticks([])
                    yticks([])
                    plot(res.results['fitResults']['x0'] / vx,
                         res.results['fitResults']['y0'] / vy, '+b')

                #figure()
                #imshow()
            except AttributeError:
                #d = self.image.data[:,:,zp].squeeze().T
                d = (ft.data.squeeze() - ft.bg.squeeze()).T
                imshow(d,
                       cmap=cm.jet,
                       interpolation='nearest',
                       clim=[0, d.max()])
                xlim(0, d.shape[1])
                ylim(d.shape[0], 0)

                vx = 1e3 * self.image.mdh['voxelsize.x']
                vy = 1e3 * self.image.mdh['voxelsize.y']
                plot(res.results['fitResults']['x0'] / vx,
                     res.results['fitResults']['y0'] / vy, 'ow')
                pass

            show()

            matplotlib.interactive(True)

        return ft, res
예제 #14
0
    def __init__(self,
                 name,
                 dataFilename=None,
                 resultsFilename=None,
                 onEmpty=doNix,
                 fTaskToPop=popZero,
                 startAt='guestimate',
                 frameSize=(-1, -1),
                 complevel=6,
                 complib='zlib'):
        if dataFilename is None:
            self.dataFilename = genDataFilename(name)
        else:
            self.dataFilename = dataFilename

        if resultsFilename is None:
            resultsFilename = genResultFileName(self.dataFilename)
        else:
            resultsFilename = resultsFilename

        ffn = getFullFilename(self.dataFilename)

        self.acceptNewTasks = False
        self.releaseNewTasks = False

        self.postTaskBuffer = []

        initialTasks = []

        if os.path.exists(ffn):  #file already exists - read from it
            self.h5DataFile = tables.openFile(ffn, 'r')
            #self.metaData = MetaData.genMetaDataFromHDF(self.h5DataFile)
            self.dataMDH = MetaDataHandler.NestedClassMDHandler(
                MetaDataHandler.HDFMDHandler(self.h5DataFile))
            #self.dataMDH.mergeEntriesFrom(MetaData.TIRFDefault)
            self.imageData = self.h5DataFile.root.ImageData

            if startAt == 'guestimate':  #calculate a suitable starting value
                tLon = self.dataMDH.EstimatedLaserOnFrameNo
                if tLon == 0:
                    startAt = 0
                else:
                    startAt = tLon + 10

            if startAt == 'notYet':
                initialTasks = []
            else:
                initialTasks = list(
                    range(startAt, self.h5DataFile.root.ImageData.shape[0]))

            self.imNum = len(self.imageData)
            self.dataRW = False

        else:  #make ourselves a new file
            self.h5DataFile = tables.openFile(ffn, 'w')
            filt = tables.Filters(complevel, complib, shuffle=True)

            self.imageData = self.h5DataFile.createEArray(
                self.h5DataFile.root,
                'ImageData',
                tables.UInt16Atom(), (0, ) + tuple(frameSize),
                filters=filt,
                chunkshape=(1, ) + tuple(frameSize))
            self.events = self.h5DataFile.createTable(self.h5DataFile.root,
                                                      'Events',
                                                      SpoolEvent,
                                                      filters=filt)
            self.imNum = 0
            self.acceptNewTasks = True

            self.dataMDH = MetaDataHandler.HDFMDHandler(self.h5DataFile)
            self.dataMDH.mergeEntriesFrom(MetaData.TIRFDefault)
            self.dataRW = True

        HDFResultsTaskQueue.__init__(self, name, resultsFilename, initialTasks,
                                     onEmpty, fTaskToPop)

        self.resultsMDH.copyEntriesFrom(self.dataMDH)
        self.metaData.copyEntriesFrom(self.resultsMDH)

        #copy events to results file
        if len(self.h5DataFile.root.Events) > 0:
            self.resultsEvents.append(self.h5DataFile.root.Events[:])

        self.queueID = name

        self.numSlices = self.imageData.shape[0]

        #self.dataFileLock = threading.Lock()
        self.dataFileLock = tablesLock
        #self.getTaskLock = threading.Lock()
        self.lastTaskTime = 0
예제 #15
0
    def pushImagesDS(self, startingAt=0, detThresh=.9, fitFcn='LatGaussFitFR'):
        #global seriesName
        #dataFilename = self.image.seriesName
        resultsFilename = genResultFileName(self.image.seriesName)
        while os.path.exists(resultsFilename):
            di, fn = os.path.split(resultsFilename)
            fdialog = wx.FileDialog(
                None,
                'Analysis file already exists, please select a new filename',
                wildcard='H5R files|*.h5r',
                defaultDir=di,
                defaultFile=os.path.splitext(fn)[0] + '_1.h5r',
                style=wx.SAVE)
            succ = fdialog.ShowModal()
            if (succ == wx.ID_OK):
                resultsFilename = fdialog.GetPath().encode()
            else:
                raise RuntimeError('Invalid results file - not running')
            #self.image.seriesName = resultsFilename

        debugPrint('Results file = %s' % resultsFilename)

        self.tq.createQueue('HDFResultsTaskQueue', resultsFilename, None)

        debugPrint('Queue created')

        mdhQ = MetaDataHandler.QueueMDHandler(self.tq, resultsFilename,
                                              self.image.mdh)
        mdhQ.setEntry('Analysis.DetectionThreshold', detThresh)
        mdhQ.setEntry('Analysis.FitModule', fitFcn)
        mdhQ.setEntry('Analysis.DataFileID',
                      fileID.genDataSourceID(self.image.dataSource))

        debugPrint('Metadata transferred to queue')

        evts = self.image.dataSource.getEvents()
        if len(evts) > 0:
            self.tq.addQueueEvents(resultsFilename, evts)

        md = MetaDataHandler.NestedClassMDHandler(mdhQ)

        mn = self.image.dataSource.moduleName

        #if it's a buffered source, go back to underlying source
        if mn == 'BufferedDataSource':
            mn = self.image.dataSource.dataSource.moduleName

        for i in range(startingAt, self.image.dataSource.getNumSlices()):
            debugPrint('Posting task %d' % i)
            if 'Analysis.BGRange' in md.getEntryNames():
                bgi = range(
                    max(i + md.Analysis.BGRange[0],
                        md.EstimatedLaserOnFrameNo),
                    max(i + md.Analysis.BGRange[1],
                        md.EstimatedLaserOnFrameNo))
            elif 'Analysis.NumBGFrames' in md.getEntryNames():
                bgi = range(
                    max(i - md.Analysis.NumBGFrames,
                        md.EstimatedLaserOnFrameNo), i)
            else:
                bgi = range(max(i - 10, md.EstimatedLaserOnFrameNo), i)

            #task = fitTask(self.queueID, taskNum, self.metaData.Analysis.DetectionThreshold, self.metaData, self.metaData.Analysis.FitModule, 'TQDataSource', bgindices =bgi, SNThreshold = True)

            self.tq.postTask(remFitBuf.fitTask(self.image.seriesName,
                                               i,
                                               detThresh,
                                               md,
                                               fitFcn,
                                               bgindices=bgi,
                                               SNThreshold=True,
                                               dataSourceModule=mn),
                             queueName=resultsFilename)

        self.image.seriesName = resultsFilename
예제 #16
0
    def testFrames(self, detThresh=0.9, offset=0):
        from pylab import *
        close('all')
        if self.image.dataSource.moduleName == 'TQDataSource':
            self.checkTQ()
        matplotlib.interactive(False)
        clf()
        sq = min(
            self.image.mdh.getEntry('EstimatedLaserOnFrameNo') + 1000,
            self.image.dataSource.getNumSlices() / 4)
        # sq = 1000

        zps = array(
            range(
                self.image.mdh.getEntry('EstimatedLaserOnFrameNo') + 20,
                self.image.mdh.getEntry('EstimatedLaserOnFrameNo') + 24) +
            range(sq, sq + 4) +
            range(self.image.dataSource.getNumSlices() / 2,
                  self.image.dataSource.getNumSlices() / 2 + 4))
        # zps = [20 21 22 23 1000 1001 1002 1003 2500 2501 2502 2503]
        zps += offset

        fitMod = self.fitFactories[self.cFitType.GetSelection()]
        # e.g. fitMod = 'LatGaussFitFR'

        #bgFrames = int(tBackgroundFrames.GetValue())
        bgFrames = [
            int(v) for v in self.tBackgroundFrames.GetValue().split(':')
        ]
        # bgFrames = [-30, 0]

        print(zps)
        for i in range(12):
            print(i)
            #if 'Analysis.NumBGFrames' in md.getEntryNames():
            #bgi = range(max(zps[i] - bgFrames,mdh.getEntry('EstimatedLaserOnFrameNo')), zps[i])
            bgi = range(
                max(zps[i] + bgFrames[0],
                    self.image.mdh.getEntry('EstimatedLaserOnFrameNo')),
                max(zps[i] + bgFrames[1],
                    self.image.mdh.getEntry('EstimatedLaserOnFrameNo')))
            # bgi = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, ..., 19]

            #else:
            #    bgi = range(max(zps[i] - 10,md.EstimatedLaserOnFrameNo), zps[i])
            mn = self.image.dataSource.moduleName
            if mn == 'BufferedDataSource':
                mn = self.image.dataSource.dataSource.moduleName

            if 'Splitter' in fitMod:
                ft = remFitBuf.fitTask(self.image.seriesName,
                                       zps[i],
                                       detThresh,
                                       MetaDataHandler.NestedClassMDHandler(
                                           self.image.mdh),
                                       'SplitterObjFindR',
                                       bgindices=bgi,
                                       SNThreshold=True,
                                       dataSourceModule=mn)
            else:
                ft = remFitBuf.fitTask(self.image.seriesName,
                                       zps[i],
                                       detThresh,
                                       MetaDataHandler.NestedClassMDHandler(
                                           self.image.mdh),
                                       'LatObjFindFR',
                                       bgindices=bgi,
                                       SNThreshold=True,
                                       dataSourceModule=mn)

            res = ft(taskQueue=self.tq)
            xp = floor(i / 4) / 3.
            yp = (3 - i % 4) / 4.
            #print xp, yp
            axes((xp, yp, 1. / 6, 1. / 4.5))
            #d = ds[zps[i], :,:].squeeze().T
            d = self.image.dataSource.getSlice(zps[i]).T
            imshow(d,
                   cmap=cm.hot,
                   interpolation='nearest',
                   hold=False,
                   clim=(median(d.ravel()), d.max()))
            title('Frame %d' % zps[i])
            xlim(0, d.shape[1])
            ylim(0, d.shape[0])
            xticks([])
            yticks([])
            #print 'i = %d, ft.index = %d' % (i, ft.index)
            #subplot(4,6,2*i+13)
            xp += 1. / 6
            axes((xp, yp, 1. / 6, 1. / 4.5))
            d = ft.ofd.filteredData.T
            #d = ft.data.squeeze().T
            imshow(d,
                   cmap=cm.hot,
                   interpolation='nearest',
                   hold=False,
                   clim=(median(d.ravel()), d.max()))
            plot([p.x for p in ft.ofd], [p.y for p in ft.ofd],
                 'o',
                 mew=2,
                 mec='g',
                 mfc='none',
                 ms=9)
            if ft.driftEst:
                plot([p.x for p in ft.ofdDr], [p.y for p in ft.ofdDr],
                     'o',
                     mew=2,
                     mec='b',
                     mfc='none',
                     ms=9)
            if ft.fitModule in remFitBuf.splitterFitModules:
                plot([p.x for p in ft.ofd], [d.shape[0] - p.y for p in ft.ofd],
                     'o',
                     mew=2,
                     mec='g',
                     mfc='none',
                     ms=9)
            axis('tight')
            xlim(0, d.shape[1])
            ylim(0, d.shape[0])
            xticks([])
            yticks([])
        show()
        matplotlib.interactive(True)
예제 #17
0
    def OnMakeComposites(self, event):
        import numpy as np

        dlg = CompositeDialog(self.dsviewer, self.dsviewer.image)
        imageNames = dlg.imageNames
        #dispNames = dlg.dispNames

        if dlg.ShowModal() == wx.ID_OK:
            #others = [dispNames[n] for n in dlg.GetSelections()]
            others = dlg.GetSelections()
            #master, mchan = _getImage(dlg.GetMaster())
            ignoreZ = dlg.GetIgnoreZ()
            interp = dlg.GetInterp()
            if interp:
                order = 3
            else:
                order = 0

            shape, origin, voxelsize = dlg.shape, dlg.origin, dlg.voxelsize
            print((shape, origin, voxelsize))

            if len(others) > 0:
                newNames = []
                newData = []

                for otherN in others:
                    other, chan = _getImage(otherN)

                    try:
                        cn = other.mdh.getEntry('ChannelNames')[chan]
                        otherName = '%s - %s' % (os.path.split(
                            other.filename)[1], cn)
                    except:
                        if other.data.shape[3] == 1:
                            otherName = os.path.split(other.filename)[1]
                        else:
                            otherName = '%s -  %d' % (os.path.split(
                                other.filename)[1], chan)

                    newNames.append(otherName)

                    if isinstance(other.data, dataWrap.ListWrap):
                        od = other.data.dataList[chan]
                    else:
                        od = other.data

                    if ignoreZ:
                        originsEqual = np.allclose(other.origin[:2],
                                                   origin[:2],
                                                   atol=1)
                    else:
                        originsEqual = np.allclose(other.origin,
                                                   origin,
                                                   atol=1)

                    shiftField = dlg.GetShiftmap(otherN)

                    print shape[:3], other.data.shape[:3]

                    if (not np.allclose(
                            other.pixelSize, voxelsize[0], rtol=.001)) or (
                                not (other.data.shape[:3] == shape[:3])) or (
                                    not originsEqual) or shiftField:
                        #need to rescale ...
                        print(('Remapping ', otherN, originsEqual,
                               other.origin,
                               np.allclose(other.pixelSize,
                                           voxelsize[0],
                                           rtol=.001),
                               (not (other.data.shape[:3] == shape[:3])),
                               shiftField, other.pixelSize, ignoreZ))
                        #print origin, voxelsize
                        od = self.RemapData(other,
                                            chan,
                                            shape,
                                            voxelsize,
                                            origin,
                                            shiftField=shiftField,
                                            ignoreZ=ignoreZ,
                                            order=order)

                    newData += [od]

                pre = common_prefix(newNames)
                print(pre)
                lPre = len(pre)
                newNames = [n[lPre:] for n in newNames]

                mdh = MetaDataHandler.NestedClassMDHandler(self.image.mdh)
                mdh.setEntry('ChannelNames', newNames)

                mdh['voxelsize.x'] = voxelsize[0] / 1e3
                mdh['voxelsize.y'] = voxelsize[1] / 1e3
                mdh['voxelsize.z'] = voxelsize[2] / 1e3

                mdh['Origin.x'] = origin[0]
                mdh['Origin.y'] = origin[1]
                mdh['Origin.z'] = origin[2]

                View3D(dataWrap.ListWrap(newData, 3),
                       'Composite',
                       mdh=mdh,
                       mode=self.dsviewer.mode,
                       parent=wx.GetTopLevelParent(self.dsviewer),
                       glCanvas=self.dsviewer.glCanvas)

        dlg.Destroy()
예제 #18
0
 def getMetadata(self):
     return pickle.dumps(MetaDataHandler.NestedClassMDHandler(self.mdh))
예제 #19
0
    def __init__(self,
                 data=None,
                 mdh=None,
                 filename=None,
                 queueURI=None,
                 events=[],
                 titleStub='Untitled Image',
                 haveGUI=True):
        ''' Create an Image Stack.
        
        This is a essentially a wrapper of the image data and any ascociated 
        metadata. The class can be given a ndarray like* data source, or  
        alternatively supports loading from file, or from a PYME task queue 
        URI, in which case individual slices will be fetched from the server 
        as required.  
        
        For details on the file type support, see the Load method.
        
        You should provide one of the 'data' or 'filename' parmeters,
        with all other parameters being optional.
        
        Parameters:
            data    Image data. Something that supports ndarray like slicing and exposes
                    a .shape parameter, something implementing the
                    PYME.Analysis.DataSources interface, or a list of either
                    of the above. Dimensionality can be between 1 and 4, with
                    the dimensions being interpreted as x, y, z/t, colour.
                    A mangled (will support slicing, but not necessarily other array 
                    operations) version of the data will be stored as the .data 
                    member of the class.
                    
            mdh     something derived from PYME.Acquire.MetaDataHandler.MDHandlerBase
                    If None, and empty one will be created.
            
            filename    filename of the data to load (see Load), or PYME queue identifier
            
            queueURI    PYRO URI of the task server. This exists to optionally speed up 
                        loading from a queue by eliminating the PYRO nameserver
                        lookup. The queue name itself should be passed in the filename,
                        with a leading QUEUE://.
            
            events      An array of time series events (TODO - more doc)
            
            haveGUI     Whether we have a wx GUI available, so that we can
                        display dialogs asking for, e.g. a file name if no
                        data or filename is supplied, or for missing metadata
                        entries
                    
        '''
        global nUntitled
        self.data = data  #image data
        self.mdh = mdh  #metadata (a MetaDataHandler class)
        self.events = events  #events

        self.queueURI = queueURI
        self.filename = filename

        self.haveGUI = haveGUI

        #default 'mode' / image type - see PYME/DSView/modules/__init__.py
        self.mode = 'LM'

        self.saved = False
        self.volatile = False  #is the data likely to change and need refreshing?

        #s408
        if (data is None):
            #if we've supplied data, use that, otherwise load from file
            self.Load(filename)

        #do the necessary munging to get the data in the format we want it
        self.SetData(self.data)

        #generate a placeholder filename / window title
        if self.filename == None:
            self.filename = '%s %d' % (titleStub, nUntitled[titleStub])
            nUntitled[titleStub] += 1

            self.seriesName = self.filename

        #generate some empty metadata if we don't have any
        if self.mdh == None:
            self.mdh = MetaDataHandler.NestedClassMDHandler()

        #hack to make spectral data behave right - doesn't really belong here
        if 'Spectrum.Wavelengths' in self.mdh.getEntryNames():
            self.xvals = self.mdh['Spectrum.Wavelengths']
            self.xlabel = 'Wavelength [nm]'

        #if we have 1D data, plot as graph rather than image
        if self.data.shape[1] == 1:
            self.mode = 'graph'

        #add ourselves to the list of open images
        openImages[self.filename] = self
예제 #20
0
    def OnDeconvICTM(self, event, beadMode=False):
        from PYME.Deconv.deconvDialogs import DeconvSettingsDialog, DeconvProgressDialog, DeconvProgressPanel

        dlg = DeconvSettingsDialog(self.dsviewer, beadMode,
                                   self.image.data.shape[3])
        if dlg.ShowModal() == wx.ID_OK:
            from PYME.Deconv import dec, decThread, richardsonLucy
            nIter = dlg.GetNumIterationss()
            regLambda = dlg.GetRegularisationLambda()

            decMDH = MetaDataHandler.NestedClassMDHandler(self.image.mdh)
            decMDH['Deconvolution.NumIterations'] = nIter
            decMDH['Deconvolution.OriginalFile'] = self.image.filename

            #self.dlgDeconProg = DeconvProgressDialog(self.dsviewer, nIter)
            #self.dlgDeconProg.Show()
            vx = self.image.mdh.getEntry('voxelsize.x')
            vy = self.image.mdh.getEntry('voxelsize.y')
            vz = self.image.mdh.getEntry('voxelsize.z')

            if beadMode:
                from PYME.Deconv import beadGen
                psf = beadGen.genBeadImage(dlg.GetBeadRadius(),
                                           (1e3 * vx, 1e3 * vy, 1e3 * vz))

                decMDH['Deconvolution.BeadRadius'] = dlg.GetBeadRadius()

            else:
                #psf, vs = numpy.load(dlg.GetPSFFilename())
                #psf = numpy.atleast_3d(psf)
                psfFilename, psf, vs = dlg.GetPSF(vshint=vx)

                decMDH['Deconvolution.PSFFile'] = dlg.GetPSFFilename()

                if not (vs.x == vx and vs.y == vy and vs.z == vz):
                    #rescale psf to match data voxel size
                    psf = ndimage.zoom(psf, [vs.x / vx, vs.y / vy, vs.z / vz])

            data = self.image.data[:, :, :, dlg.GetChannel()].astype(
                'f') - dlg.GetOffset()
            decMDH['Deconvolution.Offset'] = dlg.GetOffset()

            bg = dlg.GetBackground()
            decMDH['Deconvolution.Background'] = bg

            #crop PSF in z if bigger than stack

            print psf.shape, data.shape
            if psf.shape[2] > data.shape[2]:
                dz = psf.shape[2] - data.shape[2]

                psf = psf[:, :,
                          numpy.floor(dz / 2):(psf.shape[2] -
                                               numpy.ceil(dz / 2))]

            print((data.shape, psf.shape))

            if dlg.GetPadding():
                padsize = numpy.array(dlg.GetPadSize())
                decMDH['Deconvolution.Padding'] = padsize
                dp = numpy.ones(numpy.array(data.shape) + 2 * padsize,
                                'f') * data.mean()
                weights = numpy.zeros_like(dp)
                px, py, pz = padsize

                #print data.shape, dp[px:-(px+1), py:-(py+1), pz:-(pz+1)].shape
                dp[px:-px, py:-py, pz:-pz] = data
                #if dlg.GetRemovePadding():
                #    data = dp[px:-px, py:-py, pz:-pz]#should be a slice
                #else:
                #    data = dp
                weights[px:-px, py:-py, pz:-pz] = 1.

                weights = weights.ravel()
            else:
                dp = data
                weights = 1

            if dlg.GetBlocking():
                decMDH['Deconvolution.Method'] = 'Blocked ICTM'
                self.checkTQ()
                from PYME.Deconv import tq_block_dec
                bs = dlg.GetBlockSize()
                self.decT = tq_block_dec.blocking_deconv(self.tq,
                                                         data,
                                                         psf,
                                                         self.image.seriesName,
                                                         blocksize={
                                                             'y': bs,
                                                             'x': bs,
                                                             'z': 256
                                                         })
                self.decT.go()

            else:
                decMDH['Deconvolution.Method'] = dlg.GetMethod()
                if dlg.GetMethod() == 'ICTM':
                    decMDH['Deconvolution.RegularisationParameter'] = regLambda
                    if beadMode:
                        self.dec = dec.dec_bead()
                    else:
                        self.dec = dec.dec_conv()
                else:
                    if beadMode:
                        self.dec = richardsonLucy.rlbead()
                    else:
                        self.dec = richardsonLucy.dec_conv()

                self.dec.psf_calc(psf, dp.shape)

                self.decT = decThread.decThread(self.dec,
                                                dp,
                                                regLambda,
                                                nIter,
                                                weights,
                                                bg=bg)
                self.decT.start()

                tries = 0
                while tries < 10 and not hasattr(self.dec, 'fs'):
                    time.sleep(1)
                    tries += 1

                if dlg.GetPadding() and dlg.GetRemovePadding():
                    fs = self.dec.fs[px:-px, py:-py, pz:-pz]
                else:
                    fs = self.dec.fs

                im = ImageStack(data=fs,
                                mdh=decMDH,
                                titleStub='Deconvolution Result')
                mode = 'lite'
                if beadMode:
                    mode = 'psf'
                    im.defaultExt = '*.psf'  #we want to save as PSF by default
                self.res = ViewIm3D(im,
                                    mode=mode,
                                    parent=wx.GetTopLevelParent(self.dsviewer))

                #self.res = View3D(fs, 'Deconvolution Result', mdh=decMDH, parent=wx.GetTopLevelParent(self.dsviewer), mode=mode)

                self.dlgDeconProg = DeconvProgressPanel(self.res, nIter)

                self.pinfo1 = aui.AuiPaneInfo().Name("deconvPanel").Top(
                ).Caption('Deconvolution Progress').DestroyOnClose(
                    True
                ).CloseButton(
                    False
                )  #.MinimizeButton(True).MinimizeMode(aui.AUI_MINIMIZE_CAPT_SMART|aui.AUI_MINIMIZE_POS_RIGHT)#.CaptionVisible(False)
                self.res._mgr.AddPane(self.dlgDeconProg, self.pinfo1)
                self.res._mgr.Update()

            self.deconTimer = mytimer()
            self.deconTimer.WantNotification.append(self.OnDeconTimer)

            self.deconTimer.Start(500)
예제 #21
0
    def OnDeconvMovie(self, event, beadMode=False):
        from PYME.Deconv.deconvDialogs import DeconvSettingsDialog  #,DeconvProgressDialog,DeconvProgressPanel
        #import multiprocessing

        dlg = DeconvSettingsDialog(self.dsviewer, beadMode,
                                   self.image.data.shape[3])
        if dlg.ShowModal() == wx.ID_OK:
            from PYME.Deconv import dec, richardsonLucy  #, decThread
            nIter = dlg.GetNumIterationss()
            regLambda = dlg.GetRegularisationLambda()

            decMDH = MetaDataHandler.NestedClassMDHandler(self.image.mdh)
            decMDH['Deconvolution.NumIterations'] = nIter
            decMDH['Deconvolution.OriginalFile'] = self.image.filename

            #self.dlgDeconProg = DeconvProgressDialog(self.dsviewer, nIter)
            #self.dlgDeconProg.Show()
            vx = self.image.mdh.getEntry('voxelsize.x')
            vy = self.image.mdh.getEntry('voxelsize.y')
            vz = self.image.mdh.getEntry('voxelsize.z')

            if beadMode:
                from PYME.Deconv import beadGen
                psf = beadGen.genBeadImage(dlg.GetBeadRadius(),
                                           (1e3 * vx, 1e3 * vy, 1e3 * vz))

                decMDH['Deconvolution.BeadRadius'] = dlg.GetBeadRadius()

            else:
                #psf, vs = numpy.load(dlg.GetPSFFilename())
                #psf = numpy.atleast_3d(psf)

                psfFilename, psf, vs = dlg.GetPSF(vshint=vx)

                decMDH['Deconvolution.PSFFile'] = psfFilename

                if not (vs.x == vx and vs.y == vy):  # and vs.z ==vz):
                    #rescale psf to match data voxel size
                    psf = ndimage.zoom(psf, [vs.x / vx, vs.y / vy, 1])

            data = self.image.data[:, :, :, dlg.GetChannel()].astype(
                'f') - dlg.GetOffset()
            decMDH['Deconvolution.Offset'] = dlg.GetOffset()

            bg = dlg.GetBackground()
            decMDH['Deconvolution.Background'] = bg

            #crop PSF in z if bigger than stack

            #print psf.shape, data.shape
            if psf.shape[2] > data.shape[2]:
                dz = psf.shape[2] - data.shape[2]

                psf = psf[:, :,
                          numpy.floor(dz / 2):(psf.shape[2] -
                                               numpy.ceil(dz / 2))]

            print((data.shape, psf.shape))

            dp = numpy.array(data)
            weights = 1

            if dlg.GetBlocking():
                decMDH['Deconvolution.Method'] = 'Blocked ICTM'
                self.checkTQ()
                from PYME.Deconv import tq_block_dec
                bs = dlg.GetBlockSize()
                self.decT = tq_block_dec.blocking_deconv(self.tq,
                                                         data,
                                                         psf,
                                                         self.image.seriesName,
                                                         blocksize={
                                                             'y': bs,
                                                             'x': bs,
                                                             'z': 256
                                                         })
                self.decT.go()

            else:
                decMDH['Deconvolution.Method'] = dlg.GetMethod()
                if dlg.GetMethod() == 'ICTM':
                    decMDH['Deconvolution.RegularisationParameter'] = regLambda
                    if beadMode:
                        self.dec = dec.dec_bead()
                    else:
                        self.dec = dec.dec_conv()
                else:
                    if beadMode:
                        self.dec = richardsonLucy.rlbead()
                    else:
                        self.dec = richardsonLucy.dec_conv()

                self.dec.psf_calc(psf, dp[:, :, 0:1].shape)

                #print dp.__class__

                #self.decT = decThread.decThread(self.dec, dp, regLambda, nIter, weights)
                #self.decT.start()

                #                p = multiprocessing.Pool()
                #                slices = [(self.dec, psf, dp[:,:,i:(i+1)],regLambda, nIter, weights)  for i in range(dp.shape[2])]
                #                r = p.map(_pt, slices)
                #                res = numpy.concatenate(r, 2)

                res = numpy.concatenate([
                    self.dec.deconv(
                        dp[:, :, i:(i + 1)], regLambda, nIter, weights,
                        bg=bg).reshape(self.dec.shape)
                    for i in range(dp.shape[2])
                ], 2)

                im = ImageStack(data=res,
                                mdh=decMDH,
                                titleStub='Deconvolution Result')
                mode = 'lite'

                self.res = ViewIm3D(im,
                                    mode=mode,
                                    parent=wx.GetTopLevelParent(self.dsviewer))
예제 #22
0
    def OpenFile(self, filename='', ds=None, **kwargs):
        '''Open a file - accepts optional keyword arguments for use with files
        saved as .txt and .mat. These are:
            
            FieldNames: a list of names for the fields in the text file or
                        matlab variable.
            VarName:    the name of the variable in the .mat file which 
                        contains the data.
            SkipRows:   Number of header rows to skip for txt file data
            
            PixelSize:  Pixel size if not in nm
            
        '''

        #close any files we had open previously
        while len(self.filesToClose) > 0:
            self.filesToClose.pop().close()

        #clear our state
        self.dataSources = []
        if 'zm' in dir(self):
            del self.zm
        self.filter = None
        self.mapping = None
        self.colourFilter = None
        self.events = None
        self.mdh = MetaDataHandler.NestedClassMDHandler()

        self.filename = filename

        if not ds is None:
            self.selectedDataSource = ds
            self.dataSources.append(ds)
        elif os.path.splitext(filename)[1] == '.h5r':
            try:
                self.selectedDataSource = inpFilt.h5rSource(filename)
                self.dataSources.append(self.selectedDataSource)

                self.filesToClose.append(self.selectedDataSource.h5f)

                if 'DriftResults' in self.selectedDataSource.h5f.root:
                    self.dataSources.append(
                        inpFilt.h5rDSource(self.selectedDataSource.h5f))

                    if len(self.selectedDataSource['x']) == 0:
                        self.selectedDataSource = self.dataSources[-1]

            except:  #fallback to catch series that only have drift data
                self.selectedDataSource = inpFilt.h5rDSource(filename)
                self.dataSources.append(self.selectedDataSource)

                self.filesToClose.append(self.selectedDataSource.h5f)

            #catch really old files which don't have any metadata
            if 'MetaData' in self.selectedDataSource.h5f.root:
                self.mdh = MetaDataHandler.HDFMDHandler(
                    self.selectedDataSource.h5f)

            if ('Events' in self.selectedDataSource.h5f.root) and (
                    'StartTime' in self.mdh.keys()):
                self.events = self.selectedDataSource.h5f.root.Events[:]

        elif os.path.splitext(filename)[1] == '.mat':  #matlab file
            ds = inpFilt.matfileSource(filename, kwargs['FieldNames'],
                                       kwargs['VarName'])
            self.selectedDataSource = ds
            self.dataSources.append(ds)

        elif os.path.splitext(filename)[1] == '.csv':
            #special case for csv files - tell np.loadtxt to use a comma rather than whitespace as a delimeter
            if 'SkipRows' in kwargs.keys():
                ds = inpFilt.textfileSource(filename,
                                            kwargs['FieldNames'],
                                            delimiter=',',
                                            skiprows=kwargs['SkipRows'])
            else:
                ds = inpFilt.textfileSource(filename,
                                            kwargs['FieldNames'],
                                            delimiter=',')
            self.selectedDataSource = ds
            self.dataSources.append(ds)

        else:  #assume it's a tab (or other whitespace) delimited text file
            if 'SkipRows' in kwargs.keys():
                ds = inpFilt.textfileSource(filename,
                                            kwargs['FieldNames'],
                                            skiprows=kwargs['SkipRows'])
            else:
                ds = inpFilt.textfileSource(filename, kwargs['FieldNames'])
            self.selectedDataSource = ds
            self.dataSources.append(ds)

        #wrap the data source with a mapping so we can fiddle with things
        #e.g. combining z position and focus
        self.inputMapping = inpFilt.mappingFilter(self.selectedDataSource)
        self.selectedDataSource = self.inputMapping
        self.dataSources.append(self.inputMapping)

        if 'PixelSize' in kwargs.keys():
            self.selectedDataSource.pixelSize = kwargs['PixelSize']
            self.selectedDataSource.setMapping('x', 'x*pixelSize')
            self.selectedDataSource.setMapping('y', 'y*pixelSize')

        #Retrieve or estimate image bounds
        if 'Camera.ROIWidth' in self.mdh.getEntryNames():
            x0 = 0
            y0 = 0

            x1 = self.mdh.getEntry(
                'Camera.ROIWidth') * 1e3 * self.mdh.getEntry('voxelsize.x')
            y1 = self.mdh.getEntry(
                'Camera.ROIHeight') * 1e3 * self.mdh.getEntry('voxelsize.y')

            if 'Splitter' in self.mdh.getEntry('Analysis.FitModule'):
                if 'Splitter.Channel0ROI' in self.mdh.getEntryNames():
                    rx0, ry0, rw, rh = self.mdh['Splitter.Channel0ROI']
                    x1 = rw * 1e3 * self.mdh.getEntry('voxelsize.x')
                    x1 = rh * 1e3 * self.mdh.getEntry('voxelsize.x')
                else:
                    y1 = y1 / 2

            self.imageBounds = ImageBounds(x0, y0, x1, y1)
        else:
            self.imageBounds = ImageBounds.estimateFromSource(
                self.selectedDataSource)

        #extract information from any events
        self._processEvents()

        #handle special cases which get detected by looking for the presence or
        #absence of certain variables in the data.
        if 'fitResults_Ag' in self.selectedDataSource.keys():
            #if we used the splitter set up a number of mappings e.g. total amplitude and ratio
            self._processSplitter()

        if 'fitResults_ratio' in self.selectedDataSource.keys():
            #if we used the splitter set up a number of mappings e.g. total amplitude and ratio
            self._processPriSplit()

        if 'fitResults_sigxl' in self.selectedDataSource.keys():
            #fast, quickpalm like astigmatic fitting
            self.selectedDataSource.setMapping(
                'sig', 'fitResults_sigxl + fitResults_sigyu')
            self.selectedDataSource.setMapping(
                'sig_d', 'fitResults_sigxl - fitResults_sigyu')

            self.selectedDataSource.dsigd_dz = -30.
            self.selectedDataSource.setMapping('fitResults_z0',
                                               'dsigd_dz*sig_d')

        if not 'y' in self.selectedDataSource.keys():
            self.selectedDataSource.setMapping('y', '10*t')

        #set up correction for foreshortening and z focus stepping
        if not 'foreShort' in dir(self.selectedDataSource):
            self.selectedDataSource.foreShort = 1.

        if not 'focus' in self.selectedDataSource.mappings.keys():
            self.selectedDataSource.focus = np.zeros(
                self.selectedDataSource['x'].shape)

        if 'fitResults_z0' in self.selectedDataSource.keys():
            self.selectedDataSource.setMapping(
                'z', 'fitResults_z0 + foreShort*focus')
        elif not 'z' in self.selectedDataSource.keys():
            self.selectedDataSource.setMapping('z', 'foreShort*focus')

        #Fit module specific filter settings
        if 'Analysis.FitModule' in self.mdh.getEntryNames():
            fitModule = self.mdh['Analysis.FitModule']

            print 'fitModule = %s' % fitModule

            if 'Interp' in fitModule:
                self.filterKeys['A'] = (5, 100000)

            if 'LatGaussFitFR' in fitModule:
                self.selectedDataSource.nPhot = getPhotonNums(
                    self.selectedDataSource, self.mdh)
                self.selectedDataSource.setMapping('nPhotons', 'nPhot')

            if fitModule == 'SplitterShiftEstFR':
                self.filterKeys['fitError_dx'] = (0, 10)
                self.filterKeys['fitError_dy'] = (0, 10)

        #remove any keys from the filter which are not present in the data
        for k in self.filterKeys.keys():
            if not k in self.selectedDataSource.keys():
                self.filterKeys.pop(k)

        self.Rebuild()

        if 'Sample.Labelling' in self.mdh.getEntryNames(
        ) and 'gFrac' in self.selectedDataSource.keys():
            self.SpecFromMetadata()