Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error with invertible custom transforms, also keep include/exclude lists for inverse transforms. #398

Merged
merged 4 commits into from
Dec 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions torchio/data/subject.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from ..constants import TYPE, INTENSITY
from .image import Image
from ..utils import get_subclasses


class Subject(dict):
Expand Down Expand Up @@ -110,10 +111,12 @@ def spacing(self):

@property
def history(self):
from .. import transforms
from ..transforms.transform import Transform
transform_classes = {cls.__name__: cls for cls in get_subclasses(Transform)}

transforms_list = []
for transform_name, arguments in self.applied_transforms:
transform = getattr(transforms, transform_name)(**arguments)
transform = transform_classes[transform_name](**arguments)
transforms_list.append(transform)
return transforms_list

Expand Down
4 changes: 3 additions & 1 deletion torchio/transforms/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,9 @@ def _get_reproducing_arguments(self):
Return a dictionary with the arguments that would be necessary to
reproduce the transform exactly.
"""
return {name: getattr(self, name) for name in self.args_names}
reproducing_arguments = dict(include=self.include, exclude=self.exclude, copy=self.copy)
reproducing_arguments.update({name: getattr(self, name) for name in self.args_names})
return reproducing_arguments

def is_invertible(self):
return hasattr(self, 'invert_transform')
Expand Down
6 changes: 6 additions & 0 deletions torchio/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,9 @@ def history_collate(batch: Sequence, collate_transforms=True):
if hasattr(first_element, attr):
dictionary.update({attr: [getattr(d, attr) for d in batch]})
return dictionary


def get_subclasses(target_class: type) -> List[type]:
subclasses = target_class.__subclasses__()
subclasses += sum([get_subclasses(cls) for cls in subclasses], [])
return subclasses