Skip to content

Migrate tests/test_core.py to npt.assert_allclose - #1188

Open
viknesh-ai wants to merge 1 commit into
stumpy-dev:mainfrom
viknesh-ai:fix/1175-migrate-test-core
Open

Migrate tests/test_core.py to npt.assert_allclose#1188
viknesh-ai wants to merge 1 commit into
stumpy-dev:mainfrom
viknesh-ai:fix/1175-migrate-test-core

Conversation

@viknesh-ai

Copy link
Copy Markdown
Contributor

Related to #1175

Continuing the file-by-file split — this one covers tests/test_core.py. Largest and messiest file in the split so far: 131 call sites, and unlike the other files, the naming here was inconsistent throughout (some pairs ref/comp, some ref/cmp, some ref_X/comp_X, and a few reversed as suffix forms like P_ref/P_comp, I_ref/I_comp).

assert_almost_equal only checks a fixed absolute tolerance and NumPy's docs recommend assert_allclose instead. What changed:

  • Normalized every pair to the ref_X/cmp_X prefix convention, including the reversed suffix forms (P_ref/P_comp -> ref_P/cmp_P, I_ref/I_comp -> ref_I/cmp_I, mp_ref -> ref_mp, etc.) and a couple of comp-not-cmp stragglers. Did a token-level rename (not a blind substring replace) to avoid clobbering unrelated words like compare/compute/comparison in comments.
  • All 131 original calls were already in (ref, comp) order, so every one needed flipping to (cmp_side, ref_side).
  • None of the 131 sites used decimal= or referenced config.STUMPY_TEST_PRECISION — all used numpy's implicit decimal=7 default, so all 131 use the literal atol=1.5e-07.
  • Checked the actual runtime dtype of every distinct naive.* source feeding a ref_ side before touching anything. Only naive.find_matches/core._find_matches (2 call sites, in test_find_matches_all/test_find_matches_maxmatch) come back dtype=object (a [distance, index] pair array, same root cause as the earlier files) — cast both sides to float64 there. Everywhere else the naive source (compute_mean_std, rolling_nanstd, distance_matrix, rolling_isconstant, get_array_ranges, rolling_window_dot_product, merge_topk_PI/merge_topk_ρI, and this file's own local naive_idx_to_mp/naive_bfs_indices helpers) is already clean float64/int64, so no unnecessary casting was added.
  • Also applied the same ref_/cmp_ naming fix to a handful of npt.assert_array_equal calls that shared the same inconsistent naming (test_apply_exclusion_zone*, test_get_mask_slices, test_gpu_searchsorted, test_process_isconstant_*), per the standing rule that this convention applies to assert_array_equal too, not just assert_allclose.
  • One non-naive.* pair (test_array_to_temp_file, a value round-tripped through core.array_to_temp_file + np.load) got the same treatment: the original value is ref_val, the round-tripped value is cmp_val. Left the unrelated left/right pairs in test_jagged_list_to_array*/test_replace_distance untouched since those aren't naive-vs-stumpy comparisons and are out of scope for this migration.
  • A few commented-out # npt.assert_almost_equal(...) lines got their syntax updated too for consistency, staying commented.

Ran the full file twice locally (normal mode and JIT-disabled/CUDA-simulated, matching test.sh coverage) — 244 passed + 1 skipped (GPU test, no CUDA available locally) in normal mode, 245 passed in CI-equivalent mode (the GPU test runs under the CUDA simulator there).

To Do List

Standing checklist from @seanlaw for this migration, applies to every file in the split - status below is for test_core.py:

  • Each test file correctly replaces all uses of npt.assert_almost_equal with its equivalent npt.assert_allclose
  • When rtol=0 for npt.assert_allclose, simply omit this parameter and value since this is the default
  • Ensure that npt.assert_allclose(actual, desired) always has the stumpy computed value as actual and the naive computation as desired
  • Use atol=1.5e-07 rather than atol=1.5*10**-07 - applies to all 131 sites here (none referenced config.STUMPY_TEST_PRECISION)
  • Eventually, replace the ugly 1.5*10**-config.STUMPY_TEST_PRECISION with config.STUMPY_TEST_PRECISION = 1.5e-07 (in config.py) - tracked as a follow-up, not part of this per-file migration (n/a for this specific file, but keeping the item open since it's still pending overall)
  • Use cmp instead of comp - this file had several comp/comp_X stragglers, all renamed
  • Rename naive outputs to ref_ and stumpy-computed outputs to cmp_ (applies to npt.assert_array_equal too) - this file had the most inconsistent naming of the series (including reversed suffix forms like P_ref/P_comp), fully normalized

Pull Request Checklist

Below is a simple checklist but please do not hesitate to ask for assistance!

  • Read our Contributing Guide
  • Referenced a Github issue (or create one if one doesn't already exist)
  • Read and reviewed all of the comments in the Github issue that you've referenced (along with cross-referenced issues/pull requests) to ensure that the issue still requires a pull request
  • Checked that the issue has not already been assigned to anybody else or is already being addressed in another pull request
  • Left a meaningful comment on the original Github issue to discuss the detailed approach for your contribution and received confirmation from the maintainers before proceeding with this pull request
  • Forked, cloned, and checked out the newest version of the code
  • Created a new branch
  • Made necessary code changes
  • Installed black (i.e., python -m pip install black or conda install -c conda-forge black)
  • Installed flake8 (i.e., python -m pip install flake8 or conda install -c conda-forge flake8)
  • Installed pytest-cov (i.e., python -m pip install pytest-cov or conda install -c conda-forge pytest-cov)
  • Ran black --exclude=".*\.ipynb" --extend-exclude=".venv" --diff ./ in the root stumpy directory
  • Ran flake8 --extend-exclude=.venv ./ in the root stumpy directory
  • Ran ./setup.sh dev && ./test.sh in the root stumpy directory and ensured that all tests are passing locally
  • Check this box if AI code assistance was used to generate 15%+ of the code in this pull request

Please do not commit any code to avoid/circumvent a failing test and, instead, engage in a discussion (below) to determine the best course of action.

Only request a review after the checklist above is fully completed!

@viknesh-ai
viknesh-ai requested a review from seanlaw as a code owner August 16, 2026 15:32
@gitnotebooks

gitnotebooks Bot commented Aug 16, 2026

Copy link
Copy Markdown

Review these changes at https://app.gitnotebooks.com/stumpy-dev/stumpy/pull/1188

@viknesh-ai

Copy link
Copy Markdown
Contributor Author

@seanlaw next file in the split — same conventions as the previous PRs. Ready for review whenever you have time.

@seanlaw seanlaw 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.

For assert_array_equal, please also ensure that switch cmp is first and ref is second

Comment thread tests/test_core.py
cmp_T_subseq_isconstant = core.process_isconstant(T, m, T_subseq_isconstant=None)

npt.assert_array_equal(T_subseq_isconstant_ref, T_subseq_isconstant_comp)
npt.assert_array_equal(ref_T_subseq_isconstant, cmp_T_subseq_isconstant)

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.

npt.assert_allclose and switch ref/cmp order

Comment thread tests/test_core.py
naive.replace_inf(comp)
npt.assert_array_equal(ref, comp)
naive.replace_inf(cmp)
npt.assert_array_equal(ref, cmp)

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.

switch ref/cmp order

Comment thread tests/test_core.py
naive.replace_inf(comp)
npt.assert_array_equal(ref, comp)
naive.replace_inf(cmp)
npt.assert_array_equal(ref, cmp)

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.

switch ref/cmp order

Comment thread tests/test_core.py
naive.replace_inf(comp)
npt.assert_array_equal(ref, comp)
naive.replace_inf(cmp)
npt.assert_array_equal(ref, cmp)

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.

switch ref/cmp order

Comment thread tests/test_core.py
naive.replace_inf(comp)
npt.assert_array_equal(ref, comp)
naive.replace_inf(cmp)
npt.assert_array_equal(ref, cmp)

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.

switch ref/cmp order

Comment thread tests/test_core.py
comp_slices = core._get_mask_slices(mask)
npt.assert_array_equal(ref_slices, comp_slices)
cmp_slices = core._get_mask_slices(mask)
npt.assert_array_equal(ref_slices, cmp_slices)

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.

switch ref/cmp order

Comment thread tests/test_core.py
comp_IDX = device_comp_IDX.copy_to_host()
npt.assert_array_equal(ref_IDX, comp_IDX)
cmp_IDX = device_cmp_IDX.copy_to_host()
npt.assert_array_equal(ref_IDX, cmp_IDX)

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.

switch ref/cmp order

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