SelectionCache

SelectionCache#

class braincell.filter.SelectionCache(_masks=dict(), _morpho=None, _revision=-1)[source]#

Scratch space threaded through a whole selection expression tree.

Memoization keyed by expression value is the whole of it: evaluated() lets a composite reuse an operand’s mask instead of walking the morphology again, so (A | B) & (A | C) evaluates A once rather than twice. Consumers that derive something from a mask can memoize that too, by giving it its own cache instance rather than sharing the keyspace holding the masks – see _RegionCache in braincell._discretization.mechanism.

Notes

Memoized entries are valid only for the morphology and structural revision they were produced from. Handing the same cache to a second morphology, or reusing it after Morphology.attach(), drops the stored masks rather than returning a stale answer.

This class previously also carried tree_distance_to_root, euclidean_distance_to_root, and branch_radius_summary, reserved for the region types that still raise NotImplementedError. Nothing ever wrote them. Whoever implements one of those types should add the field it actually needs rather than inherit three guesses – note that per-node tree distance is already computed by braincell.morph._spatial.MorphologySpatialGeometry.

evaluated(expr, morpho, evaluate)[source]#

Return evaluate(), reusing an earlier result for expr.

Parameters:
  • expr (object) – The expression being evaluated. Used as the memo key, so the result is shared between operands that compare equal – and, just as importantly, not shared between two that do not.

  • morpho (object) – Morphology the result belongs to.

  • evaluate (Callable[[], object]) – Zero-argument thunk producing the value when there is no hit.

Returns:

The freshly computed or previously memoized value – a mask for the sub-expression cache, whatever the thunk returns otherwise.

Return type:

object

Notes

Expressions carrying an unhashable payload (a list of branch indices, an array of bounds) simply do not memoize; they fall through to evaluate() unchanged.