diff --git a/nwb_linkml/src/nwb_linkml/generators/pydantic.py b/nwb_linkml/src/nwb_linkml/generators/pydantic.py index 659bd2b..fe151af 100644 --- a/nwb_linkml/src/nwb_linkml/generators/pydantic.py +++ b/nwb_linkml/src/nwb_linkml/generators/pydantic.py @@ -31,6 +31,7 @@ from nwb_linkml.includes.base import ( BASEMODEL_COERCE_VALUE, BASEMODEL_EXTRA_TO_VALUE, BASEMODEL_GETITEM, + BASEMODEL_SERIALIZER, ) from nwb_linkml.includes.hdmf import ( DYNAMIC_TABLE_IMPORTS, @@ -60,12 +61,20 @@ class NWBPydanticGenerator(PydanticGenerator): BASEMODEL_CAST_WITH_VALUE, BASEMODEL_COERCE_CHILD, BASEMODEL_EXTRA_TO_VALUE, + BASEMODEL_SERIALIZER, ) split: bool = True imports: list[Import] = field( default_factory=lambda: [ Import(module="numpy", alias="np"), - Import(module="pydantic", objects=[ObjectImport(name="model_validator")]), + Import( + module="pydantic", + objects=[ + ObjectImport(name="model_validator"), + ObjectImport(name="model_serializer"), + ], + ), + Import(module="pdb"), ] ) @@ -294,12 +303,15 @@ class AfterGenerateClass: else: # pragma: no cover - for completeness, shouldn't happen cls.imports = DYNAMIC_TABLE_IMPORTS.model_copy() elif cls.cls.name == "VectorData": - cls.cls.bases = ["VectorDataMixin"] + cls.cls.bases = ["VectorDataMixin[T]", "Generic[T]"] # make ``value`` generic on T if "value" in cls.cls.attributes: cls.cls.attributes["value"].range = "Optional[T]" elif cls.cls.name == "VectorIndex": - cls.cls.bases = ["VectorIndexMixin"] + cls.cls.bases = ["VectorIndexMixin[T]", "Generic[T]"] + # make ``value`` generic on T + if "value" in cls.cls.attributes: + cls.cls.attributes["value"].range = "Optional[T]" elif cls.cls.name == "DynamicTableRegion": cls.cls.bases = ["DynamicTableRegionMixin", "VectorData"] elif cls.cls.name == "AlignedDynamicTable": diff --git a/nwb_linkml/src/nwb_linkml/includes/base.py b/nwb_linkml/src/nwb_linkml/includes/base.py index 4747f57..a4aea1b 100644 --- a/nwb_linkml/src/nwb_linkml/includes/base.py +++ b/nwb_linkml/src/nwb_linkml/includes/base.py @@ -88,3 +88,17 @@ BASEMODEL_EXTRA_TO_VALUE = """ v["value"] = extras return v """ + +BASEMODEL_SERIALIZER = """ + @model_serializer(mode="wrap", when_used="json") + def serialize_model(self, nxt, info) -> Dict[str, Any]: + try: + return nxt(self, info) + except Exception as e: + return {"ERROR":str(e), "TYPE": str(type(self))} + if 'Circular reference' in str(e): + return {"REFERENCE":"REFERENCE"} + pdb.set_trace() + json_dump_fields = ('indent', 'include', 'exclude', 'context', 'by_alias', 'exclude_unset', 'exclude_defaults', 'exclude_none', 'round_trip', 'warnings', 'serialize_as_any') + return self.model_dump_json(**{k:v for k,v in info.__dict__.items() if k in json_dump_fields}) +""" diff --git a/nwb_linkml/src/nwb_linkml/io/hdf5.py b/nwb_linkml/src/nwb_linkml/io/hdf5.py index d46465f..23bd2fa 100644 --- a/nwb_linkml/src/nwb_linkml/io/hdf5.py +++ b/nwb_linkml/src/nwb_linkml/io/hdf5.py @@ -171,23 +171,7 @@ def _load_node( del args[".specloc"] model = provider.get_class(obj.attrs["namespace"], obj.attrs["neurodata_type"]) - # try: return model(**args) - # except ValidationError as e1: - # # try to restack extra fields into ``value`` - # if "value" in model.model_fields: - # value_dict = { - # key: val for key, val in args.items() if key not in model.model_fields - # } - # for k in value_dict: - # del args[k] - # args["value"] = value_dict - # try: - # return model(**args) - # except Exception as e2: - # raise e2 from e1 - # else: - # raise e1 else: if "name" in args: diff --git a/nwb_linkml/tests/test_io/test_io_nwb.py b/nwb_linkml/tests/test_io/test_io_nwb.py index 32a50d1..bb05f4e 100644 --- a/nwb_linkml/tests/test_io/test_io_nwb.py +++ b/nwb_linkml/tests/test_io/test_io_nwb.py @@ -2,6 +2,7 @@ Placeholder test module to test reading from pynwb-generated NWB file """ +import pdb from datetime import datetime import numpy as np @@ -11,6 +12,7 @@ from numpydantic.interface.hdf5 import H5Proxy from pydantic import BaseModel from pynwb import NWBHDF5IO from pynwb import NWBFile as PyNWBFile +from pydantic_core import to_json from nwb_linkml.io.hdf5 import HDF5IO from nwb_models.models import NWBFile @@ -29,6 +31,13 @@ def test_read_from_nwbfile(nwb_file): @pytest.fixture(scope="module") def read_nwbfile(nwb_file) -> NWBFile: res = HDF5IO(nwb_file).read() + + def fallback(*args, **kwargs): + pdb.set_trace() + + to_json(res, fallback=fallback) + + pdb.set_trace() return res @@ -63,6 +72,7 @@ def test_nwbfile_base(read_nwbfile, read_pynwb): """ Base attributes on top-level nwbfile are correct """ + _compare_attrs(read_nwbfile, read_pynwb) diff --git a/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_base.py b/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_base.py index 6c8a7fb..ca53d3b 100644 --- a/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_base.py +++ b/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_base.py @@ -1,5 +1,6 @@ from __future__ import annotations +import pdb import re import sys from datetime import date, datetime, time @@ -32,6 +33,7 @@ from pydantic import ( RootModel, ValidationInfo, field_validator, + model_serializer, model_validator, ) @@ -134,6 +136,32 @@ class ConfiguredBaseModel(BaseModel): v["value"] = extras return v + @model_serializer(mode="wrap", when_used="json") + def serialize_model(self, nxt, info) -> Dict[str, Any]: + try: + return nxt(self, info) + except Exception as e: + return {"ERROR": str(e), "TYPE": str(type(self))} + if "Circular reference" in str(e): + return {"REFERENCE": "REFERENCE"} + pdb.set_trace() + json_dump_fields = ( + "indent", + "include", + "exclude", + "context", + "by_alias", + "exclude_unset", + "exclude_defaults", + "exclude_none", + "round_trip", + "warnings", + "serialize_as_any", + ) + return self.model_dump_json( + **{k: v for k, v in info.__dict__.items() if k in json_dump_fields} + ) + class LinkMLMeta(RootModel): root: Dict[str, Any] = {} diff --git a/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_behavior.py b/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_behavior.py index d22df1e..16124b1 100644 --- a/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_behavior.py +++ b/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_behavior.py @@ -1,5 +1,6 @@ from __future__ import annotations +import pdb import re import sys from datetime import date, datetime, time @@ -9,7 +10,15 @@ from typing import Any, ClassVar, Dict, List, Literal, Optional, Union import numpy as np from numpydantic import NDArray, Shape -from pydantic import BaseModel, ConfigDict, Field, RootModel, field_validator, model_validator +from pydantic import ( + BaseModel, + ConfigDict, + Field, + RootModel, + field_validator, + model_serializer, + model_validator, +) from ...core.v2_7_0.core_nwb_base import ( NWBDataInterface, @@ -115,6 +124,32 @@ class ConfiguredBaseModel(BaseModel): v["value"] = extras return v + @model_serializer(mode="wrap", when_used="json") + def serialize_model(self, nxt, info) -> Dict[str, Any]: + try: + return nxt(self, info) + except Exception as e: + return {"ERROR": str(e), "TYPE": str(type(self))} + if "Circular reference" in str(e): + return {"REFERENCE": "REFERENCE"} + pdb.set_trace() + json_dump_fields = ( + "indent", + "include", + "exclude", + "context", + "by_alias", + "exclude_unset", + "exclude_defaults", + "exclude_none", + "round_trip", + "warnings", + "serialize_as_any", + ) + return self.model_dump_json( + **{k: v for k, v in info.__dict__.items() if k in json_dump_fields} + ) + class LinkMLMeta(RootModel): root: Dict[str, Any] = {} diff --git a/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_device.py b/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_device.py index 0e96640..04515f5 100644 --- a/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_device.py +++ b/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_device.py @@ -1,5 +1,6 @@ from __future__ import annotations +import pdb import re import sys from datetime import date, datetime, time @@ -8,7 +9,15 @@ from enum import Enum from typing import Any, ClassVar, Dict, List, Literal, Optional, Union import numpy as np -from pydantic import BaseModel, ConfigDict, Field, RootModel, field_validator, model_validator +from pydantic import ( + BaseModel, + ConfigDict, + Field, + RootModel, + field_validator, + model_serializer, + model_validator, +) from ...core.v2_7_0.core_nwb_base import NWBContainer @@ -108,6 +117,32 @@ class ConfiguredBaseModel(BaseModel): v["value"] = extras return v + @model_serializer(mode="wrap", when_used="json") + def serialize_model(self, nxt, info) -> Dict[str, Any]: + try: + return nxt(self, info) + except Exception as e: + return {"ERROR": str(e), "TYPE": str(type(self))} + if "Circular reference" in str(e): + return {"REFERENCE": "REFERENCE"} + pdb.set_trace() + json_dump_fields = ( + "indent", + "include", + "exclude", + "context", + "by_alias", + "exclude_unset", + "exclude_defaults", + "exclude_none", + "round_trip", + "warnings", + "serialize_as_any", + ) + return self.model_dump_json( + **{k: v for k, v in info.__dict__.items() if k in json_dump_fields} + ) + class LinkMLMeta(RootModel): root: Dict[str, Any] = {} diff --git a/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_ecephys.py b/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_ecephys.py index 85141cf..452f09d 100644 --- a/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_ecephys.py +++ b/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_ecephys.py @@ -1,5 +1,6 @@ from __future__ import annotations +import pdb import re import sys from datetime import date, datetime, time @@ -17,6 +18,7 @@ from pydantic import ( RootModel, ValidationInfo, field_validator, + model_serializer, model_validator, ) @@ -126,6 +128,32 @@ class ConfiguredBaseModel(BaseModel): v["value"] = extras return v + @model_serializer(mode="wrap", when_used="json") + def serialize_model(self, nxt, info) -> Dict[str, Any]: + try: + return nxt(self, info) + except Exception as e: + return {"ERROR": str(e), "TYPE": str(type(self))} + if "Circular reference" in str(e): + return {"REFERENCE": "REFERENCE"} + pdb.set_trace() + json_dump_fields = ( + "indent", + "include", + "exclude", + "context", + "by_alias", + "exclude_unset", + "exclude_defaults", + "exclude_none", + "round_trip", + "warnings", + "serialize_as_any", + ) + return self.model_dump_json( + **{k: v for k, v in info.__dict__.items() if k in json_dump_fields} + ) + class LinkMLMeta(RootModel): root: Dict[str, Any] = {} diff --git a/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_epoch.py b/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_epoch.py index e5cc476..eb538a9 100644 --- a/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_epoch.py +++ b/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_epoch.py @@ -1,5 +1,6 @@ from __future__ import annotations +import pdb import re import sys from datetime import date, datetime, time @@ -17,6 +18,7 @@ from pydantic import ( RootModel, ValidationInfo, field_validator, + model_serializer, model_validator, ) @@ -124,6 +126,32 @@ class ConfiguredBaseModel(BaseModel): v["value"] = extras return v + @model_serializer(mode="wrap", when_used="json") + def serialize_model(self, nxt, info) -> Dict[str, Any]: + try: + return nxt(self, info) + except Exception as e: + return {"ERROR": str(e), "TYPE": str(type(self))} + if "Circular reference" in str(e): + return {"REFERENCE": "REFERENCE"} + pdb.set_trace() + json_dump_fields = ( + "indent", + "include", + "exclude", + "context", + "by_alias", + "exclude_unset", + "exclude_defaults", + "exclude_none", + "round_trip", + "warnings", + "serialize_as_any", + ) + return self.model_dump_json( + **{k: v for k, v in info.__dict__.items() if k in json_dump_fields} + ) + class LinkMLMeta(RootModel): root: Dict[str, Any] = {} diff --git a/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_file.py b/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_file.py index 2b52d85..c6c3a44 100644 --- a/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_file.py +++ b/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_file.py @@ -1,5 +1,6 @@ from __future__ import annotations +import pdb import re import sys from datetime import date, datetime, time @@ -9,7 +10,15 @@ from typing import Any, ClassVar, Dict, List, Literal, Optional, Union import numpy as np from numpydantic import NDArray, Shape -from pydantic import BaseModel, ConfigDict, Field, RootModel, field_validator, model_validator +from pydantic import ( + BaseModel, + ConfigDict, + Field, + RootModel, + field_validator, + model_serializer, + model_validator, +) from ...core.v2_7_0.core_nwb_base import ( Images, @@ -137,6 +146,32 @@ class ConfiguredBaseModel(BaseModel): v["value"] = extras return v + @model_serializer(mode="wrap", when_used="json") + def serialize_model(self, nxt, info) -> Dict[str, Any]: + try: + return nxt(self, info) + except Exception as e: + return {"ERROR": str(e), "TYPE": str(type(self))} + if "Circular reference" in str(e): + return {"REFERENCE": "REFERENCE"} + pdb.set_trace() + json_dump_fields = ( + "indent", + "include", + "exclude", + "context", + "by_alias", + "exclude_unset", + "exclude_defaults", + "exclude_none", + "round_trip", + "warnings", + "serialize_as_any", + ) + return self.model_dump_json( + **{k: v for k, v in info.__dict__.items() if k in json_dump_fields} + ) + class LinkMLMeta(RootModel): root: Dict[str, Any] = {} diff --git a/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_icephys.py b/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_icephys.py index 7a77474..5cdedeb 100644 --- a/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_icephys.py +++ b/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_icephys.py @@ -1,5 +1,6 @@ from __future__ import annotations +import pdb import re import sys from datetime import date, datetime, time @@ -17,6 +18,7 @@ from pydantic import ( RootModel, ValidationInfo, field_validator, + model_serializer, model_validator, ) @@ -133,6 +135,32 @@ class ConfiguredBaseModel(BaseModel): v["value"] = extras return v + @model_serializer(mode="wrap", when_used="json") + def serialize_model(self, nxt, info) -> Dict[str, Any]: + try: + return nxt(self, info) + except Exception as e: + return {"ERROR": str(e), "TYPE": str(type(self))} + if "Circular reference" in str(e): + return {"REFERENCE": "REFERENCE"} + pdb.set_trace() + json_dump_fields = ( + "indent", + "include", + "exclude", + "context", + "by_alias", + "exclude_unset", + "exclude_defaults", + "exclude_none", + "round_trip", + "warnings", + "serialize_as_any", + ) + return self.model_dump_json( + **{k: v for k, v in info.__dict__.items() if k in json_dump_fields} + ) + class LinkMLMeta(RootModel): root: Dict[str, Any] = {} diff --git a/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_image.py b/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_image.py index 66edd17..1d80f97 100644 --- a/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_image.py +++ b/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_image.py @@ -1,5 +1,6 @@ from __future__ import annotations +import pdb import re import sys from datetime import date, datetime, time @@ -9,7 +10,15 @@ from typing import Any, ClassVar, Dict, List, Literal, Optional, Union import numpy as np from numpydantic import NDArray, Shape -from pydantic import BaseModel, ConfigDict, Field, RootModel, field_validator, model_validator +from pydantic import ( + BaseModel, + ConfigDict, + Field, + RootModel, + field_validator, + model_serializer, + model_validator, +) from ...core.v2_7_0.core_nwb_base import ( Image, @@ -116,6 +125,32 @@ class ConfiguredBaseModel(BaseModel): v["value"] = extras return v + @model_serializer(mode="wrap", when_used="json") + def serialize_model(self, nxt, info) -> Dict[str, Any]: + try: + return nxt(self, info) + except Exception as e: + return {"ERROR": str(e), "TYPE": str(type(self))} + if "Circular reference" in str(e): + return {"REFERENCE": "REFERENCE"} + pdb.set_trace() + json_dump_fields = ( + "indent", + "include", + "exclude", + "context", + "by_alias", + "exclude_unset", + "exclude_defaults", + "exclude_none", + "round_trip", + "warnings", + "serialize_as_any", + ) + return self.model_dump_json( + **{k: v for k, v in info.__dict__.items() if k in json_dump_fields} + ) + class LinkMLMeta(RootModel): root: Dict[str, Any] = {} diff --git a/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_misc.py b/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_misc.py index 513f113..f8431f7 100644 --- a/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_misc.py +++ b/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_misc.py @@ -1,5 +1,6 @@ from __future__ import annotations +import pdb import re import sys from datetime import date, datetime, time @@ -17,6 +18,7 @@ from pydantic import ( RootModel, ValidationInfo, field_validator, + model_serializer, model_validator, ) @@ -126,6 +128,32 @@ class ConfiguredBaseModel(BaseModel): v["value"] = extras return v + @model_serializer(mode="wrap", when_used="json") + def serialize_model(self, nxt, info) -> Dict[str, Any]: + try: + return nxt(self, info) + except Exception as e: + return {"ERROR": str(e), "TYPE": str(type(self))} + if "Circular reference" in str(e): + return {"REFERENCE": "REFERENCE"} + pdb.set_trace() + json_dump_fields = ( + "indent", + "include", + "exclude", + "context", + "by_alias", + "exclude_unset", + "exclude_defaults", + "exclude_none", + "round_trip", + "warnings", + "serialize_as_any", + ) + return self.model_dump_json( + **{k: v for k, v in info.__dict__.items() if k in json_dump_fields} + ) + class LinkMLMeta(RootModel): root: Dict[str, Any] = {} diff --git a/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_ogen.py b/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_ogen.py index 8e6d7c3..5e35936 100644 --- a/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_ogen.py +++ b/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_ogen.py @@ -1,5 +1,6 @@ from __future__ import annotations +import pdb import re import sys from datetime import date, datetime, time @@ -9,7 +10,15 @@ from typing import Any, ClassVar, Dict, List, Literal, Optional, Union import numpy as np from numpydantic import NDArray, Shape -from pydantic import BaseModel, ConfigDict, Field, RootModel, field_validator, model_validator +from pydantic import ( + BaseModel, + ConfigDict, + Field, + RootModel, + field_validator, + model_serializer, + model_validator, +) from ...core.v2_7_0.core_nwb_base import ( NWBContainer, @@ -115,6 +124,32 @@ class ConfiguredBaseModel(BaseModel): v["value"] = extras return v + @model_serializer(mode="wrap", when_used="json") + def serialize_model(self, nxt, info) -> Dict[str, Any]: + try: + return nxt(self, info) + except Exception as e: + return {"ERROR": str(e), "TYPE": str(type(self))} + if "Circular reference" in str(e): + return {"REFERENCE": "REFERENCE"} + pdb.set_trace() + json_dump_fields = ( + "indent", + "include", + "exclude", + "context", + "by_alias", + "exclude_unset", + "exclude_defaults", + "exclude_none", + "round_trip", + "warnings", + "serialize_as_any", + ) + return self.model_dump_json( + **{k: v for k, v in info.__dict__.items() if k in json_dump_fields} + ) + class LinkMLMeta(RootModel): root: Dict[str, Any] = {} diff --git a/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_ophys.py b/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_ophys.py index 5fdf2a0..e2a0bf2 100644 --- a/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_ophys.py +++ b/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_ophys.py @@ -1,5 +1,6 @@ from __future__ import annotations +import pdb import re import sys from datetime import date, datetime, time @@ -17,6 +18,7 @@ from pydantic import ( RootModel, ValidationInfo, field_validator, + model_serializer, model_validator, ) @@ -133,6 +135,32 @@ class ConfiguredBaseModel(BaseModel): v["value"] = extras return v + @model_serializer(mode="wrap", when_used="json") + def serialize_model(self, nxt, info) -> Dict[str, Any]: + try: + return nxt(self, info) + except Exception as e: + return {"ERROR": str(e), "TYPE": str(type(self))} + if "Circular reference" in str(e): + return {"REFERENCE": "REFERENCE"} + pdb.set_trace() + json_dump_fields = ( + "indent", + "include", + "exclude", + "context", + "by_alias", + "exclude_unset", + "exclude_defaults", + "exclude_none", + "round_trip", + "warnings", + "serialize_as_any", + ) + return self.model_dump_json( + **{k: v for k, v in info.__dict__.items() if k in json_dump_fields} + ) + class LinkMLMeta(RootModel): root: Dict[str, Any] = {} diff --git a/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_retinotopy.py b/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_retinotopy.py index 8affbfc..f5ebc0f 100644 --- a/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_retinotopy.py +++ b/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/core_nwb_retinotopy.py @@ -1,5 +1,6 @@ from __future__ import annotations +import pdb import re import sys from datetime import date, datetime, time @@ -9,7 +10,15 @@ from typing import Any, ClassVar, Dict, List, Literal, Optional, Union import numpy as np from numpydantic import NDArray, Shape -from pydantic import BaseModel, ConfigDict, Field, RootModel, field_validator, model_validator +from pydantic import ( + BaseModel, + ConfigDict, + Field, + RootModel, + field_validator, + model_serializer, + model_validator, +) from ...core.v2_7_0.core_nwb_base import NWBDataInterface @@ -109,6 +118,32 @@ class ConfiguredBaseModel(BaseModel): v["value"] = extras return v + @model_serializer(mode="wrap", when_used="json") + def serialize_model(self, nxt, info) -> Dict[str, Any]: + try: + return nxt(self, info) + except Exception as e: + return {"ERROR": str(e), "TYPE": str(type(self))} + if "Circular reference" in str(e): + return {"REFERENCE": "REFERENCE"} + pdb.set_trace() + json_dump_fields = ( + "indent", + "include", + "exclude", + "context", + "by_alias", + "exclude_unset", + "exclude_defaults", + "exclude_none", + "round_trip", + "warnings", + "serialize_as_any", + ) + return self.model_dump_json( + **{k: v for k, v in info.__dict__.items() if k in json_dump_fields} + ) + class LinkMLMeta(RootModel): root: Dict[str, Any] = {} diff --git a/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/namespace.py b/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/namespace.py index 68e374e..d8c204f 100644 --- a/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/namespace.py +++ b/nwb_models/src/nwb_models/models/pydantic/core/v2_7_0/namespace.py @@ -1,5 +1,6 @@ from __future__ import annotations +import pdb import re import sys from datetime import date, datetime, time @@ -8,7 +9,15 @@ from enum import Enum from typing import Any, ClassVar, Dict, List, Literal, Optional, Union import numpy as np -from pydantic import BaseModel, ConfigDict, Field, RootModel, field_validator, model_validator +from pydantic import ( + BaseModel, + ConfigDict, + Field, + RootModel, + field_validator, + model_serializer, + model_validator, +) from ...core.v2_7_0.core_nwb_base import ( Image, @@ -280,6 +289,32 @@ class ConfiguredBaseModel(BaseModel): v["value"] = extras return v + @model_serializer(mode="wrap", when_used="json") + def serialize_model(self, nxt, info) -> Dict[str, Any]: + try: + return nxt(self, info) + except Exception as e: + return {"ERROR": str(e), "TYPE": str(type(self))} + if "Circular reference" in str(e): + return {"REFERENCE": "REFERENCE"} + pdb.set_trace() + json_dump_fields = ( + "indent", + "include", + "exclude", + "context", + "by_alias", + "exclude_unset", + "exclude_defaults", + "exclude_none", + "round_trip", + "warnings", + "serialize_as_any", + ) + return self.model_dump_json( + **{k: v for k, v in info.__dict__.items() if k in json_dump_fields} + ) + class LinkMLMeta(RootModel): root: Dict[str, Any] = {} diff --git a/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_8_0/hdmf_common_base.py b/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_8_0/hdmf_common_base.py index 2d2b0b9..88dd602 100644 --- a/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_8_0/hdmf_common_base.py +++ b/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_8_0/hdmf_common_base.py @@ -1,5 +1,6 @@ from __future__ import annotations +import pdb import re import sys from datetime import date, datetime, time @@ -8,7 +9,15 @@ from enum import Enum from typing import Any, ClassVar, Dict, List, Literal, Optional, Union import numpy as np -from pydantic import BaseModel, ConfigDict, Field, RootModel, field_validator, model_validator +from pydantic import ( + BaseModel, + ConfigDict, + Field, + RootModel, + field_validator, + model_serializer, + model_validator, +) metamodel_version = "None" @@ -106,6 +115,32 @@ class ConfiguredBaseModel(BaseModel): v["value"] = extras return v + @model_serializer(mode="wrap", when_used="json") + def serialize_model(self, nxt, info) -> Dict[str, Any]: + try: + return nxt(self, info) + except Exception as e: + return {"ERROR": str(e), "TYPE": str(type(self))} + if "Circular reference" in str(e): + return {"REFERENCE": "REFERENCE"} + pdb.set_trace() + json_dump_fields = ( + "indent", + "include", + "exclude", + "context", + "by_alias", + "exclude_unset", + "exclude_defaults", + "exclude_none", + "round_trip", + "warnings", + "serialize_as_any", + ) + return self.model_dump_json( + **{k: v for k, v in info.__dict__.items() if k in json_dump_fields} + ) + class LinkMLMeta(RootModel): root: Dict[str, Any] = {} diff --git a/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_8_0/hdmf_common_sparse.py b/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_8_0/hdmf_common_sparse.py index 1aad5a2..4410802 100644 --- a/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_8_0/hdmf_common_sparse.py +++ b/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_8_0/hdmf_common_sparse.py @@ -1,5 +1,6 @@ from __future__ import annotations +import pdb import re import sys from datetime import date, datetime, time @@ -9,7 +10,15 @@ from typing import Any, ClassVar, Dict, List, Literal, Optional, Union import numpy as np from numpydantic import NDArray, Shape -from pydantic import BaseModel, ConfigDict, Field, RootModel, field_validator, model_validator +from pydantic import ( + BaseModel, + ConfigDict, + Field, + RootModel, + field_validator, + model_serializer, + model_validator, +) from ...hdmf_common.v1_8_0.hdmf_common_base import Container @@ -109,6 +118,32 @@ class ConfiguredBaseModel(BaseModel): v["value"] = extras return v + @model_serializer(mode="wrap", when_used="json") + def serialize_model(self, nxt, info) -> Dict[str, Any]: + try: + return nxt(self, info) + except Exception as e: + return {"ERROR": str(e), "TYPE": str(type(self))} + if "Circular reference" in str(e): + return {"REFERENCE": "REFERENCE"} + pdb.set_trace() + json_dump_fields = ( + "indent", + "include", + "exclude", + "context", + "by_alias", + "exclude_unset", + "exclude_defaults", + "exclude_none", + "round_trip", + "warnings", + "serialize_as_any", + ) + return self.model_dump_json( + **{k: v for k, v in info.__dict__.items() if k in json_dump_fields} + ) + class LinkMLMeta(RootModel): root: Dict[str, Any] = {} diff --git a/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_8_0/hdmf_common_table.py b/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_8_0/hdmf_common_table.py index b779c48..b6605d3 100644 --- a/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_8_0/hdmf_common_table.py +++ b/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_8_0/hdmf_common_table.py @@ -1,5 +1,6 @@ from __future__ import annotations +import pdb import re import sys from datetime import date, datetime, time @@ -32,6 +33,7 @@ from pydantic import ( ValidationInfo, ValidatorFunctionWrapHandler, field_validator, + model_serializer, model_validator, ) @@ -133,6 +135,32 @@ class ConfiguredBaseModel(BaseModel): v["value"] = extras return v + @model_serializer(mode="wrap", when_used="json") + def serialize_model(self, nxt, info) -> Dict[str, Any]: + try: + return nxt(self, info) + except Exception as e: + return {"ERROR": str(e), "TYPE": str(type(self))} + if "Circular reference" in str(e): + return {"REFERENCE": "REFERENCE"} + pdb.set_trace() + json_dump_fields = ( + "indent", + "include", + "exclude", + "context", + "by_alias", + "exclude_unset", + "exclude_defaults", + "exclude_none", + "round_trip", + "warnings", + "serialize_as_any", + ) + return self.model_dump_json( + **{k: v for k, v in info.__dict__.items() if k in json_dump_fields} + ) + class LinkMLMeta(RootModel): root: Dict[str, Any] = {} @@ -908,7 +936,7 @@ linkml_meta = LinkMLMeta( ) -class VectorData(VectorDataMixin): +class VectorData(VectorDataMixin[T], Generic[T]): """ An n-dimensional dataset representing a column of a DynamicTable. If used without an accompanying VectorIndex, first dimension is along the rows of the DynamicTable and each step along the first dimension is a cell of the larger table. VectorData can also be used to represent a ragged array if paired with a VectorIndex. This allows for storing arrays of varying length in a single cell of the DynamicTable by indexing into this VectorData. The first vector is at VectorData[0:VectorIndex[0]]. The second vector is at VectorData[VectorIndex[0]:VectorIndex[1]], and so on. """ @@ -922,7 +950,7 @@ class VectorData(VectorDataMixin): value: Optional[T] = Field(None) -class VectorIndex(VectorIndexMixin): +class VectorIndex(VectorIndexMixin[T], Generic[T]): """ Used with VectorData to encode a ragged array. An array of indices into the first dimension of the target VectorData, and forming a map between the rows of a DynamicTable and the indices of the VectorData. The name of the VectorIndex is expected to be the name of the target VectorData object followed by \"_index\". """ @@ -936,14 +964,7 @@ class VectorIndex(VectorIndexMixin): None, description="""Reference to the target dataset that this index applies to.""" ) description: str = Field(..., description="""Description of what these vectors represent.""") - value: Optional[ - Union[ - NDArray[Shape["* dim0"], Any], - NDArray[Shape["* dim0, * dim1"], Any], - NDArray[Shape["* dim0, * dim1, * dim2"], Any], - NDArray[Shape["* dim0, * dim1, * dim2, * dim3"], Any], - ] - ] = Field(None) + value: Optional[T] = Field(None) class ElementIdentifiers(ElementIdentifiersMixin, Data): diff --git a/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_8_0/namespace.py b/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_8_0/namespace.py index 64c8e43..76795dc 100644 --- a/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_8_0/namespace.py +++ b/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_8_0/namespace.py @@ -1,5 +1,6 @@ from __future__ import annotations +import pdb import re import sys from datetime import date, datetime, time @@ -8,7 +9,15 @@ from enum import Enum from typing import Any, ClassVar, Dict, List, Literal, Optional, Union import numpy as np -from pydantic import BaseModel, ConfigDict, Field, RootModel, field_validator, model_validator +from pydantic import ( + BaseModel, + ConfigDict, + Field, + RootModel, + field_validator, + model_serializer, + model_validator, +) from ...hdmf_common.v1_8_0.hdmf_common_base import Container, Data, SimpleMultiContainer from ...hdmf_common.v1_8_0.hdmf_common_sparse import CSRMatrix @@ -117,6 +126,32 @@ class ConfiguredBaseModel(BaseModel): v["value"] = extras return v + @model_serializer(mode="wrap", when_used="json") + def serialize_model(self, nxt, info) -> Dict[str, Any]: + try: + return nxt(self, info) + except Exception as e: + return {"ERROR": str(e), "TYPE": str(type(self))} + if "Circular reference" in str(e): + return {"REFERENCE": "REFERENCE"} + pdb.set_trace() + json_dump_fields = ( + "indent", + "include", + "exclude", + "context", + "by_alias", + "exclude_unset", + "exclude_defaults", + "exclude_none", + "round_trip", + "warnings", + "serialize_as_any", + ) + return self.model_dump_json( + **{k: v for k, v in info.__dict__.items() if k in json_dump_fields} + ) + class LinkMLMeta(RootModel): root: Dict[str, Any] = {} diff --git a/nwb_models/src/nwb_models/models/pydantic/hdmf_experimental/v0_5_0/hdmf_experimental_experimental.py b/nwb_models/src/nwb_models/models/pydantic/hdmf_experimental/v0_5_0/hdmf_experimental_experimental.py index 6aaf19b..21ed983 100644 --- a/nwb_models/src/nwb_models/models/pydantic/hdmf_experimental/v0_5_0/hdmf_experimental_experimental.py +++ b/nwb_models/src/nwb_models/models/pydantic/hdmf_experimental/v0_5_0/hdmf_experimental_experimental.py @@ -1,5 +1,6 @@ from __future__ import annotations +import pdb import re import sys from datetime import date, datetime, time @@ -9,7 +10,15 @@ from typing import Any, ClassVar, Dict, List, Literal, Optional, Union import numpy as np from numpydantic import NDArray, Shape -from pydantic import BaseModel, ConfigDict, Field, RootModel, field_validator, model_validator +from pydantic import ( + BaseModel, + ConfigDict, + Field, + RootModel, + field_validator, + model_serializer, + model_validator, +) from ...hdmf_common.v1_8_0.hdmf_common_table import VectorData @@ -109,6 +118,32 @@ class ConfiguredBaseModel(BaseModel): v["value"] = extras return v + @model_serializer(mode="wrap", when_used="json") + def serialize_model(self, nxt, info) -> Dict[str, Any]: + try: + return nxt(self, info) + except Exception as e: + return {"ERROR": str(e), "TYPE": str(type(self))} + if "Circular reference" in str(e): + return {"REFERENCE": "REFERENCE"} + pdb.set_trace() + json_dump_fields = ( + "indent", + "include", + "exclude", + "context", + "by_alias", + "exclude_unset", + "exclude_defaults", + "exclude_none", + "round_trip", + "warnings", + "serialize_as_any", + ) + return self.model_dump_json( + **{k: v for k, v in info.__dict__.items() if k in json_dump_fields} + ) + class LinkMLMeta(RootModel): root: Dict[str, Any] = {} diff --git a/nwb_models/src/nwb_models/models/pydantic/hdmf_experimental/v0_5_0/hdmf_experimental_resources.py b/nwb_models/src/nwb_models/models/pydantic/hdmf_experimental/v0_5_0/hdmf_experimental_resources.py index 8acfc1a..03f3ea1 100644 --- a/nwb_models/src/nwb_models/models/pydantic/hdmf_experimental/v0_5_0/hdmf_experimental_resources.py +++ b/nwb_models/src/nwb_models/models/pydantic/hdmf_experimental/v0_5_0/hdmf_experimental_resources.py @@ -1,5 +1,6 @@ from __future__ import annotations +import pdb import re import sys from datetime import date, datetime, time @@ -9,7 +10,15 @@ from typing import Any, ClassVar, Dict, List, Literal, Optional, Union import numpy as np from numpydantic import NDArray, Shape -from pydantic import BaseModel, ConfigDict, Field, RootModel, field_validator, model_validator +from pydantic import ( + BaseModel, + ConfigDict, + Field, + RootModel, + field_validator, + model_serializer, + model_validator, +) from ...hdmf_common.v1_8_0.hdmf_common_base import Container, Data @@ -109,6 +118,32 @@ class ConfiguredBaseModel(BaseModel): v["value"] = extras return v + @model_serializer(mode="wrap", when_used="json") + def serialize_model(self, nxt, info) -> Dict[str, Any]: + try: + return nxt(self, info) + except Exception as e: + return {"ERROR": str(e), "TYPE": str(type(self))} + if "Circular reference" in str(e): + return {"REFERENCE": "REFERENCE"} + pdb.set_trace() + json_dump_fields = ( + "indent", + "include", + "exclude", + "context", + "by_alias", + "exclude_unset", + "exclude_defaults", + "exclude_none", + "round_trip", + "warnings", + "serialize_as_any", + ) + return self.model_dump_json( + **{k: v for k, v in info.__dict__.items() if k in json_dump_fields} + ) + class LinkMLMeta(RootModel): root: Dict[str, Any] = {} diff --git a/nwb_models/src/nwb_models/models/pydantic/hdmf_experimental/v0_5_0/namespace.py b/nwb_models/src/nwb_models/models/pydantic/hdmf_experimental/v0_5_0/namespace.py index 46184eb..b03be6f 100644 --- a/nwb_models/src/nwb_models/models/pydantic/hdmf_experimental/v0_5_0/namespace.py +++ b/nwb_models/src/nwb_models/models/pydantic/hdmf_experimental/v0_5_0/namespace.py @@ -1,5 +1,6 @@ from __future__ import annotations +import pdb import re import sys from datetime import date, datetime, time @@ -8,7 +9,15 @@ from enum import Enum from typing import Any, ClassVar, Dict, List, Literal, Optional, Union import numpy as np -from pydantic import BaseModel, ConfigDict, Field, RootModel, field_validator, model_validator +from pydantic import ( + BaseModel, + ConfigDict, + Field, + RootModel, + field_validator, + model_serializer, + model_validator, +) from ...hdmf_common.v1_8_0.hdmf_common_base import Container, Data, SimpleMultiContainer from ...hdmf_common.v1_8_0.hdmf_common_sparse import CSRMatrix @@ -127,6 +136,32 @@ class ConfiguredBaseModel(BaseModel): v["value"] = extras return v + @model_serializer(mode="wrap", when_used="json") + def serialize_model(self, nxt, info) -> Dict[str, Any]: + try: + return nxt(self, info) + except Exception as e: + return {"ERROR": str(e), "TYPE": str(type(self))} + if "Circular reference" in str(e): + return {"REFERENCE": "REFERENCE"} + pdb.set_trace() + json_dump_fields = ( + "indent", + "include", + "exclude", + "context", + "by_alias", + "exclude_unset", + "exclude_defaults", + "exclude_none", + "round_trip", + "warnings", + "serialize_as_any", + ) + return self.model_dump_json( + **{k: v for k, v in info.__dict__.items() if k in json_dump_fields} + ) + class LinkMLMeta(RootModel): root: Dict[str, Any] = {} diff --git a/nwb_models/src/nwb_models/schema/linkml/hdmf_common/v1_8_0/hdmf-common.nwb.language.yaml b/nwb_models/src/nwb_models/schema/linkml/hdmf_common/v1_8_0/hdmf-common.nwb.language.yaml index 6b8ce10..e933768 100644 --- a/nwb_models/src/nwb_models/schema/linkml/hdmf_common/v1_8_0/hdmf-common.nwb.language.yaml +++ b/nwb_models/src/nwb_models/schema/linkml/hdmf_common/v1_8_0/hdmf-common.nwb.language.yaml @@ -5,7 +5,7 @@ annotations: value: 'False' namespace: tag: namespace - value: hdmf-experimental + value: core description: Adapter objects to mimic the behavior of elements in the nwb-schema-language id: nwb.language imports: diff --git a/nwb_models/src/nwb_models/schema/linkml/hdmf_experimental/v0_5_0/hdmf-experimental.nwb.language.yaml b/nwb_models/src/nwb_models/schema/linkml/hdmf_experimental/v0_5_0/hdmf-experimental.nwb.language.yaml index dcf2549..925d1ba 100644 --- a/nwb_models/src/nwb_models/schema/linkml/hdmf_experimental/v0_5_0/hdmf-experimental.nwb.language.yaml +++ b/nwb_models/src/nwb_models/schema/linkml/hdmf_experimental/v0_5_0/hdmf-experimental.nwb.language.yaml @@ -5,7 +5,7 @@ annotations: value: 'False' namespace: tag: namespace - value: hdmf-experimental + value: core description: Adapter objects to mimic the behavior of elements in the nwb-schema-language id: nwb.language imports: