From ddec95fb2c9e089a00aaf9f0e3facc65860a89c8 Mon Sep 17 00:00:00 2001 From: arielleleon <54869022+arielleleon@users.noreply.github.com> Date: Mon, 18 Nov 2024 16:41:17 -0800 Subject: [PATCH] Bug/pull behavior items (#200) * Option to add stim table to modality formatted files. * Opt. to write stim table to modality directory and compute fps from behavior sessions. --- src/aind_metadata_mapper/mesoscope/models.py | 3 +++ src/aind_metadata_mapper/mesoscope/session.py | 8 +++++++- .../open_ephys/utils/pkl_utils.py | 8 +++++++- src/aind_metadata_mapper/stimulus/camstim.py | 11 ++++------- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/aind_metadata_mapper/mesoscope/models.py b/src/aind_metadata_mapper/mesoscope/models.py index 767c670c..69eb7406 100644 --- a/src/aind_metadata_mapper/mesoscope/models.py +++ b/src/aind_metadata_mapper/mesoscope/models.py @@ -18,6 +18,9 @@ class JobSettings(BaseJobSettings): input_source: Path = Field(..., title="Path to the input source") session_id: str = Field(..., title="ID of the session") behavior_source: Path = Field(..., title="Path to the behavior source") + make_camsitm_dir: bool = Field( + default=False, title="Make camsitm directory" + ) output_directory: Path = Field(..., title="Path to the output directory") session_start_time: datetime = Field( ..., title="Start time of the session" diff --git a/src/aind_metadata_mapper/mesoscope/session.py b/src/aind_metadata_mapper/mesoscope/session.py index 95bcff3b..9d3376ef 100644 --- a/src/aind_metadata_mapper/mesoscope/session.py +++ b/src/aind_metadata_mapper/mesoscope/session.py @@ -55,10 +55,16 @@ def __init__(self, job_settings: Union[JobSettings, str]): job_settings_model.behavior_source = Path( job_settings_model.behavior_source ) + camstim_output = job_settings_model.output_directory + if job_settings_model.make_camsitm_dir: + camstim_output = ( + job_settings_model.output_directory + / f"{job_settings_model.session_id}_behavior" + ) super().__init__(job_settings=job_settings_model) camstim_settings = CamstimSettings( input_source=self.job_settings.input_source, - output_directory=self.job_settings.output_directory, + output_directory=camstim_output, session_id=self.job_settings.session_id, subject_id=self.job_settings.subject_id, ) diff --git a/src/aind_metadata_mapper/open_ephys/utils/pkl_utils.py b/src/aind_metadata_mapper/open_ephys/utils/pkl_utils.py index 9105c315..c39a70d6 100644 --- a/src/aind_metadata_mapper/open_ephys/utils/pkl_utils.py +++ b/src/aind_metadata_mapper/open_ephys/utils/pkl_utils.py @@ -66,7 +66,13 @@ def get_fps(pkl): fps. """ - return pkl["fps"] + if not pkl.get("fps"): + fps = round( + 1 / np.mean(pkl["items"]["behavior"]["intervalsms"]) * 0.001, 2 + ) + else: + fps = pkl["fps"] + return fps def get_pre_blank_sec(pkl): diff --git a/src/aind_metadata_mapper/stimulus/camstim.py b/src/aind_metadata_mapper/stimulus/camstim.py index 905b6875..3559009f 100644 --- a/src/aind_metadata_mapper/stimulus/camstim.py +++ b/src/aind_metadata_mapper/stimulus/camstim.py @@ -65,15 +65,12 @@ def __init__( self.input_source = Path(self.camstim_settings.input_source) session_id = self.camstim_settings.session_id self.pkl_path = next(self.input_source.rglob("*.pkl")) + if not self.camstim_settings.output_directory.is_dir(): + self.camstim_settings.output_directory.mkdir(parents=True) self.stim_table_path = ( - self.pkl_path.parent / f"{session_id}_stim_table.csv" + self.camstim_settings.output_directory + / f"{session_id}_stim_table.csv" ) - if self.camstim_settings.output_directory: - self.stim_table_path = ( - self.camstim_settings.output_directory - / f"{session_id}_behavior" - / f"{session_id}_stim_table.csv" - ) self.pkl_data = pkl.load_pkl(self.pkl_path) self.fps = pkl.get_fps(self.pkl_data) self.session_start, self.session_end = self._get_sync_times()