Skip to content

Clean up some agent code#290

Merged
tristan-deep merged 10 commits intomainfrom
feature/actions
Mar 13, 2026
Merged

Clean up some agent code#290
tristan-deep merged 10 commits intomainfrom
feature/actions

Conversation

@wesselvannierop
Copy link
Collaborator

@wesselvannierop wesselvannierop commented Mar 13, 2026

Found some inconsistencies after the updates in #266 in the docstrings and decided to clean it up a bit.

  • We have the function indices_to_k_hot but this was not used by some zea.agent.selection classes, which used their own implementation (which was actually better).
  • compute_pixelwise_entropy is simplified, is a staticmethod now for standalone usage, and can be used on N-D data, which is nice to visualize the Entropy maps for 3D data.
  • Updates the docstrings and names of some functions to better reflect what is going on and fix some mistakes in the comments.

Summary by CodeRabbit

  • Refactor

    • Improved per-pixel and per-line entropy computations and selection/reweighting for more consistent sampling.
    • Mask generation now defaults to boolean masks, accepts tensor-style indices, and produces k-hot masks with updated shapes.
  • Tests

    • Updated tests to validate sampling outputs and mask/selection shapes under the new entropy, selection, and mask flow.

@wesselvannierop wesselvannierop requested review from a team and OisinNolan March 13, 2026 10:48
@wesselvannierop wesselvannierop self-assigned this Mar 13, 2026
@wesselvannierop wesselvannierop added documentation Improvements or additions to documentation agent Updates related to the zea.agent module labels Mar 13, 2026
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 13, 2026

Walkthrough

Refactors entropy computation and selection: indices_to_k_hot now accepts tensor indices and returns (..., n_possible_actions) bool masks; compute_pairwise_pixel_gaussian_error signature simplified; compute_pixelwise_entropy made a staticmethod; select_line_and_reweight_entropy renamed to reweight_entropies_around_line and vmap added; tests updated for sampling output shapes.

Changes

