-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathconftest.py
More file actions
497 lines (411 loc) · 16.7 KB
/
conftest.py
File metadata and controls
497 lines (411 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
from __future__ import annotations
import dask
dask.config.set({"dataframe.query-planning": False})
from collections.abc import Sequence
from pathlib import Path
from typing import Any
import dask.dataframe as dd
import geopandas as gpd
import numpy as np
import pandas as pd
import pytest
from anndata import AnnData
from dask.dataframe import DataFrame as DaskDataFrame
from geopandas import GeoDataFrame
from numpy.random import default_rng
from scipy import ndimage as ndi
from shapely import linearrings, polygons
from shapely.geometry import MultiPolygon, Point, Polygon
from skimage import data
from xarray import DataArray, DataTree
from spatialdata._core._deepcopy import deepcopy
from spatialdata._core.spatialdata import SpatialData
from spatialdata._types import ArrayLike
from spatialdata.datasets import BlobsDataset
from spatialdata.models import (
Image2DModel,
Image3DModel,
Labels2DModel,
Labels3DModel,
PointsModel,
ShapesModel,
TableModel,
)
SEED = 0
RNG = default_rng(seed=SEED)
POLYGON_PATH = Path(__file__).parent / "data/polygon.json"
MULTIPOLYGON_PATH = Path(__file__).parent / "data/polygon.json"
POINT_PATH = Path(__file__).parent / "data/points.json"
@pytest.fixture()
def images() -> SpatialData:
return SpatialData(images=_get_images())
@pytest.fixture()
def labels() -> SpatialData:
return SpatialData(labels=_get_labels())
@pytest.fixture()
def shapes() -> SpatialData:
return SpatialData(shapes=_get_shapes())
@pytest.fixture()
def points() -> SpatialData:
return SpatialData(points=_get_points())
@pytest.fixture()
def table_single_annotation() -> SpatialData:
return SpatialData(tables={"table": _get_table(region="labels2d")})
@pytest.fixture()
def table_multiple_annotations() -> SpatialData:
return SpatialData(tables={"table": _get_table(region=["labels2d", "poly"])})
@pytest.fixture()
def tables() -> list[AnnData]:
_tables = []
for region, region_key, instance_key in (
[None, None, None],
["my_region0", None, "my_instance_key"],
[["my_region0", "my_region1"], "my_region_key", "my_instance_key"],
):
_tables.append(_get_table(region=region, region_key=region_key, instance_key=instance_key))
return _tables
@pytest.fixture()
def full_sdata() -> SpatialData:
return SpatialData(
images=_get_images(),
labels=_get_labels(),
shapes=_get_shapes(),
points=_get_points(),
tables=_get_tables(region="labels2d"),
)
# @pytest.fixture()
# def empty_points() -> SpatialData:
# geo_df = GeoDataFrame(
# geometry=[],
# )
# from spatialdata import NgffIdentity
# _set_transformations(geo_df, NgffIdentity())
#
# return SpatialData(points={"empty": geo_df})
# @pytest.fixture()
# def empty_table() -> SpatialData:
# adata = AnnData(shape=(0, 0), obs=pd.DataFrame(columns="region"), var=pd.DataFrame())
# adata = TableModel.parse(adata=adata)
# return SpatialData(table=adata)
@pytest.fixture(
# params=["labels"]
params=["full", "empty"] + ["images", "labels", "points", "table_single_annotation", "table_multiple_annotations"]
# + ["empty_" + x for x in ["table"]] # TODO: empty table not supported yet
)
def sdata(request) -> SpatialData:
if request.param == "full":
return SpatialData(
images=_get_images(),
labels=_get_labels(),
shapes=_get_shapes(),
points=_get_points(),
tables=_get_tables(region="labels2d"),
)
if request.param == "empty":
return SpatialData()
return request.getfixturevalue(request.param)
def _get_images() -> dict[str, DataArray | DataTree]:
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, DataArray | DataTree]:
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_shapes() -> dict[str, GeoDataFrame]:
# TODO: add polygons from geojson and from ragged arrays since now only the GeoDataFrame initializer is tested.
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=SEED)
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, DaskDataFrame]:
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:
# we need to test the case in which we have a categorical column with more than 127 categories, see full
# explanation in write_points() (the parser will convert this column to a categorical since
# feature_key="genes")
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_tables(
region: None | str | list[str] = "sample1",
region_key: None | str = "region",
instance_key: None | str = "instance_id",
) -> dict[str, AnnData]:
return {"table": _get_table(region=region, region_key=region_key, instance_key=instance_key)}
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)
def _get_new_table(spatial_element: None | str | Sequence[str], instance_id: None | Sequence[Any]) -> AnnData:
adata = AnnData(np.random.default_rng(seed=SEED).random(10, 20000))
return TableModel.parse(adata=adata, spatial_element=spatial_element, instance_id=instance_id)
@pytest.fixture()
def labels_blobs() -> ArrayLike:
"""Create a 2D labels."""
return BlobsDataset()._labels_blobs()
@pytest.fixture()
def sdata_blobs() -> SpatialData:
"""Create a 2D labels."""
from spatialdata.datasets import blobs
return deepcopy(blobs(256, 300, 3))
def _make_points(coordinates: np.ndarray) -> DaskDataFrame:
"""Helper function to make a Points element."""
k0 = int(len(coordinates) / 3)
k1 = len(coordinates) - k0
genes = np.hstack((np.repeat("a", k0), np.repeat("b", k1)))
return PointsModel.parse(coordinates, annotation=pd.DataFrame({"genes": genes}), feature_key="genes")
def _make_squares(centroid_coordinates: np.ndarray, half_widths: list[float]) -> polygons:
linear_rings = []
for centroid, half_width in zip(centroid_coordinates, half_widths, strict=True):
min_coords = centroid - half_width
max_coords = centroid + half_width
linear_rings.append(
linearrings(
[
[min_coords[0], min_coords[1]],
[min_coords[0], max_coords[1]],
[max_coords[0], max_coords[1]],
[max_coords[0], min_coords[1]],
]
)
)
s = polygons(linear_rings)
polygon_series = gpd.GeoSeries(s)
cell_polygon_table = gpd.GeoDataFrame(geometry=polygon_series)
return ShapesModel.parse(cell_polygon_table)
def _make_circles(centroid_coordinates: np.ndarray, radius: list[float]) -> GeoDataFrame:
return ShapesModel.parse(centroid_coordinates, geometry=0, radius=radius)
def _make_sdata_for_testing_querying_and_aggretation() -> SpatialData:
"""
Creates a SpatialData object with many edge cases for testing querying and aggregation.
Returns
-------
The SpatialData object.
Notes
-----
Description of what is tested (for a quick visualization, please plot the returned SpatialData object):
- values to query/aggregate: polygons, points, circles
- values to query by: polygons, circles
- the shapes are completely inside, outside, or intersecting the query region (with the centroid inside or
outside the query region)
Additional cases:
- concave shape intersecting multiple times the same shape; used both as query and as value
- shape intersecting multiple shapes; used both as query and as value
"""
values_centroids_squares = np.array([[x * 18, 0] for x in range(8)] + [[8 * 18 + 7, 0]] + [[0, 90], [50, 90]])
values_centroids_circles = np.array([[x * 18, 30] for x in range(8)] + [[8 * 18 + 7, 30]])
by_centroids_squares = np.array([[119, 15], [100, 90], [150, 90], [210, 15]])
by_centroids_circles = np.array([[24, 15], [290, 15]])
values_points = _make_points(np.vstack((values_centroids_squares, values_centroids_circles)))
values_squares = _make_squares(values_centroids_squares, half_widths=[6] * 9 + [15, 15])
values_circles = _make_circles(values_centroids_circles, radius=[6] * 9)
values_circles["categorical_in_gdf"] = pd.Categorical(["a"] * 9)
values_circles["numerical_in_gdf"] = np.arange(9)
by_squares = _make_squares(by_centroids_squares, half_widths=[30, 15, 15, 30])
by_circles = _make_circles(by_centroids_circles, radius=[30, 30])
from shapely.geometry import Polygon
polygon = Polygon([(100, 90 - 10), (100 + 30, 90), (100, 90 + 10), (150, 90)])
values_squares.loc[len(values_squares)] = [polygon]
ShapesModel.validate(values_squares)
values_squares["categorical_in_gdf"] = pd.Categorical(["a"] * 9 + ["b"] * 3)
values_squares["numerical_in_gdf"] = np.arange(12)
polygon = Polygon([(0, 90 - 10), (0 + 30, 90), (0, 90 + 10), (50, 90)])
by_squares.loc[len(by_squares)] = [polygon]
ShapesModel.validate(by_squares)
s_cat = pd.Series(pd.Categorical(["a"] * 9 + ["b"] * 9 + ["c"] * 2))
s_num = pd.Series(RNG.random(20))
# workaround for https://github.com/dask/dask/issues/11147, let's recompute the dataframe (it's a small one)
values_points = PointsModel.parse(
dd.from_pandas(values_points.compute().assign(categorical_in_ddf=s_cat, numerical_in_ddf=s_num), npartitions=1)
)
sdata = SpatialData(
points={"points": values_points},
shapes={
"values_polygons": values_squares,
"values_circles": values_circles,
"by_polygons": by_squares,
"by_circles": by_circles,
},
)
# generate table
x = RNG.random((21, 1))
region = np.array(["values_circles"] * 9 + ["values_polygons"] * 12)
instance_id = np.array(list(range(9)) + list(range(12)))
categorical_obs = pd.Series(pd.Categorical(["a"] * 9 + ["b"] * 9 + ["c"] * 3))
numerical_obs = pd.Series(RNG.random(21))
table = AnnData(
x,
obs=pd.DataFrame(
{
"region": region,
"instance_id": instance_id,
"categorical_in_obs": categorical_obs,
"numerical_in_obs": numerical_obs,
}
),
var=pd.DataFrame(index=["numerical_in_var"]),
)
table = TableModel.parse(
table, region=["values_circles", "values_polygons"], region_key="region", instance_key="instance_id"
)
sdata.table = table
return sdata
@pytest.fixture()
def sdata_query_aggregation() -> SpatialData:
return _make_sdata_for_testing_querying_and_aggretation()
def generate_adata(n_var: int, obs: pd.DataFrame, obsm: dict[Any, Any], uns: dict[Any, Any]) -> AnnData:
rng = np.random.default_rng(SEED)
return AnnData(
rng.normal(size=(obs.shape[0], n_var)),
obs=obs,
obsm=obsm,
uns=uns,
dtype=np.float64,
)
def _get_blobs_galaxy() -> tuple[ArrayLike, ArrayLike]:
blobs = data.binary_blobs(rng=SEED)
blobs = ndi.label(blobs)[0]
return blobs, data.hubble_deep_field()[: blobs.shape[0], : blobs.shape[0]]
@pytest.fixture
def adata_labels() -> AnnData:
n_var = 50
blobs, _ = _get_blobs_galaxy()
seg = np.unique(blobs)[1:]
n_obs_labels = len(seg)
rng = np.random.default_rng(SEED)
obs_labels = pd.DataFrame(
{
"a": rng.normal(size=(n_obs_labels,)),
"categorical": pd.Categorical(rng.integers(0, 2, size=(n_obs_labels,))),
"cell_id": pd.Categorical(seg),
"instance_id": range(n_obs_labels),
"region": ["test"] * n_obs_labels,
},
index=np.arange(n_obs_labels),
)
uns_labels = {
"spatialdata_attrs": {"region": "test", "region_key": "region", "instance_key": "instance_id"},
}
obsm_labels = {
"tensor": rng.integers(0, blobs.shape[0], size=(n_obs_labels, 2)),
"tensor_copy": rng.integers(0, blobs.shape[0], size=(n_obs_labels, 2)),
}
return generate_adata(n_var, obs_labels, obsm_labels, uns_labels)