def get_ImageData(num_model, geometry): '''Returns an ImageData relative to geometry with the model num_model from tomophantom :param num_model: model number :type num_model: int :param geometry: geometrical info that describes the phantom :type geometry: ImageGeometry Example usage: .. code-block:: python ndim = 2 N=128 angles = np.linspace(0, 360, 50, True, dtype=np.float32) offset = 0.4 channels = 3 if ndim == 2: ag = AcquisitionGeometry.create_Cone2D((offset,-100), (offset,100)) ag.set_panel(N) else: ag = AcquisitionGeometry.create_Cone3D((offset,-100, 0), (offset,100,0)) ag.set_panel((N,N-2)) ag.set_channels(channels) ag.set_angles(angles, angle_unit=AcquisitionGeometry.DEGREE) ig = ag.get_ImageGeometry() num_model = 1 phantom = TomoPhantom.get_ImageData(num_model=num_model, geometry=ig) ''' ig = geometry.copy() ig.set_labels(DataOrder.TOMOPHANTOM_IG_LABELS) num_dims = len(ig.dimension_labels) if ImageGeometry.CHANNEL in ig.dimension_labels: if not is_model_temporal(num_model): raise ValueError('Selected model {} is not a temporal model, please change your selection'.format(num_model)) if num_dims == 4: # 3D+time for tomophantom # output dimensions channel and then spatial, # e.g. [ 'channel', 'vertical', 'horizontal_y', 'horizontal_x' ] num_model = num_model shape = tuple(ig.shape[1:]) phantom_arr = TomoP3D.ModelTemporal(num_model, shape, path_library3D) elif num_dims == 3: # 2D+time for tomophantom # output dimensions channel and then spatial, # e.g. [ 'channel', 'horizontal_y', 'horizontal_x' ] N = ig.shape[1] num_model = num_model phantom_arr = TomoP2D.ModelTemporal(num_model, ig.shape[1], path_library2D) else: raise ValueError('Wrong ImageGeometry') if ig.channels != phantom_arr.shape[0]: raise ValueError('The required model {} has {} channels. The ImageGeometry you passed has {}. Please update your ImageGeometry.'\ .format(num_model, ig.channels, phantom_arr.shape[0])) else: if num_dims == 3: # 3D num_model = num_model phantom_arr = TomoP3D.Model(num_model, ig.shape, path_library3D) elif num_dims == 2: # 2D if ig.shape[0] != ig.shape[1]: raise ValueError('Can only handle square ImageData, got shape'.format(ig.shape)) N = ig.shape[0] num_model = num_model phantom_arr = TomoP2D.Model(num_model, N, path_library2D) else: raise ValueError('Wrong ImageGeometry') im_data = ImageData(phantom_arr, geometry=ig, suppress_warning=True) im_data.reorder(list(geometry.dimension_labels)) return im_data
print("MAE" , mae(res1, res2)) np.testing.assert_array_almost_equal(res1.as_array(), res2.as_array(), decimal=3) ################################################################### ################################################################### ################################################################### ################################################################### print("Compare CIL_FGP_TV vs CCPiReg_FGP_TV no tolerance (3D)") print ("Building 3D phantom using TomoPhantom software") model = 13 # select a model number from the library N_size = 64 # Define phantom dimensions using a scalar value (cubic phantom) path = os.path.dirname(tomophantom.__file__) path_library3D = os.path.join(path, "Phantom3DLibrary.dat") #This will generate a N_size x N_size x N_size phantom (3D) phantom_tm = TomoP3D.Model(model, N_size, path_library3D) ig = ImageGeometry(N_size, N_size, N_size) data = ig.allocate() data.fill(phantom_tm) n1 = TestData.random_noise(data.as_array(), mode = 'gaussian', seed = 10) noisy_data = ig.allocate() noisy_data.fill(n1) # Show Ground Truth and Noisy Data plt.figure(figsize=(10,5)) plt.subplot(1,2,1) plt.imshow(data.as_array()[32]) plt.title('Ground Truth')
def test_compare_regularisation_toolkit_tomophantom(self): print("Compare CIL_FGP_TV vs CCPiReg_FGP_TV no tolerance (3D)") print("Building 3D phantom using TomoPhantom software") model = 13 # select a model number from the library N_size = 64 # Define phantom dimensions using a scalar value (cubic phantom) path = os.path.dirname(tomophantom.__file__) path_library3D = os.path.join(path, "Phantom3DLibrary.dat") #This will generate a N_size x N_size x N_size phantom (3D) phantom_tm = TomoP3D.Model(model, N_size, path_library3D) ig = ImageGeometry(N_size, N_size, N_size) data = ig.allocate() data.fill(phantom_tm) noisy_data = noise.gaussian(data, seed=10) alpha = 0.1 iters = 1000 print("Use tau as an array of ones") # CIL_TotalVariation no tolerance g_CIL = alpha * TotalVariation(iters, tolerance=None, info=True) res1 = g_CIL.proximal(noisy_data, ig.allocate(1.)) t0 = timer() res1 = g_CIL.proximal(noisy_data, ig.allocate(1.)) t1 = timer() print(t1 - t0) # CCPi Regularisation toolkit high tolerance r_alpha = alpha r_iterations = iters r_tolerance = 1e-9 r_iso = 0 r_nonneg = 0 r_printing = 0 g_CCPI_reg_toolkit = CCPiReg_FGP_TV(r_alpha, r_iterations, r_tolerance, r_iso, r_nonneg, r_printing, 'cpu') t2 = timer() res2 = g_CCPI_reg_toolkit.proximal(noisy_data, 1.) t3 = timer() print(t3 - t2) np.testing.assert_array_almost_equal(res1.as_array(), res2.as_array(), decimal=3) # CIL_FGP_TV no tolerance #g_CIL = FGP_TV(ig, alpha, iters, tolerance=None, info=True) g_CIL.tolerance = None t0 = timer() res1 = g_CIL.proximal(noisy_data, 1.) t1 = timer() print(t1 - t0) ################################################################### ################################################################### ################################################################### ################################################################### data = dataexample.PEPPERS.get(size=(256, 256)) ig = data.geometry ag = ig noisy_data = noise.gaussian(data, seed=10) alpha = 0.1 iters = 1000 # CIL_FGP_TV no tolerance g_CIL = alpha * TotalVariation(iters, tolerance=None) t0 = timer() res1 = g_CIL.proximal(noisy_data, 1.) t1 = timer() print(t1 - t0) # CCPi Regularisation toolkit high tolerance r_alpha = alpha r_iterations = iters r_tolerance = 1e-8 r_iso = 0 r_nonneg = 0 r_printing = 0 g_CCPI_reg_toolkit = CCPiReg_FGP_TV(r_alpha, r_iterations, r_tolerance, r_iso, r_nonneg, r_printing, 'cpu') t2 = timer() res2 = g_CCPI_reg_toolkit.proximal(noisy_data, 1.) t3 = timer() print(t3 - t2)