Skip to content

Commit 20b31c6

Browse files
committed
[docs] updated tutorials
1 parent 916e71e commit 20b31c6

File tree

3 files changed

+146
-1
lines changed

3 files changed

+146
-1
lines changed

docs/tutorials/advanced.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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", "x": "complex128"},
42+
data=unstructured_to_structured(
43+
np.stack([np.linspace(0, 1, 101), np.sin(np.linspace(0, 1, 101))], -1),
44+
dtype=np.dtype([("t", np.float32), ("x", np.complex128)]),
45+
),
46+
)
47+
48+
49+
group = CustomGroup(dset=dset, tbl=tbl, fld=fld)
50+
```
51+
52+
## Initialize Datastore
53+
54+
```python
55+
from oqd_datastore import Datastore
56+
57+
datastore = Datastore(groups={"g1": group})
58+
```
59+
60+
## Save Datastore
61+
62+
```python
63+
datastore.model_dump_hdf5(pathlib.Path("datastore.h5"), mode="w")
64+
```
65+
66+
## Load Datastore
67+
68+
```python
69+
reloaded_datastore = Datastore.model_validate_hdf5(pathlib.Path("datastore.h5"))
70+
```

docs/tutorials/basic.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
t: Dataset
15+
x: Dataset
16+
```
17+
18+
Defined groups are automatically registered into the [`GroupRegistry`][oqd_dataschema.group.GroupRegistry].
19+
20+
```python
21+
from oqd_dataschema import GroupRegistry
22+
23+
GroupRegistry.groups
24+
```
25+
26+
## Initialize Group
27+
28+
```python
29+
t = np.linspace(0, 1, 101).astype(np.float32)
30+
x = np.sin(t).astype(np.complex64)
31+
32+
group = CustomGroup(
33+
t=Dataset(dtype="float32", shape=(101,)), x=Dataset(dtype="complex64", shape=(101,))
34+
)
35+
36+
group.t.data = t
37+
group.x.data = x
38+
```
39+
40+
## Initialize Datastore
41+
42+
```python
43+
from oqd_datastore import Datastore
44+
45+
datastore = Datastore(groups={"g1": group})
46+
```
47+
48+
## Data pipeline
49+
50+
```python
51+
def process(datastore) -> Datastore:
52+
_g = datastore.get("g1")
53+
54+
g2 = CustomGroup(t=Dataset(data=_g.t.data), x=Dataset(data=_g.x.data + 1j))
55+
g2.attrs["_gen_by_pipe"] = "process"
56+
57+
datastore.add(g2=g2)
58+
59+
60+
datastore.pipe(process)
61+
```
62+
63+
## Save Datastore
64+
65+
```python
66+
datastore.model_dump_hdf5(pathlib.Path("datastore.h5"), mode="w")
67+
```
68+
69+
## Load Datastore
70+
71+
```python
72+
reloaded_datastore = Datastore.model_validate_hdf5(pathlib.Path("datastore.h5"))
73+
```

mkdocs.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ extra:
1818

1919
nav:
2020
- Get Started: index.md
21-
- Tutorial: tutorial.md
21+
- Tutorials:
22+
- Basics: tutorials/basic.md
23+
- Datasets/Tables/Folders: tutorials/advanced.md
2224
- Explanation: explanation.md
2325
- API Reference:
2426
- Base: api/base.md

0 commit comments

Comments
 (0)