Ejemplo n.º 1
0
    def test_ndgrid_2D(self):
        XY = ndgrid([self.a, self.b])

        X1_test = np.array([1, 2, 3, 1, 2, 3])
        X2_test = np.array([1, 1, 1, 2, 2, 2])

        self.assertTrue(np.all(XY[:, 0] == X1_test))
        self.assertTrue(np.all(XY[:, 1] == X2_test))
Ejemplo n.º 2
0
    def test_ndgrid_2D(self):
        XY = ndgrid([self.a, self.b])

        X1_test = np.array([1, 2, 3, 1, 2, 3])
        X2_test = np.array([1, 1, 1, 2, 2, 2])

        self.assertTrue(np.all(XY[:, 0] == X1_test))
        self.assertTrue(np.all(XY[:, 1] == X2_test))
Ejemplo n.º 3
0
    def test_ndgrid_3D(self):
        XYZ = ndgrid([self.a, self.b, self.c])

        X1_test = np.array([1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3])
        X2_test = np.array([1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2])
        X3_test = np.array([1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4])

        self.assertTrue(np.all(XYZ[:, 0] == X1_test))
        self.assertTrue(np.all(XYZ[:, 1] == X2_test))
        self.assertTrue(np.all(XYZ[:, 2] == X3_test))
Ejemplo n.º 4
0
 def test_indexCube_3D(self):
     nN = np.array([3, 3, 3])
     self.assertTrue(
         np.all(
             indexCube('A', nN) == np.array([0, 1, 3, 4, 9, 10, 12, 13])))
     self.assertTrue(
         np.all(
             indexCube('B', nN) == np.array([3, 4, 6, 7, 12, 13, 15, 16])))
     self.assertTrue(
         np.all(
             indexCube('C', nN) == np.array([4, 5, 7, 8, 13, 14, 16, 17])))
     self.assertTrue(
         np.all(
             indexCube('D', nN) == np.array([1, 2, 4, 5, 10, 11, 13, 14])))
     self.assertTrue(
         np.all(
             indexCube('E', nN) == np.array([9, 10, 12, 13, 18, 19, 21, 22
                                             ])))
     self.assertTrue(
         np.all(
             indexCube('F', nN) == np.array(
                 [12, 13, 15, 16, 21, 22, 24, 25])))
     self.assertTrue(
         np.all(
             indexCube('G', nN) == np.array(
                 [13, 14, 16, 17, 22, 23, 25, 26])))
     self.assertTrue(
         np.all(
             indexCube('H', nN) == np.array(
                 [10, 11, 13, 14, 19, 20, 22, 23])))
Ejemplo n.º 5
0
 def test_indexCube_3D(self):
     nN = np.array([3, 3, 3])
     self.assertTrue(np.all(indexCube('A', nN) == np.array([0, 1, 3, 4, 9, 10, 12, 13])))
     self.assertTrue(np.all(indexCube('B', nN) == np.array([3, 4, 6, 7, 12, 13, 15, 16])))
     self.assertTrue(np.all(indexCube('C', nN) == np.array([4, 5, 7, 8, 13, 14, 16, 17])))
     self.assertTrue(np.all(indexCube('D', nN) == np.array([1, 2, 4, 5, 10, 11, 13, 14])))
     self.assertTrue(np.all(indexCube('E', nN) == np.array([9, 10, 12, 13, 18, 19, 21, 22])))
     self.assertTrue(np.all(indexCube('F', nN) == np.array([12, 13, 15, 16, 21, 22, 24, 25])))
     self.assertTrue(np.all(indexCube('G', nN) == np.array([13, 14, 16, 17, 22, 23, 25, 26])))
     self.assertTrue(np.all(indexCube('H', nN) == np.array([10, 11, 13, 14, 19, 20, 22, 23])))
Ejemplo n.º 6
0
    def test_ndgrid_3D(self):
        XYZ = ndgrid([self.a, self.b, self.c])

        X1_test = np.array([
            1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1,
            2, 3
        ])
        X2_test = np.array([
            1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2,
            2, 2
        ])
        X3_test = np.array([
            1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
            4, 4
        ])

        self.assertTrue(np.all(XYZ[:, 0] == X1_test))
        self.assertTrue(np.all(XYZ[:, 1] == X2_test))
        self.assertTrue(np.all(XYZ[:, 2] == X3_test))
