Skip to content

Commit f3babcd

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

File tree

1 file changed

+33
-78
lines changed

1 file changed

+33
-78
lines changed

docs/tutorial.md

Lines changed: 33 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,63 @@
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-
```
26-
27-
6+
import datetime
7+
from oqd_dataschema import GroupBase, Attrs
288

29-
```python
30-
raw.camera_images.data = np.random.uniform(size=(3, 2, 2)).astype("float32")
31-
pprint(raw)
9+
class CustomGroup(GroupBase):
10+
attrs: Attrs = Field(
11+
default_factory=lambda: dict(
12+
timestamp=str(datetime.datetime.now(datetime.timezone.utc))
13+
)
14+
)
15+
t: Dataset
16+
x: Dataset
3217
```
3318

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

3621
```python
37-
raw.camera_images.data = np.random.uniform(size=(3, 2, 2)).astype("float32")
38-
```
22+
from oqd_dataschema import GroupRegistry
3923

40-
41-
42-
```python
43-
data = Datastore(groups={"raw": raw})
44-
pprint(data)
24+
print(GroupRegistry.groups)
4525
```
4626

47-
48-
27+
## Initialize Group
4928

5029
```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
58-
59-
60-
processed = process_raw(data.groups["raw"])
61-
pprint(processed)
62-
```
63-
30+
import numpy as np
6431

32+
t = np.linspace(0, 1, 101).astype(np.float32)
33+
x = np.sin(t).astype(np.complex64)
6534

35+
group = CustomGroup(
36+
t=Dataset(dtype="float32", shape=(101,)), x=Dataset(dtype="complex64", shape=(101,))
37+
)
6638

67-
```python
68-
data.groups.update(processed=processed)
69-
pprint(data)
39+
group.t.data = t
40+
group.x.data = x
7041
```
7142

72-
73-
43+
## Initialize Datastore
7444

7545
```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
88-
89-
90-
expval = process_outcomes(processed)
91-
data.groups.update(expval=process_outcomes(data.groups["processed"]))
46+
from oqd_datastore import Datastore
9247

93-
pprint(expval)
48+
datastore = Datastore(groups={"g1": group})
9449
```
9550

96-
51+
## Data pipeline
9752

9853
```python
99-
filepath = pathlib.Path("test.h5")
100-
data.model_dump_hdf5(filepath)
101-
```
54+
def process(datastore) -> Datastore:
55+
_g = datastore.get("g1")
10256

57+
datastore.add
10358

59+
return datastore
10460

105-
```python
106-
data_reload = Datastore.model_validate_hdf5(filepath)
107-
pprint(data_reload)
61+
62+
datastore.pipe(process)
10863
```

0 commit comments

Comments
 (0)