def apply_transform( input_path, transform_name, output_path, kwargs, seed, ): """Apply transform to an image. \b Example: $ torchio-transform -k "degrees=(-5,15) num_transforms=3" input.nrrd RandomMotion output.nii """ # Imports are placed here so that the tool loads faster if not being run import torchio.transforms as transforms from torchio.transforms.augmentation import RandomTransform from torchio.utils import apply_transform_to_file try: transform_class = getattr(transforms, transform_name) except AttributeError as error: message = f'Transform "{transform_name}" not found in torchio' raise ValueError(message) from error params_dict = get_params_dict_from_kwargs(kwargs) if issubclass(transform_class, RandomTransform): params_dict['seed'] = seed transform = transform_class(**params_dict) apply_transform_to_file( input_path, transform, output_path, ) return 0
def apply_transform(input_path, transform_name, output_path, kwargs, seed, verbose): """Apply transform to an image. \b Example: $ torchio-transform input.nrrd RandomMotion output.nii --kwargs "proportion_to_augment=1 num_transforms=3" """ import torchio.transforms as transforms from torchio.transforms.augmentation import RandomTransform from torchio.utils import apply_transform_to_file, guess_type try: transform_class = getattr(transforms, transform_name) except AttributeError: raise AttributeError(f'"{transform_name}" class not found in torchio') params_dict = {} if kwargs is not None: for substring in kwargs.split(): key, value_string = substring.split('=') value = guess_type(value_string) params_dict[key] = value debug_kwargs = dict(verbose=verbose) if isinstance(transform_class, RandomTransform): debug_kwargs['seed'] = seed params_dict.update(debug_kwargs) transform = transform_class(**params_dict) apply_transform_to_file( input_path, transform, output_path, ) return 0
def test_apply_transform_to_file(self): transform = RandomFlip() apply_transform_to_file( self.get_image_path('input'), transform, self.get_image_path('output'), verbose=True, )
def apply_transform( input_path, transform_name, output_path, kwargs, seed, ): """Apply transform to an image. \b Example: $ torchio-transform -k "degrees=(-5,15) num_transforms=3" input.nrrd RandomMotion output.nii """ import torchio.transforms as transforms from torchio.transforms.augmentation import RandomTransform from torchio.utils import apply_transform_to_file, guess_type try: transform_class = getattr(transforms, transform_name) except AttributeError as error: message = f'Transform "{transform_name}" not found in torchio' raise ValueError(message) from error params_dict = {} if kwargs is not None: for substring in kwargs.split(): try: key, value_string = substring.split('=') except ValueError as error: message = f'Arguments string "{kwargs}" not valid' raise ValueError(message) from error value = guess_type(value_string) params_dict[key] = value if issubclass(transform_class, RandomTransform): params_dict['seed'] = seed transform = transform_class(**params_dict) apply_transform_to_file( input_path, transform, output_path, ) return 0
def main( input_path, transform_name, output_path, kwargs, imclass, seed, verbose, ): """Apply transform to an image. \b Example: $ torchio-transform -k "degrees=(-5,15) num_transforms=3" input.nrrd RandomMotion output.nii """ # noqa: E501 # Imports are placed here so that the tool loads faster if not being run import torch import torchio.transforms as transforms from torchio.utils import apply_transform_to_file try: transform_class = getattr(transforms, transform_name) except AttributeError as error: message = f'Transform "{transform_name}" not found in torchio' raise ValueError(message) from error params_dict = get_params_dict_from_kwargs(kwargs) transform = transform_class(**params_dict) if seed is not None: torch.manual_seed(seed) apply_transform_to_file( input_path, transform, output_path, verbose=verbose, class_=imclass, ) return 0