-
Notifications
You must be signed in to change notification settings - Fork 86
Fix reset_index crash when obs index name matches existing column #1100
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
timtreis
wants to merge
3
commits into
main
Choose a base branch
from
fix/reset-index-column-collision
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
3 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import annsel as an | ||
| import geopandas as gpd | ||
| import numpy as np | ||
| import pandas as pd | ||
| import pytest | ||
| import shapely | ||
| from anndata import AnnData | ||
|
|
||
| from spatialdata import SpatialData, get_values, match_table_to_element | ||
|
|
@@ -14,7 +16,7 @@ | |
| get_element_annotators, | ||
| join_spatialelement_table, | ||
| ) | ||
| from spatialdata.models.models import TableModel | ||
| from spatialdata.models.models import ShapesModel, TableModel | ||
| from spatialdata.testing import assert_anndata_equal, assert_geodataframe_equal | ||
|
|
||
|
|
||
|
|
@@ -1262,3 +1264,28 @@ def test_filter_by_table_query_complex_combination(complex_sdata): | |
| assert ("circles", idx) in table_instance_ids | ||
| for idx in result["poly"].index: | ||
| assert ("poly", idx) in table_instance_ids | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("how", ["inner", "left"]) | ||
| def test_join_spatialelement_table_obs_index_name_collision(how): | ||
| """join_spatialelement_table must not crash when obs index name matches an existing column. | ||
|
Member
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. I think it should crash in general to avoid a silent branching in the behavior. The ok case would be if both the index and the column have the same values. |
||
|
|
||
| Regression test for https://github.com/scverse/spatialdata/issues/1099. | ||
| """ | ||
| n = 5 | ||
| shapes = ShapesModel.parse( | ||
| gpd.GeoDataFrame({"geometry": [shapely.Point(i, i) for i in range(n)], "radius": np.ones(n)}) | ||
| ) | ||
| obs = pd.DataFrame({"region": pd.Categorical(["shapes"] * n), "EntityID": np.arange(n), "cell_type": list("AABBC")}) | ||
| table = AnnData(obs=obs) | ||
| table = TableModel.parse(table, region="shapes", region_key="region", instance_key="EntityID") | ||
| sdata = SpatialData(shapes={"shapes": shapes}, tables={"table": table}) | ||
|
|
||
| # Introduce the conflicting state: index name == existing column name | ||
| sdata["table"].obs.index = pd.Index([str(i) for i in range(n)], name="EntityID") | ||
|
|
||
| element_dict, joined_table = join_spatialelement_table( | ||
| sdata=sdata, spatial_element_names="shapes", table_name="table", how=how | ||
| ) | ||
| assert joined_table.n_obs == n | ||
| assert "shapes" in element_dict | ||
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.
So here we are dropping the index instead of a column. This could be a problem if the column is different from the index.
What's the origin behind this problem? Is it that for the merfish data we should have had
sdata['table'].index.name = None? That would be a better fix, while still raising an exception here.