def test_add_stimulus_presentations(nwbfile, stimulus_presentations_behavior, stimulus_timestamps, roundtrip, roundtripper, stimulus_templates: StimulusTemplate): nwb.add_stimulus_timestamps(nwbfile, stimulus_timestamps) nwb.add_stimulus_presentations(nwbfile, stimulus_presentations_behavior) nwb.add_stimulus_template(nwbfile=nwbfile, stimulus_template=stimulus_templates) # Add index for this template to NWB in-memory object: nwb_template = nwbfile.stimulus_template[stimulus_templates.image_set_name] compare = (stimulus_presentations_behavior['image_set'] == nwb_template.name) curr_stimulus_index = stimulus_presentations_behavior[compare] nwb.add_stimulus_index(nwbfile, curr_stimulus_index, nwb_template) if roundtrip: obt = roundtripper(nwbfile, BehaviorNwbApi) else: obt = BehaviorNwbApi.from_nwbfile(nwbfile) expected = stimulus_presentations_behavior.copy() expected['is_change'] = [False, True, True, True, True] obtained = obt.get_stimulus_presentations() pd.testing.assert_frame_equal(expected[sorted(expected.columns)], obtained[sorted(obtained.columns)], check_dtype=False)
def _add_stimulus_templates(nwbfile: NWBFile, stimulus_templates: StimulusTemplate, stimulus_presentations: pd.DataFrame): nwb.add_stimulus_template(nwbfile=nwbfile, stimulus_template=stimulus_templates) # Add index for this template to NWB in-memory object: nwb_template = nwbfile.stimulus_template[ stimulus_templates.image_set_name] stimulus_index = stimulus_presentations[ stimulus_presentations['image_set'] == nwb_template.name] nwb.add_stimulus_index(nwbfile, stimulus_index, nwb_template) return nwbfile
def test_add_stimulus_presentations(nwbfile, stimulus_presentations_behavior, stimulus_timestamps, roundtrip, roundtripper, stimulus_templates): nwb.add_stimulus_timestamps(nwbfile, stimulus_timestamps) nwb.add_stimulus_presentations(nwbfile, stimulus_presentations_behavior) for key, val in stimulus_templates.items(): nwb.add_stimulus_template(nwbfile, val, key) # Add index for this template to NWB in-memory object: nwb_template = nwbfile.stimulus_template[key] curr_stimulus_index = stimulus_presentations_behavior[stimulus_presentations_behavior['image_set'] == nwb_template.name] nwb.add_stimulus_index(nwbfile, curr_stimulus_index, nwb_template) if roundtrip: obt = roundtripper(nwbfile, BehaviorOphysNwbApi) else: obt = BehaviorOphysNwbApi.from_nwbfile(nwbfile) pd.testing.assert_frame_equal(stimulus_presentations_behavior, obt.get_stimulus_presentations(), check_dtype=False)
def to_nwb(self, nwbfile: NWBFile, stimulus_presentations: Presentations) -> NWBFile: stimulus_templates = self.value unwarped_images = [] warped_images = [] image_names = [] for image_name, image_data in stimulus_templates.items(): image_names.append(image_name) unwarped_images.append(image_data.unwarped) warped_images.append(image_data.warped) image_index = np.zeros(len(image_names)) image_index[:] = np.nan visual_stimulus_image_series = \ StimulusTemplateExtension( name=stimulus_templates.image_set_name, data=warped_images, unwarped=unwarped_images, control=list(range(len(image_names))), control_description=image_names, unit='NA', format='raw', timestamps=image_index) nwbfile.add_stimulus_template(visual_stimulus_image_series) # Add index for this template to NWB in-memory object: nwb_template = nwbfile.stimulus_template[ stimulus_templates.image_set_name] stimulus_index = stimulus_presentations.value[ stimulus_presentations.value[ 'image_set'] == nwb_template.name] nwb.add_stimulus_index(nwbfile, stimulus_index, nwb_template) return nwbfile
def save(self, session_object): nwbfile = NWBFile( session_description=str(session_object.metadata['session_type']), identifier=str(session_object.ophys_experiment_id), session_start_time=session_object.metadata['experiment_datetime'], file_create_date=pytz.utc.localize(datetime.datetime.now())) # Add stimulus_timestamps to NWB in-memory object: nwb.add_stimulus_timestamps(nwbfile, session_object.stimulus_timestamps) # Add running data to NWB in-memory object: unit_dict = { 'v_sig': 'V', 'v_in': 'V', 'speed': 'cm/s', 'timestamps': 's', 'dx': 'cm' } nwb.add_running_data_df_to_nwbfile(nwbfile, session_object.running_data_df, unit_dict) # Add stimulus template data to NWB in-memory object: for name, image_data in session_object.stimulus_templates.items(): nwb.add_stimulus_template(nwbfile, image_data, name) # Add index for this template to NWB in-memory object: nwb_template = nwbfile.stimulus_template[name] stimulus_index = session_object.stimulus_presentations[ session_object.stimulus_presentations['image_set'] == nwb_template.name] nwb.add_stimulus_index(nwbfile, stimulus_index, nwb_template) # Add stimulus presentations data to NWB in-memory object: nwb.add_stimulus_presentations(nwbfile, session_object.stimulus_presentations) # Add trials data to NWB in-memory object: nwb.add_trials(nwbfile, session_object.trials, TRIAL_COLUMN_DESCRIPTION_DICT) # Add licks data to NWB in-memory object: if len(session_object.licks) > 0: nwb.add_licks(nwbfile, session_object.licks) # Add rewards data to NWB in-memory object: if len(session_object.rewards) > 0: nwb.add_rewards(nwbfile, session_object.rewards) # Add max_projection image data to NWB in-memory object: nwb.add_max_projection(nwbfile, session_object.max_projection) # Add average_image image data to NWB in-memory object: nwb.add_average_image(nwbfile, session_object.average_projection) # Add segmentation_mask_image image data to NWB in-memory object: nwb.add_segmentation_mask_image(nwbfile, session_object.segmentation_mask_image) # Add metadata to NWB in-memory object: nwb.add_metadata(nwbfile, session_object.metadata) # Add task parameters to NWB in-memory object: nwb.add_task_parameters(nwbfile, session_object.task_parameters) # Add roi metrics to NWB in-memory object: nwb.add_cell_specimen_table(nwbfile, session_object.cell_specimen_table) # Add dff to NWB in-memory object: nwb.add_dff_traces(nwbfile, session_object.dff_traces, session_object.ophys_timestamps) # Add corrected_fluorescence to NWB in-memory object: nwb.add_corrected_fluorescence_traces( nwbfile, session_object.corrected_fluorescence_traces) # Add motion correction to NWB in-memory object: nwb.add_motion_correction(nwbfile, session_object.motion_correction) # Write the file: with NWBHDF5IO(self.path, 'w') as nwb_file_writer: nwb_file_writer.write(nwbfile) return nwbfile