def test_add_events(tmp_path, nwbfile, roundtripper, roundtrip, cell_specimen_table, metadata_fixture, dff_traces, ophys_timestamps): # Need to add metadata, cell specimen table, dff traces first nwb.add_metadata(nwbfile, metadata_fixture, behavior_only=False) nwb.add_cell_specimen_table(nwbfile, cell_specimen_table, metadata_fixture) nwb.add_dff_traces(nwbfile, dff_traces, ophys_timestamps) events = pd.DataFrame({ 'events': [np.array([0., 0., .69]), np.array([.3, 0.0, .2])], 'filtered_events': [ np.array([0.0, 0.0, 0.22949295]), np.array([0.09977954, 0.08805513, 0.127039049]) ], 'lambda': [0., 1.0], 'noise_std': [.25, .3], 'cell_roi_id': [123, 321] }, index=pd.Index([42, 84], name='cell_specimen_id')) api = BehaviorOphysNwbApi.from_nwbfile(nwbfile) nwbfile = api.add_events( nwbfile=nwbfile, events=events ) if roundtrip: obt = roundtripper(nwbfile, BehaviorOphysNwbApi) else: obt = BehaviorOphysNwbApi.from_nwbfile(nwbfile) obtained = obt.get_events() pd.testing.assert_frame_equal(obtained, events, check_like=True)
def test_add_eye_tracking_rig_geometry_data_to_nwbfile(nwbfile, roundtripper, roundtrip, rig_geometry, expected): api = BehaviorOphysNwbApi.from_nwbfile(nwbfile) nwbfile = api.add_eye_tracking_rig_geometry_data_to_nwbfile(nwbfile, rig_geometry) if roundtrip: obt = roundtripper(nwbfile, BehaviorOphysNwbApi) else: obt = BehaviorOphysNwbApi.from_nwbfile(nwbfile) obtained_eye_rig_geometry = obt.get_eye_tracking_rig_geometry() assert obtained_eye_rig_geometry == expected
def test_add_partial_metadata(test_partial_metadata, roundtrip, roundtripper, cell_specimen_table, metadata_fixture, partial_metadata_fixture): if test_partial_metadata: meta = partial_metadata_fixture else: meta = metadata_fixture nwbfile = pynwb.NWBFile( session_description='asession', identifier='afile', session_start_time=meta['date_of_acquisition'] ) nwb.add_metadata(nwbfile, meta, behavior_only=False) if not test_partial_metadata: nwb.add_cell_specimen_table(nwbfile, cell_specimen_table, meta) if roundtrip: obt = roundtripper(nwbfile, BehaviorOphysNwbApi) else: obt = BehaviorOphysNwbApi.from_nwbfile(nwbfile) if not test_partial_metadata: metadata_obt = obt.get_metadata() else: with warnings.catch_warnings(record=True) as record: metadata_obt = obt.get_metadata() exp_warn_msg = "Could not locate 'ophys' module in NWB" print(record) assert record[0].message.args[0].startswith(exp_warn_msg) assert len(metadata_obt) == len(meta) for key, val in meta.items(): assert val == metadata_obt[key]
def test_get_corrected_fluorescence_traces( nwbfile, roundtrip, filter_invalid_rois, valid_roi_ids, roundtripper, dff_traces, corrected_fluorescence_traces, cell_specimen_table, metadata_fixture, ophys_timestamps): nwb.add_metadata(nwbfile, metadata_fixture, behavior_only=False) nwb.add_cell_specimen_table(nwbfile, cell_specimen_table, metadata_fixture) nwb.add_dff_traces(nwbfile, dff_traces, ophys_timestamps) nwb.add_corrected_fluorescence_traces(nwbfile, corrected_fluorescence_traces) if roundtrip: obt = roundtripper(nwbfile, BehaviorOphysNwbApi, filter_invalid_rois=filter_invalid_rois) else: obt = BehaviorOphysNwbApi.from_nwbfile( nwbfile, filter_invalid_rois=filter_invalid_rois) if filter_invalid_rois: corrected_fluorescence_traces = corrected_fluorescence_traces[ corrected_fluorescence_traces["cell_roi_id"].isin(valid_roi_ids)] print(corrected_fluorescence_traces) print(obt.get_corrected_fluorescence_traces()) pd.testing.assert_frame_equal( corrected_fluorescence_traces, obt.get_corrected_fluorescence_traces(), check_dtype=False)
def test_add_licks(nwbfile, roundtrip, roundtripper, licks): nwb.add_licks(nwbfile, licks) if roundtrip: obt = roundtripper(nwbfile, BehaviorOphysNwbApi) else: obt = BehaviorOphysNwbApi.from_nwbfile(nwbfile) pd.testing.assert_frame_equal(licks, obt.get_licks(), check_dtype=False)
def test_add_eye_tracking_data_to_nwbfile( tmp_path, nwbfile, eye_tracking_data, rig_geometry, roundtripper, roundtrip): api = BehaviorOphysNwbApi.from_nwbfile(nwbfile) nwbfile = api.add_eye_tracking_data_to_nwb( nwbfile=nwbfile, eye_tracking_df=eye_tracking_data, eye_tracking_rig_geometry=rig_geometry ) if roundtrip: obt = roundtripper(nwbfile, BehaviorOphysNwbApi) else: obt = BehaviorOphysNwbApi.from_nwbfile(nwbfile) obtained = obt.get_eye_tracking() pd.testing.assert_frame_equal(obtained, eye_tracking_data, check_like=True)
def test_segmentation_mask_image(nwbfile, roundtrip, roundtripper, segmentation_mask_image, image_api): nwb.add_segmentation_mask_image(nwbfile, segmentation_mask_image) if roundtrip: obt = roundtripper(nwbfile, BehaviorOphysNwbApi) else: obt = BehaviorOphysNwbApi.from_nwbfile(nwbfile) assert image_api.deserialize(segmentation_mask_image) == \ image_api.deserialize(obt.get_segmentation_mask_image())
def test_add_average_image(nwbfile, roundtrip, roundtripper, average_image, image_api): nwb.add_average_image(nwbfile, average_image) if roundtrip: obt = roundtripper(nwbfile, BehaviorOphysNwbApi) else: obt = BehaviorOphysNwbApi.from_nwbfile(nwbfile) assert image_api.deserialize(average_image) == \ image_api.deserialize(obt.get_average_projection())
def test_add_stimulus_timestamps(nwbfile, stimulus_timestamps, roundtrip, roundtripper): nwb.add_stimulus_timestamps(nwbfile, stimulus_timestamps) if roundtrip: obt = roundtripper(nwbfile, BehaviorOphysNwbApi) else: obt = BehaviorOphysNwbApi.from_nwbfile(nwbfile) np.testing.assert_array_almost_equal(stimulus_timestamps, obt.get_stimulus_timestamps())
def test_add_max_projection(nwbfile, roundtrip, roundtripper, max_projection, image_api): nwb.add_max_projection(nwbfile, max_projection) if roundtrip: obt = roundtripper(nwbfile, BehaviorOphysNwbApi) else: obt = BehaviorOphysNwbApi.from_nwbfile(nwbfile) assert image_api.deserialize(max_projection) == \ image_api.deserialize(obt.get_max_projection())
def test_add_running_speed_to_nwbfile(nwbfile, running_speed, roundtrip, roundtripper): nwbfile = nwb.add_running_speed_to_nwbfile(nwbfile, running_speed) if roundtrip: obt = roundtripper(nwbfile, BehaviorOphysNwbApi) else: obt = BehaviorOphysNwbApi.from_nwbfile(nwbfile) running_speed_obt = obt.get_running_speed() assert np.allclose(running_speed.timestamps, running_speed_obt.timestamps) assert np.allclose(running_speed.values, running_speed_obt.values)
def test_add_stimulus_templates(nwbfile, stimulus_templates, roundtrip, roundtripper): for key, val in stimulus_templates.items(): nwb.add_stimulus_template(nwbfile, val, key) if roundtrip: obt = roundtripper(nwbfile, BehaviorOphysNwbApi) else: obt = BehaviorOphysNwbApi.from_nwbfile(nwbfile) stimulus_templates_obt = obt.get_stimulus_templates() for key in set(stimulus_templates.keys()).union( set(stimulus_templates_obt.keys())): np.testing.assert_array_almost_equal(stimulus_templates[key], stimulus_templates_obt[key])
def test_get_motion_correction(nwbfile, roundtrip, roundtripper, motion_correction, ophys_timestamps, metadata, cell_specimen_table, dff_traces): nwb.add_metadata(nwbfile, metadata) nwb.add_cell_specimen_table(nwbfile, cell_specimen_table, metadata) nwb.add_dff_traces(nwbfile, dff_traces, ophys_timestamps) nwb.add_motion_correction(nwbfile, motion_correction) if roundtrip: obt = roundtripper(nwbfile, BehaviorOphysNwbApi) else: obt = BehaviorOphysNwbApi.from_nwbfile(nwbfile) pd.testing.assert_frame_equal(motion_correction, obt.get_motion_correction(), check_dtype=False)
def test_add_running_data_df_to_nwbfile(nwbfile, running_data_df, roundtrip, roundtripper): unit_dict = { 'v_sig': 'V', 'v_in': 'V', 'speed': 'cm/s', 'timestamps': 's', 'dx': 'cm' } nwbfile = nwb.add_running_data_df_to_nwbfile(nwbfile, running_data_df, unit_dict) if roundtrip: obt = roundtripper(nwbfile, BehaviorOphysNwbApi) else: obt = BehaviorOphysNwbApi.from_nwbfile(nwbfile) pd.testing.assert_frame_equal(running_data_df, obt.get_running_data_df())
def test_add_task_parameters(nwbfile, roundtrip, roundtripper, task_parameters): nwb.add_task_parameters(nwbfile, task_parameters) if roundtrip: obt = roundtripper(nwbfile, BehaviorOphysNwbApi) else: obt = BehaviorOphysNwbApi.from_nwbfile(nwbfile) task_parameters_obt = obt.get_task_parameters() assert len(task_parameters_obt) == len(task_parameters) for key, val in task_parameters.items(): if key == 'omitted_flash_fraction': if math.isnan(val): assert math.isnan(task_parameters_obt[key]) if math.isnan(task_parameters_obt[key]): assert math.isnan(val) else: assert val == task_parameters_obt[key]
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 test_get_dff_traces(nwbfile, roundtrip, filter_invalid_rois, valid_roi_ids, roundtripper, dff_traces, cell_specimen_table, metadata, ophys_timestamps): nwb.add_metadata(nwbfile, metadata) nwb.add_cell_specimen_table(nwbfile, cell_specimen_table, metadata) nwb.add_dff_traces(nwbfile, dff_traces, ophys_timestamps) if roundtrip: obt = roundtripper(nwbfile, BehaviorOphysNwbApi, filter_invalid_rois=filter_invalid_rois) else: obt = BehaviorOphysNwbApi.from_nwbfile( nwbfile, filter_invalid_rois=filter_invalid_rois) if filter_invalid_rois: dff_traces = dff_traces[dff_traces["cell_roi_id"].isin(valid_roi_ids)] pd.testing.assert_frame_equal(dff_traces, obt.get_dff_traces(), check_dtype=False)
def test_get_cell_specimen_table(nwbfile, roundtrip, filter_invalid_rois, valid_roi_ids, roundtripper, cell_specimen_table, metadata_fixture, ophys_timestamps): nwb.add_metadata(nwbfile, metadata_fixture, behavior_only=False) nwb.add_cell_specimen_table(nwbfile, cell_specimen_table, metadata_fixture) if roundtrip: obt = roundtripper(nwbfile, BehaviorOphysNwbApi, filter_invalid_rois=filter_invalid_rois) else: obt = BehaviorOphysNwbApi.from_nwbfile( nwbfile, filter_invalid_rois=filter_invalid_rois) if filter_invalid_rois: cell_specimen_table = \ cell_specimen_table[ cell_specimen_table["cell_roi_id"].isin( valid_roi_ids)] pd.testing.assert_frame_equal( cell_specimen_table, obt.get_cell_specimen_table(), check_dtype=False)