-
Notifications
You must be signed in to change notification settings - Fork 42
/
precomputed.py
1070 lines (853 loc) · 32.3 KB
/
precomputed.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
from typing import Optional, Sequence
import itertools
import gevent.socket
import json
import os
import sys
import uuid
import socket
import fastremap
from six.moves import range
import numpy as np
from tqdm import tqdm
from six import string_types
import multiprocessing as mp
from .. import lib
from ..cacheservice import CacheService
from .. import exceptions
from ..lib import (
colorize, red, mkdir,
Vec, Bbox, jsonify, BboxLikeType,
)
from ..datasource import autocropfn
from ..datasource.precomputed import PrecomputedMetadata
from ..provenance import DataLayerProvenance
from ..storage import SimpleStorage, Storage, reset_connection_pools
from ..volumecutout import VolumeCutout
from .. import sharedmemory
def warn(text):
print(colorize('yellow', text))
class CloudVolumePrecomputed(object):
def __init__(self,
meta, cache, config,
image=None, mesh=None, skeleton=None,
mip=0
):
self.config = config
self.cache = cache
self.meta = meta
self.image = image
self.mesh = mesh
self.skeleton = skeleton
self.green_threads = self.config.green # display warning message
# needs to be set after info is defined since
# its setter is based off of scales
self.mip = mip
self.pid = os.getpid()
@property
def autocrop(self):
return self.image.autocrop
@autocrop.setter
def autocrop(self, val):
self.image.autocrop = val
@property
def background_color(self):
return self.image.background_color
@background_color.setter
def background_color(self, val):
self.image.background_color = val
@property
def bounded(self):
return self.image.bounded
@bounded.setter
def bounded(self, val):
self.image.bounded = val
@property
def fill_missing(self):
return self.image.fill_missing
@fill_missing.setter
def fill_missing(self, val):
self.image.fill_missing = val
@property
def green_threads(self):
return self.config.green
@green_threads.setter
def green_threads(self, val):
if val and socket.socket is not gevent.socket.socket:
warn("""
WARNING: green_threads is set but this process is
not monkey patched. This will cause severely degraded
performance.
CloudVolume uses gevent for cooperative (green)
threading but it requires patching the Python standard
library to perform asynchronous IO. Add this code to
the top of your program (before any other imports):
import gevent.monkey
gevent.monkey.patch_all(threads=False)
More Information:
http://www.gevent.org/intro.html#monkey-patching
""")
self.config.green = bool(val)
@property
def non_aligned_writes(self):
return self.image.non_aligned_writes
@non_aligned_writes.setter
def non_aligned_writes(self, val):
self.image.non_aligned_writes = val
@property
def delete_black_uploads(self):
return self.image.delete_black_uploads
@delete_black_uploads.setter
def delete_black_uploads(self, val):
self.image.delete_black_uploads = val
@property
def parallel(self):
return self.config.parallel
@parallel.setter
def parallel(self, num_processes):
if type(num_processes) == bool:
num_processes = mp.cpu_count() if num_processes == True else 1
elif num_processes <= 0:
raise ValueError('Number of processes must be >= 1. Got: ' + str(num_processes))
else:
num_processes = int(num_processes)
self.config.parallel = num_processes
@property
def cdn_cache(self):
return self.config.cdn_cache
@cdn_cache.setter
def cdn_cache(self, val):
self.config.cdn_cache = val
@property
def compress(self):
return self.config.compress
@compress.setter
def compress(self, val):
self.config.compress = val
@property
def progress(self):
return self.config.progress
@progress.setter
def progress(self, val):
self.config.progress = bool(val)
@property
def info(self):
return self.meta.info
@info.setter
def info(self, val):
self.meta.info = val
@property
def provenance(self):
return self.meta.provenance
@provenance.setter
def provenance(self, val):
self.meta.provenance = val
def __setstate__(self, d):
"""Called when unpickling which is integral to multiprocessing."""
self.__dict__ = d
pid = os.getpid()
if 'pid' in d and d['pid'] != pid:
# otherwise the pickle might have references to old connections
reset_connection_pools()
self.pid = pid
@classmethod
def create_new_info(cls,
num_channels, layer_type, data_type, encoding,
resolution, voxel_offset, volume_size,
mesh=None, skeletons=None, chunk_size=(64,64,64),
compressed_segmentation_block_size=(8,8,8),
max_mip=0, factor=Vec(2,2,1), redirect=None,
*args, **kwargs
):
"""
Create a new neuroglancer Precomputed info file.
Required:
num_channels: (int) 1 for grayscale, 3 for RGB
layer_type: (str) typically "image" or "segmentation"
data_type: (str) e.g. "uint8", "uint16", "uint32", "float32"
encoding: (str) "raw" for binaries like numpy arrays, "jpeg"
resolution: int (x,y,z), x,y,z voxel dimensions in nanometers
voxel_offset: int (x,y,z), beginning of dataset in positive cartesian space
volume_size: int (x,y,z), extent of dataset in cartesian space from voxel_offset
Optional:
mesh: (str) name of mesh directory, typically "mesh"
skeletons: (str) name of skeletons directory, typically "skeletons"
chunk_size: int (x,y,z), dimensions of each downloadable 3D image chunk in voxels
compressed_segmentation_block_size: (x,y,z) dimensions of each compressed sub-block
(only used when encoding is 'compressed_segmentation')
max_mip: (int), the maximum mip level id.
factor: (Vec), the downsampling factor for each mip level
redirect: If this volume has moved, you can set an automatic redirect
by specifying a cloudpath here.
Returns: dict representing a single mip level that's JSON encodable
"""
return PrecomputedMetadata.create_info(
num_channels, layer_type, data_type, encoding,
resolution, voxel_offset, volume_size,
mesh, skeletons, chunk_size,
compressed_segmentation_block_size,
max_mip, factor,
*args, **kwargs
)
def refresh_info(self):
"""Restore the current info from cache or storage."""
return self.meta.refresh_info()
def commit_info(self):
return self.meta.commit_info()
def refresh_provenance(self):
return self.meta.refresh_provenance()
def commit_provenance(self):
return self.meta.commit_provenance()
@property
def dataset_name(self):
return self.meta.dataset
@property
def layer(self):
return self.meta.layer
@property
def mip(self):
return self.config.mip
@mip.setter
def mip(self, mip):
self.config.mip = self.meta.to_mip(mip)
@property
def scales(self):
return self.meta.scales
@scales.setter
def scales(self, val):
self.meta.scales = val
@property
def scale(self):
return self.meta.scale(self.mip)
@scale.setter
def scale(self, val):
self.info['scales'][self.mip] = val
def mip_scale(self, mip):
return self.meta.scale(mip)
@property
def basepath(self):
return self.meta.basepath
@property
def layerpath(self):
return self.meta.layerpath
@property
def base_cloudpath(self):
return self.meta.base_cloudpath
@property
def cloudpath(self):
return self.layer_cloudpath
@property
def layer_cloudpath(self):
return self.meta.cloudpath
@property
def info_cloudpath(self):
return self.meta.infopath
@property
def cache_path(self):
return self.cache.path
@property
def ndim(self):
return len(self.shape)
def mip_ndim(self, mip):
return len(self.meta.shape(mip))
@property
def shape(self):
"""Returns Vec(x,y,z,channels) shape of the volume similar to numpy."""
return tuple(self.meta.shape(self.mip))
def mip_shape(self, mip):
return tuple(self.meta.shape(mip))
@property
def volume_size(self):
"""Returns Vec(x,y,z) shape of the volume (i.e. shape - channels)."""
return self.meta.volume_size(self.mip)
def mip_volume_size(self, mip):
return self.meta.volume_size(mip)
@property
def available_mips(self):
"""Returns a list of mip levels that are defined."""
return self.meta.available_mips
@property
def available_resolutions(self):
"""Returns a list of defined resolutions."""
return (s["resolution"] for s in self.scales)
@property
def layer_type(self):
"""e.g. 'image' or 'segmentation'"""
return self.meta.layer_type
@property
def dtype(self):
"""e.g. 'uint8'"""
return self.meta.dtype
@property
def data_type(self):
return self.meta.data_type
@property
def encoding(self):
"""e.g. 'raw' or 'jpeg'"""
return self.meta.encoding(self.mip)
def mip_encoding(self, mip):
return self.meta.encoding(mip)
@property
def compressed_segmentation_block_size(self):
return self.mip_compressed_segmentation_block_size(self.mip)
def mip_compressed_segmentation_block_size(self, mip):
if 'compressed_segmentation_block_size' in self.info['scales'][mip]:
return self.info['scales'][mip]['compressed_segmentation_block_size']
return None
@property
def num_channels(self):
return self.meta.num_channels
@property
def voxel_offset(self):
"""Vec(x,y,z) start of the dataset in voxels"""
return self.meta.voxel_offset(self.mip)
def mip_voxel_offset(self, mip):
return self.meta.voxel_offset(mip)
@property
def resolution(self):
"""Vec(x,y,z) dimensions of each voxel in nanometers"""
return self.meta.resolution(self.mip)
def mip_resolution(self, mip):
return self.meta.resolution(mip)
@property
def downsample_ratio(self):
"""Describes how downsampled the current mip level is as an (x,y,z) factor triple."""
return self.meta.downsample_ratio(self.mip)
@property
def chunk_size(self):
"""Underlying chunk size dimensions in voxels. Synonym for underlying."""
return self.meta.chunk_size(self.mip)
def mip_chunk_size(self, mip):
return self.meta.chunk_size(mip)
@property
def underlying(self):
"""Underlying chunk size dimensions in voxels. Synonym for chunk_size."""
return self.meta.chunk_size(self.mip)
def mip_underlying(self, mip):
return self.meta.chunk_size(mip)
@property
def key(self):
"""The subdirectory within the data layer containing the chunks for this mip level"""
return self.meta.key(self.mip)
def mip_key(self, mip):
return self.meta.key(mip)
@property
def bounds(self):
"""Returns a bounding box for the dataset with dimensions in voxels"""
return self.meta.bounds(self.mip)
def mip_bounds(self, mip):
offset = self.meta.voxel_offset(mip)
shape = self.meta.volume_size(mip)
return Bbox( offset, offset + shape )
def point_to_mip(self, pt, mip, to_mip):
return self.meta.point_to_mip(pt, mip, to_mip)
def bbox_to_mip(self, bbox, mip, to_mip):
"""Convert bbox or slices from one mip level to another."""
return self.meta.bbox_to_mip(bbox, mip, to_mip)
def slices_to_global_coords(self, slices):
"""
Used to convert from a higher mip level into mip 0 resolution.
"""
bbox = self.meta.bbox_to_mip(slices, self.mip, 0)
return bbox.to_slices()
def slices_from_global_coords(self, slices):
"""
Used for converting from mip 0 coordinates to upper mip level
coordinates. This is mainly useful for debugging since the neuroglancer
client displays the mip 0 coordinates for your cursor.
"""
bbox = self.meta.bbox_to_mip(slices, 0, self.mip)
return bbox.to_slices()
def reset_scales(self):
"""Used for manually resetting downsamples if something messed up."""
self.meta.reset_scales()
return self.commit_info()
def add_scale(self, factor, encoding=None, chunk_size=None, info=None):
"""
Generate a new downsample scale to for the info file and return an updated dictionary.
You'll still need to call self.commit_info() to make it permenant.
Required:
factor: int (x,y,z), e.g. (2,2,1) would represent a reduction of 2x in x and y
Optional:
encoding: force new layer to e.g. jpeg or compressed_segmentation
chunk_size: force new layer to new chunk size
Returns: info dict
"""
return self.meta.add_scale(factor, encoding, chunk_size, info)
def exists(self, bbox_or_slices):
"""
Produce a summary of whether all the requested chunks exist.
bbox_or_slices: accepts either a Bbox or a tuple of slices representing
the requested volume.
Returns: { chunk_file_name: boolean, ... }
"""
return self.image.exists(bbox_or_slices)
def delete(self, bbox_or_slices):
"""
Delete the files within the bounding box.
bbox_or_slices: accepts either a Bbox or a tuple of slices representing
the requested volume.
"""
return self.image.delete(bbox_or_slices)
def transfer_to(self, cloudpath, bbox, block_size=None, compress=True):
"""
Transfer files from one storage location to another, bypassing
volume painting. This enables using a single CloudVolume instance
to transfer big volumes. In some cases, gsutil or aws s3 cli tools
may be more appropriate. This method is provided for convenience. It
may be optimized for better performance over time as demand requires.
cloudpath (str): path to storage layer
bbox (Bbox object): ROI to transfer
block_size (int): number of file chunks to transfer per I/O batch.
compress (bool): Set to False to upload as uncompressed
"""
return self.image.transfer_to(cloudpath, bbox, self.mip, block_size, compress)
def __getitem__(self, slices):
if type(slices) == Bbox:
slices = slices.to_slices()
slices = self.meta.bbox(self.mip).reify_slices(slices, bounded=self.bounded)
steps = Vec(*[ slc.step for slc in slices ])
slices = [ slice(slc.start, slc.stop) for slc in slices ]
channel_slice = slices.pop()
requested_bbox = Bbox.from_slices(slices)
img = self.download(requested_bbox, self.mip)
return img[::steps.x, ::steps.y, ::steps.z, channel_slice]
def unique(
self,
bbox:BboxLikeType,
mip:Optional[int] = None,
# Absorbing polymorphic Graphene calls
agglomerate:Optional[bool] = None,
timestamp:Optional[int] = None,
stop_layer:Optional[int] = None,
# new download arguments
coord_resolution:Optional[Sequence[int]] = None,
):
"""
Downloads segmentation and extracts unique
labels from it without rendering a full image.
Faster and saves memory.
"""
bbox = Bbox.create(
bbox, context=self.bounds,
bounded=(self.bounded and coord_resolution is None),
autocrop=self.autocrop
)
if mip is None:
mip = self.mip
if coord_resolution is not None:
factor = self.meta.resolution(mip) / coord_resolution
bbox /= factor
if self.bounded and not self.meta.bounds(mip).contains_bbox(bbox):
raise exceptions.OutOfBoundsError(f"Computed {bbox} is not contained within bounds {self.meta.bounds(mip)}")
return self.image.unique(
bbox.astype(np.int64), mip
)
def download_files(
self,
bbox:BboxLikeType,
mip:Optional[int] = None,
parallel:Optional[int] = None,
segids:Optional[Sequence[int]] = None,
# Absorbing polymorphic Graphene calls
agglomerate:Optional[bool] = None,
timestamp:Optional[int] = None,
stop_layer:Optional[int] = None,
coord_resolution:Optional[Sequence[int]] = None,
cache_only:bool = False,
decompress:bool = True,
):
"""
Downloads files without rendering to an image.
decompress: automatically decompress downloaded
files. If False, returns the raw bytes.
cache_only: discard downloaded files to avoid
inflating memory. (you must enable the cache separately)
Returns: { filename: binary }
"""
bbox = Bbox.create(
bbox, context=self.bounds,
bounded=(self.bounded and coord_resolution is None),
autocrop=self.autocrop
)
if mip is None:
mip = self.mip
if coord_resolution is not None:
factor = self.meta.resolution(mip) / coord_resolution
bbox /= factor
if self.bounded and not self.meta.bounds(mip).contains_bbox(bbox):
raise exceptions.OutOfBoundsError(f"Computed {bbox} is not contained within bounds {self.meta.bounds(mip)}")
if parallel is None:
parallel = self.parallel
files = self.image.download_files(
bbox.astype(np.int64), mip,
parallel=parallel,
decompress=decompress,
cache_only=cache_only
)
if not segids:
return files
for key in files:
val = files[key]
labels = set(chunks.labels(val))
mask_labels = labels - set(segids)
remap = { lbl: lbl for lbl in segids }
if preserve_zeros:
mask_value = np.inf
if np.issubdtype(self.dtype, np.integer):
mask_value = np.iinfo(self.dtype).max
remap.update({ mask_labels: mask_value for lbl in mask_labels })
if preserve_zeros:
remap[0] = 0
files[key] = chunks.remap(
files[key],
encoding=self.meta.encoding(mip),
shape=self.meta.chunk_size(mip),
dtype=self.meta.dtype,
block_size=self.meta.compressed_segmentation_block_size(mip),
mapping=remap,
preserve_missing_labels=True,
)
return files
def download(
self,
bbox:BboxLikeType,
mip:Optional[int] = None,
parallel:Optional[int] = None,
segids:Optional[Sequence[int]] = None,
preserve_zeros:bool = False,
# Absorbing polymorphic Graphene calls
agglomerate:Optional[bool] = None,
timestamp:Optional[int] = None,
stop_layer:Optional[int] = None,
# new download arguments
renumber:bool = False,
coord_resolution:Optional[Sequence[int]] = None,
) -> VolumeCutout:
"""
Downloads segmentation from the indicated cutout
region.
bbox: specifies cutout to fetch
mip: which resolution level to get (default self.mip)
parallel: what parallel level to use (default self.parallel)
segids: agglomerate the leaves of these segids from the graph
server and label them with the given segid.
preserve_zeros: If segids is not None:
False: mask other segids with zero
True: mask other segids with the largest integer value
contained by the image data type and leave zero as is.
renumber: dynamically rewrite downloaded segmentation into
a more compact data type. Only compatible with single-process
non-sharded download.
coord_resolution: (rx,ry,rz) the coordinate resolution of the input point.
Sometimes Neuroglancer is working in the resolution of another
higher res layer and this can help correct that.
agglomerate, timestamp, and stop_layer are just there to
absorb arguments to what could be a graphene frontend.
Returns: img
"""
bbox = Bbox.create(
bbox, context=self.bounds,
bounded=(self.bounded and coord_resolution is None),
autocrop=self.autocrop
)
if mip is None:
mip = self.mip
if coord_resolution is not None:
factor = self.meta.resolution(mip) / coord_resolution
bbox /= factor
if self.bounded and not self.meta.bounds(mip).contains_bbox(bbox):
raise exceptions.OutOfBoundsError(f"Computed {bbox} is not contained within bounds {self.meta.bounds(mip)}")
if parallel is None:
parallel = self.parallel
tup = self.image.download(
bbox.astype(np.int64), mip, parallel=parallel, renumber=bool(renumber)
)
if renumber:
img, remap = tup
else:
remap = {}
img = tup
if segids is None:
return tup
mask_value = 0
if preserve_zeros:
mask_value = np.inf
if np.issubdtype(self.dtype, np.integer):
mask_value = np.iinfo(self.dtype).max
segids.append(0)
img = fastremap.mask_except(img, segids, in_place=True, value=mask_value)
img = VolumeCutout.from_volume(
self.meta, mip, img, bbox
)
if renumber:
return img, remap
else:
return img
def download_point(
self, pt, size=256,
mip=None, parallel=None,
coord_resolution=None,
**kwargs
):
"""
Download to the right of point given in mip 0 coords.
Useful for quickly visualizing a neuroglancer coordinate
at an arbitary mip level.
pt: (x,y,z)
size: int or (sx,sy,sz)
mip: int representing resolution level
parallel: number of processes to launch (0 means all cores)
coord_resolution: (rx,ry,rz) the coordinate resolution of the input point.
Sometimes Neuroglancer is working in the resolution of another
higher res layer and this can help correct that.
Return: image
"""
if isinstance(size, int):
size = Vec(size, size, size)
else:
size = Vec(*size)
if mip is None:
mip = self.mip
mip = self.meta.to_mip(mip)
size2 = size // 2
if coord_resolution is not None:
factor = self.meta.resolution(0) / Vec(*coord_resolution)
pt = Vec(*pt) / factor
pt = self.point_to_mip(pt, mip=0, to_mip=mip)
bbox = Bbox(pt - size2, pt + size2).astype(np.int64)
for i, sz in enumerate(size):
if sz == 1:
bbox.minpt[i] = pt[i]
bbox.maxpt[i] = pt[i] + 1
if self.autocrop:
bbox = Bbox.intersection(bbox, self.meta.bounds(mip))
bbox = bbox.astype(np.int32)
if parallel is None:
parallel = self.parallel
return self.image.download(bbox, mip, parallel=parallel, **kwargs)
def unlink_shared_memory(self):
"""Unlink the current shared memory location from the filesystem."""
return self.image.unlink_shared_memory()
def download_to_shared_memory(self, slices, location=None, mip=None):
"""
Download images to a shared memory array.
https://github.com/seung-lab/cloud-volume/wiki/Advanced-Topic:-Shared-Memory
tip: If you want to use slice notation, np.s_[...] will help in a pinch.
MEMORY LIFECYCLE WARNING: You are responsible for managing the lifecycle of the
shared memory. CloudVolume will merely write to it, it will not unlink the
memory automatically. To fully clear the shared memory you must unlink the
location and close any mmap file handles. You can use `cloudvolume.sharedmemory.unlink(...)`
to help you unlink the shared memory file or `vol.unlink_shared_memory()` if you do
not specify location (meaning the default instance location is used).
EXPERT MODE WARNING: If you aren't sure you need this function (e.g. to relieve
memory pressure or improve performance in some way) you should use the ordinary
download method of img = vol[:]. A typical use case is transferring arrays between
different processes without making copies. For reference, this feature was created
for downloading a 62 GB array and working with it in Julia.
Required:
slices: (Bbox or list of slices) the bounding box the shared array represents. For instance
if you have a 1024x1024x128 volume and you're uploading only a 512x512x64 corner
touching the origin, your Bbox would be `Bbox( (0,0,0), (512,512,64) )`.
Optional:
location: (str) Defaults to self.shared_memory_id. Shared memory location
e.g. 'cloudvolume-shm-RANDOM-STRING' This typically corresponds to a file
in `/dev/shm` or `/run/shm/`. It can also be a file if you're using that for mmap.
Returns: ndarray backed by shared memory
"""
if mip is None:
mip = self.mip
slices = self.meta.bbox(mip).reify_slices(slices, bounded=self.bounded)
steps = Vec(*[ slc.step for slc in slices ])
channel_slice = slices.pop()
requested_bbox = Bbox.from_slices(slices)
if self.autocrop:
requested_bbox = Bbox.intersection(requested_bbox, self.bounds)
img = self.image.download(
requested_bbox, mip, parallel=self.parallel,
location=location, retain=True, use_shared_memory=True
)
return img[::steps.x, ::steps.y, ::steps.z, channel_slice]
def download_to_file(self, path, bbox, mip=None):
"""
Download images directly to a file.
Required:
slices: (Bbox) the bounding box the shared array represents. For instance
if you have a 1024x1024x128 volume and you're uploading only a 512x512x64 corner
touching the origin, your Bbox would be `Bbox( (0,0,0), (512,512,64) )`.
path: (str)
Optional:
mip: (int; default: self.mip) The current resolution level.
Returns: ndarray backed by an mmapped file
"""
if mip is None:
mip = self.mip
slices = self.meta.bbox(mip).reify_slices(bbox, bounded=self.bounded)
steps = Vec(*[ slc.step for slc in slices ])
channel_slice = slices.pop()
requested_bbox = Bbox.from_slices(slices)
if self.autocrop:
requested_bbox = Bbox.intersection(requested_bbox, self.bounds)
img = self.image.download(
requested_bbox, mip, parallel=self.parallel,
location=lib.toabs(path), retain=True, use_file=True
)
return img[::steps.x, ::steps.y, ::steps.z, channel_slice]
def __setitem__(self, slices, img):
if type(slices) == Bbox:
slices = slices.to_slices()
slices = self.meta.bbox(self.mip).reify_slices(slices, bounded=self.bounded)
bbox = Bbox.from_slices(slices)
slice_shape = list(bbox.size())
bbox = Bbox.from_slices(slices[:3])
if np.isscalar(img):
img = np.zeros(slice_shape, dtype=self.dtype) + img
imgshape = list(img.shape)
if len(imgshape) == 3:
imgshape = imgshape + [ self.num_channels ]
if not np.array_equal(imgshape, slice_shape):
raise exceptions.AlignmentError("""
Input image shape does not match slice shape.
Image Shape: {}
Slice Shape: {}
""".format(imgshape, slice_shape))
if self.autocrop:
if not self.bounds.contains_bbox(bbox):
img, bbox = autocropfn(self.meta, img, bbox, self.mip)
if bbox.subvoxel():
return
self.image.upload(img, bbox.minpt, self.mip, parallel=self.parallel)
def upload_from_shared_memory(self, location, bbox, order='F', cutout_bbox=None):
"""
Upload from a shared memory array.
https://github.com/seung-lab/cloud-volume/wiki/Advanced-Topic:-Shared-Memory
tip: If you want to use slice notation, np.s_[...] will help in a pinch.
MEMORY LIFECYCLE WARNING: You are responsible for managing the lifecycle of the
shared memory. CloudVolume will merely read from it, it will not unlink the
memory automatically. To fully clear the shared memory you must unlink the
location and close any mmap file handles. You can use `cloudvolume.sharedmemory.unlink(...)`
to help you unlink the shared memory file.
EXPERT MODE WARNING: If you aren't sure you need this function (e.g. to relieve
memory pressure or improve performance in some way) you should use the ordinary
upload method of vol[:] = img. A typical use case is transferring arrays between
different processes without making copies. For reference, this feature was created
for uploading a 62 GB array that originated in Julia.
Required:
location: (str) Shared memory location e.g. 'cloudvolume-shm-RANDOM-STRING'
This typically corresponds to a file in `/dev/shm` or `/run/shm/`. It can
also be a file if you're using that for mmap.
bbox: (Bbox or list of slices) the bounding box the shared array represents. For instance
if you have a 1024x1024x128 volume and you're uploading only a 512x512x64 corner
touching the origin, your Bbox would be `Bbox( (0,0,0), (512,512,64) )`.
Optional:
cutout_bbox: (bbox or list of slices) If you only want to upload a section of the
array, give the bbox in volume coordinates (not image coordinates) that should
be cut out. For example, if you only want to upload 256x256x32 of the upper
rightmost corner of the above example but the entire 512x512x64 array is stored
in memory, you would provide: `Bbox( (256, 256, 32), (512, 512, 64) )`
By default, just upload the entire image.
Returns: void
"""
bbox = Bbox.create(bbox)
cutout_bbox = Bbox.create(cutout_bbox) if cutout_bbox else bbox.clone()
if not bbox.contains_bbox(cutout_bbox):
raise exceptions.AlignmentError("""
The provided cutout is not wholly contained in the given array.
Bbox: {}
Cutout: {}
""".format(bbox, cutout_bbox))
if self.autocrop:
cutout_bbox = Bbox.intersection(cutout_bbox, self.bounds)
if cutout_bbox.subvoxel():
return
shape = list(bbox.size3()) + [ self.num_channels ]
mmap_handle, shared_image = sharedmemory.ndarray(
location=location, shape=shape,
dtype=self.dtype, order=order,
readonly=True
)
delta_box = cutout_bbox.clone() - bbox.minpt
cutout_image = shared_image[ delta_box.to_slices() ]
self.image.upload(
cutout_image, cutout_bbox.minpt, self.mip,
parallel=self.parallel,
location=location,
location_bbox=bbox,
order=order,
use_shared_memory=True,
)
mmap_handle.close()
def upload_from_file(self, location, bbox, order='F', cutout_bbox=None):
"""
Upload from an mmapped file.
tip: If you want to use slice notation, np.s_[...] will help in a pinch.
Required:
location: (str) Shared memory location e.g. 'cloudvolume-shm-RANDOM-STRING'
This typically corresponds to a file in `/dev/shm` or `/run/shm/`. It can
also be a file if you're using that for mmap.
bbox: (Bbox or list of slices) the bounding box the shared array represents. For instance
if you have a 1024x1024x128 volume and you're uploading only a 512x512x64 corner
touching the origin, your Bbox would be `Bbox( (0,0,0), (512,512,64) )`.
Optional:
cutout_bbox: (bbox or list of slices) If you only want to upload a section of the
array, give the bbox in volume coordinates (not image coordinates) that should
be cut out. For example, if you only want to upload 256x256x32 of the upper
rightmost corner of the above example but the entire 512x512x64 array is stored
in memory, you would provide: `Bbox( (256, 256, 32), (512, 512, 64) )`