-
Notifications
You must be signed in to change notification settings - Fork 2
/
tiff2octree.py
executable file
·1376 lines (1174 loc) · 52.1 KB
/
tiff2octree.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
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import argparse
import fnmatch
import glob
import io
import re
import json
import logging
import os
import random
import sys
import time
import uuid
import warnings
from pathlib import Path
from typing import Any, Dict, List, Optional, Union
try:
from os import scandir, walk
except ImportError:
from scandir import scandir, walk
import dask
import dask.array as da
import dask.bag as db
import dask.dataframe as dd
import dask_image.imread
from dask.distributed import Client, Variable
from distributed.diagnostics.progressbar import progress
from dask.diagnostics import ProgressBar, Profiler, ResourceProfiler, CacheProfiler, visualize
import numpy as np
import pandas as pd
import skimage
from skimage import util
from skimage.transform import resize, downscale_local_mean
from dask_jobqueue import LSFCluster
from distributed import LocalCluster
from tifffile import TiffFile
from multiprocessing.pool import ThreadPool
from shutil import which, copyfile, rmtree
from typing import Union, List, Dict, Any, Optional
import warnings
import itertools
import ktx
from ktx.util import create_mipmaps, mipmap_dimension, interleave_channel_arrays, downsample_array_xy
from ktx.octree.ktx_from_rendered_tiff import RenderedMouseLightOctree, RenderedTiffBlock
import rasterio
from rasterio.windows import Window
from scipy import ndimage
import zarr
import s3fs
import numcodecs
numcodecs.blosc.use_threads = False
dask.config.set({"jobqueue.lsf.use-stdin": True})
threading_env_vars = [
"NUM_MKL_THREADS",
"OPENBLAS_NUM_THREADS",
"OPENMP_NUM_THREADS",
"OMP_NUM_THREADS",
]
def make_single_threaded_env_vars(threads: int) -> List[str]:
return [f"export {var}={threads}" for var in threading_env_vars]
def bsub_available() -> bool:
"""Check if the `bsub` shell command is available
Returns True if the `bsub` command is available on the path, False otherwise. This is used to check whether code is
running on the Janelia Compute Cluster.
"""
result = which("bsub") is not None
return result
def get_LSFCLuster(
threads_per_worker: int = 1,
walltime: str = "1:00",
death_timeout: str = "600s",
**kwargs,
) -> LSFCluster:
"""Create a dask_jobqueue.LSFCluster for use on the Janelia Research Campus compute cluster.
This function wraps the class dask_jobqueue.LSFCLuster and instantiates this class with some sensible defaults,
given how the Janelia cluster is configured.
This function will add environment variables that prevent libraries (OPENMP, MKL, BLAS) from running multithreaded routines with parallelism
that exceeds the number of requested cores.
Additional keyword arguments added to this function will be passed to the dask_jobqueue.LSFCluster constructor.
Parameters
----------
threads_per_worker: int
Number of cores to request from LSF. Directly translated to the `cores` kwarg to LSFCluster.
walltime: str
The expected lifetime of a worker. Defaults to one hour, i.e. "1:00"
cores: int
The number of cores to request per worker. Defaults to 1.
death_timeout: str
The duration for the scheduler to wait for workers before flagging them as dead, e.g. "600s". For jobs with a large number of workers,
LSF may take a long time (minutes) to request workers. This timeout value must exceed that duration, otherwise the scheduler will
flag these slow-to-arrive workers as unresponsive and kill them.
**kwargs:
Additional keyword arguments passed to the LSFCluster constructor
Examples
--------
>>> cluster = get_LSFCLuster(cores=2, project="scicompsoft", queue="normal")
"""
if "env_extra" not in kwargs:
kwargs["env_extra"] = []
kwargs["env_extra"].extend(make_single_threaded_env_vars(threads_per_worker))
USER = os.environ["USER"]
HOME = os.environ["HOME"]
if "local_directory" not in kwargs:
# The default local scratch directory on the Janelia Cluster
kwargs["local_directory"] = f"/scratch/{USER}/"
if "log_directory" not in kwargs:
log_dir = f"{HOME}/.dask_distributed/"
Path(log_dir).mkdir(parents=False, exist_ok=True)
kwargs["log_directory"] = log_dir
cluster = LSFCluster(
cores=threads_per_worker,
walltime=walltime,
death_timeout=death_timeout,
**kwargs,
)
return cluster
def get_LocalCluster(threads_per_worker: int = 1, n_workers: int = 0, memory_limit: str = '16GB', **kwargs):
"""
Creata a distributed.LocalCluster with defaults that make it more similar to a deployment on the Janelia Compute cluster.
This function is a light wrapper around the distributed.LocalCluster constructor.
Parameters
----------
n_workers: int
The number of workers to start the cluster with. This defaults to 0 here.
threads_per_worker: int
The number of threads to assign to each worker.
**kwargs:
Additional keyword arguments passed to the LocalCluster constructor
Examples
--------
>>> cluster = get_LocalCluster(threads_per_worker=8)
"""
return LocalCluster(
n_workers=n_workers, threads_per_worker=threads_per_worker, memory_limit=memory_limit, **kwargs
)
def get_cluster(
threads_per_worker: int = 1,
walltime: str = "1:00",
local_memory_limit: str = "16GB",
deployment: Optional[str] = None,
local_kwargs: Dict[str, Any] = {},
lsf_kwargs: Dict[str, Any] = {"memory": "16GB"},
) -> Union[LSFCluster, LocalCluster]:
"""Convenience function to generate a dask cluster on either a local machine or the compute cluster.
Create a distributed.Client object backed by either a dask_jobqueue.LSFCluster (for use on the Janelia Compute Cluster)
or a distributed.LocalCluster (for use on a single machine). This function uses the output of the bsubAvailable function
to determine whether code is running on the compute cluster or not.
Additional keyword arguments given to this function will be forwarded to the constructor for the Client object.
Parameters
----------
threads_per_worker: int
Number of threads per worker. Defaults to 1.
deployment: str or None
Which deployment (LocalCluster or LSFCluster) to prefer. If deployment=None, then LSFCluster is preferred, but LocalCluster is used if
bsub is not available. If deployment='lsf' and bsub is not available, an error is raised.
local_kwargs: dict
Dictionary of keyword arguments for the distributed.LocalCluster constructor
lsf_kwargs: dict
Dictionary of keyword arguments for the dask_jobqueue.LSFCluster constructor
"""
if "cores" in lsf_kwargs:
warnings.warn(
"The `cores` kwarg for LSFCLuster has no effect. Use the `threads_per_worker` argument instead."
)
if "threads_per_worker" in local_kwargs:
warnings.warn(
"the `threads_per_worker` kwarg was found in `local_kwargs`. It will be overwritten with the `threads_per_worker` argument to this function."
)
if deployment is None:
if bsub_available():
cluster = get_LSFCLuster(threads_per_worker, **lsf_kwargs)
else:
cluster = get_LocalCluster(threads_per_worker, **local_kwargs)
elif deployment == "lsf":
if bsub_available():
cluster = get_LSFCLuster(threads_per_worker, walltime, **lsf_kwargs)
else:
raise EnvironmentError(
"You requested an LSFCluster but the command `bsub` is not available."
)
elif deployment == "local":
cluster = get_LocalCluster(threads_per_worker, 0, local_memory_limit, **local_kwargs)
else:
raise ValueError(
f'deployment must be one of (None, "lsf", or "local"), not {deployment}'
)
return cluster
# https://stackoverflow.com/questions/62567983/block-reduce-downsample-3d-array-with-mode-function
def blockify(image, block_size):
shp = image.shape
out_shp = [s//b for s,b in zip(shp, block_size)]
reshape_shp = np.c_[out_shp,block_size].ravel()
nC = np.prod(block_size)
return image.reshape(reshape_shp).transpose(0,2,4,1,3,5).reshape(-1,nC)
def get_output_bbox_for_downsampling(coord, shape_leaf_px):
ix = (((coord - 1) >> 0) & 1) * shape_leaf_px[2] >> 1
iy = (((coord - 1) >> 1) & 1) * shape_leaf_px[1] >> 1
iz = (((coord - 1) >> 2) & 1) * shape_leaf_px[0] >> 1
ixx = ix+(shape_leaf_px[2] >> 1)
iyy = iy+(shape_leaf_px[1] >> 1)
izz = iz+(shape_leaf_px[0] >> 1)
return np.array([[iz, iy, ix], [izz, iyy, ixx]])
def downsample_2ndmax(out_tile_jl, coord, shape_leaf_px, scratch):
bbox = get_output_bbox_for_downsampling(coord, shape_leaf_px)
b = blockify(scratch, block_size=(2,2,2))
b.sort(axis=1)
down_img = b[:,-2].reshape(bbox[1,0]-bbox[0,0], bbox[1,1]-bbox[0,1], bbox[1,2]-bbox[0,2])
out_tile_jl[bbox[0,0]:bbox[1,0], bbox[0,1]:bbox[1,1], bbox[0,2]:bbox[1,2]] = down_img.astype(scratch.dtype)
def downsample_aa(out_tile_jl, coord, shape_leaf_px, scratch):
bbox = get_output_bbox_for_downsampling(coord, shape_leaf_px)
if scratch.dtype is np.dtype('uint8'):
out_tile_jl[bbox[0,0]:bbox[1,0], bbox[0,1]:bbox[1,1], bbox[0,2]:bbox[1,2]] = util.img_as_ubyte(resize(scratch, (shape_leaf_px[0]>>1, shape_leaf_px[1]>>1, shape_leaf_px[2]>>1), anti_aliasing=True))
elif scratch.dtype is np.dtype('uint16'):
out_tile_jl[bbox[0,0]:bbox[1,0], bbox[0,1]:bbox[1,1], bbox[0,2]:bbox[1,2]] = util.img_as_uint(resize(scratch, (shape_leaf_px[0]>>1, shape_leaf_px[1]>>1, shape_leaf_px[2]>>1), anti_aliasing=True))
elif scratch.dtype is np.dtype('float32'):
out_tile_jl[bbox[0,0]:bbox[1,0], bbox[0,1]:bbox[1,1], bbox[0,2]:bbox[1,2]] = util.img_as_float32(resize(scratch, (shape_leaf_px[0]>>1, shape_leaf_px[1]>>1, shape_leaf_px[2]>>1), anti_aliasing=True))
def downsample_area(out_tile_jl, coord, shape_leaf_px, scratch):
bbox = get_output_bbox_for_downsampling(coord, shape_leaf_px)
down_img = downscale_local_mean(scratch, (2, 2 ,2))
out_tile_jl[bbox[0,0]:bbox[1,0], bbox[0,1]:bbox[1,1], bbox[0,2]:bbox[1,2]] = down_img.astype(scratch.dtype)
def downsample_spline3(out_tile_jl, coord, shape_leaf_px, scratch):
bbox = get_output_bbox_for_downsampling(coord, shape_leaf_px)
down_img = ndimage.zoom(scratch, 0.5)
out_tile_jl[bbox[0,0]:bbox[1,0], bbox[0,1]:bbox[1,1], bbox[0,2]:bbox[1,2]] = down_img.astype(scratch.dtype)
def get_octree_relative_path(chunk_coord, level, sep=True):
relpath = ''
pos = np.asarray(chunk_coord)
pos = np.add(pos, np.full(3, -1))
for lv in range(level-1, -1, -1):
d = pow(2, lv)
octant_path = str(1 + int(pos[2] / d) + 2 * int(pos[1] / d) + 4 * int(pos[0] / d))
if lv < level-1:
if sep:
relpath = os.path.join(relpath, octant_path)
else:
relpath += octant_path
pos[2] = pos[2] - int(pos[2] / d) * d
pos[1] = pos[1] - int(pos[1] / d) * d
pos[0] = pos[0] - int(pos[0] / d) * d
return relpath
def get_cropped_image_rasterio(file_paths, z0, y0, x0, d, h, w, type, ch):
output = np.zeros((d, h, w), dtype=type)
for i in range(z0, z0 + d):
if i - z0 < len(file_paths):
try:
with rasterio.open(file_paths[i - z0]) as src:
data = src.read(ch+1, window=Window(x0, y0, w, h))
output[i - z0, :h, :w] = data[:, :]
except BaseException as err:
logging.error(file_paths[i - z0])
logging.error(err)
return output
def gen_blocks_from_slices_batch(chunk_coords, file_paths, target_path, nlevels, dim_leaf, ch, type, resolutions, chnum, resume):
for pos in chunk_coords:
gen_block_from_slices(pos, file_paths, target_path, nlevels, dim_leaf, ch, type, resolutions, chnum, resume)
def gen_block_from_slices(chunk_coord, file_paths, target_path, nlevels, dim_leaf, chid, type, resolutions, chnum, resume):
relpath = get_octree_relative_path(chunk_coord, nlevels)
dir_path = os.path.join(target_path, relpath)
for ch in range(chnum):
full_path = os.path.join(dir_path, "default.{0}.tif".format(chid+ch))
if resume and os.path.exists(full_path):
logging.info("skipped (exists): " + full_path)
continue
#logging.info((dim_leaf[0]*chunk_coord[0], dim_leaf[1]*chunk_coord[1], dim_leaf[2]*chunk_coord[2]))
img_data = get_cropped_image_rasterio(file_paths, dim_leaf[0]*(chunk_coord[0]-1), dim_leaf[1]*(chunk_coord[1]-1), dim_leaf[2]*(chunk_coord[2]-1), dim_leaf[0], dim_leaf[1], dim_leaf[2], type, ch)
if img_data.max() > 0:
logging.info(full_path)
Path(dir_path).mkdir(parents=True, exist_ok=True)
skimage.io.imsave(full_path, img_data, compression=("ZLIB", 6))
else:
logging.info("skipped (empty): " + full_path)
def gen_blocks_from_n5_zarr_batch(chunk_coords, input_array_path, target_path, nlevels, dim_leaf, ch, type, resolutions, resume):
numcodecs.blosc.use_threads = False
input_array = None
n5store = None
try:
input_array = zarr.open(input_array_path, mode='r')
except zarr.errors.PathNotFoundError:
input_array = zarr.open(store=zarr.N5Store(input_array_path), mode='r')
for pos in chunk_coords:
gen_block_from_n5_zarr(pos, input_array, target_path, nlevels, dim_leaf, ch, type, resolutions, resume)
def gen_block_from_n5_zarr(chunk_coord, input_array, target_path, nlevels, dim_leaf, chid, type, resolutions, resume):
relpath = get_octree_relative_path(chunk_coord, nlevels)
dir_path = os.path.join(target_path, relpath)
full_path = os.path.join(dir_path, "default.{0}.tif".format(chid))
if resume and os.path.exists(full_path):
logging.info("skipped (exists): " + full_path)
return
#logging.info((dim_leaf[0]*chunk_coord[0], dim_leaf[1]*chunk_coord[1], dim_leaf[2]*chunk_coord[2]))
img_data = get_cropped_image_n5_zarr(input_array, dim_leaf[0]*(chunk_coord[0]-1), dim_leaf[1]*(chunk_coord[1]-1), dim_leaf[2]*(chunk_coord[2]-1), dim_leaf[0], dim_leaf[1], dim_leaf[2], type)
if img_data.max() > 0:
logging.info(full_path)
Path(dir_path).mkdir(parents=True, exist_ok=True)
skimage.io.imsave(full_path, img_data, compression=("ZLIB", 6))
else:
logging.info("skipped (empty): " + full_path)
def get_cropped_image_n5_zarr(input_array, z0, y0, x0, d, h, w, type):
output = np.zeros((d, h, w), dtype=type)
output[:d, :h, :w] = input_array[..., z0:z0+d, y0:y0+h, x0:x0+w]
return output
#return True if the block exists
def check_block(chunk_coord, target_path, nlevels, chid, chnum):
relpath = get_octree_relative_path(chunk_coord, nlevels)
dir_path = os.path.join(target_path, relpath)
block_exists = True
for ch in range(chnum):
full_path = os.path.join(dir_path, "default.{0}.tif".format(chid+ch))
if not os.path.exists(full_path):
block_exists = False
return block_exists
def save_block(chunk, target_path, nlevels, dim_leaf, ch, block_id=None):
if block_id == None:
return np.array(0)[None, None, None]
block_id = [i+1 for i in block_id]
relpath = get_octree_relative_path(block_id, nlevels)
dir_path = os.path.join(target_path, relpath)
Path(dir_path).mkdir(parents=True, exist_ok=True)
full_path = os.path.join(dir_path, "default.{0}.tif".format(ch))
logging.info(full_path)
skimage.io.imsave(full_path, chunk, compression=("ZLIB", 6))
return np.array(block_id[0])[None, None, None] if block_id != None else np.array(0)[None, None, None]
def downsample_and_save_block_batch(chunk_coords, target_path, level, dim_leaf, type, downsampling_method, resolutions, ktxdir, delete_source, resume):
for pos in chunk_coords:
downsample_and_save_block(pos, target_path, level, dim_leaf, type, downsampling_method, resolutions, ktxdir, delete_source, resume)
def downsample_and_save_block(chunk_coord, target_path, level, dim_leaf, type, downsampling_method, resolutions, ktxdir, delete_source, resume):
relpath = get_octree_relative_path(chunk_coord, level)
dir_path = os.path.join(target_path, relpath)
if resume:
if ktxdir:
ktx_name = 'block_8_xy_'+get_octree_relative_path(chunk_coord, level, False)+".ktx"
if os.path.exists(os.path.join(ktxdir, ktx_name)):
return
downsampled_images = {}
for oct in range(1, 9):
down_dir = os.path.join(dir_path, str(oct))
file_list = glob.glob(os.path.join(down_dir, 'default.[0-9].tif'))
for full_path in file_list:
img_name = os.path.basename(full_path)
if resume and os.path.exists(os.path.join(dir_path, img_name)):
continue
if not img_name in downsampled_images.keys():
downsampled_images[img_name] = np.zeros(dim_leaf, dtype=type)
blk_path = os.path.join(os.path.join(dir_path, str(oct)), img_name)
try:
scratch = skimage.io.imread(blk_path)
except:
logging.info("empty: " + blk_path)
continue
if downsampling_method == 'area':
downsample_area(downsampled_images[img_name], oct, dim_leaf, scratch)
elif downsampling_method == 'aa':
downsample_aa(downsampled_images[img_name], oct, dim_leaf, scratch)
elif downsampling_method == 'spline':
downsample_spline3(downsampled_images[img_name], oct, dim_leaf, scratch)
else:
downsample_2ndmax(downsampled_images[img_name], oct, dim_leaf, scratch)
if ktxdir:
convert_block_ktx_path(os.path.join(relpath, str(oct)), target_path, ktxdir, level+1, True, True, True, delete_source)
Path(dir_path).mkdir(parents=True, exist_ok=True)
if level > 1:
for img_name in downsampled_images.keys():
if downsampled_images[img_name].max() > 0:
full_path = os.path.join(dir_path, img_name)
logging.info(full_path)
skimage.io.imsave(full_path, downsampled_images[img_name], compression=("ZLIB", 6))
else:
for img_name in downsampled_images.keys():
full_path = os.path.join(dir_path, img_name)
logging.info(full_path)
skimage.io.imsave(full_path, downsampled_images[img_name])
if ktxdir:
convert_block_ktx_path(relpath, target_path, ktxdir, level, True, True, True, False)
def convert_block_ktx_batch(chunk_coords, source_path, target_path, level, downsample_intensity, downsample_xy, make_dir, delete_source):
for pos in chunk_coords:
convert_block_ktx(pos, source_path, target_path, level, downsample_intensity, downsample_xy, make_dir, delete_source)
def convert_block_ktx(chunk_coord, source_path, target_path, level, downsample_intensity, downsample_xy, make_dir, delete_source):
relpath = get_octree_relative_path(chunk_coord, level)
convert_block_ktx_path(relpath, source_path, target_path, level, downsample_intensity, downsample_xy, make_dir, delete_source)
def convert_block_ktx_path(relpath, source_path, target_path, level, downsample_intensity, downsample_xy, make_dir, delete_source):
tardir_path = os.path.join(target_path, relpath)
if make_dir:
Path(tardir_path).mkdir(parents=True, exist_ok=True)
srcdir_path = os.path.join(source_path, relpath)
file_list = glob.glob(os.path.join(srcdir_path, 'default.[0-9].tif'))
if len(file_list) == 0:
return
octree_path0 = relpath.split(os.path.sep)
octree_path = []
for level_str in octree_path0:
if re.match(r'[1-8]', level_str):
octree_path.append(int(level_str))
o = RenderedMouseLightOctree(input_folder=source_path, downsample_intensity=downsample_intensity, downsample_xy=downsample_xy)
block = RenderedTiffBlock(os.path.join(source_path, relpath), o, octree_path)
file_name = 'block'
if downsample_intensity:
file_name += '_8'
if downsample_xy:
file_name += '_xy'
opath1 = "".join([str(n) for n in block.octree_path])
file_name += '_'+opath1+".ktx"
output_dir = os.path.join(target_path, relpath)
full_path = os.path.join(output_dir, file_name)
logging.info(full_path)
overwrite = True
if os.path.exists(full_path) and not overwrite:
file_size = os.stat(full_path).st_size
if file_size > 0:
logging.info("Skipping an existing file %s" % full_path)
return
try:
f = open(full_path, 'wb')
block.write_ktx_file(f)
f.flush()
f.close()
except IOError as err:
logging.error(err)
logging.error("Error writing file %s" % full_path)
f.flush()
f.close()
os.unlink(f.name)
if delete_source and level > 1:
try:
for fpath in glob.glob(os.path.join(srcdir_path, 'default.[0-9].tif')):
os.remove(fpath)
except OSError as err:
logging.error(err)
logging.error("Could not delete %s" % fpath)
def conv_tiled_tiff(input, output, tilesize, resume):
if not os.path.exists(input):
return input
if resume and os.path.exists(output):
return output
is_tiled = True
is_jpeg = False
if input.endswith('.tif'):
try:
tif = TiffFile(input)
is_tiled = tif.pages[0].is_tiled
tif.close()
except Exception:
return input
elif input.endswith('.jp2'):
is_jpeg = True
is_tiled = False
else:
return input
if not is_tiled:
try:
if is_jpeg:
img = rasterio.open(input).read()
else:
img = skimage.io.imread(input)
skimage.io.imsave(output, img, compression=("ZLIB", 6), tile=tilesize)
logging.info("saved tiled-tiff: " + output)
except Exception as err:
logging.error(err)
logging.error("failed to convert: " + output)
return input
return output
def conv_tiled_tiffs(input_list, outdir, tilesize, resume):
ret_list = []
for fpath in input_list:
fname = os.path.splitext(os.path.basename(fpath))[0] + ".tif"
ret = conv_tiled_tiff(fpath, os.path.join(outdir, fname), tilesize, resume)
ret_list.append(ret)
return ret_list
def delete_files(target_list):
for fpath in target_list:
try:
os.remove(fpath)
except:
logging.error("Error while deleting file ", fpath)
def scantree(path):
for entry in scandir(path):
if entry.is_dir(follow_symlinks=False):
yield from scantree(entry.path)
else:
yield entry
def setup_cluster(
cluster_address: str = None,
monitoring: bool = False,
is_lsf: bool = False,
walltime: str = "1:00",
memory_limit: str = "16GB",
lsf_maximum_jobs: int = 1,
thread_num: int = 1,
project: str = ""
):
cluster = None
if cluster_address:
cluster = cluster_address
elif is_lsf:
my_lsf_kwargs={}
my_lsf_kwargs['memory'] = memory_limit
if project:
my_lsf_kwargs['project'] = project
cluster = get_cluster(deployment="lsf", walltime=walltime, lsf_kwargs = my_lsf_kwargs)
cluster.adapt(minimum_jobs=1, maximum_jobs = lsf_maximum_jobs)
cluster.scale(thread_num)
else:
cluster = get_cluster(deployment="local", local_memory_limit = memory_limit)
cluster.scale(thread_num)
dashboard_address = None
if monitoring:
dashboard_address = ":8787"
logging.info(f"Starting dashboard on {dashboard_address}")
client = Client(address=cluster, processes=True, dashboard_address=dashboard_address)
else:
client = Client(address=cluster)
return client
def adjust_dimensions(dim, nlevels):
ret = np.copy(dim)
for i in range(0, len(ret)):
while(ret[i] % pow(2, nlevels) > 0):
ret[i] -= 1
return ret
def calc_optimal_nlevels(dim):
lv = 1
while True:
dim = adjust_dimensions(dim, lv)
dim_leaf = [x >> (lv - 1) for x in dim]
if dim_leaf[0]*dim_leaf[1]*dim_leaf[2] < 512*512*512:
break
lv += 1
return lv
def stack_to_dask_array(
file_path: str,
nlevels: int
):
ret = None
lv = nlevels
if file_path:
img = dask_image.imread.imread(file_path)
img = img[..., np.newaxis]
logging.info("Image size: " + str(img.shape))
dim = np.asarray(img.shape)[:3]
if lv < 1:
lv = calc_optimal_nlevels(dim)
dim = adjust_dimensions(dim, lv)
dim_leaf = [x >> (lv - 1) for x in dim]
logging.info("Adjusted image size: " + str(dim) + ", Dim leaf: " + str(dim_leaf))
adjusted = img[:dim[0], :dim[1], :dim[2], :]
ret = adjusted.rechunk((dim_leaf[0], dim_leaf[1], dim_leaf[2], 1))
return (ret, lv)
def slice_to_dask_array(
indir: str,
nlevels: int
):
ret = None
images = None
dim = None
volume_dtype = None
lv = nlevels
if indir:
img_files = [os.path.join(indir, f) for f in os.listdir(indir) if f.endswith(('.tif', '.jp2'))]
im_width = 0
im_height = 0
samples_per_pixel = 1
if img_files[0].endswith('.tif'):
with TiffFile(img_files[0]) as tif:
im_width = tif.pages[0].imagewidth
im_height = tif.pages[0].imagelength
logging.info(tif.pages[0].dtype)
volume_dtype = tif.pages[0].dtype
elif img_files[0].endswith('.jp2'):
with rasterio.open(img_files[0]) as src:
im_width = src.width
im_height = src.height
samples_per_pixel = src.count
volume_dtype = src.dtypes[0]
dim = np.asarray((len(img_files), im_height, im_width))
if lv < 1:
lv = calc_optimal_nlevels(dim)
dim = adjust_dimensions(dim, lv)
dim_leaf = [x >> (lv - 1) for x in dim]
logging.info("Adjusted image size: " + str(dim) + ", Dim leaf: " + str(dim_leaf))
ret = da.zeros((dim[0], dim[1], dim[2], samples_per_pixel), chunks=(dim_leaf[0], dim_leaf[1], dim_leaf[2], samples_per_pixel), dtype=volume_dtype)
return (ret, lv)
def get_pixel_resolution_n5_zarr(indir: str):
ret = None
if indir:
img = None
try:
img = zarr.open(indir, mode='r')
except:
try:
img = zarr.open(store=zarr.N5Store(indir), mode='r')
except:
return ret
if not img and 'pixelResolution' in img.attrs:
ret = img.attrs['pixelResolution']
return ret
def check_n5_levels(indir: str):
ret = None
levels = []
for entry2 in scandir(indir):
levels.append(entry2.name)
for i in range(0, 10):
lv_name = 's{0}'.format(i)
if lv_name in levels:
lvpath = os.path.join(indir, lv_name)
try:
img = zarr.open(store=zarr.N5Store(lvpath), mode='r')
ret = lvpath
break
except:
continue
return ret
def check_n5_channels(indir: str):
in_dirs = []
if os.path.exists(indir):
p = check_n5_levels(indir)
if p:
in_dirs.append(p)
if len(in_dirs) > 0:
return in_dirs
for entry in scandir(indir):
channels = []
if entry.is_dir(follow_symlinks=False):
channels.append(entry.name)
for i in range(0, 10):
ch_name = 'c{0}'.format(i)
if ch_name in channels:
p = check_n5_levels(entry.path)
if p:
in_dirs.append(p)
if len(in_dirs) > 0:
return in_dirs
for entry in scandir(indir):
if entry.is_dir(follow_symlinks=False):
p = check_n5_levels(entry.path)
if p:
in_dirs.append(p)
if len(in_dirs) > 0:
return in_dirs
img = None
try:
img = zarr.open(indir, mode='r')
except:
try:
img = zarr.open(store=zarr.N5Store(indir), mode='r')
except:
return in_dirs
if isinstance(img, zarr.core.Array):
in_dirs.append(indir)
elif isinstance(img, zarr.hierarchy.Group):
for i in img:
if isinstance(img[i], zarr.core.Array):
in_dirs.append(os.path.join(indir, img[i].path))
return in_dirs
def n5_zarr_to_dask_array(
indir: str,
nlevels: int
):
ret = None
images = None
dim = None
volume_dtype = None
lv = nlevels
if indir:
img = None
try:
img = zarr.open(indir, mode='r')
except zarr.errors.PathNotFoundError:
try:
img = zarr.open(store=zarr.N5Store(indir), mode='r')
except zarr.errors.PathNotFoundError:
return None
samples_per_pixel = 1
if isinstance(img, zarr.core.Array):
dim = np.asarray(img.shape)[-3:]
volume_dtype = img.dtype
elif isinstance(img, zarr.hierarchy.Group):
for i in range(0, 10):
item_name = 's{0}'.format(i)
if item_name in img:
dim = np.asarray(img[item_name].shape)[-3:]
volume_dtype = img.dtype
break
if not volume_dtype:
return None
if lv < 1:
lv = calc_optimal_nlevels(dim)
dim = adjust_dimensions(dim, lv)
dim_leaf = [x >> (lv - 1) for x in dim]
logging.info("Adjusted image size: " + str(dim) + ", Dim leaf: " + str(dim_leaf))
ret = da.zeros((dim[0], dim[1], dim[2]), chunks=(dim_leaf[0], dim_leaf[1], dim_leaf[2]), dtype=volume_dtype)
return (ret, lv)
def parse_voxel_size(voxel_size_str: str):
vsstr = voxel_size_str.split(",")
vs = []
if len(vsstr) > 0:
vs.append(float(vsstr[2]))
else:
vs.append(1.0)
if len(vsstr) > 1:
vs.append(float(vsstr[1]))
else:
vs.append(1.0)
if len(vsstr) > 2:
vs.append(float(vsstr[0]))
else:
vs.append(1.0)
return vs
def save_transform_txt(
outdir: str,
origin: str,
voxsize: str,
nlevels: int,
ktxout: str
):
ostr = origin.split(",")
o = []
if len(ostr) > 0:
o.append(ostr[2])
else:
o.append("0.0")
if len(ostr) > 1:
o.append(ostr[1])
else:
o.append("0.0")
if len(ostr) > 2:
o.append(ostr[0])
else:
o.append("0.0")
vsstr = voxsize.split(",")
vs = []
if len(vsstr) > 0:
vs.append(float(vsstr[2]))
else:
vs.append(1.0)
if len(vsstr) > 1:
vs.append(float(vsstr[1]))
else:
vs.append(1.0)
if len(vsstr) > 2:
vs.append(float(vsstr[0]))
else:
vs.append(1.0)
l = []
l.append("ox: " + o[2])
l.append("oy: " + o[1])
l.append("oz: " + o[0])
l.append("sx: " + '{:.14g}'.format(vs[2] * 1000 * pow(2, nlevels-1)))
l.append("sy: " + '{:.14g}'.format(vs[1] * 1000 * pow(2, nlevels-1)))
l.append("sz: " + '{:.14g}'.format(vs[0] * 1000 * pow(2, nlevels-1)))
l.append("nl: " + str(nlevels))
Path(outdir).mkdir(parents=True, exist_ok=True)
tr_path = os.path.join(outdir, "transform.txt")
with open(tr_path, mode='w') as f:
f.write('\n'.join(l))
if ktxout:
ktxroot = str(Path(ktxout).parent)
if outdir != ktxroot:
Path(ktxroot).mkdir(parents=True, exist_ok=True)
copyfile(tr_path, os.path.join(ktxroot, "transform.txt"))
if outdir != ktxout:
Path(ktxout).mkdir(parents=True, exist_ok=True)
copyfile(tr_path, os.path.join(ktxout, "transform.txt"))
def covert_tiff_to_tiled_tiff(input_paths: List[str], task_num: int, outdir: str, maxbatch: int = 100, resume: bool = False):
slice_files = input_paths
futures = []
logging.info("tiff-to-tiled-tiff conversion")
batch_slice_num = len(slice_files) / task_num
if maxbatch > 0 and batch_slice_num > maxbatch:
batch_slice_num = maxbatch
if batch_slice_num < 1:
batch_slice_num = 1
chunked_tif_list = np.array_split(slice_files, (int)(len(slice_files) / batch_slice_num))
logging.info("tiff-to-tiled-tiff conversion: list " + str(len(chunked_tif_list)))
for tlist in chunked_tif_list:
#logging.info(tlist)
future = dask.delayed(conv_tiled_tiffs)(tlist, outdir, (256, 256), resume)
futures.append(future)
with ProgressBar():
results = dask.compute(futures)
slice_files = [item for sublist in results[0] for item in sublist]
logging.info("done")
return slice_files
def delete_temporary_files(tmpdir: str, task_num: int, maxbatch: int = 200):
logging.info("deleting temporary files...")
tlist = [entry.path for entry in scantree(tmpdir)]
if len(tlist) == 0:
return
batch_file_num = len(tlist) / task_num
if maxbatch > 0 and batch_file_num > maxbatch:
batch_file_num = maxbatch
if batch_file_num < 1:
batch_file_num = 1
chunked_file_list = np.array_split(tlist, (int)(len(tlist) / batch_file_num))
futures = []
for flist in chunked_file_list:
future = dask.delayed(delete_files)(flist)
futures.append(future)
with ProgressBar():
dask.compute(futures)
logging.info("done")
# loop over the chunks in the dask array.
def save_tiff_blocks(input_slices: List[str], output_path: str, z: int, nlevels: int, task_num: int, maxbatch: int, ch: int, voxel_size_str: str, darray: da.Array, resume: bool):
futures = []
bnum = pow(2, nlevels - 1)
volume_dtype = darray.dtype
samples_per_pixel = darray.shape[3] if len(darray.shape) >= 4 else 1
dim_leaf = darray.chunksize[:3]
vs = parse_voxel_size(voxel_size_str)
batch_block_num = (int)(bnum * bnum / task_num)
if maxbatch > 0 and batch_block_num > maxbatch:
batch_block_num = maxbatch
if batch_block_num < 1:
batch_block_num = 1
coord_list = []
for y in range(1, bnum+1):
for x in range(1, bnum+1):
coord_list.append((z,y,x))
if len(coord_list) >= batch_block_num:
future = dask.delayed(gen_blocks_from_slices_batch)(coord_list, input_slices, output_path, nlevels, dim_leaf, ch, volume_dtype, vs, samples_per_pixel, resume)
futures.append(future)
coord_list = []
if len(coord_list) > 0:
future = dask.delayed(gen_blocks_from_slices_batch)(coord_list, input_slices, output_path, nlevels, dim_leaf, ch, volume_dtype, vs, samples_per_pixel, resume)
futures.append(future)
with ProgressBar():
dask.compute(futures)
# loop over the chunks in the dask array.
def save_tiff_blocks_from_n5_zarr(input_array_path: str, output_path: str, nlevels: int, task_num: int, maxbatch: int, ch: int, voxel_size_str: str, darray: da.Array, resume: bool):
futures = []
bnum = pow(2, nlevels - 1)
volume_dtype = darray.dtype
dim_leaf = darray.chunksize[:3]
vs = parse_voxel_size(voxel_size_str)
batch_block_num = (int)(bnum * bnum / task_num)
if maxbatch > 0 and batch_block_num > maxbatch:
batch_block_num = maxbatch
if batch_block_num < 1:
batch_block_num = 1
coord_list = []
for inds in itertools.product(*map(range, darray.blocks.shape)):
coord_list.append((inds[0]+1, inds[1]+1, inds[2]+1))
if len(coord_list) >= batch_block_num:
future = dask.delayed(gen_blocks_from_n5_zarr_batch)(coord_list, input_array_path, output_path, nlevels, dim_leaf, ch, volume_dtype, vs, resume)
futures.append(future)
coord_list = []
if len(coord_list) > 0:
future = dask.delayed(gen_blocks_from_n5_zarr_batch)(coord_list, input_array_path, output_path, nlevels, dim_leaf, ch, volume_dtype, vs, resume)
futures.append(future)
with ProgressBar():
dask.compute(futures)
def check_tiff_blocks(output_path: str, z: int, nlevels: int, ch: int, darray: da.Array):
logging.info("resume: checking tiff blocks")
futures = []
bnum = pow(2, nlevels - 1)