Cohort / File(s) Summary
Tests
tests/test_agent.py
Updated test_greedy_entropy to construct GreedyEntropy with n_possible_actions and validate sample() output shapes (mask shape (1,h,w) and selected_lines shape (1, w//2)).
Mask utilities
zea/agent/masks.py
indices_to_k_hot signature loosened to accept tensor-like indices; implementation now uses moveaxis + one_hot + reduction; return shape changed to (..., n_possible_actions) and default dtype set to boolean.
Entropy & selection logic
zea/agent/selection.py
Replaced select_line_and_reweight_entropy with reweight_entropies_around_line and added reweight_entropies_around_line_vmap; simplified compute_pairwise_pixel_gaussian_error signature; compute_pixelwise_entropy converted to @staticmethod; sampling flow updated to compute pixelwise entropy, aggregate to lines, and use new reweighting and mask construction via indices_to_k_hot.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~50 minutes

Possibly related PRs

Suggested labels

tests, enhancement

Suggested reviewers

  • OisinNolan
  • swpenninga
  • tristan-deep
🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Title check ❓ Inconclusive The title "Clean up some agent code" is vague and generic, using non-descriptive language that does not convey meaningful information about the substantial changes made (function renames, API signature updates, and refactoring of entropy computation logic). Use a more specific title that captures the primary changes, such as "Refactor GreedyEntropy entropy computation and rename reweighting functions" to better communicate the scope of modifications.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feature/actions
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov
Copy link

codecov bot commented Mar 13, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🧹 Nitpick comments (1)
tests/test_agent.py (1)

138-142: Assert the grouped-action behavior, not just the shapes.

This is the only coverage for n_possible_actions < w, but it still passes if the implementation returns the right shapes with the wrong number of selected actions or the wrong column expansion in mask.

Possible assertion upgrade
     agent = selection.GreedyEntropy(n_actions, w // 2, h, w)
     selected_lines, mask = agent.sample(particles)
     assert mask.shape == (1, h, w)
     assert selected_lines.shape == (1, w // 2)
+    assert ops.count_nonzero(selected_lines[0]) == n_actions
+    assert ops.all(mask[:, 0] == ops.repeat(selected_lines, agent.stack_n_cols, axis=1))
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/test_agent.py` around lines 138 - 142, Update the test to verify
grouped-action semantics from GreedyEntropy.sample: assert that selected_lines
contains exactly n_possible_actions entries (len(selected_lines[0]) ==
n_possible_actions) and that mask reflects grouping of width group_width = w //
n_possible_actions by checking for each selected action index a in
selected_lines[0] the mask has ones in columns a*group_width:(a+1)*group_width
(and zeros elsewhere), and that mask.sum() == n_possible_actions * group_width;
use the same names (GreedyEntropy.sample, selected_lines, mask,
n_possible_actions, w) to locate and implement these assertions.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@zea/agent/masks.py`:
- Around line 38-42: The result of indices_to_k_hot currently uses ops.any which
always yields a boolean tensor, losing the requested dtype; change
indices_to_k_hot to cast the boolean mask returned by ops.any(...) to the
provided dtype (e.g., use ops.cast(..., dtype)) before returning so callers like
make_line_mask(..., dtype=...) receive the correct numeric type.

In `@zea/agent/selection.py`:
- Around line 201-220: The docstring for reweight_entropies_around_line is
incorrect: update the Returns section to indicate it returns only the updated
entropy vector (not a tuple with the selected line index). Edit the Returns
description to specify the returned value is a Tensor (or same type as
entropy_per_line) of shape (n_possible_actions,) representing the updated
entropies per line, and remove any mention of a selected line index; reference
the function name reweight_entropies_around_line and its parameters
entropy_per_line and line_index to locate the docstring to modify.
- Around line 260-269: The current code derives linewise_action scores by
summing pixelwise entropy (compute_pixelwise_entropy) across columns, which
treats grouped columns as independent and can misrank actions; instead, compute
entropy at the grouped-action level inside sample(): aggregate particle
probabilities/logits across each action's span (self.stack_n_cols) to form a
single probability distribution per action (using particles' logits or summed
probabilities for columns belonging to the same action), then compute the
entropy of that action-level distribution to produce
column_wise_entropy/linewise_entropy; update the logic that builds
column_wise_entropy/linewise_entropy in sample() to aggregate by action
(respecting self.n_possible_actions and self.stack_n_cols) rather than summing
pixelwise_entropy, and keep compute_pixelwise_entropy only for visualization.

---

Nitpick comments:
In `@tests/test_agent.py`:
- Around line 138-142: Update the test to verify grouped-action semantics from
GreedyEntropy.sample: assert that selected_lines contains exactly
n_possible_actions entries (len(selected_lines[0]) == n_possible_actions) and
that mask reflects grouping of width group_width = w // n_possible_actions by
checking for each selected action index a in selected_lines[0] the mask has ones
in columns a*group_width:(a+1)*group_width (and zeros elsewhere), and that
mask.sum() == n_possible_actions * group_width; use the same names
(GreedyEntropy.sample, selected_lines, mask, n_possible_actions, w) to locate
and implement these assertions.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: fe180884-ff24-48ca-a50c-750c7ba10c46

📥 Commits

Reviewing files that changed from the base of the PR and between baf6590 and b5f1e31.

📒 Files selected for processing (3)
  • tests/test_agent.py
  • zea/agent/masks.py
  • zea/agent/selection.py

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

♻️ Duplicate comments (2)
zea/agent/selection.py (2)

201-220: ⚠️ Potential issue | 🟡 Minor

reweight_entropies_around_line() docstring still reflects the old helper behavior.

This method no longer selects a line or returns a tuple; it only reweights around the provided line_index and returns the updated vector. The current Args/Returns text is still describing the old behavior.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@zea/agent/selection.py` around lines 201 - 220, Update the docstring for
reweight_entropies_around_line to reflect its current behavior: it does not
select a line or return a tuple but instead reweights the provided
entropy_per_line vector around the given line_index and returns the updated
entropy vector. Modify the Args section to describe entropy_per_line (Tensor of
shape (n_possible_actions,)) and line_index (int) as inputs, and change the
Returns section to state that the function returns a Tensor of the same shape
with reweighted entropies (selected line set to 0, neighbours decreased). Remove
or adjust any text that claims the method selects a line or returns
(selected_line_index, entropies) and keep the note about torch backend
compatibility if still applicable.

260-269: ⚠️ Potential issue | 🟠 Major

Line/action scores are still being derived from pixelwise entropy after the log step.

That is not the entropy of the full observed line/action. Once an action spans multiple pixels, summing compute_pixelwise_entropy() outputs can change the ranking because the Gaussian-mixture entropy needs to be computed on the aggregated action span before log, not after. Please keep compute_pixelwise_entropy() for visualization, and compute action-level entropy directly inside sample().

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@zea/agent/selection.py` around lines 260 - 269, The current code builds
action/line scores by summing outputs of compute_pixelwise_entropy
(pixelwise_entropy → column_wise_entropy → linewise_entropy), which is incorrect
because mixtures must be combined across the action span before taking the
log/entropy; instead, keep compute_pixelwise_entropy for visualization only and
compute the action-level entropy inside sample() by aggregating the
Gaussian-mixture parameters across the span defined by stack_n_cols and
n_possible_actions and then computing the entropy of that aggregated mixture (do
not sum pixelwise entropies). Locate compute_pixelwise_entropy and replace the
linewise_entropy construction in sample() with a call that aggregates mixture
parameters over the span (or a new helper that computes
entropy_from_span(particles, span_start, span_width)) and use that result for
selection while leaving pixelwise_entropy/column_wise_entropy available for viz.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@zea/agent/selection.py`:
- Around line 171-184: The docstring for compute_pixelwise_entropy still refers
to "each line" and is misleading given the implementation returns per-pixel
entropies; update the description and summary to state it computes entropy per
pixel using a Gaussian Mixture Model approximation of the posterior (not
per-line), clarify the input shape (particles: (batch_size, n_particles,
*pixels)) and the returned shape (Tensor of shape (batch_size, *pixels)), and
adjust any mention of "line" to "pixel" throughout the docstring so the text
matches the function behavior.

---

Duplicate comments:
In `@zea/agent/selection.py`:
- Around line 201-220: Update the docstring for reweight_entropies_around_line
to reflect its current behavior: it does not select a line or return a tuple but
instead reweights the provided entropy_per_line vector around the given
line_index and returns the updated entropy vector. Modify the Args section to
describe entropy_per_line (Tensor of shape (n_possible_actions,)) and line_index
(int) as inputs, and change the Returns section to state that the function
returns a Tensor of the same shape with reweighted entropies (selected line set
to 0, neighbours decreased). Remove or adjust any text that claims the method
selects a line or returns (selected_line_index, entropies) and keep the note
about torch backend compatibility if still applicable.
- Around line 260-269: The current code builds action/line scores by summing
outputs of compute_pixelwise_entropy (pixelwise_entropy → column_wise_entropy →
linewise_entropy), which is incorrect because mixtures must be combined across
the action span before taking the log/entropy; instead, keep
compute_pixelwise_entropy for visualization only and compute the action-level
entropy inside sample() by aggregating the Gaussian-mixture parameters across
the span defined by stack_n_cols and n_possible_actions and then computing the
entropy of that aggregated mixture (do not sum pixelwise entropies). Locate
compute_pixelwise_entropy and replace the linewise_entropy construction in
sample() with a call that aggregates mixture parameters over the span (or a new
helper that computes entropy_from_span(particles, span_start, span_width)) and
use that result for selection while leaving
pixelwise_entropy/column_wise_entropy available for viz.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 62a9ccf0-6358-4885-a0cc-4d6a4d51f8b2

📥 Commits

Reviewing files that changed from the base of the PR and between b5f1e31 and bd597a5.

📒 Files selected for processing (1)
  • zea/agent/selection.py

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

♻️ Duplicate comments (1)
zea/agent/selection.py (1)

218-220: ⚠️ Potential issue | 🟡 Minor

Docstring return type is still inaccurate.

On Line 219, reweight_entropies_around_line(...) is documented as returning a Tuple, but it returns a single tensor.

Suggested doc-only fix
-        Returns:
-            Tuple: The reweighted entropy per line, of shape (n_possible_actions,)
+        Returns:
+            Tensor: The reweighted entropy per line, of shape (n_possible_actions,)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@zea/agent/selection.py` around lines 218 - 220, The docstring for
reweight_entropies_around_line currently states it returns a Tuple but the
function actually returns a single tensor; update the Returns section to list
the correct type (e.g., torch.Tensor or Tensor) and describe the shape
(n_possible_actions,) and meaning (the reweighted entropy per line) to match the
implementation in reweight_entropies_around_line.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In `@zea/agent/selection.py`:
- Around line 218-220: The docstring for reweight_entropies_around_line
currently states it returns a Tuple but the function actually returns a single
tensor; update the Returns section to list the correct type (e.g., torch.Tensor
or Tensor) and describe the shape (n_possible_actions,) and meaning (the
reweighted entropy per line) to match the implementation in
reweight_entropies_around_line.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 0a03bd4d-60f9-497f-8328-e8b22b1e6ed3

📥 Commits

Reviewing files that changed from the base of the PR and between bd597a5 and 3b82230.

📒 Files selected for processing (2)
  • zea/agent/masks.py
  • zea/agent/selection.py

Copy link
Collaborator

@tristan-deep tristan-deep left a comment

Choose a reason for hiding this comment

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

lgtm, 2 questions.

@tristan-deep tristan-deep added this to the v0.0.11 milestone Mar 13, 2026
@tristan-deep tristan-deep merged commit 66084b1 into main Mar 13, 2026
10 checks passed
@tristan-deep tristan-deep deleted the feature/actions branch March 13, 2026 14:39
@tristan-deep tristan-deep mentioned this pull request Mar 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent Updates related to the zea.agent module documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants