Skip to content

Commit

Permalink
Fix tox errors
Browse files Browse the repository at this point in the history
  • Loading branch information
fepegar committed Dec 30, 2019
1 parent dd6fc58 commit f652384
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
17 changes: 13 additions & 4 deletions torchio/dataset/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,18 @@ def __init__(
"""
Each element of subjects_list is a dictionary:
subject = {
'one_image': dict(path=path_to_one_image, type=torchio.INTENSITY),
'another_image': dict(path=path_to_another_image, type=torchio.INTENSITY),
'a_label': dict(path=path_to_a_label, type=torchio.LABEL),
'one_image': dict(
path=path_to_one_image,
type=torchio.INTENSITY,
),
'another_image': dict(
path=path_to_another_image,
type=torchio.INTENSITY,
),
'a_label': dict(
path=path_to_a_label,
type=torchio.LABEL,
),
}
See examples/example_multimodal.py for -obviously- an example.
"""
Expand Down Expand Up @@ -90,7 +99,7 @@ def parse_path(path):
subject_dict = element
for image_dict in subject_dict.values():
for key in ('path', 'type'):
if not key in image_dict:
if key not in image_dict:
raise ValueError(
f'"{key}" not found in image dict {image_dict}')
parse_path(image_dict['path'])
Expand Down
2 changes: 1 addition & 1 deletion torchio/transforms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from .random_noise import RandomNoise
from .random_affine import RandomAffine
from .random_motion import RandomMotion
from .random_elastic_deformation import RandomElasticDeformation
from .interpolation import Interpolation
from .random_elastic_deform import RandomElasticDeformation

# Intensity transforms
from .rescale import Rescale
Expand Down
29 changes: 17 additions & 12 deletions torchio/transforms/random_motion.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,14 @@ def apply_transform(self, sample):
self.translation_range,
self.num_transforms,
)
sample[image_name]['random_motion_times'] = times_params
sample[image_name]['random_motion_degrees'] = degrees_params
sample[image_name]['random_motion_translation'] = translation_params
keys = (
'random_motion_times',
'random_motion_degrees',
'random_motion_translation',
)
all_params = times_params, degrees_params, translation_params
for key, params in zip(keys, all_params):
sample[image_name][key] = params
if not is_image_dict(image_dict):
continue
if image_dict['type'] == LABEL:
Expand Down Expand Up @@ -202,21 +207,21 @@ def matrix_sqrt(A, epsilon=1e-9):
while diff > epsilon:
iX = np.linalg.inv(X)
iY = np.linalg.inv(Y)
X = 1/2 * (X + iY)
Y = 1/2 * (Y + iX)
X = 1 / 2 * (X + iY)
Y = 1 / 2 * (Y + iX)
diff = np.linalg.norm(X @ X - A)
return X

@staticmethod
def matrix_exp(A, q=6):
I = np.eye(4, dtype=float)
identity = np.eye(4, dtype=float)
norm_a = np.linalg.norm(A)
log = np.log2(norm_a)
j = int(max(0, 1 + np.floor(log))) if norm_a > 0 else 0
A = 2 ** (-j) * A
D = I.copy()
N = I.copy()
X = I.copy()
D = identity.copy()
N = identity.copy()
X = identity.copy()
c = 1
for k in range(1, q + 1):
c = c * (q - k + 1) / (k * (2 * q - k + 1))
Expand All @@ -228,14 +233,14 @@ def matrix_exp(A, q=6):
return X

def matrix_log(self, A, epsilon=1e-9):
I = np.eye(4)
identity = np.eye(4)
k = 0
diff = np.inf
while diff > 0.5:
A = self.matrix_sqrt(A)
k += 1
diff = np.linalg.norm(A - I)
A = I - A
diff = np.linalg.norm(A - identity)
A = identity - A
Z = A.copy()
X = A.copy()
i = 1
Expand Down

0 comments on commit f652384

Please sign in to comment.