def execute(self, namespace): from PYME.experimental import labview_spooling_hacks from PYME.Analysis import piecewiseMapping im = namespace[self.inputImage] position, frames = labview_spooling_hacks.spoof_focus_from_metadata(im.mdh) frame_nums = np.arange(im.data.shape[2]) zm = piecewiseMapping.piecewiseMap(0, frames, position, im.mdh['Camera.CycleTime'], xIsSecs=False) z = zm[frame_nums] namespace[self.outputName] = tabular.ColumnSource(z=z)
def _processEvents(ds, events, mdh): """Read data from events table and translate it into mappings for, e.g. z position""" eventCharts = [] ev_mappings = {} if not events is None: evKeyNames = set() for e in events: evKeyNames.add(e['EventName']) if b'ProtocolFocus' in evKeyNames: zm = piecewiseMapping.GeneratePMFromEventList( events, mdh, mdh['StartTime'], mdh['Protocol.PiezoStartPos'], eventName=b'ProtocolFocus') ev_mappings['z_command'] = zm if b'PiezoOnTarget' in evKeyNames: # Sometimes we also emit PiezoOnTarget events with the actual piezo position, rather than where we # told it to go, use these preferentially # TODO - deprecate in favour of, e.g. 'FocusOnTarget' events which are offset-corrected - see issue 766 from PYME.Analysis import piezo_movement_correction spoofed_evts = piezo_movement_correction.spoof_focus_events_from_ontarget( events, mdh) zm = piecewiseMapping.GeneratePMFromEventList( spoofed_evts, mdh, mdh['StartTime'], mdh['Protocol.PiezoStartPos'], eventName=b'ProtocolFocus') ev_mappings['z_ontarget'] = zm ev_mappings[ 'piezo_moving'] = piecewiseMapping.bool_map_between_events( events, mdh, b'ProtocolFocus', b'PiezoOnTarget', default=False) # the z position we use for localizations gets the ontarget info if present ev_mappings['zm'] = zm eventCharts.append(('Focus [um]', zm, b'ProtocolFocus')) if b'ScannerXPos' in evKeyNames: x0 = 0 if 'Positioning.Stage_X' in mdh.getEntryNames(): x0 = mdh.getEntry('Positioning.Stage_X') xm = piecewiseMapping.GeneratePMFromEventList( events, mdh, mdh['StartTime'], x0, b'ScannerXPos', 0) ev_mappings['xm'] = xm eventCharts.append(('XPos [um]', xm, 'ScannerXPos')) if b'ScannerYPos' in evKeyNames: y0 = 0 if 'Positioning.Stage_Y' in mdh.getEntryNames(): y0 = mdh.getEntry('Positioning.Stage_Y') ym = piecewiseMapping.GeneratePMFromEventList( events, mdh, mdh.getEntry('StartTime'), y0, b'ScannerYPos', 0) ev_mappings['ym'] = ym eventCharts.append(('YPos [um]', ym, 'ScannerYPos')) if b'ShiftMeasure' in evKeyNames: driftx = piecewiseMapping.GeneratePMFromEventList( events, mdh, mdh.getEntry('StartTime'), 0, b'ShiftMeasure', 0) drifty = piecewiseMapping.GeneratePMFromEventList( events, mdh, mdh.getEntry('StartTime'), 0, b'ShiftMeasure', 1) driftz = piecewiseMapping.GeneratePMFromEventList( events, mdh, mdh.getEntry('StartTime'), 0, b'ShiftMeasure', 2) ev_mappings['driftx'] = driftx ev_mappings['drifty'] = drifty ev_mappings['driftz'] = driftz eventCharts.append(('X Drift [px]', driftx, 'ShiftMeasure')) eventCharts.append(('Y Drift [px]', drifty, 'ShiftMeasure')) eventCharts.append(('Z Drift [px]', driftz, 'ShiftMeasure')) # self.eventCharts = eventCharts # self.ev_mappings = ev_mappings elif all(k in mdh.keys() for k in [ 'StackSettings.FramesPerStep', 'StackSettings.StepSize', 'StackSettings.NumSteps', 'StackSettings.NumCycles' ]): # TODO - Remove this code - anytime we get here it's generally the result of an error in the input data # This should be handled upstream when spooling logger.warning( 'Spoofing focus from metadata: this usually implies an error in the input data (missing events) and results might vary' ) try: # if we dont have events file, see if we can use metadata to spoof focus from PYME.experimental import labview_spooling_hacks position, frames = labview_spooling_hacks.spoof_focus_from_metadata( mdh) zm = piecewiseMapping.piecewiseMap(0, frames, position, mdh['Camera.CycleTime'], xIsSecs=False) ev_mappings['z_command'] = zm eventCharts.append(('Focus [um]', zm, b'ProtocolFocus')) except: # It doesn't really matter if this fails, print our traceback anyway logger.exception('Error trying to fudge focus positions') return ev_mappings, eventCharts
def _processEvents(ds, events, mdh): """Read data from events table and translate it into mappings for, e.g. z position""" eventCharts = [] ev_mappings = {} if not events is None: evKeyNames = set() for e in events: evKeyNames.add(e['EventName']) if b'ProtocolFocus' in evKeyNames: zm = piecewiseMapping.GeneratePMFromEventList(events, mdh, mdh['StartTime'], mdh['Protocol.PiezoStartPos']) ev_mappings['zm'] = zm eventCharts.append(('Focus [um]', zm, b'ProtocolFocus')) if b'ScannerXPos' in evKeyNames: x0 = 0 if 'Positioning.Stage_X' in mdh.getEntryNames(): x0 = mdh.getEntry('Positioning.Stage_X') xm = piecewiseMapping.GeneratePMFromEventList(events, mdh, mdh['StartTime'], x0, b'ScannerXPos', 0) ev_mappings['xm'] = xm eventCharts.append(('XPos [um]', xm, 'ScannerXPos')) if b'ScannerYPos' in evKeyNames: y0 = 0 if 'Positioning.Stage_Y' in mdh.getEntryNames(): y0 = mdh.getEntry('Positioning.Stage_Y') ym = piecewiseMapping.GeneratePMFromEventList(events, mdh, mdh.getEntry('StartTime'), y0, b'ScannerYPos', 0) ev_mappings['ym'] = ym eventCharts.append(('YPos [um]', ym, 'ScannerYPos')) if b'ShiftMeasure' in evKeyNames: driftx = piecewiseMapping.GeneratePMFromEventList(events, mdh, mdh.getEntry('StartTime'), 0, b'ShiftMeasure', 0) drifty = piecewiseMapping.GeneratePMFromEventList(events, mdh, mdh.getEntry('StartTime'), 0, b'ShiftMeasure', 1) driftz = piecewiseMapping.GeneratePMFromEventList(events, mdh, mdh.getEntry('StartTime'), 0, b'ShiftMeasure', 2) ev_mappings['driftx'] = driftx ev_mappings['drifty'] = drifty ev_mappings['driftz'] = driftz eventCharts.append(('X Drift [px]', driftx, 'ShiftMeasure')) eventCharts.append(('Y Drift [px]', drifty, 'ShiftMeasure')) eventCharts.append(('Z Drift [px]', driftz, 'ShiftMeasure')) # self.eventCharts = eventCharts # self.ev_mappings = ev_mappings elif all(k in mdh.keys() for k in ['StackSettings.FramesPerStep', 'StackSettings.StepSize', 'StackSettings.NumSteps', 'StackSettings.NumCycles']): # TODO - Remove this code - anytime we get here it's generally the result of an error in the input data # This should be handled upstream when spooling logger.warning('Spoofing focus from metadata: this usually implies an error in the input data (missing events) and results might vary') try: # if we dont have events file, see if we can use metadata to spoof focus from PYME.experimental import labview_spooling_hacks position, frames = labview_spooling_hacks.spoof_focus_from_metadata(mdh) zm = piecewiseMapping.piecewiseMap(0, frames, position, mdh['Camera.CycleTime'], xIsSecs=False) ev_mappings['zm'] = zm eventCharts.append(('Focus [um]', zm, b'ProtocolFocus')) except: # It doesn't really matter if this fails, print our traceback anyway logger.exception('Error trying to fudge focus positions') return ev_mappings, eventCharts