diff --git a/allensdk/brain_observatory/behavior/behavior_project_cache/tables/metadata_table_schemas.py b/allensdk/brain_observatory/behavior/behavior_project_cache/tables/metadata_table_schemas.py index d579114c8..f4b930c1f 100644 --- a/allensdk/brain_observatory/behavior/behavior_project_cache/tables/metadata_table_schemas.py +++ b/allensdk/brain_observatory/behavior/behavior_project_cache/tables/metadata_table_schemas.py @@ -12,6 +12,7 @@ class BehaviorSessionMetadataSchema(RaisingSchema): age_in_days = Int(required=True, description="Subject age") behavior_session_id = Int( required=True, + allow_none=True, description=( "Unique identifier for the " "behavior session to write into " @@ -19,10 +20,13 @@ class BehaviorSessionMetadataSchema(RaisingSchema): ), ) cre_line = String( - required=True, description="Genetic cre line of the subject." + required=True, + allow_none=True, + description="Genetic cre line of the subject." ) date_of_acquisition = String( required=True, + allow_none=True, description=( "Date of acquisition of " "behavior session, in string " "format" ), @@ -30,30 +34,45 @@ class BehaviorSessionMetadataSchema(RaisingSchema): driver_line = List( String, required=True, + allow_none=True, cli_as_single_argument=True, description="Genetic driver line(s) of subject", ) equipment_name = String( - required=True, description=("Name of the equipment used.") + required=True, + allow_none=True, + description=("Name of the equipment used.") ) full_genotype = String( - required=True, description="Full genotype of subject" + required=True, + allow_none=True, + description="Full genotype of subject" ) mouse_id = String( required=True, + allow_none=True, description="LabTracks ID of the subject. aka external_specimen_name.", ) project_code = String( rquired=True, + allow_none=True, description="LabTracks ID of the subject. aka external_specimen_name.", ) reporter_line = String( - required=True, description="Genetic reporter line(s) of subject" + required=True, + allow_none=True, + description="Genetic reporter line(s) of subject" ) session_type = String( - required=True, description="Full name of session type." + required=True, + allow_none=True, + description="Full name of session type." + ) + sex = String( + required=True, + allow_none=True, + description="Subject sex" ) - sex = String(required=True, description="Subject sex") @mm.post_load def convert_date_time(self, data, **kwargs): diff --git a/allensdk/brain_observatory/behavior/write_nwb/behavior/schemas.py b/allensdk/brain_observatory/behavior/write_nwb/behavior/schemas.py index feacbc7c6..01e3ff30f 100644 --- a/allensdk/brain_observatory/behavior/write_nwb/behavior/schemas.py +++ b/allensdk/brain_observatory/behavior/write_nwb/behavior/schemas.py @@ -1,4 +1,6 @@ import marshmallow as mm +from warnings import warn +import pandas as pd from allensdk.brain_observatory.argschema_utilities import ( InputFile, RaisingSchema, @@ -59,24 +61,70 @@ def _get_behavior_metadata(self, bs_row): """ """ behavior_session_metadata = {} - behavior_session_metadata["age_in_days"] = bs_row["age_in_days"] - behavior_session_metadata["cre_line"] = bs_row["cre_line"] - behavior_session_metadata["date_of_acquisition"] = bs_row[ - "date_of_acquisition" - ] - behavior_session_metadata["driver_line"] = sorted( - bs_row["driver_line"] + behavior_session_metadata["age_in_days"] = self._retrieve_column( + bs_row=bs_row, column_name="age_in_days" + ) + behavior_session_metadata["cre_line"] = self._retrieve_column( + bs_row=bs_row, column_name="cre_line" + ) + behavior_session_metadata["date_of_acquisition"] = self._retrieve_column( # noqa: E501 + bs_row=bs_row, column_name="date_of_acquisition" + ) + behavior_session_metadata["driver_line"] = self._retrieve_column( + bs_row=bs_row, column_name="driver_line" + ) + behavior_session_metadata["equipment_name"] = self._retrieve_column( + bs_row=bs_row, columns_name="equipment_name" + ) + behavior_session_metadata["full_genotype"] = self._retrieve_column( + bs_row=bs_row, column_name="full_genotype" + ) + behavior_session_metadata["mouse_id"] = self._retrieve_column( + bs_row=bs_row, column_name="mouse_id" + ) + behavior_session_metadata["project_code"] = self._retrieve_column( + bs_row=bs_row, column_name="project_code" + ) + behavior_session_metadata["reporter_line"] = self._retrieve_column( + bs_row=bs_row, column_name="reporter_line" + ) + behavior_session_metadata["session_type"] = self._retrieve_column( + bs_row=bs_row, column_name="session_type" + ) + behavior_session_metadata["sex"] = self._retrieve_column( + bs_row=bs_row, column_name="sex" ) - behavior_session_metadata["equipment_name"] = bs_row["equipment_name"] - behavior_session_metadata["full_genotype"] = bs_row["full_genotype"] - behavior_session_metadata["mouse_id"] = bs_row["mouse_id"] - behavior_session_metadata["project_code"] = bs_row["project_code"] - behavior_session_metadata["reporter_line"] = bs_row["reporter_line"] - behavior_session_metadata["session_type"] = bs_row["session_type"] - behavior_session_metadata["sex"] = bs_row["sex"] return behavior_session_metadata + def _retrieve_column(self, bs_row: pd.Series, column_name: str): + """Pull a column safely, return None otherwise. + + Parameters + ---------- + bs_row : pd.Series + Row of a BehaviorSessionTable + column_name : str + Name of column to retrieve + + Returns + ------- + value : object + Value of column_name in bs_row, or None if column_name is not in + bs_row + """ + if column_name not in bs_row.index: + warn(f"Warning, {column_name} not in metadata table. Unless this " + "has been added it to skip_metadata_key or " + "skip_stimulus_file_key as inpput, creating the NWB file " + "fail.") + return None + else: + value = bs_row[column_name] + if isinstance(value, list): + value = sorted(value) + return value + class BehaviorInputSchema(BaseInputSchema): behavior_session_id = Int(