Skip to content

Commit 2727e15

Browse files
authored
doc: update book on metadata + Python (#754)
1 parent 874e571 commit 2727e15

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

book/src/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* [Metadata](./metadata.md)
2020
- [Defining metadata types in rust](./metadata_derive.md)
2121
- [Metadata and tables](./metadata_tables.md)
22-
- [Metadata schema](./metadata_schema.md)
22+
- [Metadata processing with Python](./metadata_python.md)
2323
- [Advanced topics](./metadata_advanced.md)
2424

2525
* [Error handling](./error_handling.md)

book/src/metadata_python.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Metadata processing with Python
2+
3+
## `JSON` metadata
4+
5+
If your metadata are generated in `JSON` format via `serde` (see [here](metadata_derive.md)), then the metadata are simple to access from Python.
6+
The code repository for `tskit-rust` contains examples in the `python/` subdirectory.
7+
8+
You may work with `JSON` metadata with or without a metadata schema (see [here](https://tskit.dev/tskit/docs/stable/metadata.html)).
9+
A schema is useful for data validation but there is an unfortunate inefficiency if your input to Python is a tree sequence rather than a table collection.
10+
You will have to copy the tables, add the metadata schema, and regenerate a tree sequence.
11+
See the examples mentioned above.
12+
13+
## Other formats
14+
15+
The `tskit-python` API only supports `JSON` and Python's `struct` data formats.
16+
It is useful to use a format other than `JSON` in order to minimize storage requirements.
17+
However, doing so will require that you provide a method to covert the data into a valid Python object.
18+
19+
An easy way to provide conversion methods is to use [pyo3](https://pyo3.rs) to create a small Python module to deserialize your metadata into Python objects.
20+
The `tskit-rust` code repository contains an example of this in the `python/` subdirectory.
21+
The module is shown in its entirety below:
22+
23+
```rust, noplaygound, ignore
24+
{{#include ../../python/tskit_glue/src/lib.rs}}
25+
```
26+
27+
Using it in Python is just a matter of importing the module:
28+
29+
```python
30+
{{#include ../../python/test_bincode_metadata.py}}
31+
```

book/src/metadata_schema.md

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)