Repository navigation
[1/5] Tests: characterize flatten_fragments before the memory refactor - #434
Merged
Merged
Conversation
flatten_fragments had no unit test. Add black-box tests that pin its contract, so the refactoring that follows can be checked step by step. `_dense_library` builds dense frames with mz == 0 padding from both sources that produce it: unmodified precursors carry no modloss fragments, and some fragments fall outside the mz range. Intensities are distinct, so the top-k selection is unambiguous. `_expected_keep_mask` derives the expected mask per precursor, without flatten_fragments. The tests cover the kept fragments over five combinations of `keep_top_k_fragments` and `min_fragment_intensity`, every annotation column, the reannotated precursor pointers, `custom_df` and `custom_columns`, the path without intensities, an empty library, and a precursor long enough to need more than a uint8 fragment number. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
mschwoer
reviewed
Aug 31, 2026
| @pytest.mark.requires_numba | ||
| def test_flatten_fragments_long_precursor(): | ||
| """A long precursor keeps fragment numbers above 255, and the columns stay uint32.""" | ||
| n_rows = 260 # close to the max_frag_per_peptide limit of _fill_in_indices |
| def _expected_keep_mask( | ||
| precursor_df, mz_df, intensity_df, keep_top_k_fragments, min_fragment_intensity | ||
| ): | ||
| """Give the keep mask over all dense slots. This does not use flatten_fragments.""" |
Contributor
There was a problem hiding this comment.
could we add a test for this?
Contributor
There was a problem hiding this comment.
seriously, could we not work with statically define input data (and data to assert against)?
such minimal data models would also help understand/debug the code better one day
Collaborator
Author
There was a problem hiding this comment.
@mschwoer do you mean we SHOULD work with static examples or we should keep this logic?
Contributor
There was a problem hiding this comment.
we should "strongly consider"™️ it :-D
Collaborator
Author
There was a problem hiding this comment.
changedf it to use a static fixture
_expected_keep_mask reimplemented the filtering logic, so it could agree with a broken flatten_fragments. Add a minimal_library fixture of two precursors over a forward and a reverse fragment type, with the kept slots written out per filter setting, and assert both the helper and flatten_fragments against it. Derive the long precursor row count from the uint8 boundary it tests instead of picking a number near the fill_in_indices limit, and guard the limit explicitly so exceeding it fails on an assertion rather than on a broadcast error inside numba. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
mschwoer
reviewed
Sep 15, 2026
|
|
||
| # Then | ||
| # kept slots: p0 r0 b, p0 r0 y, p0 r1 y, p1 r0 b, p1 r0 y, p1 r1 b | ||
| np.testing.assert_array_equal( |
Contributor
There was a problem hiding this comment.
why not assert_frame_equal?
The library was generated from a seeded rng and the expectations came from _expected_keep_mask, which reimplemented the filtering logic and so could agree with a broken flatten_fragments. Replace both with one library fixture over hand-written mz and intensity grids, and hand-written keep masks per filter setting. Two fragment types carry the whole annotation surface: y_z1 is reverse without loss and b_modloss_z1 is forward with loss, so direction, series and loss all vary over two columns. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Comparing column by column left the column set, the column order and the dtypes unchecked. Assert the whole annotated frame against an expected frame instead, and fold the row count of the filter cases into a frame comparison of mz and intensity. type and charge come out as int8, so the expected frame spells that out. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Replace the remaining column comparisons. Each one now also pins the column set, the column order and the dtypes: - without_intensity subsumes its separate missing-column assertion - selects_custom_columns checked only the column names - filters_custom_df_columns now pins where cardinality lands - reannotates_precursor_pointers compares hardcoded flat pointers instead of reslicing mz per precursor, which was the last place the tests recomputed an expectation - empty_precursor_df compares both frames instead of their lengths Five tests assert against the same unfiltered library, so it moves into a flat_unfiltered fixture. test_flatten_fragments_long_precursor keeps its property assertions. It builds 257 rows to push fragment numbers past 255, so a frame of 1542 generated values would bury what it tests. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The four scalar assertions become two equality comparisons. The dtypes need their own comparison, because .max() over columns of different dtypes upcasts to a common one and would hide a narrowed column. Also drop the tuple rebuild in the parse test, expected is already the tuple being compared. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
mschwoer
approved these changes
Sep 15, 2026
GeorgWa
pushed a commit
that referenced
this pull request
Sep 15, 2026
Main carries PR #434, the characterization tests of this stack, so the test file arrives identical apart from the helper names this branch makes private. Kept the private names in both conflicts. Main also dropped Python 3.8 and rewrote the typing generics of the whole file. The annotations of the extracted helpers came from main unchanged, so nothing here still references the removed typing imports. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
GeorgWa
pushed a commit
that referenced
this pull request
Sep 15, 2026
Main carries PRs #434 to #436, so the three branches below this one arrive from main and only fragment.py still differs. Six conflicts, all where this branch had rewritten what main still has: the dense annotation tiling, the cumsum in _reannotate_precursor_pointers that searchsorted replaces, and the helper signatures around them. Kept this branch's version of each, and main's tuple annotation, since main dropped Python 3.8 and the typing imports with it. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
GeorgWa
pushed a commit
that referenced
this pull request
Sep 15, 2026
…gment-positions Brings main and PRs #434 to #436 in behind it. Two conflicts, both where the annotation of a function this branch reworked met main's builtin generics. Kept this branch's signature and docstring with the builtin tuple, since main dropped Python 3.8 and the typing imports with it. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Tests only. No production change.
flatten_fragmentshad no unit test. Add black-box tests that pin its contract, so the refactoring that follows can be checked step by step.All tests pass unchanged on
main.First of 5 stacked PRs splitting #429.
🤖 Generated with Claude Code