Skip to content

doc: update book on metadata + Python #754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* [Metadata](./metadata.md)
- [Defining metadata types in rust](./metadata_derive.md)
- [Metadata and tables](./metadata_tables.md)
- [Metadata schema](./metadata_schema.md)
- [Metadata processing with Python](./metadata_python.md)
- [Advanced topics](./metadata_advanced.md)

* [Error handling](./error_handling.md)
Expand Down
31 changes: 31 additions & 0 deletions book/src/metadata_python.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Metadata processing with Python

## `JSON` metadata

If your metadata are generated in `JSON` format via `serde` (see [here](metadata_derive.md)), then the metadata are simple to access from Python.
The code repository for `tskit-rust` contains examples in the `python/` subdirectory.

You may work with `JSON` metadata with or without a metadata schema (see [here](https://tskit.dev/tskit/docs/stable/metadata.html)).
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.
You will have to copy the tables, add the metadata schema, and regenerate a tree sequence.
See the examples mentioned above.

## Other formats

The `tskit-python` API only supports `JSON` and Python's `struct` data formats.
It is useful to use a format other than `JSON` in order to minimize storage requirements.
However, doing so will require that you provide a method to covert the data into a valid Python object.

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.
The `tskit-rust` code repository contains an example of this in the `python/` subdirectory.
The module is shown in its entirety below:

```rust, noplaygound, ignore
{{#include ../../python/tskit_glue/src/lib.rs}}
```

Using it in Python is just a matter of importing the module:

```python
{{#include ../../python/test_bincode_metadata.py}}
```
17 changes: 0 additions & 17 deletions book/src/metadata_schema.md

This file was deleted.

Loading