Skip to content

minor fix + test (test_tree_of_boxes)#118

Open
ShawnL00 wants to merge 2 commits into
inducer:mainfrom
ShawnL00:minor-fix
Open

minor fix + test (test_tree_of_boxes)#118
ShawnL00 wants to merge 2 commits into
inducer:mainfrom
ShawnL00:minor-fix

Conversation

@ShawnL00

@ShawnL00 ShawnL00 commented Jun 6, 2026

Copy link
Copy Markdown

Fixes

  • (_apply_refine_flags_without_sorting): refining multiple boxes in one call gave the new children wrong parent IDs. refine_parents_per_child was built (nchildren, len(refine_parents)) and flattened row-major (np.tile). But box_children[:, refine_parents] numbers the new boxes in one contiguous block per parent. Now uses np.repeat(refine_parents, nchildren).

  • (_sort_boxes_by_level): reordered the box arrays by neworder but didn't update the IDs stored in box_parent_ids and box_child_ids, which still referred to the old numbering. Sorting needs two steps - rearrange the boxes into their new positions, and remap the stored IDs to the new numbering.

  • (_apply_coarsen_flags, _sort_and_prune_deleted_boxes):

    • box_levels is an integer array, so using np.inf raises OverflowError.
    • coarsen_peers had a stray .reshape(-1) that flattened the (2**dim, n_sources), so coarsen_exec_flags = np.all(coarsen_peer_is_leaf, axis=0) collapsed to a single scalar.
    • coarsen_exec_flags != coarsen_flags was wrong since they have different sizes.

Tests are included for the above fixes.

@alexfikl alexfikl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a few nitpicks going through this. 😁

It would be very appreciated if you could add a bit more context to this (explanation of the bug and fix) either to the commit or to the PR description.

Comment thread boxtree/tree_of_boxes.py
Comment on lines -177 to +178
refine_parents_per_child = np.empty(
(nchildren, len(refine_parents)), dtype=np.intp)
refine_parents_per_child[:] = refine_parents.reshape(-1)
refine_parents_per_child = refine_parents_per_child.reshape(-1)
# Assign the parent box number for each new child box.
refine_parents_per_child = np.repeat(refine_parents, nchildren)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct? The before code looks more like an np.tile.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see, this was a bug fix! Fair enough.

Comment thread boxtree/tree_of_boxes.py
Comment on lines +299 to +302
parent_has_id = box_parent_ids >= 0
box_parent_ids[parent_has_id] = old_to_new[box_parent_ids[parent_has_id]]
child_has_id = box_child_ids != 0
box_child_ids[child_has_id] = old_to_new[box_child_ids[child_has_id]]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure it's clear to me why the above box_parent_ids = tob.box_parent_ids[neworder] isn't enough. Maybe add a little comment to this block to explain what it's trying to do?

Comment thread boxtree/tree_of_boxes.py
Comment on lines -262 to +263
box_levels[coarsen_peers] = np.inf
box_levels[coarsen_peers] = np.iinfo(box_levels.dtype).max

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did this work before? I thought numpy won't let you convert inf to an integer like this.

>>> x[2] = np.inf
Traceback (most recent call last):
  File "<console>", line 1, in <module>
OverflowError: cannot convert float infinity to integer

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you are right. I can not find any test for coarsening, so I guess this was never run.

@ShawnL00

ShawnL00 commented Jun 7, 2026

Copy link
Copy Markdown
Author

Thank you for the review, Alex! I should have included more context in the PR description. Please let me know if anything else needs to be clarified or changed.

@alexfikl

alexfikl commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

Thank you for the review, Alex! I should have included more context in the PR description. Please let me know if anything else needs to be clarified or changed.

Looks good to me now! Thanks for all the helpful explanations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants