-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathvolume_zarr.py
1352 lines (1214 loc) · 50.2 KB
/
volume_zarr.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 tifffile
import numpy as np
import os
import zarr
import time
import pathlib
import re
import queue
import json
from concurrent.futures import ThreadPoolExecutor
import cv2
from scipy import ndimage
from utils import Utils
CHUNK_SIZE = 500
def load_tif(path):
"""This function will take a path to a folder that contains a stack of .tif
files and returns a concatenated 3D zarr array that will allow access to an
arbitrary region of the stack.
We support two different styles of .tif stacks. The first are simply
numbered filenames, e.g., 00.tif, 01.tif, 02.tif, etc. In this case, the
numbers are taken as the index into the zstack, and we assume that the zslices
are fully continuous. Masked versions of these are available for download here:
http://dl.ash2txt.org/full-scrolls/Scroll1.volpkg/volumes_masked/20230205180739/
The second follows @spelufo's reprocessing, and are not 2D images but 3D cells
of the data. These should be labeled
cell_yxz_YINDEX_XINDEX_ZINDEX
where these provide the position in the Y, X, and Z grid of cuboids that make
up the image data. They are available to download here:
http://dl.ash2txt.org/full-scrolls/Scroll1.volpkg/volume_grids/20230205180739/
Note that if data is missing (e.g., a missing 00001.tif or missing cube) that
that region of the zarr is filled in as zeros. For the cubes this is desirable
to reduce space use to store non-scroll information. A CSV annotating only
cubes with valid scroll information is available here:
https://github.com/spelufo/vesuvius-build/blob/main/masks/scroll_1_54_mask.csv
These arrays are non-writable.
"""
# Get a list of .tif files
tiffs = [filename for filename in os.listdir(path) if filename.endswith(".tif")]
if all([filename[:-4].isnumeric() for filename in tiffs]):
# This looks like a set of z-level images
tiffs.sort(key=lambda f: int(''.join(filter(str.isdigit, f))))
paths = [os.path.join(path, filename) for filename in tiffs]
store = tifffile.imread(paths, aszarr=True, fillvalue=0)
elif all([filename.startswith("cell_yxz_") for filename in tiffs]):
# This looks like a set of cell cuboid images
pattern=r"cell_yxz_(\d+)_(\d+)_(\d+)"
images = tifffile.TiffSequence(os.path.join(path, "*.tif"), pattern=pattern)
# The indices (locations of chunks in zarr) are not
# set correctly in TiffSequence, so reset them
# below, based on the file names.
# They will be used by images.aszarr()
new_indices = []
pattern_compiled = re.compile(pattern)
maxx = 0
maxy = 0
maxz = 0
for i in range(len(images.indices)):
file = images.files[i]
m = pattern_compiled.search(file)
ystr,xstr,zstr = m.groups()
# -1 because the file names are indexed from 1,1,1
ix = int(xstr)-1
iy = int(ystr)-1
iz = int(zstr)-1
new_index = (iy,ix,iz)
maxx = max(maxx, ix)
maxy = max(maxy, iy)
maxz = max(maxz, iz)
new_indices.append(new_index)
images.indices = new_indices
images.shape=(maxy+1,maxx+1,maxz+1)
store = images.aszarr(axestiled={0: 1, 1: 2, 2: 0}, fillvalue=0)
stack_array = zarr.open(store, mode="r")
return stack_array
def load_zarr(dirname):
stack_array = zarr.open(dirname, mode="r")
return stack_array
def load_writable_volume(path):
"""This function takes a path to a zarr DirectoryStore folder that contains
chunked volume information. This zarr Array is writable and can be used
for fast persistent storage.
"""
if not os.path.exists(path):
raise ValueError("Error: f{path} does not exist")
store = zarr.DirectoryStore(path)
root = zarr.group(store=store, overwrite=False)
return root.volume
def create_writeable_volume(path, volume_shape):
"""Generates a new zarr Array object serialized to disk as an empty array of
zeros. Requires the size of the array to be given; this may be arbitrarily
large. When initialized, this takes up very little space on disk since all
chunks are empty. As it is written, it can get much larger. Be sure you
have enough disk space!
"""
if os.path.exists(path):
raise ValueError("Error: f{path} already exists")
store = zarr.DirectoryStore(path)
root = zarr.group(store=store, overwrite=True)
volume = root.zeros(
name="volume",
shape=volume_shape,
chunks=tuple([CHUNK_SIZE for d in volume_shape]),
dtype=np.uint16,
write_empty_chunks=False,
)
return volume
def slice_to_hashable(slice):
return (slice.start, slice.stop)
def hashable_to_slice(item):
return slice(item[0], item[1], None)
class TransposedDataView():
def __init__(self, data, direction=0, from_vc_render=False, original_dtype=None):
self.data = data
self.from_vc_render = from_vc_render
self.direction = direction
self.original_dtype = original_dtype
@property
def shape(self):
shape = self.data.shape
if self.from_vc_render:
shape = (shape[1],shape[0],shape[2])
if self.direction == 0:
return (shape[2], shape[0], shape[1])
elif self.direction == 1:
return (shape[1], shape[0], shape[2])
# Two steps:
# First, select the data from the original data cube
# (need to transpose the selection into global coordinates
# to do this);
# Second, transpose the results (which are aligned with the global
# data axes) back to the transposed axes.
# In step one, make sure that axes are not squeezed out,
# because that would cause the transpose to fail
def __getitem__(self, selection):
# transpose selection to global axes
if self.direction == 0:
s2, s0, s1 = selection
elif self.direction == 1:
s1, s0, s2 = selection
if self.from_vc_render:
s1,s0,s2 = s0,s1,s2
# convert integer selections into slices;
# the data[] call "squeezes" (removes)
# all axes that have integer selections, which would
# cause the transpose to fail because the array
# would have fewer dimensions than expected
alls = []
# print(type(s0),type(s1),type(s2))
for s in (s0,s1,s2):
if isinstance(s, int):
alls.append(slice(s,s+1))
else:
alls.append(s)
result = self.data[alls[0],alls[1],alls[2]]
if self.original_dtype == np.uint8 and result.dtype == np.uint8:
result = result.astype(np.uint16)
result = result * 256 + 128 # Scale up to full 16-bit range
if len(result.shape) == 1:
# Fancy-indexing collapses the shape, so we don't need to transpose
return result
# print("ar", alls, result.shape)
# transpose the result back to the transposed axes
if self.from_vc_render:
result = result.transpose(1,0,2)
if self.direction == 0:
result = result.transpose(2, 0, 1)
elif self.direction == 1:
result = result.transpose(1, 0, 2)
# squeeze away any axes of size 1
result = np.squeeze(result)
return result
'''
LRU (least-recently-used) cache based on the version
in https://github.com/zarr-developers/zarr-python.
It has been modified to work in threaded mode:
that is, when __getitem__ is called, if the requested
chunk is not in cache, a KeyError is immediately returned
to the caller (telling the caller to treat the chunk as all
zeros), and a request is submitted to ThreadPoolExecutor
to run a thread to retrieve the chunk. Once the thread has retrieved the
chunk, the chunk is added to the cache, and (optionally)
a callback is called.
NOTE: In khartes, this callback is set to MainWindow.zarrFutureDoneCallback.
Sometimes the caller would rather wait for the
data, instead of having the request put on the work queue.
In this case, the caller needs to call
setImmediateDataMode(True), before making requesting any data,
and after the data has been retrieved, call setImmediateDataMode(False)
(to restore request queueing).
'''
class KhartesThreadedLRUCache(zarr.storage.LRUStoreCache):
def __init__(self, store, max_size):
super().__init__(store, max_size)
self.future_done_callback = None
self.callback_called = False
self.zero_vols = set()
self.submitted = set()
# non-zero misses: that is, misses due to
# key not being in the cache, and not being
# in the list of empty chunks
self.nz_misses = 0
self.immediate_data_mode = False
self.executor = ThreadPoolExecutor(max_workers=4)
# This is a dubious thing to do from a coding standpoint,
# but over a slow connection, the user probably wants to
# see the most-recently-requested data first.
self.executor._work_queue = queue.LifoQueue()
def __getitem__old(self, key):
print("get item", key)
return super().__getitem__(key)
def setImmediateDataMode(self, flag):
with self._mutex:
self.immediate_data_mode = flag
def __contains__(self, key):
try:
# In threaded mode, self[key] will raise an exception
# if the chunk doesn't exist on disk, or if the
# chunk has already been submitted on a thread.
# If the chunk had not been previously submitted,
# it will be submitted first, and then the exception will
# be raised.
_ = self[key]
return True
except KeyError:
return False
def __getitem__(self, key):
try:
# first try to obtain the value from the cache
with self._mutex:
value = self._values_cache[key]
# cache hit if no KeyError is raised
self.hits += 1
# treat the end as most recently used
self._values_cache.move_to_end(key)
return value
except KeyError:
# cache miss, retrieve value from the store
# print("cache miss", key)
# wait_for_data = True means do the read right away.
# Note that this blocks the calling program until
# the data has been read.
# wait_for_data = False means submit the request
# to the thread pool, then return.
wait_for_data = False
if self.immediate_data_mode:
wait_for_data = True
elif len(key) == 0: # not sure this ever happens
wait_for_data = True
else:
# Check if key is the name of a metadata file
# (for instance, '0/.zarray'), in which case
# the value must be read immediately
parts = key.split('/')
if parts[-1][0] == '.':
wait_for_data = True
raise_error = False
with self._mutex:
# check whether
# key is known to correspond to an all-zeros volume,
# or key has already been submitted to the thread queue:
if key in self.zero_vols or key in self.submitted:
# this tells the caller to treat the current
# chunk as all zeros
raise_error = True
if key not in self.zero_vols:
self.nz_misses += 1
if not raise_error and not wait_for_data:
# the add() is done here, instead of below,
# where the request is submitted, because here
# the add() operation is protected by the _mutex
self.submitted.add(key)
# print("submitted",self.submitted)
# the "if wait_for_data" clause below ignores whether
# raise_error has been set. This is intentional;
# if wait_for_data is set, hand all control to
# the getValue call, and let it decide for
# itself whether to raise an error.
if wait_for_data: # read the value immediately
value = self.getValue(key)
self.cacheValue(key, value)
return value
elif raise_error:
raise KeyError(key)
else: # submit to the thread pool a request to read the value
future = self.executor.submit(self.getValue, key)
future.add_done_callback(lambda x: self.processValue(key, x))
raise KeyError(key)
def getValue(self, key):
# print("getValue", key)
value = self._store[key]
# print(" found", key)
return value
def cacheValue(self, key, value):
with self._mutex:
self.misses += 1
# need to check if key is not in the cache, as it
# may have been cached
# while we were retrieving the value from the store
if key not in self._values_cache:
# print("pv caching",key)
self._cache_value(key, value)
# print(" pv done")
# This is called when the thread reports that it has
# completed the getValue (disk read) operation
def processValue(self, key, future):
# print("pv", key)
with self._mutex:
self.submitted.discard(key)
# print("pv submitted", self.submitted)
try:
# get the result
value = future.result()
# print("pv got value")
except KeyError:
# KeyError implies that the data store has no file
# corresponding to this key, meaning that the data
# for this chunk is all zeros.
# The "return" statement below means that this
# KeyError will be relayed to the caller, who will
# know what it means (chunk is all zeros).
# print("pv key error", key)
with self._mutex:
self.zero_vols.add(key)
if self.future_done_callback is not None:
self.future_done_callback(key, False)
return
self.cacheValue(key, value)
if self.future_done_callback is not None:
self.future_done_callback(key, True)
class ZarrLevel():
def __init__(self, array, path, scale, ilevel, max_mem_gb, from_vc_render=False, original_dtype=None):
klru = KhartesThreadedLRUCache(
array.store, max_size=int(max_mem_gb*2**30))
self.klru = klru
self.ilevel = ilevel
self.data = zarr.open(klru, mode="r")
if path != "":
self.data = self.data[path]
self.scale = scale
# don't know if self.from_vc_render will ever be used
self.from_vc_render = from_vc_render
self.original_dtype = original_dtype
self.trdatas = []
self.trdatas.append(TransposedDataView(self.data, 0, from_vc_render, original_dtype))
self.trdatas.append(TransposedDataView(self.data, 1, from_vc_render, original_dtype))
# The callback takes 2 arguments: key (a string) and
# has_data (a bool). key is the key of the chunk that
# the thread was reading. has_data is set to True if the
# chunk contains data (i.e. there is a corresponding file)
# and false if the chunk is all zeros (there is no
# corresponding file).
# The return value, if any, of the callback is ignored.
def setCallback(self, cb):
self.klru.future_done_callback = cb
def setImmediateDataMode(self, flag):
self.klru.setImmediateDataMode(flag)
class CachedZarrVolume():
"""An interface to cached volume data stored on disk as .tif files
but not fully loaded into memory.
Mimics the Khartes-based interface so we can use it instead of the
existing Volume() class.
There are four different styles of coordinates used:
- Global - [z, y, x]: equivalent to indexing directly in the full zarr
- Data - [z, y, x]: for standard volumes, direct indexing in the data array,
and offset (and potentially slices) relative to the global data.
For cached zarr volumes, this is equivalent to global.
- Transposed (direction=0) - [x, z, y]: Equivalent to data but with the indices
transposed.
- Transposed (direction=1) - [y, z, x]: Equivalent to data but with the indices
transposed.
"""
def __init__(self):
self.data = None
self.trdatas = None
self.is_zarr = True
self.data_header = None
self.original_dtype = None
self.valid = False
self.error = "no error message set"
self.active_project_views = set()
self.from_vc_render = False
self.levels = []
# class member
max_mem_gb = 8
@property
def shape(self):
shape = self.data.shape
if self.from_vc_render:
shape = (shape[1],shape[0],shape[2])
return shape
def trshape(self, direction):
shape = self.shape
if self.from_vc_render:
shape = (shape[1],shape[0],shape[2])
if direction == 0:
return (shape[2], shape[0], shape[1])
else:
return (shape[1], shape[0], shape[2])
@staticmethod
def createErrorVolume(error):
"""Creates and returns an empty volume with an error message set
"""
vol = CachedZarrVolume()
vol.error = error
return vol
@staticmethod
def sliceSize(start, stop, step):
"""Counts the number of items in a particular slice.
"""
if step != 1:
raise ValueError("Zarr volumes do not support slicing")
width = stop - start
quotient = width // step
remainder = width % step
size = quotient
if remainder != 0:
size += 1
return size
@staticmethod
def globalIjksToTransposedGlobalIjks(gijks, direction):
if direction == 0:
return gijks[:,(1,2,0)]
else:
return gijks[:,(0,2,1)]
@staticmethod
def transposedGlobalIjksToGlobalIjks(tijks, direction):
if direction == 0:
return tijks[:, (2,0,1)]
else:
return tijks[:,(0,2,1)]
@staticmethod
def sortVolumeList(vols):
"""Does an in-place sort of a list of volumes by name"""
vols.sort(key=lambda v: v.name)
@staticmethod
def createFromDataStore(
project,
ds_directory,
ds_directory_name,
name,
from_vc_render=False
):
"""
Generates a new volume object from a zarr directory
"""
tdir = pathlib.Path(ds_directory)
if not tdir.is_dir():
err = f"{tdir} is not a directory"
print(err)
return CachedZarrVolume.createErrorVolume(err)
output_filename = name
if not output_filename.endswith(".volzarr"):
output_filename += ".volzarr"
filepath = os.path.join(project.volumes_path, output_filename)
if os.path.exists(filepath):
err = f"{filepath} already exists"
print(err)
return CachedZarrVolume.createErrorVolume(err)
timestamp = Utils.timestamp()
# max_width in header will be ignored by the latest versions
# of khartes, but it is kept here for compatibility
# with older versions
header = {
"khartes_version": "1.0",
"khartes_created": timestamp,
"khartes_modified": timestamp,
"khartes_from_vc_render": from_vc_render,
ds_directory_name: str(ds_directory),
"max_width": 240,
}
# Write out the project file
with open(filepath, "w") as outfile:
# switched from old format to json
# for key, value in header.items():
# outfile.write(f"{key}\t{value}\n")
json.dump(header, outfile, indent=4)
volume = CachedZarrVolume.loadFile(filepath)
# print("about to set callback")
project.addVolume(volume)
return volume
@staticmethod
def createFromZarr(
project,
zarr_directory,
name,
from_vc_render=False
):
return CachedZarrVolume.createFromDataStore(
project,
zarr_directory,
"zarr_dir",
name,
from_vc_render
)
@staticmethod
def createFromTiffs(
project,
tiff_directory,
name,
from_vc_render=False
):
return CachedZarrVolume.createFromDataStore(
project,
tiff_directory,
"tiff_dir",
name,
from_vc_render
)
@staticmethod
def loadFile(filename):
try:
try:
with open(filename, "r") as infile:
header = json.load(infile)
# print("json", header)
except:
with open(filename, "r") as infile:
# old format
header = {}
for line in infile:
key, value = line.split("\t")
header[key] = value
# print("old format", header)
tiff_directory = header.get("tiff_dir", None)
zarr_directory = header.get("zarr_dir", None)
# max_width = int(header.get("max_width", 0))
except Exception as e:
err = f"Failed to read input file {filename} (error {e})"
print(err)
return CachedZarrVolume.createErrorVolume(err)
if tiff_directory is None and zarr_directory is None:
err = f"Input file {filename} does not specify a data store"
print(err)
return CachedZarrVolume.createErrorVolume(err)
volume = CachedZarrVolume()
volume.data_header = header
# volume.max_width = max_width
volume.path = filename
# _, volume.name = os.path.split(filename)
_, name = os.path.split(filename)
if name.endswith(".volzarr") and len(name) > 8:
name = name[:-8]
volume.name = name
volume.version = float(header.get("khartes_version", 0.0))
volume.created = header.get("khartes_created", "")
volume.modified = header.get("khartes_modified", "")
from_vc_render = header.get("khartes_from_vc_render", False)
volume.from_vc_render = from_vc_render
# These are set in common for all zarr arrays: they always start
# at the global origin with no stepping.
volume.gijk_starts = [0, 0, 0]
volume.gijk_steps = [1, 1, 1]
ddir = ""
if tiff_directory is not None:
ddir = tiff_directory.strip()
elif zarr_directory is not None:
ddir = zarr_directory.strip()
try:
if tiff_directory is not None:
print(f"Loading tiff directory {ddir}")
array = load_tif(ddir)
elif zarr_directory is not None:
print(f"Loading zarr directory {ddir}")
array = load_zarr(ddir)
except Exception as e:
err = f"Failed to read input directory {ddir}\n specified in {filename} (error {e})"
print(err)
return CachedZarrVolume.createErrorVolume(err)
if isinstance(array, zarr.hierarchy.Group):
volume.setLevelsFromHierarchy(array, CachedZarrVolume.max_mem_gb)
else:
volume.setLevelFromArray(array, CachedZarrVolume.max_mem_gb)
if len(volume.levels) < 1:
err = f"Problem parsing zdata from input directory {ddir}"
print(err)
return CachedZarrVolume.createErrorVolume(err)
# print("len levels", len(volume.levels))
volume.data = volume.levels[0].data
volume.valid = True
volume.trdatas = []
volume.trdatas.append(TransposedDataView(volume.data, 0, from_vc_render, volume.data.dtype))
volume.trdatas.append(TransposedDataView(volume.data, 1, from_vc_render, volume.data.dtype))
volume.sizes = [int(size) for size in volume.data.shape]
# volume.sizes is in ijk order, volume.data.shape is in kji order
volume.sizes.reverse()
volume.sizes = tuple(volume.sizes)
if volume.from_vc_render:
volume.sizes = (volume.sizes[0],volume.sizes[2],volume.sizes[1])
return volume
def setLevelFromArray(self, array, max_mem_gb):
self.original_dtype = array.dtype
level = ZarrLevel(array, "", 1., 0, max_mem_gb, self.from_vc_render, self.original_dtype)
self.levels.append(level)
def parseMetadata(self, hier):
adict = hier.attrs.asdict()
if "multiscales" not in adict:
print("'multiscales' missing from metadata")
return None
ms = adict["multiscales"]
if not isinstance(ms, list):
print("'multiscales' in metadata is not a list")
return None
if len(ms) < 1:
print("'multiscales' in metadata is a zero-length list")
return None
ms0 = ms[0]
if not isinstance(ms0, dict):
print("multiscales[0] is not a dict")
return None
if "datasets" not in ms0:
print("'datasets' missing from multiscales[0]")
return None
ds = ms0["datasets"]
if not isinstance(ds, list):
print("datasets is not a list")
return None
return ds
def parseLevelMetadata(self, lmd):
if not isinstance(lmd, dict):
print("lmd is not a dict")
return None
if "path" not in lmd:
print("'path' not in lmd")
return None
path = lmd['path']
if "coordinateTransformations" not in lmd:
print("'coordinateTransformations' not in lmd")
return None
xfms = lmd["coordinateTransformations"]
scales = None
for xfm in xfms:
if not isinstance(xfm, dict):
continue
if "scale" not in xfm:
continue
scales = xfm["scale"]
break
if scales is None:
print("Could not find 'scale'")
return None
if not isinstance(scales, list):
print("'scale' is not a list")
return None
if len(scales) != 3:
print("'scale' is wrong length")
return None
scale = 0.
for s in scales:
if scale == 0.:
scale = s
elif s != scale:
print("scales are inconsistent")
return None
return (path, scale)
def setLevelsFromHierarchy(self, hier, max_mem_gb):
# divide metadata into parts, one per level
metadata = self.parseMetadata(hier)
if metadata is None:
print("Problem parsing metadata")
return
expected_scale = 1.
expected_path_int = 0
max_gb = .5*max_mem_gb
# create this solely for the purpose of getting the chunk size
level0 = ZarrLevel(hier, '0', 1., 0, 0, self.from_vc_render)
chunk = level0.data.chunks
min_max_gb = 3*16*2*chunk[0]*chunk[1]*chunk[2]/(2**30)
for i, lmd in enumerate(metadata):
# for each array in hierarchy, parse level metadata
info = self.parseLevelMetadata(lmd)
if info is None:
print(f"Problem parsing level {i} metadata")
return
path, scale = info
# make sure scale and path are as expected
try:
path_int = int(path)
except:
print(f"Level {i}: path {path} is not an integer")
return
if path_int != expected_path_int:
print(f"Level {i} expected path {expected_path_int}, got {path}")
return
if scale != expected_scale:
print(f"Level {i} expected scale {expected_scale}, got {scale}")
return
# calculate local max_mem_gb
max_gb = max(max_gb, min_max_gb)
# Get the dtype of the zarr array without loading it into memory
self.original_dtype = hier[path].dtype
# Create a custom ZarrLevel that handles the dtype conversion
level = ZarrLevel(hier, path, scale, i, max(min_max_gb, max_gb), self.from_vc_render, self.original_dtype)
self.levels.append(level)
expected_scale *= 2.
expected_path_int += 1
max_gb *= .5
def setImmediateDataMode(self, flag):
for level in self.levels:
level.setImmediateDataMode(flag)
def setCallback(self, cb):
print("setting callback")
for level in self.levels:
level.setCallback(cb)
def dataSize(self):
"""Size of the whole dataset in bytes
"""
if self.data_header is None:
return 0
# TODO: fix assumption that data words are two bytes (uint16)
return 2 * self.sizes[0] * self.sizes[1] * self.sizes[2]
def averageStepSize(self):
"""Computes the geometric average of steps in all dimensions. Note
that zarr arrays do not support steps so we can set this directly to 1.
"""
return 1
def setVoxelSizeUm(self, voxelSizeUm):
self.apparentVoxelSize = self.averageStepSize() * voxelSizeUm
def corners(self):
"""Returns a numpy array containing the Global positions of
the corners of the dataset.
"""
gmin = np.array([0, 0, 0], dtype=np.int32)
gmax = np.array(self.sizes, dtype=np.int32) - 1
return np.array((gmin, gmax))
def loadData(self, project_view):
self.active_project_views.add(project_view)
def unloadData(self, project_view):
self.active_project_views.discard(project_view)
# self.data.store.invalidate()
for level in self.levels:
level.data.store.invalidate()
def createTransposedData(self):
pass
def ijkToTransposedIjk(self, ijk, direction):
i,j,k = ijk
if direction == 0:
return (j,k,i)
else:
return (i,k,j)
def transposedIjkToIjk(self, ijkt, direction):
it,jt,kt = ijkt
if direction == 0:
return (kt,it,jt)
else:
return (it,kt,jt)
def transposedIjkToGlobalPosition(self, ijkt, direction):
return self.transposedIjkToIjk(ijkt, direction)
def globalPositionsToTransposedIjks(self, gpoints, direction):
if direction == 0:
return gpoints[:,(1,2,0)]
else:
return gpoints[:,(0,2,1)]
def transposedIjksToGlobalPositions(self, ijkts, direction):
if direction == 0:
return ijkts[:,(2,0,1)]
else:
return ijkts[:,(0,2,1)]
def getGlobalRanges(self):
arr = []
for i in range(3):
# Note we reverse the indices here
# because arr is in ijk order, but shape is in kji order
arr.append([0, self.data.shape[2 - i]])
return arr
def globalAxisFromTransposedAxis(self, axis, direction):
if direction == 0:
return (axis + 1) % 3
else:
return (0,2,1)[axis]
def getSliceShape(self, axis, zarr_max_width, direction):
# single-resolution zarr file, not multi-resolution OME
if len(self.levels) == 1:
sz = zarr_max_width
return sz, sz
# OME
shape = self.trdatas[direction].shape
if axis == 2: # depth
return shape[1],shape[2]
elif axis == 1: # xline
return shape[0],shape[2]
else: # inline
return shape[0],shape[1]
# Get bounds of slice after taking into account
# possible windowing (slices from single-resolution zarr
# data stores are windowed in order to avoid creating
# a giant high-resolution slice when the user zooms out)
def getSliceBounds(self, axis, ijkt, zarr_max_width, direction):
idxi, idxj = self.ijIndexesInPlaneOfSlice(axis)
shape = self.trdatas[direction].shape
# shape is in kji order
ni = shape[2-idxi]
nj = shape[2-idxj]
r = ((0,0),(ni,nj))
# single-resolution zarr file, not multi-resolution OME
if len(self.levels) == 1:
sz = zarr_max_width//2
i = ijkt[idxi]
j = ijkt[idxj]
rw = ((i-sz,j-sz),(i+sz,j+sz))
r = Utils.rectIntersection(r, rw)
return r
# Need data, not just direction, since data is tied to
# a particular level
def getSliceInRange(self, data, islice, jslice, k, axis):
i, j = self.ijIndexesInPlaneOfSlice(axis)
slices = [0]*3
slices[axis] = k
slices[i] = islice
slices[j] = jslice
result = data[slices[2],slices[1],slices[0]]
# print(islice, jslice, k, data.shape, axis, result.shape)
return result
# returns True if out has been completely painted,
# False otherwise
def paintLevel(self, out, axis, oijkt, zoom, direction, level, draw, zarr_max_width):
# if not draw:
# return True
# mask = (out == 0).astype(np.uint8)
mask = (out == 0)
msum = mask.sum()
if msum == 0: # no zeros
return True
if msum != out.shape[0]*out.shape[1]: # some but not all zeros
# dilate the mask by one pixel
# to avoid small black lines from appearing
# (cause unknown!) during loading
kernel = np.ones((3,3),dtype=np.bool_)
mask = ndimage.binary_dilation(mask, kernel)
pass
scale = level.scale
data = level.trdatas[direction]
z = zoom*scale
it,jt,kt = oijkt
iscale = int(scale)
it = it//iscale
jt = jt//iscale
kt = kt//iscale
ijkt = (it,jt,kt)
wh,ww = out.shape
whw = ww//2
whh = wh//2
il, jl = self.ijIndexesInPlaneOfSlice(axis)
fi, fj = ijkt[il], ijkt[jl]
# slice width, height
# shape is in kji order
sw = data.shape[2-il]
sh = data.shape[2-jl]
# print("sw,sh",z,il,jl,fi,fj,sw,sh)
# print(axis, scale, sw, sh)
zsw = max(int(z*sw), 1)
zsh = max(int(z*sh), 1)
# all coordinates below are in drawing window coordinates,
# unless specified otherwise
# location of upper left corner of data slice:
ax1 = int(whw-z*fi)
ay1 = int(whh-z*fj)
# location of lower right corner of data slice:
ax2 = ax1+zsw
ay2 = ay1+zsh
# cx1 etc are a copy of the upper left corner of the data slice
(cx1,cy1),(cx2,cy2) = ((ax1,ay1),(ax2,ay2))
# print("fi,j", fi, fj)
# print("a", ((ax1,ay1),(ax2,ay2)))
if zarr_max_width > 0:
rb = self.getSliceBounds(axis, ijkt, zarr_max_width, direction)
# print("rb",rb)
if rb is None:
return True
((bsx1,bsy1),(bsx2,bsy2)) = rb
cx1 = int(whw+z*(bsx1-fi))
cy1 = int(whh+z*(bsy1-fj))
cx2 = int(whw+z*(bsx2-fi))
cy2 = int(whh+z*(bsy2-fj))
# print("c", ((cx1,cy1),(cx2,cy2)))
# locations of upper left and lower right corners of drawing window
bx1 = 0
by1 = 0
bx2 = ww
by2 = wh
# intersection of data slice and drawing window
ri = Utils.rectIntersection(
((cx1,cy1),(cx2,cy2)), ((bx1,by1),(bx2,by2)))
# print("ri", ri)
misses0 = level.klru.nz_misses
if ri is not None:
# upper left and lower right corners of intersected rectangle
(x1,y1),(x2,y2) = ri
# corners of windowed data slice, in
# data slice coordinates
# note the use here of ax1 etc, which are the
# corners of the data slice, before intersection
# with the limited data window.
# These are still needed for coordinate transformations
x1s = int((x1-ax1)/z)
y1s = int((y1-ay1)/z)
x2s = int((x2-ax1)/z)