Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document numpy traversals #1952

Merged
merged 1 commit into from
Dec 1, 2021
Merged

Conversation

benjeffery
Copy link
Member

Fixes #1788

Added for follow up work tskit-dev/tutorials#150

@codecov
Copy link

codecov bot commented Nov 26, 2021

Codecov Report

Merging #1952 (7ff7a17) into main (8bb1b5d) will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #1952   +/-   ##
=======================================
  Coverage   93.32%   93.32%           
=======================================
  Files          27       27           
  Lines       25212    25212           
  Branches     1108     1108           
=======================================
  Hits        23530    23530           
  Misses       1648     1648           
  Partials       34       34           
Flag Coverage Δ
c-tests 92.32% <ø> (ø)
lwt-tests 89.14% <ø> (ø)
python-c-tests 71.68% <ø> (ø)
python-tests 98.80% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
python/tskit/trees.py 97.79% <ø> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8bb1b5d...7ff7a17. Read the comment docs.

@hyanwong
Copy link
Member

Yay, I was just about to do this @benjeffery but you beat me to it!

A couple of things:

  1. you might be able to save some repetition by nuking some of the text in the descriptions of orders in the Tree.nodes() docketing, and simply pointing to these new methods, e.g.
... The available orders are:

        - 'preorder': Standard preorder traversal, see :meth:`Tree.preorder`.
        - ...
  1. The docs don't say what we do if there are multiple roots in a tree. I think we do everything from one root, then everything from the next root, etc, but don't guarantee the order in which we iterate over the roots, right? This could also do with clarifying for e.g. inorder and levelorder in the Tree.nodes() docketing, but that's a different issue I guess.

python/tskit/trees.py Outdated Show resolved Hide resolved
Copy link
Member

@jeromekelleher jeromekelleher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, needs some tweaks as the semantics aren't quite right.

Also see :ref:`tutorials:sec_analysing_trees_traversals` for examples of how
to use traversals.

:param int u: The node to start from, defaults to the virtual root.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't quite right. If we start from the virtual root we return the virtual root in the arrays, but this isn't what we usually want. It's more like "if specified, return all nodes in the subtree rooted at u (including u) in traversal order".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

def preorder(self, u=NULL):
"""
Returns a numpy array of node ids. Starting at `u`, yield the current node,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't quite right I think. How about

Returns a number array of node ids in preorder <https://en.wikipedia.org/wiki/Tree_traversal#Pre-order_(NLR)>. If the node u the traversal is rooted at this node (and it will be the first element in the returned array). Otherwise, all nodes reachable from the tree roots will be returned. See :ref:tutorials:sec_analysing_trees_traversals for examples.

I don't think need to describe what "preorder" is, the wikipedia link is fine. I think talking about "yielding" here is more confusing, since this isn't a generator.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@benjeffery benjeffery force-pushed the doc_traversals branch 2 times, most recently from e6e24b7 to 2f60e98 Compare December 1, 2021 12:12
@benjeffery
Copy link
Member Author

Should be good to go now

<https://en.wikipedia.org/wiki/Tree_traversal##Post-order_(LRN)>. If the node u
is specified the traversal is rooted at this node (and it will be the first
element in the returned array). Otherwise, all nodes reachable from the tree
roots will be returned. See :ref:tutorials:sec_analysing_trees_traversals for
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need backticks:

:ref:`tutorials:sec_analysing_trees_traversals`

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

<https://en.wikipedia.org/wiki/Tree_traversal#Pre-order_(NLR)>. If the node u
is specified the traversal is rooted at this node (and it will be the first
element in the returned array). Otherwise, all nodes reachable from the tree
roots will be returned. See :ref:tutorials:sec_analysing_trees_traversals for
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need backticks:

:ref:`tutorials:sec_analysing_trees_traversals`

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it does

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Member

@hyanwong hyanwong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trivial nitpicks

"""
Returns a number array of node ids in postorder
<https://en.wikipedia.org/wiki/Tree_traversal##Post-order_(LRN)>. If the node u
is specified the traversal is rooted at this node (and it will be the first
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"last element"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@hyanwong
Copy link
Member

hyanwong commented Dec 1, 2021

Should be good to go now

Great. It might be good to add something about the order of traversal if there are multiple roots. Something like:

Returns a 1D numpy array of node ids in preorder
<https://en.wikipedia.org/wiki/Tree_traversal#Pre-order_(NLR)>. If the node u
is specified the traversal is rooted at this node (and it will be the first
element in the returned array). Otherwise, return an array in which the node
in preorder reachable from each tree root have been concatenated together.

but I can add that as a further PR if you like. It would be good to merge this, then I can push my doc PR properly.

python/tskit/trees.py Outdated Show resolved Hide resolved
python/tskit/trees.py Outdated Show resolved Hide resolved
Copy link
Member

@hyanwong hyanwong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More nitpicks, sorry!

@benjeffery
Copy link
Member Author

Thanks @hyanwong should be good now

@jeromekelleher
Copy link
Member

Needs another pass @benjeffery

@jeromekelleher
Copy link
Member

We should discuss the semantics of the virtual root too - but, possibly simpler to merge this once the cross references have been fixed so that @hyanwong can merge in his version?

@benjeffery
Copy link
Member Author

Fixed

@benjeffery benjeffery added the AUTOMERGE-REQUESTED Ask Mergify to merge this PR label Dec 1, 2021
@benjeffery
Copy link
Member Author

@Mergifyio rebase

@mergify
Copy link
Contributor

mergify bot commented Dec 1, 2021

rebase

✅ Branch has been successfully rebased

@benjeffery
Copy link
Member Author

Ok, hopefully that does it. FML.

@hyanwong
Copy link
Member

hyanwong commented Dec 1, 2021

Do you want me to merge stuff into this before it's merged into main, or simply make a follow-up PR? I've never done the former, but happy to try.

@jeromekelleher
Copy link
Member

Let's do a follow-up @hyanwong

@mergify mergify bot merged commit 1d3f6de into tskit-dev:main Dec 1, 2021
@mergify mergify bot removed the AUTOMERGE-REQUESTED Ask Mergify to merge this PR label Dec 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Document traversal arrays and illustrate usage
3 participants