@@ -437,6 +437,16 @@ def rotation(self)->np.ndarray:
437437 rotation = rotation_zoom / zoom
438438 return rotation
439439
440+ @property
441+ def direction (self ) -> list :
442+ direction_matrix = self .rotation
443+ direction_flat = direction_matrix .flatten ().tolist ()
444+ return direction_flat
445+ @property
446+ def direction_itk (self ) -> list :
447+ a = np .array (self .direction )
448+ a [:len (a )// 3 * 2 ]*= - 1
449+ return a .tolist ()
440450
441451 @orientation .setter
442452 def orientation (self , value : AX_CODES ):
@@ -488,7 +498,7 @@ def get_array(self) -> np.ndarray:
488498 return self ._arr .copy ()
489499 def numpy (self , * _args ):
490500 return self .get_array ()
491- def set_array (self ,arr :np .ndarray | Self , inplace = False ,verbose :logging = False )-> Self : # noqa: ARG002
501+ def set_array (self ,arr :np .ndarray | Self , inplace = False ,verbose :logging = False , seg = None )-> Self : # noqa: ARG002
492502 """Creates a NII where the array is replaces with the input array.
493503
494504 Note: This function works "Out-of-place" by default, like all other methods.
@@ -500,6 +510,7 @@ def set_array(self,arr:np.ndarray|Self, inplace=False,verbose:logging=False)-> S
500510 Returns:
501511 self
502512 """
513+
503514 if hasattr (arr ,"get_array" ):
504515 arr = arr .get_array () # type: ignore
505516 if arr .dtype == bool :
@@ -520,10 +531,12 @@ def set_array(self,arr:np.ndarray|Self, inplace=False,verbose:logging=False)-> S
520531 #if all(a is None for a in self.header.get_slope_inter()):
521532 # nii.header.set_slope_inter(1,self.get_c_val()) # type: ignore
522533 if inplace :
534+ if seg is not None :
535+ self .seg = seg
523536 self .nii = nii
524537 return self
525538 else :
526- return self .copy (nii ) # type: ignore
539+ return self .copy (nii , seg = seg ) # type: ignore
527540
528541 def set_array_ (self ,arr :np .ndarray ,verbose :logging = True ):
529542 return self .set_array (arr ,inplace = True ,verbose = verbose )
@@ -1255,13 +1268,13 @@ def filter_connected_components(self, labels: int |list[int]|None=None,min_volum
12551268 """
12561269 assert self .seg , "This only works on segmentations"
12571270 arr = np_filter_connected_components (self .get_seg_array (), largest_k_components = max_count_component ,label_ref = labels ,connectivity = connectivity ,return_original_labels = keep_label ,min_volume = min_volume ,max_volume = max_volume ,removed_to_label = removed_to_label ,)
1258- # if keep_label and labels is not None:
1259- # if isinstance(labels,int):
1260- # labels = [labels]
1261- # old_labels = [i for i in self.unique() if i not in labels]
1262- # if len(old_labels) != 0:
1263- # s = self.extract_label(old_labels,keep_label=True)
1264- # nii [s != 0] = s[s!=0]
1271+ if keep_label and labels is not None :
1272+ if isinstance (labels ,int ):
1273+ labels = [labels ]
1274+ old_labels = [i for i in self .unique () if i not in labels ]
1275+ if len (old_labels ) != 0 :
1276+ s = self .extract_label (old_labels ,keep_label = True ). get_array ( )
1277+ arr [s != 0 ] = s [s != 0 ]
12651278 #print("filter",nii.unique())
12661279 #assert max_count_component is None or nii.max() <= max_count_component, nii.unique()
12671280 return self .set_array (arr , inplace = inplace )
@@ -1648,10 +1661,10 @@ def map_labels(self, label_map:LABEL_MAP , verbose:logging=True, inplace=False):
16481661 def map_labels_ (self , label_map : LABEL_MAP , verbose :logging = True ):
16491662 return self .map_labels (label_map ,verbose = verbose ,inplace = True )
16501663
1651- def copy (self , nib :Nifti1Image | _unpacked_nii | None = None ):
1664+ def copy (self , nib :Nifti1Image | _unpacked_nii | None = None , seg = None ):
16521665 if nib is None :
16531666 nib = (self .get_array ().copy (), self .affine .copy (), self .header .copy ())
1654- return NII (nib ,seg = self .seg ,c_val = self .c_val ,info = self .info )
1667+ return NII (nib ,seg = self .seg if seg is None else seg ,c_val = self .c_val ,info = self .info )
16551668
16561669 def clone (self ):
16571670 return self .copy ()
@@ -1745,9 +1758,10 @@ def __array__(self,dtype=None):
17451758 return self ._arr
17461759 else :
17471760 return self ._arr .astype (dtype , copy = False )
1748- def __array_wrap__ (self , array ):
1761+ def __array_wrap__ (self , array ,context = None , return_scalar = False ):
1762+ assert not return_scalar ,context
17491763 if array .shape != self .shape :
1750- raise SyntaxError (f"Function call induce a shape change of nii image. Before { self .shape } after { array .shape } ." )
1764+ raise SyntaxError (f"Function call induce a shape change of nii image. Before { self .shape } after { array .shape } . { context } " )
17511765 return self .set_array (array )
17521766 def __getitem__ (self , key )-> Any :
17531767 if isinstance (key ,Sequence ):
0 commit comments