Skip to content

Commit

Permalink
Add orientation property to Image class
Browse files Browse the repository at this point in the history
  • Loading branch information
fepegar committed May 30, 2020
1 parent b3d4aa5 commit f28e4c9
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions torchio/data/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import torch
import numpy as np
import nibabel as nib
import SimpleITK as sitk

from ..utils import nib_to_sitk
Expand All @@ -19,6 +20,7 @@
INTENSITY,
)
from .io import read_image
from .orientation import name_dimensions


class Image(dict):
Expand Down Expand Up @@ -77,6 +79,10 @@ def shape(self) -> Tuple[int, int, int, int]:
def spatial_shape(self) -> TypeTripletInt:
return self.shape[1:]

@property
def orientation(self):
return nib.aff2axcodes(self.affine)

@staticmethod
def _parse_path(path: TypePath) -> Path:
if path is None:
Expand Down Expand Up @@ -134,7 +140,13 @@ def load(self, check_nans: bool = True) -> Tuple[torch.Tensor, np.ndarray]:
tensor, affine = read_image(self.path)
# https://github.com/pytorch/pytorch/issues/9410#issuecomment-404968513
tensor = tensor[(None,) * (3 - tensor.ndim)] # force to be 3D
# Remove next line and uncomment the two following ones once/if this issue
# gets fixed:
# https://github.com/pytorch/pytorch/issues/29010
# See also https://discuss.pytorch.org/t/collating-named-tensors/78650/4
tensor = tensor.unsqueeze(0) # add channels dimension
# name_dimensions(tensor, affine)
# tensor = tensor.align_to('channels', ...)
if check_nans and torch.isnan(tensor).any():
warnings.warn(f'NaNs found in file "{self.path}"')
return tensor, affine
Expand Down

1 comment on commit f28e4c9

@fepegar
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related to #176.

Please sign in to comment.