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

add spatialdata testdata #10

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2,640 changes: 2,208 additions & 432 deletions poetry.lock

Large diffs are not rendered by default.

12 changes: 7 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ license = "BSD-3"
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.9"
python = ">=3.9,<3.13"
Copy link
Member

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.

Copy link
Author

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 the xarray version in SpatialData

Copy link
Author

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no due to following:

Check your dependencies Python requirement: The Python requirement can be specified via the `python` or `markers` properties
    
    For xarray-dataclasses, a possible solution would be to set the `python` property to ">=3.9,<3.13"

botocore = "1.31.17" # Setting this version is not a hard requirement but reduces resolving time to less than 1 min.
spatialdata = "^0.0.15" # includes numpy, zarr, h5py etc..
linkml-runtime = ">=1.7.0"
numpy = ">=1.24.3"
h5py = ">=3.9.0"
zarr = ">=2.16.1"
nptyping = ">=2.5.0"
xarray = "^2024.1.1"
tox = "^3.25.1" # TODO move out of main deps

[tool.poetry.dev-dependencies]
Expand Down Expand Up @@ -45,6 +43,10 @@ black = "^24.1.1"
pytest = "^7.1.2"
mypy = "^1.8.0"

[tool.pytest.ini_options]
testpaths = ["tests"]
xfail_strict = true

[tool.poetry-dynamic-versioning]
enable = true
vcs = "git"
Expand Down
248 changes: 248 additions & 0 deletions tests/conftest.py
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
)
5 changes: 3 additions & 2 deletions tests/test_dumpers/array_classes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from __future__ import annotations
import numpy as np
from pydantic import BaseModel as BaseModel, ConfigDict, Field

import numpy as np
from pydantic import BaseModel as BaseModel
from pydantic import ConfigDict, Field

metamodel_version = "None"
version = "None"
Expand Down
1 change: 1 addition & 0 deletions tests/test_spatialdata/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Test for writing and reading SpatialData using pytest fixture."""
19 changes: 19 additions & 0 deletions tests/test_spatialdata/test_spatialdata_temp.py
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)