-
Notifications
You must be signed in to change notification settings - Fork 27
Fix NotFittedError in umap_embedding when fewer than 2 rows have nonzero variance #19
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5108fdb
Initial plan
Copilot aaef2fd
Fix umap_embedding edge case: return early when fewer than 2 non-cons…
Copilot 0e59f8e
Update tests/test_clustering.py
lappalainenj 512596e
Fix return type annotation: use Optional[UMAP] and clarify mask seman…
Copilot 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import numpy as np | ||
| import pytest | ||
|
lappalainenj marked this conversation as resolved.
Outdated
|
||
|
|
||
| from flyvis.analysis.clustering import umap_embedding | ||
|
|
||
|
|
||
| def test_umap_embedding_single_nonzero_variance_row(): | ||
| """Test that umap_embedding handles the edge case where only one row has | ||
| nonzero variance (all others are constant). UMAP should not be fitted and | ||
| the function should return NaN embedding with None reducer.""" | ||
| rng = np.random.default_rng(0) | ||
| # One row with variance, four constant rows | ||
| X = np.zeros((5, 10)) | ||
| X[2] = rng.random(10) | ||
|
|
||
| embedding, mask, reducer = umap_embedding(X) | ||
|
|
||
| assert reducer is None | ||
| assert np.all(np.isnan(embedding)) | ||
| # Only the one non-constant row should be True in the mask | ||
| expected_mask = np.array([False, False, True, False, False]) | ||
| np.testing.assert_array_equal(mask, expected_mask) | ||
|
|
||
|
|
||
| def test_umap_embedding_all_zero_variance_rows(): | ||
| """Test that umap_embedding handles all-constant rows gracefully.""" | ||
| X = np.ones((5, 10)) | ||
|
|
||
| embedding, mask, reducer = umap_embedding(X) | ||
|
|
||
| assert reducer is None | ||
| assert np.all(np.isnan(embedding)) | ||
| assert not np.any(mask) | ||
|
|
||
|
|
||
| def test_umap_embedding_returns_none_reducer_when_insufficient_data(): | ||
| """Test that reducer is None when fewer than 2 rows have nonzero variance.""" | ||
| X = np.zeros((4, 8)) | ||
| # Only one non-constant row | ||
| X[0] = np.arange(8, dtype=float) | ||
|
|
||
| embedding, mask, reducer = umap_embedding(X) | ||
|
|
||
| assert reducer is None | ||
| assert embedding.shape == (4, 2) | ||
| assert np.all(np.isnan(embedding)) | ||
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.
The docstring now says
reducermay beNone, but the function’s return type annotation still indicates a non-optionalUMAP. Please update the type hint to reflect the new behavior (e.g.,Optional[UMAP]) so static type checking matches runtime behavior. Also consider clarifying themasksemantics in thereducer is Noneearly-return case (it currently reflects non-constant rows, not connectivity).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.
@copilot apply changes based on this feedback
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.
Done in 512596e. Updated the return type to
Optional[UMAP]and expanded themaskdocstring entry to clarify the semantics in both the early-return case (only non-constant rows are True) and the normal case (non-constant rows that were also connected in the UMAP graph).