Skip to content

Commit

Permalink
Merge branch 'master' of github.com:fepegar/torchio
Browse files Browse the repository at this point in the history
  • Loading branch information
fepegar committed Jun 1, 2020
2 parents af3bc06 + acc191d commit fca4b74
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/source/conf.py
Expand Up @@ -26,7 +26,7 @@

# version is the short X.Y version
# release is the full version, including alpha/beta/rc tags
version = release = '0.16.15'
version = release = '0.16.17'


# -- General configuration ---------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion setup.cfg
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.16.15
current_version = 0.16.17
commit = True
tag = True

Expand All @@ -22,3 +22,4 @@ universal = 1
exclude = docs

[aliases]

2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -57,6 +57,6 @@
test_suite='tests',
tests_require=[],
url='https://github.com/fepegar/torchio',
version='0.16.15',
version='0.16.17',
zip_safe=False,
)
2 changes: 1 addition & 1 deletion torchio/__init__.py
Expand Up @@ -2,7 +2,7 @@

__author__ = """Fernando Perez-Garcia"""
__email__ = 'fernando.perezgarcia.17@ucl.ac.uk'
__version__ = '0.16.15'
__version__ = '0.16.17'

from . import utils
from .torchio import *
Expand Down
12 changes: 12 additions & 0 deletions torchio/data/image.py
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
24 changes: 24 additions & 0 deletions torchio/data/orientation.py
@@ -0,0 +1,24 @@
import warnings
import nibabel as nib


AXCODES_TO_WORDS = {
'L': 'left',
'R': 'right',
'P': 'posterior',
'A': 'anterior',
'I': 'inferior',
'S': 'superior',
# 'C': 'caudal',
# 'R': 'rostral', # conflic with right
# 'D': 'dorsal',
# 'V': 'ventral',
}


def name_dimensions(tensor, affine):
axcodes = nib.aff2axcodes(affine)
names = [AXCODES_TO_WORDS[axcode] for axcode in axcodes]
with warnings.catch_warnings():
warnings.simplefilter('ignore', UserWarning)
tensor.rename_(*names)

0 comments on commit fca4b74

Please sign in to comment.