22
22
# Offset = Tuple[int, int, int]
23
23
from .validate import validate_by_template_matching
24
24
25
- def type_is_valid (type : str ):
25
+ def layer_type_is_valid (type : str ):
26
26
return type in set ([None , 'image' , 'segmentation' , 'probability_map' , 'affinity_map' , 'unknown' ])
27
27
28
28
29
29
class Chunk (NDArrayOperatorsMixin ):
30
30
def __init__ (self , array : np .ndarray ,
31
31
voxel_offset : Cartesian = None ,
32
32
voxel_size : Cartesian = None ,
33
- type : str = None ):
33
+ layer_type : str = None ):
34
34
"""chunk of a volume
35
35
36
36
a chunk of big array with offset
@@ -49,7 +49,7 @@ def __init__(self, array: np.ndarray,
49
49
"""
50
50
assert array .ndim >= 3 and array .ndim <= 4
51
51
assert isinstance (array , np .ndarray ) or isinstance (array , Chunk )
52
- assert type_is_valid ( type )
52
+ assert layer_type_is_valid ( layer_type ), f'layer type: { layer_type } is unsupported!'
53
53
54
54
self .array = array
55
55
if voxel_offset is None :
@@ -76,20 +76,20 @@ def __init__(self, array: np.ndarray,
76
76
assert len (voxel_size ) == 3
77
77
assert np .alltrue ([vs > 0 for vs in voxel_size ])
78
78
79
- if type is not None :
80
- self .type = type
79
+ if layer_type is not None :
80
+ self .layer_type = layer_type
81
81
else :
82
82
# best guess
83
83
if self .is_image :
84
- self .type = 'image'
84
+ self .layer_type = 'image'
85
85
elif self .is_segmentation :
86
- self .type = 'segmentation'
86
+ self .layer_type = 'segmentation'
87
87
elif self .is_probability_map :
88
- self .type = 'probability_map'
88
+ self .layer_type = 'probability_map'
89
89
elif self .is_affinity_map :
90
- self .type = 'affinity_map'
90
+ self .layer_type = 'affinity_map'
91
91
else :
92
- self .type = 'unknown'
92
+ self .layer_type = 'unknown'
93
93
94
94
# One might also consider adding the built-in list type to this
95
95
# list, to support operations like np.add(array_like, list)
@@ -268,7 +268,7 @@ def from_h5(cls, file_name: str,
268
268
cutout_stop : tuple = None ,
269
269
cutout_size : tuple = None ,
270
270
dtype : str = None ,
271
- type : str = None ):
271
+ layer_type : str = None ):
272
272
273
273
file_name = os .path .expanduser (file_name )
274
274
if not os .path .exists (file_name ):
@@ -310,17 +310,23 @@ def from_h5(cls, file_name: str,
310
310
else :
311
311
voxel_size = Cartesian (1 , 1 , 1 )
312
312
313
- if type is None :
314
- if 'type ' in f .attrs :
315
- type = f .attrs ['type ' ]
313
+ if layer_type is None :
314
+ if 'layer_type ' in f .attrs :
315
+ layer_type = f .attrs ['layer_type ' ]
316
316
# type = str(f['type'])
317
- breakpoint ()
318
- assert type_is_valid (type )
317
+ assert layer_type_is_valid (layer_type )
319
318
320
319
if cutout_start is None :
321
320
cutout_start = voxel_offset
322
321
if cutout_size is None :
323
322
cutout_size = dset .shape [- 3 :]
323
+ cutout_size = Cartesian .from_collection (cutout_size )
324
+ elif np .min (cutout_size ) < 0 :
325
+ cutout_size = [x for x in cutout_size ]
326
+ for idx in range (- 1 , - 4 , - 1 ):
327
+ if cutout_size [idx ]< 0 :
328
+ cutout_size [idx ] = dset .shape [idx ]
329
+ cutout_size = Cartesian .from_collection (cutout_size )
324
330
if cutout_stop is None :
325
331
cutout_stop = tuple (t + s for t , s in zip (cutout_start , cutout_size ))
326
332
@@ -346,7 +352,7 @@ def from_h5(cls, file_name: str,
346
352
347
353
logging .info (f'new chunk voxel offset: { cutout_start } ' )
348
354
349
- return cls (arr , voxel_offset = cutout_start , voxel_size = voxel_size , type = type )
355
+ return cls (arr , voxel_offset = cutout_start , voxel_size = voxel_size , layer_type = layer_type )
350
356
351
357
def to_h5 (self , file_name : str , with_offset : bool = True ,
352
358
chunk_size : Union [Cartesian , tuple ] = (8 ,8 ,8 ),
@@ -378,8 +384,8 @@ def to_h5(self, file_name: str, with_offset: bool=True,
378
384
voxel_size = self .voxel_size
379
385
if voxel_size is not None :
380
386
f .create_dataset ('/voxel_size' , data = voxel_size )
381
- if self .type is not None :
382
- f .attrs ['type ' ] = self .type
387
+ if self .layer_type is not None :
388
+ f .attrs ['layer_type ' ] = self .layer_type
383
389
384
390
if with_offset and self .voxel_offset is not None :
385
391
f .create_dataset ('/voxel_offset' , data = self .voxel_offset )
@@ -493,36 +499,15 @@ def is_affinity_map(self) -> bool:
493
499
def is_probability_map (self ) -> bool :
494
500
return self .array .ndim == 4 and self .array .dtype == np .float32
495
501
496
- # @property
497
- # def type(self) -> str:
498
- # if self.data_type is None:
499
- # if self.is_image:
500
- # self.type = 'image'
501
- # elif self.is_segmentation:
502
- # self.type = 'segmentation'
503
- # elif self.is_probability_map:
504
- # self.type = 'probability_map'
505
- # elif self.is_affinity_map:
506
- # self.type = 'affinity_map'
507
- # else:
508
- # self.type = 'unknown'
509
-
510
- # return self.type
511
-
512
- # @type.setter
513
- # def type(self, value: str):
514
- # assert value in set([None, 'image', 'segmentation', 'probability_map', 'affinity_map', 'unknown'])
515
- # self.type = value
516
-
517
502
@property
518
503
def properties (self ) -> dict :
519
504
props = dict ()
520
505
if self .voxel_offset is not None or self .voxel_offset != Cartesian (0 , 0 , 0 ):
521
506
props ['voxel_offset' ] = self .voxel_offset
522
507
if self .voxel_size is not None or self .voxel_size != Cartesian (1 , 1 , 1 ):
523
508
props ['voxel_size' ] = self .voxel_size
524
- if self .type is not None :
525
- props ['type ' ] = self .type
509
+ if self .layer_type is not None :
510
+ props ['layer_type ' ] = self .layer_type
526
511
527
512
return props
528
513
@@ -533,8 +518,8 @@ def set_properties(self, properties: dict):
533
518
if 'voxel_size' in properties :
534
519
self .voxel_size = properties ['voxel_size' ]
535
520
536
- if 'type ' in properties :
537
- self .type = properties ['type ' ]
521
+ if 'layer_type ' in properties :
522
+ self .layer_type = properties ['layer_type ' ]
538
523
539
524
@properties .setter
540
525
def properties (self , value : dict ):
@@ -643,7 +628,7 @@ def channel_voting(self):
643
628
return Chunk (out ,
644
629
voxel_offset = self .voxel_offset ,
645
630
voxel_size = self .voxel_size ,
646
- type = 'segmentation' ,
631
+ layer_type = 'segmentation' ,
647
632
)
648
633
649
634
def mask_using_last_channel (self , threshold : float = 0.3 ) -> np .ndarray :
@@ -742,7 +727,7 @@ def cutout(self, x: Union[tuple, BoundingBox, Bbox]):
742
727
return Chunk (arr ,
743
728
voxel_offset = voxel_offset ,
744
729
voxel_size = self .voxel_size ,
745
- type = self .type )
730
+ layer_type = self .layer_type )
746
731
747
732
def save (self , patch ):
748
733
"""
0 commit comments