def test_resizing_op_mixed_uni_nonuni(): """Check if resizing along uniform axes in mixed discretizations works.""" nonuni_part = odl.nonuniform_partition([0, 1, 4]) uni_part = odl.uniform_partition(-1, 1, 4) part = uni_part.append(nonuni_part, uni_part, nonuni_part) fspace = odl.FunctionSpace(odl.IntervalProd(part.min_pt, part.max_pt)) tspace = odl.rn(part.shape) space = odl.DiscreteLp(fspace, part, tspace) # Keep non-uniform axes fixed res_op = odl.ResizingOperator(space, ran_shp=(6, 3, 6, 3)) assert res_op.axes == (0, 2) assert res_op.offset == (1, 0, 1, 0) # Evaluation test with a simpler case part = uni_part.append(nonuni_part) fspace = odl.FunctionSpace(odl.IntervalProd(part.min_pt, part.max_pt)) tspace = odl.rn(part.shape) space = odl.DiscreteLp(fspace, part, tspace) res_op = odl.ResizingOperator(space, ran_shp=(6, 3)) result = res_op(space.one()) true_result = [[0, 0, 0], [1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1], [0, 0, 0]] assert np.array_equal(result, true_result) # Test adjoint elem = noise_element(space) res_elem = noise_element(res_op.range) inner1 = res_op(elem).inner(res_elem) inner2 = elem.inner(res_op.adjoint(res_elem)) assert almost_equal(inner1, inner2)
def test_init(exponent): # Validate that the different init patterns work and do not crash. space = odl.FunctionSpace(odl.IntervalProd(0, 1)) part = odl.uniform_partition_fromintv(space.domain, 10) rn = odl.rn(10, exponent=exponent) odl.DiscreteLp(space, part, rn, exponent=exponent) odl.DiscreteLp(space, part, rn, exponent=exponent, interp='linear') # Normal discretization of unit interval with complex complex_space = odl.FunctionSpace(odl.IntervalProd(0, 1), field=odl.ComplexNumbers()) cn = odl.cn(10, exponent=exponent) odl.DiscreteLp(complex_space, part, cn, exponent=exponent) space = odl.FunctionSpace(odl.IntervalProd([0, 0], [1, 1])) part = odl.uniform_partition_fromintv(space.domain, (10, 10)) rn = odl.rn(100, exponent=exponent) odl.DiscreteLp(space, part, rn, exponent=exponent, interp=['nearest', 'linear']) # Real space should not work with complex with pytest.raises(ValueError): odl.DiscreteLp(space, part, cn) # Complex space should not work with reals with pytest.raises(ValueError): odl.DiscreteLp(complex_space, part, rn) # Wrong size of underlying space rn_wrong_size = odl.rn(20) with pytest.raises(ValueError): odl.DiscreteLp(space, part, rn_wrong_size)
def test_norm_nonuniform(): """Check if norms are correct in non-uniform discretizations.""" fspace = odl.FunctionSpace(odl.IntervalProd(0, 5)) part = odl.nonuniform_partition([0, 2, 3, 5], min_pt=0, max_pt=5) weights = part.cell_sizes_vecs[0] tspace = odl.rn(part.size, weighting=weights) discr = odl.DiscreteLp(fspace, part, tspace) sqrt = discr.element(lambda x: np.sqrt(x)) # Exact norm is the square root of the integral from 0 to 5 of x, # which is sqrt(5**2 / 2) exact_norm = np.sqrt(5**2 / 2.0) norm = sqrt.norm() assert norm == pytest.approx(exact_norm)
def test_inner_nonuniform(): """Check if inner products are correct in non-uniform discretizations.""" fspace = odl.FunctionSpace(odl.IntervalProd(0, 5)) part = odl.nonuniform_partition([0, 2, 3, 5], min_pt=0, max_pt=5) weights = part.cell_sizes_vecs[0] tspace = odl.rn(part.size, weighting=weights) discr = odl.DiscreteLp(fspace, part, tspace) one = discr.one() linear = discr.element(lambda x: x) # Exact inner product is the integral from 0 to 5 of x, which is 5**2 / 2 exact_inner = 5**2 / 2.0 inner = one.inner(linear) assert inner == pytest.approx(exact_inner)
def ExpOp_builder(bin_param, filter_space, interp): # Create binning scheme if interp == 'Full': spf_space = filter_space Exp_op = odl.IdentityOperator(filter_space) elif interp == 'uniform': # Create binning scheme dpix = np.size(filter_space) dsize = filter_space.max_pt filt_bin_space = odl.uniform_discr(-dsize, dsize, dpix // (bin_param)) spf_space = odl.uniform_discr(0, dsize, dpix // (2 * bin_param)) resamp = odl.Resampling(filt_bin_space, filter_space) sym = SymOp(spf_space, filt_bin_space) Exp_op = resamp * sym else: if interp == 'constant': interp = 'nearest' elif interp == 'linear': pass else: raise ValueError('unknown `expansion operator type` ({})' ''.format(interp)) B = ExpBin(bin_param, np.size(filter_space)) * \ filter_space.weighting.const B[-1] -= 1 / 2 * filter_space.weighting.const # Create sparse filter space spf_part = odl.nonuniform_partition(B, min_pt=0, max_pt=B[-1]) spf_weight = np.ravel( np.multiply.reduce(np.meshgrid(*spf_part.cell_sizes_vecs))) spf_fspace = odl.FunctionSpace(spf_part.set) spf_space = odl.DiscreteLp(spf_fspace, spf_part, odl.rn(spf_part.size, weighting=spf_weight), interp=interp) filt_pos_part = odl.uniform_partition(0, B[-1], int(np.size(filter_space) / 2)) filt_pos_space = odl.uniform_discr_frompartition(filt_pos_part, dtype='float64') lin_interp = odl.Resampling(spf_space, filt_pos_space) # Create symmetry operator sym = SymOp(filt_pos_space, filter_space) # Create sparse filter operator Exp_op = sym * lin_interp return spf_space, Exp_op
def test_resizing_op_raise(): # domain not a uniformely discretized Lp with pytest.raises(TypeError): odl.ResizingOperator(odl.rn(5), ran_shp=(10, )) grid = odl.RectGrid([0, 2, 3]) part = odl.RectPartition(odl.IntervalProd(0, 3), grid) fspace = odl.FunctionSpace(odl.IntervalProd(0, 3)) dspace = odl.rn(3) space = odl.DiscreteLp(fspace, part, dspace) with pytest.raises(ValueError): odl.ResizingOperator(space, ran_shp=(10, )) # different cell sides in domain and range space = odl.uniform_discr(0, 1, 10) res_space = odl.uniform_discr(0, 1, 15) with pytest.raises(ValueError): odl.ResizingOperator(space, res_space) # non-integer multiple of cell sides used as shift (grid of the # resized space shifted) space = odl.uniform_discr(0, 1, 5) res_space = odl.uniform_discr(-0.5, 1.5, 10) with pytest.raises(ValueError): odl.ResizingOperator(space, res_space) # need either range or ran_shp with pytest.raises(ValueError): odl.ResizingOperator(space) # offset cannot be combined with range space = odl.uniform_discr([0, -1], [1, 1], (10, 5)) res_space = odl.uniform_discr([0, -3], [2, 3], (20, 15)) with pytest.raises(ValueError): odl.ResizingOperator(space, res_space, offset=(0, 0)) # bad pad_mode with pytest.raises(ValueError): odl.ResizingOperator(space, res_space, pad_mode='something')
def test_init_cuda(exponent): # Normal discretization of unit interval space = odl.FunctionSpace(odl.Interval(0, 1), out_dtype='float32') part = odl.uniform_partition_fromintv(space.domain, 10) rn = odl.CudaRn(10, exponent=exponent) odl.DiscreteLp(space, part, rn, exponent=exponent)