kitchensjn / tskit_arg_visualizer

Interactive visualization method for ancestral recombination graphs
MIT License
11 stars 3 forks source link

Node labels use index rather than ID #52

Closed hyanwong closed 11 months ago

hyanwong commented 11 months ago

For an ARG with nodes marked as msprime.NODE_IS_RE_EVENT, the node IDs are not the same as the index into the nodes list. When you call d3arg.set_node_labels({id1: "lab1", id2: "lab2"}), rather than using the node IDs, the key seems to be an index into the list of nodes. For instance, in the d3arg below, the node with id 20 is actually number 16 in the list:

>>> for node in d3arg.nodes:
>>>    print(node['id'], node['label'])
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7/8
9 9/10
11 11/12
13 13
14 14
15 15
16 16
17 17/18
19 19
20 20

>>> d3arg.set_node_labels({20:""})
IndexError: list index out of range

>>> print(d3arg.nodes[16])
{'id': 20,
 'flag': 0,
 'time': 12.0,
 'time_01': 0.0,
 'logtime_01': -0.0322115551827129,
 'rank_01': 0.0,
 'child_of': [],
 'parent_of': [16, 19],
 'label': 20}
kitchensjn commented 11 months ago

Ah yeah, that function is definitely not working how I intended. Thank you for noticing that. It should be fixed with the latest commit to main. The new version will now update the label of any node in the D3ARG with "id" 20 to "". Technically, "id" should always be a unique field so only one node should be updated (though there aren't any checks implemented for this at the moment). If you pass in an "id" that doesn't exist in the D3ARG, the function ignores it and continues with the other nodes. Lastly, node labels are now consistently strings, rather than a mix of strings and ints.