Skip to content

Commit

Permalink
fix: properly handle modalit*ies* list, fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dbirman committed Sep 26, 2024
1 parent 4ae43c2 commit 51a36d6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
31 changes: 16 additions & 15 deletions src/aind_data_schema/core/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,23 +198,24 @@ def validate_metadata(self):
def validate_expected_files_by_modality(self):
"""Validator checks that all required/excluded files match the metadata model"""
if self.data_description:
modality = self.data_description.modality
modalities = self.data_description.modality

for file in CORE_FILES:
# For each field, check if this is a required/excluded file
file_requirement = getattr(getattr(ExpectedFiles, str(modality.abbreviation).upper()), file)

# Check required case
if file_requirement == FileRequirement.REQUIRED and not getattr(self, file):
raise ValueError(
f"{modality.abbreviation} metadata missing required file: {file}"
)
for modality in modalities:
for file in CORE_FILES:
# For each field, check if this is a required/excluded file
file_requirement = getattr(getattr(ExpectedFiles, str(modality.abbreviation).upper()), file)

# Check required case
if file_requirement == FileRequirement.REQUIRED and not getattr(self, file):
raise ValueError(
f"{modality.abbreviation} metadata missing required file: {file}"
)

# Check excluded case
if file_requirement == FileRequirement.EXCLUDED and getattr(self, file):
raise ValueError(
f"{modality.abbreviation} metadata includes excluded file: {file}"
)
# Check excluded case
if file_requirement == FileRequirement.EXCLUDED and getattr(self, file):
raise ValueError(
f"{modality.abbreviation} metadata includes excluded file: {file}"
)

return self

Expand Down
23 changes: 16 additions & 7 deletions tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from aind_data_schema_models.organizations import Organization
from aind_data_schema_models.platforms import Platform
from aind_data_schema_models.modalities import Modality
from pydantic import ValidationError
from pydantic import __version__ as pyd_version

Expand Down Expand Up @@ -140,13 +141,16 @@ def test_validate_smartspim_metadata(self):
name="ecephys_655019_2023-04-03_18-17-09",
location="bucket",
data_description=DataDescription.model_construct(
label="some label", platform=Platform.SMARTSPIM, creation_time=time(12, 12, 12)
label="some label",
platform=Platform.SMARTSPIM,
creation_time=time(12, 12, 12),
modality=[Modality.SPIM],
),
procedures=Procedures.model_construct(subject_procedures=[surgery1]),
acquisition=Acquisition.model_construct(),
)
self.assertIn(
"Missing some metadata for SmartSpim. Requires subject, procedures, acquisition, and instrument.",
"SPIM metadata missing required file: subject",
str(context.exception),
)

Expand All @@ -157,12 +161,14 @@ def test_validate_smartspim_metadata(self):
name="ecephys_655019_2023-04-03_18-17-09",
location="bucket",
data_description=DataDescription.model_construct(
label="some label", platform=Platform.SMARTSPIM, creation_time=time(12, 12, 12)
label="some label", platform=Platform.SMARTSPIM, creation_time=time(12, 12, 12),
modality=[Modality.SPIM]
),
subject=Subject.model_construct(),
procedures=Procedures.model_construct(subject_procedures=[surgery2]),
acquisition=Acquisition.model_construct(),
instrument=Instrument.model_construct(),
processing=Processing.model_construct()
)
self.assertIn("Injection is missing injection_materials.", str(context.exception))

Expand All @@ -179,13 +185,14 @@ def test_validate_ecephys_metadata(self):
name="ecephys_655019_2023-04-03_18-17-09",
location="bucket",
data_description=DataDescription.model_construct(
label="some label", platform=Platform.ECEPHYS, creation_time=time(12, 12, 12)
label="some label", platform=Platform.ECEPHYS, creation_time=time(12, 12, 12),
modality=[Modality.ECEPHYS],
),
procedures=Procedures.model_construct(subject_procedures=[surgery1]),
rig=Rig.model_construct(),
)
self.assertIn(
"Missing some metadata for Ecephys. Requires subject, procedures, session, rig, and processing.",
"ecephys metadata missing required file: subject",
str(context.exception),
)

Expand All @@ -196,7 +203,8 @@ def test_validate_ecephys_metadata(self):
name="ecephys_655019_2023-04-03_18-17-09",
location="bucket",
data_description=DataDescription.model_construct(
label="some label", platform=Platform.ECEPHYS, creation_time=time(12, 12, 12)
label="some label", platform=Platform.ECEPHYS, creation_time=time(12, 12, 12),
modality=[Modality.ECEPHYS],
),
subject=Subject.model_construct(),
procedures=Procedures.model_construct(subject_procedures=[surgery2]),
Expand All @@ -216,7 +224,8 @@ def test_validate_rig_session_compatibility(self):
name="ecephys_655019_2023-04-03_18-17-09",
location="bucket",
data_description=DataDescription.model_construct(
label="some label", platform=Platform.ECEPHYS, creation_time=time(12, 12, 12)
label="some label", platform=Platform.ECEPHYS, creation_time=time(12, 12, 12),
modality=[Modality.ECEPHYS]
),
subject=Subject.model_construct(),
procedures=Procedures.model_construct(),
Expand Down

0 comments on commit 51a36d6

Please sign in to comment.