Example #1
0
    def testDdsCreateWithSubdInfo(self):
        """
        Test the :func:`mango.empty` using the :samp:`subdorigin`
        and :samp:`subdshape` arguments.
        """

        # Create initial fixture.
        ddsOrig = \
            mango.empty(
                shape=(self.shape[0]+3, self.shape[1]+2, self.shape[0]+1),
                origin=(-32,-64,-128),
                dtype="uint16"
            )
        np.random.seed((mango.mpi.rank + 1) * 975421)
        ddsOrig.asarray()[:] = \
            np.random.randint(
                np.iinfo(ddsOrig.dtype).min,
                np.iinfo(ddsOrig.dtype).max,
                ddsOrig.asarray().shape
            )

        subdZSz = ddsOrig.shape[0] // mango.mpi.size
        mySubdZSz = subdZSz
        if (mango.mpi.rank == mango.mpi.size - 1):
            mySubdZSz += (ddsOrig.shape[0] % mango.mpi.size)

        mpidims = sp.array((mango.mpi.size, 1, 1))
        subdshape = sp.array((mySubdZSz, ddsOrig.shape[1], ddsOrig.shape[2]))
        subdorigin = ddsOrig.origin + sp.array(
            (subdZSz, subdshape[1], subdshape[2])) * (mango.mpi.rank, 0, 0)
        ddsSubd =\
            mango.empty(
                shape       = ddsOrig.shape,
                origin      = ddsOrig.origin,
                dtype       = ddsOrig.dtype,
                subdshape   = subdshape,
                subdorigin  = subdorigin,
                mpidims     = mpidims
            )
        self.assertTrue(sp.all(ddsSubd.origin == ddsOrig.origin))
        self.assertTrue(sp.all(ddsSubd.shape == ddsOrig.shape))
        self.assertTrue(sp.all(ddsSubd.subd.origin == subdorigin))
        self.assertTrue(sp.all(ddsSubd.subd.shape == subdshape))

        if (mpi.haveMpi4py):
            self.assertEqual(mango.mpi.world.allreduce(ddsOrig.asarray().size),
                             mango.mpi.world.allreduce(ddsSubd.asarray().size))
        else:
            self.assertEqual(ddsOrig.asarray().size, ddsSubd.asarray().size)

        logger.info("ddsOrig.origin=%s, ddsOrig.subd.origin=%s" %
                    (ddsOrig.origin, ddsOrig.subd.origin))
        logger.info("ddsSubd.origin=%s, ddsSubd.subd.origin=%s" %
                    (ddsSubd.origin, ddsSubd.subd.origin))
        ddsSubd.fill(ddsOrig)
        ddsCmp = mango.zeros_like(ddsOrig)
        ddsCmp.fill(ddsSubd)
        self.assertTrue(sp.all(ddsOrig.asarray() == ddsCmp.asarray()))
Example #2
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)])
Example #3
0
def createCheckerDds(shape, checkShape, checkOrigin=(0,0,0), black=0, white=1, dtype="uint8", mtype=None, halo=(0,0,0), mpidims=(0,0,0), origin=(0,0,0)):
    """
    Creates a 3D checker-board image.
    
    :type shape: 3-sequence
    :param shape: The global shape :samp:`(zSz,ySz,xSz)` of the returned :obj:`mango.Dds` image.
    :type checkShape: 3-sequence
    :param checkShape: The shape of the checks.
    :type checkOrigin: 3-sequence
    :param checkOrigin: The origin (relative to the :samp:`origin` parameter) of where the checks
       begin to be stacked.
    :type black: value
    :param black: The value for the *black* checks.
    :type white: value
    :param white: The value for the *white* checks.
    :type dtype: :obj:`numpy.dtype`
    :param dtype: The element type of the returned array.
    :type mtype: :obj:`mango.mtype`
    :param mtype: The mango data type for the checker-board image.
    :type halo: 3-sequence
    :param halo: The halo size for the returned :obj:`mango.Dds` object.
    :type mpidims: 3-sequence
    :param mpidims: Cartesian MPI domain decomposition.
    :type origin: 3-sequence
    :param origin: The global origin index for the returned :obj:`mango.Dds` object.
    :rtype: :obj:`mango.Dds`
    :return: Checker-board :obj:`mango.Dds`

    """
    
    while (len(shape) <= 2):
        shape = (1,) + tuple(shape)

    cbDds = mango.empty(shape, dtype=dtype, mtype=mtype, halo=halo, mpidims=mpidims, origin=origin)
    checkOrigin = sp.array(checkOrigin) + cbDds.origin
    cbArr = cbDds.subd.asarray()
    
    sbeg = sp.array(cbDds.subd.origin)
    send = sbeg + cbDds.subd.shape
    
    coords = np.ogrid[sbeg[0]:send[0],sbeg[1]:send[1],sbeg[2]:send[2]]

    vals = np.array([black,white],dtype=dtype)
    cbArr[...] = \
        vals[
            (
                (coords[0]-checkOrigin[0])//checkShape[0]
                +
                (coords[1]-checkOrigin[1])//checkShape[1]
                +
                (coords[2]-checkOrigin[2])//checkShape[2]
            )
            %
            2
        ]

    return cbDds
