Skip to content

Commit

Permalink
fix: Do not convert mask nifti to RAS but to ornt
Browse files Browse the repository at this point in the history
  • Loading branch information
shernshiou committed Jul 17, 2024
1 parent 9de8334 commit 95c6964
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions darwin/importer/formats/nifti.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def _parse_nifti(
is_mpr: bool,
legacy: bool = False,
) -> dt.AnnotationFile:
img, pixdims = process_nifti(nib.load(nifti_path))
img, pixdims = process_nifti(nib.load(nifti_path), legacy=legacy)

processed_class_map = process_class_map(class_map)
video_annotations = []
Expand Down Expand Up @@ -367,7 +367,7 @@ def nifti_to_video_polygon_annotation(
def mask_to_polygon(
mask: np.ndarray, class_name: str, pixdims: List[float], legacy: bool = False
) -> Optional[dt.Annotation]:
def adjust_for_pixdims(x, y, pixdims, legacy):
def adjust_for_pixdims(x, y, pixdims):
if legacy:
if pixdims[1] > pixdims[0]:
return {"x": y, "y": x * pixdims[1] / pixdims[0]}
Expand All @@ -386,7 +386,7 @@ def adjust_for_pixdims(x, y, pixdims, legacy):
if len(external_path) // 2 <= 2:
continue
path = [
adjust_for_pixdims(x, y, pixdims, legacy=legacy)
adjust_for_pixdims(x, y, pixdims)
for x, y in zip(external_path[0::2], external_path[1::2])
]
paths.append(path)
Expand All @@ -406,7 +406,7 @@ def adjust_for_pixdims(x, y, pixdims, legacy):
polygon = dt.make_polygon(
class_name,
point_paths=[
adjust_for_pixdims(x, y, pixdims, legacy=legacy)
adjust_for_pixdims(x, y, pixdims)
for x, y in zip(external_path[0::2], external_path[1::2])
],
)
Expand Down Expand Up @@ -513,9 +513,10 @@ def correct_nifti_header_if_necessary(img_nii):
def process_nifti(
input_data: nib.nifti1.Nifti1Image,
ornt: Optional[List[List[float]]] = [[0.0, -1.0], [1.0, -1.0], [2.0, -1.0]],
legacy: bool = False,
) -> Tuple[np.ndarray, Tuple[float]]:
"""
Function that converts a nifti object to the passed ornt orientation.
Function that converts a nifti object to RAS orientation (if legacy), then converts to the passed ornt orientation.
The default ornt is for LPI.
Args:
Expand All @@ -529,6 +530,8 @@ def process_nifti(
pixdims: tuple of nifti header zoom values.
"""
img = correct_nifti_header_if_necessary(input_data)
if legacy:
img = nib.funcs.as_closest_canonical(img)
data_array = nib.orientations.apply_orientation(img.get_fdata(), ornt)
pixdims = img.header.get_zooms()
return data_array, pixdims
Expand Down

0 comments on commit 95c6964

Please sign in to comment.