|
| 1 | +# Tutorial |
| 2 | + |
| 3 | +## Group Definition |
| 4 | + |
| 5 | +```python |
| 6 | +from oqd_dataschema import GroupBase, Attrs |
| 7 | + |
| 8 | +class CustomGroup(GroupBase): |
| 9 | + attrs: Attrs = Field( |
| 10 | + default_factory=lambda: dict( |
| 11 | + timestamp=str(datetime.datetime.now(datetime.timezone.utc)) |
| 12 | + ) |
| 13 | + ) |
| 14 | + dset: Dataset |
| 15 | + tbl: Table |
| 16 | + fld: Folder |
| 17 | +``` |
| 18 | + |
| 19 | +Defined groups are automatically registered into the [`GroupRegistry`][oqd_dataschema.group.GroupRegistry]. |
| 20 | + |
| 21 | +```python |
| 22 | +from oqd_dataschema import GroupRegistry |
| 23 | + |
| 24 | +GroupRegistry.groups |
| 25 | +``` |
| 26 | + |
| 27 | +## Initialize Group |
| 28 | + |
| 29 | +```python |
| 30 | +from oqd_dataschema import Dataset, Table, Folder, unstructured_to_structured |
| 31 | + |
| 32 | +dset = Dataset(data=np.linspace(0, 1, 101).astype(np.float32)) |
| 33 | +tbl = Table( |
| 34 | + columns=[("t", "float32"), ("x", "complex128")], |
| 35 | + data=unstructured_to_structured( |
| 36 | + np.stack([np.linspace(0, 1, 101), np.sin(np.linspace(0, 1, 101))], -1), |
| 37 | + dtype=np.dtype([("t", np.float32), ("x", np.complex128)]), |
| 38 | + ), |
| 39 | +) |
| 40 | +fld = Folder( |
| 41 | + document_schema={"t": "float32", "signal": {"x": "complex128", "y": "complex128"}}, |
| 42 | + data=unstructured_to_structured( |
| 43 | + np.stack( |
| 44 | + [ |
| 45 | + np.linspace(0, 1, 101), |
| 46 | + np.sin(np.linspace(0, 1, 101)), |
| 47 | + np.cos(np.linspace(0, 1, 101)), |
| 48 | + ], |
| 49 | + -1, |
| 50 | + ), |
| 51 | + dtype=np.dtype( |
| 52 | + [ |
| 53 | + ("t", np.float32), |
| 54 | + ("signal", np.dtype([("x", np.complex128), ("y", np.complex128)])), |
| 55 | + ] |
| 56 | + ), |
| 57 | + ), |
| 58 | +) |
| 59 | + |
| 60 | + |
| 61 | +group = CustomGroup(dset=dset, tbl=tbl, fld=fld) |
| 62 | +``` |
| 63 | + |
| 64 | +## Initialize Datastore |
| 65 | + |
| 66 | +```python |
| 67 | +from oqd_datastore import Datastore |
| 68 | + |
| 69 | +datastore = Datastore(groups={"g1": group}) |
| 70 | +``` |
| 71 | + |
| 72 | +## Save Datastore |
| 73 | + |
| 74 | +```python |
| 75 | +datastore.model_dump_hdf5(pathlib.Path("datastore.h5"), mode="w") |
| 76 | +``` |
| 77 | + |
| 78 | +## Load Datastore |
| 79 | + |
| 80 | +```python |
| 81 | +reloaded_datastore = Datastore.model_validate_hdf5(pathlib.Path("datastore.h5")) |
| 82 | +``` |
0 commit comments