|
| 1 | +import tskit |
| 2 | +import numpy as np |
| 3 | + |
| 4 | + |
| 5 | +def setup_ts_without_schema(): |
| 6 | + ts = tskit.TreeSequence.load("with_json_metadata.trees") |
| 7 | + return ts |
| 8 | + |
| 9 | + |
| 10 | +def setup_ts_with_schema(): |
| 11 | + ts = setup_ts_without_schema() |
| 12 | + tables = ts.tables |
| 13 | + tables.individuals.metadata_schema = tskit.metadata.MetadataSchema( |
| 14 | + { |
| 15 | + "codec": "json", |
| 16 | + "type": "object", |
| 17 | + "name": "Individual metadata", |
| 18 | + "properties": {"name": {"type": "string"}, |
| 19 | + "phenotypes": {"type": "array"}}, |
| 20 | + "additionalProperties": False, |
| 21 | + }) |
| 22 | + tables.mutations.metadata_schema = tskit.metadata.MetadataSchema( |
| 23 | + { |
| 24 | + "codec": "json", |
| 25 | + "type": "object", |
| 26 | + "name": "Individual metadata", |
| 27 | + "properties": {"effect_size": {"type": "number"}, |
| 28 | + "dominance": {"type": "number"}}, |
| 29 | + "additionalProperties": False, |
| 30 | + }) |
| 31 | + return tables.tree_sequence() |
| 32 | + |
| 33 | + |
| 34 | +def test_individual_metadata(): |
| 35 | + # NOTE: the assertions here rely on knowing |
| 36 | + # what examples/json_metadata.rs put into the |
| 37 | + # metadata! |
| 38 | + ts = setup_ts_with_schema() |
| 39 | + md = ts.individual(0).metadata |
| 40 | + assert md["name"] == "Jerome" |
| 41 | + assert md["phenotypes"] == [0, 1, 2, 0] |
| 42 | + |
| 43 | + |
| 44 | +def test_individual_metadata_without_schema(): |
| 45 | + # NOTE: the assertions here rely on knowing |
| 46 | + # what examples/json_metadata.rs put into the |
| 47 | + # metadata! |
| 48 | + ts = setup_ts_without_schema() |
| 49 | + md = eval(ts.individual(0).metadata) |
| 50 | + assert md["name"] == "Jerome" |
| 51 | + assert md["phenotypes"] == [0, 1, 2, 0] |
| 52 | + |
| 53 | + |
| 54 | +def test_mutation_metadata(): |
| 55 | + # NOTE: the assertions here rely on knowing |
| 56 | + # what examples/json_metadata.rs put into the |
| 57 | + # metadata! |
| 58 | + ts = setup_ts_with_schema() |
| 59 | + md = ts.mutation(0).metadata |
| 60 | + assert np.isclose(md["effect_size"], -1e-3) |
| 61 | + assert np.isclose(md["dominance"], 0.1) |
| 62 | + |
| 63 | + |
| 64 | +def test_mutation_metadata_without_schema(): |
| 65 | + # NOTE: the assertions here rely on knowing |
| 66 | + # what examples/json_metadata.rs put into the |
| 67 | + # metadata! |
| 68 | + ts = setup_ts_without_schema() |
| 69 | + md = eval(ts.mutation(0).metadata) |
| 70 | + assert np.isclose(md["effect_size"], -1e-3) |
| 71 | + assert np.isclose(md["dominance"], 0.1) |
0 commit comments