Ejemplo n.º 7
0
 def _warn_non_axis_aligned_sources(self, change):
     value = change['value']
     axaligned = [
         True for vec in
         [np.r_[1., 0., 0.], np.r_[0., 1., 0.], np.r_[0., 0., 1.]]
         if np.all(value == vec)
     ]
     if len(axaligned) != 1:
         warnings.warn('non-axes aligned orientations {} are not rigorously'
                       ' tested'.format(value))
Ejemplo n.º 8
0
    def test_asArray_N_x_Dim(self):

        true = np.array([[1, 2, 3]])

        listArray = asArray_N_x_Dim([1, 2, 3], 3)
        self.assertTrue(np.all(true == listArray))
        self.assertTrue(true.shape == listArray.shape)

        listArray = asArray_N_x_Dim(np.r_[1, 2, 3], 3)
        self.assertTrue(np.all(true == listArray))
        self.assertTrue(true.shape == listArray.shape)

        listArray = asArray_N_x_Dim(np.array([[1, 2, 3.]]), 3)
        self.assertTrue(np.all(true == listArray))
        self.assertTrue(true.shape == listArray.shape)

        true = np.array([[1, 2], [4, 5]])

        listArray = asArray_N_x_Dim([[1, 2], [4, 5]], 2)
        self.assertTrue(np.all(true == listArray))
        self.assertTrue(true.shape == listArray.shape)
Ejemplo n.º 9
0
    def test_asArray_N_x_Dim(self):

        true = np.array([[1,2,3]])

        listArray = asArray_N_x_Dim([1,2,3],3)
        self.assertTrue(np.all(true == listArray))
        self.assertTrue(true.shape == listArray.shape)

        listArray = asArray_N_x_Dim(np.r_[1,2,3],3)
        self.assertTrue(np.all(true == listArray))
        self.assertTrue(true.shape == listArray.shape)

        listArray = asArray_N_x_Dim(np.array([[1,2,3.]]),3)
        self.assertTrue(np.all(true == listArray))
        self.assertTrue(true.shape == listArray.shape)

        true = np.array([[1,2],[4,5]])

        listArray = asArray_N_x_Dim([[1,2],[4,5]],2)
        self.assertTrue(np.all(true == listArray))
        self.assertTrue(true.shape == listArray.shape)
Ejemplo n.º 10
0
 def test_sub2ind(self):
     x = np.ones((5,2))
     self.assertTrue(np.all(sub2ind(x.shape, [0,0]) == [0]))
     self.assertTrue(np.all(sub2ind(x.shape, [4,0]) == [4]))
     self.assertTrue(np.all(sub2ind(x.shape, [0,1]) == [5]))
     self.assertTrue(np.all(sub2ind(x.shape, [4,1]) == [9]))
     self.assertTrue(np.all(sub2ind(x.shape, [[4,1]]) == [9]))
     self.assertTrue(np.all(sub2ind(x.shape, [[0,0],[4,0],[0,1],[4,1]]) == [0,4,5,9]))
Ejemplo n.º 11
0
    def getInterpolationMat(self, loc, locType, zerosOutside=False):
        """ Produces interpolation matrix

        :param numpy.ndarray loc: Location of points to interpolate to
        :param str locType: What to interpolate (see below)
        :rtype: scipy.sparse.csr.csr_matrix
        :return: M, the interpolation matrix

        locType can be::

            'Ex'    -> x-component of field defined on edges
            'Ey'    -> y-component of field defined on edges
            'Ez'    -> z-component of field defined on edges
            'Fx'    -> x-component of field defined on faces
            'Fy'    -> y-component of field defined on faces
            'Fz'    -> z-component of field defined on faces
            'N'     -> scalar field defined on nodes
            'CC'    -> scalar field defined on cell centers
        """
        if self._meshType == 'CYL' and self.isSymmetric and locType in ['Ex','Ez','Fy']:
            raise Exception('Symmetric CylMesh does not support %s interpolation, as this variable does not exist.' % locType)

        loc = Utils.asArray_N_x_Dim(loc, self.dim)

        if zerosOutside is False:
            assert np.all(self.isInside(loc)), "Points outside of mesh"
        else:
            indZeros = np.logical_not(self.isInside(loc))
            loc[indZeros, :] = np.array([v.mean() for v in self.getTensor('CC')])

        if locType in ['Fx','Fy','Fz','Ex','Ey','Ez']:
            ind = {'x':0, 'y':1, 'z':2}[locType[1]]
            assert self.dim >= ind, 'mesh is not high enough dimension.'
            nF_nE = self.vnF if 'F' in locType else self.vnE
            components = [Utils.spzeros(loc.shape[0], n) for n in nF_nE]
            components[ind] = Utils.interpmat(loc, *self.getTensor(locType))
            # remove any zero blocks (hstack complains)
            components = [comp for comp in components if comp.shape[1] > 0]
            Q = sp.hstack(components)
        elif locType in ['CC', 'N']:
            Q = Utils.interpmat(loc, *self.getTensor(locType))
        else:
            raise NotImplementedError('getInterpolationMat: locType=='+locType+' and mesh.dim=='+str(self.dim))

        if zerosOutside:
            Q[indZeros, :] = 0

        return Q.tocsr()
