Chunking cleanup: remove chunk_iter, auto-derive iteration count from chunk_size - #18
Conversation
…chunk_size - Remove `chunk_iter` parameter from `pted()`, `pted_coverage_test()`, `pted_chunk_numpy/torch/jax`, and `_energy_distance_estimate_*` helpers - Compute number of iterations automatically as `max(len(x), len(y)) // chunk_size` - Iterate sequentially over the larger dataset; cycle through the smaller one using modular indexing when sizes differ - Add `chunk_size <= max(len(x), len(y))` assertion in `pted()` - Update all docstrings and README to reflect the new interface - Update tests: remove `chunk_iter` args, fix seeds, add `test_pted_chunk_mismatched_sizes` for cycling behavior
|
@copilot I think for the energy distance estimate, you can now use slicing instead of fancy indexing. This might mean that there can just be one energy distance estimate function now. Also, I think if the chunk size is larger than both the datasets, then you can just fallback to regular PTED. |
…, add chunk_size fallback
- Replace three `_energy_distance_estimate_{numpy,torch,jax}` functions with a
single `_energy_distance_estimate` core that uses plain slicing via a
`_tile_to_length` helper; the named functions become thin wrappers for API
compatibility.
- Removes the `torch.tensor(idx)` fancy-indexing workaround from the torch path.
- Add fallback in `pted()`: when `chunk_size >= len(x)` and `chunk_size >= len(y)`,
chunking covers the full dataset so regular PTED is used automatically.
- Add `test_pted_chunk_size_fallback` to cover the new fallback behaviour.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #18 +/- ##
==========================================
- Coverage 98.05% 97.66% -0.39%
==========================================
Files 4 4
Lines 359 342 -17
==========================================
- Hits 352 334 -18
- Misses 7 8 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
🟡 Not ready to approve
The new chunk estimator can produce nan for some small-input usages (public pted_chunk_*) and chunk_size validation/documentation mismatches need to be resolved.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
This review doesn't count toward merge requirements. Sign up for the private preview to control whether Copilot approvals count.
Pull request overview
This PR simplifies PTED’s chunking interface by removing the redundant chunk_iter parameter and deriving the number of chunk iterations from chunk_size, including support for mismatched x/y sizes by cycling the smaller dataset’s chunks.
Changes:
- Removed
chunk_iterfrompted(),pted_coverage_test(), backend chunk helpers, and tests. - Implemented sequential chunk traversal with iteration count derived from
max(len(x), len(y)) // chunk_size, cycling the smaller dataset’s chunks. - Updated documentation and tests, including new mismatched-size coverage.
File summaries
| File | Description |
|---|---|
tests/test_pted.py |
Updates chunking tests to the new API, adds mismatched-size and fallback coverage, and revises energy-distance estimate tests. |
src/pted/utils.py |
Refactors chunked energy-distance estimation to remove chunk_iter and share logic across backends. |
src/pted/pted.py |
Removes chunk_iter from the public API and updates chunking behavior/parameter documentation. |
README.md |
Updates argument docs and chunking guidance to match the new chunking interface. |
Review details
Suppressed comments (2)
src/pted/utils.py:125
_energy_distance_estimatewill returnnanwhenchunk_sizeexceeds both dataset lengths (because_chunk_slicesyields 0 iterations andnp.mean([])isnan). Sincepted_chunk_*are exported in__all__, calling them on small inputs with the defaultchunk_size=100can trigger this. Consider falling back to a single full energy-distance computation when no chunks are produced, and update the docstring (it currently claims the smaller input is tiled).
"""Estimate energy distance by averaging over sequential sliced chunks.
Iterates ``max(len(x), len(y)) // chunk_size`` times, using plain slicing
on both arrays. The smaller of the two is tiled along axis 0 as needed so
that both arrays are at least ``n_iter * chunk_size`` rows long before the
loop begins.
"""
src/pted/pted.py:128
- The PR description mentions an added assertion
chunk_size <= max(len(x), len(y)), but the implementation instead silently disables chunking whenchunk_sizecovers both datasets (and also acceptschunk_sizelarger than both). This is a user-visible contract difference; either the PR description/docs should be updated to describe the fallback, or the code should enforce the asserted constraint.
if chunk_size is not None:
# If chunk_size covers both full datasets, chunking adds no benefit
if chunk_size >= len(x) and chunk_size >= len(y):
chunk_size = None
- Files reviewed: 4/4 changed files
- Comments generated: 4
- Review effort level: Lite
We're testing this review assessment. Please use 👍 or 👎 to tell us if it's correct.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
The chunking API required users to specify both
chunk_sizeandchunk_itertogether, withchunk_iterbeing redundant given the data size. The new interface infers iteration count automatically and handles mismatched dataset sizes by cycling the smaller dataset.Interface changes
chunk_iterfrompted(),pted_coverage_test(), and all internalpted_chunk_*/_energy_distance_estimate_*functions.max(len(x), len(y)) // chunk_size(remainder samples discarded).chunk_size <= max(len(x), len(y)).Implementation
chunk_iter, with a newtest_pted_chunk_mismatched_sizestest covering the cycling behavior.