def testDdsSubdAttributes(self): """ Test :prop:`Dds.subd` and :prop:`Dds.subd_h` objects. """ dds = mango.zeros(shape=self.shape, dtype="uint16", mpidims=(0, 1, 1)) self.assertEqual(0, dds[0, 0, 0]) dds[0, 0, 0] = 1 self.assertEqual(1, dds[0, 0, 0]) self.assertEqual(1, dds.subd[0, 0, 0]) self.assertEqual(1, dds.subd_h[0, 0, 0]) for h in (0, 1, 2, 3): for halo in ((h, h, h), (2 * h, h // 2, h), (h, 2 * h, h // 2)): dds = mango.zeros(shape=self.shape, dtype="uint16", halo=halo) rootLogger.info("====") rootLogger.info( "dds.halo = %s" % (dds.halo, )) rootLogger.info( "dds.subd.origin = %s" % (dds.subd.origin, )) rootLogger.info( "dds.subd.shape = %s" % (dds.subd.shape, )) rootLogger.info( "dds.subd.asarray().shape = %s" % (dds.subd.asarray().shape, )) rootLogger.info( "dds.subd_h.asarray().shape = %s" % (dds.subd_h.asarray().shape, )) rootLogger.info( "dds.subd_h.asarray().shape-(2*dds.halo) = %s" % (dds.subd_h.asarray().shape - (2 * dds.halo), )) rootLogger.info( "dds.subd_h.origin = %s" % (dds.subd_h.origin, )) rootLogger.info( "dds.subd_h.shape = %s" % (dds.subd_h.shape, )) self.assertTrue( sp.all(dds.subd.origin == (dds.subd_h.origin + dds.halo))) self.assertTrue( sp.all(dds.subd.shape == (dds.subd_h.shape - (2 * dds.halo)))) self.assertTrue( sp.all(dds.subd.asarray().shape == ( dds.subd_h.asarray().shape - (2 * dds.halo)))) self.assertEqual( 0, dds.subd_h.asarray()[halo[0], halo[1], halo[2]]) self.assertEqual(0, dds.subd.asarray()[0, 0, 0]) dds.subd.asarray()[0, 0, 0] = 1 self.assertEqual(1, dds.subd.asarray()[0, 0, 0]) self.assertEqual( 1, dds.subd_h.asarray()[halo[0], halo[1], halo[2]])
def testCircularCylinderFill(self): dir = self.createTmpDir("testCircularCylinderFill") #dir = "." dds = mango.zeros(shape=self.shape, dtype="uint16") c = dds.shape*0.5 r = np.min(dds.shape-3)*0.5 axislen = dds.shape[0]*2 mango.data.fill_circular_cylinder(dds, c, r, axislen, fill=1) mango.io.writeDds(os.path.join(dir, "tomoCyl0.nc"), dds) dds1 = mango.zeros(shape=self.shape, dtype="uint16", origin=(100, 55, 13)) c = dds.shape*0.5 r = np.min(dds.shape-3)*0.5 axislen = dds.shape[0]*2 mango.data.fill_circular_cylinder(dds1, c, r, axislen, fill=1) mango.io.writeDds(os.path.join(dir, "tomoCyl1.nc"), dds1) self.assertTrue(sp.all(dds.subd.asarray() == dds1.subd.asarray())) del dds1 dds2 = mango.zeros(shape=self.shape, mtype="tomo", origin=(100, 55, 13)) dds2.md.setVoxelSize((0.1,0.1,0.1), "cm") c = dds.shape*0.5 r = np.min(dds.shape-3)*0.5 axislen = dds.shape[0]*2 mango.data.fill_circular_cylinder(dds2, c, r, axislen, fill=1, unit="mm") mango.io.writeDds(os.path.join(dir, "tomoCyl2.nc"), dds2) self.assertTrue(sp.all(dds.subd.asarray() == dds2.subd.asarray())) del dds2 dds3 = mango.zeros(shape=self.shape, mtype="tomo", origin=(100, 55, 13)) dds3.md.setVoxelSize((2.5,2.5,2.5), "um") c = dds.shape*0.5*dds3.md.getVoxelSize("mm") r = np.min(dds.shape-3)*0.5*dds3.md.getVoxelSize("mm")[0] axislen = dds.shape[0]*2*dds3.md.getVoxelSize("mm")[0] mango.data.fill_circular_cylinder(dds3, c, r, axislen, fill=1, unit="mm") mango.io.writeDds(os.path.join(dir, "tomoCyl3.nc"), dds3) self.assertTrue(sp.all(dds.subd.asarray() == dds3.subd.asarray())) del dds3 dds4 = mango.zeros(shape=self.shape, mtype="tomo", origin=(100, 55, 13)) dds4.md.setVoxelSize((2.5,2.5,2.5), "um") c = (dds4.origin + dds.shape*0.5)*dds4.md.getVoxelSize("mm") r = np.min(dds.shape-3)*0.5*dds4.md.getVoxelSize("mm")[0] axislen = dds.shape[0]*2*dds4.md.getVoxelSize("mm")[0] mango.data.fill_circular_cylinder(dds4, c, r, axislen, fill=1, unit="mm", coordsys="abs") mango.io.writeDds(os.path.join(dir, "tomoCyl4.nc"), dds4) self.assertTrue(sp.all(dds.subd.asarray() == dds4.subd.asarray())) del dds4
def testDdsAsArray(self): """ Test the :meth:`Dds.asarray` method. """ dds = mango.zeros(shape=self.shape, dtype="uint16", mpidims=(0, 1, 1)) self.assertEqual(0, dds[0, 0, 0]) a = dds.asarray() a += 10 self.assertEqual(10, dds[0, 0, 0]) a -= 10 self.assertEqual(0, dds[0, 0, 0]) a = dds.asarray() del dds a += 10 self.assertEqual(10, a[0, 0, 0]) a0 = self.createTmpArrayFromDds() self.assertEqual(0, a0[0, 0, 0]) a0 += 16 self.assertEqual(16, a0[0, 0, 0]) a += 20 self.assertEqual(30, a[0, 0, 0])
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 testNonDistributedMetric(self): if ((mpi.world == None) or (mpi.world.Get_size() == 1)): img = mango.zeros((128, 64, 64), mtype="tomo") img.md.setVoxelSize((1, 1, 1), "voxel") c = img.origin + img.shape * 0.5 r = np.min(img.shape) * 0.4 a = (1, 0, 0) mango.data.fill_circular_cylinder(img, c, r, img.shape[0] * 1.1, fill=32000, coordsys="abs", unit="voxel") metric = cylinder_fit_gradient_metric() metric.setGradientFromImage(img) x = metric.getDefaultX() rootLogger.info("Default x = %s" % (x, )) x[metric.CENTRE_X_IDX] = c[2] x[metric.CENTRE_Y_IDX] = c[1] x[metric.CENTRE_Z_IDX] = c[0] x[metric.AXIS_X_IDX] = a[2] x[metric.AXIS_Y_IDX] = a[1] x[metric.AXIS_Z_IDX] = a[0] bestSoln = (0, 0) for rr in range(-5, 6): x[metric.RADIUS_IDX] = r + rr val = metric(x) if (val > bestSoln[0]): bestSoln = (val, x[metric.RADIUS_IDX]) rootLogger.info("metric[%s] = %16.2f" % (x, val)) self.assertEqual(bestSoln[1], r) self.assertLess(0, bestSoln[0])
def testMultiResFit1(self): dir = self.createTmpDir("CylinderFitMultiResTest_testMultiResFit1") #dir = "." rootLogger.info("Generating synthetic image...") #img = mango.zeros((128+((mpi.size//8)*16),64,64), mtype="tomo") img = mango.zeros((512+((mpi.size//8)*16),256,256), mtype="tomo") #img = mango.zeros((1024,512,512), mtype="tomo") img.md.setVoxelSize((0.002,0.002,0.002), "mm") c = (img.origin + img.shape*0.5)*img.md.getVoxelSize() r = np.min(img.shape*img.md.getVoxelSize())*0.4 a = (1,0,0) img.subd_h.asarray()[...] = 10000 mango.data.fill_circular_cylinder(img, c, r, img.shape[0]*1.1, fill=25000, coordsys="abs", unit=img.md.getVoxelSizeUnit()) img.subd.asarray()[...] += mango.data.gaussian_noise_like(img, mean=0, stdd=300).subd.asarray() mango.io.writeDds(os.path.join(dir,"tomoCyl1.nc"), img) rootLogger.info("Done generating synthetic image.") fit, cylImg = cylinder_fit_multi_res(img, 1, resolutions=[64, 128, 512], retcylimg=True, metricThreshRelTol=0.50) mango.io.writeDds(os.path.join(dir,"segmentedCyl1AnnularMsk.nc"), cylImg) rootLogger.info("Img voxel size = %s %s" % (img.md.getVoxelSize(), img.md.getVoxelSizeUnit())) rootLogger.info("Cyl img voxel size = %s %s" % (cylImg.md.getVoxelSize(), cylImg.md.getVoxelSizeUnit())) rootLogger.info("c=%s, r=%s, a=%s" % (c,r,a)) rootLogger.info("Fit Results:\n%s" % ("\n".join(map(str,fit)),)) f0 = fit[0] self.assertAlmostEqual(c[1], f0[1][0][1], 3) self.assertAlmostEqual(c[2], f0[1][0][2], 3) self.assertAlmostEqual(r, f0[1][1], 3) self.assertAlmostEqual(sp.absolute(sp.dot(a, f0[1][3])), 1.0, 3)
def testNonDistributedMetric(self): if ((mpi.world == None) or (mpi.world.Get_size() == 1)): img = mango.zeros((128,64,64), mtype="tomo") img.md.setVoxelSize((1,1,1), "voxel") c = img.origin + img.shape*0.5 r = np.min(img.shape)*0.4 a = (1,0,0) mango.data.fill_circular_cylinder(img, c, r, img.shape[0]*1.1, fill=32000, coordsys="abs", unit="voxel") metric = cylinder_fit_gradient_metric() metric.setGradientFromImage(img) x = metric.getDefaultX() rootLogger.info("Default x = %s" % (x,)) x[metric.CENTRE_X_IDX] = c[2] x[metric.CENTRE_Y_IDX] = c[1] x[metric.CENTRE_Z_IDX] = c[0] x[metric.AXIS_X_IDX] = a[2] x[metric.AXIS_Y_IDX] = a[1] x[metric.AXIS_Z_IDX] = a[0] bestSoln = (0,0) for rr in range(-5, 6): x[metric.RADIUS_IDX] = r + rr val = metric(x) if (val > bestSoln[0]): bestSoln = (val, x[metric.RADIUS_IDX]) rootLogger.info("metric[%s] = %16.2f" % (x, val)) self.assertEqual(bestSoln[1], r) self.assertLess(0, bestSoln[0])
def testNonDistributedOptimization(self): if ((mpi.world == None) or (mpi.world.Get_size() == 1)): img = mango.zeros((128, 64, 64), mtype="tomo") img.md.setVoxelSize((1, 1, 1), "voxel") c = img.origin + img.shape * 0.5 r = np.min(img.shape) * 0.4 a = (1, 0, 0) mango.data.fill_circular_cylinder(img, c, r, img.shape[0] * 1.1, fill=32000, coordsys="abs", unit="voxel") metric = cylinder_fit_gradient_metric() metric.setGradientFromImage(img) x = metric.getDefaultX() rootLogger.info("Default x = %s" % (x, )) x[metric.RADIUS_IDX] = r x[metric.CENTRE_X_IDX] = c[2] x[metric.CENTRE_Y_IDX] = c[1] x[metric.CENTRE_Z_IDX] = c[0] x[metric.AXIS_X_IDX] = a[2] x[metric.AXIS_Y_IDX] = a[1] x[metric.AXIS_Z_IDX] = a[0] solnX = x.copy() bestRes = None for rr in range(-5, 6): x[metric.RADIUS_IDX] = r + rr res = sp.optimize.minimize( lambda x: -sp.absolute(metric(x)), x[0:6], method="Powell", options={ 'xtol': 1.0e-1, 'ftol': 1.0e-2 }) #res = sp.optimize.minimize(metric, x[0:6], jac=False, method="BFGS", options={'gtol':1.0e3, 'eps':(0.05,0.05,0.05,0.001,0.001,0.001)}) rootLogger.info( "result.success = %s, result.message = %s" % (res.success, res.message)) rootLogger.info( "optimized (metric_val, x) = (%16.2f, %s) = " % (res.fun, res.x)) rootLogger.info("res.nfev = %8d" % (res.nfev, )) if ((bestRes == None) or (res.fun < bestRes.fun)): bestRes = res bestRes.x[3:] /= sp.sqrt(sp.sum(bestRes.x[3:] * bestRes.x[3:])) rootLogger.info("soln x = %s" % (solnX[0:bestRes.x.size], )) rootLogger.info("best x = %s" % (bestRes.x, )) self.assertTrue( sp.all( sp.absolute(bestRes.x - solnX[0:bestRes.x.size]) < 0.1)) self.assertGreater(0, bestRes.fun)
def testCircularAnnularCylinderFillRotated(self): dir = self.createTmpDir("testCircularAnnularCylinderFillRotated") #dir = "." dds = mango.zeros(shape=self.shape, mtype="segmented") c = dds.shape*0.5 r = np.min(dds.shape-3)*0.35 w = np.min(dds.shape-3)*0.1 axislen = dds.shape[0]*2. rm = np.dot(mango.math.rotation_matrix(-10, 1), mango.math.rotation_matrix(20, 2)) mango.data.fill_annular_circular_cylinder(dds, c, r, w, axislen, fill=1, rotation=rm) mango.io.writeDds(os.path.join(dir, "segmentedAnnularCyl0_rotmtx.nc"), dds) (axis, angle) = mango.math.axis_angle_from_rotation_matrix(rm) rm = mango.math.axis_angle_to_rotation_matrix(axis, angle) dds = mango.zeros(shape=self.shape, mtype="segmented") mango.data.fill_annular_circular_cylinder(dds, c, r, w, axislen, fill=1, rotation=rm) mango.io.writeDds(os.path.join(dir, "segmentedAnnularCyl0_rotmtxaa.nc"), dds) dds1 = mango.zeros(shape=self.shape, mtype="segmented") mango.data.fill_annular_circular_cylinder(dds1, c, r, w, axislen, fill=1, rotation=(axis, angle)) mango.io.writeDds(os.path.join(dir, "segmentedAnnularCyl1_axang.nc"), dds1) self.assertTrue(sp.all(dds.subd.asarray() == dds1.subd.asarray())) del dds1 dds1 = mango.zeros(shape=self.shape, mtype="segmented") mango.data.fill_annular_circular_cylinder(dds1, c, r, w, axislen, fill=1, rotation=(axis.tolist(), angle)) mango.io.writeDds(os.path.join(dir, "segmentedAnnularCyl2_axlistang.nc"), dds1) self.assertTrue(sp.all(dds.subd.asarray() == dds1.subd.asarray())) del dds1 dds2 = mango.zeros(shape=self.shape, mtype="segmented") mango.data.fill_annular_circular_cylinder(dds2, c, r, w, axislen, fill=1, rotation=(angle, axis.tolist())) mango.io.writeDds(os.path.join(dir, "segmentedAnnularCyl3_angaxlist.nc"), dds2) self.assertTrue(sp.all(dds.subd.asarray() == dds2.subd.asarray())) del dds2 dds3 = mango.zeros(shape=self.shape, mtype="segmented") mango.data.fill_annular_circular_cylinder(dds3, c, r, w, axislen, fill=1, rotation=(angle, axis)) mango.io.writeDds(os.path.join(dir, "segmentedAnnularCyl4_angax.nc"), dds3) self.assertTrue(sp.all(dds.subd.asarray() == dds3.subd.asarray())) del dds3 dds4 = mango.zeros(shape=self.shape, mtype="segmented") mango.data.fill_annular_circular_cylinder(dds4, c, r, w, axislen, fill=1, rotation=angle*axis) mango.io.writeDds(os.path.join(dir, "segmentedAnnularCyl5_axis.nc"), dds4) self.assertTrue(sp.all(dds.subd.asarray() == dds4.subd.asarray())) del dds4 dds5 = mango.zeros(shape=self.shape, mtype="segmented") mango.data.fill_annular_circular_cylinder(dds5, c, r, w, axislen, fill=1, rotation=(angle*axis).tolist()) mango.io.writeDds(os.path.join(dir, "segmentedAnnularCyl4_axislist.nc"), dds5) self.assertTrue(sp.all(dds.subd.asarray() == dds5.subd.asarray())) del dds5
def _testNonDistributedMetricFit3(self): dir = self.createTmpDir( "CylinderFitTest_testNonDistributedMetricFit3") #dir = "." rootLogger.info("Generating synthetic image...") #img = mango.zeros((128+((mpi.size//8)*16),64,64), mtype="tomo") img = mango.zeros((256 + ((mpi.size // 8) * 16), 128, 128), mtype="tomo") #img = mango.zeros((1024,512,512), mtype="tomo") img.md.setVoxelSize((0.002, 0.002, 0.002), "mm") c = (img.origin + img.shape * 0.5) * img.md.getVoxelSize() r = np.min(img.shape * img.md.getVoxelSize()) * 0.4 a = sp.array((1., 0., 0.)) cList = [c, c, c + (img.shape * 0.02) * img.md.getVoxelSize()] rList = [r, 0.8 * r, 0.65 * r] aList = [a, a, sp.array([1.0, 0.01, 0.02])] fList = [20000, 15000, 18000] img.subd_h.asarray()[...] = 10000 for i in range(len(cList)): rMtx = mango.math.rotation_matrix_from_cross_prod( sp.array([1., 0., 0.]), aList[i]) mango.data.fill_circular_cylinder( img, cList[i], rList[i], img.shape[0] * 1.25, fill=fList[i], rotation=rMtx, coordsys="abs", unit=img.md.getVoxelSizeUnit()) img.subd.asarray()[...] += mango.data.gaussian_noise_like( img, mean=0, stdd=300).subd.asarray() mango.io.writeDds(os.path.join(dir, "tomoCyl3.nc"), img) rootLogger.info("Done generating synthetic image.") fit, cylImg = \ cylinder_fit( img, numcyl=3, retcylimg=True, distributedMetricEvaluation=False, metricThreshRelTol=0.5 ) mango.io.writeDds(os.path.join(dir, "segmentedCyl3AnnularMsk.nc"), cylImg) rootLogger.info("Voxel size = %s %s" % (img.md.getVoxelSize(), img.md.getVoxelSizeUnit())) for i in range(len(cList)): rootLogger.info("c%s=%s, r%s=%s, a%s=%s" % (i, cList[i], i, rList[i], i, aList[i])) rootLogger.info("Fit Results:\n%s" % ("\n".join(map(str, fit)), ))
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 testDdsSubdProperty(self): """ Test :prop:`Dds.subd` object. """ dds = mango.zeros(shape=self.shape, dtype="uint16", mpidims=(0, 1, 1)) self.assertEqual(0, dds[0, 0, 0]) rootLogger.info("dds.shape=%s, dds.mpi.shape=%s" % (dds.shape, dds.mpi.shape)) logger.info("dds.mpi.rank=%s, dds.mpi.index=%s" % (dds.mpi.rank, dds.mpi.index)) logger.info("dds.subd.origin=%s, dds.subd.shape=%s" % (dds.subd.origin, dds.subd.shape)) dds = mango.zeros(shape=self.shape, dtype="uint16", mpidims=(0, 0, 0)) self.assertEqual(0, dds[0, 0, 0]) rootLogger.info("dds.shape=%s, dds.mpi.shape=%s" % (dds.shape, dds.mpi.shape)) logger.info("dds.mpi.rank=%s, dds.mpi.index=%s" % (dds.mpi.rank, dds.mpi.index)) logger.info("dds.subd.origin=%s, dds.subd.shape=%s" % (dds.subd.origin, dds.subd.shape))
def testDdsClassAttr(self): dds = mango.zeros(shape=self.shape) rootLogger.info(str(dir(dds))) self.assertTrue(hasattr(dds.__class__, "dtype")) self.assertTrue(hasattr(dds.__class__, "__getitem__")) self.assertTrue(hasattr(dds.__class__, "__setitem__")) self.assertTrue(hasattr(dds.__class__, "fill")) self.assertTrue(hasattr(dds, "mtype")) self.assertTrue(hasattr(dds, "shape")) self.assertTrue(hasattr(dds, "origin")) self.assertTrue(hasattr(dds, "halo")) self.assertTrue(hasattr(dds, "subd")) self.assertTrue(hasattr(dds, "mpi")) self.assertTrue(hasattr(dds, "md"))
def testDdsCommunicator(self): """ Test :prop:`Dds.mpi.comm` object. """ dds = mango.zeros(shape=self.shape, dtype="uint16", mpidims=(0, 1, 1)) self.assertEqual(0, dds[0, 0, 0]) comm = dds.mpi.comm if (mango.mpi.haveMpi4py): self.assertNotEqual(None, comm) rootLogger.info("dds.mpi.comm=%s" % (dds.mpi.comm, )) self.assertEqual(mango.mpi.world.size, dds.mpi.comm.size) self.assertEqual(mango.mpi.world.rank, dds.mpi.comm.rank) else: self.assertEqual(None, comm)
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 testDdsMirrorOuterLayersToBorder(self): """ Test the :meth:`Dds.mirrorOuterLayersToBorder` method. """ dds = mango.zeros(shape=self.shape, dtype="uint16", mpidims=(0, 1, 1), halo=2) dds.setBorderToValue(1) self.assertTrue(sp.sum(sp.where(dds.asarray() != 0, 1, 0)) > 0) dds.mirrorOuterLayersToBorder(False) logger.info("Num non-zero = %s" % sp.sum(sp.where(dds.asarray() != 0, 1, 0))) logger.info("Idx non-zero = %s" % (sp.where(dds.asarray() != 0), )) self.assertTrue(sp.all(dds.asarray() == 0))
def testDistributedOptimization(self): if ((mpi.size % 4) == 0): mpidims = (0,2,2) elif ((mpi.size % 2) == 0): mpidims = (0,0,2) else: mpidims = (0,0,0) img = mango.zeros((120+8*mpi.size,64,64), mtype="tomo", mpidims=mpidims) img.md.setVoxelSize((1,1,1), "voxel") c = img.origin + img.shape*0.5 r = np.min(img.shape)*0.4 a = (1,0,0) mango.data.fill_circular_cylinder(img, c, r, img.shape[0]*1.1, fill=32000, coordsys="abs", unit="voxel") metric = cylinder_fit_gradient_metric() metric = DistributedGradCylFitMetric(metric, comm=img.mpi.comm, root=0) metric.setGradientFromImage(img) x = metric.getDefaultX() rootLogger.info("Default x = %s" % (x,)) x[metric.RADIUS_IDX] = r x[metric.CENTRE_X_IDX] = c[2] x[metric.CENTRE_Y_IDX] = c[1] x[metric.CENTRE_Z_IDX] = c[0] x[metric.AXIS_X_IDX] = a[2] x[metric.AXIS_Y_IDX] = a[1] x[metric.AXIS_Z_IDX] = a[0] solnX = x.copy() bestRes = None for rr in range(-5, 6): x[metric.RADIUS_IDX] = r + rr res = mango.optimize.distributed_minimize(metric, x[0:5], method="Powell", options={'xtol':1.0e-1, 'ftol':1.0e-2}) #res = sp.optimize.minimize(metric, x[0:6], jac=False, method="BFGS", options={'gtol':1.0e3, 'eps':(0.05,0.05,0.05,0.001,0.001,0.001)}) rootLogger.info("result.success = %s, result.message = %s" % (res.success, res.message)) rootLogger.info("optimized (metric_val, x) = (%16.2f, %s) = " % (res.fun, res.x)) rootLogger.info("res.nfev = %8d" % (res.nfev,)) if ((bestRes == None) or (res.fun < bestRes.fun)): bestRes = res #bestRes.x[3:] /= sp.sqrt(sp.sum(bestRes.x[3:]*bestRes.x[3:])) rootLogger.info("soln x = %s" % (solnX[0:bestRes.x.size],)) rootLogger.info("best x = %s" % (bestRes.x,)) self.assertTrue(sp.all(sp.absolute(bestRes.x - solnX[0:bestRes.x.size]) < 0.1)) self.assertGreater(0, bestRes.fun)
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 testMultiResFit1(self): dir = self.createTmpDir("CylinderFitMultiResTest_testMultiResFit1") #dir = "." rootLogger.info("Generating synthetic image...") #img = mango.zeros((128+((mpi.size//8)*16),64,64), mtype="tomo") img = mango.zeros((512 + ((mpi.size // 8) * 16), 256, 256), mtype="tomo") #img = mango.zeros((1024,512,512), mtype="tomo") img.md.setVoxelSize((0.002, 0.002, 0.002), "mm") c = (img.origin + img.shape * 0.5) * img.md.getVoxelSize() r = np.min(img.shape * img.md.getVoxelSize()) * 0.4 a = (1, 0, 0) img.subd_h.asarray()[...] = 10000 mango.data.fill_circular_cylinder(img, c, r, img.shape[0] * 1.1, fill=25000, coordsys="abs", unit=img.md.getVoxelSizeUnit()) img.subd.asarray()[...] += mango.data.gaussian_noise_like( img, mean=0, stdd=300).subd.asarray() mango.io.writeDds(os.path.join(dir, "tomoCyl1.nc"), img) rootLogger.info("Done generating synthetic image.") fit, cylImg = cylinder_fit_multi_res(img, 1, resolutions=[64, 128, 512], retcylimg=True, metricThreshRelTol=0.50) mango.io.writeDds(os.path.join(dir, "segmentedCyl1AnnularMsk.nc"), cylImg) rootLogger.info("Img voxel size = %s %s" % (img.md.getVoxelSize(), img.md.getVoxelSizeUnit())) rootLogger.info( "Cyl img voxel size = %s %s" % (cylImg.md.getVoxelSize(), cylImg.md.getVoxelSizeUnit())) rootLogger.info("c=%s, r=%s, a=%s" % (c, r, a)) rootLogger.info("Fit Results:\n%s" % ("\n".join(map(str, fit)), )) f0 = fit[0] self.assertAlmostEqual(c[1], f0[1][0][1], 3) self.assertAlmostEqual(c[2], f0[1][0][2], 3) self.assertAlmostEqual(r, f0[1][1], 3) self.assertAlmostEqual(sp.absolute(sp.dot(a, f0[1][3])), 1.0, 3)
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 testDdsMetaData(self): dds = mango.zeros(shape=self.shape, dtype="uint16") self.assertNotEqual(None, dds.md) rootLogger.info( "dds.md.getVoxelSize()=%s, dds.md.getVoxelSizeUnit()=%s" % (dds.md.getVoxelSize(), dds.md.getVoxelSizeUnit())) vSz = (0.5, 0.125, 0.25) dds.md.setVoxelSize(vSz) dds.md.setVoxelSizeUnit("um") rootLogger.info( "dds.md.getVoxelSize()=%s, dds.md.getVoxelSizeUnit()=%s" % (dds.md.getVoxelSize(), dds.md.getVoxelSizeUnit())) self.assertEqual("micrometre", dds.md.getVoxelSizeUnit()) self.assertTrue(sp.all(dds.md.getVoxelSize() - vSz < 1.0e-6)) dds.md.setVoxelSize(1) dds.md.setVoxelSizeUnit("millimetre") rootLogger.info( "dds.md.getVoxelSize()=%s, dds.md.getVoxelSizeUnit()=%s" % (dds.md.getVoxelSize(), dds.md.getVoxelSizeUnit())) self.assertEqual("millimetre", dds.md.getVoxelSizeUnit()) self.assertTrue(sp.all(dds.md.getVoxelSize() == 1))
def testDdsAsArrayMemoryDealloc(self): """ Alloc and dealloc :obj:`Dds` objects and check memory consumption. """ for i in range(0, 16): rootLogger.info("Creating Dds...") dds = mango.zeros(shape=(256, 512, 512), dtype="uint16", mpidims=(0, 0, 0)) rootLogger.info("Done creating Dds.") self.assertEqual(0, dds[0, 0, 0]) a = dds.asarray() rootLogger.info("Deleting Dds...") del dds rootLogger.info("Done deleting Dds.") a += 10 self.assertEqual(10, a[0, 0, 0]) a -= 10 self.assertEqual(0, a[0, 0, 0])
def _testMultiResFit3(self): dir = self.createTmpDir("CylinderFitMultiResTest_testMultiResFit3") #dir = "." rootLogger.info("Generating synthetic image...") #img = mango.zeros((128+((world.size//8)*16),64,64), mtype="tomo") img = mango.zeros((256+((world.size//8)*16),128,128), mtype="tomo") #img = mango.zeros((1024,512,512), mtype="tomo") img.md.setVoxelSize((0.002,0.002,0.002), "mm") c = (img.origin + img.shape*0.5)*img.md.getVoxelSize() r = np.min(img.shape*img.md.getVoxelSize())*0.4 a = sp.array((1.,0.,0.)) cList = [c, c, c+(img.shape*0.02)*img.md.getVoxelSize()] rList = [r, 0.8*r, 0.65*r] aList = [a, a, sp.array([1.0, 0.01,0.02])] fList = [20000, 15000, 18000] img.subd_h.asarray()[...] = 10000 for i in range(len(cList)): rMtx = mango.math.rotation_matrix_from_cross_prod(sp.array([1.,0.,0.]), aList[i]) mango.data.fill_circular_cylinder(img, cList[i], rList[i], img.shape[0]*1.25, fill=fList[i], rotation=rMtx, coordsys="abs", unit=img.md.getVoxelSizeUnit()) img.subd.asarray()[...] += mango.data.gaussian_noise_like(img, mean=0, stdd=300).subd.asarray() mango.io.writeDds(os.path.join(dir, "tomoCyl3.nc"), img) rootLogger.info("Done generating synthetic image.") fit, cylImg = \ cylinder_fit_multi_res( img, 3, resolutions=[64, 128, 512], retcylimg=True, metricThreshRelTol=0.50 ) mango.io.writeDds(os.path.join(dir,"segmentedCyl3AnnularMsk.nc"), cylImg) rootLogger.info("Voxel size = %s %s" % (img.md.getVoxelSize(), img.md.getVoxelSizeUnit())) for i in range(len(cList)): rootLogger.info("c%s=%s, r%s=%s, a%s=%s" % (i,cList[i],i,rList[i],i,aList[i])) rootLogger.info("Fit Results:\n%s" % ("\n".join(map(str,fit)),))
def createTmpArrayFromDds(self): dds = mango.zeros(shape=self.shape, dtype="uint16", mpidims=(0, 1, 1)) a = dds.asarray() del dds return a
def testDdsSetGlobalFaceValue(self): """ Test the :meth:`mango.Dds.setFacesToValue` method. """ outDir = self.createTmpDir("testDdsSetGlobalFaceValue") ddsorig = mango.zeros(shape=self.shape, mtype="tomo", origin=(-8, 5, -32)) for depth in (1, int(np.min(ddsorig.shape) // 2)): for axis in (0, 1, 2): dds = mango.copy(ddsorig) vallo = 16 + axis rootLogger.info("LO: axis=%s, val=%s, depth=%s" % (axis, vallo, depth)) dds.setFacesToValue(vallo, axislo=axis, depth=depth) #mango.io.writeDds(os.path.join(outDir, "tomoLoFaceAxis%s.nc" % axis), dds) shpFace = dds.shape shpFace[axis] = depth mpidimsFace = [0, 0, 0] mpidimsFace[axis] = 1 rootLogger.info("LO: shpFace=%s, mpidimsFace=%s" % (shpFace, mpidimsFace)) ddsFace = mango.copy(dds, shape=shpFace, mpidims=mpidimsFace) self.assertTrue(sp.all(ddsFace.subd.asarray() == vallo)) shpNotFace = dds.shape shpNotFace[axis] = dds.shape[axis] - depth orgNotFace = dds.origin orgNotFace[axis] = dds.origin[axis] + depth mpidimsNotFace = [0, 0, 0] mpidimsNotFace[axis] = 1 rootLogger.info( "LO: shpNotFace=%s, mpidimsNotFace=%s, orgNotFace=%s" % (shpNotFace, mpidimsNotFace, orgNotFace)) ddsNotFace = mango.copy(dds, origin=orgNotFace, shape=shpNotFace, mpidims=mpidimsNotFace) self.assertTrue(sp.all(ddsNotFace.subd.asarray() == 0)) dds = mango.copy(ddsorig) valhi = 2 * vallo rootLogger.info("HI: axis=%s, val=%s, depth=%s" % (axis, valhi, depth)) dds.setFacesToValue(valhi, axishi=axis, depth=depth) #mango.io.writeDds(os.path.join(outDir, "tomoLoFaceAxis%s.nc" % axis), dds) shpFace = dds.shape shpFace[axis] = depth mpidimsFace = [0, 0, 0] mpidimsFace[axis] = 1 orgFace = dds.origin orgFace[axis] = dds.origin[axis] + dds.shape[axis] - depth rootLogger.info("HI: shpFace=%s, mpidimsFace=%s, orgFace=%s" % (shpFace, mpidimsFace, orgFace)) ddsFace = mango.copy(dds, origin=orgFace, shape=shpFace, mpidims=mpidimsFace) self.assertTrue(sp.all(ddsFace.subd.asarray() == valhi)) shpNotFace = dds.shape shpNotFace[axis] = dds.shape[axis] - depth mpidimsNotFace = [0, 0, 0] mpidimsNotFace[axis] = 1 rootLogger.info( "HI: shpNotFace=%s, mpidimsNotFace=%s, orgNotFace=%s" % (shpNotFace, mpidimsNotFace, orgNotFace)) ddsNotFace = mango.copy(dds, shape=shpNotFace, mpidims=mpidimsNotFace) self.assertTrue(sp.all(ddsNotFace.subd.asarray() == 0))