Skip to content

fix(datasets): preserve zeros and propagate NaN in williams_2018 - #33

Merged
idf-io merged 2 commits into
mainfrom
fix/williams-2018-missing-value-handling
Aug 2, 2026
Merged

fix(datasets): preserve zeros and propagate NaN in williams_2018#33
idf-io merged 2 commits into
mainfrom
fix/williams-2018-missing-value-handling

Conversation

@idf-io

@idf-io idf-io commented Aug 2, 2026

Copy link
Copy Markdown
Collaborator

What

williams_2018() coerced every zero in .X to np.nan. A zero in this dataset is a measurement —
the peptide was looked for and its intensity was zero — and is not interchangeable with "not
measured". 13,547 of the 1,307,600 cells are genuine zeros and were all being discarded.

Removing that coercion exposed two related problems in the charge-state summation, both of which it
had been hiding:

Problem Cells
min_count=0 (pandas default) a group whose charge states are all missing sums to 0.0, inventing a measurement 3,324
NaN skipped in a partially measured group [NaN, 5000] sums to 5000, reporting a partial total as complete 260

The invented zeros were previously converted straight back to NaN by the coercion, so fixing that
one alone is a no-op — but it is wrong on its own terms, and the partial-total case is not.
min_count does not address the second: it governs the all-missing case only, and pandas has no
skipna on GroupBy.sum, so the fix masks on the count.

intensities = grouped[sample_cols]
complete = intensities.count().eq(grouped.size(), axis=0)
X = intensities.sum(min_count=1).where(complete).values.T

A peptide is now quantified in a sample only if every one of its charge states was quantified there.

zero_to_na

Since zeros now survive, both datasets.williams_2018() and download.williams_2018() gain
zero_to_na: bool = False, per the convention in AGENTS.md. Mutually exclusive with fill_na
(raises ValueError), matching pl.peptide_intensities.

It governs zero semantics only. A partially measured group stays NaN under zero_to_na=True,
because it was never measured completely — not because its total happened to be zero.

Resulting .X

before after
positive 1,290,469 1,290,469
zero 0 13,547
missing 17,063 3,584

Breaking change

.X and the file written by download.williams_2018() both change, and their test hashes change
with them. The var and sample annotation hashes are unchanged — the fix touches .X only.

Commits

  1. style: — black over the four files. They were not black-clean on main, and pre-commit
    runs black over changed files, so a PR touching them has to absorb it. Split out so the logic is
    reviewable on its own.
  2. fix: — the change.

HISTORY.md updated under [Unreleased]. Full suite passes locally (Python 3.10, pandas 2.3.3).


🤖 Generated with Claude Code

idf-io and others added 2 commits August 2, 2026 22:43
Pure formatting, no behaviour change. Split out so the missing-value fix
in the following commit can be reviewed without reformatting noise.

These files were not black-clean on main, and the repository's
pre-commit runs black over changed files, so a PR touching them has to
absorb this either way.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Zeros were being coerced to missing values. A zero in this dataset is a
measurement -- the peptide was looked for and its intensity was zero --
and is not interchangeable with "not measured". 13,547 of the 1,307,600
cells are genuine zeros and were all being discarded.

Two related problems in the charge-state summation, both hidden by the
coercion above:

  - pandas' default min_count=0 meant a group whose charge states were
    ALL missing summed to 0.0, inventing 3,324 measurements. Those
    zeros were then converted straight back to NaN, so fixing this
    alone is a no-op -- but it is wrong on its own terms.

  - the same sum skipped NaN inside a PARTIALLY measured group, so
    [NaN, 5000] summed to 5000, reporting a partial total as complete.
    260 cells. min_count does not govern this; it decides the
    all-missing case only, and pandas has no skipna on GroupBy.sum, so
    the fix masks on the count.

A peptide is now quantified in a sample only if every one of its charge
states was quantified there.

Adds a `zero_to_na` parameter (default False) to the dataset loader and
the download wrapper, per the convention in AGENTS.md, mutually
exclusive with `fill_na` as in `pl.peptide_intensities`. It governs zero
semantics only: a partially measured group stays NaN under
`zero_to_na=True`, because it was never measured completely rather than
because its total happened to be zero.

`.X` and the downloaded intensities file change, and their test hashes
change with them. The var and sample annotation hashes are unchanged.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@read-the-docs-community

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5de06e497a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread proteopy/datasets/williams_2018.py
@idf-io idf-io closed this Aug 2, 2026
@idf-io idf-io reopened this Aug 2, 2026
@idf-io
idf-io merged commit a661078 into main Aug 2, 2026
12 of 13 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.

1 participant