Skip to content

Trial mode, move, and lifecycle methods - #147

Merged
AP6YC merged 5 commits into
developfrom
trial-mode
Nov 14, 2026
Merged

AP6YC merged 5 commits into
developfrom
trial-mode

Conversation

@NiklasMelton

@NiklasMelton NiklasMelton commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

Adds the cvi pieces artlib's unified CVIART wrapper needs.

Trial mode (commit keyword)

  • get_cvi, remove, merge, split, and the new move take a keyword-only commit: bool = True.
  • With commit=False, the call returns the score the operation would produce and leaves the object unchanged.
  • A previously unseen label means "create a cluster". Scores are NaN while the index is undefined.
  • With a 1-D sample and commit=False, get_cvi(sample, labels) and move(sample, old, labels) accept a 1-D array of candidate labels and return one score per label. Committing still requires a scalar label, so batch get_cvi stays unambiguous.
  • Commit after trial reuses the work.
    • Trials of additions use a new plan/apply path: a pure step computes each candidate's new statistics, and a separate step writes one of them. Structural trials stage on a copy.
    • Plain commits (no matching trial) still use each index's original _param_inc, unchanged from develop, so existing users see the same numbers and speed. This means the add math exists twice per index for now; tests keep the two paths in agreement, and Unify the plain-commit and trial add paths #148 tracks unifying them.
    • The object keeps its most recent trial's computed state. A following committed call with the same operation and arguments (for add/move: same sample, and a label that was among the candidates) applies that state instead of recomputing.
    • The stored state is private, is cleared by any other mutation, and is excluded from pickles.

move(sample, old_label, new_label, *, commit=True)

  • A committed move leaves exactly the state of remove followed by get_cvi.
  • The move is applied atomically: no transient state with a deleted singleton is visible, and a failed move leaves the object unchanged.
  • Moving a sample to its own label is a no-op that returns the current score, matching artlib's switch_label.

Lifecycle, accessors, capabilities

  • reset() clears the data and keeps the configuration and backend. Batch get_cvi works again afterwards.
  • clone() returns a fresh, unfitted object with the same constructor configuration.
  • New read-only accessors: n_clusters, n_samples, labels.
  • is_better(new, old, tol=0.0) follows info.optimality, accepts ties, and treats NaN as acceptance.
  • CVICapabilities gains trial and move. Both are True for NumPy and Numba, and False for CONN and JAX, which raise NotImplementedError.

Cost

  • Vectorized scoring of 40 candidates (K=40, d=8) takes 65 µs for CH/WB and 140–460 µs for the pairwise indices. A loop of scalar trials takes 2–4 ms.
  • CH/WB are O(K·d) over all candidates. Pairwise indices are O(K²·d), since each candidate needs its new centroid row.
  • Plain committing get_cvi (no trial) is within 1–2 µs of develop (CH: 13.1 → 14.7 µs at K=40, d=8). That remainder is the check for a saved trial.

Tests

tests/test_trial.py covers every index except CONN, on both NumPy and Numba:

  • trial score equals the committed score;
  • the object is unchanged after any trial;
  • vectorized scores equal per-label trials;
  • a commit reusing a trial matches a plain commit (relative tolerance 1e-12) and does not call the planner;
  • plain commits never call the planner;
  • move equals remove plus get_cvi;
  • random add/move/remove/merge sequences match batch scores, with about 150 finite comparisons per index;
  • pickle round-trip for every index;
  • import cvi does not import artlib.

The full suite passes locally (1807 passed, 149 skipped). JAX is not installed locally, so its tests were skipped.

CI

The Python 3.9 job now runs on ubuntu-22.04, because ubuntu-latest no longer provides Python 3.9.

Incremental operations (get_cvi, remove, merge, split, and the new move)
take a keyword-only commit argument. With commit=False they return the
score the operation would produce and leave the object unchanged; get_cvi
and move also accept a 1-D array of candidate labels and return one score
per label. Unseen labels create a new cluster; undefined scores are NaN.

Additions are split into a pure plan step and an apply step. The object
keeps its most recent trial's computed state, so a following committed
call with the same operation and arguments applies it without
recomputing. Structural trials stage on a copy that the commit adopts.

move(sample, old_label, new_label) commits exactly the state of remove
followed by get_cvi, atomically; moving to the same label is a no-op.

Also adds reset(), clone(), n_clusters, n_samples, labels, is_better(),
and trial/move capability flags (False for CONN and JAX). The trial memo
is excluded from pickles. Version bumped to 0.8.0.
@codecov

codecov Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.59893% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 96.34%. Comparing base (8627c9a) to head (e0b7aca).

Files with missing lines Patch % Lines
src/cvi/modules/_base.py 99.14% 3 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #147      +/-   ##
===========================================
+ Coverage    95.31%   96.34%   +1.03%     
===========================================
  Files           21       22       +1     
  Lines         2584     3313     +729     
===========================================
+ Hits          2463     3192     +729     
  Misses         121      121              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

GitHub's ubuntu-latest image no longer provides Python 3.9, so setup
failed and fail-fast cancelled every other version.
Covers clone for subclasses and older pickles, invalid trial inputs,
warnings from undefined batch trials, rejection of stale plans, and the
_trial helper functions.
The version bump will land in a separate PR.
@NiklasMelton NiklasMelton changed the title Trial mode, move, and lifecycle methods (0.8.0) Trial mode, move, and lifecycle methods Oct 27, 2026
@NiklasMelton
NiklasMelton requested a review from AP6YC October 27, 2026 04:25
Restore each index's _param_inc exactly as on develop, so committing
get_cvi and uncached move calls keep their previous numerics and speed.
The plan/apply path is now used only by trials and by commits that reuse
a matching trial. Tests compare the two paths to a tight tolerance and
check that plain commits never call the planner.
@AP6YC AP6YC self-assigned this Nov 14, 2026
@AP6YC AP6YC added the enhancement New feature or request label Nov 14, 2026

@AP6YC AP6YC left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Overall good, but things are starting to get bloated. Next PRs are going to consolidate functionality since both functionality and documentation is getting duplicated.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Yeah, I'll add another PR to update the CI matrix since 3.9 is EOL now.

Comment thread docs/source/guide.rst

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This works for me as a separate section in the guide. Another PR might be useful to consolidate and simplify a lot of this ever expanding guide, but this works okay for now.

Comment thread src/cvi/modules/_base.py

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Base is getting bloated. Compactness compute is getting duplicated here, and more private methods are making cvi decently large in memory. I'll accept this, but there's got to be a better way to start structuring a lot of the machinery for base so that it isn't so bloated and confusing, either through OOP refactoring or identifying where functionality is duplicated and consolidating.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes, I even opened an issue about this. I wanted to add the new feature prior to making major removals. Next set is to remove all the duplicate code.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Comment thread src/cvi/modules/_trial.py

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I'm assuming pure helpers meaning pure functions here, which is good for trialing, so I'm good for this additional module.

Comment thread README.md

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I appreciate the add here for readme, but the readme is getting way too bloated; I'm gonna start enforcing cutback and human writing/reading of the readme, saving details for the docs. Accepting here mainly to start another PR to cut back and consolidate with the docs. LLMs are currently trained to eagerly self document in the README, but I'm using READMEs for humans since my eyes glaze over when reading other verbose READMEs for other tools, and I don't want that for us.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Agreed. I'll make a pass on the readme as well to ensure it isn't too verbose

@AP6YC
AP6YC merged commit 2c9c428 into develop Nov 14, 2026
9 checks passed
@AP6YC
AP6YC deleted the trial-mode branch November 14, 2026 20:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants