File tree Expand file tree Collapse file tree 2 files changed +29
-8
lines changed
Expand file tree Collapse file tree 2 files changed +29
-8
lines changed Original file line number Diff line number Diff 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
2121def test_mutation_metadata ():
Original file line number Diff line number Diff line change 11use 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]
2940fn 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]
3858fn 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}
You can’t perform that action at this time.
0 commit comments