def test_write_probe_lfp_file_roundtrip(tmpdir_factory, roundtrip, lfp_data, probe_data, csd_data): expected_csd = xr.DataArray( name="CSD", data=csd_data["csd"], dims=["virtual_channel_index", "time"], coords={ "virtual_channel_index": np.arange(csd_data["csd"].shape[0]), "time": csd_data["relative_window"], "vertical_position": (("virtual_channel_index",), csd_data["csd_locations"][:, 1]), "horizontal_position": (("virtual_channel_index",), csd_data["csd_locations"][:, 0]), } ) expected_lfp = xr.DataArray( name="LFP", data=lfp_data["data"], dims=["time", "channel"], coords=[lfp_data["timestamps"], [2, 1]] ) tmpdir = Path(tmpdir_factory.mktemp("probe_lfp_nwb")) input_data_path = tmpdir / Path("lfp_data.dat") input_timestamps_path = tmpdir / Path("lfp_timestamps.npy") input_channels_path = tmpdir / Path("lfp_channels.npy") input_csd_path = tmpdir / Path("csd.h5") output_path = str(tmpdir / Path("lfp.nwb")) # pynwb.NWBHDF5IO chokes on Path test_lfp_paths = { "input_data_path": input_data_path, "input_timestamps_path": input_timestamps_path, "input_channels_path": input_channels_path, "output_path": output_path } probe_data.update({"lfp": test_lfp_paths}) probe_data.update({"csd_path": input_csd_path}) write_csd_to_h5(path=input_csd_path, **csd_data) np.save(input_timestamps_path, lfp_data["timestamps"], allow_pickle=False) np.save(input_channels_path, lfp_data["subsample_channels"], allow_pickle=False) with open(input_data_path, "wb") as input_data_file: input_data_file.write(lfp_data["data"].tobytes()) write_nwb.write_probe_lfp_file(4242, None, datetime.now(), logging.INFO, probe_data) obt = EcephysNwbSessionApi(path=None, probe_lfp_paths={12345: NWBHDF5IO(output_path, "r").read}) obtained_lfp = obt.get_lfp(12345) obtained_csd = obt.get_current_source_density(12345) xr.testing.assert_equal(obtained_lfp, expected_lfp) xr.testing.assert_equal(obtained_csd, expected_csd)
def test_add_raw_running_data_to_nwbfile(nwbfile, raw_running_data, roundtripper, roundtrip): nwbfile = write_nwb.add_raw_running_data_to_nwbfile(nwbfile, raw_running_data) if roundtrip: api_obt = roundtripper(nwbfile, EcephysNwbSessionApi) else: api_obt = EcephysNwbSessionApi.from_nwbfile(nwbfile) obtained = api_obt.get_raw_running_data() expected = raw_running_data.rename(columns={"dx": "net_rotation", "vsig": "signal_voltage", "vin": "supply_voltage"}) pd.testing.assert_frame_equal(expected, obtained, check_like=True)
def test_add_probe_to_nwbfile(nwbfile, roundtripper, roundtrip, pid, name, srate, lfp_srate, has_lfp, expected): nwbfile, _, _ = write_nwb.add_probe_to_nwbfile(nwbfile, pid, name=name, sampling_rate=srate, lfp_sampling_rate=lfp_srate, has_lfp_data=has_lfp) if roundtrip: obt = roundtripper(nwbfile, EcephysNwbSessionApi) else: obt = EcephysNwbSessionApi.from_nwbfile(nwbfile) pd.testing.assert_frame_equal(expected, obt.get_probes(), check_like=True)
def test_add_running_speed_to_nwbfile(nwbfile, running_speed, roundtripper, roundtrip, include_rotation): nwbfile = write_nwb.add_running_speed_to_nwbfile(nwbfile, running_speed) if roundtrip: api_obt = roundtripper(nwbfile, EcephysNwbSessionApi) else: api_obt = EcephysNwbSessionApi.from_nwbfile(nwbfile) obtained = api_obt.get_running_speed(include_rotation=include_rotation) expected = running_speed if not include_rotation: expected = expected.drop(columns="net_rotation") pd.testing.assert_frame_equal(expected, obtained, check_like=True)
def test_add_eye_tracking_rig_geometry_data_to_nwbfile(nwbfile, roundtripper, roundtrip, eye_tracking_rig_geometry, expected): nwbfile = write_nwb.add_eye_tracking_rig_geometry_data_to_nwbfile(nwbfile, eye_tracking_rig_geometry) if roundtrip: obt = roundtripper(nwbfile, EcephysNwbSessionApi) else: obt = EcephysNwbSessionApi.from_nwbfile(nwbfile) obtained_metadata = obt.get_rig_metadata() pd.testing.assert_frame_equal(obtained_metadata["geometry"], expected["geometry"], check_like=True) assert obtained_metadata["equipment"] == expected["equipment"]
def test_add_probewise_data_to_nwbfile(monkeypatch, nwbfile, roundtripper, roundtrip, probes, parsed_probe_data, expected_units_table): def mock_parse_probes_data(probes): return parsed_probe_data monkeypatch.setattr(write_nwb, "parse_probes_data", mock_parse_probes_data) nwbfile = write_nwb.add_probewise_data_to_nwbfile(nwbfile, probes) if roundtrip: obt = roundtripper(nwbfile, EcephysNwbSessionApi) else: obt = EcephysNwbSessionApi.from_nwbfile(nwbfile) pd.testing.assert_frame_equal(obt.nwbfile.units.to_dataframe(), expected_units_table)
def _build_nwb_api_for_session(self, path, session_id, filter_by_validity, **unit_filter_kwargs): get_analysis_metrics = partial( self.get_unit_analysis_metrics_for_session, session_id=session_id, annotate=False, filter_by_validity=True, **unit_filter_kwargs ) return EcephysNwbSessionApi( path=path, probe_lfp_paths=self._setup_probe_promises(session_id), additional_unit_metrics=get_analysis_metrics, external_channel_columns=partial(self._get_substitute_channel_columns, session_id), filter_by_validity=filter_by_validity, **unit_filter_kwargs )
def test_add_eye_tracking_data_to_nwbfile(nwbfile, roundtripper, roundtrip, eye_tracking_frame_times, eye_dlc_tracking_data, eye_gaze_data, expected_pupil_data, expected_gaze_data): nwbfile = write_nwb.add_eye_tracking_data_to_nwbfile(nwbfile, eye_tracking_frame_times, eye_dlc_tracking_data, eye_gaze_data) if roundtrip: obt = roundtripper(nwbfile, EcephysNwbSessionApi) else: obt = EcephysNwbSessionApi.from_nwbfile(nwbfile) obtained_pupil_data = obt.get_pupil_data() obtained_screen_gaze_data = obt.get_screen_gaze_data(include_filtered_data=True) pd.testing.assert_frame_equal(obtained_pupil_data, expected_pupil_data, check_like=True) pd.testing.assert_frame_equal(obtained_screen_gaze_data, expected_gaze_data, check_like=True)