def test_healpix_convolution(): nside = 16 keywords = { 'fwhm': np.radians(30), 'iter': 2, 'lmax': 8, 'use_weights': False } input = np.arange(12 * nside**2) op = HealpixConvolutionGaussianOperator(**keywords) for i in input, np.repeat(input[:, None], 3, 1): expected = np.transpose(hp.smoothing(i.T, verbose=False, **keywords)) assert_same(op(i), expected) if hp.__version__ <= '1.8.6': # healpy #259 return op = HealpixConvolutionGaussianOperator(pol=False, **keywords) input_ = np.arange(12 * nside**2) input = np.array([input_, input_, input_]).T expected_ = hp.smoothing(input_, verbose=False, **keywords) expected = np.array([expected_, expected_, expected_]).T assert_same(op(input), expected)
def test_gaussian(): fsamp = 5 nsamples = 2**10 white = [0.3, 0.5] fknee = 0. alpha = 1 def func(twosided): np.random.seed(0) p = _gaussian_psd_1f(nsamples, fsamp, white, fknee, alpha, twosided=twosided) t = _gaussian_sample(nsamples, fsamp, p, twosided=twosided) return p, t p1, t1 = func(False) p2, t2 = func(True) bandwidth = fsamp / nsamples assert_same(np.sum(p1, axis=-1) * bandwidth, np.sum(p2, axis=-1) * bandwidth, rtol=1000) assert_allclose(np.sqrt(np.sum(p1, axis=-1) * bandwidth), white, rtol=1e-3) assert_same(t1, t2)
def func(dtype, shape): sqrt3 = np.sqrt(np.array(3, dtype)) c = [sqrt3 / 2, np.array(1, dtype) / 2] coords = np.empty(shape + (2, ), dtype) coords[...] = c assert_same(rotate(coords, 30), e, broadcasting=True) assert_same(rotate(coords, 30, out=coords), e, broadcasting=True)
def test_endian(): bendian = _read_filters(path + 'invntt_be', bigendian=True) lendian = _read_filters(path + 'invntt_le') assert len(bendian) == 6 assert bendian[0]['data'].size == 101 assert_same(bendian[0]['data'][0], 5597147.4155586753) assert_eq(lendian, bendian)
def test_equ2gal(): equ2gal = CartesianEquatorial2GalacticOperator() gal2equ = CartesianGalactic2EquatorialOperator() assert equ2gal.I is equ2gal.T assert gal2equ.I is gal2equ.T assert_same(equ2gal.todense(shapein=3), equ2gal.data) assert_same(gal2equ.todense(shapein=3), equ2gal.data.T) shapes = (3, (2, 3), (4, 2, 3)) def func(op, shape): vec = np.arange(product(shape)).reshape(shape) vec_ = vec.reshape((-1, 3)) expected = np.empty(shape) expected_ = expected.reshape((-1, 3)) for i in range(expected.size // 3): expected_[i] = op(vec_[i]) actual = op(vec) assert_same(actual, expected) for op in (equ2gal, gal2equ): for shape in shapes: yield func, op, shape assert_is_instance(equ2gal(gal2equ), IdentityOperator) assert_is_instance(gal2equ(equ2gal), IdentityOperator)
def test_sky2(): assert_allclose(outputs['I'], outputs['IQU'][..., 0], atol=2e-2) assert_allclose(outputs['QU'], outputs['IQU'][..., 1:], atol=2e-2) for k in ('QU', 'IQU'): assert_equal(cbiks[k], cbiks['I']) assert_same(pT1s[k], pT1s['I'].astype(np.float32), rtol=15)
def func(m, d, v): expected = np.dot(m, v) assert_same(d(v), expected) if d.flags.square: w = v.copy() d(w, w) assert_same(w, expected)
def func(op1, op2): op = op1(op2) attr = {} attr.update(op2.attrout) attr.update(op1.attrout) assert_equal(op.attrout, attr) assert_is(op.classout, op1.classout) if op1.flags.linear: assert_is_type(op, ZeroOperator) assert_same(op.todense(shapein=3, shapeout=4), np.zeros((4, 3))) return if op1.flags.shape_output == 'unconstrained' or \ op1.flags.shape_input != 'explicit' and \ op2.flags.shape_output != 'explicit': assert_is_type(op, CompositionOperator) else: assert_is_type(op, ConstantOperator) if op1.flags.shape_input == 'unconstrained' and \ op2.flags.shape_output == 'unconstrained': return with rule_manager(none=True): op_ref = op1(op2) assert_same(op.todense(shapein=3, shapeout=4), op_ref.todense(shapein=3, shapeout=4))
def func(d): x_ = np.array(x if d.kind == 'c' else x.real, dtype=d) actual = abs2(x_) expected = np.abs(x_**2) assert_same(actual, expected) abs2(x_, actual) assert_same(actual, expected)
def test_add_subtract_grid_operator(): acq = QubicAcquisition(150, sampling, detector_ngrids=2) tod = map2tod(acq, input_map, convolution=False) add = acq.get_add_grids_operator() sub = acq.get_subtract_grids_operator() assert_same(add(tod), tod[:992] + tod[992:]) assert_same(sub(tod), tod[:992] - tod[992:])
def func(dtype, shape): sqrt3 = np.sqrt(np.array(3, dtype)) c = [sqrt3 / 2, np.array(1, dtype) / 2] coords = np.empty(shape + (2,), dtype) coords[...] = c assert_same(rotate(coords, 30), e, broadcasting=True) assert_same(rotate(coords, 30, out=coords), e, broadcasting=True)
def func(n, m): slices = split(n, m) assert_eq(len(slices), m) x = np.zeros(n, int) for s in slices: x[s] += 1 assert_same(x, 1, broadcasting=True) assert_eq([split(n, m, i) for i in range(m)], slices)
def func2(itype, ftype, shapein): mat = get_mat(itype, ftype) op = SparseOperator(mat) todense = op.todense(shapein=(6,) + shapein) assert_same(todense.T, op.T.todense(shapeout=(6,) + shapein)) op2 = SparseOperator(mat, shapeout=(2, 2) + shapein, shapein=(3, 2) + shapein) assert_same(op2.todense(), todense)
def func(spacing, angle): origin = shape_grid * np.array(spacing) * np.sqrt(2) / 2 grid = LayoutGridSquares(shape_grid, spacing, origin=origin, angle=angle, filling_factor=filling_factor) shape_plane = np.ceil(shape_grid * np.array(spacing) * np.sqrt(2)) scene = SceneGrid(shape_plane) proj = scene.get_integration_operator(grid.vertex) assert not proj.outside assert_same(np.sum(proj.matrix.data.value), len(grid) * spacing ** 2 * filling_factor, rtol=100)
def func(shape, xreflection, yreflection): actual = create_grid_squares( shape, spacing, filling_factor=filling_factor, xreflection=xreflection, yreflection=yreflection, center=origin) expected = create_grid_squares_slow( shape, spacing, filling_factor=filling_factor, xreflection=xreflection, yreflection=yreflection, center=origin) assert_same(actual, expected)
def test_scatter(): np.random.seed(0) n = 4 x = np.random.random(n) layout = PackedTable(n, x=x) s = split(n, size, rank) scattered = layout.scatter() assert_same(scattered.x, x[s]) assert_same(scattered.all.x, x)
def func(op, shape): vec = np.arange(product(shape)).reshape(shape) vec_ = vec.reshape((-1, 3)) expected = np.empty(shape) expected_ = expected.reshape((-1, 3)) for i in range(expected.size // 3): expected_[i] = op(vec_[i]) actual = op(vec) assert_same(actual, expected)
def test_dense_rule_homothety(): m = np.array([[1, 2], [3, 4], [5, 6]]) d = HomothetyOperator(2) * DenseOperator(m) assert_is_type(d, DenseOperator) assert_same(d.data, m * 2) d = HomothetyOperator(2j) * DenseOperator(m) assert_is_type(d, DenseOperator) assert_same(d.data, m * 2j) assert_equal(d.dtype, complex)
def func(lmax, n): class XpolDummy(Xpol): def __init__(self): self.lmax = lmax self.wl = np.ones(max(n+1, 1)) xpol = XpolDummy() mll = xpol._get_Mll(binning=False) expected = FitsArray('test/data/xpol_mll_{}_{}.fits'.format(lmax, n)) assert_same(mll, expected, atol=200)
def func(s, v): if s is setattr_unpacked: assert_raises(TypeError, s, layout, 'key', v) return s(layout, 'key', v) assert isalias(layout.key, val) assert isalias(layout.all.key, val) assert_same(layout.key, val) assert_same(layout.all.key, val.reshape(shape))
def test_noiseless(): sampling = create_random_pointings([0, 90], 100, 10) acq_qubic = QubicAcquisition(150, sampling) acq_planck = PlanckAcquisition(150, acq_qubic.scene, true_sky=SKY) acq_fusion = QubicPlanckAcquisition(acq_qubic, acq_planck) np.random.seed(0) y1 = acq_fusion.get_observation() np.random.seed(0) y2 = acq_fusion.get_observation(noiseless=True) + acq_fusion.get_noise() assert_same(y1, y2)
def func(format): cls = getattr(sp, format + '_matrix') shapein = (2, 2) shapeout = (1, 4, 1) so = SparseOperator(cls(A), shapein=shapein, shapeout=shapeout) for vec in vecs: assert_same(so(np.reshape(vec, shapein)), np.dot(A, vec).reshape(shapeout)) assert_same(so.T(np.reshape(vec, shapeout)), np.dot(A.T, vec).reshape(shapein))
def func(format): cls = getattr(sp, format + '_matrix') so = SparseOperator(cls(A)) out = np.zeros(4, dtype=int) outT = np.zeros(4, dtype=int) for vec in vecs: so(vec, out, operation=operator.iadd) so.T(vec, outT, operation=operator.iadd) assert_same(out, np.sum(A, axis=1)) assert_same(outT, np.sum(A, axis=0))
def test_special_attribute_func3(): shape = (6, 6) ordering = np.arange(product(shape))[::-1].reshape(shape) val = np.arange(product(shape)).reshape(shape) func = lambda s, x: x * s.val * s.myscalar1 * s._myscalar2 layout = PackedTable(shape, ordering=ordering, val=val, key=func) layout.myscalar1 = 2 layout._myscalar2 = 3 assert_same(layout.key(2), val.ravel()[::-1] * 12) assert_same(layout.all.key(2), val * 12)
def func(algo, reuse_initial_state): g = algo(reuse_initial_state=reuse_initial_state) pi = g.run() assert g.niterations == 10 assert_same(pi, np.pi) if g.reuse_initial_state: assert_raises(RuntimeError, g.restart) return g.restart() assert g.niterations == 10 assert_same(pi, np.pi)
def test_gaussian(): shape = (100, 90) sigma = 2.1, 2.4 center = 14.3, 52.2 dtype = float y, x = np.ogrid[0:shape[0], 0:shape[1]] ref = np.exp(-(x - center[0])**2 / (2 * sigma[0]**2) + -(y - center[1])**2 / (2 * sigma[1]**2)) / \ (2 * np.pi * sigma[0] * sigma[1]) actual = gaussian(shape, sigma=sigma, center=center, dtype=dtype) assert_same(actual, ref, atol=2)
def func(shape, degrees): angle = np.arange(product(shape)).reshape(shape) if degrees: angle_ = np.radians(angle) else: angle_ = angle angle_ = angle_.reshape(angle.size) r = Rotation2dOperator(angle, degrees=degrees) actual = r([1, 0]).reshape((angle.size, 2)) expected = np.array([np.cos(angle_), np.sin(angle_)]).T assert_same(actual, expected)
def test_composite(): global counter counter = 0 proxy_lists = [get_operator(proxy_list, attr) for attr in ('', 'C', 'T', 'H', 'I', 'IC', 'IT', 'IH')] ref_lists = [get_operator(ref_list, attr) for attr in ('', 'C', 'T', 'H', 'I', 'IC', 'IT', 'IH')] op = AdditionOperator(CompositionOperator(_) for _ in zip(*proxy_lists)) ref = AdditionOperator(CompositionOperator(_) for _ in zip(*ref_lists)) assert_same(op.todense(), ref.todense()) assert_equal(counter, nproxy * op.shapein[0])
def func(shape, xreflection, yreflection): actual = create_grid(shape, spacing, xreflection=xreflection, yreflection=yreflection, center=origin) expected = create_grid_slow(shape, spacing, xreflection=xreflection, yreflection=yreflection, center=origin) assert_same(actual, expected)
def func(s, v): layout = PackedTable(shape, key=np.ones(shape, int)) s(layout, 'key', v) assert_same(layout.key, val.ravel()) assert_equal(layout.key.dtype, float) assert_same(layout.all.key, val) assert_equal(layout.all.key.dtype, float) if s is setattr: assert isalias(layout.key, val) assert isalias(layout.all.key, val) else: assert not isalias(layout.key, val) assert not isalias(layout.all.key, val)
def func2(itype, ftype): mat = get_mat(itype, ftype) op = SparseOperator(mat, shapein=4) todense = op.todense() pTp = op.T * op if (itype, ftype) not in ((np.int32, np.float32), (np.int32, np.float64), (np.int64, np.float32), (np.int64, np.float64)): assert_is_type(pTp, CompositionOperator) return assert_is_type(pTp, DiagonalOperator) assert_same(pTp.todense(), np.dot(todense.T, todense))
def func(dtype, shape): n = max(product(shape), 1) size = np.arange(1, n + 1, dtype=dtype).reshape(shape) actual = create_square(size, center=origin, dtype=dtype) expected = np.empty(shape + (4, 2)) expected[..., 0, 0] = origin[0] + size / 2 expected[..., 0, 1] = origin[1] + size / 2 expected[..., 1, 0] = origin[0] - size / 2 expected[..., 1, 1] = origin[1] + size / 2 expected[..., 2, 0] = origin[0] - size / 2 expected[..., 2, 1] = origin[1] - size / 2 expected[..., 3, 0] = origin[0] + size / 2 expected[..., 3, 1] = origin[1] - size / 2 assert_same(actual, expected)
def test_zero6(): @flags.linear class Op(Operator): def direct(self, input, output): output[:] = np.concatenate([input, 2*input]) def transpose(self, input, output): output[:] = input[0:output.size] def reshapein(self, shapein): return (2 * shapein[0],) def reshapeout(self, shapeout): return (shapeout[0] // 2,) z = ZeroOperator(flags='square') o = Op() od = o.todense(shapein=4) zo = z * o zod_ref = np.dot(np.zeros((8, 8)), od) assert_same((z * o).todense(shapein=4), zod_ref) oz = o * z ozd_ref = np.dot(od, np.zeros((4, 4))) assert_same((o * z).todense(shapein=4), ozd_ref) assert_same(zo.T.todense(shapein=8), zod_ref.T) assert_same(oz.T.todense(shapein=8), ozd_ref.T)
def func(shape, xreflection, yreflection): actual = create_grid_squares(shape, spacing, filling_factor=filling_factor, xreflection=xreflection, yreflection=yreflection, center=origin) expected = create_grid_squares_slow(shape, spacing, filling_factor=filling_factor, xreflection=xreflection, yreflection=yreflection, center=origin) assert_same(actual, expected)
def test_healpix_laplacian(): nside = 1 ipix = 4 npix = 12 map = np.zeros(npix) map[ipix] = 1 L = HealpixLaplacianOperator(nside) assert L.flags.square assert L.flags.symmetric h2 = np.array(4 * np.pi / npix) expected = [1, 0, 0, 1, -20, 4, 0, 4, 1, 0, 0, 1] / (6 * h2) assert_same(L(map), expected) assert_same(L(np.repeat(map, 5).reshape(12, 5)), expected[:, None], broadcasting=True)
def func(spacing, angle): origin = shape_grid * np.array(spacing) * np.sqrt(2) / 2 grid = LayoutGridSquares(shape_grid, spacing, origin=origin, angle=angle, filling_factor=filling_factor) shape_plane = np.ceil(shape_grid * np.array(spacing) * np.sqrt(2)) scene = SceneGrid(shape_plane) proj = scene.get_integration_operator(grid.vertex) assert not proj.outside assert_same(np.sum(proj.matrix.data.value), len(grid) * spacing**2 * filling_factor, rtol=100)
def func1(itype, ftype, vtype, block_size): if np.dtype(itype).kind != 'u': exp = expected else: exp = expected_u input_ = np.array(input, vtype) if block_size == 2: input_ = np.array([input_, input_]).T.ravel() exp = np.array([exp, exp]).T.ravel() op = get_mat(itype, ftype) out = op * input_ assert_same(out, exp) out[...] = 0 op._matvec(input_, out=out) assert_same(out, exp)
def func(map, msk, partial, compress): with tempfile.TemporaryFile('a+b') as tmpfile: write_map(tmpfile, map, msk, compress=compress) tmpfile.seek(0) actual = read_map(tmpfile, partial=partial) if partial: out, mask_ = actual if msk is None: assert_is_none(mask_) actual = out else: actual = np.full(mask_.shape, np.nan) actual[mask_] = out assert_equal(actual.dtype.byteorder, '=') assert_same(actual, complete)
def func2(itype, ftype): array = np.recarray((6, 1), dtype=[('index', itype), ('r11', ftype), ('r22', ftype), ('r32', ftype)]) dense = np.zeros((6*3, 4*3), dtype=ftype) fill(array, dense, index1, value1, angle1, 0) op = SparseOperator(FSRRotation3dMatrix(dense.shape, data=array)) pTp = op.T * op if (itype, ftype) not in ((np.int32, np.float32), (np.int32, np.float64), (np.int64, np.float32), (np.int64, np.float64)): assert_is_type(pTp, CompositionOperator) return assert_is_type(pTp, DiagonalOperator) assert_same(pTp.todense(), np.dot(dense.T, dense), atol=1)
def func(center, ncolmax, itype, ftype, expected): detectors = create_grid_squares((2, 2), Quantity(pixsize, "um"), center=center) proj = plane.get_integration_operator(plane.topixel(detectors), ncolmax=ncolmax, dtype=ftype, dtype_index=itype) x = np.arange(len(plane)).reshape(plane_shape) y = proj(x) assert_equal(proj.matrix.dtype, ftype) assert_equal(proj.matrix.data.index.dtype, itype) assert_equal(proj.shapein, plane.shape) assert_equal(proj.shapeout, detectors.shape[:-2]) assert_equal(proj.matrix.shape, (4, len(plane))) expected_min_ncolmax = 1 if center == (0, 0) else 4 assert_equal(proj.matrix.data.shape, (4, max(expected_min_ncolmax, ncolmax))) assert_equal(proj.min_ncolmax, expected_min_ncolmax) assert_same(y, expected)