How to use the tskit.pack_bytes function in tskit

To help you get started, we’ve selected a few tskit examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github tskit-dev / msprime / tskit_tests / test_tables.py View on Github external
def test_simple_string_case(self):
        strings = [b"hello", b"world"]
        packed, offset = tskit.pack_bytes(strings)
        self.assertEqual(list(offset), [0, 5, 10])
        self.assertEqual(packed.shape, (10,))
        returned = tskit.unpack_bytes(packed, offset)
        self.assertEqual(returned, strings)
github tskit-dev / msprime / tskit_tests / test_tables.py View on Github external
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
github molpopgen / fwdpy11 / fwdpy11 / _tables_to_tskit.py View on Github external
def _generate_mutation_metadata(pop):
    muts = []
    for mr in pop.tables.mutations:
        m = pop.mutations[mr.key]
        d = {'s': m.s,
             'h': m.h,
             'age': pop.generation - m.g + 1,
             'label': m.label,
             'esizes': list(m.esizes),
             'heffects': list(m.heffects),
             'neutral': m.neutral,
             'key': mr.key}
        muts.append(str(d).encode('utf-8'))
    return tskit.pack_bytes(muts)
github molpopgen / fwdpy11 / fwdpy11 / _tables_to_tskit.py View on Github external
individal_nodes[2*i] = i
        individal_nodes[2*i+1] = i
    metadata_strings = _generate_individual_metadata(pop.diploid_metadata, tc)

    # Now, preserved nodes
    num_ind_nodes = pop.N
    for i in pop.ancient_sample_metadata:
        assert i not in individal_nodes, "indivudal record error"
        individal_nodes[i.nodes[0]] = num_ind_nodes
        individal_nodes[i.nodes[1]] = num_ind_nodes
        num_ind_nodes += 1

    metadata_strings.extend(_generate_individual_metadata(
        pop.ancient_sample_metadata, tc))

    md, mdo = tskit.pack_bytes(metadata_strings)
    flags = [0 for i in range(pop.N+len(pop.ancient_sample_metadata))]
    tc.individuals.set_columns(flags=flags, metadata=md, metadata_offset=mdo)
    return individal_nodes
github molpopgen / fwdpy11 / fwdpy11 / _tables_to_tskit.py View on Github external
def _initializePopulationTable(node_view, tc):
    population_metadata = []
    for i in sorted(np.unique(node_view['population'])):
        md = "deme"+str(i)
        population_metadata.append(md.encode("utf-8"))

    pmd, pmdo = tskit.pack_bytes(population_metadata)
    tc.populations.set_columns(metadata=pmd, metadata_offset=pmdo)
github tskit-dev / pyslim / pyslim / slim_tree_sequence.py View on Github external
for j in samples:
        u = node_ind[j]
        assert(u >= 0)
        ploidy[u] += 1
        if tables.nodes.population[j] != ind_population[u]:
            raise ValueError("Inconsistent populations: nodes and individuals do not agree.")

    if any([p != 2 for p in ploidy]):
        raise ValueError("Not all individuals have two assigned nodes.")

    tables.nodes.set_columns(flags=tables.nodes.flags, time=tables.nodes.time,
                             population=tables.nodes.population, individual=node_ind,
                             metadata=tables.nodes.metadata,
                             metadata_offset=tables.nodes.metadata_offset)

    loc_vec, loc_off = tskit.pack_bytes(location)
    tables.individuals.set_columns(
            flags=ind_flags, location=loc_vec, location_offset=loc_off)

    individual_metadata = [IndividualMetadata(*x) for x in
                           zip(ind_id, age, ind_population, ind_sex, slim_ind_flags)]
    node_metadata = [None for _ in range(num_nodes)]
    for j in samples:
        node_metadata[j] = NodeMetadata(slim_id=node_id[j], is_null=node_is_null[j],
                                        genome_type=node_type[j])

    annotate_individual_metadata(tables, individual_metadata)
    annotate_node_metadata(tables, node_metadata)