Skip to content
This repository was archived by the owner on Oct 24, 2024. It is now read-only.

Docs manipulating trees2 #265

Closed
Closed
Changes from 35 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
adcfa87
why hierarchical data
TomNicholas Jan 1, 2023
5490775
add hierarchical data page to index
TomNicholas Jan 1, 2023
be81f78
Simpsons family tree
TomNicholas Jan 1, 2023
e05fe6d
evolutionary tree
TomNicholas Jan 2, 2023
f9ae6fd
WIP rearrangement of creating trees
TomNicholas Jan 2, 2023
a9e3bb6
Merge branch 'main' into hierarchical_data_docs
TomNicholas Jan 4, 2023
f625b95
fixed examples in data structures page
TomNicholas Jan 4, 2023
c0ea814
dict-like navigation
TomNicholas Jan 4, 2023
0165312
filesystem-like paths explained
TomNicholas Jan 4, 2023
2de37ec
split PR into parts
TomNicholas Jan 4, 2023
15fa84a
plan
TomNicholas Jan 4, 2023
12d209c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 4, 2023
de6250a
Merge branch 'main' into docs_manipulating_trees
TomNicholas Jan 4, 2023
c8d1f39
Merge branch 'main' into docs_manipulating_trees
TomNicholas Jan 5, 2023
ebc5b75
fix ipython bug
TomNicholas Jan 5, 2023
2a4286f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 5, 2023
9eed6a4
Merge branch 'main' into docs_manipulating_trees
TomNicholas Jan 5, 2023
7605fe8
filter simpsons family tree by age
TomNicholas Jan 5, 2023
b62447f
Merge branch 'main' into docs_manipulating_trees
TomNicholas Jan 6, 2023
d4772e3
use new filter method
TomNicholas Jan 6, 2023
e633b81
test about filter
TomNicholas Jan 6, 2023
487df12
simple example of mapping over a subtree
TomNicholas Jan 6, 2023
c1bd68c
ideas for docs on iterating over trees
TomNicholas Jan 6, 2023
3fdd54f
Merge branch 'main' into docs_manipulating_trees
TomNicholas Jan 6, 2023
8b4f705
add section on iterating over subtree
TomNicholas Jan 6, 2023
dd388d3
Merge branch 'main' into docs_manipulating_trees
TomNicholas Jan 6, 2023
c020e50
Merge branch 'main' into docs_manipulating_trees
TomNicholas Oct 23, 2023
ed09fd8
Merge branch 'main' into docs_manipulating_trees
TomNicholas Oct 23, 2023
cf5c2c0
text to accompany Simpsons family aging example
TomNicholas Oct 23, 2023
23b9d21
add voltage dataset
TomNicholas Oct 23, 2023
997ed41
RMS as example of mapping custom computation
TomNicholas Oct 23, 2023
0dbe218
isomorphism
TomNicholas Oct 23, 2023
e2aa95f
P=IV example of binary multiplication
TomNicholas Oct 23, 2023
ce2b865
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
a2e9a9e
Merge branch 'main' into docs_manipulating_trees2
TomNicholas Jan 22, 2024
f4967ce
Correct greater than or equal to symbol
TomNicholas Jan 22, 2024
fb0c554
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 22, 2024
ffd180f
remove personal notes
TomNicholas Jan 22, 2024
02663ae
whatsnew
TomNicholas Jan 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 43 additions & 13 deletions docs/source/hierarchical-data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,22 @@ You can see this tree is similar to the ``dt`` object above, except that it is m
Manipulating Trees
------------------

Moving Tree Branches
~~~~~~~~~~~~~~~~~~~~

pruning, grafting

Tree of life?

Graft new discoveries onto the tree?

Prune when we realise something is in the wrong place?

Save our updated tree out with ``to_dict``

leaves are either currently living or died out with no descendants
Subset only the living leaves of the evolutionary tree?

Subsetting Tree Nodes
~~~~~~~~~~~~~~~~~~~~~

Expand All @@ -397,7 +413,8 @@ We can use :py:meth:`DataTree.match` for this:

We can also subset trees by the contents of the nodes.
:py:meth:`DataTree.filter` retains only the nodes of a tree that meet a certain condition.
For example, we could recreate the Simpson's family tree with the ages of each individual, then filter for only the adults:

For example, we could recreate the Simpson's family tree with the ages of each individual, then filter for only the adults.
First lets recreate the tree but with an `age` data variable in every node:

.. ipython:: python
Expand All @@ -419,12 +436,20 @@ Now let's filter out the minors:

.. ipython:: python

simpsons.filter(lambda node: node["age"] > 18)
simpsons.filter(lambda node: node["age"] => 18)

The result is a new tree, containing only the nodes matching the condition.

(Yes, under the hood :py:meth:`~DataTree.filter` is just syntactic sugar for the pattern we showed you in :ref:`iterating over trees` !)

Collapsing Subtrees
~~~~~~~~~~~~~~~~~~~

Merge all nodes in one subtree into a single dataset

Find total number of species
Find total biomass

.. _Tree Contents:

Tree Contents
Expand Down Expand Up @@ -540,33 +565,30 @@ See that the same change (fast-forwarding by adding 10 years to the age of each
Mapping Custom Functions Over Trees
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can map custom computation over each node in a tree using :py:meth:`DataTree.map_over_subtree`.
You can map any function, so long as it takes `xarray.Dataset` objects as one (or more) of the input arguments,
You can map custom computation over each node in a tree using the :py:func:`DataTree.map_over_subtree` method.

Any function can be mapped, so long as it takes `xarray.Dataset` objects as one (or more) of the input arguments,
and returns one (or more) xarray datasets.

.. note::

Functions passed to :py:func:`map_over_subtree` cannot alter nodes in-place.
Instead they must return new `xarray.Dataset` objects.

For example, we can define a function to calculate the Root Mean Square of a timeseries
For example, we can calculate the Root Mean Square value of these signals:

.. ipython:: python

def rms(signal):
return np.sqrt(np.mean(signal**2))

Then calculate the RMS value of these signals:

.. ipython:: python

voltages.map_over_subtree(rms)

.. _multiple trees:

We can also use the :py:func:`map_over_subtree` decorator to promote a function which accepts datasets into one which
We can alternatively use the :py:func:`map_over_subtree` decorator to promote a function which accepts datasets into one which
accepts datatrees.

.. _multiple trees:

Operating on Multiple Trees
---------------------------

Expand Down Expand Up @@ -631,9 +653,17 @@ we can do arithmetic between them.

currents.isomorphic(voltages)

We could use this feature to quickly calculate the electrical power in our signal, P=IV.
We could use this feature to quickly calculate the electrical power in our signals, P=IV.

.. ipython:: python

power = currents * voltages
power


Mapping over Multiple Trees
~~~~~~~~~~~~~~~~~~~~~~~~~~~

map_over_subtree with binary function
example?
meter readings?