-
Notifications
You must be signed in to change notification settings - Fork 86
allow filtering by ids #627
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 all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
fc09cc0
allow filtering by ids
melonora a9f2b62
Merge branch 'main' into filter
LucaMarconato 122aa5f
wip match_table_to_sdata()
LucaMarconato 829a4cd
add match_table_to_sdata(); improve table model, get_annotated_regions()
LucaMarconato 0d963a0
remove filter()
LucaMarconato 23ac075
Merge branch 'main' into filter
LucaMarconato d79e533
add default argument for match_sdata_to_table(); fix tests
LucaMarconato d67d5d2
Merge branch 'main' into filter
LucaMarconato 399fc48
Apply suggestions from code review
LucaMarconato e9ddf94
apply suggestions from code review
LucaMarconato 61b1845
Merge branch 'filter' of https://github.com/melonora/spatialdata into…
LucaMarconato b01b4ce
Merge branch 'main' into filter
LucaMarconato d7a27be
fix pre-commit
LucaMarconato f521f1f
Merge branch 'filter' of https://github.com/melonora/spatialdata into…
LucaMarconato 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
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
144 changes: 144 additions & 0 deletions
144
tests/core/query/test_relational_query_match_sdata_to_table.py
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,144 @@ | ||
| import pytest | ||
|
|
||
| from spatialdata import SpatialData, concatenate, match_sdata_to_table | ||
| from spatialdata.datasets import blobs_annotating_element | ||
|
|
||
|
|
||
| def _make_test_data() -> SpatialData: | ||
| sdata1 = blobs_annotating_element("blobs_polygons") | ||
| sdata2 = blobs_annotating_element("blobs_polygons") | ||
| sdata = concatenate({"sdata1": sdata1, "sdata2": sdata2}, concatenate_tables=True) | ||
| sdata["table"].obs["value"] = list(range(sdata["table"].obs.shape[0])) | ||
| return sdata | ||
|
|
||
|
|
||
| # constructing the example data; let's use a global variable as we can reuse the same object on most tests | ||
| # without having to recreate it | ||
| sdata = _make_test_data() | ||
|
|
||
|
|
||
| def test_match_sdata_to_table_filter_specific_instances(): | ||
| """ | ||
| Filter to keep only specific instances. Note that it works even when the table annotates multiple elements. | ||
| """ | ||
| matched = match_sdata_to_table( | ||
| sdata, | ||
| table=sdata["table"][sdata["table"].obs.instance_id.isin([1, 2])], | ||
| table_name="table", | ||
| ) | ||
| assert len(matched["table"]) == 4 | ||
| assert "blobs_polygons-sdata1" in matched | ||
| assert "blobs_polygons-sdata2" in matched | ||
|
|
||
|
|
||
| def test_match_sdata_to_table_filter_specific_instances_element(): | ||
| """ | ||
| Filter to keep only specific instances, in a specific element. | ||
| """ | ||
| matched = match_sdata_to_table( | ||
| sdata, | ||
| table=sdata["table"][ | ||
| sdata["table"].obs.instance_id.isin([1, 2]) & (sdata["table"].obs.region == "blobs_polygons-sdata1") | ||
| ], | ||
| table_name="table", | ||
| ) | ||
| assert len(matched["table"]) == 2 | ||
| assert "blobs_polygons-sdata1" in matched | ||
| assert "blobs_polygons-sdata2" not in matched | ||
|
|
||
|
|
||
| def test_match_sdata_to_table_filter_by_threshold(): | ||
| """ | ||
| Filter by a threshold on a value column, in a specific element. | ||
| """ | ||
| matched = match_sdata_to_table( | ||
| sdata, | ||
| table=sdata["table"][sdata["table"].obs.query('value < 5 and region == "blobs_polygons-sdata1"').index], | ||
| table_name="table", | ||
| ) | ||
| assert len(matched["table"]) == 5 | ||
| assert "blobs_polygons-sdata1" in matched | ||
| assert "blobs_polygons-sdata2" not in matched | ||
|
|
||
|
|
||
| def test_match_sdata_to_table_subset_certain_obs(): | ||
| """ | ||
| Subset to certain obs (we could also subset to certain var or layer). | ||
| """ | ||
| matched = match_sdata_to_table( | ||
| sdata, | ||
| table=sdata["table"][[0, 1, 2, 3]], | ||
| table_name="table", | ||
| ) | ||
| assert len(matched["table"]) == 4 | ||
| assert "blobs_polygons-sdata1" in matched | ||
| assert "blobs_polygons-sdata2" not in matched | ||
|
|
||
|
|
||
| def test_match_sdata_to_table_shapes_and_points(): | ||
| """ | ||
| The function works both for shapes (examples above) and points. | ||
| Changes the target of the table to labels. | ||
| """ | ||
| sdata = _make_test_data() | ||
| sdata["table"].obs["region"] = sdata["table"].obs["region"].apply(lambda x: x.replace("polygons", "points")) | ||
| sdata["table"].obs["region"] = sdata["table"].obs["region"].astype("category") | ||
| sdata.set_table_annotates_spatialelement( | ||
| table_name="table", | ||
| region=["blobs_points-sdata1", "blobs_points-sdata2"], | ||
| region_key="region", | ||
| instance_key="instance_id", | ||
| ) | ||
|
|
||
| matched = match_sdata_to_table( | ||
| sdata, | ||
| table=sdata["table"], | ||
| table_name="table", | ||
| ) | ||
|
|
||
| assert len(matched["table"]) == 10 | ||
| assert "blobs_points-sdata1" in matched | ||
| assert "blobs_points-sdata2" in matched | ||
| assert "blobs_polygons-sdata1" not in matched | ||
|
|
||
|
|
||
| def test_match_sdata_to_table_match_labels_error(): | ||
| """ | ||
| match_sdata_to_table() uses the join operations; so when trying to match labels, the error will be raised by the | ||
| join. | ||
| """ | ||
| sdata = _make_test_data() | ||
| sdata["table"].obs["region"] = sdata["table"].obs["region"].apply(lambda x: x.replace("polygons", "labels")) | ||
| sdata["table"].obs["region"] = sdata["table"].obs["region"].astype("category") | ||
| sdata.set_table_annotates_spatialelement( | ||
| table_name="table", | ||
| region=["blobs_labels-sdata1", "blobs_labels-sdata2"], | ||
| region_key="region", | ||
| instance_key="instance_id", | ||
| ) | ||
|
|
||
| with pytest.warns( | ||
| UserWarning, | ||
| match="Element type `labels` not supported for 'right' join. Skipping ", | ||
| ): | ||
| matched = match_sdata_to_table( | ||
| sdata, | ||
| table=sdata["table"], | ||
| table_name="table", | ||
| ) | ||
|
|
||
| assert len(matched["table"]) == 10 | ||
| assert "blobs_labels-sdata1" in matched | ||
| assert "blobs_labels-sdata2" in matched | ||
| assert "blobs_points-sdata1" not in matched | ||
|
|
||
|
|
||
| def test_match_sdata_to_table_no_table_argument(): | ||
| """ | ||
| If no table argument is passed, the table_name argument will be used to match the table. | ||
| """ | ||
| matched = match_sdata_to_table(sdata=sdata, table_name="table") | ||
|
|
||
| assert len(matched["table"]) == 10 | ||
| assert "blobs_polygons-sdata1" in matched | ||
| assert "blobs_polygons-sdata2" in matched |
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
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.
Uh oh!
There was an error while loading. Please reload this page.