forked from KhartesViewer/scroll2zarr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscroll_to_ome.py
More file actions
540 lines (489 loc) · 16.8 KB
/
scroll_to_ome.py
File metadata and controls
540 lines (489 loc) · 16.8 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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
import sys
import re
from pathlib import Path
import json
import shutil
import argparse
import copy
import numpy as np
import tifffile
import zarr
import skimage.transform
from tqdm import tqdm
from concurrent.futures import ThreadPoolExecutor
from multiprocessing import cpu_count
'''
# tiffdir = Path(r"C:\Vesuvius\scroll 1 2000-2030")
# zarrdir = Path(r"C:\Vesuvius\test.zarr")
# tiffdir = Path(r"H:\Vesuvius\Scroll1.volpkg\volumes_masked\20230205180739")
# tiffdir = Path(r"H:\Vesuvius\zarr_tests\masked_subset")
# zarrdir = Path(r"H:\Vesuvius\zarr_tests\testzo.zarr")
# zarrdir = Path(r"H:\Vesuvius\testzc.zarr")
# tif files, ome dir
# set chunk_size
chunk_size = 128
# slices = (None, None, slice(1975,2010))
# slices = (slice(2000,2500), slice(2000,2512), slice(1975,2010))
slices = (slice(2000,2500), slice(2000,2512), slice(1975,2005))
maxgb = None
nlevels = 6
zarr_only = False
first_new_level = 0
# maxgb = .0036
'''
# create ome dir, .zattrs, .zgroup
# (don't need to know output array dimensions, just number of levels,
# possibly unit/dimension info)
# create_ome_dir(zarrdir, nlevels)
# quit if dir already exists
# tifs2zarr(tiffdir, zarrdir+"/0", chunk_size, range(optional))
def parseSlices(istr):
sstrs = istr.split(",")
if len(sstrs) != 3:
print("Could not parse ranges argument '%s'; expected 3 comma-separated ranges"%istr)
return None
slices = []
for sstr in sstrs:
if sstr == "":
slices.append(None)
continue
parts = sstr.split(':')
if len(parts) == 1:
slices.append(slice(int(parts[0])))
else:
iparts = [None if p=="" else int(p) for p in parts]
if len(iparts)==2:
iparts.append(None)
slices.append(slice(iparts[0], iparts[1], iparts[2]))
return slices
# return None if succeeds, err string if fails
def create_ome_dir(zarrdir):
# complain if directory already exists
if zarrdir.exists():
err = "Directory %s already exists"%zarrdir
print(err)
return err
try:
zarrdir.mkdir()
except Exception as e:
err = "Error while creating %s: %s"%(zarrdir, e)
print(err)
return err
def create_ome_headers(zarrdir, nlevels):
zattrs_dict = {
"multiscales": [
{
"axes": [
{
"name": "z",
"type": "space"
},
{
"name": "y",
"type": "space"
},
{
"name": "x",
"type": "space"
}
],
"datasets": [],
"name": "/",
"version": "0.4"
}
]
}
dataset_dict = {
"coordinateTransformations": [
{
"scale": [
],
"type": "scale"
}
],
"path": ""
}
zgroup_dict = { "zarr_format": 2 }
datasets = []
for l in range(nlevels):
ds = copy.deepcopy(dataset_dict)
ds["path"] = "%d"%l
scale = 2.**l
ds["coordinateTransformations"][0]["scale"] = [scale]*3
# print(json.dumps(ds, indent=4))
datasets.append(ds)
zad = copy.deepcopy(zattrs_dict)
zad["multiscales"][0]["datasets"] = datasets
json.dump(zgroup_dict, (zarrdir / ".zgroup").open("w"), indent=4)
json.dump(zad, (zarrdir / ".zattrs").open("w"), indent=4)
def slice_step_is_1(s):
if s is None:
return True
if s.step is None:
return True
if s.step == 1:
return True
return False
def slice_start(s):
if s.start is None:
return 0
return s.start
def slice_count(s, maxx):
mn = s.start
if mn is None:
mn = 0
mn = max(0, mn)
mx = s.stop
if mx is None:
mx = maxx
mx = min(mx, maxx)
return mx-mn
def tifs2zarr(tiffdir, zarrdir, chunk_size, obytes=0, slices=None, maxgb=None):
if slices is None:
xslice = yslice = zslice = None
else:
xslice, yslice, zslice = slices
if not all([slice_step_is_1(s) for s in slices]):
err = "All slice steps must be 1 in slices"
print(err)
return err
# Note this is a generator, not a list
tiffs = tiffdir.glob("*.tif")
rec = re.compile(r'([0-9]+)\.\w+$')
# rec = re.compile(r'[0-9]+$')
inttiffs = {}
for tiff in tiffs:
tname = tiff.name
match = rec.match(tname)
if match is None:
continue
# Look for last match (closest to end of file name)
# ds = match[-1]
ds = match.group(1)
itiff = int(ds)
if itiff in inttiffs:
err = "File %s: tiff id %d already used"%(tname,itiff)
print(err)
return err
inttiffs[itiff] = tiff
if len(inttiffs) == 0:
err = "No tiffs found"
print(err)
return err
itiffs = list(inttiffs.keys())
itiffs.sort()
z0 = 0
if zslice is not None:
maxz = itiffs[-1]+1
valid_zs = range(maxz)[zslice]
itiffs = list(filter(lambda z: z in valid_zs, itiffs))
# z0 = itiffs[0]
if zslice.start is None:
z0 = 0
else:
z0 = zslice.start
# for testing
# itiffs = itiffs[2048:2048+256]
minz = itiffs[0]
maxz = itiffs[-1]
cz = maxz-z0+1
try:
tiff0 = tifffile.imread(inttiffs[minz])
except Exception as e:
err = "Error reading %s: %s"%(inttiffs[minz],e)
print(err)
return err
ny0, nx0 = tiff0.shape
dt0 = tiff0.dtype
otype = tiff0.dtype
divisor = 1
if obytes == 1 and dt0 == np.uint16:
print("Converting from uint16 in input to uint8 in output")
otype = np.uint8
divisor = 256
elif obytes != 0 and dt0.itemsize != obytes:
err = "Cannot perform pixel conversion from %s to %d bytes"%(dt0, obytes)
print(err)
return err
else:
print("Byte conversion: none")
print("tiff size", nx0, ny0, "z range", minz, maxz)
cx = nx0
cy = ny0
x0 = 0
y0 = 0
if xslice is not None:
cx = slice_count(xslice, nx0)
x0 = slice_start(xslice)
if yslice is not None:
cy = slice_count(yslice, ny0)
y0 = slice_start(yslice)
print("cx,cy,cz",cx,cy,cz)
print("x0,y0,z0",x0,y0,z0)
store = zarr.NestedDirectoryStore(zarrdir)
tzarr = zarr.open(
store=store,
shape=(cz, cy, cx),
chunks=(chunk_size, chunk_size, chunk_size),
dtype = otype,
write_empty_chunks=False,
fill_value=0,
compressor=None,
mode='w',
)
# nb of chunks in y direction that fit inside of max_gb
chy = cy // chunk_size + 1
if maxgb is not None:
maxy = int((maxgb*10**9)/(cx*chunk_size*dt0.itemsize))
chy = maxy // chunk_size
chy = max(1, chy)
# nb of y chunk groups
ncgy = cy // (chunk_size*chy) + 1
print("chy, ncgy", chy, ncgy)
buf = np.zeros((chunk_size, min(cy, chy*chunk_size), cx), dtype=dt0)
for icy in range(ncgy):
ys = icy*chy*chunk_size
ye = ys+chy*chunk_size
ye = min(ye, cy)
if ye == ys:
break
prev_zc = -1
for itiff in itiffs:
z = itiff-z0
tiffname = inttiffs[itiff]
try:
print("reading",itiff," ", end='\r')
# print("reading",itiff)
tarr = tifffile.imread(tiffname)
except Exception as e:
print("\nError reading",tiffname,":",e)
# If reading fails (file missing or deformed)
tarr = np.zeros((ny, nx), dtype=dt0)
# print("done reading",itiff, end='\r')
# tzarr[itiff,:,:] = tarr
ny, nx = tarr.shape
if nx != nx0 or ny != ny0:
print("\nFile %s is the wrong shape (%d, %d); expected %d, %d"%(tiffname,nx,ny,nx0,ny0))
continue
if xslice is not None and yslice is not None:
tarr = tarr[yslice, xslice]
cur_zc = z // chunk_size
if cur_zc != prev_zc:
if prev_zc >= 0:
zs = prev_zc*chunk_size
ze = zs+chunk_size
if ncgy == 1:
print("\nwriting, z range %d,%d"%(zs+z0, ze+z0))
else:
print("\nwriting, z range %d,%d y range %d,%d"%(zs+z0, ze+z0, ys+y0, ye+y0))
tzarr[zs:z,ys:ye,:] = buf[:ze-zs,:ye-ys,:]
buf[:,:,:] = 0
prev_zc = cur_zc
cur_bufz = z-cur_zc*chunk_size
# print("cur_bufzk,ye,ys", cur_bufz,ye,ys)
buf[cur_bufz,:ye-ys,:] = tarr[ys:ye,:] // divisor
if prev_zc >= 0:
zs = prev_zc*chunk_size
ze = zs+chunk_size
ze = min(itiffs[-1]+1-z0, ze)
if ze > zs:
if ncgy == 1:
print("\nwriting, z range %d,%d"%(zs+z0, ze+z0))
else:
print("\nwriting, z range %d,%d y range %d,%d"%(zs+z0, ze+z0, ys+y0, ye+y0))
# print("\nwriting (end)", zs, ze)
# tzarr[zs:zs+bufnz,:,:] = buf[0:(1+cur_bufz)]
tzarr[zs:ze,ys:ye,:] = buf[:ze-zs,:ye-ys,:] // divisor
else:
print("\n(end)")
buf[:,:,:] = 0
def divp1(s, c):
n = s // c
if s%c > 0:
n += 1
return n
def process_chunk(args):
idata, odata, z, y, x, cz, cy, cx, algorithm = args
ibuf = idata[2*z*cz:(2*z*cz+2*cz),
2*y*cy:(2*y*cy+2*cy),
2*x*cx:(2*x*cx+2*cx)]
if np.max(ibuf) == 0:
return # Skip if the block is empty to save computation
# pad ibuf to even in all directions
ibs = ibuf.shape
pad = (ibs[0]%2, ibs[1]%2, ibs[2]%2)
if any(pad):
ibuf = np.pad(ibuf,
((0,pad[0]),(0,pad[1]),(0,pad[2])),
mode="symmetric")
# algorithms:
if algorithm == "nearest":
obuf = ibuf[::2, ::2, ::2]
elif algorithm == "gaussian":
obuf = np.round(skimage.transform.rescale(ibuf, .5, preserve_range=True))
elif algorithm == "mean":
obuf = np.round(skimage.transform.downscale_local_mean(ibuf, (2,2,2)))
else:
raise ValueError(f"algorithm {algorithm} not valid")
odata[z*cz:(z*cz+cz),
y*cy:(y*cy+cy),
x*cx:(x*cx+cx)] = np.round(obuf)
def resize(zarrdir, old_level, num_threads, algorithm="mean"):
idir = zarrdir / ("%d"%old_level)
if not idir.exists():
err = f"input directory {idir} does not exist"
print(err)
return err
odir = zarrdir / ("%d"%(old_level+1))
idata = zarr.open(idir, mode="r")
print("Creating level",old_level+1," input array shape", idata.shape, " algorithm", algorithm)
cz, cy, cx = idata.chunks
sz, sy, sx = idata.shape
store = zarr.NestedDirectoryStore(odir)
odata = zarr.open(
store=store,
shape=(divp1(sz,2), divp1(sy,2), divp1(sx,2)),
chunks=idata.chunks,
dtype=idata.dtype,
write_empty_chunks=False,
fill_value=0,
compressor=None,
mode='w',
)
# Prepare tasks
tasks = [(idata, odata, z, y, x, cz, cy, cx, algorithm) for z in range(divp1(sz, 2*cz))
for y in range(divp1(sy, 2*cy))
for x in range(divp1(sx, 2*cx))]
# Use ThreadPoolExecutor to process blocks in parallel
with ThreadPoolExecutor(max_workers=num_threads) as executor:
list(tqdm(executor.map(process_chunk, tasks), total=len(tasks)))
print("Processing complete")
def main():
# parser = argparse.ArgumentParser()
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description="Create OME/Zarr data store from a set of TIFF files")
parser.add_argument(
"input_tiff_dir",
help="Directory containing tiff files")
parser.add_argument(
"output_zarr_ome_dir",
help="Name of directory that will contain OME/zarr datastore")
parser.add_argument(
"--chunk_size",
type=int,
default=128,
help="Size of chunk")
parser.add_argument(
"--obytes",
type=int,
default=0,
help="number of bytes per pixel in output")
parser.add_argument(
"--nlevels",
type=int,
default=6,
help="Number of subdivision levels to create, including level 0")
parser.add_argument(
"--max_gb",
type=float,
default=None,
help="Maximum amount of memory (in Gbytes) to use; None means no limit")
parser.add_argument(
"--zarr_only",
action="store_true",
help="Create a simple Zarr data store instead of an OME/Zarr hierarchy")
parser.add_argument(
"--overwrite",
action="store_true",
# default=False,
help="Overwrite the output directory, if it already exists")
parser.add_argument(
"--num_threads",
type=int,
default=cpu_count(),
help="Advanced: Number of threads to use for processing. Default is number of CPUs")
parser.add_argument(
"--algorithm",
choices=['mean', 'gaussian', 'nearest'],
default="mean",
help="Advanced: algorithm used to sub-sample the data")
parser.add_argument(
"--ranges",
help="Advanced: output only a subset of the data. Example (in xyz order): 2500:3000,1500:4000,500:600")
parser.add_argument(
"--first_new_level",
type=int,
default=None,
help="Advanced: If some subdivision levels already exist, create new levels, starting with this one")
args = parser.parse_args()
zarrdir = Path(args.output_zarr_ome_dir)
if zarrdir.suffix != ".zarr":
print("Name of ouput zarr directory must end with '.zarr'")
return 1
tiffdir = Path(args.input_tiff_dir)
if not tiffdir.exists() and args.first_new_level is None:
print("Input TIFF directory",tiffdir,"does not exist")
return 1
chunk_size = args.chunk_size
nlevels = args.nlevels
maxgb = args.max_gb
zarr_only = args.zarr_only
overwrite = args.overwrite
num_threads = args.num_threads
algorithm = args.algorithm
obytes = args.obytes
print("overwrite", overwrite)
first_new_level = args.first_new_level
if first_new_level is not None and first_new_level < 1:
print("first_new_level must be at least 1")
slices = None
if args.ranges is not None:
slices = parseSlices(args.ranges)
if slices is None:
print("Error parsing ranges argument")
return 1
print("slices", slices)
# even if overwrite flag is False, overwriting is permitted
# when the user has set first_new_level
if not overwrite and first_new_level is None:
if zarrdir.exists():
print("Error: Directory",zarrdir,"already exists")
return(1)
if first_new_level is None or zarr_only:
if zarrdir.exists():
print("removing", zarrdir)
shutil.rmtree(zarrdir)
# tifs2zarr(tiffdir, zarrdir, chunk_size, slices=slices, maxgb=maxgb)
if zarr_only:
err = tifs2zarr(tiffdir, zarrdir, chunk_size, obytes=obytes, slices=slices, maxgb=maxgb)
if err is not None:
print("error returned:", err)
return 1
return
if first_new_level is None:
err = create_ome_dir(zarrdir)
if err is not None:
print("error returned:", err)
return 1
err = create_ome_headers(zarrdir, nlevels)
if err is not None:
print("error returned:", err)
return 1
if first_new_level is None:
print("Creating level 0")
err = tifs2zarr(tiffdir, zarrdir/"0", chunk_size, obytes=obytes, slices=slices, maxgb=maxgb)
if err is not None:
print("error returned:", err)
return 1
# for each level (1 and beyond):
existing_level = 0
if first_new_level is not None:
existing_level = first_new_level-1
for l in range(existing_level, nlevels-1):
err = resize(zarrdir, l, num_threads, algorithm)
if err is not None:
print("error returned:", err)
return 1
if __name__ == '__main__':
sys.exit(main())