-
Notifications
You must be signed in to change notification settings - Fork 1
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
add spatialdata testdata #10
Open
melonora
wants to merge
6
commits into
linkml:main
Choose a base branch
from
melonora:sdata_blobs
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
6 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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,248 @@ | ||
"""Pytest fixture and helper functions to create a SpatialData object during test time.""" | ||
|
||
from typing import Union | ||
|
||
import numpy as np | ||
import pandas as pd | ||
import pyarrow as pa | ||
import pytest | ||
from anndata import AnnData | ||
from geopandas import GeoDataFrame | ||
from multiscale_spatial_image import MultiscaleSpatialImage | ||
from numpy.random import default_rng | ||
from shapely.geometry import MultiPolygon, Point, Polygon | ||
from spatial_image import SpatialImage | ||
from spatialdata import SpatialData | ||
from spatialdata.models import ( | ||
Image2DModel, | ||
Image3DModel, | ||
Labels2DModel, | ||
Labels3DModel, | ||
PointsModel, | ||
ShapesModel, | ||
TableModel, | ||
) | ||
from xarray import DataArray | ||
|
||
# Added from https://github.com/scverse/spatialdata-plot | ||
|
||
RNG = default_rng() | ||
|
||
|
||
@pytest.fixture() | ||
def full_sdata() -> SpatialData: | ||
"""Create SpatialData object.""" | ||
return SpatialData( | ||
images=_get_images(), | ||
labels=_get_labels(), | ||
shapes=_get_shapes(), | ||
points=_get_points(), | ||
table=_get_table(region="sample1"), | ||
) | ||
|
||
|
||
def _get_images() -> dict[str, Union[SpatialImage, MultiscaleSpatialImage]]: | ||
out = {} | ||
dims_2d = ("c", "y", "x") | ||
dims_3d = ("z", "y", "x", "c") | ||
out["image2d"] = Image2DModel.parse( | ||
RNG.normal(size=(3, 64, 64)), dims=dims_2d, c_coords=["r", "g", "b"] | ||
) | ||
out["image2d_multiscale"] = Image2DModel.parse( | ||
RNG.normal(size=(3, 64, 64)), scale_factors=[2, 2], dims=dims_2d, c_coords=["r", "g", "b"] | ||
) | ||
out["image2d_xarray"] = Image2DModel.parse( | ||
DataArray(RNG.normal(size=(3, 64, 64)), dims=dims_2d), dims=None | ||
) | ||
out["image2d_multiscale_xarray"] = Image2DModel.parse( | ||
DataArray(RNG.normal(size=(3, 64, 64)), dims=dims_2d), | ||
scale_factors=[2, 4], | ||
dims=None, | ||
) | ||
out["image3d_numpy"] = Image3DModel.parse(RNG.normal(size=(2, 64, 64, 3)), dims=dims_3d) | ||
out["image3d_multiscale_numpy"] = Image3DModel.parse( | ||
RNG.normal(size=(2, 64, 64, 3)), scale_factors=[2], dims=dims_3d | ||
) | ||
out["image3d_xarray"] = Image3DModel.parse( | ||
DataArray(RNG.normal(size=(2, 64, 64, 3)), dims=dims_3d), dims=None | ||
) | ||
out["image3d_multiscale_xarray"] = Image3DModel.parse( | ||
DataArray(RNG.normal(size=(2, 64, 64, 3)), dims=dims_3d), | ||
scale_factors=[2], | ||
dims=None, | ||
) | ||
return out | ||
|
||
|
||
def _get_labels() -> dict[str, Union[SpatialImage, MultiscaleSpatialImage]]: | ||
out = {} | ||
dims_2d = ("y", "x") | ||
dims_3d = ("z", "y", "x") | ||
|
||
out["labels2d"] = Labels2DModel.parse(RNG.integers(0, 100, size=(64, 64)), dims=dims_2d) | ||
out["labels2d_multiscale"] = Labels2DModel.parse( | ||
RNG.integers(0, 100, size=(64, 64)), scale_factors=[2, 4], dims=dims_2d | ||
) | ||
out["labels2d_xarray"] = Labels2DModel.parse( | ||
DataArray(RNG.integers(0, 100, size=(64, 64)), dims=dims_2d), dims=None | ||
) | ||
out["labels2d_multiscale_xarray"] = Labels2DModel.parse( | ||
DataArray(RNG.integers(0, 100, size=(64, 64)), dims=dims_2d), | ||
scale_factors=[2, 4], | ||
dims=None, | ||
) | ||
out["labels3d_numpy"] = Labels3DModel.parse( | ||
RNG.integers(0, 100, size=(10, 64, 64)), dims=dims_3d | ||
) | ||
out["labels3d_multiscale_numpy"] = Labels3DModel.parse( | ||
RNG.integers(0, 100, size=(10, 64, 64)), scale_factors=[2, 4], dims=dims_3d | ||
) | ||
out["labels3d_xarray"] = Labels3DModel.parse( | ||
DataArray(RNG.integers(0, 100, size=(10, 64, 64)), dims=dims_3d), dims=None | ||
) | ||
out["labels3d_multiscale_xarray"] = Labels3DModel.parse( | ||
DataArray(RNG.integers(0, 100, size=(10, 64, 64)), dims=dims_3d), | ||
scale_factors=[2, 4], | ||
dims=None, | ||
) | ||
return out | ||
|
||
|
||
def _get_polygons() -> dict[str, GeoDataFrame]: | ||
out = {} | ||
poly = GeoDataFrame( | ||
{ | ||
"geometry": [ | ||
Polygon(((0, 0), (0, 1), (1, 1), (1, 0))), | ||
Polygon(((0, 0), (0, -1), (-1, -1), (-1, 0))), | ||
Polygon(((0, 0), (0, 1), (1, 10))), | ||
Polygon(((0, 0), (0, 1), (1, 1))), | ||
Polygon(((0, 0), (0, 1), (1, 1), (1, 0), (1, 0))), | ||
] | ||
} | ||
) | ||
|
||
multipoly = GeoDataFrame( | ||
{ | ||
"geometry": [ | ||
MultiPolygon( | ||
[ | ||
Polygon(((0, 0), (0, 1), (1, 1), (1, 0))), | ||
Polygon(((0, 0), (0, -1), (-1, -1), (-1, 0))), | ||
] | ||
), | ||
MultiPolygon( | ||
[ | ||
Polygon(((0, 0), (0, 1), (1, 10))), | ||
Polygon(((0, 0), (0, 1), (1, 1))), | ||
Polygon(((0, 0), (0, 1), (1, 1), (1, 0), (1, 0))), | ||
] | ||
), | ||
] | ||
} | ||
) | ||
|
||
out["poly"] = ShapesModel.parse(poly, name="poly") | ||
out["multipoly"] = ShapesModel.parse(multipoly, name="multipoly") | ||
|
||
return out | ||
|
||
|
||
def _get_shapes() -> dict[str, AnnData]: | ||
out = {} | ||
poly = GeoDataFrame( | ||
{ | ||
"geometry": [ | ||
Polygon(((0, 0), (0, 1), (1, 1), (1, 0))), | ||
Polygon(((0, 0), (0, -1), (-1, -1), (-1, 0))), | ||
Polygon(((0, 0), (0, 1), (1, 10))), | ||
Polygon(((10, 10), (10, 20), (20, 20))), | ||
Polygon(((0, 0), (0, 1), (1, 1), (1, 0), (1, 0))), | ||
] | ||
} | ||
) | ||
|
||
multipoly = GeoDataFrame( | ||
{ | ||
"geometry": [ | ||
MultiPolygon( | ||
[ | ||
Polygon(((0, 0), (0, 1), (1, 1), (1, 0))), | ||
Polygon(((0, 0), (0, -1), (-1, -1), (-1, 0))), | ||
] | ||
), | ||
MultiPolygon( | ||
[ | ||
Polygon(((0, 0), (0, 1), (1, 10))), | ||
Polygon(((0, 0), (1, 0), (1, 1))), | ||
] | ||
), | ||
] | ||
} | ||
) | ||
|
||
points = GeoDataFrame( | ||
{ | ||
"geometry": [ | ||
Point((0, 1)), | ||
Point((1, 1)), | ||
Point((3, 4)), | ||
Point((4, 2)), | ||
Point((5, 6)), | ||
] | ||
} | ||
) | ||
rng = np.random.default_rng(seed=0) | ||
points["radius"] = np.abs(rng.normal(size=(len(points), 1))) | ||
|
||
out["poly"] = ShapesModel.parse(poly) | ||
out["poly"].index = [0, 1, 2, 3, 4] | ||
out["multipoly"] = ShapesModel.parse(multipoly) | ||
out["circles"] = ShapesModel.parse(points) | ||
|
||
return out | ||
|
||
|
||
def _get_points() -> dict[str, pa.Table]: | ||
name = "points" | ||
out = {} | ||
for i in range(2): | ||
name = f"{name}_{i}" | ||
arr = RNG.normal(size=(300, 2)) | ||
# randomly assign some values from v to the points | ||
points_assignment0 = RNG.integers(0, 10, size=arr.shape[0]).astype(np.int_) | ||
if i == 0: | ||
genes = RNG.choice(["a", "b"], size=arr.shape[0]) | ||
else: | ||
genes = np.tile(np.array(list(map(str, range(280)))), 2)[:300] | ||
annotation = pd.DataFrame( | ||
{ | ||
"genes": genes, | ||
"instance_id": points_assignment0, | ||
}, | ||
) | ||
out[name] = PointsModel.parse( | ||
arr, annotation=annotation, feature_key="genes", instance_key="instance_id" | ||
) | ||
return out | ||
|
||
|
||
def _get_table( | ||
region: None | str | list[str] = "sample1", | ||
region_key: None | str = "region", | ||
instance_key: None | str = "instance_id", | ||
) -> AnnData: | ||
adata = AnnData( | ||
RNG.normal(size=(100, 10)), | ||
obs=pd.DataFrame(RNG.normal(size=(100, 3)), columns=["a", "b", "c"]), | ||
) | ||
if not all(var for var in (region, region_key, instance_key)): | ||
return TableModel.parse(adata=adata) | ||
adata.obs[instance_key] = np.arange(adata.n_obs) | ||
if isinstance(region, str): | ||
adata.obs[region_key] = region | ||
elif isinstance(region, list): | ||
adata.obs[region_key] = RNG.choice(region, size=adata.n_obs) | ||
return TableModel.parse( | ||
adata=adata, region=region, region_key=region_key, instance_key=instance_key | ||
) |
This file contains 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 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 @@ | ||
"""Test for writing and reading SpatialData using pytest fixture.""" |
This file contains 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,19 @@ | ||
"""Test writing and reading of SpatialData using fixture.""" | ||
|
||
from pathlib import Path | ||
|
||
from spatialdata import SpatialData | ||
|
||
|
||
class TestWriteRead: | ||
"""Test writing and reading using pytest fixture.""" | ||
|
||
def test_write_read( | ||
self, | ||
tmp_path: str, | ||
full_sdata: SpatialData, | ||
) -> None: | ||
"""Test writing and reading a SpatialData object with all elements present.""" | ||
tmpdir = Path(tmp_path) / "tmp.zarr" | ||
full_sdata.write(tmpdir) | ||
SpatialData.read(tmpdir) |
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.
is it possible to get this to work with
^3.9
.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.
At the moment no due to xarray. The dependencies couldn't be resolved when using
^3.9
with thexarray
version inSpatialData
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.
or maybe let me retry because this was before merge
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.
no due to following: