Skip to content

Commit 3bfc340

Browse files
committed
let's see what happens...
1 parent 0be339b commit 3bfc340

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

python/test_bincode_metadata.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import tskit
2+
import tskit_glue
3+
import numpy as np
4+
5+
6+
def setup_ts_without_schema():
7+
ts = tskit.TreeSequence.load("with_json_metadata.trees")
8+
return ts
9+
10+
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]
19+
20+
21+
def test_mutation_metadata():
22+
# NOTE: the assertions here rely on knowing
23+
# what examples/json_metadata.rs put into the
24+
# metadata!
25+
ts = setup_ts_without_schema()
26+
md = tskit_glue.decode_bincode_mutation_metadata(ts.mutation(0).metadata)
27+
assert np.isclose(md.effect_size(), -1e-3)
28+
assert np.isclose(md.dominance(), 0.1)

python/tskit_glue/src/lib.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,35 @@ struct MutationMetadata {
77
dominance: f64,
88
}
99

10+
#[pymethods]
11+
impl MutationMetadata {
12+
fn effect_size(&self) -> f64 {
13+
self.effect_size
14+
}
15+
fn dominance(&self) -> f64 {
16+
self.dominance
17+
}
18+
}
19+
1020
#[derive(serde::Serialize, serde::Deserialize)]
1121
#[pyclass]
1222
struct IndividualMetadata {
1323
name: String,
1424
phenotypes: Vec<i32>,
1525
}
1626

17-
/// Formats the sum of two numbers as string.
27+
/// Decode mutation metadata generated in rust via the `bincode` crate.
1828
#[pyfunction]
19-
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
20-
Ok((a + b).to_string())
29+
fn decode_bincode_mutation_metadata(md: Vec<u8>) -> MutationMetadata {
30+
// NOTE: the unwrap here is not correct for production code
31+
// and a failure will crash the Python interpreter!
32+
let md = bincode::deserialize_from(md.as_slice()).unwrap();
33+
md
2134
}
2235

2336
/// A Python module implemented in Rust.
2437
#[pymodule]
2538
fn tskit_glue(m: &Bound<'_, PyModule>) -> PyResult<()> {
26-
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
39+
m.add_function(wrap_pyfunction!(decode_bincode_mutation_metadata, m)?)?;
2740
Ok(())
2841
}

0 commit comments

Comments
 (0)