Skip to content

test/normalize_median - #32

Open
leventetn wants to merge 3 commits into
mainfrom
tests/normalize_median
Open

test/normalize_median#32
leventetn wants to merge 3 commits into
mainfrom
tests/normalize_median

Conversation

@leventetn

Copy link
Copy Markdown
Collaborator

No description provided.

@read-the-docs-community

read-the-docs-community Bot commented Jul 2, 2026

Copy link
Copy Markdown

@idf-io idf-io left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please review and address my comments. Only the ones with checkboxes need to be adressed.

adata,
log_space=True,
)

@idf-io idf-io Jul 3, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

  • Missing complementary functionality of "linear space detection".
  • In addition please cover that when detected input spaces and passed space differ and force is passed, no error os returned


class TestNormalizeMedian:
"""Contract tests for normalize_median."""

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please include a succinct description of the testing approach as an holistic intro. It should allow for a quick understanding of where to categorically look for a specific test and if a more general conceptual testing approach is included. Improves readability.

E.g.

  1. Linear space validation
  2. Log space validation
  • More explanation if relevant for holistic understanding
  1. Approach x: e.g. (not relevant) due to probabilistic nature of the output data points, a bayes factor approach is used to measure ... under a threshold.
  2. Validate param fill_na
  3. Edge cases:
  • concept 1
  • concept n
  1. Verbose validation
  2. Input parameter validation

log_space=True,
)

# ── G. Verbose output ────────────────────────────────────────────

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Missing validation of parameters:

  • fill_na and zero_to_na -> actually I found this one under the "Edge cases" section ( test_zero_to_na_persists_and_ignores_zero_in_median and test_fill_na_persists_before_normalization). This highlights the value of a good test class docstring overview.

)

@pytest.mark.parametrize("inplace", [True, False])
def test_zero_to_na_persists_and_ignores_zero_in_median(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I would have done two separate tests, one to validate zero_to_na and another to ensure correct nan handling in the function. This could highlight the tested functionalities more for and easier finding but this is also fine.

)

@pytest.mark.parametrize("inplace", [True, False])
def test_fill_na_persists_before_normalization(self, inplace):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Same as comment above, I would have made this two different tests but this is also fine. Don't spend time on refactoring.

np.testing.assert_allclose(
factors.loc[1:, "shift_log"].to_numpy(),
[1.0, -1.0],
)

@idf-io idf-io Jul 3, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

  • Also test that when a sample is all nan, yielding a sample-level nan value, that the target=max parameter works as expected. The function computing the max could have a different handling of nan compared to the median function.

assert factors is adata_out.uns["median_norm"]
assert "median_norm" not in adata.uns
adata_out.X[0, 0] = -100.0
assert adata.X[0, 0] == 18.0

@idf-io idf-io Jul 3, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

  • You missed a crucial edge case, which is easy to miss if you haven't worked with math programming much ;)
    Division by zero can be problematic because it can yield nan or ±inf depending on the numerator. In our case this can happen when the normalize_median(log_space=False) function computes a sample-level median == 0. Because this is very rare and biologically difficult to handle, I implemented the function to raise an error, prompting the user to handle the input data beforehand.
    In the future please be extra careful when you see mathematical operations such as division and logarithms (also as part of more complex functions such as the variance which includes a division).

Implemented in tests/pp/test_normalize_median.py]
What changed:
Expanded the module docstring into a short holistic testing intro.
Renamed the detection section to F. Input-space detection.
Added target="max" coverage with an all-NaN sample-level median.
Added linear-space detection success coverage.
Added force=True coverage for linear-like data passed as log-space.
Strengthened the zero-median linear-space error test to cover both
inplace modes and confirm no partial mutation or factor storage occurs.
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