def test_pad_constant(self): reference_shape = (4, 6) in_shape = (3, 5) values = np.ones(in_shape[::-1]) # since dimesions are switched in itk image = sitk.GetImageFromArray(values) corrector = m.SizeCorrectionFilter(pad_constant=1) corr_image = corrector.execute(image, params=m.SizeCorrectionParams(reference_shape)) self.assertEqual(corr_image.GetSize(), reference_shape) np_corr_image = sitk.GetArrayFromImage(corr_image) self.assertTrue((np_corr_image == 1).all()) # dimensions are switched from itk to numpy
def test_2d(self): reference_shape = (4, 6) in_shape = (5, 5) values = np.random.randint(0, 10, in_shape[::-1]) # since dimesions are switched in itk image = sitk.GetImageFromArray(values) corrector = m.SizeCorrectionFilter() corr_image = corrector.execute(image, params=m.SizeCorrectionParams(reference_shape)) self.assertEqual(corr_image.GetSize(), reference_shape) np_corr_image = sitk.GetArrayFromImage(corr_image) self.assertTrue((np_corr_image[:-1] == values[:, :-1]).all()) # dimensions are switched from itk to numpy
def test_one_sided(self): reference_shape = (4, 4, 3) in_shape = (2, 4, 5) values = np.random.randint(0, 10, in_shape[::-1]) # since dimesions are switched in itk image = sitk.GetImageFromArray(values) corrector = m.SizeCorrectionFilter(two_sided=False) corr_image = corrector.execute(image, params=m.SizeCorrectionParams(reference_shape)) self.assertEqual(corr_image.GetSize(), reference_shape) np_corr_image = sitk.GetArrayFromImage(corr_image) self.assertTrue((np_corr_image[:, :, 2:] == values[2:, :, :]).all())
def main(imginppath: str, samplerate: float, outputsubdir: str): samplerate_str = str("{:.2f}".format(samplerate)) print(samplerate_str) print('------') outfilename = os.path.split(imginppath)[1].split( '.nii.gz')[0] + '-subsampled_' + samplerate_str + '.nii.gz' outfilename = os.path.join( os.path.split(imginppath)[0], outputsubdir, outfilename) # load image img = sitk.ReadImage(imginppath) print(img.GetSize()) # pad image padfilter = flt.SizeCorrectionFilter(two_sided=False, pad_constant=0.0) padparams = flt.SizeCorrectionParams(img.GetSize()) sitkpadflt = sitk.FFTPadImageFilter() sitkpadflt.SetBoundaryCondition(1) img_padded = sitkpadflt.Execute(img) brainmask = sitk.ReadImage( os.path.join(os.path.split(imginppath)[0], 'brainmask.nii.gz')) # transform to k-space print('Unpadded: ' + str(img.GetSize())) print('Padded: ' + str(img_padded.GetSize())) img_k = sitk.ForwardFFT(img_padded) # get size of transformed image img_k_size = img_k.GetSize() # random sample numel = img_k_size[0] * img_k_size[1] * img_k_size[2] samplearr = np.zeros(numel, dtype=int) samplearr[:int(numel * samplerate)] = 1 np.random.shuffle(samplearr) maskarr = np.reshape(samplearr, img_k_size) maskimg = sitk.GetImageFromArray(np.swapaxes(maskarr, 0, 2)) maskimg.CopyInformation(img_k) masked_kspace = sitk.Mask(img_k, maskimg) img_subsampled = sitk.InverseFFT(masked_kspace) # unpad unpadparams = flt.SizeCorrectionParams(img.GetSize()) img_subsampled = padfilter.execute(img_subsampled, unpadparams) img_subsampled.CopyInformation(img) # mask with brainmask img_subsampled = sitk.Mask(sitk.Cast(img_subsampled, sitk.sitkInt64), sitk.Cast(brainmask, sitk.sitkInt64)) img_subsampled = sitk.Cast(img_subsampled, img.GetPixelID()) sitk.WriteImage(img_subsampled, outfilename)