-
Notifications
You must be signed in to change notification settings - Fork 3
chore: groupby warn and add tests for warnings #195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
selmanozleyen
wants to merge
8
commits into
scverse:main
Choose a base branch
from
selmanozleyen:feat/issue-193-categorical-validation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c00f88f
warn and add tests for warnings
selmanozleyen f0cb3d0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 08af1ef
do the proper expected behaviour: warn when outer adds categorical cols
selmanozleyen f723ae3
keep categorical checks consistent
selmanozleyen 1804126
better warning asserts
selmanozleyen 0ddc159
warning refactor
selmanozleyen 223d095
Merge branch 'main' into feat/issue-193-categorical-validation
selmanozleyen ca6f7c7
Merge branch 'main' into feat/issue-193-categorical-validation
selmanozleyen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import glob | ||
| import re | ||
| import warnings | ||
| from contextlib import nullcontext | ||
| from typing import TYPE_CHECKING, Literal | ||
|
|
||
|
|
@@ -22,6 +24,10 @@ | |
| from pathlib import Path | ||
|
|
||
|
|
||
| def _assert_warning_count(caught_warnings: list[warnings.WarningMessage], match: str, count: int) -> None: | ||
| assert sum(bool(re.search(match, str(warning.message))) for warning in caught_warnings) == count | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ["chunk_size", "expected_shard_size"], | ||
| [pytest.param(3, 9, id="n_obs_not_divisible_by_chunk"), pytest.param(5, 10, id="n_obs_divisible_by_chunk")], | ||
|
|
@@ -44,14 +50,16 @@ def test_store_creation_warnings_with_different_keys(elem_name: Literal["obsm", | |
| path_2 = tmp_path / "with_extra_key.h5ad" | ||
| adata_1.write_h5ad(path_1) | ||
| adata_2.write_h5ad(path_2) | ||
| with pytest.warns(UserWarning, match=rf"Found {elem_name} keys.* not present in all anndatas"): | ||
| with warnings.catch_warnings(record=True) as caught_warnings: | ||
| warnings.simplefilter("always") | ||
|
Comment on lines
+53
to
+54
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this just blanket ignoring warnings? Seems bad |
||
| DatasetCollection(tmp_path / "collection.zarr").add_adatas( | ||
| [path_1, path_2], | ||
| n_obs_per_chunk=5, | ||
| shard_size=10, | ||
| dataset_size=10, | ||
| shuffle_chunk_size=10, | ||
| ) | ||
| _assert_warning_count(caught_warnings, rf"Found {elem_name} keys.* not present in all anndatas", 1) | ||
|
|
||
|
|
||
| def test_store_creation_no_warnings_with_custom_load(tmp_path: Path): | ||
|
|
@@ -121,14 +129,16 @@ def test_store_addition_different_keys( | |
| adata = ad.AnnData(X=np.random.randn(10, 20), **extra_args) | ||
| additional_path = tmp_path / "with_extra_key.h5ad" | ||
| adata.write_h5ad(additional_path) | ||
| with pytest.warns(UserWarning, match=rf"Found {elem_name} keys.* not present in all anndatas"): | ||
| with warnings.catch_warnings(record=True) as caught_warnings: | ||
| warnings.simplefilter("always") | ||
| collection.add_adatas( | ||
| [additional_path], | ||
| load_adata=load_adata, | ||
| n_obs_per_chunk=5, | ||
| shard_size=10, | ||
| shuffle_chunk_size=2, | ||
| ) | ||
| _assert_warning_count(caught_warnings, rf"Found {elem_name} keys.* not present in all anndatas", 1) | ||
|
|
||
|
|
||
| def test_h5ad_and_zarr_simultaneously(tmp_path: Path): | ||
|
|
@@ -262,7 +272,7 @@ def _write_groupby_test_adata( | |
| obs["label"] = pd.Categorical(label_values, categories=label_categories) | ||
| ad.AnnData( | ||
| X=sp.csr_matrix(np.eye(n_obs, dtype="f4")), | ||
| obs=pd.DataFrame(obs, index=[f"cell_{i}" for i in range(n_obs)]), | ||
| obs=pd.DataFrame(obs, index=[f"{path.stem}_cell_{i}" for i in range(n_obs)]), | ||
| var=pd.DataFrame(index=[f"gene_{i}" for i in range(n_obs)]), | ||
| ).write_h5ad(path, compression=None) | ||
| return path | ||
|
|
@@ -287,6 +297,168 @@ def consistent_groupby_h5_paths(tmp_path: Path) -> list[Path]: | |
| ] | ||
|
|
||
|
|
||
| def test_store_creation_warns_when_outer_join_introduces_missing_categorical_values(tmp_path: Path): | ||
| first = _write_groupby_test_adata( | ||
| tmp_path / "first.h5ad", | ||
| label_values=["a", "b", "a"], | ||
| label_categories=["a", "b"], | ||
| ) | ||
| second = _write_groupby_test_adata(tmp_path / "second.h5ad") | ||
| with warnings.catch_warnings(): | ||
| # annbatch writes Zarr v3 stores and consolidates metadata during writes, | ||
| # which currently triggers a known zarr warning unrelated to the behavior under test. | ||
| warnings.filterwarnings( | ||
| "ignore", | ||
| message="Consolidated metadata is currently not part.*", | ||
| category=UserWarning, | ||
| ) | ||
| with pytest.warns(UserWarning) as caught_warnings: | ||
| DatasetCollection(tmp_path / "collection.zarr").add_adatas( | ||
| [first, second], | ||
| n_obs_per_chunk=2, | ||
| shard_size=2, | ||
| dataset_size=3, | ||
| shuffle_chunk_size=1, | ||
| shuffle=False, | ||
| rng=np.random.default_rng(0), | ||
| ) | ||
| _assert_warning_count(caught_warnings, r"Found obs keys", 1) | ||
| _assert_warning_count(caught_warnings, r"categorical obs columns", 1) | ||
|
|
||
|
|
||
| def test_store_addition_warns_when_outer_join_introduces_missing_categorical_values(tmp_path: Path): | ||
| initial = _write_groupby_test_adata(tmp_path / "initial.h5ad") | ||
| additional = _write_groupby_test_adata( | ||
| tmp_path / "additional.h5ad", | ||
| label_values=["a", "b", "a"], | ||
| label_categories=["a", "b"], | ||
| ) | ||
| with warnings.catch_warnings(): | ||
| # annbatch writes Zarr v3 stores and consolidates metadata during writes, | ||
| # which currently triggers a known zarr warning unrelated to the behavior under test. | ||
| warnings.filterwarnings( | ||
| "ignore", | ||
| message="Consolidated metadata is currently not part.*", | ||
| category=UserWarning, | ||
| ) | ||
| collection = DatasetCollection(tmp_path / "collection.zarr").add_adatas( | ||
| [initial], | ||
| n_obs_per_chunk=2, | ||
| shard_size=2, | ||
| dataset_size=3, | ||
| shuffle_chunk_size=1, | ||
| shuffle=False, | ||
| rng=np.random.default_rng(0), | ||
| ) | ||
| with warnings.catch_warnings(): | ||
| # annbatch writes Zarr v3 stores and consolidates metadata during writes, | ||
| # which currently triggers a known zarr warning unrelated to the behavior under test. | ||
| warnings.filterwarnings( | ||
| "ignore", | ||
| message="Consolidated metadata is currently not part.*", | ||
| category=UserWarning, | ||
| ) | ||
| with pytest.warns(UserWarning) as caught_warnings: | ||
| collection.add_adatas( | ||
| [additional], | ||
| n_obs_per_chunk=2, | ||
| shard_size=2, | ||
| shuffle_chunk_size=1, | ||
| shuffle=False, | ||
| rng=np.random.default_rng(0), | ||
| ) | ||
| _assert_warning_count(caught_warnings, r"Found obs keys", 1) | ||
| _assert_warning_count(caught_warnings, r"categorical obs columns", 1) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("first_kwargs", "second_kwargs"), | ||
| [ | ||
| pytest.param( | ||
| {"label_values": ["a", "b", "a"], "label_categories": ["a", "b"]}, | ||
| {"label_values": ["b", "a", "b"], "label_categories": ["a", "b", "c"]}, | ||
| id="different_categories", | ||
| ), | ||
| pytest.param( | ||
| {"label_values": ["a", "b", "a"], "label_categories": ["a", "b"]}, | ||
| {"label_values": ["b", "a", "b"], "label_categories": ["b", "a"]}, | ||
| id="different_category_order", | ||
| ), | ||
| ], | ||
| ) | ||
| def test_store_creation_does_not_warn_for_categorical_category_expansion( | ||
| tmp_path: Path, | ||
| first_kwargs: dict, | ||
| second_kwargs: dict, | ||
| ): | ||
| first = _write_groupby_test_adata(tmp_path / "first.h5ad", **first_kwargs) | ||
| second = _write_groupby_test_adata(tmp_path / "second.h5ad", **second_kwargs) | ||
| with warnings.catch_warnings(record=True) as caught_warnings: | ||
| warnings.simplefilter("always") | ||
| DatasetCollection(tmp_path / "collection.zarr").add_adatas( | ||
| [first, second], | ||
| n_obs_per_chunk=2, | ||
| shard_size=2, | ||
| dataset_size=3, | ||
| shuffle_chunk_size=1, | ||
| shuffle=False, | ||
| rng=np.random.default_rng(0), | ||
| ) | ||
| _assert_warning_count(caught_warnings, r"categorical obs columns", 0) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("initial_kwargs", "additional_kwargs"), | ||
| [ | ||
| pytest.param( | ||
| {"label_values": ["a", "b", "a"], "label_categories": ["a", "b"]}, | ||
| {"label_values": ["b", "a", "b"], "label_categories": ["a", "b", "c"]}, | ||
| id="different_categories", | ||
| ), | ||
| pytest.param( | ||
| {"label_values": ["a", "b", "a"], "label_categories": ["a", "b"]}, | ||
| {"label_values": ["b", "a", "b"], "label_categories": ["b", "a"]}, | ||
| id="different_category_order", | ||
| ), | ||
| ], | ||
| ) | ||
| def test_store_addition_does_not_warn_for_categorical_category_expansion( | ||
| tmp_path: Path, | ||
| initial_kwargs: dict, | ||
| additional_kwargs: dict, | ||
| ): | ||
| initial = _write_groupby_test_adata(tmp_path / "initial.h5ad", **initial_kwargs) | ||
| additional = _write_groupby_test_adata(tmp_path / "additional.h5ad", **additional_kwargs) | ||
| with warnings.catch_warnings(): | ||
| # annbatch writes Zarr v3 stores and consolidates metadata during writes, | ||
| # which currently triggers a known zarr warning unrelated to the behavior under test. | ||
| warnings.filterwarnings( | ||
| "ignore", | ||
| message="Consolidated metadata is currently not part.*", | ||
| category=UserWarning, | ||
| ) | ||
| collection = DatasetCollection(tmp_path / "collection.zarr").add_adatas( | ||
| [initial], | ||
| n_obs_per_chunk=2, | ||
| shard_size=2, | ||
| dataset_size=3, | ||
| shuffle_chunk_size=1, | ||
| shuffle=False, | ||
| rng=np.random.default_rng(0), | ||
| ) | ||
| with warnings.catch_warnings(record=True) as caught_warnings: | ||
| warnings.simplefilter("always") | ||
| collection.add_adatas( | ||
| [additional], | ||
| n_obs_per_chunk=2, | ||
| shard_size=2, | ||
| shuffle_chunk_size=1, | ||
| shuffle=False, | ||
| rng=np.random.default_rng(0), | ||
| ) | ||
| _assert_warning_count(caught_warnings, r"categorical obs columns", 0) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("groupby", "match"), | ||
| [ | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can just get away with adding
obsandvarto this check. Why not? It would would go beyond categoricals, but I'm not sure that is so bad. I am not sure whyobsandvarare excluded. This check is relatively naive as well so we could also add some behavior around e.g., mismatched dtypes. But that appears out-of-scope for this PR, aside from maybe just adding the special warning about categoricals.