-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimg_util.py
More file actions
418 lines (335 loc) · 10.5 KB
/
img_util.py
File metadata and controls
418 lines (335 loc) · 10.5 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
"""
Created on Sat May 9 11:00:00 2024
@author: Anna Grim
@email: anna.grim@alleninstitute.org
Helper routines for processing images.
"""
from copy import deepcopy
import numpy as np
import tensorstore as ts
from skimage.color import label2rgb
from deep_neurographs.utils import util
SUPPORTED_DRIVERS = ["neuroglancer_precomputed", "n5", "zarr"]
# --- io utils ---
def open_tensorstore(path, driver="neuroglancer_precomputed"):
"""
Opens an image that is assumed to be stored as a directory of shard files.
Parameters
----------
path : str
Path to directory containing shard files.
driver : str, optional
Storage driver needed to read data at "path". The default is
"neuroglancer_precomputed".
Returns
-------
tensorstore.TensorStore
Pointer to image stored at "path" in a GCS bucket.
"""
assert driver in SUPPORTED_DRIVERS, "Driver is not supported!"
img = ts.open(
{
"driver": driver,
"kvstore": {
"driver": "gcs",
"bucket": "allen-nd-goog",
"path": path,
},
"context": {
"cache_pool": {"total_bytes_limit": 1000000000},
"cache_pool#remote": {"total_bytes_limit": 1000000000},
"data_copy_concurrency": {"limit": 8},
},
"recheck_cached_data": "open",
}
).result()
if driver == "neuroglancer_precomputed":
return img[ts.d["channel"][0]]
elif driver == "zarr":
img = img[0, 0, :, :, :]
img = img[ts.d[0].transpose[2]]
img = img[ts.d[0].transpose[1]]
return img
def read(img, voxel, shape, from_center=True):
"""
Reads a chunk of data from an image given a voxel coordinate and shape.
Parameters
----------
img : numpy.ndarray
Image to be read.
voxel : tuple
Voxel coordinate that specifies either the center or top, left, front
corner of the chunk to be read.
shape : tuple
Shape (dimensions) of the chunk to be read.
from_center : bool, optional
Indication of whether the provided coordinates represent the center of
the chunk or the top, left, front corner. The default is True.
Returns
-------
numpy.ndarray
Chunk of data read from an image.
"""
start, end = get_start_end(voxel, shape, from_center=from_center)
return deepcopy(
img[start[0]: end[0], start[1]: end[1], start[2]: end[2]]
)
def read_tensorstore(img, voxel, shape, from_center=True):
"""
Reads a chunk from an image given a voxel coordinate and the desired shape
of the chunk.
Parameters
----------
img : tensorstore.TensorStore
Image to be read.
voxel : tuple
Voxel coordinate that specifies either the center or top, left, front
corner of the chunk to be read.
shape : tuple
Shape (dimensions) of the chunk to be read.
from_center : bool, optional
Indication of whether the provided coordinates represent the center of
the chunk or the starting point. The default is True.
Returns
-------
numpy.ndarray
Chunk of data read from an image.
"""
return read(img, voxel, shape, from_center=from_center).read().result()
def read_tensorstore_with_bbox(img, bbox, normalize=True):
"""
Reads a chunk from a subarray that is determined by "bbox".
Parameters
----------
img : tensorstore.TensorStore
Image to be read.
bbox : dict
Dictionary that contains min and max coordinates of a bounding box.
Returns
-------
numpy.ndarray
Chunk of data read from an image.
"""
try:
shape = [bbox["max"][i] - bbox["min"][i] for i in range(3)]
return read_tensorstore(img, bbox["min"], shape, from_center=False)
except Exception:
return np.zeros(shape)
def read_profile(img, spec):
"""
Reads an intensity profile from an image (i.e. image profile).
Parameters
----------
img : tensorstore.TensorStore
Image to be read.
spec : dict
Dictionary that stores the bounding box of chunk to be read and the
voxel coordinates of the profile path.
Returns
-------
numpy.ndarray
Image profile.
"""
img_patch = normalize(read_tensorstore_with_bbox(img, spec["bbox"]))
return [img_patch[voxel] for voxel in map(tuple, spec["profile_path"])]
def get_start_end(voxel, shape, from_center=True):
"""
Gets the start and end indices of the chunk to be read.
Parameters
----------
voxel : tuple
Voxel coordinate that specifies either the center or top, left, front
corner of the chunk to be read.
shape : tuple
Shape (dimensions) of the chunk to be read.
from_center : bool, optional
Indication of whether the provided coordinates represent the center of
the chunk or the starting point. The default is True.
Return
------
tuple[list[int]]
Start and end indices of the chunk to be read.
"""
if from_center:
start = [voxel[i] - shape[i] // 2 for i in range(3)]
end = [voxel[i] + shape[i] // 2 for i in range(3)]
else:
start = voxel
end = [voxel[i] + shape[i] for i in range(3)]
return start, end
# -- operations --
def normalize(img):
"""
Normalizes an image so that the minimum and maximum intensity values are 0
and 1.
Parameters
----------
img : numpy.ndarray
Image to be normalized.
Returns
-------
numpy.ndarray
Normalized image.
"""
img -= np.min(img)
return img / max(1, np.max(img))
def get_mip(img, axis=0):
"""
Compute the maximum intensity projection (MIP) of "img" along "axis".
Parameters
----------
img : numpy.ndarray
Image to compute MIP of.
axis : int, optional
Projection axis. The default is 0.
Returns
-------
numpy.ndarray
MIP of "img".
"""
return np.max(img, axis=axis)
def get_labels_mip(img, axis=0):
"""
Compute the maximum intensity projection (MIP) of a segmentation along
"axis". This routine differs from "get_mip" because it retuns an rgb
image.
Parameters
----------
img : numpy.ndarray
Image to compute MIP of.
axis : int, optional
Projection axis. The default is 0.
Returns
-------
numpy.ndarray
MIP of "img".
"""
mip = np.max(img, axis=axis)
mip = label2rgb(mip)
return (255 * mip).astype(np.uint8)
def get_profile(img, spec, profile_id):
"""
Gets the image profile for a given proposal.
Parameters
----------
img : tensorstore.TensorStore
Image that profiles are generated from.
spec : dict
Dictionary that contains the image bounding box and coordinates of the
image profile path.
profile_id : frozenset
Identifier of profile.
Returns
-------
dict
Dictionary that maps an id (e.g. node, edge, or proposal) to its image
profile.
"""
profile = read_profile(img, spec)
avg, std = util.get_avg_std(profile)
profile.extend([avg, std])
return {profile_id: profile}
# --- coordinate conversions ---
def to_physical(voxel, anisotropy, shift=[0, 0, 0]):
"""
Converts a voxel coordinate to a physical coordinate by applying the
anisotropy scaling factors.
Parameters
----------
coord : ArrayLike
Coordinate to be converted.
anisotropy : ArrayLike
Image to physical coordinates scaling factors to account for the
anisotropy of the microscope.
shift : ArrayLike, optional
Shift to be applied to "coord". The default is [0, 0, 0].
Returns
-------
tuple
Converted coordinates.
"""
return tuple([voxel[i] * anisotropy[i] - shift[i] for i in range(3)])
def to_voxels(xyz, anisotropy, multiscale=0):
"""
Converts coordinates from world to voxel.
Parameters
----------
xyz : ArrayLike
Physical coordiante to be converted to a voxel coordinate.
anisotropy : ArrayLike
Image to physical coordinates scaling factors to account for the
anisotropy of the microscope.
multiscale : int, optional
Level in the image pyramid that the voxel coordinate must index into.
The default is 0.
Returns
-------
numpy.ndarray
Voxel coordinate of the input.
"""
scaling_factor = 1.0 / 2 ** multiscale
voxel = scaling_factor * xyz / np.array(anisotropy)
return np.round(voxel).astype(int)
# -- utils --
def init_bbox(origin, shape):
"""
Gets the min and max coordinates of a bounding box based on "origin" and
"shape".
Parameters
----------
origin : tuple[int]
Voxel coordinate of the origin of the bounding box, which is assumed
to be top-front-left corner.
shape : Tuple[int]
Shape of the bounding box.
Returns
-------
dict or None
Bounding box.
"""
if origin and shape:
origin = np.array(origin)
shape = np.array(shape)
return {"min": origin, "max": origin + shape}
else:
return None
def get_minimal_bbox(voxels, buffer=0):
"""
Gets the min and max coordinates of a bounding box that contains "voxels".
Parameters
----------
voxels : numpy.ndarray
Array containing voxel coordinates.
buffer : int, optional
Constant value added/subtracted from the max/min coordinates of the
bounding box. The default is 0.
Returns
-------
dict
Bounding box.
"""
bbox = {
"min": np.floor(np.min(voxels, axis=0) - buffer).astype(int),
"max": np.ceil(np.max(voxels, axis=0) + buffer).astype(int),
}
return bbox
def find_img_path(bucket_name, img_root, dataset_name):
"""
Find the path of a specific dataset in a GCS bucket.
Parameters:
----------
bucket_name : str
Name of the GCS bucket where the images are stored.
img_root : str
Root directory path in the GCS bucket where the images are located.
dataset_name : str
Name of the dataset to be searched for within the subdirectories.
Returns:
-------
str
Path of the found dataset subdirectory within the specified GCS bucket.
"""
for subdir in util.list_gcs_subdirectories(bucket_name, img_root):
if dataset_name in subdir:
return subdir + "whole-brain/fused.zarr/"
raise f"Dataset not found in {bucket_name} - {img_root}"