-
Notifications
You must be signed in to change notification settings - Fork 71
Filter Operations on Label2DModel and Shape #946
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
13
commits into
scverse:main
Choose a base branch
from
selmanozleyen:feature/filter_operations_on_label
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 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a28c7c9
init
selmanozleyen 225d593
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e549b4b
fix mypy linterrors
selmanozleyen 2aad72b
update the location and the design
selmanozleyen ef74057
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d6e22cb
update docs
selmanozleyen 46c41db
Merge branch 'feature/filter_operations_on_label' of https://github.c…
selmanozleyen 80d95a2
make coverage 100/100 because why not
selmanozleyen 4438605
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4c927ee
fixed type annotation
selmanozleyen e9e0da2
dont compute eagerly use. delete other instance key for consistency
selmanozleyen 7534c91
update the tests and make sure we use match_element_to_table
selmanozleyen b4901cb
Merge branch 'main' into feature/filter_operations_on_label
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
from functools import partial | ||
|
||
import numpy as np | ||
import xarray as xr | ||
from geopandas import GeoDataFrame | ||
from xarray.core.dataarray import DataArray | ||
from xarray.core.datatree import DataTree | ||
|
||
from spatialdata.models import Labels2DModel, ShapesModel | ||
|
||
|
||
def _mask_block(block: xr.DataArray, ids_to_remove: list[int]) -> xr.DataArray: | ||
# Use apply_ufunc for efficient processing | ||
# Create a copy to avoid modifying read-only array | ||
result = block.copy() | ||
result[np.isin(result, ids_to_remove)] = 0 | ||
return result | ||
|
||
|
||
def _set_instance_ids_in_labels_to_zero(image: xr.DataArray, ids_to_remove: list[int]) -> xr.DataArray: | ||
processed = xr.apply_ufunc( | ||
partial(_mask_block, ids_to_remove=ids_to_remove), | ||
image, | ||
input_core_dims=[["y", "x"]], | ||
output_core_dims=[["y", "x"]], | ||
vectorize=True, | ||
dask="parallelized", | ||
output_dtypes=[image.dtype], | ||
dataset_fill_value=0, | ||
dask_gufunc_kwargs={"allow_rechunk": True}, | ||
) | ||
|
||
# Force computation to ensure the changes are materialized | ||
computed_result = processed.compute() | ||
|
||
# Create a new DataArray to ensure persistence | ||
return xr.DataArray( | ||
data=computed_result.data, | ||
coords=image.coords, | ||
dims=image.dims, | ||
attrs=image.attrs.copy(), # Preserve all attributes | ||
) | ||
|
||
|
||
def _get_scale_factors(labels_element: Labels2DModel) -> list[tuple[float, float]]: | ||
scales = list(labels_element.keys()) | ||
|
||
# Calculate relative scale factors between consecutive scales | ||
scale_factors = [] | ||
for i in range(len(scales) - 1): | ||
y_size_current = labels_element[scales[i]].image.shape[0] | ||
x_size_current = labels_element[scales[i]].image.shape[1] | ||
y_size_next = labels_element[scales[i + 1]].image.shape[0] | ||
x_size_next = labels_element[scales[i + 1]].image.shape[1] | ||
y_factor = y_size_current / y_size_next | ||
x_factor = x_size_current / x_size_next | ||
|
||
scale_factors.append((y_factor, x_factor)) | ||
|
||
return scale_factors | ||
|
||
|
||
def filter_shapesmodel_by_instance_ids(element: ShapesModel, ids_to_remove: list[str]) -> GeoDataFrame: | ||
""" | ||
Filter a ShapesModel by instance ids. | ||
|
||
Parameters | ||
---------- | ||
element | ||
The ShapesModel to filter. | ||
ids_to_remove | ||
The instance ids to remove. | ||
|
||
Returns | ||
------- | ||
The filtered ShapesModel. | ||
""" | ||
element2: GeoDataFrame = element[~element.index.isin(ids_to_remove)] # type: ignore[index, attr-defined] | ||
return ShapesModel.parse(element2) | ||
|
||
|
||
def filter_labels2dmodel_by_instance_ids(element: Labels2DModel, ids_to_remove: list[int]) -> DataArray | DataTree: | ||
""" | ||
Filter a Labels2DModel by instance ids. | ||
|
||
This function works for both DataArray and DataTree and sets the | ||
instance ids to zero. | ||
|
||
Parameters | ||
---------- | ||
element | ||
The Labels2DModel to filter. | ||
ids_to_remove | ||
The instance ids to remove. | ||
|
||
Returns | ||
------- | ||
The filtered Labels2DModel. | ||
""" | ||
if isinstance(element, xr.DataArray): | ||
return Labels2DModel.parse(_set_instance_ids_in_labels_to_zero(element, ids_to_remove)) | ||
|
||
if isinstance(element, DataTree): | ||
# we extract the info to just reconstruct | ||
# the DataTree after filtering the max scale | ||
max_scale = list(element.keys())[0] | ||
scale_factors_temp = _get_scale_factors(element) | ||
scale_factors = [int(sf[0]) for sf in scale_factors_temp] | ||
|
||
return Labels2DModel.parse( | ||
data=_set_instance_ids_in_labels_to_zero(element[max_scale].image, ids_to_remove), | ||
scale_factors=scale_factors, | ||
) | ||
raise ValueError(f"Unknown element type: {type(element)}") |
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,42 @@ | ||
import numpy as np | ||
|
||
from spatialdata._core.query.masking import filter_labels2dmodel_by_instance_ids, filter_shapesmodel_by_instance_ids | ||
from spatialdata.datasets import blobs_annotating_element | ||
|
||
|
||
def test_filter_labels2dmodel_by_instance_ids(): | ||
sdata = blobs_annotating_element("blobs_labels") | ||
labels_element = sdata["blobs_labels"] | ||
all_instance_ids = sdata.tables["table"].obs["instance_id"].unique() | ||
filtered_labels_element = filter_labels2dmodel_by_instance_ids(labels_element, [2, 3]) | ||
|
||
# because 0 is the background, we expect the filtered ids to be the instance ids that are not 0 | ||
filtered_ids = set(np.unique(filtered_labels_element.data.compute())) - { | ||
0, | ||
} | ||
preserved_ids = np.unique(labels_element.data.compute()) | ||
assert filtered_ids == (set(all_instance_ids) - {2, 3}) | ||
# check if there is modification of the original labels | ||
assert set(preserved_ids) == set(all_instance_ids) | {0} | ||
|
||
sdata.tables["table"].uns["spatialdata_attrs"]["region"] = "blobs_multiscale_labels" | ||
sdata.tables["table"].obs.region = "blobs_multiscale_labels" | ||
labels_element = sdata["blobs_multiscale_labels"] | ||
filtered_labels_element = filter_labels2dmodel_by_instance_ids(labels_element, [2, 3]) | ||
|
||
for scale in labels_element: | ||
filtered_ids = set(np.unique(filtered_labels_element[scale].image.compute())) - { | ||
0, | ||
} | ||
preserved_ids = np.unique(labels_element[scale].image.compute()) | ||
assert filtered_ids == (set(all_instance_ids) - {2, 3}) | ||
# check if there is modification of the original labels | ||
assert set(preserved_ids) == set(all_instance_ids) | {0} | ||
|
||
|
||
def test_filter_shapesmodel_by_instance_ids(): | ||
sdata = blobs_annotating_element("blobs_circles") | ||
shapes_element = sdata["blobs_circles"] | ||
filtered_shapes_element = filter_shapesmodel_by_instance_ids(shapes_element, [2, 3]) | ||
|
||
assert set(filtered_shapes_element.index.tolist()) == {0, 1, 4} |
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.