Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_annotate_individuals(self):
for ts in self.get_slim_examples():
tables = ts.tables
new_tables = ts.tables
metadata = []
for md in tskit.unpack_bytes(tables.individuals.metadata,
tables.individuals.metadata_offset):
dm = pyslim.decode_individual(md)
edm = pyslim.encode_individual(dm)
self.assertEqual(md, edm)
metadata.append(dm)
pyslim.annotate_individual_metadata(new_tables, metadata)
self.assertEqual(tables, new_tables)
def test_annotate_populations(self):
for ts in self.get_slim_examples():
tables = ts.tables
new_tables = ts.tables
metadata = []
for md in tskit.unpack_bytes(tables.populations.metadata,
tables.populations.metadata_offset):
dm = pyslim.decode_population(md)
edm = pyslim.encode_population(dm)
self.assertEqual(md, edm)
metadata.append(dm)
pyslim.annotate_population_metadata(new_tables, metadata)
self.assertEqual(tables, new_tables)
dumped_ts.tables.edges.left.sum())
self.assertEqual(eview['right'].sum(),
dumped_ts.tables.edges.right.sum())
tv = fwdpy11.TreeIterator(self.pop.tables,
[i for i in range(2*self.pop.N)])
tt_fwd = 0
for t in tv:
tt_fwd += t.total_time(self.pop.tables.nodes)
tt_tskit = 0
for t in dumped_ts.trees():
tt_tskit += t.get_total_branch_length()
self.assertEqual(tt_fwd, tt_tskit)
# Now, we make sure that the metadata can
# be decoded
md = tskit.unpack_bytes(dumped_ts.tables.individuals.metadata,
dumped_ts.tables.individuals.metadata_offset)
for i, j in zip(self.pop.diploid_metadata, md):
d = eval(j)
self.assertEqual(i.g, d['g'])
self.assertEqual(i.w, d['w'])
self.assertEqual(i.e, d['e'])
self.assertEqual(i.label, d['label'])
self.assertEqual(i.parents, d['parents'])
self.assertEqual(i.sex, d['sex'])
self.assertEqual(i.deme, d['deme'])
self.assertEqual(i.geography, d['geography'])
# Test that we can go backwards from node table to individuals
samples = np.where(dumped_ts.tables.nodes.flags ==
tskit.NODE_IS_SAMPLE)[0]
self.assertEqual(len(samples), 2*self.pop.N)
tskit.NODE_IS_SAMPLE)[0]
self.assertEqual(len(samples), 2*self.pop.N)
for i in samples[::2]:
ind = i//2
d = eval(md[ind])
fwdpy11_md = self.pop.diploid_metadata[ind]
self.assertEqual(fwdpy11_md.g, d['g'])
self.assertEqual(fwdpy11_md.w, d['w'])
self.assertEqual(fwdpy11_md.e, d['e'])
self.assertEqual(fwdpy11_md.label, d['label'])
self.assertEqual(fwdpy11_md.parents, d['parents'])
self.assertEqual(fwdpy11_md.sex, d['sex'])
self.assertEqual(fwdpy11_md.deme, d['deme'])
self.assertEqual(fwdpy11_md.geography, d['geography'])
md = tskit.unpack_bytes(dumped_ts.tables.mutations.metadata,
dumped_ts.tables.mutations.metadata_offset)
for i, j, k in zip(self.pop.tables.mutations,
dumped_ts.tables.mutations.site,
md):
d = eval(k)
self.assertEqual(i.key, d['key'])
site = dumped_ts.tables.sites[j]
m = self.pop.mutations[d['key']]
self.assertEqual(site.position, m.pos)
self.assertEqual(d['s'], m.s)
self.assertEqual(d['h'], m.h)
self.assertTrue(np.array_equal(np.array(d['esizes']), m.esizes))
self.assertTrue(np.array_equal(
np.array(d['heffects']), m.heffects))
self.assertEqual(d['label'], m.label)
self.assertEqual(d['neutral'], m.neutral)
def verify_packing(self, data):
packed, offset = tskit.pack_bytes(data)
self.assertEqual(packed.dtype, np.int8)
self.assertEqual(offset.dtype, np.uint32)
self.assertEqual(packed.shape[0], offset[-1])
returned = tskit.unpack_bytes(packed, offset)
self.assertEqual(data, returned)
return returned
def _set_populations(
tables, pop_id=None, selfing_fraction=0.0, female_cloning_fraction=0.0,
male_cloning_fraction=0.0, sex_ratio=0.5, bounds_x0=0.0, bounds_x1=0.0,
bounds_y0=0.0, bounds_y1=0.0, bounds_z0=0.0, bounds_z1=0.0,
migration_records=None):
'''
Adds to a TableCollection the information about populations required for SLiM
to load a tree sequence. This will replace anything already in the Population
table.
'''
num_pops = max(tables.nodes.population) + 1
for md in tskit.unpack_bytes(tables.individuals.metadata,
tables.individuals.metadata_offset):
try:
ind_md = decode_individual(md)
except:
raise ValueError("Individuals do not have metadata: "
"need to run set_nodes_individuals() first?")
assert(ind_md.population < num_pops)
if pop_id is None:
pop_id = list(range(num_pops))
assert(len(pop_id) == num_pops)
if type(selfing_fraction) is float:
selfing_fraction = [selfing_fraction for _ in range(num_pops)]
assert(len(selfing_fraction) == num_pops)
if type(female_cloning_fraction) is float:
def extract_population_metadata(tables):
'''
Returns an iterator over lists of :class:`PopulationMetadata` objects
containing information about the populations in the tables.
:param TableCollection tables: The tables, as produced by SLiM.
'''
metadata = tskit.unpack_bytes(tables.populations.metadata,
tables.populations.metadata_offset)
for md in metadata:
yield decode_population(md)
def extract_node_metadata(tables):
'''
Returns an iterator over lists of :class: `NodeMetadata` objects containing
information about the nodes in the tables.
:param TableCollection tables: The tables, as produced by SLiM.
'''
metadata = tskit.unpack_bytes(tables.nodes.metadata,
tables.nodes.metadata_offset)
for md in metadata:
yield decode_node(md)