Skip to content
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

Rasterize shapes #566

Merged
merged 27 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
fbad5b8
minimal rasterize structure for shapes
quentinblampey May 22, 2024
0682748
test rasterize
quentinblampey May 24, 2024
31d4f48
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 24, 2024
583afa8
rename squares -> box
quentinblampey May 24, 2024
4953da3
Merge branch 'rasterize' of https://github.com/quentinblampey/spatial…
quentinblampey May 24, 2024
2f42b52
fix mypy and use python>=3.10 typings
quentinblampey May 24, 2024
bdc15e0
fix docstrings and support points
quentinblampey May 24, 2024
2c3e002
Merge branch 'scverse:main' into rasterize
quentinblampey May 28, 2024
340dbbf
save label_index_to_category mapping dict
quentinblampey May 28, 2024
ef386ba
remove column drop, since using view of the data
quentinblampey May 28, 2024
edc9220
fix docs
LucaMarconato May 28, 2024
d690e6e
Merge branch 'main' into rasterize
LucaMarconato May 29, 2024
30483b9
reworking docstring
LucaMarconato May 29, 2024
cf2e961
add tests points
quentinblampey May 30, 2024
012096f
wip
LucaMarconato May 30, 2024
d3a8eab
Merge branch 'rasterize' of https://github.com/quentinblampey/spatial…
LucaMarconato May 30, 2024
da718c0
remove instance_key_as_default_value_key
quentinblampey May 30, 2024
6c975c1
fix tests pre release install
LucaMarconato Jun 2, 2024
1a71b42
refactoring
LucaMarconato Jun 2, 2024
7ecec2c
tests for alternative calls of rasterize(); refactor relational_query
LucaMarconato Jun 2, 2024
8b415ce
fix type checking
LucaMarconato Jun 2, 2024
806da6a
using to polygons
quentinblampey Jun 3, 2024
057ad86
Merge branch 'rasterize' of https://github.com/quentinblampey/spatial…
quentinblampey Jun 3, 2024
6bb04e6
labels now accept table annotations
LucaMarconato Jun 7, 2024
9d4db8a
testing rasterize for points and shapes when index is str
LucaMarconato Jun 8, 2024
99dfb77
testing rasterize() for SpatialData objects
LucaMarconato Jun 8, 2024
9f8575f
changelog
LucaMarconato Jun 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/spatialdata/_core/operations/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from multiscale_spatial_image import MultiscaleSpatialImage
from spatial_image import SpatialImage

from spatialdata.models import SpatialElement

if TYPE_CHECKING:
from spatialdata._core.spatialdata import SpatialData

Expand Down Expand Up @@ -134,3 +136,21 @@ def transform_to_data_extent(
for k, v in sdata.tables.items():
sdata_to_return_elements[k] = v.copy()
return SpatialData.from_elements_dict(sdata_to_return_elements)


def _parse_element(
element: str | SpatialElement, sdata: SpatialData | None, element_var_name: str, sdata_var_name: str
) -> SpatialElement:
if not ((sdata is not None and isinstance(element, str)) ^ (not isinstance(element, str))):
raise ValueError(
f"To specify the {element_var_name!r} SpatialElement, please do one of the following: "
f"- either pass a SpatialElement to the {element_var_name!r} parameter (and keep "
f"`{sdata_var_name}` = None);"
f"- either `{sdata_var_name}` needs to be a SpatialData object, and {element_var_name!r} needs "
f"to be the string name of the element."
)
if sdata is not None:
assert isinstance(element, str)
return sdata[element]
assert element is not None
return element
31 changes: 9 additions & 22 deletions src/spatialdata/_core/operations/aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from spatial_image import SpatialImage
from xrspatial import zonal_stats

from spatialdata._core.operations._utils import _parse_element
from spatialdata._core.operations.transform import transform
from spatialdata._core.query.relational_query import get_values
from spatialdata._core.spatialdata import SpatialData
Expand All @@ -26,7 +27,6 @@
Labels2DModel,
PointsModel,
ShapesModel,
SpatialElement,
TableModel,
get_model,
)
Expand All @@ -35,22 +35,6 @@
__all__ = ["aggregate"]


def _parse_element(element: str | SpatialElement, sdata: SpatialData | None, str_for_exception: str) -> SpatialElement:
if not ((sdata is not None and isinstance(element, str)) ^ (not isinstance(element, str))):
raise ValueError(
f"To specify the {str_for_exception!r} SpatialElement, please do one of the following: "
f"- either pass a SpatialElement to the {str_for_exception!r} parameter (and keep "
f"`{str_for_exception}_sdata` = None);"
f"- either `{str_for_exception}_sdata` needs to be a SpatialData object, and {str_for_exception!r} needs "
f"to be the string name of the element."
)
if sdata is not None:
assert isinstance(element, str)
return sdata[element]
assert element is not None
return element


def aggregate(
values: ddf.DataFrame | gpd.GeoDataFrame | SpatialImage | MultiscaleSpatialImage | str,
by: gpd.GeoDataFrame | SpatialImage | MultiscaleSpatialImage | str,
Expand Down Expand Up @@ -90,8 +74,9 @@ def aggregate(
The key can be:

- the name of a column(s) in the dataframe (Dask `DataFrame` for points or `GeoDataFrame` for shapes);
- the name of obs column(s) in the associated `AnnData` table (for shapes and labels);
- the name of a var(s), referring to the column(s) of the X matrix in the table (for shapes and labels).
- the name of obs column(s) in the associated `AnnData` table (for points, shapes and labels);
- the name of a var(s), referring to the column(s) of the X matrix in the table (for points, shapes and
labels).

If nothing is passed here, it defaults to the equivalent of a column of ones.
Defaults to `FEATURE_KEY` for points (if present).
Expand Down Expand Up @@ -127,7 +112,7 @@ def aggregate(
Whether to deepcopy the shapes in the returned `SpatialData` object. If the shapes are large (e.g. large
multiscale labels), you may consider disabling the deepcopy to use a lazy Dask representation.
table_name
The table optionally containing the value_key and the name of the table in the returned `SpatialData` object.
The table optionally containing the `value_key` and the name of the table in the returned `SpatialData` object.
buffer_resolution
Resolution parameter to pass to the of the .buffer() method to convert circles to polygons. A higher value
results in a more accurate representation of the circle, but also in a more complex polygon and computation.
Expand All @@ -154,8 +139,10 @@ def aggregate(
to a large memory usage. This Github issue https://github.com/scverse/spatialdata/issues/210 keeps track of the
changes required to address this behavior.
"""
values_ = _parse_element(element=values, sdata=values_sdata, str_for_exception="values")
by_ = _parse_element(element=by, sdata=by_sdata, str_for_exception="by")
values_ = _parse_element(
element=values, sdata=values_sdata, element_var_name="values", sdata_var_name="values_sdata"
)
by_ = _parse_element(element=by, sdata=by_sdata, element_var_name="by", sdata_var_name="by_sdata")

if values_ is by_:
# this case breaks the groupy aggregation in _aggregate_shapes(), probably a non relevant edge case so
Expand Down
Loading
Loading