From 8b1be4f6f324bc4c31e4a39632c20291b7f82998 Mon Sep 17 00:00:00 2001 From: Yan Wong Date: Thu, 22 Feb 2024 14:53:55 +0000 Subject: [PATCH] Add note about creating a reverse node map --- python/tskit/tables.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/python/tskit/tables.py b/python/tskit/tables.py index bedeaff4fa..f3184884e9 100644 --- a/python/tskit/tables.py +++ b/python/tskit/tables.py @@ -1,7 +1,7 @@ # # MIT License # -# Copyright (c) 2018-2023 Tskit Developers +# Copyright (c) 2018-2024 Tskit Developers # Copyright (c) 2017 University of Oxford # # Permission is hereby granted, free of charge, to any person obtaining a copy @@ -3368,7 +3368,7 @@ def simplify( function and ``u`` is the ID of a node in the input table, then ``a[u]`` is the ID of this node in the output table. For any node ``u`` that is not mapped into the output tables, this mapping will equal - ``-1``. + ``tskit.NULL`` (``-1``). Tables operated on by this function must: be sorted (see :meth:`TableCollection.sort`), have children be born strictly after their @@ -3377,6 +3377,18 @@ def simplify( requirements to specify a valid tree sequence (but the resulting tables will). + .. note:: + To invert the returned ``node_map``, that is, to obtain a reverse + mapping from the node ID in the output table to the node ID in + the input table, you can use:: + + rev_map = np.zeros_like(node_map, shape=simplified_ts.num_nodes) + kept = node_map != tskit.NULL + rev_map[node_map[kept]] = np.arange(len(node_map))[kept] + + In this case, no elements of the ``rev_map`` array will be set to + ``tskit.NULL``. + .. seealso:: This is identical to :meth:`TreeSequence.simplify` but acts *in place* to alter the data in this :class:`TableCollection`. Please see the