def testReductionSum2(self): dds = mango.ones(shape=self.shape, dtype="uint16", halo=2) dds.setBorderToValue(100) centreIdx = tuple(sp.array(dds.subd.shape) // 2) if ((dds.mpi.comm == None) or (dds.mpi.comm.Get_rank() == 0)): dds[centreIdx] = 2 expectSum = sp.product(dds.shape) + 2 * 2 - 1 s = mango.sum2(dds, dtype="uint64") self.assertEqual(expectSum, s) dds = mango.ones(shape=self.shape, mtype="tomo", halo=2) dds.setBorderToValue(100) centreIdx = tuple(sp.array(dds.subd.shape) // 2) if ((dds.mpi.comm == None) or (dds.mpi.comm.Get_rank() == 0)): dds[centreIdx] = 9 expectSum = sp.product(dds.shape) + 9 * 9 - 1 s = mango.sum2(dds, dtype="uint64") self.assertEqual(expectSum, s) dds = mango.ones(shape=self.shape, mtype="tomo", halo=2) dds.setBorderToValue(0) centreIdx = tuple(sp.array(dds.subd.shape) // 2) if ((dds.mpi.comm == None) or (dds.mpi.comm.Get_rank() == 0)): dds[centreIdx] = 9 if ((dds.mpi.comm == None) or (dds.mpi.comm.Get_rank() == 0)): dds[centreIdx[0], centreIdx[1], centreIdx[2] + 1] = dds.mtype.maskValue() expectSum = sp.product(dds.shape) + 9 * 9 - 1 - 1 s = mango.sum2(dds, dtype="uint64") self.assertEqual(expectSum, s)
def testReductionMinMax(self): dds = mango.ones(shape=self.shape, dtype="uint16", halo=2) dds.setBorderToValue(0) centreIdx = tuple(sp.array(dds.subd.shape) // 2) if ((dds.mpi.comm == None) or (dds.mpi.comm.Get_rank() == 0)): dds[centreIdx] = 2 mn, mx = mango.minmax(dds) self.assertEqual(1, mn) self.assertEqual(2, mx) dds = mango.ones(shape=self.shape, mtype="tomo", halo=2) dds.setBorderToValue(0) centreIdx = tuple(sp.array(dds.subd.shape) // 2) if ((dds.mpi.comm == None) or (dds.mpi.comm.Get_rank() == 0)): dds[centreIdx] = 2 mn, mx = mango.minmax(dds) rootLogger.info("mn,mx = %s" % ((mn, mx), )) self.assertEqual(1, mn) self.assertEqual(2, mx) dds = mango.ones(shape=self.shape, mtype="tomo", halo=2) dds.setBorderToValue(0) centreIdx = tuple(sp.array(dds.subd.shape) // 2) if ((dds.mpi.comm == None) or (dds.mpi.comm.Get_rank() == 0)): dds[centreIdx] = 2 if ((dds.mpi.comm == None) or (dds.mpi.comm.Get_rank() == 0)): dds[centreIdx[0], centreIdx[1], centreIdx[2] + 1] = dds.mtype.maskValue() mn, mx = mango.minmax(dds) rootLogger.info("mn,mx = %s" % ((mn, mx), )) self.assertEqual(1, mn) self.assertEqual(2, mx) mn, mx = dds.minmax() self.assertEqual(1, mn) self.assertEqual(dds.mtype.maskValue(), mx)
def testDdsCreateFloat16(self): """ Test the :func:`mango.empty` and :func:`mango.ones` and :func:`mango.zeros` methods for :samp:`dtype="float16"` and :samp:`mtype="float16"`. """ if (mango.haveFloat16): dds = mango.empty(shape=self.shape, dtype="float16") fVal = np.float16(2.25) dds.setAllToValue(fVal) self.assertEqual(fVal, dds[0]) self.assertEqual(fVal, dds[0, 0, 0]) self.assertEqual(fVal, dds[(0, 0, 0)]) dds = mango.zeros(shape=self.shape, mtype="float16") fVal = np.float16(2.25) dds.asarray()[...] += fVal self.assertEqual(fVal, dds[0]) self.assertEqual(fVal, dds[0, 0, 0]) self.assertEqual(fVal, dds[(0, 0, 0)]) dds = mango.ones(shape=self.shape, mtype="float16") fVal = np.float16(2.25) dds.asarray()[...] *= fVal self.assertEqual(fVal, dds[0]) self.assertEqual(fVal, dds[0, 0, 0]) self.assertEqual(fVal, dds[(0, 0, 0)])
def testDdsCopyCreate(self): """ Test the :func:`mango.copy` method. """ dds0 = mango.ones(shape=self.shape, dtype="uint8") dds1 = mango.copy(dds0, dtype="uint16") self.assertEqual(dds0[0], dds1[0]) self.assertEqual(dds0[0, 0, 0], dds1[0, 0, 0]) self.assertEqual(dds0[(0, 0, 0)], dds1[(0, 0, 0)])
def testDdsCreate(self): """ Test the :func:`mango.empty` and :func:`mango.ones` and :func:`mango.zeros` methods. """ dds = mango.zeros(shape=self.shape, dtype="uint16") self.assertEqual(0, dds[0]) self.assertEqual(0, dds[0, 0, 0]) self.assertEqual(0, dds[(0, 0, 0)]) dds = mango.ones(shape=self.shape, dtype="uint8") self.assertEqual(1, dds[0]) self.assertEqual(1, dds[0, 0, 0]) self.assertEqual(1, dds[(0, 0, 0)]) self.assertEqual(tuple(self.shape), tuple(dds.shape)) self.assertEqual(None, dds.mtype) dds = mango.ones(shape=self.shape, mtype="tomo_float") self.assertEqual(tuple(self.shape), tuple(dds.shape)) self.assertEqual(mango.mtype("tomo_float").dtype, dds.dtype) self.assertEqual(mango.mtype("tomo_float"), dds.mtype) dds = mango.ones(shape=self.shape, mtype="tomo_float", origin=(20, 50, -100)) elDds = mango.empty_like(dds) self.assertTrue(sp.all(elDds.origin == dds.origin)) self.assertTrue(sp.all(elDds.shape == dds.shape)) self.assertTrue(sp.all(elDds.subd.origin == dds.subd.origin)) self.assertTrue(sp.all(elDds.subd.shape == dds.subd.shape)) elDds = mango.empty_like(dds, origin=dds.origin + (10, 20, 40)) self.assertTrue(sp.all(elDds.origin == dds.origin + (10, 20, 40))) self.assertTrue(sp.all(elDds.shape == dds.shape)) self.assertTrue( sp.all(elDds.subd.origin == dds.subd.origin + (10, 20, 40))) self.assertTrue(sp.all(elDds.subd.shape == dds.subd.shape)) self.assertNotEqual(dds.mtype, None) self.assertEqual(dds.mtype, elDds.mtype)
def testReductionCountMasked(self): dds = mango.ones(shape=self.shape, dtype="uint16", halo=2) dds.setBorderToValue(100) centreIdx = tuple(sp.array(dds.subd.shape) // 2) if ((dds.mpi.comm == None) or (dds.mpi.comm.Get_rank() == 0)): dds[centreIdx] = 2 expectMskCount = 0 expectNonMskCount = sp.product(dds.shape) - expectMskCount mc = mango.count_masked(dds) nmc = mango.count_non_masked(dds) self.assertEqual(expectMskCount, mc) self.assertEqual(expectNonMskCount, nmc) dds = mango.ones(shape=self.shape, mtype="tomo", halo=2) dds.setBorderToValue(100) centreIdx = tuple(sp.array(dds.subd.shape) // 2) if ((dds.mpi.comm == None) or (dds.mpi.comm.Get_rank() == 0)): dds[centreIdx] = dds.mtype.maskValue() expectMskCount = 1 expectNonMskCount = sp.product(dds.shape) - expectMskCount mc = mango.count_masked(dds) nmc = mango.count_non_masked(dds) self.assertEqual(expectMskCount, mc) self.assertEqual(expectNonMskCount, nmc) dds = mango.ones(shape=self.shape, mtype="tomo", halo=2) dds.setBorderToValue(dds.mtype.maskValue()) centreIdx = tuple(sp.array(dds.subd.shape) // 2) if ((dds.mpi.comm == None) or (dds.mpi.comm.Get_rank() == 0)): dds[centreIdx] = dds.mtype.maskValue() if ((dds.mpi.comm == None) or (dds.mpi.comm.Get_rank() == 0)): dds[centreIdx[0], centreIdx[1], centreIdx[2] + 1] = dds.mtype.maskValue() expectMskCount = 2 expectNonMskCount = sp.product(dds.shape) - expectMskCount mc = mango.count_masked(dds) nmc = mango.count_non_masked(dds) self.assertEqual(expectMskCount, mc) self.assertEqual(expectNonMskCount, nmc)
def testDdsCreateComplex(self): """ Test the :func:`mango.empty` and :func:`mango.ones` and :func:`mango.zeros` methods for dtype=complex64/complex128. """ dds = mango.empty(shape=self.shape, dtype="complex64") cVal = np.complex64(2 + 1j * 4) dds.setAllToValue(cVal) self.assertEqual(cVal, dds[0]) self.assertEqual(cVal, dds[0, 0, 0]) self.assertEqual(cVal, dds[(0, 0, 0)]) dds = mango.zeros(shape=self.shape, dtype="complex64") self.assertEqual(np.complex64(0), dds[0]) self.assertEqual(np.complex64(0), dds[0, 0, 0]) self.assertEqual(np.complex64(0), dds[(0, 0, 0)]) dds = mango.ones(shape=self.shape, dtype="complex64") self.assertEqual(np.complex64(1), dds[0]) self.assertEqual(np.complex64(1), dds[0, 0, 0]) self.assertEqual(np.complex64(1), dds[(0, 0, 0)]) dds = mango.empty(shape=self.shape, dtype="complex128") cVal = np.complex128(2 + 1j * 4) dds.setAllToValue(cVal) self.assertEqual(cVal, dds[0]) self.assertEqual(cVal, dds[0, 0, 0]) self.assertEqual(cVal, dds[(0, 0, 0)]) dds = mango.zeros(shape=self.shape, dtype="complex128") self.assertEqual(np.complex128(0), dds[0]) self.assertEqual(np.complex128(0), dds[0, 0, 0]) self.assertEqual(np.complex128(0), dds[(0, 0, 0)]) dds = mango.ones(shape=self.shape, dtype="complex128") self.assertEqual(np.complex128(1), dds[0]) self.assertEqual(np.complex128(1), dds[0, 0, 0]) self.assertEqual(np.complex128(1), dds[(0, 0, 0)])
def testDdsIndexingSetAndGet(self): """ Test the :meth:`Dds.__getitem__` and :meth:`Dds.__setitem__` methods. """ dds = mango.zeros(shape=self.shape, dtype="uint16") dds[0, 0, 0] = 5 self.assertEqual(5, dds[0]) self.assertEqual(5, dds[0, 0, 0]) self.assertEqual(5, dds[(0, 0, 0)]) dds = mango.ones(shape=self.shape, dtype="uint8") self.assertEqual(1, dds[0]) self.assertEqual(1, dds[0, 0, 0]) self.assertEqual(1, dds[(0, 0, 0)])
def testAllGather(self): dds = mango.ones(shape=self.shape, mtype="tomo", origin=(5, 11, 7), halo=2) allDds = mango.gather(dds) self.assertTrue(sp.all(dds.shape == allDds.shape)) self.assertTrue(sp.all(dds.origin == allDds.origin)) self.assertTrue(sp.all(dds.halo == allDds.halo)) self.assertTrue(sp.all(dds.shape == allDds.subd.shape)) self.assertTrue(sp.all(dds.origin == allDds.subd.origin)) self.assertTrue(sp.all(dds.halo == allDds.subd.halo)) self.assertEqual(dds.dtype, allDds.dtype) self.assertEqual(dds.mtype, allDds.mtype)
def testDdsSetGlobalOrigin(self): """ Test the :attr:`mango.Dds.origin` attribute for read/write access. """ origin = (5, 3, 1) dds0 = mango.ones(shape=self.shape, dtype="uint8", origin=origin) self.assertTrue(sp.all(origin == dds0.origin)) dds1 = mango.copy(dds0, dtype="uint16") self.assertTrue(sp.all(origin == dds1.origin)) newOrigin = sp.array((8, 16, 2), dtype="int32") dds0.origin = newOrigin self.assertTrue(sp.all(newOrigin == dds0.origin)) self.assertTrue( sp.all(dds1.subd.origin + (newOrigin - origin) == dds0.subd.origin))
def testDdsFill(self): """ Test the :meth:`Dds.fill` method. """ ddsDst = mango.zeros(shape=self.shape, dtype="uint16", mpidims=(0, 1, 1)) self.assertEqual(0, ddsDst[0, 0, 0]) ddsSrc = mango.ones(shape=self.shape, dtype="uint16", mpidims=(1, 0, 1)) self.assertEqual(1, ddsSrc[0, 0, 0]) ddsDst.fill(ddsSrc) self.assertEqual(1, ddsDst[0, 0, 0])
def testSingleProcessGather(self): dds = mango.ones(shape=self.shape, mtype="labels", origin=(5, 11, 7), halo=2) if (dds.mpi.comm != None): for rootRank in range(0, dds.mpi.comm.Get_size()): rootDds = mango.gather(dds, root=rootRank) self.assertTrue(sp.all(dds.shape == rootDds.shape)) self.assertTrue(sp.all(dds.origin == rootDds.origin)) self.assertEqual(dds.dtype, rootDds.dtype) self.assertEqual(dds.mtype, rootDds.mtype) if (dds.mpi.comm.Get_rank() == rootRank): self.assertTrue(sp.all(dds.halo == rootDds.halo)) self.assertTrue(sp.all(dds.shape == rootDds.subd.shape)) self.assertTrue(sp.all(dds.origin == rootDds.subd.origin)) self.assertTrue(sp.all(dds.halo == rootDds.subd.halo)) else: self.assertTrue(sp.all(0 == rootDds.halo)) self.assertTrue(sp.all(0 == rootDds.subd.shape)) self.assertTrue(sp.all(dds.origin == rootDds.subd.origin)) self.assertTrue(sp.all(0 == rootDds.subd.halo))