QuantTree

class PlasmaCalcs.quantities.quant_tree.QuantTree(obj, parent=None)

Bases: Tree

Tree of MatchedQuantity objects.

See QuantTree.display() for display options.
Most common use-case:
QuantTree.from_quantity_loader(quantity_loader, var)
will tell the QuantTree associated with loading that var from quantity_loader.
Result might depend on current values of quantity_loader attributes.

Methods

__getitem__(i)

returns self.children[i].

__iter__()

iterates over self.children.

add_child(child)

adds this child (a Tree) to self.children.

contains_var(var)

whether this tree has any node with obj.var == var

display([show_depth, max_depth, shorthand])

display self in html.

enumerate_flat(*[, include_self])

returns a generator which iterates over all of self's descendants, in depth-first order,

flat(*[, include_self])

returns a generator which iterates over all of self's descendants, in depth-first order.

flat_branches_until(branches_until, *[, ...])

returns a generator which iterates over all of self's descendants, in depth-first order,

flat_branches_until_vars(*[, include_self])

iterator over self and all descendants, but no descendants of MatchedVar nodes.

from_quantity_loader(quantity_loader, var, *)

returns a QuantTree from a QuantityLoader and var name.

has_node_where(condition, *[, include_self])

returns True if any node in self or descendants satisfies condition(node).

html([show_depth, max_depth, shorthand])

returns html for displaying self and all of self's children.

loading_dims()

return a list of all the dims for loading across, as implied by this tree.

make_child(obj)

makes a child of self, with obj as its stored object, and returns the child.

make_children(objs)

make_child(obj) for obj in objs; returns the list of newly made children.

result_dims()

return a list of all the dims the result would have, as implied by this tree.

set_parent(parent, *[, _internal])

sets self.parent = parent.

_on_added_child(child)

called immediately after adding a child to self.

_on_added_descendant(descendant)

called immediately after adding a descendant to self (or any of self.children).

_on_adding_ancestor(ancestor)

called immediately before adding an ancestor to self (i.e., self.parent, parent of self.parent, etc)

_on_setting_parent(parent)

called immediately before setting self.parent = parent.

_repr_html_()

returns html string for displaying self.

_shorthand_repr()

returns shorthand repr for this node (without children): "((depth, height, size)) obj".

Attributes

DEFAULT_TREE_SHORTHAND

DEFAULT_TREE_SHOW_DEPTH

DEFAULT_TREE_SHOW_MAX_DEPTH

depth

number of layers above self.

height

number of layers below self.

parent

parent node of self.

parent_ref

stores parent value, but internally uses weakref to avoid circular references.

size

number of nodes in this tree.

__getitem__(i)

returns self.children[i]. If i is a tuple, index repeatedly, e.g. self[i[0]][i[1]][i[2]]…

__iter__()

iterates over self.children.

_assert_type

alias of MatchedQuantity

_on_added_child(child)

called immediately after adding a child to self.

Tracks depth, height, size. This method does not connect any parents or children.
_on_added_descendant(descendant)

called immediately after adding a descendant to self (or any of self.children).

Tracks depth, height, size. This method does not connect any parents or children.
_on_adding_ancestor(ancestor)

called immediately before adding an ancestor to self (i.e., self.parent, parent of self.parent, etc)

Tracks depth, height, size. This method does not connect any parents or children.
_on_setting_parent(parent)

called immediately before setting self.parent = parent.

Tracks depth, height, size. This method does not connect any parents or children.
_repr_html_()

returns html string for displaying self. (this display hook is used by Jupyter automatically.).

Includes self.html() and DEFAULTS.TREE_CSS.

[TODO] apply the css to ONLY this output cell in Jupyter, instead of all output cells…

_shorthand_repr()

returns shorthand repr for this node (without children): “((depth, height, size)) obj”.

add_child(child)

adds this child (a Tree) to self.children. Also, child.set_parent(self).

returns the added child.
contains_var(var)

whether this tree has any node with obj.var == var

property depth

number of layers above self. (parent, parent’s parents, etc.)

depth = 0 for the node with parent = None.
display(show_depth=None, max_depth=None, *, shorthand=None)

display self in html. Includes self.html() and DEFAULTS.TREE_CSS.

show_depth: None or int
max number of layers of tree to show by default (i.e. “not hidden” by default)
None –> use self.DEFAULT_TREE_SHOW_DEPTH if defined else DEFAULTS.TREE_SHOW_DEPTH
max_depth: None or int
max number of layers of tree to render (even if all layers are “not hidden”).
Anything deeper will not be converted to html string.
None –> use self.DEFAULT_TREE_SHOW_MAX_DEPTH if defined else DEFAULTS.TREE_SHOW_MAX_DEPTH
shorthand: None or bool
whether to use shorthand for the “Tree([depth=N, height=N, size=N], obj=…)” part of the repr.
True –> use shorthand; replace that^ with: “((N, N, N)) …”
None –> use self.DEFAULT_TREE_SHORTHAND if defined else DEFAULTS.TREE_SHORTHAND
enumerate_flat(*, include_self=False)

