def test_layout_vertex(): shape = (4, 4) vertex = create_grid_squares(shape, 0.1, filling_factor=0.8) center = np.mean(vertex, axis=-2) get_vertex = lambda: vertex.reshape(-1, 4, 2) class Vertex1(Layout): @property def vertex(self): return get_vertex() class Vertex2(Layout): def vertex(self): return get_vertex() layouts = (Layout(shape, vertex=vertex), Layout(shape, vertex=get_vertex), Vertex1(shape), Vertex2(shape)) def func(layout): assert_equal(layout.nvertices, 4) assert_same(layout.center, center.reshape((-1, 2))) assert_same(layout.all.center, center) assert_same(layout.vertex, vertex.reshape((-1, 4, 2))) assert_same(layout.all.vertex, vertex) for layout in layouts: yield func, layout
def test_layout_grid_squares(): shape = (3, 2) spacing = 0.1 filling_factor = 0.9 xreflection = True yreflection = True origin = (1, 1) angle = 10 vertex = create_grid_squares(shape, spacing, filling_factor=filling_factor, center=origin, xreflection=xreflection, yreflection=yreflection, angle=angle) layout = LayoutGridSquares(shape, spacing, filling_factor=filling_factor, origin=origin, xreflection=xreflection, yreflection=yreflection, angle=angle) assert layout.nvertices == 4 assert_same(layout.all.center, np.mean(vertex, axis=-2)) assert_same(layout.all.vertex, vertex) assert_same(layout.all.removed, np.zeros(shape, bool))
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 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 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)
def test_layout_grid_squares(): shape = (3, 2) spacing = 0.1 filling_factor = 0.9 xreflection = True yreflection = True origin = (1, 1) angle = 10 vertex = create_grid_squares( shape, spacing, filling_factor=filling_factor, center=origin, xreflection=xreflection, yreflection=yreflection, angle=angle) layout = LayoutGridSquares( shape, spacing, filling_factor=filling_factor, origin=origin, xreflection=xreflection, yreflection=yreflection, angle=angle) assert layout.nvertices == 4 assert_same(layout.all.center, np.mean(vertex, axis=-2)) assert_same(layout.all.vertex, vertex) assert_same(layout.all.removed, np.zeros(shape, bool))
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)
def test_packunpack(): shape = (3, 3) size = 9 ordering1 = np.arange(size)[::-1].reshape(shape) ordering2 = ordering1.copy() ordering2[0, 2] = -1 selection = [[False, True, True], [True, True, True], [False, True, False]] vertex = create_grid_squares(shape, 0.1) center = np.mean(vertex, axis=-2) reccenter = np.recarray(center.shape[:-1], [('x', float), ('y', float)]) reccenter.x, reccenter.y = center[..., 0], center[..., 1] recvertex = np.recarray(vertex.shape[:-1], [('x', float), ('y', float)]) recvertex.x, recvertex.y = vertex[..., 0], vertex[..., 1] valb = (np.arange(size, dtype=int) % 2).astype(bool).reshape(shape) vali = np.arange(size, dtype=int).reshape(shape) valu = np.arange(size, dtype=np.uint64).reshape(shape) valf = np.arange(size, dtype=float).reshape(shape) valb2 = (np.arange(size * 2, dtype=int) % 2).astype(bool).reshape( shape + (2,)) vali2 = np.arange(size * 2, dtype=int).reshape(shape + (2,)) valu2 = np.arange(size * 2, dtype=np.uint64).reshape(shape + (2,)) valf2 = np.arange(size * 2, dtype=float).reshape(shape + (2,)) valb3 = (np.arange(size * 2 * 3, dtype=int) % 2).astype(bool).reshape( shape + (2, 3)) vali3 = np.arange(size * 2 * 3, dtype=int).reshape(shape + (2, 3)) valu3 = np.arange(size * 2 * 3, dtype=np.uint64).reshape(shape + (2, 3)) valf3 = np.arange(size * 2 * 3, dtype=float).reshape(shape + (2, 3)) data = (valb, vali, valu, valf, valb2, vali2, valu2, valf2, valb3, vali3, valu3, valf3, center, reccenter, vertex, recvertex) def missing(x): kind = x.dtype.kind if kind == 'b': return ~x if kind == 'i': return x == -1 if kind == 'u': return x == 0 if kind == 'f': return ~np.isfinite(x) if kind == 'V': if x.dtype.names == ('x', 'y'): return ~np.isfinite(x.x) & ~np.isfinite(x.y) assert False def func(s, i, d): layout = PackedTable(shape, vertex=vertex, center=center, selection=s, ordering=i) packed = layout.pack(d) assert_equal(packed.shape, (len(layout),) + d.shape[2:]) d_ = layout.unpack(packed) assert_equal(d_.shape, d.shape) assert np.all((d == d_) | missing(d_)) out_packed = np.empty((len(layout),) + d.shape[2:], d.dtype).view(type(d)) out_unpacked = np.empty(d.shape, d.dtype).view(type(d)) layout.pack(d, out=out_packed) layout.unpack(out_packed, out=out_unpacked) assert np.all((d == out_unpacked) | missing(out_unpacked)) out_unpacked = np.empty((6,)+d.shape[1:], d.dtype)[::2].view(type(d)) out_packed = np.empty((out_packed.shape[0]*2,)+out_packed.shape[1:], d.dtype)[::2].view(type(d)) layout.pack(d, out=out_packed) layout.unpack(out_packed, out=out_unpacked) assert np.all((d == out_unpacked) | missing(out_unpacked)) for s, i in ((None, None), (None, ordering1), (None, ordering2), (selection, None), (selection, ordering1), (selection, ordering2)): for d in data: yield func, s, i, d