Ejemplo n.º 12
0
 def test_sub2ind(self):
     x = np.ones((5, 2))
     self.assertTrue(np.all(sub2ind(x.shape, [0, 0]) == [0]))
     self.assertTrue(np.all(sub2ind(x.shape, [4, 0]) == [4]))
     self.assertTrue(np.all(sub2ind(x.shape, [0, 1]) == [5]))
     self.assertTrue(np.all(sub2ind(x.shape, [4, 1]) == [9]))
     self.assertTrue(np.all(sub2ind(x.shape, [[4, 1]]) == [9]))
     self.assertTrue(
         np.all(
             sub2ind(x.shape, [[0, 0], [4, 0], [0, 1], [4, 1]]) ==
             [0, 4, 5, 9]))
Ejemplo n.º 13
0
 def test_numpy_one(self):
     o = Identity()
     n = np.r_[2.,3]
     assert np.all(n+1 == n+o)
     assert np.all(1+n == o+n)
     assert np.all(n-1 == n-o)
     assert np.all(1-n == o-n)
     assert np.all(n/1 == n/o)
     assert np.all(n/-1 == n/-o)
     assert np.all(1/n == o/n)
     assert np.all(-1/n == -o/n)
     assert np.all(n*1 == n*o)
     assert np.all(n*-1 == n*-o)
     assert np.all(1*n == o*n)
     assert np.all(-1*n == -o*n)
Ejemplo n.º 14
0
 def test_indexCube_2D(self):
     nN = np.array([3, 3])
     self.assertTrue(np.all(indexCube('A', nN) == np.array([0, 1, 3, 4])))
     self.assertTrue(np.all(indexCube('B', nN) == np.array([3, 4, 6, 7])))
     self.assertTrue(np.all(indexCube('C', nN) == np.array([4, 5, 7, 8])))
     self.assertTrue(np.all(indexCube('D', nN) == np.array([1, 2, 4, 5])))
Ejemplo n.º 15
0
 def test_ind2sub(self):
     x = np.ones((5, 2))
     self.assertTrue(
         np.all(ind2sub(x.shape, [0, 4, 5, 9])[0] == [0, 4, 0, 4]))
     self.assertTrue(
         np.all(ind2sub(x.shape, [0, 4, 5, 9])[1] == [0, 0, 1, 1]))
