Skip to content

Commit

Permalink
Allow for missing data in input metadata table.
Browse files Browse the repository at this point in the history
Warn that the column is missing.
  • Loading branch information
morriscb committed Nov 11, 2023
1 parent 966b8c5 commit 44809f2
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,67 @@ 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 "
"NWB format"
),
)
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"
),
)
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):
Expand Down
76 changes: 62 additions & 14 deletions allensdk/brain_observatory/behavior/write_nwb/behavior/schemas.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 44809f2

Please sign in to comment.