You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Methods for reordering nodes and individuals #2060
Python APIIssue is about the Python APIenhancementNew feature or requestfutureIssues that are closed as they are not planned in the medium-term, but which are still desirable.
I would like to be able to reorder the individuals so that the IDs change but the structure is all the same. I would similarly like to be able to do this for nodes. I don't think there are built-in functions to do this yet, although TreeSequence.subset will do it as a side effect for nodes, if given an ordering that lists all the node IDs.
Ok - so, what exactly do you need to do? subset( ) will let you put the nodes in arbitrary order, and then orders individuals by first occurrence of node, IIRC.
So I'm assuming this would be a method on TableCollection that would take an array for each table that specifies the new order (or None for no change)? Sounds do-able. I'd like to understand a little more about the context you're doing this in though?
Actually it turns out that I don't need it in this instance. But there have been times in the past when I have wanted to do this.
Here, I wanted it to be able to order all individuals at a given time (in generations) so that when visualising their pedigree, the lines connecting them had crossings minimised. I had the order within each generation, but it was fiddly re-sorting the indexes in the right way, because I would have to remap the individuals.parents fields, as well as the nodes.individual field. I could imagine easily forgetting which references I needed to change.
Previously I've wanted, for example, to put all the sample nodes as the first n nodes, even when some are historical samples (and so there might be younger internal nodes). But that's just off the top of my head.
Hi @benjeffery - this seems like it would be useful in some tsinfer improvements that @szhan might make (see tskit-dev/tsinfer#189 (comment) for context). I think this might be a simple enough addition to tskit for him to try making a PR about it. Does that seem sensible? Are there any misgivings about adding the functionality to tskit?
I guess at a minimum we would want TableCollection.reorder_tables(nodes=X, individuals=Y, populations=Z), since those are the only tables which are allowed to have an arbitrary order. I'm not sure about migrations?
The ability to reorder the other tables edges, sites, etc to form a non-valid table collection is a bit esoteric, so not sure we want to bother with that.
How will this interact with subset? For instance, if nodes=X would X be required to have all of the nodes? One way forward would be to add individual_order=Y and population_order=Z arguments to subset( ) (and then possibly rename it reorder( ) since maybe that's a better name)?
The ability to reorder the other tables edges, sites, etc to form a non-valid table collection is a bit esoteric, so not sure we want to bother with that.
Are there any misgivings about adding the functionality to tskit?
I don't think so - is this a perf sensitive application? We normally add these things in at the python layer, at least initially and only push down to C if needed. @szhan should go ahead and PR something for sure, especially if it isn't much work to get to a prototype that we can concretely discuss.
No, I don't think this is perf-sensitive. It might be done for a large tree sequence, but it is only done once in the entire pipeline, so it will be a minuscule part of the whole. I think implementing in python is the way forward.
However, I guess we want to decide on whether (as @petrelharp says) we want to roll this into the "subset" method?
Acts in place, and could potentially return a tuple of 3 numpy arrays mapping old->new indexes? That could be a namedtuple, I guess, if we want to be sophisticated:
Here's some demo code showing the logic, @szhan. I think I have it right, but you should check that the mapping is the right way around. Now edited - you do need to create an inverted map
importnumpyasnpimportmsprimets=msprime.sim_ancestry(10, sequence_length=100, recombination_rate=0.1, random_seed=1)
ts=msprime.sim_mutations(ts, rate=0.1, random_seed=1)
defreorder_nodes(tables, node_map):
assertlen(np.unique(node_map)) ==len(node_map) ==tables.nodes.num_rowsassertnp.min(node_map) ==0assertnp.max(node_map) ==tables.nodes.num_rows-1inverted_map=np.zeros(tables.nodes.num_rows, dtype=tables.edges.parent.dtype)
inverted_map[node_map] =np.arange(tables.nodes.num_rows)
tables.nodes.replace_with(tables.nodes[node_map])
tables.edges.parent=inverted_map[tables.edges.parent]
tables.edges.child=inverted_map[tables.edges.child]
tables.mutations.node=inverted_map[tables.mutations.node]
tables.sort()
random_node_order=np.arange(ts.num_nodes)
np.random.seed(123) # make this example replicablenp.random.shuffle(random_node_order)
table_collection=ts.dump_tables()
reorder_nodes(table_collection, random_node_order)
new_ts=table_collection.tree_sequence()
# check the diversity is the sameprint(ts.diversity(), new_ts.diversity())
# more intensive: check that the (unlabelled) topologies are the sameforinterval, tree1, tree2ints.coiterate(new_ts):
asserttree1.rank()[0] ==tree2.rank()[0] # could use rank().shape once https://github.com/tskit-dev/tskit/pull/2392 is merged
Python APIIssue is about the Python APIenhancementNew feature or requestfutureIssues that are closed as they are not planned in the medium-term, but which are still desirable.
Activity
petrelharp commentedon Dec 11, 2021
Ok - so, what exactly do you need to do?
subset( )will let you put the nodes in arbitrary order, and then orders individuals by first occurrence of node, IIRC.benjeffery commentedon Dec 16, 2021
So I'm assuming this would be a method on
TableCollectionthat would take an array for each table that specifies the new order (orNonefor no change)? Sounds do-able. I'd like to understand a little more about the context you're doing this in though?hyanwong commentedon Dec 16, 2021
Actually it turns out that I don't need it in this instance. But there have been times in the past when I have wanted to do this.
Here, I wanted it to be able to order all individuals at a given time (in generations) so that when visualising their pedigree, the lines connecting them had crossings minimised. I had the order within each generation, but it was fiddly re-sorting the indexes in the right way, because I would have to remap the individuals.parents fields, as well as the nodes.individual field. I could imagine easily forgetting which references I needed to change.
Previously I've wanted, for example, to put all the sample nodes as the first n nodes, even when some are historical samples (and so there might be younger internal nodes). But that's just off the top of my head.
hyanwong commentedon Feb 10, 2022
Hi @benjeffery - this seems like it would be useful in some tsinfer improvements that @szhan might make (see tskit-dev/tsinfer#189 (comment) for context). I think this might be a simple enough addition to tskit for him to try making a PR about it. Does that seem sensible? Are there any misgivings about adding the functionality to tskit?
hyanwong commentedon Feb 10, 2022
I guess at a minimum we would want
TableCollection.reorder_tables(nodes=X, individuals=Y, populations=Z), since those are the only tables which are allowed to have an arbitrary order. I'm not sure about migrations?The ability to reorder the other tables edges, sites, etc to form a non-valid table collection is a bit esoteric, so not sure we want to bother with that.
petrelharp commentedon Feb 10, 2022
How will this interact with
subset? For instance, ifnodes=XwouldXbe required to have all of the nodes? One way forward would be to addindividual_order=Yandpopulation_order=Zarguments tosubset( )(and then possibly rename itreorder( )since maybe that's a better name)?Definitely.
benjeffery commentedon Feb 11, 2022
I don't think so - is this a perf sensitive application? We normally add these things in at the python layer, at least initially and only push down to C if needed. @szhan should go ahead and PR something for sure, especially if it isn't much work to get to a prototype that we can concretely discuss.
hyanwong commentedon Feb 11, 2022
No, I don't think this is perf-sensitive. It might be done for a large tree sequence, but it is only done once in the entire pipeline, so it will be a minuscule part of the whole. I think implementing in python is the way forward.
However, I guess we want to decide on whether (as @petrelharp says) we want to roll this into the "subset" method?
benjeffery commentedon Feb 11, 2022
I'd rather have a separate method so the two uses are clear, even if there is some small overlap in functionality.
hyanwong commentedon Feb 11, 2022
OK, If @petrelharp agrees, I think the method / API is well defined enough for @szhan to have a go at it:
Acts in place, and could potentially return a tuple of 3 numpy arrays mapping old->new indexes? That could be a namedtuple, I guess, if we want to be sophisticated:
jeromekelleher commentedon Feb 11, 2022
Need not even have tuple-like behaviour, could just be a simple dataclass. Possibly better, since the tuple-like semantics are often unhelpful.
petrelharp commentedon Feb 16, 2022
Soiunds good to me! However, it'd be nice to have a "see also" note in the docstring for both of them.
hyanwong commentedon Jul 7, 2022
Here's some demo code showing the logic, @szhan.
I think I have it right, but you should check that the mapping is the right way around.Now edited - you do need to create an inverted mapbenjeffery commentedon Jun 12, 2025
Closing for inactivity and labelling "future", please re-open if you plan to work on this.