def test_transforms(self): landmarks_dict = dict( t1=np.linspace(0, 100, 13), t2=np.linspace(0, 100, 13), ) transforms = ( CenterCropOrPad((9, 21, 30)), ToCanonical(), Resample((1, 1.1, 1.25)), RandomFlip(axes=(0, 1, 2), flip_probability=1), RandomMotion(proportion_to_augment=1), RandomGhosting(proportion_to_augment=1, axes=(0, 1, 2)), RandomSpike(), RandomNoise(), RandomBlur(), RandomSwap(patch_size=2, num_iterations=5), Lambda(lambda x: 1.5 * x, types_to_apply=INTENSITY), RandomBiasField(), Rescale((0, 1)), ZNormalization(masking_method='label'), HistogramStandardization(landmarks_dict=landmarks_dict), RandomElasticDeformation(proportion_to_augment=1), RandomAffine(), Pad((1, 2, 3, 0, 5, 6)), Crop((3, 2, 8, 0, 1, 4)), ) transformed = self.get_sample() for transform in transforms: transformed = transform(transformed)
def mri_artifact(p=1): return OneOf( { RandomMotion(): 0.34, RandomGhosting(): 0.33, RandomSpike(): 0.33 }, p=p)
def test_reproducibility_compose(self): trsfm = Compose([RandomNoise(p=0.0), RandomSpike(num_spikes=3, p=1.0)]) subject1, subject2 = self.get_subjects() transformed1 = trsfm(subject1) history1 = transformed1.history trsfm_hist, seeds_hist = compose_from_history(history=history1) transformed2 = self.apply_transforms(subject2, trsfm_list=trsfm_hist, seeds_list=seeds_hist) data1, data2 = transformed1.img.data, transformed2.img.data self.assertTensorEqual(data1, data2)
def compose_transforms() -> Compose: print(f"{ctime()}: Setting up transformations...") """ # Our Preprocessing Options available in TorchIO are: * Intensity - NormalizationTransform - RescaleIntensity - ZNormalization - HistogramStandardization * Spatial - CropOrPad - Crop - Pad - Resample - ToCanonical We should read and experiment with these, but for now will just use a bunch with the default values. """ preprocessors = [ ToCanonical(p=1), ZNormalization(masking_method=None, p=1), # alternately, use RescaleIntensity ] """ # Our Augmentation Options available in TorchIO are: * Spatial - RandomFlip - RandomAffine - RandomElasticDeformation * Intensity - RandomMotion - RandomGhosting - RandomSpike - RandomBiasField - RandomBlur - RandomNoise - RandomSwap We should read and experiment with these, but for now will just use a bunch with the default values. """ augments = [ RandomFlip(axes=(0, 1, 2), flip_probability=0.5), RandomAffine(image_interpolation="linear", p=0.8), # default, compromise on speed + quality # this will be most processing intensive, leave out for now, see results # RandomElasticDeformation(p=1), RandomMotion(), RandomSpike(), RandomBiasField(), RandomBlur(), RandomNoise(), ] transform = Compose(preprocessors + augments) print(f"{ctime()}: Transformations registered.") return transform
def mri_artifact(parameters): return OneOf( {RandomGhosting(): 0.5, RandomSpike(): 0.5}, p=parameters["probability"], )
} t = RandomMotionFromTimeCourse(**dico_params) t._calc_dimensions((100,20,50)) fitP = t._simulate_random_trajectory() fitP = t.fitpars if True:# y_Disp>0: plt.figure() plt.plot(fit_pars.T) plt.plot(fitP.T,'--') #test transforms from torchio.transforms import RandomSpike t = RandomSpike(num_spikes_range=(5,10), intensity_range=(0.1,0.2)) dataset = ImagesDataset(suj, transform=t) for i in range(1,10): sample = dataset[0] fout='/tmp/toto{}_nb{}_I{}.nii'.format(i,sample['T1']['random_spike_num_spikes'],np.floor(sample['T1']['random_spike_intensity']*100)) dataset.save_sample(sample, dict(T1=fout)) out_dir = '/data/ghiles/motion_simulation/tests/' def corrupt_data(data, percentage): n_pts_to_corrupt = int(round(percentage * len(data)))