Ejemplo n.º 16
0
    def getInterpolationMat(self, loc, locType='CC', zerosOutside=False):
        """ Produces interpolation matrix

        :param numpy.ndarray loc: Location of points to interpolate to
        :param str locType: What to interpolate (see below)
        :rtype: scipy.sparse.csr.csr_matrix
        :return: M, the interpolation matrix

        locType can be::

            'Ex'    -> x-component of field defined on edges
            'Ey'    -> y-component of field defined on edges
            'Ez'    -> z-component of field defined on edges
            'Fx'    -> x-component of field defined on faces
            'Fy'    -> y-component of field defined on faces
            'Fz'    -> z-component of field defined on faces
            'N'     -> scalar field defined on nodes
            'CC'    -> scalar field defined on cell centers
            'CCVx'  -> x-component of vector field defined on cell centers
            'CCVy'  -> y-component of vector field defined on cell centers
            'CCVz'  -> z-component of vector field defined on cell centers
        """
        if self._meshType == 'CYL' and self.isSymmetric and locType in [
                'Ex', 'Ez', 'Fy'
        ]:
            raise Exception(
                'Symmetric CylMesh does not support %s interpolation, as this variable does not exist.'
                % locType)

        loc = Utils.asArray_N_x_Dim(loc, self.dim)

        if zerosOutside is False:
            assert np.all(self.isInside(loc)), "Points outside of mesh"
        else:
            indZeros = np.logical_not(self.isInside(loc))
            loc[indZeros, :] = np.array(
                [v.mean() for v in self.getTensor('CC')])

        if locType in ['Fx', 'Fy', 'Fz', 'Ex', 'Ey', 'Ez']:
            ind = {'x': 0, 'y': 1, 'z': 2}[locType[1]]
            assert self.dim >= ind, 'mesh is not high enough dimension.'
            nF_nE = self.vnF if 'F' in locType else self.vnE
            components = [Utils.spzeros(loc.shape[0], n) for n in nF_nE]
            components[ind] = Utils.interpmat(loc, *self.getTensor(locType))
            # remove any zero blocks (hstack complains)
            components = [comp for comp in components if comp.shape[1] > 0]
            Q = sp.hstack(components)
        elif locType in ['CC', 'N']:
            Q = Utils.interpmat(loc, *self.getTensor(locType))
        elif locType in ['CCVx', 'CCVy', 'CCVz']:
            Q = Utils.interpmat(loc, *self.getTensor('CC'))
            Z = Utils.spzeros(loc.shape[0], self.nC)
            if locType == 'CCVx':
                Q = sp.hstack([Q, Z, Z])
            elif locType == 'CCVy':
                Q = sp.hstack([Z, Q, Z])
            elif locType == 'CCVz':
                Q = sp.hstack([Z, Z, Q])

        else:
            raise NotImplementedError('getInterpolationMat: locType==' +
                                      locType + ' and mesh.dim==' +
                                      str(self.dim))

        if zerosOutside:
            Q[indZeros, :] = 0

        return Q.tocsr()
Ejemplo n.º 17
0
 def check(exp,ans):
     assert np.all((exp).todense() == ans)
Ejemplo n.º 18
0
 def test_indexCube_2D(self):
     nN = np.array([3, 3])
     self.assertTrue(np.all(indexCube('A', nN) == np.array([0, 1, 3, 4])))
     self.assertTrue(np.all(indexCube('B', nN) == np.array([3, 4, 6, 7])))
     self.assertTrue(np.all(indexCube('C', nN) == np.array([4, 5, 7, 8])))
     self.assertTrue(np.all(indexCube('D', nN) == np.array([1, 2, 4, 5])))
Ejemplo n.º 19
0
def xy_2_lineID(DCsurvey):
    """
        Read DC survey class and append line ID.
        Assumes that the locations are listed in the order
        they were collected. May need to generalize for random
        point locations, but will be more expensive

        Input:
        :param DCdict Vectors of station location

        Output:
        :param LineID Vector of integers
        :return

        Created on Thu Feb 11, 2015

        @author: dominiquef

    """

    # Compute unit vector between two points
    nstn = DCsurvey.nSrc

    # Pre-allocate space
    lineID = np.zeros(nstn)

    linenum = 0
    indx    = 0

    for ii in range(nstn):

        if ii == 0:

            A = DCsurvey.srcList[ii].loc[0]
            B = DCsurvey.srcList[ii].loc[1]

            xout = np.mean([A[0:2],B[0:2]], axis = 0)

            xy0 = A[:2]
            xym = xout

            # Deal with replicate pole location
            if np.all(xy0==xym):

                xym[0] = xym[0] + 1e-3

            continue

        A = DCsurvey.srcList[ii].loc[0]
        B = DCsurvey.srcList[ii].loc[1]

        xin = np.mean([A[0:2],B[0:2]], axis = 0)

        # Compute vector between neighbours
        vec1, r1 = r_unit(xout,xin)

        # Compute vector between current stn and mid-point
        vec2, r2 = r_unit(xym,xin)

        # Compute vector between current stn and start line
        vec3, r3 = r_unit(xy0,xin)

        # Compute vector between mid-point and start line
        vec4, r4 = r_unit(xym,xy0)

        # Compute dot product
        ang1 = np.abs(vec1.dot(vec2))
        ang2 = np.abs(vec3.dot(vec4))

        # If the angles are smaller then 45d, than next point is on a new line
        if ((ang1 < np.cos(np.pi/4.)) | (ang2 < np.cos(np.pi/4.))) & (np.all(np.r_[r1,r2,r3,r4] > 0)):

            # Re-initiate start and mid-point location
            xy0 = A[:2]
            xym = xin

            # Deal with replicate pole location
            if np.all(xy0==xym):

                xym[0] = xym[0] + 1e-3

            linenum += 1
            indx = ii

        else:
            xym = np.mean([xy0,xin], axis = 0)

        lineID[ii] = linenum
        xout = xin

    return lineID
