def run(self):
        # For debugging purposes
        experiment.lastExperiment = self
        
        self.sanityCheckEnvironment()
        self.prepareHandlers()

        self.cameraToReadoutTime = dict([(c, c.getTimeBetweenExposures(isExact = True)) for c in self.cameras])
        for camera, readTime in self.cameraToReadoutTime.items():
            if type(readTime) is not decimal.Decimal:
                raise RuntimeError("Camera %s did not provide an exact (decimal.Decimal) readout time" % camera.name)

        for camera, func in self.camToFunc.items():
            events.subscribe(events.NEW_IMAGE % camera.name, func)
        for exposureTime in self.exposureTimes:
            if self.shouldAbort:
                break
            self.camToImages = {}
            self.camToNumImagesReceived = {}
            self.camToLock = {}
            for camera in self.cameras:
                # Prepare a memory buffer to store images in.
                width, height = camera.getImageSize()
                self.camToImages[camera] = numpy.zeros((self.numExposures, height, width))
                self.camToNumImagesReceived[camera] = 0
                self.camToLock[camera] = threading.Lock()
                # Indicate any frame transfer cameras for reset at start of
                # table.
                if camera.getExposureMode() == cockpit.handlers.camera.TRIGGER_AFTER:
                    self.cameraToIsReady[camera] = False
                
            self.table = self.generateActions(exposureTime)
            self.table.sort()
            self.examineActions()
            self.table.sort()
            self.table.enforcePositiveTimepoints()
            self.lastMinuteActions()
            self.doneReceivingThread = threading.Thread(target = self.waiter)
            self.doneReceivingThread.start()
            self.execute()
            
            if self.shouldAbort:
                break
            
            # Wait until it's been a short time after the last received image.
            self.doneReceivingThread.join()
            progress = cockpit.gui.progressDialog.ProgressDialog("Processing images", 
                    "Processing images for exposure time %.4f" % exposureTime,
                    parent = None)
            self.processImages(exposureTime)
            progress.Destroy()

        for camera, func in self.camToFunc.items():
            events.unsubscribe(events.NEW_IMAGE % camera.name, func)

        self.save()
        self.showResults()
        self.cleanup()
Beispiel #2
0
    def run(self):
        # For debugging purposes
        experiment.lastExperiment = self

        self.sanityCheckEnvironment()
        self.prepareHandlers()

        self.cameraToReadoutTime = dict([
            (c, c.getTimeBetweenExposures(isExact=True)) for c in self.cameras
        ])
        for camera, readTime in self.cameraToReadoutTime.items():
            if type(readTime) is not decimal.Decimal:
                raise RuntimeError(
                    "Camera %s did not provide an exact (decimal.Decimal) readout time"
                    % camera.name)

        # Start out with no-exposure-time images to get a measured offset.
        multiplier = 0
        for camera, func in self.camToFunc.items():
            events.subscribe(events.NEW_IMAGE % camera.name, func)
        activeCameras = set(self.cameras)
        for i in range(self.numCollections):
            if not activeCameras or self.shouldAbort:
                break
            print("Running with cams", activeCameras)
            self.camToImages = {}
            self.camToNumImagesReceived = {}
            self.camToLock = {}
            for camera in activeCameras:
                # Prepare a memory buffer to store images in.
                width, height = camera.getImageSize()
                self.camToImages[camera] = numpy.zeros(
                    (self.numExposures, height, width))
                self.camToNumImagesReceived[camera] = 0
                self.camToLock[camera] = threading.Lock()
                # Indicate any frame transfer cameras for reset at start of
                # table.
                if camera.getExposureMode(
                ) == cockpit.handlers.camera.TRIGGER_AFTER:
                    self.cameraToIsReady[camera] = False

            self.table = self.generateActions(multiplier, activeCameras)
            self.table.sort()
            self.examineActions()
            self.table.sort()
            self.table.enforcePositiveTimepoints()
            self.lastMinuteActions()
            self.doneReceivingThread = threading.Thread(target=self.waiter)
            self.doneReceivingThread.start()
            self.execute()
            # Wait until it's been a short time after the last received image.
            self.doneReceivingThread.join()

            if multiplier == 0:
                multiplier = decimal.Decimal(1)
            else:
                multiplier *= self.exposureMultiplier
            activeCameras = self.processImages(multiplier)
            print("Came out with active cams", activeCameras)

        for camera, func in self.camToFunc.items():
            events.unsubscribe(events.NEW_IMAGE % camera.name, func)

        if self.shouldAbort:
            # Don't bother processing images.
            self.cleanup()
            return

        results = []
        for camera in self.cameras:
            results.append(self.makeFit(self.camToAverages[camera]))
        results = numpy.array(results, dtype=numpy.float32)
        results.shape = len(
            self.cameras), 1, 2, results.shape[-2], results.shape[-1]

        # Construct a header for the image data.
        pixel_size = wx.GetApp().Objectives.GetPixelSize()
        wavelengths = [c.wavelength for c in self.cameras]
        header = cockpit.util.datadoc.makeHeaderFor(results,
                                                    XYSize=pixel_size,
                                                    ZSize=0,
                                                    wavelengths=wavelengths)

        filehandle = open(self.savePath, 'wb')
        cockpit.util.datadoc.writeMrcHeader(header, filehandle)
        filehandle.write(results)
        filehandle.close()

        self.cleanup()