def test_uniform_discr_fromdiscr_two_attrs(): # Change 2 attributes -> resize and translate discr = odl.uniform_discr([0, -1], [1, 1], [10, 5]) # csides = [0.1, 0.4] new_min_pt = [-2, 1] new_max_pt = [4, 2] true_new_csides = [0.6, 0.2] new_discr = odl.uniform_discr_fromdiscr(discr, min_pt=new_min_pt, max_pt=new_max_pt) assert all_almost_equal(new_discr.min_pt, new_min_pt) assert all_almost_equal(new_discr.max_pt, new_max_pt) assert all_equal(new_discr.shape, discr.shape) assert all_almost_equal(new_discr.cell_sides, true_new_csides) new_min_pt = [-2, 1] new_shape = (5, 20) true_new_max_pt = [-1.5, 9] new_discr = odl.uniform_discr_fromdiscr(discr, min_pt=new_min_pt, shape=new_shape) assert all_almost_equal(new_discr.min_pt, new_min_pt) assert all_almost_equal(new_discr.max_pt, true_new_max_pt) assert all_equal(new_discr.shape, new_shape) assert all_almost_equal(new_discr.cell_sides, discr.cell_sides) new_min_pt = [-2, 1] new_csides = [0.6, 0.2] true_new_max_pt = [4, 2] new_discr = odl.uniform_discr_fromdiscr(discr, min_pt=new_min_pt, cell_sides=new_csides) assert all_almost_equal(new_discr.min_pt, new_min_pt) assert all_almost_equal(new_discr.max_pt, true_new_max_pt) assert all_equal(new_discr.shape, discr.shape) assert all_almost_equal(new_discr.cell_sides, new_csides) new_max_pt = [4, 2] new_shape = (5, 20) true_new_min_pt = [3.5, -6] new_discr = odl.uniform_discr_fromdiscr(discr, max_pt=new_max_pt, shape=new_shape) assert all_almost_equal(new_discr.min_pt, true_new_min_pt) assert all_almost_equal(new_discr.max_pt, new_max_pt) assert all_equal(new_discr.shape, new_shape) assert all_almost_equal(new_discr.cell_sides, discr.cell_sides) new_max_pt = [4, 2] new_csides = [0.6, 0.2] true_new_min_pt = [-2, 1] new_discr = odl.uniform_discr_fromdiscr(discr, max_pt=new_max_pt, cell_sides=new_csides) assert all_almost_equal(new_discr.min_pt, true_new_min_pt) assert all_almost_equal(new_discr.max_pt, new_max_pt) assert all_equal(new_discr.shape, discr.shape) assert all_almost_equal(new_discr.cell_sides, new_csides)
def test_uniform_discr_fromdiscr_per_axis(): discr = odl.uniform_discr([0, -1], [1, 1], [10, 5]) # csides = [0.1, 0.4] new_min_pt = [-2, None] new_max_pt = [4, 2] new_shape = (None, 20) new_csides = [None, None] true_new_min_pt = [-2, -6] true_new_max_pt = [4, 2] true_new_shape = (10, 20) true_new_csides = [0.6, 0.4] new_discr = odl.uniform_discr_fromdiscr(discr, min_pt=new_min_pt, max_pt=new_max_pt, shape=new_shape, cell_sides=new_csides) assert all_almost_equal(new_discr.min_pt, true_new_min_pt) assert all_almost_equal(new_discr.max_pt, true_new_max_pt) assert all_equal(new_discr.shape, true_new_shape) assert all_almost_equal(new_discr.cell_sides, true_new_csides) new_min_pt = None new_max_pt = [None, 2] new_shape = (5, None) new_csides = [None, 0.2] true_new_min_pt = [0, 1] true_new_max_pt = [1, 2] true_new_shape = (5, 5) true_new_csides = [0.2, 0.2] new_discr = odl.uniform_discr_fromdiscr(discr, min_pt=new_min_pt, max_pt=new_max_pt, shape=new_shape, cell_sides=new_csides) assert all_almost_equal(new_discr.min_pt, true_new_min_pt) assert all_almost_equal(new_discr.max_pt, true_new_max_pt) assert all_equal(new_discr.shape, true_new_shape) assert all_almost_equal(new_discr.cell_sides, true_new_csides)
def test_uniform_discr_fromdiscr_one_attr(): # Change 1 attribute discr = odl.uniform_discr([0, -1], [1, 1], [10, 5]) # csides = [0.1, 0.4] # min_pt -> translate, keep cells new_min_pt = [3, 7] true_new_max_pt = [4, 9] new_discr = odl.uniform_discr_fromdiscr(discr, min_pt=new_min_pt) assert all_almost_equal(new_discr.min_pt, new_min_pt) assert all_almost_equal(new_discr.max_pt, true_new_max_pt) assert all_equal(new_discr.shape, discr.shape) assert all_almost_equal(new_discr.cell_sides, discr.cell_sides) # max_pt -> translate, keep cells new_max_pt = [3, 7] true_new_min_pt = [2, 5] new_discr = odl.uniform_discr_fromdiscr(discr, max_pt=new_max_pt) assert all_almost_equal(new_discr.min_pt, true_new_min_pt) assert all_almost_equal(new_discr.max_pt, new_max_pt) assert all_equal(new_discr.shape, discr.shape) assert all_almost_equal(new_discr.cell_sides, discr.cell_sides) # shape -> resize cells, keep corners new_shape = (5, 20) true_new_csides = [0.2, 0.1] new_discr = odl.uniform_discr_fromdiscr(discr, shape=new_shape) assert all_almost_equal(new_discr.min_pt, discr.min_pt) assert all_almost_equal(new_discr.max_pt, discr.max_pt) assert all_equal(new_discr.shape, new_shape) assert all_almost_equal(new_discr.cell_sides, true_new_csides) # cell_sides -> resize cells, keep corners new_csides = [0.5, 0.2] true_new_shape = (2, 10) new_discr = odl.uniform_discr_fromdiscr(discr, cell_sides=new_csides) assert all_almost_equal(new_discr.min_pt, discr.min_pt) assert all_almost_equal(new_discr.max_pt, discr.max_pt) assert all_equal(new_discr.shape, true_new_shape) assert all_almost_equal(new_discr.cell_sides, new_csides)
def test_uniform_discr_fromdiscr_one_attr(): # Change 1 attribute discr = odl.uniform_discr([0, -1], [1, 1], [10, 5]) # csides = [0.1, 0.4] # min_corner -> translate, keep cells new_min_corner = [3, 7] true_new_end = [4, 9] new_discr = odl.uniform_discr_fromdiscr(discr, min_corner=new_min_corner) assert all_almost_equal(new_discr.min_corner, new_min_corner) assert all_almost_equal(new_discr.max_corner, true_new_end) assert all_equal(new_discr.shape, discr.shape) assert all_almost_equal(new_discr.cell_sides, discr.cell_sides) # max_corner -> translate, keep cells new_max_corner = [3, 7] true_new_begin = [2, 5] new_discr = odl.uniform_discr_fromdiscr(discr, max_corner=new_max_corner) assert all_almost_equal(new_discr.min_corner, true_new_begin) assert all_almost_equal(new_discr.max_corner, new_max_corner) assert all_equal(new_discr.shape, discr.shape) assert all_almost_equal(new_discr.cell_sides, discr.cell_sides) # nsamples -> resize cells, keep corners new_nsamples = (5, 20) true_new_csides = [0.2, 0.1] new_discr = odl.uniform_discr_fromdiscr(discr, nsamples=new_nsamples) assert all_almost_equal(new_discr.min_corner, discr.min_corner) assert all_almost_equal(new_discr.max_corner, discr.max_corner) assert all_equal(new_discr.shape, new_nsamples) assert all_almost_equal(new_discr.cell_sides, true_new_csides) # cell_sides -> resize cells, keep corners new_csides = [0.5, 0.2] true_new_nsamples = (2, 10) new_discr = odl.uniform_discr_fromdiscr(discr, cell_sides=new_csides) assert all_almost_equal(new_discr.min_corner, discr.min_corner) assert all_almost_equal(new_discr.max_corner, discr.max_corner) assert all_equal(new_discr.shape, true_new_nsamples) assert all_almost_equal(new_discr.cell_sides, new_csides)
def test_uniform_discr_fromdiscr_per_axis(): discr = odl.uniform_discr([0, -1], [1, 1], [10, 5]) # csides = [0.1, 0.4] new_min_pt = [-2, None] new_max_pt = [4, 2] new_shape = (None, 20) new_csides = [None, None] true_new_min_pt = [-2, -6] true_new_max_pt = [4, 2] true_new_shape = (10, 20) true_new_csides = [0.6, 0.4] new_discr = odl.uniform_discr_fromdiscr( discr, min_pt=new_min_pt, max_pt=new_max_pt, shape=new_shape, cell_sides=new_csides) assert all_almost_equal(new_discr.min_pt, true_new_min_pt) assert all_almost_equal(new_discr.max_pt, true_new_max_pt) assert all_equal(new_discr.shape, true_new_shape) assert all_almost_equal(new_discr.cell_sides, true_new_csides) new_min_pt = None new_max_pt = [None, 2] new_shape = (5, None) new_csides = [None, 0.2] true_new_min_pt = [0, 1] true_new_max_pt = [1, 2] true_new_shape = (5, 5) true_new_csides = [0.2, 0.2] new_discr = odl.uniform_discr_fromdiscr( discr, min_pt=new_min_pt, max_pt=new_max_pt, shape=new_shape, cell_sides=new_csides) assert all_almost_equal(new_discr.min_pt, true_new_min_pt) assert all_almost_equal(new_discr.max_pt, true_new_max_pt) assert all_equal(new_discr.shape, true_new_shape) assert all_almost_equal(new_discr.cell_sides, true_new_csides)
insert_min_pt1, insert_max_pt1) coarse_mask2 = multigrid.operators.MaskingOperator(coarse_discr, insert_min_pt2, insert_max_pt2) # Define the forward operator of the masked coarse grid coarse_ray_trafo = odl.tomo.RayTransform(coarse_discr, geometry, impl='astra_cuda') masked_coarse_ray_trafo = coarse_ray_trafo * coarse_mask1 * coarse_mask2 # Define insert discretization (ROI discretization) using the fine cell sizes # but the insert min and max points insert_discr1 = odl.uniform_discr_fromdiscr(fine_discr, min_pt=insert_min_pt1, max_pt=insert_max_pt1, cell_sides=fine_discr.cell_sides) insert_discr2 = odl.uniform_discr_fromdiscr(fine_discr, min_pt=insert_min_pt2, max_pt=insert_max_pt2, cell_sides=fine_discr.cell_sides) # Ray trafo on the insert discretization only insert_ray_trafo1 = odl.tomo.RayTransform(insert_discr1, geometry, impl='astra_cuda') insert_ray_trafo2 = odl.tomo.RayTransform(insert_discr2, geometry, impl='astra_cuda')
impl='astra_cuda') masked_coarse_ray_trafo = coarse_ray_trafo * coarse_mask ray_trafo_fine = odl.tomo.RayTransform(fine_discr, geometry, impl='astra_cuda') # Phantom phantom_c = odl.phantom.shepp_logan(coarse_discr, modified=True) phantom_f = odl.phantom.shepp_logan(fine_discr, modified=True) # Define insert discretization using the fine cell sizes but the insert # min and max points insert_discr = odl.uniform_discr_fromdiscr( fine_discr, min_pt=insert_min_pt, max_pt=insert_max_pt, cell_sides=fine_discr.cell_sides) # Restrict the phantom to the insert discr resizing_operator = odl.ResizingOperator(fine_discr, insert_discr) phantom_insert = resizing_operator(phantom_f) # Ray trafo on the insert discretization only insert_ray_trafo = odl.tomo.RayTransform(insert_discr, geometry, impl='astra_cuda') # Forward operator = sum of masked coarse ray trafo and insert ray trafo sum_ray_trafo = odl.ReductionOperator(masked_coarse_ray_trafo, insert_ray_trafo)
R22(X2.one()).show('ROI footprint') # %% Make phantoms # Merged low-resolution phantom f1 = odl.phantom.shepp_logan(X1, modified=True) f1 += odl.phantom.shepp_logan(X1, modified=True, min_pt=X2.min_pt, max_pt=X2.max_pt) f1.show('f1 -- low-res full phantom') f1.show('low-res ROI part in f1', coords=[X2.min_pt, X2.max_pt]) # High-resolution full phantom (for data generation) X1_hires = odl.uniform_discr_fromdiscr(X1, cell_sides=X2.cell_sides) f1_hires = odl.phantom.shepp_logan(X1_hires, modified=True) f1_hires += odl.phantom.shepp_logan(X1_hires, modified=True, min_pt=X2.min_pt, max_pt=X2.max_pt) f1_hires.show('f1_hires -- hi-res full phantom') # ROI high-resolution phantom f2 = odl.phantom.shepp_logan(X2, modified=True) f2.show('f2 -- hi-res ROI part') # %% Generate projection data # Full data from low-resolution phantom
# Initialize data as ODL space element and display it, clipping to a # somewhat reasonable range data = ray_trafo_fbp.range.element(log_data_arr) data.show('Sinogram', clim=[0, 4.5]) # Compute FBP reco for a good initial guess and for reference fbp = odl.tomo.fbp_op(ray_trafo_fbp, padding=True, filter_type='Hamming', frequency_scaling=0.8) reco_fbp = fbp(data) reco_fbp.show('FBP reconstruction', clim=[0.019, 0.023]) # Low-resolution space covering the same domain space_lowres = odl.uniform_discr_fromdiscr(space_fbp, shape=(64, 64)) # Define the high-resolution space for the detail region. # Find partition indices closest to the desired min and max points, such # that there is no overlap. detail_min_pt = [-39, -55] detail_max_pt = [41, 25] detail_min_pt_idx = np.floor( (detail_min_pt - space_lowres.min_pt) / space_lowres.cell_sides) detail_min_pt_idx = detail_min_pt_idx.astype(int) detail_max_pt_idx = np.ceil( (detail_max_pt - space_lowres.min_pt) / space_lowres.cell_sides) detail_max_pt_idx = detail_max_pt_idx.astype(int) detail_shape_coarse = detail_max_pt_idx - detail_min_pt_idx detail_min_pt = (space_lowres.min_pt + detail_min_pt_idx * space_lowres.cell_sides)