braintools.tree.as_numpy

Contents

braintools.tree.as_numpy#

braintools.tree.as_numpy(tree, is_leaf=<function is_quantity>)[source]#

Convert all leaves of a PyTree to NumPy arrays.

Parameters:
  • tree (Quantity]) – Input PyTree whose leaves will be converted.

  • is_leaf (Callable[[Any], bool] | None) – Predicate to treat a node as a leaf during traversal. Defaults to u.math.is_quantity.

Returns:

PyTree with np.asarray applied to each leaf.

Return type:

ndarray]

Notes

This performs a best-effort conversion using np.asarray; for JAX arrays this typically results in host NumPy arrays.

Examples

>>> import jax.numpy as jnp
>>> import braintools
>>> tree = {'a': jnp.array([1.0, 2.0]), 'b': jnp.array([3.0])}
>>> out = braintools.tree.as_numpy(tree)
>>> type(out['a']).__name__, out['b'].tolist()
('ndarray', [3.0])