1414
1515# %%
1616
17+ import json
1718import pathlib
1819from typing import Any , Dict , Literal , Optional
1920
2021import h5py
2122import numpy as np
22- from pydantic import BaseModel , model_validator
23+ from pydantic import (
24+ BaseModel ,
25+ field_validator ,
26+ )
2327from pydantic .types import TypeVar
2428
25- from oqd_dataschema .base import Dataset , GroupBase , GroupRegistry
29+ from oqd_dataschema .base import Attrs , Dataset , GroupBase , GroupRegistry
2630
2731########################################################################################
2832
@@ -44,15 +48,17 @@ class Datastore(BaseModel, extra="forbid"):
4448
4549 groups : Dict [str , Any ]
4650
47- @model_validator (mode = "before" )
51+ attrs : Attrs = {}
52+
53+ @field_validator ("groups" , mode = "before" )
4854 @classmethod
4955 def validate_groups (cls , data ):
50- if isinstance (data , dict ) and "groups" in data :
56+ if isinstance (data , dict ):
5157 # Get the current adapter from registry
5258 try :
5359 validated_groups = {}
5460
55- for key , group_data in data [ "groups" ] .items ():
61+ for key , group_data in data .items ():
5662 if isinstance (group_data , GroupBase ):
5763 # Already a Group instance
5864 validated_groups [key ] = group_data
@@ -66,7 +72,7 @@ def validate_groups(cls, data):
6672 f"Invalid group data for key '{ key } ': { type (group_data )} "
6773 )
6874
69- data [ "groups" ] = validated_groups
75+ data = validated_groups
7076
7177 except ValueError as e :
7278 if "No group types registered" in str (e ):
@@ -90,13 +96,17 @@ def model_dump_hdf5(self, filepath: pathlib.Path, mode: Literal["w", "a"] = "a")
9096
9197 with h5py .File (filepath , mode ) as f :
9298 # store the model JSON schema
93- f .attrs ["model" ] = self .model_dump_json ()
99+ f .attrs ["_model_signature" ] = self .model_dump_json ()
100+ for akey , attr in self .attrs .items ():
101+ f .attrs [akey ] = attr
94102
95103 # store each group
96104 for gkey , group in self .groups .items ():
97105 if gkey in f .keys ():
98106 del f [gkey ]
99107 h5_group = f .create_group (gkey )
108+
109+ h5_group .attrs ["_model_schema" ] = json .dumps (group .model_json_schema ())
100110 for akey , attr in group .attrs .items ():
101111 h5_group .attrs [akey ] = attr
102112
@@ -118,7 +128,7 @@ def model_validate_hdf5(
118128 filepath (pathlib.Path): The path to the HDF5 file where the model data will be read and validated from.
119129 """
120130 with h5py .File (filepath , "r" ) as f :
121- self = cls .model_validate_json (f .attrs ["model " ])
131+ self = cls .model_validate_json (f .attrs ["_model_signature " ])
122132
123133 # loop through all groups in the model schema and load HDF5 store
124134 for gkey , group in self .groups .items ():
0 commit comments