Skip to content

Commit 4ae641f

Browse files
committed
individual metadata
1 parent 32a9b1f commit 4ae641f

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

python/test_bincode_metadata.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ def setup_ts_without_schema():
88
return ts
99

1010

11-
# def test_individual_metadata():
12-
# # NOTE: the assertions here rely on knowing
13-
# # what examples/json_metadata.rs put into the
14-
# # metadata!
15-
# ts = setup_ts_without_schema()
16-
# md = ts.individual(0).metadata
17-
# assert md["name"] == "Jerome"
18-
# assert md["phenotypes"] == [0, 1, 2, 0]
11+
def test_individual_metadata():
12+
# NOTE: the assertions here rely on knowing
13+
# what examples/json_metadata.rs put into the
14+
# metadata!
15+
ts = setup_ts_without_schema()
16+
md = tskit_glue.decode_bincode_individual_metadata(ts.individual(0).metadata)
17+
assert md.name() == "Jerome"
18+
assert md.phenotypes() == [0, 1, 2, 0]
1919

2020

2121
def test_mutation_metadata():

python/tskit_glue/src/lib.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use pyo3::prelude::*;
2+
use serde::Deserialize;
23

34
#[derive(serde::Serialize, serde::Deserialize)]
45
#[pyclass]
@@ -24,6 +25,16 @@ struct IndividualMetadata {
2425
phenotypes: Vec<i32>,
2526
}
2627

28+
#[pymethods]
29+
impl IndividualMetadata {
30+
fn name(&self) -> String {
31+
self.name.clone()
32+
}
33+
fn phenotypes(&self) -> Vec<i32> {
34+
self.phenotypes.clone()
35+
}
36+
}
37+
2738
/// Decode mutation metadata generated in rust via the `bincode` crate.
2839
#[pyfunction]
2940
fn decode_bincode_mutation_metadata(md: Vec<u8>) -> MutationMetadata {
@@ -33,9 +44,19 @@ fn decode_bincode_mutation_metadata(md: Vec<u8>) -> MutationMetadata {
3344
md
3445
}
3546

47+
/// Decode mutation metadata generated in rust via the `bincode` crate.
48+
#[pyfunction]
49+
fn decode_bincode_individual_metadata(md: Vec<u8>) -> IndividualMetadata {
50+
// NOTE: the unwrap here is not correct for production code
51+
// and a failure will crash the Python interpreter!
52+
let md = bincode::deserialize_from(md.as_slice()).unwrap();
53+
md
54+
}
55+
3656
/// A Python module implemented in Rust.
3757
#[pymodule]
3858
fn tskit_glue(m: &Bound<'_, PyModule>) -> PyResult<()> {
3959
m.add_function(wrap_pyfunction!(decode_bincode_mutation_metadata, m)?)?;
60+
m.add_function(wrap_pyfunction!(decode_bincode_individual_metadata, m)?)?;
4061
Ok(())
4162
}

0 commit comments

Comments
 (0)