def test_ndim3(): shape = (13, 3) l = PackedTable(shape, ndim=1) l1, l2 = l.split(2) assert_equal(len(l1), 7) assert_equal(l1.shape, shape) assert_equal(len(l2), 6) assert_equal(l2.shape, shape)
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(shape, ndim, ordering, value): table = PackedTable(shape, ndim=ndim, ordering=ordering, value=value) table_local = table.scatter() table_global = table_local.gather() assert_equal(table_global.shape, table.shape) assert_equal(table_global.ndim, table.ndim) assert_equal(table_global._index, table._index) assert_equal(table_global.removed, table.removed) assert_equal(table_global.value, table.value)
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(n, s, m): if s is not Ellipsis: tmp = np.ones(n, bool) tmp[s] = False s = tmp layout = PackedTable(n, selection=s, val=np.arange(n)*2.) slices = layout.split(m) assert_eq(len(slices), m) o = np.zeros(layout.shape, int) v = np.full(layout.shape, np.nan) for s in slices: o[s._index] += 1 v[s._index] = s.val o[o == 0] = -1 assert_same(o, layout.unpack(1)) assert_same(v, layout.all.val) assert_same(np.concatenate([_.val for _ in slices]), layout.val)
def func(s): sn = PackedTable._normalize_slice(s, n) if sn is Ellipsis: assert_equal(array, array[s]) return assert_equal((sn.stop - sn.start) // sn.step, len(array[s])) if sn.stop < 0: sn = slice(sn.start, None, sn.step) assert_equal(array[sn], array[s])
def test_special_attribute_func2(): shape = (6, 6) ordering = np.arange(product(shape))[::-1].reshape(shape) val = np.arange(product(shape)).reshape(shape) layout = PackedTable(shape, ordering=ordering, val=val, key=None) layout.myscalar1 = 2 layout._myscalar2 = 3 layout_funcs = (lambda: val.ravel()[::-1] * 6, lambda s: s.val * s.myscalar1 * s._myscalar2) def func(v): setattr(layout, 'key', v) assert_equal(layout.all.key, val * 6) assert_equal(layout.key, val.ravel()[::-1] * 6) assert_same(layout.all.key.shape, layout.shape) assert_same(layout.key.shape, (len(layout),)) for v in layout_funcs: yield func, v
def test(): scene = Scene(1024) instrument = Instrument('instrument', PackedTable((32, 32))) sampling = Sampling(1000) acq = Acquisition(instrument, sampling, scene, nprocs_sampling=max(size // 2, 1)) print(acq.comm.rank, acq.instrument.detector.comm.rank, '/', acq.instrument.detector.comm.size, acq.sampling.comm.rank, '/', acq.sampling.comm.size)
def func3(shape): instrument = Instrument('', PackedTable(shape)) acq = MyAcquisition3(instrument, sampling, scene) noise = acq.get_noise() assert noise.shape == (len(acq.instrument), len(acq.sampling)) assert_allclose(np.std(noise), sigma, rtol=1e-2)
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))
def test_pack_none(): layout = PackedTable((3, 3)) assert_is_none(layout.pack(None)) assert_is_none(layout.unpack(None))
def __init__(self, shape, ordering, val): PackedTable.__init__(self, shape, ordering=ordering, val=val, key1=None, key2=None, key3=None)
def func(cls, keywords): instrument = cls(name, PackedTable(shape), **keywords) assert instrument.name == name assert instrument.detector.shape == shape assert len(instrument.detector) == len(instrument.detector.all) assert instrument
def test_pack_unpack(): layout = PackedTable(4, selection=[True, False, True, True]) instrument = Instrument('instrument', layout) v = [1, 2, 3, 4] assert_same(instrument.pack(v), [1, 3, 4]) assert_same(instrument.unpack([1, 3, 4]), [1, -1, 3, 4])