Example #4
0
def gaussian_noise(shape,
                   mean=0.0,
                   stdd=1.0,
                   dtype=None,
                   mtype=None,
                   halo=(0, 0, 0),
                   mpidims=(0, 0, 0),
                   origin=(0, 0, 0),
                   subdshape=None,
                   subdorigin=None):
    """
    Generates image of Gaussian (Normal) distributed noise.
    
    :type shape: 3-sequence
    :param shape: The global shape :samp:`(zSz,ySz,xSz)` of the returned :obj:`mango.Dds` image.
    :type mean: float
    :param mean: The mean parameter of the normally distributed noise.
    :type stdd: float
    :param stdd: The standard-deviation parameter of the normally distributed noise.
    :type dtype: :obj:`numpy.dtype`
    :param dtype: The element type of the returned array.
    :type mtype: :obj:`mango.mtype`
    :param mtype: The mango data type for the return noise image.
    :type halo: 3-sequence
    :param halo: The halo size for the returned :obj:`mango.Dds` object.
    :type mpidims: 3-sequence
    :param mpidims: Cartesian MPI domain decomposition.
    :type origin: 3-sequence
    :param origin: The global origin index for the returned :obj:`mango.Dds` object.
    :type subdshape: 3-sequence
    :param subdshape: Explicitly specify the sub-domain shape for the current MPI process.
    :type subdorigin: 3-sequence
    :param subdorigin: Explicitly specify the sub-domain origin for the current MPI process.

    :rtype: :obj:`mango.Dds`
    :return: Noise :obj:`mango.Dds` image.

    """
    dds = \
        mango.empty(
            shape=shape,
            dtype=dtype,
            mtype=mtype,
            halo=halo,
            mpidims=mpidims,
            origin=origin,
            subdshape=subdshape,
            subdorigin=subdorigin
        )
    dds.subd.asarray()[...] = np.random.normal(loc=mean,
                                               scale=stdd,
                                               size=dds.subd.asarray().shape)

    return dds
Example #5
0
def gaussian_noise(
    shape,
    mean=0.0,
    stdd=1.0,
    dtype=None,
    mtype=None,
    halo=(0,0,0),
    mpidims=(0,0,0),
    origin=(0,0,0),
    subdshape=None,
    subdorigin=None
):
    """
    Generates image of Gaussian (Normal) distributed noise.
    
    :type shape: 3-sequence
    :param shape: The global shape :samp:`(zSz,ySz,xSz)` of the returned :obj:`mango.Dds` image.
    :type mean: float
    :param mean: The mean parameter of the normally distributed noise.
    :type stdd: float
    :param stdd: The standard-deviation parameter of the normally distributed noise.
    :type dtype: :obj:`numpy.dtype`
    :param dtype: The element type of the returned array.
    :type mtype: :obj:`mango.mtype`
    :param mtype: The mango data type for the return noise image.
    :type halo: 3-sequence
    :param halo: The halo size for the returned :obj:`mango.Dds` object.
    :type mpidims: 3-sequence
    :param mpidims: Cartesian MPI domain decomposition.
    :type origin: 3-sequence
    :param origin: The global origin index for the returned :obj:`mango.Dds` object.
    :type subdshape: 3-sequence
    :param subdshape: Explicitly specify the sub-domain shape for the current MPI process.
    :type subdorigin: 3-sequence
    :param subdorigin: Explicitly specify the sub-domain origin for the current MPI process.

    :rtype: :obj:`mango.Dds`
    :return: Noise :obj:`mango.Dds` image.

    """
    dds = \
        mango.empty(
            shape=shape,
            dtype=dtype,
            mtype=mtype,
            halo=halo,
            mpidims=mpidims,
            origin=origin,
            subdshape=subdshape,
            subdorigin=subdorigin
        )
    dds.subd.asarray()[...] = np.random.normal(loc=mean, scale=stdd, size=dds.subd.asarray().shape)
    
    return dds;
Example #6
0
def chi_squared_noise(
    shape,
    dof=1.0,
    dtype=None,
    mtype=None,
    halo=(0,0,0),
    mpidims=(0,0,0),
    origin=(0,0,0),
    subdshape=None,
    subdorigin=None
):
    """
    Generates image of Chi-Squared distributed noise.
    
    :type shape: 3-sequence
    :param shape: The global shape :samp:`(zSz,ySz,xSz)` of the returned :obj:`mango.Dds` image.
    :type dof: float
    :param dof: The degrees-of-freedom parameter of the central-Chi-Squared distributed noise.
    :type dtype: :obj:`numpy.dtype`
    :param dtype: The element type of the returned array.
    :type mtype: :obj:`mango.mtype`
    :param mtype: The mango data type for the return noise image.
    :type halo: 3-sequence
    :param halo: The halo size for the returned :obj:`mango.Dds` object.
    :type mpidims: 3-sequence
    :param mpidims: Cartesian MPI domain decomposition.
    :type origin: 3-sequence
    :param origin: The global origin index for the returned :obj:`mango.Dds` object.
    :type subdshape: 3-sequence
    :param subdshape: Explicitly specify the sub-domain shape for the current MPI process.
    :type subdorigin: 3-sequence
    :param subdorigin: Explicitly specify the sub-domain origin for the current MPI process.

    :rtype: :obj:`mango.Dds`
    :return: Noise :obj:`mango.Dds` image.


    """
    dds = \
        mango.empty(
            shape=shape,
            dtype=dtype,
            mtype=mtype,
            halo=halo,
            mpidims=mpidims,
            origin=origin,
            subdshape=subdshape,
            subdorigin=subdorigin
        )
    dds.subd.asarray()[...] = np.random.chisquare(df=dof, size=dds.subd.asarray().shape)
    
    return dds;
