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()
def exposureMode(self, triggerType): """Set exposure mode. If the device set a softTrigger handler, subscribe to "dummy take image" if exposureMode is TRIGGER_SOFT, otherwise unsubscribe.""" self._exposureMode = triggerType softTrigger = self.callbacks.get('softTrigger', None) events.unsubscribe("dummy take image", softTrigger) if softTrigger: events.subscribe("dummy take image", softTrigger)
def toggleSyncViews(self, event=None): self.syncViews = not self.syncViews if self.syncViews: events.subscribe( events.SYNCED_VIEW, self.setView, ) else: events.unsubscribe( events.SYNCED_VIEW, self.setView, )
def disable(self): self.selector.SetLabel("No camera") self.selector.SetBackgroundColour((180, 180, 180)) self.selector.Refresh() if self.curCamera is not None: # Wrap this in a try/catch since it will fail if the initial # camera enabling failed. events.unsubscribe(events.NEW_IMAGE % self.curCamera.name, self.onImage) self.curCamera = None if self.canvas is not None: # Destroy the canvas. self.canvas.clear(shouldDestroy = True) self.canvas = None
def correctSensorlessImage(self, image, timestamp): del timestamp if len(self.correction_stack) < self.zernike_applied.shape[0]: logger.log.info("Correction image %i/%i" % ( len(self.correction_stack) + 1, self.zernike_applied.shape[0], )) # Store image for current applied phase self.correction_stack.append(np.ndarray.tolist(image)) wx.CallAfter(self.correctSensorlessProcessing) else: logger.log.error( "Failed to unsubscribe to camera events. Trying again.") events.unsubscribe( events.NEW_IMAGE % self.camera.name, self.correctSensorlessImage, )
def correctSensorlessProcessing(self): logger.log.info("Processing sensorless image") if len(self.correction_stack) < self.zernike_applied.shape[0]: if len(self.correction_stack) % self.numMes == 0: self.findAbberationAndCorrect() # Advance counter by 1 and apply next phase self.proxy.set_phase( self.zernike_applied[len(self.correction_stack), :], offset=self.actuator_offset, ) else: # Once all images have been obtained, unsubscribe logger.log.debug("Unsubscribing to camera %s events" % self.camera.name) events.unsubscribe( events.NEW_IMAGE % self.camera.name, self.correctSensorlessImage, ) self.findAbberationAndCorrect() log_correction_applied( self.correction_stack, self.zernike_applied, self.nollZernike, self.sensorless_correct_coef, self.actuator_offset, ) logger.log.debug("Actuator positions applied: %s", self.actuator_offset) self.proxy.send(self.actuator_offset) # Take image, but ensure it's called after the phase is applied time.sleep(0.1) wx.CallAfter(wx.GetApp().Imager.takeImage)
def cleanup(self): self.statusThread.shouldStop = True for i, camera in enumerate(self.cameras): events.unsubscribe(events.NEW_IMAGE % camera.name, self.lambdas[i]) events.unsubscribe(events.USER_ABORT, self.onAbort)
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()
def onClose(self, event): events.unsubscribe('image pixel info', self.onImagePixelInfo) event.Skip()
def onClose(self, evt): events.unsubscribe(events.SETTINGS_CHANGED % self.device, self.updateGrid) if evt.GetId() == wx.ID_OK: self.device.updateSettings(self.current) self.Close()
def cleanup(self): self.statusThread.shouldStop = True for i, camera in enumerate(self.cameras): events.unsubscribe('new image %s' % camera.name, self.lambdas[i]) events.unsubscribe('user abort', self.onAbort)