Skip to content

Commit

Permalink
Merge pull request #161 from AllenNeuralDynamics/release-v0.18.2
Browse files Browse the repository at this point in the history
Release v0.18.2
  • Loading branch information
jtyoung84 authored Sep 15, 2024
2 parents cf5f85b + ff50710 commit 649ff9d
Show file tree
Hide file tree
Showing 5 changed files with 896 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/aind_metadata_mapper/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Init package"""

__version__ = "0.18.1"
__version__ = "0.18.2"
53 changes: 33 additions & 20 deletions src/aind_metadata_mapper/gather_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from aind_data_schema.core.subject import Subject
from aind_data_schema_models.pid_names import PIDName
from pydantic import ValidationError
from pydantic_core import PydanticSerializationError

from aind_metadata_mapper.bergamo.models import (
JobSettings as BergamoSessionJobSettings,
Expand Down Expand Up @@ -343,12 +344,12 @@ def get_instrument_metadata(self) -> Optional[dict]:
else:
return None

def get_main_metadata(self) -> Metadata:
"""Get main Metadata model"""
def get_main_metadata(self) -> dict:
"""Get serialized main Metadata model"""

def load_model(
filepath: Optional[Path], model: Type[AindCoreModel]
) -> Optional[AindCoreModel]:
) -> Optional[dict]:
"""
Validates contents of file with an AindCoreModel
Parameters
Expand All @@ -358,16 +359,25 @@ def load_model(
Returns
-------
Optional[AindCodeModel]
Optional[dict]
"""
if filepath is not None and filepath.is_file():
with open(filepath, "r") as f:
contents = json.load(f)
try:
output = model.model_validate_json(json.dumps(contents))
except (ValidationError, AttributeError, ValueError, KeyError):
output = model.model_construct(**contents)
valid_model = model.model_validate_json(
json.dumps(contents)
)
output = json.loads(valid_model.model_dump_json())
except (
ValidationError,
AttributeError,
ValueError,
KeyError,
PydanticSerializationError,
):
output = contents

return output
else:
Expand Down Expand Up @@ -410,22 +420,26 @@ def load_model(
acquisition=acquisition,
instrument=instrument,
)
return metadata
metadata_json = json.loads(metadata.model_dump_json(by_alias=True))
return metadata_json
except Exception as e:
logging.warning(f"Issue with metadata construction! {e.args}")
metadata = Metadata.model_construct(
# Set basic parameters
metadata = Metadata(
name=self.settings.metadata_settings.name,
location=self.settings.metadata_settings.location,
subject=subject,
data_description=data_description,
procedures=procedures,
session=session,
rig=rig,
processing=processing,
acquisition=acquisition,
instrument=instrument,
)
return metadata
metadata_json = json.loads(metadata.model_dump_json(by_alias=True))
# Attach dict objects
metadata_json["subject"] = subject
metadata_json["data_description"] = data_description
metadata_json["procedures"] = procedures
metadata_json["session"] = session
metadata_json["rig"] = rig
metadata_json["processing"] = processing
metadata_json["acquisition"] = acquisition
metadata_json["instrument"] = instrument
return metadata_json

def _write_json_file(self, filename: str, contents: dict) -> None:
"""
Expand Down Expand Up @@ -504,14 +518,13 @@ def run_job(self) -> None:
self._gather_automated_metadata()
self._gather_non_automated_metadata()
if self.settings.metadata_settings is not None:
metadata = self.get_main_metadata()
contents = self.get_main_metadata()
# TODO: may need to update aind-data-schema write standard file
# class
output_path = (
self.settings.directory_to_write_to
/ Metadata.default_filename()
)
contents = json.loads(metadata.model_dump_json(by_alias=True))
with open(output_path, "w") as f:
json.dump(
contents,
Expand Down
16 changes: 8 additions & 8 deletions src/aind_metadata_mapper/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
)


class SessionSettings(BaseSettings, extra='allow'):
class SessionSettings(BaseSettings, extra="allow"):
"""Settings needed to retrieve session metadata"""

job_settings: Annotated[
Expand All @@ -39,29 +39,29 @@ class SessionSettings(BaseSettings, extra='allow'):
]


class AcquisitionSettings(BaseSettings, extra='allow'):
class AcquisitionSettings(BaseSettings, extra="allow"):
"""Fields needed to retrieve acquisition metadata"""

# TODO: we can change this to a tagged union once more acquisition settings
# are added
job_settings: SmartSpimAcquisitionJobSettings


class SubjectSettings(BaseSettings, extra='allow'):
class SubjectSettings(BaseSettings, extra="allow"):
"""Fields needed to retrieve subject metadata"""

subject_id: str
metadata_service_path: str = "subject"


class ProceduresSettings(BaseSettings, extra='allow'):
class ProceduresSettings(BaseSettings, extra="allow"):
"""Fields needed to retrieve procedures metadata"""

subject_id: str
metadata_service_path: str = "procedures"


class RawDataDescriptionSettings(BaseSettings, extra='allow'):
class RawDataDescriptionSettings(BaseSettings, extra="allow"):
"""Fields needed to retrieve data description metadata"""

name: str
Expand All @@ -71,7 +71,7 @@ class RawDataDescriptionSettings(BaseSettings, extra='allow'):
metadata_service_path: str = "funding"


class ProcessingSettings(BaseSettings, extra='allow'):
class ProcessingSettings(BaseSettings, extra="allow"):
"""Fields needed to retrieve processing metadata"""

pipeline_process: dict = Field(
Expand All @@ -83,7 +83,7 @@ class ProcessingSettings(BaseSettings, extra='allow'):
)


class MetadataSettings(BaseSettings, extra='allow'):
class MetadataSettings(BaseSettings, extra="allow"):
"""Fields needed to retrieve main Metadata"""

name: str
Expand All @@ -98,7 +98,7 @@ class MetadataSettings(BaseSettings, extra='allow'):
instrument_filepath: Optional[Path] = None


class JobSettings(BaseSettings, extra='allow'):
class JobSettings(BaseSettings, extra="allow"):
"""Fields needed to gather all metadata"""

job_settings_name: Literal["GatherMetadata"] = "GatherMetadata"
Expand Down
Loading

0 comments on commit 649ff9d

Please sign in to comment.