Example #7
0
    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)])
Example #8
0
def chi_squared_noise(shape,
                      dof=1.0,
                      dtype=None,
                      mtype=None,
                      halo=(0, 0, 0),
                      mpidims=(0, 0, 0),
                      origin=(0, 0, 0),
                      subdshape=None,
                      subdorigin=None):
    """
    Generates image of Chi-Squared distributed noise.
    
    :type shape: 3-sequence
    :param shape: The global shape :samp:`(zSz,ySz,xSz)` of the returned :obj:`mango.Dds` image.
    :type dof: float
    :param dof: The degrees-of-freedom parameter of the central-Chi-Squared distributed noise.
    :type dtype: :obj:`numpy.dtype`
    :param dtype: The element type of the returned array.
    :type mtype: :obj:`mango.mtype`
    :param mtype: The mango data type for the return noise image.
    :type halo: 3-sequence
    :param halo: The halo size for the returned :obj:`mango.Dds` object.
    :type mpidims: 3-sequence
    :param mpidims: Cartesian MPI domain decomposition.
    :type origin: 3-sequence
    :param origin: The global origin index for the returned :obj:`mango.Dds` object.
    :type subdshape: 3-sequence
    :param subdshape: Explicitly specify the sub-domain shape for the current MPI process.
    :type subdorigin: 3-sequence
    :param subdorigin: Explicitly specify the sub-domain origin for the current MPI process.

    :rtype: :obj:`mango.Dds`
    :return: Noise :obj:`mango.Dds` image.


    """
    dds = \
        mango.empty(
            shape=shape,
            dtype=dtype,
            mtype=mtype,
            halo=halo,
            mpidims=mpidims,
            origin=origin,
            subdshape=subdshape,
            subdorigin=subdorigin
        )
    dds.subd.asarray()[...] = np.random.chisquare(
        df=dof, size=dds.subd.asarray().shape)

    return dds
Example #9
0
def createCheckerDds(shape,
                     checkShape,
                     checkOrigin=(0, 0, 0),
                     black=0,
                     white=1,
                     dtype="uint8",
                     mtype=None,
                     halo=(0, 0, 0),
                     mpidims=(0, 0, 0),
                     origin=(0, 0, 0)):
    """
    Creates a 3D checker-board image.
    
    :type shape: 3-sequence
    :param shape: The global shape :samp:`(zSz,ySz,xSz)` of the returned :obj:`mango.Dds` image.
    :type checkShape: 3-sequence
    :param checkShape: The shape of the checks.
    :type checkOrigin: 3-sequence
    :param checkOrigin: The origin (relative to the :samp:`origin` parameter) of where the checks
       begin to be stacked.
    :type black: value
    :param black: The value for the *black* checks.
    :type white: value
    :param white: The value for the *white* checks.
    :type dtype: :obj:`numpy.dtype`
    :param dtype: The element type of the returned array.
    :type mtype: :obj:`mango.mtype`
    :param mtype: The mango data type for the checker-board image.
    :type halo: 3-sequence
    :param halo: The halo size for the returned :obj:`mango.Dds` object.
    :type mpidims: 3-sequence
    :param mpidims: Cartesian MPI domain decomposition.
    :type origin: 3-sequence
    :param origin: The global origin index for the returned :obj:`mango.Dds` object.
    :rtype: :obj:`mango.Dds`
    :return: Checker-board :obj:`mango.Dds`

    """

    while (len(shape) <= 2):
        shape = (1, ) + tuple(shape)

    cbDds = mango.empty(shape,
                        dtype=dtype,
                        mtype=mtype,
                        halo=halo,
                        mpidims=mpidims,
                        origin=origin)
    checkOrigin = sp.array(checkOrigin) + cbDds.origin
    cbArr = cbDds.subd.asarray()

    sbeg = sp.array(cbDds.subd.origin)
    send = sbeg + cbDds.subd.shape

    coords = np.ogrid[sbeg[0]:send[0], sbeg[1]:send[1], sbeg[2]:send[2]]

    vals = np.array([black, white], dtype=dtype)
    cbArr[...] = \
        vals[
            (
                (coords[0]-checkOrigin[0])//checkShape[0]
                +
                (coords[1]-checkOrigin[1])//checkShape[1]
                +
                (coords[2]-checkOrigin[2])//checkShape[2]
            )
            %
            2
        ]

    return cbDds