Ejemplo n.º 20
0
    def getInterpolationMat(self, loc, locType="CC", zerosOutside=False):
        """ Produces interpolation matrix

        :param numpy.ndarray loc: Location of points to interpolate to
        :param str locType: What to interpolate (see below)
        :rtype: scipy.sparse.csr_matrix
        :return: M, the interpolation matrix

        locType can be::

            'Ex'    -> x-component of field defined on edges
            'Ey'    -> y-component of field defined on edges
            'Ez'    -> z-component of field defined on edges
            'Fx'    -> x-component of field defined on faces
            'Fy'    -> y-component of field defined on faces
            'Fz'    -> z-component of field defined on faces
            'N'     -> scalar field defined on nodes
            'CC'    -> scalar field defined on cell centers
            'CCVx'  -> x-component of vector field defined on cell centers
            'CCVy'  -> y-component of vector field defined on cell centers
            'CCVz'  -> z-component of vector field defined on cell centers
        """
        if self._meshType == "CYL" and self.isSymmetric and locType in ["Ex", "Ez", "Fy"]:
            raise Exception(
                "Symmetric CylMesh does not support {0!s} interpolation, as this variable does not exist.".format(
                    locType
                )
            )

        loc = Utils.asArray_N_x_Dim(loc, self.dim)

        if zerosOutside is False:
            assert np.all(self.isInside(loc)), "Points outside of mesh"
        else:
            indZeros = np.logical_not(self.isInside(loc))
            loc[indZeros, :] = np.array([v.mean() for v in self.getTensor("CC")])

        if locType in ["Fx", "Fy", "Fz", "Ex", "Ey", "Ez"]:
            ind = {"x": 0, "y": 1, "z": 2}[locType[1]]
            assert self.dim >= ind, "mesh is not high enough dimension."
            nF_nE = self.vnF if "F" in locType else self.vnE
            components = [Utils.spzeros(loc.shape[0], n) for n in nF_nE]
            components[ind] = Utils.interpmat(loc, *self.getTensor(locType))
            # remove any zero blocks (hstack complains)
            components = [comp for comp in components if comp.shape[1] > 0]
            Q = sp.hstack(components)
        elif locType in ["CC", "N"]:
            Q = Utils.interpmat(loc, *self.getTensor(locType))
        elif locType in ["CCVx", "CCVy", "CCVz"]:
            Q = Utils.interpmat(loc, *self.getTensor("CC"))
            Z = Utils.spzeros(loc.shape[0], self.nC)
            if locType == "CCVx":
                Q = sp.hstack([Q, Z, Z])
            elif locType == "CCVy":
                Q = sp.hstack([Z, Q, Z])
            elif locType == "CCVz":
                Q = sp.hstack([Z, Z, Q])

        else:
            raise NotImplementedError("getInterpolationMat: locType==" + locType + " and mesh.dim==" + str(self.dim))

        if zerosOutside:
            Q[indZeros, :] = 0

        return Q.tocsr()
Ejemplo n.º 21
0
 def test_ind2sub(self):
     x = np.ones((5,2))
     self.assertTrue(np.all(ind2sub(x.shape, [0,4,5,9])[0] == [0,4,0,4]))
     self.assertTrue(np.all(ind2sub(x.shape, [0,4,5,9])[1] == [0,0,1,1]))