def test_nearest_interpolation_2d_string(): rect = odl.IntervalProd([0, 0], [1, 1]) part = odl.uniform_partition_fromintv(rect, [4, 2], nodes_on_bdry=False) # Coordinate vectors are: # [0.125, 0.375, 0.625, 0.875], [0.25, 0.75] space = odl.FunctionSet(rect, odl.Strings(1)) dspace = odl.ntuples(part.size, dtype='U1') interp_op = NearestInterpolation(space, part, dspace) values = np.array([c for c in 'mystring']) function = interp_op(values) # Evaluate at single point val = function([0.3, 0.6]) # closest to index (1, 1) -> 3 assert val == 't' # Input array, with and without output array pts = np.array([[0.3, 0.6], [1.0, 1.0]]) true_arr = ['t', 'g'] assert all_equal(function(pts.T), true_arr) out = np.empty(2, dtype='U1') function(pts.T, out=out) assert all_equal(out, true_arr) # Input meshgrid, with and without output array mg = sparse_meshgrid([0.3, 1.0], [0.4, 1.0]) # Indices: (1, 3) x (0, 1) true_mg = [['s', 't'], ['n', 'g']] assert all_equal(function(mg), true_mg) out = np.empty((2, 2), dtype='U1') function(mg, out=out) assert all_equal(out, true_mg)
def test_dspace_type_cuda(): # Plain function set -> Ntuples-like fset = odl.FunctionSet(odl.Interval(0, 1), odl.Strings(2)) assert dspace_type(fset, 'cuda') == odl.CudaNtuples assert dspace_type(fset, 'cuda', np.int) == odl.CudaNtuples # Real space rspc = odl.FunctionSpace(odl.Interval(0, 1), field=odl.RealNumbers()) assert dspace_type(rspc, 'cuda') == odl.CudaFn assert dspace_type(rspc, 'cuda', np.float64) == odl.CudaFn assert dspace_type(rspc, 'cuda', np.int) == odl.CudaFn with pytest.raises(TypeError): dspace_type(rspc, 'cuda', np.complex) with pytest.raises(TypeError): dspace_type(rspc, 'cuda', np.dtype('<U2')) # Complex space (not implemented) cspc = odl.FunctionSpace(odl.Interval(0, 1), field=odl.ComplexNumbers()) with pytest.raises(NotImplementedError): dspace_type(cspc, 'cuda') with pytest.raises(NotImplementedError): dspace_type(cspc, 'cuda', np.complex64) with pytest.raises(TypeError): dspace_type(cspc, 'cuda', np.float) with pytest.raises(TypeError): assert dspace_type(cspc, 'cuda', np.int)
def test_dspace_type_numpy(): # Plain function set -> Ntuples-like fset = odl.FunctionSet(odl.Interval(0, 1), odl.Strings(2)) assert dspace_type(fset, 'numpy') == odl.Ntuples, None assert dspace_type(fset, 'numpy', np.int) == odl.Ntuples # Real space rspc = odl.FunctionSpace(odl.Interval(0, 1), field=odl.RealNumbers()) assert dspace_type(rspc, 'numpy') == odl.Fn assert dspace_type(rspc, 'numpy', np.float32) == odl.Fn assert dspace_type(rspc, 'numpy', np.int) == odl.Fn with pytest.raises(TypeError): dspace_type(rspc, 'numpy', np.complex) with pytest.raises(TypeError): dspace_type(rspc, 'numpy', np.dtype('<U2')) # Complex space cspc = odl.FunctionSpace(odl.Interval(0, 1), field=odl.ComplexNumbers()) assert dspace_type(cspc, 'numpy') == odl.Fn assert dspace_type(cspc, 'numpy', np.complex64) == odl.Fn with pytest.raises(TypeError): dspace_type(cspc, 'numpy', np.float) with pytest.raises(TypeError): assert dspace_type(cspc, 'numpy', np.int) with pytest.raises(TypeError): dspace_type(cspc, 'numpy', np.dtype('<U2'))
def test_fspace_init(): """Check if all initialization patterns work.""" intv = odl.IntervalProd(0, 1) FunctionSpace(intv) FunctionSpace(intv, out_dtype=float) FunctionSpace(intv, out_dtype=complex) FunctionSpace(intv, out_dtype=(float, (2, 3))) str3 = odl.Strings(3) FunctionSpace(str3, out_dtype=int) # Make sure repr shows something assert repr(FunctionSpace(intv, out_dtype=(float, (2, 3))))
def test_fset_vector_eval(): str3 = odl.Strings(3) ints = odl.Integers() fset = FunctionSet(str3, ints) strings = np.array(['aa', 'b', 'cab', 'aba']) out_vec = np.empty((4, ), dtype=int) # Vectorized for arrays only f_vec = fset.element(lambda s: np.array([str(si).count('a') for si in s])) true_vec = [2, 0, 1, 2] assert f_vec('abc') == 1 assert all_equal(f_vec(strings), true_vec) f_vec(strings, out=out_vec) assert all_equal(out_vec, true_vec)
def test_fspace_elem_eval_unusual_dtypes(): """Check evaluation with unusual data types (int and string).""" str3 = odl.Strings(3) fspace = FunctionSpace(str3, out_dtype=int) strings = np.array(['aa', 'b', 'cab', 'aba']) out_vec = np.empty((4, ), dtype=int) # Vectorized for arrays only func_elem = fspace.element( lambda s: np.array([str(si).count('a') for si in s])) true_values = [2, 0, 1, 2] assert func_elem('abc') == 1 assert all_equal(func_elem(strings), true_values) func_elem(strings, out=out_vec) assert all_equal(out_vec, true_values)
def test_fspace_elem_eval_unusual_dtypes(): """Check evaluation with unusual data types (int and string).""" domain = odl.Strings(3) strings = np.array(['aa', 'b', 'cab', 'aba']) out_vec = np.empty((4, ), dtype='int64') # Can be vectorized for arrays only sampl_func = sampling_function( lambda s: np.array([str(si).count('a') for si in s]), domain, out_dtype='int64') collocator = partial(point_collocation, sampl_func) true_values = [2, 0, 1, 2] assert collocator('abc') == 1 assert all_equal(collocator(strings), true_values) collocator(strings, out=out_vec) assert all_equal(out_vec, true_values)
def test_fset_init(): str3 = odl.Strings(3) ints = odl.Integers() FunctionSet(str3, ints)