Exemplo n.º 1
0
 def test_set_tree_sequence_metadata_keeps(self):
     # make sure doesn't overwrite other stuff
     dummy_schema = tskit.MetadataSchema({
         'codec': 'json',
         'type': 'object',
         'properties': {
             'abc': {
                 'type': 'string'
             }
         }
     })
     dummy_metadata = {'abc': 'foo'}
     for ts in self.get_slim_examples():
         tables = ts.tables
         tables.metadata_schema = dummy_schema
         tables.metadata = dummy_metadata
         pyslim.set_tree_sequence_metadata(tables, "nonWF", 0)
         schema = tables.metadata_schema.schema
         for k in dummy_metadata:
             self.assertTrue(k in schema['properties'])
             self.assertTrue(k in tables.metadata)
             self.assertEqual(tables.metadata[k], dummy_metadata[k])
         self.validate_slim_metadata(tables)
         self.assertEqual(tables.metadata['SLiM']['model_type'], "nonWF")
         self.assertEqual(tables.metadata['SLiM']['generation'], 0)
         break
Exemplo n.º 2
0
 def test_set_tree_sequence_metadata_errors(self):
     for ts in self.get_slim_examples():
         tables = ts.tables
         tables.metadata_schema = tskit.MetadataSchema(None)
         self.assertGreater(len(tables.metadata), 0)
         with self.assertRaises(ValueError):
             pyslim.set_tree_sequence_metadata(tables, "nonWF", 0)
         break
Exemplo n.º 3
0
 def test_set_tree_sequence_metadata(self, recipe):
     tables = recipe["ts"].dump_tables()
     pyslim.set_tree_sequence_metadata(tables,
                                       "WF",
                                       99,
                                       spatial_dimensionality='xy',
                                       spatial_periodicity='y',
                                       separate_sexes=False,
                                       nucleotide_based=True)
     self.validate_slim_metadata(tables)
     assert tables.metadata['SLiM']['model_type'] == "WF"
     assert tables.metadata['SLiM']['generation'] == 99
     assert tables.metadata['SLiM']['spatial_dimensionality'] == 'xy'
     assert tables.metadata['SLiM']['spatial_periodicity'] == 'y'
     assert tables.metadata['SLiM']['separate_sexes'] == False
     assert tables.metadata['SLiM']['nucleotide_based'] == True
Exemplo n.º 4
0
 def test_set_tree_sequence_metadata(self):
     for ts in self.get_slim_examples():
         tables = ts.tables
         pyslim.set_tree_sequence_metadata(
                 tables, "WF", 99, 
                 spatial_dimensionality='xy',
                 spatial_periodicity='y',
                 separate_sexes=False,
                 nucleotide_based=True)
         self.validate_slim_metadata(tables)
         self.assertEqual(tables.metadata['SLiM']['model_type'], "WF")
         self.assertEqual(tables.metadata['SLiM']['generation'], 99)
         self.assertEqual(tables.metadata['SLiM']['spatial_dimensionality'], 'xy')
         self.assertEqual(tables.metadata['SLiM']['spatial_periodicity'], 'y')
         self.assertEqual(tables.metadata['SLiM']['separate_sexes'], False)
         self.assertEqual(tables.metadata['SLiM']['nucleotide_based'], True)
         break
Exemplo n.º 5
0
 def test_set_tree_sequence_metadata_keeps(self, recipe):
     # make sure doesn't overwrite other stuff
     for x in [{}, {'properties': {'abc': {'type': 'string'}}}]:
         schema_dict = {
             'codec': 'json',
             'type': 'object',
         }
         schema_dict.update(x)
         dummy_schema = tskit.MetadataSchema(schema_dict)
         dummy_metadata = {'abc': 'foo'}
         tables = recipe["ts"].dump_tables()
         tables.metadata_schema = dummy_schema
         tables.metadata = dummy_metadata
         pyslim.set_tree_sequence_metadata(tables, "nonWF", 0)
         schema = tables.metadata_schema.schema
         for k in dummy_metadata:
             if len(x) > 0:
                 assert k in schema['properties']
             assert k in tables.metadata
             assert tables.metadata[k] == dummy_metadata[k]
         self.validate_slim_metadata(tables)
         assert tables.metadata['SLiM']['model_type'] == "nonWF"
         assert tables.metadata['SLiM']['generation'] == 0
Exemplo n.º 6
0
 def test_set_tree_sequence_metadata_errors(self, recipe):
     tables = recipe["ts"].dump_tables()
     tables.metadata_schema = tskit.MetadataSchema(None)
     assert len(tables.metadata) > 0
     with pytest.raises(ValueError):
         pyslim.set_tree_sequence_metadata(tables, "nonWF", 0)
Exemplo n.º 7
0
    def test_known_answer(self):
        # a simple example to make sure we've got the edge cases right
        tables = tskit.TableCollection(sequence_length=1)
        pyslim.set_tree_sequence_metadata(tables,
                                          model_type='nonWF',
                                          generation=0)
        pyslim.set_metadata_schemas(tables)
        locs = [
            [0, 0],  # alive at 0, 1
            [0, 1],  # alive at 0, 1, 2
            [2, 0],  # alive at 0, 1, 2
            [1, 1],  # alive at 0, 1, 2
            [0, 0],  # alive at 1
            [0.5, 1],  # alive at 1
            [2, 2],  # alive at 1
            [3, 2]  # alive at 0, 1, 2, 3
        ]
        births = [1, 2, 2, 2, 1, 1, 1, 3]
        ages = [1, 2, 2, 2, 0, 0, 0, 3]
        x_bins = [0, 1, 3]
        for xy, a in zip(locs, ages):
            md = pyslim.default_slim_metadata('individual')
            md['age'] = a
            tables.individuals.add_row(
                location=xy + [np.nan],
                metadata=md,
            )

        for j, b in enumerate(births):
            tables.nodes.add_row(time=b, individual=j)

        ts = pyslim.SlimTreeSequence(tables.tree_sequence())

        # check we've got this right
        for k, n in enumerate([[0, 1, 2, 3, 7], [0, 1, 2, 3, 4, 5, 6, 7],
                               [1, 2, 3, 7], [7]]):
            np.testing.assert_array_equal(n, ts.individuals_alive_at(k))

        # no-one
        counts = pyslim.population_size(
            ts,
            x_bins=np.arange(10),
            y_bins=np.arange(10),
            time_bins=[100, 200, 300],
        )
        np.testing.assert_array_equal(
            counts,
            np.zeros((9, 9, 2)),
        )

        # everyone at the start
        counts = pyslim.population_size(
            ts,
            x_bins=[0, 10],
            y_bins=[0, 10],
            time_bins=[0, 1],
        )
        np.testing.assert_array_equal(counts, [[[5]]])

        # should omit the last one
        counts = pyslim.population_size(ts,
                                        x_bins=[0, 3],
                                        y_bins=[0, 3],
                                        time_bins=[0, 1])
        np.testing.assert_array_equal(counts, [[[4]]])

        # now should omit the ones at the boundaries
        counts = pyslim.population_size(ts,
                                        x_bins=[0, 1, 2],
                                        y_bins=[0, 1, 2],
                                        time_bins=[0, 1, 2, 5])
        np.testing.assert_array_equal(
            counts, [[[1, 2, 0], [1, 2, 1 / 3]], [[0, 0, 0], [1, 1, 1 / 3]]])