Skip to content

Commit

Permalink
Make bounding_box a cached property of TreeOfBoxes
Browse files Browse the repository at this point in the history
  • Loading branch information
inducer committed Dec 27, 2022
1 parent 1423c46 commit 13d250a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 14 deletions.
9 changes: 7 additions & 2 deletions boxtree/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"""

from typing import Tuple
from functools import cached_property

import pyopencl as cl
import numpy as np
Expand Down Expand Up @@ -214,8 +215,6 @@ class TreeOfBoxes:
box_child_ids: np.ndarray
box_levels: np.ndarray

bounding_box: Tuple[np.ndarray, np.ndarray]

box_flags: np.ndarray
level_start_box_nrs: np.ndarray

Expand Down Expand Up @@ -260,6 +259,12 @@ def leaf_boxes(self):
boxes = np.arange(self.nboxes)
return boxes[self.leaf_flags]

@cached_property
def bounding_box(self) -> Tuple[np.ndarray, np.ndarray]:
lows = self.box_centers[:, 0] - 0.5 * self.root_extent
highs = lows + self.root_extent
return lows, highs

# {{{ dummy interface for TreePlotter

def get_box_size(self, ibox):
Expand Down
11 changes: 0 additions & 11 deletions boxtree/tree_of_boxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,6 @@

# {{{ utils for tree of boxes

def _tob_bounding_box(
box_centers: np.ndarray, root_extent: np.ndarray,
) -> Tuple[np.ndarray, np.ndarray]:
lows = box_centers[:, 0] - 0.5 * root_extent
highs = lows + root_extent
return lows, highs


def _resized_array(arr: np.ndarray, new_size: int) -> np.ndarray:
"""Return a resized copy of the array. The new_size is a scalar which is
applied to the last dimension.
Expand Down Expand Up @@ -173,7 +165,6 @@ def _apply_refine_flags_without_sorting(refine_flags, tob):
box_parent_ids=box_parents,
box_child_ids=box_children,
box_levels=box_levels,
bounding_box=_tob_bounding_box(box_centers, tob.root_extent),
)


Expand Down Expand Up @@ -236,7 +227,6 @@ def _sort_boxes_by_level(tob, queue=None):
box_parent_ids=box_parent_ids,
box_child_ids=box_child_ids,
box_levels=box_levels,
bounding_box=_tob_bounding_box(box_centers, tob.root_extent),
)


Expand Down Expand Up @@ -346,7 +336,6 @@ def make_tree_of_boxes_root(
box_parent_ids=box_parent_ids,
box_child_ids=np.array([0] * 2**dim, box_id_dtype).reshape(2**dim, 1),
box_levels=np.array([0], box_level_dtype),
bounding_box=_tob_bounding_box(box_centers, root_extent),

box_flags=np.empty(1, dtype=box_flags_enum.dtype),
level_start_box_nrs=np.array([0], dtype=box_level_dtype),
Expand Down
1 change: 0 additions & 1 deletion test/test_tree_of_boxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ def test_traversal_from_tob(actx_factory):
box_parent_ids=actx.from_numpy(tob.box_parent_ids),
box_child_ids=actx.from_numpy(tob.box_child_ids),
box_levels=actx.from_numpy(tob.box_levels),
bounding_box=tob.bounding_box,
box_flags=actx.from_numpy(tob.box_flags),
)

Expand Down

0 comments on commit 13d250a

Please sign in to comment.