|
29 | 29 | from .base import ImageConverterMixin
|
30 | 30 |
|
31 | 31 |
|
| 32 | +# Function to find and return the third value based on the first value |
| 33 | +def get_dtype_from_code(dtype_code: int) -> Optional[np.dtype]: |
| 34 | + for item in _dtdefs: |
| 35 | + if item[0] == dtype_code: # Check if the first value matches the input code |
| 36 | + return item[2] # Return the third value (dtype) |
| 37 | + return None # Return None if the code is not found |
| 38 | + |
| 39 | + |
32 | 40 | class NiftiReader:
|
33 | 41 | _logger: logging.Logger
|
34 | 42 |
|
@@ -83,7 +91,12 @@ def logger(self) -> Optional[logging.Logger]:
|
83 | 91 |
|
84 | 92 | @property
|
85 | 93 | def group_metadata(self) -> Dict[str, Any]:
|
86 |
| - writer_kwargs = dict(metadata=self._metadata, binaryblock=self._binary_header) |
| 94 | + writer_kwargs = dict( |
| 95 | + metadata=self._metadata, |
| 96 | + binaryblock=self._binary_header, |
| 97 | + slope=self._nib_image.dataobj.slope, |
| 98 | + inter=self._nib_image.dataobj.inter, |
| 99 | + ) |
87 | 100 | self._logger.debug(f"Group metadata: {writer_kwargs}")
|
88 | 101 | return {"json_write_kwargs": json.dumps(writer_kwargs)}
|
89 | 102 |
|
@@ -173,7 +186,7 @@ def level_count(self) -> int:
|
173 | 186 | def level_dtype(self, level: int = 0) -> np.dtype:
|
174 | 187 | header_dict = self.nifti1_hdr_2_dict()
|
175 | 188 |
|
176 |
| - dtype = self.get_dtype_from_code(header_dict["datatype"]) |
| 189 | + dtype = get_dtype_from_code(header_dict["datatype"]) |
177 | 190 | if dtype == np.dtype([("R", "u1"), ("G", "u1"), ("B", "u1")]):
|
178 | 191 | dtype = np.uint8
|
179 | 192 | # TODO: Compare with the dtype of fields
|
@@ -218,8 +231,14 @@ def level_image(
|
218 | 231 | self._metadata["original_mode"] = self._mode
|
219 | 232 | raw_data_contiguous = np.ascontiguousarray(unscaled_img)
|
220 | 233 | numerical_data = np.frombuffer(raw_data_contiguous, dtype=self.level_dtype())
|
| 234 | + # Account endianness |
| 235 | + numerical_data = numerical_data.view( |
| 236 | + numerical_data.dtype.newbyteorder(self._nib_image.header.endianness) |
| 237 | + ) |
221 | 238 | numerical_data = numerical_data.reshape(self.level_shape())
|
222 | 239 |
|
| 240 | + # Bug! data might have slope and inter and header not contain them. |
| 241 | + |
223 | 242 | if tile is None:
|
224 | 243 | return numerical_data
|
225 | 244 | else:
|
@@ -256,13 +275,6 @@ def nifti1_hdr_2_dict(self) -> Dict[str, Any]:
|
256 | 275 | for field in structured_header_arr.dtype.names
|
257 | 276 | }
|
258 | 277 |
|
259 |
| - # Function to find and return the third value based on the first value |
260 |
| - def get_dtype_from_code(self, dtype_code: int) -> np.dtype: |
261 |
| - for item in _dtdefs: |
262 |
| - if item[0] == dtype_code: # Check if the first value matches the input code |
263 |
| - return item[2] # Return the third value (dtype) |
264 |
| - return None # Return None if the code is not foun |
265 |
| - |
266 | 278 | @staticmethod
|
267 | 279 | def _serialize_header(header_dict: Mapping[str, Any]) -> Dict[str, Any]:
|
268 | 280 | serialized_header = {
|
@@ -335,6 +347,10 @@ def write_level_image(
|
335 | 347 | nib_image = self._writer(
|
336 | 348 | structured_arr, header=header, affine=header.get_best_affine()
|
337 | 349 | )
|
| 350 | + |
| 351 | + nib_image.header.set_slope_inter( |
| 352 | + self._group_metadata["slope"], self._group_metadata["inter"] |
| 353 | + ) |
338 | 354 | nib.save(nib_image, self._output_path)
|
339 | 355 |
|
340 | 356 | def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
|
|
0 commit comments