Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release v1.1.1 #1120

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
rig,
session,
subject,
quality_control
quality_control,
)

dummy_object = [
Expand All @@ -34,7 +34,7 @@
rig,
session,
subject,
quality_control
quality_control,
] # A temporary workaround to bypass "Imported but unused" error

INSTITUTE_NAME = "Allen Institute for Neural Dynamics"
Expand Down
2 changes: 1 addition & 1 deletion src/aind_data_schema/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" imports for AindModel subclasses
"""

__version__ = "1.1.0"
__version__ = "1.1.1"
6 changes: 3 additions & 3 deletions src/aind_data_schema/core/rig.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ class Rig(AindCoreModel):
notes: Optional[str] = Field(default=None, title="Notes")

@field_serializer("modalities", when_used="json")
def serialize_modalities(modalities: Set[Modality.ONE_OF]):
"""sort modalities by name when serializing to JSON"""
return sorted(modalities, key=lambda x: x.name)
def serialize_modalities(self, modalities: Set[Modality.ONE_OF]):
"""Dynamically serialize modalities based on their type."""
return sorted(modalities, key=lambda x: x.get("name") if isinstance(x, dict) else x.name)

@model_validator(mode="after")
def validate_cameras_other(self):
Expand Down
26 changes: 26 additions & 0 deletions tests/test_rig.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
""" test Rig """

import unittest
import json
from datetime import date, datetime

from aind_data_schema_models.modalities import Modality
from aind_data_schema_models.organizations import Organization
from pydantic import ValidationError
from pydantic_core import PydanticSerializationError

from aind_data_schema.components.devices import (
Calibration,
Expand Down Expand Up @@ -821,6 +823,30 @@ def test_rig_id_validator(self):
calibrations=[calibration],
)

def test_serialize_modalities(self):
"""Tests that modalities serializer can handle different types"""
expected_modalities = [{"name": "Extracellular electrophysiology", "abbreviation": "ecephys"}]
# Case 1: Modality is a class instance
rig_instance_modality = Rig.model_construct(
modalities=[Modality.ECEPHYS] # Example with a valid Modality instance
)
rig_json = rig_instance_modality.model_dump_json()
rig_data = json.loads(rig_json)
self.assertEqual(rig_data["modalities"], expected_modalities)

# Case 2: Modality is a dictionary when Rig is constructed from JSON
rig_dict_modality = Rig.model_construct(**rig_data)
rig_dict_json = rig_dict_modality.model_dump_json()
rig_dict_data = json.loads(rig_dict_json)
self.assertEqual(rig_dict_data["modalities"], expected_modalities)

# Case 3: Modality is an unknown type
with self.assertRaises(PydanticSerializationError) as context:
rig_unknown_modality = Rig.model_construct(modalities={"UnknownModality"})

rig_unknown_modality.model_dump_json()
self.assertIn("Error calling function `serialize_modalities`", str(context.exception))


if __name__ == "__main__":
unittest.main()
Loading