Skip to content

[1/5] Tests: characterize flatten_fragments before the memory refactor - #434

Merged
GeorgWa merged 6 commits into
mainfrom
test/flatten-fragments-characterization
Sep 15, 2026
Merged

GeorgWa merged 6 commits into
mainfrom
test/flatten-fragments-characterization

Conversation

@GeorgWa

@GeorgWa GeorgWa commented Aug 31, 2026

Copy link
Copy Markdown
Collaborator

Tests only. No production change.

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.

All tests pass unchanged on main.

First of 5 stacked PRs splitting #429.

🤖 Generated with Claude Code

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>
@GeorgWa GeorgWa changed the title Tests: characterize flatten_fragments before the memory refactor [1/5] Tests: characterize flatten_fragments before the memory refactor Aug 31, 2026
Comment thread tests/unit/peptide/test_fragment.py Outdated
@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

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.

brittle?

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.

fixed it

Comment thread tests/unit/peptide/test_fragment.py Outdated
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."""

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.

could we add a test for this?

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.

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

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.

@mschwoer do you mean we SHOULD work with static examples or we should keep this logic?

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.

we should "strongly consider"™️ it :-D

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.

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>
Comment thread tests/unit/peptide/test_fragment.py Outdated

# 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(

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.

why not assert_frame_equal?

GeorgWa and others added 2 commits September 15, 2026 10:16
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>
@GeorgWa
GeorgWa requested a review from mschwoer September 15, 2026 08:56
GeorgWa and others added 2 commits September 15, 2026 11:24
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>
@GeorgWa
GeorgWa merged commit 0e5b3ff into main Sep 15, 2026
5 checks passed
@GeorgWa
GeorgWa deleted the test/flatten-fragments-characterization branch September 15, 2026 09:52
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>
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