def write_config(self, config_section=None): import ceed config = config_section if config_section is not None else \ self.nix_file.sections['app_config'] data = self.gather_config_data_dict() for k, v in data.items(): config[k] = yaml_dumps(v) config['ceed_version'] = yaml_dumps(ceed.__version__) self.has_unsaved = True
def upgrade_file(nix_file): import ceed version = yaml_dumps(ceed.__version__) if 'app_logs' not in nix_file.sections: nix_file.create_section('app_logs', 'log') nix_file.sections['app_logs']['log_data'] = '' nix_file.sections['app_logs']['notes'] = '' if 'fluorescent_images' not in nix_file.blocks: block = nix_file.create_block('fluorescent_images', 'image') sec = nix_file.create_section('fluorescent_images_metadata', 'metadata') block.metadata = sec for num in CeedDataWriterBase.get_blocks_experiment_numbers( nix_file.blocks): name = CeedDataWriterBase.get_experiment_block_name(num) metadata = nix_file.blocks[name].metadata if 'notes' not in metadata: metadata['notes'] = '' if 'save_time' not in metadata: metadata['save_time'] = '0' if 'ceed_version' not in metadata.sections['app_config']: metadata.sections['app_config']['ceed_version'] = version if 'ceed_version' not in nix_file.sections['app_config']: import ceed nix_file.sections['app_config']['ceed_version'] = version
def view_process_enter(read, write, settings, app_settings): '''Called by the second internal view process when it is created. This calls :meth:`ViewSideViewControllerBase.view_process_enter`. ''' from base_kivy_app.app import run_app from ceed.view.main import CeedViewApp app = None try: app = CeedViewApp() classes = app.get_config_instances() app.app_settings = {cls: app_settings[cls] for cls in classes} app.apply_app_settings() viewer = app.view_controller for k, v in settings.items(): setattr(viewer, k, v) viewer.queue_view_read = read viewer.queue_view_write = write Clock.schedule_interval(viewer.view_read, .25) Clock.schedule_once(viewer.prepare_view_window, 0) run_app(app) except Exception as e: if app is not None: app.handle_exception(e, exc_info=sys.exc_info()) else: exc_info = ''.join(traceback.format_exception(*sys.exc_info())) write.put_nowait(('exception', yaml_dumps((str(e), exc_info)))) finally: write.put_nowait(('eof', None))
def write_yaml_config(self, filename, overwrite=False, stages_only=False): if exists(filename) and not overwrite: raise ValueError('{} already exists'.format(filename)) data = yaml_dumps( self.gather_config_data_dict(stages_only=stages_only)) with open(filename, 'w') as fh: fh.write(data)
def on_config(self, *largs): s = '' for key, value in self.config.items(): key = str(key) s += '[b]' + key + '[/b]\n' s += '-' * len(key) + '\n' s += yaml_dumps(value) + '\n\n' self.config_str = s
def handle_exception(self, exception, exc_info=None): '''Called by the second process upon an error which is passed on to the main process. ''' if exc_info is not None: exc_info = ''.join(traceback.format_exception(*exc_info)) self.queue_view_write.put_nowait( ('exception', yaml_dumps((str(exception), exc_info))))
def format_config(self, config): s = '' for key, value in config.items(): key = str(key) s += '[b]' + key + '[/b]\n' s += '-' * len(key) + '\n' s += yaml_dumps(value) + '\n\n' self.config_str = s self.mea_config = self.data_storage.get_config_mea_matrix_string()
def write_fluorescent_image(self, block, img, postfix=''): group = block.create_group('fluorescent_image{}'.format(postfix), 'image') config = block.metadata.create_section( 'fluorescent_image_config{}'.format(postfix), 'image') config['size'] = yaml_dumps(img.get_size()) config['pix_fmt'] = img.get_pixel_format() config['linesizes'] = yaml_dumps(img.get_linesizes()) config['buffer_size'] = yaml_dumps(img.get_buffer_size()) group.metadata = config for i, plane in enumerate(img.to_bytearray()): image_data = block.create_data_array( 'fluorescent_image_plane{}_{}'.format(postfix, i), 'image', dtype=np.uint8, data=plane) group.data_arrays.append(image_data) return group
def request_stage_start(self, stage_name): '''Starts the stage either in the GUI when previewing or in the viewer. Look into immediately erroring out if already running. So that we don't overwrite the initial image if we're already running. ''' # needs to be set here so button is reset on fail self.stage_active = True self.last_cam_image = self.proj_pixels = self.proj_size = None self.initial_cam_image = None if not stage_name: self.stage_active = False raise ValueError('No stage specified') app = App.get_running_app() app.stages_container.\ copy_and_resample_experiment_stage(stage_name) app.dump_app_settings_to_file() app.load_app_settings_from_file() app.ceed_data.prepare_experiment( stage_name, app.stage_factory.stage_names[stage_name].get_stage_shape_names()) if self.propixx_lib: m = self.LED_mode self.set_led_mode(m) app.ceed_data.add_led_state(0, 'R' in m, 'G' in m, 'B' in m) self.set_pixel_mode(True) else: app.ceed_data.add_led_state(0, 1, 1, 1) if self.view_process is None: self.start_stage(stage_name, app.shape_factory.canvas) elif self.queue_view_read is not None: self.initial_cam_image = app.player.last_image self.queue_view_read.put_nowait( ('config', yaml_dumps(app.ceed_data.gather_config_data_dict()))) self.queue_view_read.put_nowait(('start_stage', stage_name)) else: self.stage_active = False raise ValueError('Already running stage')
def send_keyboard_up(self, key): '''Gets called by the window for every keyboard key release, which it passes on to the main GUI process. ''' self.queue_view_write.put_nowait(('key_up', yaml_dumps((key, ))))
def send_keyboard_down(self, key, modifiers): '''Gets called by the window for every keyboard key press, which it passes on to the main GUI process. ''' self.queue_view_write.put_nowait( ('key_down', yaml_dumps((key, list(modifiers)))))
def request_process_data(self, data_type, data): self.queue_view_write.put_nowait((data_type, yaml_dumps(data)))
def merge_data( filename, ceed_filename, mcs_filename, alignment_indices, notes='', notes_filename=None): if os.path.exists(filename): raise Exception('{} already exists'.format(filename)) if notes_filename and os.path.exists(notes_filename): with open(notes_filename, 'r') as fh: lines = fh.read() if notes and lines: notes += '\n' notes += lines mcs_f = McsPy.McsData.RawData(mcs_filename) if not len(mcs_f.recordings): raise Exception('There is no data in {}'.format(mcs_filename)) if len(mcs_f.recordings) > 1: raise Exception('There is more than one recording in {}'. format(mcs_filename)) copy2(ceed_filename, filename) f = nix.File.open(filename, nix.FileMode.ReadWrite) if 'app_logs' not in f.sections: f.create_section('app_logs', 'log') f.sections['app_logs']['notes'] = '' if f.sections['app_logs']['notes'] and notes: f.sections['app_logs']['notes'] += '\n' f.sections['app_logs']['notes'] += notes block = f.create_block( 'ceed_mcs_alignment', 'each row, r, contains the sample number ' 'in the mcs data corresponding with row r in ceed data.') for exp, indices in alignment_indices.items(): block.create_data_array( 'experiment_{}'.format(exp), 'experiment_{}'.format(exp), data=indices) streams = mcs_f.recordings[0].analog_streams num_channels = 0 for stream in streams.values(): if 128 in stream.channel_infos: num_channels += 1 elif 0 in stream.channel_infos: num_channels += len(stream.channel_infos) pbar = tqdm( total=num_channels, file=sys.stdout, unit_scale=1, unit='data channels') block = f.create_block('mcs_data', 'mcs_raw_experiment_data') block.metadata = section = f.create_section( 'mcs_metadata', 'Associated metadata for the mcs data') for stream_id, stream in streams.items(): if 128 in stream.channel_infos: pbar.update() block.create_data_array( 'digital_io', 'digital_io', data=np.array(stream.channel_data).squeeze()) elif 0 in stream.channel_infos: for i in stream.channel_infos: info = stream.channel_infos[i].info pbar.update() elec_sec = section.create_section( 'electrode_{}'.format(info['Label']), 'metadata for this electrode') for key, val in info.items(): if isinstance(val, np.generic): val = val.item() elec_sec[key] = yaml_dumps(val) freq = stream.channel_infos[i].sampling_frequency freq = freq.m_as(ureg.hertz).item() ts = stream.channel_infos[i].sampling_tick ts = ts.m_as(ureg.second).item() elec_sec['sampling_frequency'] = yaml_dumps(freq) elec_sec['sampling_tick'] = yaml_dumps(ts) data = np.array(stream.channel_data[i, :]) block.create_data_array( 'electrode_{}'.format(info['Label']), 'electrode_data', data=data) pbar.close() f.close()