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
Because `filter_fun` takes a node as input, we can even filter on the node's parent. Let's say for example we want the values for the :Length, but only for the nodes that are children of a an Internode that follows another node:
In this example it returns only one value, because there is only one node that corresponds to this criteria: The Leaf with id 7.
163
163
164
164
We could apply the same kind of filtering on the node's children, or any combination of topological information and attributes.
165
165
166
-
Note that we first test if the node is not the root node, because the root node does not have a parent. We then test if the parent's symbol is "Internode" and if the link is "<".
166
+
Note that we first test if the node is not the root node, because the root node does not have a parent. We then test if the parent's symbol is :Internode and if the link is :<.
Copy file name to clipboardExpand all lines: docs/src/tutorials/3.transform_mtg.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -213,7 +213,7 @@ transform!(mtg, node -> isleaf(node) ? println(node_id(node)," is a leaf") : not
213
213
We can also use this form to mutate the MTG of a node (which is not possible with Form 2). Here's an example where we change the "Internode" symbol into "I":
214
214
215
215
```@example usepkg
216
-
transform!(mtg, node -> symbol!(node, "I"), symbol = "Internode")
216
+
transform!(mtg, node -> symbol!(node, :I), symbol = :Internode)
217
217
218
218
mtg
219
219
```
@@ -255,7 +255,7 @@ DataFrame(mtg_select)
255
255
[`transform!`](@ref) and [`select!`](@ref) use [`traverse!`](@ref) under the hood to apply a function call to each node of an MTG. [`traverse!`](@ref) is just a little bit less easy to use as it only accepts Form 4. We can obtain the exact same results as the last example of [`transform!`](@ref) using the same call with [`traverse!`](@ref). Let's change the `Leaf` symbol into `L`:
256
256
257
257
```@example usepkg
258
-
traverse!(mtg, node -> symbol!(node, "L"), symbol = "Leaf")
258
+
traverse!(mtg, node -> symbol!(node, :L), symbol = :Leaf)
259
259
260
260
mtg
261
261
```
@@ -275,7 +275,7 @@ end
275
275
For users coming from R, we also provide the `@mutate_mtg!` macro that is similar to [`transform!`](@ref) but uses a more `tidyverse`-alike syntax. All values coming from the MTG node must be preceded by a `node.`, as with the `.data$` in the `tidyverse`. The names of the attributes are shortened to just `node.attr_name` instead of `node_attributes(node).attr_name` though. Here's an example usage:
We see that we first name the new attribute and assign the result of the computation. Constants are provided as is, and values coming from the nodes are prefixes by `node.`.
Copy file name to clipboardExpand all lines: docs/src/tutorials/7.performance_considerations.md
+9-2Lines changed: 9 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,6 +22,13 @@ The MTG encoding is the type used to store the MTG information about the node, *
22
22
23
23
By default, MultiScaleTreeGraph.jl uses a mutable encoding ([`MutableNodeMTG`](@ref)), which allows for modifying this information. However, if the user does not need to modify these, it is recommended to use an immutable encoding instead ([`NodeMTG`](@ref)). This will improve performance significantly.
24
24
25
+
The internal representation of MTG `symbol` and `link` values is based on `Symbol` for faster comparisons and lower repeated allocations. For backward compatibility, string inputs are still accepted everywhere (constructors and filters), but using symbols in performance-critical code is recommended:
26
+
27
+
```julia
28
+
NodeMTG(:/, :Internode, 1, 2)
29
+
traverse(mtg, x -> x, symbol=:Internode, link=:<)
30
+
```
31
+
25
32
### Traversal: node caching
26
33
27
34
MultiScaleTreeGraph.jl traverses all nodes by default when performing tree traversal. The traversal is done in a recursive manner so it is performant, but not always as fast as it could be. For example, we could have a very large tree with only two leaves at the top. In this case, we would traverse all nodes in the tree, even though we only need to traverse two nodes.
@@ -34,10 +41,10 @@ To improve performance, it is possible to cache any type of `traversal`, includi
34
41
To cache a traversal, you can use [`cache_nodes!`](@ref). For example, if you want to cache all the **leaf** nodes in the MTG, you can do:
35
42
36
43
```julia
37
-
cache_nodes!(mtg, symbol ="Leaf")
44
+
cache_nodes!(mtg, symbol =:Leaf)
38
45
```
39
46
40
-
This will cache all the nodes with the symbol `"Leaf"` in the MTG. Then, the tree traversal functions will use the cached traversal to iterate over the nodes.
47
+
This will cache all the nodes with the symbol `:Leaf` in the MTG. Then, the tree traversal functions will use the cached traversal to iterate over the nodes.
41
48
42
49
!!! tip
43
50
Tree traversal is *very* fast, so caching nodes is not always necessary. Caching should be used when the traversal is needed **multiple times**, and the traversal is sparse, *i.e.* a lot of nodes are filtered-out.
0 commit comments