def controller_read(self, *largs): '''Called periodically to serve the queue that receives messages from the second process. ''' read = self.queue_view_write while True: try: msg, value = read.get(False) if msg == 'eof': self.finish_stop_process() self.stage_end_cleanup() break elif msg == 'exception': e, exec_info = yaml_loads(value) App.get_running_app().handle_exception(e, exc_info=exec_info) elif msg in ('GPU', 'CPU', 'frame', 'frame_flip'): self.request_process_data(msg, yaml_loads(value)) elif msg == 'end_stage' and msg != 'response': self.stage_end_cleanup(value) elif msg == 'key_down': key, modifiers = yaml_loads(value) self.handle_key_press(key, modifiers) elif msg == 'key_up': key, = yaml_loads(value) self.handle_key_press(key, down=False) except Empty: break
def view_read(self, *largs): '''Communication between the two process occurs through queues, this is run periodically to serve the queue and read messages from the main GUI. ''' from kivy.core.window import Window read = self.queue_view_read write = self.queue_view_write while True: try: msg, value = read.get(False) if msg == 'eof': App.get_running_app().stop() break elif msg == 'config': app = App.get_running_app() if self.tick_event: raise Exception('Cannot configure while running stage') app.ceed_data.clear_existing_config_data() app.ceed_data.apply_config_data_dict(yaml_loads(value)) elif msg == 'start_stage': self.start_stage( value, App.get_running_app().get_display_canvas()) elif msg == 'end_stage': self.end_stage() elif msg == 'fullscreen': Window.fullscreen = self.fullscreen = value write.put_nowait(('response', msg)) except Empty: break
def read_config(self, config_section=None): from ceed.analysis import read_nix_prop config = config_section if config_section is not None else \ self.nix_file.sections['app_config'] data = {} for prop in config.props: data[prop.name] = yaml_loads(read_nix_prop(prop)) return data
def read_image_from_block(block, postfix=''): try: group = block.groups['fluorescent_image{}'.format(postfix)] except KeyError: return None planes = [np.array(d).tobytes() for d in group.data_arrays] img = Image(plane_buffers=planes, pix_fmt=group.metadata['pix_fmt'], size=yaml_loads(group.metadata['size'])) return img
def read_yaml_config(self, filename, stages_only=False, requires_app_settings=True): with open(filename, 'r') as fh: data = fh.read() data = yaml_loads(data) self.apply_config_data_dict( data, stages_only=stages_only, requires_app_settings=requires_app_settings) self.config_changed = True
def load_experiment(self, experiment): self._block = block = self._nix_file.blocks[ CeedDataWriterBase.get_experiment_block_name(experiment)] section = self._nix_file.sections['experiment{}_metadata'.format( experiment)] self.loaded_experiment = experiment self.experiment_stage_name = section['stage'] self.experiment_notes = section['notes'] if 'notes' in section else '' self.experiment_start_time = float( section['save_time']) if 'save_time' in section else 0 config = section.sections['app_config'] config_data = {} for prop in config.props: config_data[prop.name] = yaml_loads(read_nix_prop(prop)) view = self.view_controller = ViewControllerBase() ser = self.data_serializer = DataSerializerBase() func = self.function_factory = FunctionFactoryBase() register_all_functions(func) shape = self.shape_factory = CeedPaintCanvasBehavior() stage = self.stage_factory = StageFactoryBase(function_factory=func, shape_factory=shape) for name, obj in { 'view': view, 'serializer': ser, 'function': func }.items(): if name in config_data['app_settings']: apply_config(obj, config_data['app_settings'][name]) self.populate_config(config_data, shape, func, stage) self.experiment_cam_image = self.read_image_from_block(self._block) data = self.shapes_intensity = {} for item in block.data_arrays: if not item.name.startswith('shape_'): continue data[item.name[6:]] = item self.led_state = block.data_arrays['led_state'] if ('ceed_mcs_alignment' in self._nix_file.blocks and 'experiment_{}'.format(experiment) in self._nix_file.blocks['ceed_mcs_alignment'].data_arrays): self.electrode_intensity_alignment = self._nix_file.blocks[ 'ceed_mcs_alignment'].data_arrays['experiment_{}'.format( experiment)] else: self.electrode_intensity_alignment = None
def import_file(self, filename, stages_only=False): '''Loads the file's config data. ''' from ceed.analysis import read_nix_prop Logger.debug('Ceed Controller (storage): Importing "{}"'.format( self.filename)) data = {} f = nix.File.open(filename, nix.FileMode.ReadOnly) try: for prop in f.sections['app_config']: data[prop.name] = yaml_loads(read_nix_prop(prop)) finally: f.close() self.apply_config_data_dict(data, stages_only=stages_only) self.config_changed = True
def load_mcs_data(self): if self.electrodes_data: # already loaded return mcs_block = self._nix_file.blocks['mcs_data'] mcs_metadata = mcs_block.metadata self.electrode_dig_data = None if 'digital_io' in mcs_block.data_arrays: self.electrode_dig_data = mcs_block.data_arrays['digital_io'] electrode_data = self.electrodes_data = {} electrodes_metadata = self.electrodes_metadata = {} for item in mcs_block.data_arrays: if not item.name.startswith('electrode_'): continue electrode_data[item.name[10:]] = np.array(item) electrodes_metadata[item.name[10:]] = electrode_metadata = {} for prop in mcs_metadata.sections[item.name].props: electrode_metadata[prop.name] = yaml_loads(read_nix_prop(prop))
def read_ceed_digital_data(self, filename, experiment): from ceed.storage.controller import CeedDataWriterBase from ceed.analysis import read_nix_prop f = nix.File.open(filename, nix.FileMode.ReadOnly) block = f.blocks[ CeedDataWriterBase.get_experiment_block_name(experiment)] section = block.metadata start_t = datetime.datetime(1970, 1, 1) + \ datetime.timedelta(seconds=block.created_at) metadata = {} try: try: config = section.sections['app_config'] except KeyError: raise Exception( 'Did not find config in experiment info for experiment {}'. format(experiment)) for prop in config: metadata[prop.name] = yaml_loads(read_nix_prop(prop)) if not block.data_arrays['frame_bits'].shape or \ not block.data_arrays['frame_bits'].shape[0]: raise Exception('Experiment {} has no data'.format(experiment)) frame_bits = np.array(block.data_arrays['frame_bits']).squeeze() frame_counter = np.array(block.data_arrays['frame_counter'] ).squeeze() except Exception: f.close() raise else: f.close() self.ceed_data = { 'frame_bits': frame_bits, 'frame_counter': frame_counter, 'start_t': start_t} self.ceed_config_orig = metadata['app_settings']
def load_application_data(self): self.app_logs = self.app_notes = '' if 'app_logs' in self._nix_file.sections: self.app_logs = self._nix_file.sections['app_logs']['log_data'] self.app_notes = self._nix_file.sections['app_logs']['notes'] config = self._nix_file.sections['app_config'] config_data = {} for prop in config.props: config_data[prop.name] = yaml_loads(read_nix_prop(prop)) self.ceed_version = config_data.get('ceed_version', '') view = ViewControllerBase() ser = DataSerializerBase() func = FunctionFactoryBase() register_all_functions(func) shape = CeedPaintCanvasBehavior() stage = StageFactoryBase(function_factory=func, shape_factory=shape) for name, obj in { 'view': view, 'serializer': ser, 'function': func }.items(): if name in config_data['app_settings']: apply_config(obj, config_data['app_settings'][name]) self.populate_config(config_data, shape, func, stage) self.app_config = { 'view_controller': view, 'data_serializer': ser, 'function_factory': func, 'shape_factory': shape, 'stage_factory': stage, }