Skip to content

Share GridOptimizer candidate evaluation - #47

Merged
soodoku merged 2 commits into
mainfrom
dry-shared-helper
Oct 23, 2026
Merged

soodoku merged 2 commits into
mainfrom
dry-shared-helper

Conversation

@soodoku

@soodoku soodoku commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

Duplicated sites

  • pyppur/optimizers/grid_optimizer.py:129-145 (evaluating the supplied initial direction)
  • pyppur/optimizers/grid_optimizer.py:176-192 (evaluating each random/perturbed candidate)

Both wrote the candidate into best_directions[component], built objective_args as (X, component + 1) or (X, component + 1, *kwargs.values()), and called self.objective_func(best_directions[: component + 1].flatten(), *objective_args).

How it is shared now

New private method GridOptimizer._evaluate_direction(X, best_directions, component, direction, kwargs) does the write and the call; both sites call it and keep their own compare-and-keep. The two-branch tuple is collapsed to (X, component + 1, *kwargs.values()): with empty kwargs that is the same tuple, so the objective receives identical arguments. The final all-components evaluation is left as is. Public API unchanged.

Numerical identity

Out-of-repo script: ReconstructionObjective and DistanceObjective (with dist_X kwarg) × initial guess none / full / partial (fewer rows than components) × seeds 0, 7 on 60 standardized digits, 3 components, 15 directions, 3 iterations. Dumped directions, final loss and per-component losses on main and on this branch: 36/36 arrays bitwise identical (np.array_equal).

Tests

Existing grid tests only checked shapes and loss > 0. Added test_grid_optimizer_evaluation_protocol in tests/test_optimizer.py, using a recording objective. It pins the number and order of objective calls and their k, that X and dist_X are forwarded positionally, that the first call uses the normalised initial direction, that component 1 is scored with component 0 fixed, the argmin selection and loss_per_component, and the final evaluation. It passes on main and on this branch. It fails on two injected mutations: passing component instead of component + 1 (3 failures), and writing -direction (1 failure).

Checks

command main this branch
uv run pytest 66 passed 67 passed (coverage 89%, floor 85)
ruff check pyppur tests pass pass
ruff format --check pyppur tests pass pass
pyright 0 errors 0 errors
preen check --strict --skip tests (0.7.1) 1 audit error (virtualenv in uv.lock) same, unrelated to this PR

jscpd

uvx jscpd==5.4.2 pyppur --format python --ignore-identifiers=consistent --min-lines 12 --min-tokens 1

  • before: 4 clones (this one plus three outside this PR's scope: projection_pursuit.py 406-419 ↔ 473-486 ↔ 530-543, utils/visualization.py 52-67 ↔ 109-124)
  • after: 3 clones (the grid-optimizer clone is gone)

🤖 Generated with Claude Code

The initial-direction check and the random-candidate loop in
GridOptimizer._optimize_sequential each wrote the candidate into
best_directions, built the objective arguments and called the objective
on the first component + 1 directions. That step is now
_evaluate_direction. The `(X, k) if not kwargs else (X, k, *kwargs.values())`
branch is collapsed to `(X, k, *kwargs.values())`, which is the same tuple.

A new test pins the call protocol (count and order of calls, k, X and
kwargs forwarding, in-place write, argmin selection, final evaluation);
it passes before and after and fails on two injected mutations.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-23T19:49:27.496642Z f425ea4 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@soodoku
soodoku merged commit ee59d6d into main Oct 23, 2026
13 checks passed
@soodoku
soodoku deleted the dry-shared-helper branch October 23, 2026 20:51
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.

1 participant