From 715f728be16b1a8d6576437b18ef97cb90da3d9b Mon Sep 17 00:00:00 2001 From: Christopher Morrison Date: Mon, 25 Sep 2023 15:57:46 -0700 Subject: [PATCH] Revert to json for vbn packaging. Generalize compare_sessions input name. Allow temp compatiblity with test release (manifest min=0.0.0). --- .../data_io/behavior_project_cloud_api.py | 2 +- .../ecephys/write_nwb/nwb_writer.py | 28 +++++++++++++++++++ allensdk/brain_observatory/nwb/nwb_utils.py | 6 ++-- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/allensdk/brain_observatory/behavior/behavior_project_cache/project_apis/data_io/behavior_project_cloud_api.py b/allensdk/brain_observatory/behavior/behavior_project_cache/project_apis/data_io/behavior_project_cloud_api.py index 5ace2a6436..e2e3ca778d 100644 --- a/allensdk/brain_observatory/behavior/behavior_project_cache/project_apis/data_io/behavior_project_cloud_api.py +++ b/allensdk/brain_observatory/behavior/behavior_project_cache/project_apis/data_io/behavior_project_cloud_api.py @@ -57,7 +57,7 @@ def sanitize_data_columns( class BehaviorProjectCloudApi(BehaviorProjectBase, ProjectCloudApiBase): - MANIFEST_COMPATIBILITY = ["1.0.0", "2.0.0"] + MANIFEST_COMPATIBILITY = ["0.0.0", "2.0.0"] def _load_manifest_tables(self): expected_metadata = set( diff --git a/allensdk/brain_observatory/ecephys/write_nwb/nwb_writer.py b/allensdk/brain_observatory/ecephys/write_nwb/nwb_writer.py index 22bd98cfa9..45c974e2fb 100644 --- a/allensdk/brain_observatory/ecephys/write_nwb/nwb_writer.py +++ b/allensdk/brain_observatory/ecephys/write_nwb/nwb_writer.py @@ -30,6 +30,34 @@ def __init__( serializer=serializer ) + def write_nwb(self, **kwargs): + """Tries to write nwb to disk. If it fails, the filepath has ".error" + appended + + Parameters + ---------- + kwargs: kwargs sent to `from_json`, `from_nwb`, `to_nwb` + + """ + from_json_kwargs = { + k: v for k, v in kwargs.items() + if k in inspect.signature(self._serializer.from_json).parameters} + json_session = self._serializer.from_json( + session_data=self._session_data, **from_json_kwargs) + + try: + nwbfile = self._write_nwb( + session=json_session, **kwargs) + self._compare_sessions(nwbfile=nwbfile, + loaded_session=json_session, + **kwargs) + os.rename(self.nwb_filepath_inprogress, self._nwb_filepath) + except Exception as e: + if os.path.isfile(self.nwb_filepath_inprogress): + os.rename(self.nwb_filepath_inprogress, + self._nwb_filepath_error) + raise e + def _write_nwb( self, session: BehaviorEcephysSession, diff --git a/allensdk/brain_observatory/nwb/nwb_utils.py b/allensdk/brain_observatory/nwb/nwb_utils.py index d19a7bfb70..b37582f15b 100644 --- a/allensdk/brain_observatory/nwb/nwb_utils.py +++ b/allensdk/brain_observatory/nwb/nwb_utils.py @@ -196,7 +196,7 @@ def write_nwb( skip_stim=skip_stim, ) self._compare_sessions( - nwbfile=nwbfile, lims_session=lims_session, **kwargs + nwbfile=nwbfile, loaded_session=lims_session, **kwargs ) os.rename(self.nwb_filepath_inprogress, self._nwb_filepath) except Exception as e: @@ -326,7 +326,7 @@ def _compare_stimulus_file( raise ValueError(error_message) def _compare_sessions( - self, nwbfile: NWBFile, lims_session: DataObject, **kwargs + self, nwbfile: NWBFile, loaded_session: DataObject, **kwargs ): kwargs = { k: v @@ -334,4 +334,4 @@ def _compare_sessions( if k in inspect.signature(self._serializer.from_nwb).parameters } nwb_session = self._serializer.from_nwb(nwbfile, **kwargs) - assert sessions_are_equal(lims_session, nwb_session, reraise=True) + assert sessions_are_equal(loaded_session, nwb_session, reraise=True)