@@ -20,6 +20,7 @@ def __init__(
2020 spacing : Optional [Union [List , Tuple , np .ndarray ]] = None ,
2121 origin : Optional [Union [List , Tuple , np .ndarray ]] = None ,
2222 direction : Optional [Union [List , Tuple , np .ndarray ]] = None ,
23+ affine : Optional [Union [List , Tuple , np .ndarray ]] = None ,
2324 meta : Optional [Union [Dict , Meta ]] = None ,
2425 axis_labels : Optional [List [Union [str , AxisLabel ]]] = None ,
2526 copy : Optional ['MLArray' ] = None ,
@@ -49,6 +50,9 @@ def __init__(
4950 direction (Optional[Union[List, Tuple, np.ndarray]]): Direction
5051 cosine matrix. Provide a 2D list/tuple/ndarray with shape
5152 (ndims, ndims) for spatial dimensions.
53+ affine (Optional[Union[List, Tuple, np.ndarray]]): Homogeneous
54+ affine matrix. Provide a 2D list/tuple/ndarray with shape
55+ (spatial_ndims + 1, spatial_ndims + 1).
5256 meta (Optional[Dict | Meta]): Free-form metadata dictionary or Meta
5357 instance. Must be JSON-serializable when saving.
5458 If meta is passed as a Dict, it is internally converted into a
@@ -81,6 +85,7 @@ def __init__(
8185 spacing is not None
8286 or origin is not None
8387 or direction is not None
88+ or affine is not None
8489 or meta is not None
8590 or axis_labels is not None
8691 or copy is not None
@@ -91,14 +96,23 @@ def __init__(
9196 or dparams is not None
9297 ):
9398 raise RuntimeError (
94- "Spacing, origin, direction, meta, axis_labels, copy, patch_size, "
99+ "Spacing, origin, direction, affine, meta, axis_labels, copy, patch_size, "
95100 "chunk_size, block_size, cparams or dparams cannot be set when "
96101 "array is a filepath."
97102 )
98103 if isinstance (array , (str , Path )):
99104 self ._load (array , compressed = compressed )
100105 else :
101- self ._validate_and_add_meta (meta , spacing , origin , direction , axis_labels , False , validate = False )
106+ self ._validate_and_add_meta (
107+ meta ,
108+ spacing = spacing ,
109+ origin = origin ,
110+ direction = direction ,
111+ affine = affine ,
112+ axis_labels = axis_labels ,
113+ has_array = False ,
114+ validate = False ,
115+ )
102116 if array is not None :
103117 self ._asarray (
104118 array ,
@@ -1112,6 +1126,8 @@ def affine(self) -> np.ndarray:
11121126 """
11131127 if self ._store is None or self .meta ._has_array .has_array == False :
11141128 return None
1129+ if self .meta .spatial .affine is not None :
1130+ return self .meta .spatial .affine
11151131 spacing = np .array (self .spacing ) if self .spacing is not None else np .ones (self .spatial_ndim )
11161132 origin = np .array (self .origin ) if self .origin is not None else np .zeros (self .spatial_ndim )
11171133 direction = np .array (self .direction ) if self .direction is not None else np .eye (self .spatial_ndim )
@@ -1824,7 +1840,17 @@ def _comp_and_validate_blosc2_meta(self, meta_blosc2, patch_size, chunk_size, bl
18241840 meta_blosc2 ._validate_and_cast (ndims = len (shape ), spatial_ndims = num_spatial_axes )
18251841 return meta_blosc2
18261842
1827- def _validate_and_add_meta (self , meta , spacing = None , origin = None , direction = None , axis_labels = None , has_array = None , validate = True ):
1843+ def _validate_and_add_meta (
1844+ self ,
1845+ meta ,
1846+ spacing = None ,
1847+ origin = None ,
1848+ direction = None ,
1849+ affine = None ,
1850+ axis_labels = None ,
1851+ has_array = None ,
1852+ validate = True ,
1853+ ):
18281854 """Validate and attach metadata to the MLArray instance.
18291855
18301856 Args:
@@ -1836,6 +1862,8 @@ def _validate_and_add_meta(self, meta, spacing=None, origin=None, direction=None
18361862 spatial axis.
18371863 direction (Optional[Union[List, Tuple, np.ndarray]]): Direction
18381864 cosine matrix with shape (ndims, ndims).
1865+ affine (Optional[Union[List, Tuple, np.ndarray]]): Homogeneous
1866+ affine matrix with shape (spatial_ndims + 1, spatial_ndims + 1).
18391867 axis_labels (Optional[List[Union[str, AxisLabel]]]): Per-axis labels or roles. Length must match ndims.
18401868 has_array (Optional[bool]): Explicitly set whether array data is
18411869 present. When True, metadata is validated with array-dependent
@@ -1853,12 +1881,22 @@ def _validate_and_add_meta(self, meta, spacing=None, origin=None, direction=None
18531881 meta = Meta ()
18541882 self .meta = meta
18551883 self .meta ._mlarray_version = MLARRAY_VERSION
1884+
1885+ if affine is not None and (
1886+ spacing is not None or origin is not None or direction is not None
1887+ ):
1888+ raise ValueError (
1889+ "affine cannot be provided together with spacing, origin, or direction."
1890+ )
1891+
18561892 if spacing is not None :
18571893 self .meta .spatial .spacing = spacing
18581894 if origin is not None :
18591895 self .meta .spatial .origin = origin
18601896 if direction is not None :
18611897 self .meta .spatial .direction = direction
1898+ if affine is not None :
1899+ self .meta .spatial .affine = affine
18621900 if axis_labels is not None :
18631901 self .meta .spatial .axis_labels = axis_labels
18641902 if has_array == True :
0 commit comments