forked from scverse/spatialdata-plot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
85 lines (55 loc) · 2.86 KB
/
utils.py
File metadata and controls
85 lines (55 loc) · 2.86 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
from collections import OrderedDict
import matplotlib
import spatialdata as sd
from matplotlib.colors import LinearSegmentedColormap, ListedColormap
from spatialdata.transformations import get_transformation
def _get_linear_colormap(colors: list[str], background: str) -> list[matplotlib.colors.LinearSegmentedColormap]:
return [LinearSegmentedColormap.from_list(c, [background, c], N=256) for c in colors]
def _get_listed_colormap(color_dict: dict[str, str]) -> matplotlib.colors.ListedColormap:
sorted_labels = sorted(color_dict.keys())
colors = [color_dict[k] for k in sorted_labels]
cmap = ListedColormap(["black"] + colors, N=len(colors) + 1)
return cmap
def _get_region_key(sdata: sd.SpatialData) -> str:
"""Quick access to the data's region key."""
if not hasattr(sdata, "table"):
raise ValueError("SpatialData object does not have a table.")
region_key = str(sdata.table.uns["spatialdata_attrs"]["region_key"])
return region_key
def _get_instance_key(sdata: sd.SpatialData) -> str:
"""Quick access to the data's instance key."""
if not hasattr(sdata, "table"):
raise ValueError("SpatialData object does not have a table.")
instance_key = str(sdata.table.uns["spatialdata_attrs"]["instance_key"])
return instance_key
def _verify_plotting_tree(sdata: sd.SpatialData) -> sd.SpatialData:
"""Verify that the plotting tree exists, and if not, create it."""
if not hasattr(sdata, "plotting_tree"):
sdata.plotting_tree = OrderedDict()
return sdata
def _get_coordinate_system_mapping(sdata: sd.SpatialData) -> dict[str, list[str]]:
has_images = hasattr(sdata, "images")
has_labels = hasattr(sdata, "labels")
has_shapes = hasattr(sdata, "shapes")
coordsys_keys = sdata.coordinate_systems
image_keys = sdata.images.keys() if has_images else []
label_keys = sdata.labels.keys() if has_labels else []
shape_keys = sdata.shapes.keys() if has_shapes else []
mapping: dict[str, list[str]] = {}
if len(coordsys_keys) < 1:
raise ValueError("SpatialData object must have at least one coordinate system to generate a mapping.")
for key in coordsys_keys:
mapping[key] = []
for image_key in image_keys:
transformations = get_transformation(sdata.images[image_key], get_all=True)
if key in list(transformations.keys()):
mapping[key].append(image_key)
for label_key in label_keys:
transformations = get_transformation(sdata.labels[label_key], get_all=True)
if key in list(transformations.keys()):
mapping[key].append(label_key)
for shape_key in shape_keys:
transformations = get_transformation(sdata.shapes[shape_key], get_all=True)
if key in list(transformations.keys()):
mapping[key].append(shape_key)
return mapping