returns a generator which iterates over all of self’s descendants, in depth-first order,

yielding (index, node) pairs, such that self[index] == node.
Note that index will be a tuple with length == node.depth.
if include_self, yield self first, as: ((), self)).
flat(*, include_self=False)

returns a generator which iterates over all of self’s descendants, in depth-first order.

if include_self, yield self first.
flat_branches_until(branches_until, *, include_self=False)

returns a generator which iterates over all of self’s descendants, in depth-first order,

but stop looking at descendants on a branch as soon as branches_until(node).
E.g. self.flat_branches_until(lambda node: node.obj==7) will be similar to flat,
but won’t go to any descendants for any node with obj==7.
if include self, yield self first, and check branches_until(self) before continuing.
otherwise, never check branches_until(self).
flat_branches_until_vars(*, include_self=False)

iterator over self and all descendants, but no descendants of MatchedVar nodes.

classmethod from_quantity_loader(quantity_loader, var, *, missing_ok=False, **kw_call_options)

returns a QuantTree from a QuantityLoader and var name.

This is the tree of MatchedQuantity objects which will all be loaded if loading var.
(MatchedQuantity objects consist of a var, a LoadableQuantity, and possibly a _match.)
quantity_loader: QuantityLoader instance or subclass
the loader containing info about var dependencies.
if class, might not be possible to get as many vars’ dependencies
(and will crash if need to know current values of attributes, to determine dependency list)
missing_ok: bool
whether to be lenient sometimes when missing details that would allow to fully determine deps.
see help(MatchedQuantity.dep_vars) for more details.
additional kwargs appearing in quantity_loader.kw_call_options()
will be passed to quantity_loader.using(**kw_call_options), if quantity_loader is a QuantityLoader instance.
If any additional kwargs don’t appear in kw_call_options(), crash.
has_node_where(condition, *, include_self=False)

returns True if any node in self or descendants satisfies condition(node).

if include_self, check self as well.
property height

number of layers below self. (children, children’s children, etc.)

height = 0 for a node with no children.
html(show_depth=None, max_depth=None, *, shorthand=None)

returns html for displaying self and all of self’s children.

show_depth: None or int
max number of layers of tree to show by default (i.e. “not hidden” by default)
None –> use self.DEFAULT_TREE_SHOW_DEPTH if defined else DEFAULTS.TREE_SHOW_DEPTH
max_depth: None or int
max number of layers of tree to render (even if all layers are “not hidden”).
Anything deeper will not be converted to html string.
None –> use self.DEFAULT_TREE_SHOW_MAX_DEPTH if defined else DEFAULTS.TREE_SHOW_MAX_DEPTH
shorthand: None or bool
whether to use shorthand for the “Tree([depth=N, height=N, size=N], obj=…)” part of the repr.
True –> use shorthand; replace that^ with: “((N, N, N)) …”
None –> use self.DEFAULT_TREE_SHORTHAND if defined else DEFAULTS.TREE_SHORTHAND
loading_dims()

return a list of all the dims for loading across, as implied by this tree.

This is the set of quantity.dims for all quantities in this tree (self and all descendants), but:

- if quantity.ignores_dims is non-empty, exclude those dims for that node and all its descendants.
- if quantity.reduces_dims is non-empty, exclude those dims for the result
(even if a sybling node has a quantity that uses those dims).
make_child(obj)

makes a child of self, with obj as its stored object, and returns the child.

make_children(objs)

make_child(obj) for obj in objs; returns the list of newly made children.

property parent

parent node of self. None if self is root.

When set to a value, calls self.set_parent(value),
which also updates tracking info appropriately, and updates parent’s children.
property parent_ref

stores parent value, but internally uses weakref to avoid circular references.

Users should always use self.parent instead.
result_dims()

return a list of all the dims the result would have, as implied by this tree.

This is the set of quantity.dims for all quantities in this tree (self and all descendants), but:

- if quantity.ignores_dims is non-empty, exclude those dims for that node and all its descendants.
- if quantity.reduces_dims is non-empty, exclude those dims for that node and all its descendants,
(but not from the whole tree. See self.loading_dims() to exclude those from the whole tree.)
set_parent(parent, *, _internal=False)

sets self.parent = parent. Also, parent.add_child(self), unless _internal=True.

Users should use self.parent = parent instead of calling set_parent directly.
property size

number of nodes in this tree. (here and below)

size = 1 for a node with no children.