-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
85 lines (68 loc) · 2.28 KB
/
util.py
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
#!/usr/bin/env python3
import string
import anndata as ad
import dask.array as da
import napari
from fractal_tasks_core.ngff import load_NgffImageMeta
from fractal_tasks_core.ngff.zarr_utils import load_NgffPlateMeta
from zarr.errors import PathNotFoundError
def alpha_to_numeric(alpha: str) -> int:
"""Return the position of a single character in the alphabet
Args:
alpha: Single alphabet character
Returns:
Integer position in the alphabet
"""
return ord(alpha.upper()) - 64
def numeric_to_alpha(numeric: int, upper: bool = True) -> str:
"""Return the upper or lowercase character for a given position in the alphabet
Args:
numeric: Integer position in the alphabet
Returns:
Single alphabet character
"""
if upper:
string.ascii_uppercase[numeric - 1]
else:
string.ascii_lowercase[numeric - 1]
def add_features_to_labels(
zarr_url: str,
labels_layer: napari.layers.Labels,
feature_name: str = "regionprops",
):
"""Add features to napari labels layer
Args:
zarr_url: Path to the OME-ZARR
labels_layer: napari labels layer
feature_name: Folder name of the measured regionprobs features
"""
try:
ann_tbl = ad.read_zarr(f"{zarr_url}/tables/{feature_name}/")
labels_layer.features = ann_tbl.to_df()
labels_layer.predictions = ann_tbl.obs
except PathNotFoundError:
pass
def calculate_well_positions(plate_url, row, col, is_plate=True):
dataset = 0
level = 0
zarr_url = f"{plate_url}/{row}/{col}/{dataset}"
shape = da.from_zarr(f"{zarr_url}/{level}").shape[-2:]
image_meta = load_NgffImageMeta(zarr_url)
scale = image_meta.get_pixel_sizes_zyx(level=level)[-2:]
plate_meta = load_NgffPlateMeta(plate_url)
rows = [x.name for x in plate_meta.plate.rows]
cols = [x.name for x in plate_meta.plate.columns]
row = rows.index(row)
col = cols.index(col)
if is_plate:
top_left_corner = [
row * scale[0] * shape[0],
col * scale[1] * shape[1],
]
else:
top_left_corner = [0, 0]
bottom_right_corner = [
top_left_corner[0] + scale[0] * shape[0],
top_left_corner[1] + scale[1] * shape[1],
]
return top_left_corner, bottom_right_corner