Skip to content

Commit 5bc133a

Browse files
committed
[docs] update tutorial on docs
1 parent 7d18f6c commit 5bc133a

File tree

1 file changed

+37
-71
lines changed

1 file changed

+37
-71
lines changed

docs/tutorial.md

Lines changed: 37 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,74 @@
1-
21
# Tutorial
32

4-
```python
5-
import pathlib
6-
7-
import numpy as np
8-
from rich.pretty import pprint
9-
10-
from oqd_dataschema.base import Dataset
11-
from oqd_dataschema.datastore import Datastore
12-
from oqd_dataschema.groups import (
13-
ExpectationValueDataGroup,
14-
MeasurementOutcomesDataGroup,
15-
SinaraRawDataGroup,
16-
)
17-
```
3+
## Group Definition
184

195
```python
20-
raw = SinaraRawDataGroup(
21-
camera_images=Dataset(shape=(3, 2, 2), dtype="float32"),
22-
attrs={"date": "2025-03-26", "version": 0.1},
23-
)
24-
pprint(raw)
25-
```
6+
from oqd_dataschema import GroupBase, Attrs
267

27-
28-
29-
```python
30-
raw.camera_images.data = np.random.uniform(size=(3, 2, 2)).astype("float32")
31-
pprint(raw)
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+
t: Dataset
15+
x: Dataset
3216
```
3317

34-
18+
Defined groups are automatically registered into the [`GroupRegistry`][oqd_dataschema.group.GroupRegistry].
3519

3620
```python
37-
raw.camera_images.data = np.random.uniform(size=(3, 2, 2)).astype("float32")
38-
```
39-
21+
from oqd_dataschema import GroupRegistry
4022

41-
42-
```python
43-
data = Datastore(groups={"raw": raw})
44-
pprint(data)
23+
GroupRegistry.groups
4524
```
4625

47-
48-
26+
## Initialize Group
4927

5028
```python
51-
def process_raw(raw: SinaraRawDataGroup) -> MeasurementOutcomesDataGroup:
52-
processed = MeasurementOutcomesDataGroup(
53-
outcomes=Dataset(
54-
data=np.round(raw.camera_images.data.mean(axis=(1, 2))),
55-
)
56-
)
57-
return processed
29+
t = np.linspace(0, 1, 101).astype(np.float32)
30+
x = np.sin(t).astype(np.complex64)
5831

32+
group = CustomGroup(
33+
t=Dataset(dtype="float32", shape=(101,)), x=Dataset(dtype="complex64", shape=(101,))
34+
)
5935

60-
processed = process_raw(data.groups["raw"])
61-
pprint(processed)
36+
group.t.data = t
37+
group.x.data = x
6238
```
6339

64-
65-
40+
## Initialize Datastore
6641

6742
```python
68-
data.groups.update(processed=processed)
69-
pprint(data)
43+
from oqd_datastore import Datastore
44+
45+
datastore = Datastore(groups={"g1": group})
7046
```
7147

48+
## Data pipeline
7249

50+
```python
51+
def process(datastore) -> Datastore:
52+
_g = datastore.get("g1")
7353

54+
g2 = CustomGroup(t=Dataset(data=_g.t.data), x=Dataset(data=_g.x.data + 1j))
7455

75-
```python
76-
def process_outcomes(
77-
measurements: MeasurementOutcomesDataGroup,
78-
) -> ExpectationValueDataGroup:
79-
expval = ExpectationValueDataGroup(
80-
expectation_value=Dataset(
81-
shape=(),
82-
dtype="float32",
83-
data=measurements.outcomes.data.mean(),
84-
attrs={"date": "20", "input": 10},
85-
)
86-
)
87-
return expval
56+
datastore.add(g2=g2)
8857

58+
return datastore
8959

90-
expval = process_outcomes(processed)
91-
data.groups.update(expval=process_outcomes(data.groups["processed"]))
9260

93-
pprint(expval)
61+
datastore.pipe(process)
9462
```
9563

96-
64+
## Save Datastore
9765

9866
```python
99-
filepath = pathlib.Path("test.h5")
100-
data.model_dump_hdf5(filepath)
67+
datastore.model_dump_hdf5(pathlib.Path("datastore.h5"), mode="w")
10168
```
10269

103-
70+
## Load Datastore
10471

10572
```python
106-
data_reload = Datastore.model_validate_hdf5(filepath)
107-
pprint(data_reload)
73+
reloaded_datastore = Datastore.model_validate_hdf5(pathlib.Path("datastore.h5"))
10874
```

0 commit comments

Comments
 (0)