Skip to content

fix: limit index_mapping to the dataset size, reject negative values - #79

Merged
paulmueller merged 1 commit into
DC-analysis:mainfrom
paulmueller-agent:fix-index-mapping-bounds
Nov 23, 2026
Merged

paulmueller merged 1 commit into
DC-analysis:mainfrom
paulmueller-agent:fix-index-mapping-bounds

Conversation

@paulmueller-agent

Copy link
Copy Markdown
Contributor

chipstream-cli's --limit-events documents Python slice semantics, with n for None, e.g. 5000-n. dcnum did not support open-ended slices or bounds beyond the dataset. Results on a file with 40 events:

index_mapping main
1000 or slice(0, 1000) len() is 1000, reading fails with IndexError
slice(30, None) NotImplementedError: Slices must have start and stop defined
slice(-5, 40) len() is 45, reading fails
[-1, 2] OverflowError (uint32)
selects 0 events bare AssertionError in the runner (dtin)
negative int or slice pipeline identifier can't be parsed back

Changes

  • Clamping: get_mapping_indices(index_mapping, size=None) limits integers and slices to size when it is given. Lists or arrays with indices beyond size raise a ValueError, because there is no sensible way to clamp them.
  • Negative values: the new check_index_mapping rejects negative values (int, slice start/stop/step, list/array) with a ValueError. It is used by get_mapping_indices and HDF5Data.get_ppid_index_mapping.
  • Resolved indices: HDF5Data.index_mapping_indices holds the indices resolved against experiment:event count, and all data access uses them (scalar features, image caches, basins, __len__). DCNumJobRunner uses them to compute "basinmap0".
  • User value kept: HDF5Data.index_mapping still holds the user-specified value. The pipeline identifier and "pipeline:dcnum mapping" therefore contain what the user passed (e.g. 190-n-n), so DCscope comparisons are not affected.
  • Early validation: HDF5Data.__init__ validates the mapping right away. A mapping that selects no events raises ValueError: The index_mapping ... does not select any events.

Tests

  • test_index_mapping (read): new cases for slice(8, None), slice(None, 3), slice(None, None, 4), slice(8, 1000) and 1000. It also checks the length, the last image, and the user value in the identifier.
  • New test_index_mapping_invalid: 11 cases covering negative values, a list beyond the dataset, and empty selections.
  • New test_ppid_encoding_dat_negative_index_mapping.
  • New test_index_mapping_pipeline_beyond_dataset: slice(190, None) and slice(190, 1000) give the same basinmap0 and features as slice(190, 200), while the attributes keep the user value.
  • 21 of the new cases fail on main; all pass here.
  • The full suite passes, except 3 torch tests (torch is not installed locally; they fail on main too). All 73 pipeline tests pass, run one per process.
  • flake8 is clean, and ruff shows no new errors.

🤖 Generated with Claude Code

- `get_mapping_indices` has a new `size` argument. Integers and slices
  are limited to that size, so open-ended slices (`slice(30, None)`)
  and bounds beyond the dataset (`slice(30, 1000)`, `1000`) work as
  documented for chipstream's `--limit-events`. Lists or arrays with
  indices beyond the dataset raise a `ValueError`.
- `HDF5Data.index_mapping_indices` holds the resolved indices and is
  used for all data access, while `HDF5Data.index_mapping` keeps the
  user-specified value for the pipeline identifier.
- Negative values (`check_index_mapping`) and mappings that do not
  select any events raise a `ValueError` early, including when the
  pipeline identifier is computed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.65217% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.18%. Comparing base (e459e04) to head (6a5d21d).

Files with missing lines Patch % Lines
dcnum/read/hdf5_data.py 95.00% 1 Missing ⚠️
dcnum/read/mapped.py 95.83% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #79      +/-   ##
==========================================
+ Coverage   88.13%   88.18%   +0.05%     
==========================================
  Files          63       63              
  Lines        4483     4519      +36     
==========================================
+ Hits         3951     3985      +34     
- Misses        532      534       +2     

☔ 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.

@paulmueller
paulmueller merged commit bc11997 into DC-analysis:main Nov 23, 2026
8 checks passed
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