Пример #1
0
def drawTravelTimeData(ax, data, t=None):
    """Draw first arrival traveltime data into mpl ax a.

    data of type pg.DataContainer must contain sensorIdx 's' and 'g'
    and thus being numbered internally [0..n)
    """
    x = pg.x(data.sensorPositions())
    # z = pg.z(data.sensorPositions())

    shots = pg.unique(pg.sort(data('s')))
    geoph = pg.unique(pg.sort(data('g')))

    startOffsetIDX = 0

    if min(min(shots), min(geoph)) == 1:
        startOffsetIDX = 1

    tShow = data('t')
    if t is not None:
        tShow = t

    ax.set_xlim([min(x), max(x)])
    ax.set_ylim([max(tShow), -0.002])
    ax.figure.show()

    for shot in shots:
        gIdx = pg.find(data('s') == shot)
        sensorIdx = [int(i__ - startOffsetIDX) for i__ in data('g')[gIdx]]
        ax.plot(x[sensorIdx], tShow[gIdx], 'x-')

    yPixel = ax.transData.inverted().transform_point((1, 1))[1] - \
        ax.transData.inverted().transform_point((0, 0))[1]
    xPixel = ax.transData.inverted().transform_point((1, 1))[0] - \
        ax.transData.inverted().transform_point((0, 0))[0]

    # draw shot points
    ax.plot(x[[int(i__ - startOffsetIDX) for i__ in shots]],
            np.zeros(len(shots)) + 8. * yPixel,
            'gv',
            markersize=8)

    # draw geophone points
    ax.plot(x[[int(i__ - startOffsetIDX) for i__ in geoph]],
            np.zeros(len(geoph)) + 3. * yPixel,
            'r^',
            markersize=8)

    ax.grid()
    ax.set_ylim([max(tShow), +16. * yPixel])
    ax.set_xlim([min(x) - 5. * xPixel, max(x) + 5. * xPixel])

    ax.set_xlabel('x-Coordinate [m]')
    ax.set_ylabel('Traveltime [ms]')
Пример #2
0
def drawTravelTimeData(axes, data, t=None):
    """
        Draw first arrival traveltime data into mpl axes a.
        data of type \ref DataContainer must contain sensorIdx 's' and 'g'
        and thus being numbered internally [0..n)
    """

    x = pg.x(data.sensorPositions())
#    z = pg.z(data.sensorPositions())

    shots = pg.unique(pg.sort(data('s')))
    geoph = pg.unique(pg.sort(data('g')))

    startOffsetIDX = 0

    if min(min(shots), min(geoph)) == 1:
        startOffsetIDX = 1

    tShow = data('t')
    if t is not None:
        tShow = t

    axes.set_xlim([min(x), max(x)])
    axes.set_ylim([max(tShow), -0.002])
    axes.figure.show()

    for shot in shots:
        gIdx = pg.find(data('s') == shot)
        sensorIdx = [int(i__ - startOffsetIDX) for i__ in data('g')[gIdx]]
        axes.plot(x[sensorIdx], tShow[gIdx], 'x-')

    yPixel = axes.transData.inverted().transform_point((1, 1))[1] - \
        axes.transData.inverted().transform_point((0, 0))[1]
    xPixel = axes.transData.inverted().transform_point((1, 1))[0] - \
        axes.transData.inverted().transform_point((0, 0))[0]

    # draw shot points
    axes.plot(x[[int(i__ - startOffsetIDX) for i__ in shots]],
           np.zeros(len(shots)) + 8. * yPixel, 'gv', markersize=8)

    # draw geophone points
    axes.plot(x[[int(i__ - startOffsetIDX) for i__ in geoph]],
           np.zeros(len(geoph)) + 3. * yPixel, 'r^', markersize=8)

    axes.grid()
    axes.set_ylim([max(tShow), +16. * yPixel])
    axes.set_xlim([min(x) - 5. * xPixel, max(x) + 5. * xPixel])

    axes.set_xlabel('x-Coordinate [m]')
    axes.set_ylabel('Traveltime [ms]')
Пример #3
0
    def test_face_in_face(self):
        """Test subface with different marker constructed with hole marker."""
        w = mt.createCube(marker=1, boundaryMarker=1)
        b = w.boundary(2)

        pad = mt.createFacet(
            mt.createCircle(radius=0.2, segments=12, isHole=True))
        b2 = pad.boundary(0)

        # rotate to match target norm and pos
        rot = pg.core.getRotation(b2.norm(), b.norm())
        pad.transform(rot)
        pad.translate(b.center())

        # create a boundary with new marker match the hole
        w.copyBoundary(b2)

        w.createBoundary(w.nodes(
            [w.createNode(n.pos()).id() for n in b2.nodes()]),
                         marker=2)

        #print(w.boundaryMarkers())

        mesh = mt.createMesh(w)

        #pg.show(mesh)
        # w.exportPLC('pad.poly')
        # mesh.exportBoundaryVTU('b.vtu')
        np.testing.assert_array_equal(
            pg.unique(pg.sort(mesh.boundaryMarkers())), [0, 1, 2])

        # print(mesh)
        # mesh.exportBoundaryVTU('b.vtu')
        pg.show(mesh)
Пример #4
0
def showMatrix(mat, ax=None, **kwargs):
    """Show various pyGIMLi matrices using matplotlib.

    Args
    ----
    mat: matrix

    ax: mpl.axes

    Keyword Args
    ------------
        **kwargs : forwarded to mpl plotting commands

    Returns
    -------
        mpl.axes, Colorbar
    """
    if ax is None:
        print(ax)
        ax = pg.show()[0]

    try:
        from scipy.sparse import spmatrix

        if isinstance(mat, spmatrix):
            gci = drawSparseMatrix(ax, mat, **kwargs)
            return ax, None
    except ImportError:
        pass

    if isinstance(mat, (pg.core.RSparseMapMatrix, pg.core.RSparseMatrix)):
        gci = drawSparseMatrix(ax, mat, **kwargs)
        cBar = None
    elif isinstance(mat, pg.matrix.BlockMatrix):
        gci, cBar = drawBlockMatrix(ax, mat, **kwargs)

        if cBar is None:
            uniqueIDs = pg.unique([e.matrixID for e in mat.entries()])
            cMap = pg.plt.cm.get_cmap("Set3", len(uniqueIDs))
            sm = pg.plt.cm.ScalarMappable(cmap=cMap)

            cBar = createColorBar(sm,
                                  ax=ax,
                                  label="Matrix ID",
                                  cMin=-0.5,
                                  cMax=len(uniqueIDs) - 0.5)
            ticks = np.arange(len(uniqueIDs))
            cBar.set_ticks(ticks)

            labels = []
            for ID in uniqueIDs:
                label = "{:d}".format(ID)
                labels.append(label)
            cBar.set_ticklabels(labels)

    else:
        pg.error("Matrix type not supported yet.")
    return ax, cBar
Пример #5
0
    def test_appendTriangleBoundary(self):
        geom = mt.createWorld(start=[-10, 0], end=[10, -10], layers=[-5, -10])
        mesh = mt.createMesh(geom, area=1)

        mesh2 = mt.appendTriangleBoundary(mesh, marker=0)

        # test if boundary markers are preserved
        np.testing.assert_array_equal(
            pg.unique(pg.sort(mesh2.boundaryMarkers())), [-2, -1, 0, 2, 7, 8])
Пример #6
0
def exportSTL(mesh, fileName, ascii=True):
    """Write :term:`STL` surface mesh and returns a :gimliapi:`GIMLI::Mesh`.

    Export a three dimensional boundary :gimliapi:`GIMLI::Mesh` into a
    :term:`STL` surface mesh. Boundaries with different marker 
    will be separated into different STL solids.

    TODO: 
        * ASCII=False, write binary STL
        * QuadrangleFace Boundaries
        * p2 Boundaries

    Parameters
    ----------
    mesh : :gimliapi:`GIMLI::Mesh`
        Mesh to be exported. Only Boundaries of type TriangleFace will be 
        exported.

    fileName : str
        name of the .stl file containing the STL surface mesh

    ascii : bool [True]
        STL Ascii format
    
    """
    marker = pg.unique(pg.sort(mesh.boundaryMarkers()))

    if not '.stl' in fileName:
        fileName = fileName + '.stl'

    fi = open(fileName, 'w')
    for m in marker:
        me = mesh.extract(mesh.boundaries(mesh.boundaryMarkers() == m))

        fi.write('solid ' + str(m) + '\n')

        for b in me.boundaries():
            n = b.norm()
            fi.write('facet normal %f %f %f\n' % (n[0], n[1], n[2]))
            fi.write('\touter loop\n')
            fi.write(
                '\t\tvertex %f %f %f\n' %
                (b.node(0).pos()[0], b.node(0).pos()[1], b.node(0).pos()[2]))
            fi.write(
                '\t\tvertex %f %f %f\n' %
                (b.node(1).pos()[0], b.node(1).pos()[1], b.node(1).pos()[2]))
            fi.write(
                '\t\tvertex %f %f %f\n' %
                (b.node(2).pos()[0], b.node(2).pos()[1], b.node(2).pos()[2]))
            fi.write('\tendloop\n')
            fi.write('endfacet\n')

        fi.write('endsolid\n')

    fi.close()
Пример #7
0
    def test_cubeBasics(self):
        plc = mt.createCube()
        for i, b in enumerate(plc.boundaries()):
            b.setMarker(i + 1)

        mesh = mt.createMesh(plc)

        for marker in pg.unique(pg.sort(plc.boundaryMarkers())):
            b1 = plc.boundaries(plc.boundaryMarkers() == marker)[0]
            b2 = mesh.boundaries(mesh.boundaryMarkers() == marker)[0]

            np.testing.assert_array_equal(b1.norm(), b2.norm())
Пример #8
0
def exportSTL(mesh, fileName, ascii=True):
    """Write :term:`STL` surface mesh and returns a :gimliapi:`GIMLI::Mesh`.

    Export a three dimensional boundary :gimliapi:`GIMLI::Mesh` into a
    :term:`STL` surface mesh. Boundaries with different marker
    will be separated into different STL solids.

    TODO:
        * ASCII=False, write binary STL
        * QuadrangleFace Boundaries
        * p2 Boundaries

    Parameters
    ----------
    mesh : :gimliapi:`GIMLI::Mesh`
        Mesh to be exported. Only Boundaries of type TriangleFace will be
        exported.

    fileName : str
        name of the .stl file containing the STL surface mesh

    ascii : bool [True]
        STL Ascii format

    """
    marker = pg.unique(pg.sort(mesh.boundaryMarkers()))

    if not '.stl' in fileName:
        fileName = fileName + '.stl'

    fi = open(fileName, 'w')
    for m in marker:
        me = mesh.extract(mesh.boundaries(mesh.boundaryMarkers() == m))

        fi.write('solid ' + str(m) + '\n')

        for b in me.boundaries():
            n = b.norm()
            fi.write('facet normal %f %f %f\n' %(n[0], n[1], n[2]))
            fi.write('\touter loop\n')
            fi.write('\t\tvertex %f %f %f\n' %(b.node(0).pos()[0], b.node(0).pos()[1], b.node(0).pos()[2]))
            fi.write('\t\tvertex %f %f %f\n' %(b.node(1).pos()[0], b.node(1).pos()[1], b.node(1).pos()[2]))
            fi.write('\t\tvertex %f %f %f\n' %(b.node(2).pos()[0], b.node(2).pos()[1], b.node(2).pos()[2]))
            fi.write('\tendloop\n')
            fi.write('endfacet\n')

        fi.write('endsolid\n')

    fi.close()
Пример #9
0
    def test_io_STL(self):
        try:
            import tempfile as tmp
        except ImportError:
            return

        str = r"""solid name
    facet normal 1.000000e+000 0.000000e+000 0.000000e+000 
        outer loop 
            vertex   6.100000e+002 -1.046773e+002 -2.818708e+003 
            vertex   6.100000e+002 -1.249950e+002 -2.814064e+003 
            vertex   6.100000e+002 -2.507565e+000 -2.930000e+003 
        endloop
    endfacet
endsolid
solid name
    facet normal -2.568735e-007 1.396183e-002 -9.999025e-001
        outer loop
            vertex   1.350000e+002 3.839764e+001 -2.929429e+003
            vertex   6.100000e+002 7.930283e+001 -2.928858e+003
            vertex   6.100000e+002 -2.507565e+000 -2.930000e+003
        endloop
    endfacet
endsolid"""
        _, fileName = tmp.mkstemp(suffix='.stl')
        fi = open(fileName, 'w')
        fi.write(str)
        fi.close()

        mesh = pg.load(fileName, verbose=True)

        np.testing.assert_equal(mesh.cellCount(), 0)
        np.testing.assert_equal(mesh.nodeCount(), 5)
        np.testing.assert_equal(mesh.boundaryCount(), 2)

        np.testing.assert_equal(
            np.array(pg.unique(pg.sort(mesh.boundaryMarkers()))), [0, 1])

        try:
            os.remove(fileName)
        except:
            print("can't remove:", fileName)
Пример #10
0
    def test_io_STL(self):
        try:
            import tempfile as tmp
        except ImportError:
            return

        str = r"""solid name
    facet normal 1.000000e+000 0.000000e+000 0.000000e+000 
        outer loop 
            vertex   6.100000e+002 -1.046773e+002 -2.818708e+003 
            vertex   6.100000e+002 -1.249950e+002 -2.814064e+003 
            vertex   6.100000e+002 -2.507565e+000 -2.930000e+003 
        endloop
    endfacet
endsolid
solid name
    facet normal -2.568735e-007 1.396183e-002 -9.999025e-001
        outer loop
            vertex   1.350000e+002 3.839764e+001 -2.929429e+003
            vertex   6.100000e+002 7.930283e+001 -2.928858e+003
            vertex   6.100000e+002 -2.507565e+000 -2.930000e+003
        endloop
    endfacet
endsolid"""
        _, fileName = tmp.mkstemp(suffix='.stl')
        fi = open(fileName, 'w')
        fi.write(str)
        fi.close()

        mesh = pg.load(fileName, verbose=True)

        np.testing.assert_equal(mesh.cellCount(), 0)
        np.testing.assert_equal(mesh.nodeCount(), 5)
        np.testing.assert_equal(mesh.boundaryCount(), 2)

        np.testing.assert_equal(np.array(pg.unique(pg.sort(mesh.boundaryMarkers()))),
                               [0, 1])
        
        try:
            os.remove(fileName)
        except:
            print("can't remove:", fileName)
Пример #11
0
def drawPLC(ax,
            mesh,
            fillRegion=True,
            regionMarker=True,
            boundaryMarker=False,
            **kwargs):
    """Draw 2D PLC into the given ax.

    Parameters
    ----------

    fillRegion: bool [True]
        Fill the regions with default colormap.

    regionMarker: bool [True]
        show region marker

    boundaryMarker: bool [False]
        show boundary marker

    **kwargs

    Examples
    --------
    """
    #    eCircles = []
    cols = []

    if fillRegion and mesh.boundaryCount() > 0:
        tmpMesh = pg.meshtools.createMesh(mesh, quality=20, area=0)
        if tmpMesh.cellCount() == 0:
            pass
        else:
            drawModel(ax=ax,
                      mesh=tmpMesh,
                      data=tmpMesh.cellMarkers(),
                      nLevs=len(pg.unique(pg.sort(tmpMesh.cellMarkers()))),
                      levels=pg.utils.unique(tmpMesh.cellMarkers()),
                      tri=True,
                      alpha=0.5,
                      linewidth=0.0,
                      edgecolors='k',
                      snap=True)

    for n in mesh.nodes():
        col = (0.0, 0.0, 0.0, 0.5)

        if n.marker() == pg.MARKER_NODE_SENSOR:
            col = (0.0, 0.0, 0.0, 1.0)

        ms = kwargs.pop('markersize', 5)
        ax.plot(n.pos()[0], n.pos()[1], 'bo', markersize=ms, color=col)
        #        eCircles.append(mpl.patches.Circle((n.pos()[0], n.pos()[1])))
        #        eCircles.append(mpl.patches.Circle((n.pos()[0], n.pos()[1]), 0.1))
        cols.append(col)

    if boundaryMarker:
        for b in mesh.boundaries():
            ax.text(b.center()[0],
                    b.center()[1],
                    str(b.marker()),
                    color='red',
                    verticalalignment='center',
                    horizontalalignment='center')  # 'white'


#    p = mpl.collections.PatchCollection(eCircles, color=cols)
#    ax.add_collection(p)

    if regionMarker:
        for reg in mesh.regionMarker():
            ax.text(reg[0],
                    reg[1],
                    str(reg.marker()) + ": " + str(reg.area()),
                    color='black',
                    verticalalignment='center',
                    horizontalalignment='left')  # 'white'

        for hole in mesh.holeMarker():
            ax.text(hole[0], hole[1], 'H', color='black')

    updateAxes_(ax)
Пример #12
0
def midconfERT(data, ind=None, rnum=1, circular=False):
    """Return the midpoint and configuration key for ERT data.

    Return the midpoint and configuration key for ERT data.

    Parameters
    ----------
    data : DataContainerERT
        data container with sensorPositions and a/b/m/n fields

    ind : []
        Documentme

    rnum : []
        Documentme

    circular : bool
        Return midpoint in degree (rad) instead if meter.

    Returns
    -------
    mid : np.array of float
        representative midpoint (middle of MN, AM depending on array)
    conf : np.array of float
        configuration/array key consisting of
        1) array type (Wenner-alpha/beta, Schlumberger, PP, PD, DD, MG)
            00000: pole-pole
            10000: pole-dipole or dipole-pole
            30000: Wenner-alpha
            40000: Schlumberger or Gradient
            50000: dipole-dipole or Wenner-beta
        2) potential dipole length (in electrode spacings)
            .XX..: dipole length
        3) separation factor (current dipole length or (di)pole separation)
            ...XX: pole/dipole separation (PP,PD,DD,GR) or separation
    """
    #    xe = np.hstack((pg.x(data.sensorPositions()), np.nan))  # not used anymore
    x0 = data.sensorPosition(0).x()
    xe = pg.x(data.sensorPositions()) - x0
    ux = pg.unique(xe)

    if len(ux) * 2 > data.sensorCount():  # 2D with topography case
        dx = np.array(pg.utils.diff(pg.utils.cumDist(data.sensorPositions())))
        dxM = pg.mean(dx)
        if min(pg.y(data)) != max(pg.y(data)) or \
           min(pg.z(data)) != max(pg.z(data)):
            # Topography case
            if (max(abs(dx - dxM)) < dxM * 0.9):
                # if the maximum spacing < meanSpacing/2 we assume equidistant
                # spacing and no missing electrodes
                dx = np.ones(len(dx)) * dxM
            else:
                # topography with probably missing electrodes
                dx = np.floor(dx / np.round(dxM)) * dxM
                pass
        if max(dx) < 0.5:
            print("Detecting small distances, using mm accuracy")
            rnum = 3
        xe = np.hstack((0., np.cumsum(np.round(dx, rnum)), np.nan))

        de = np.median(np.diff(xe[:-1])).round(rnum)
        ne = np.round(xe / de)
    else:  # 3D (without topo) case => take positions directly
        de = np.median(np.diff(ux)).round(1)
        ne = np.array(xe / de, dtype=int)

    # a, b, m, n = data('a'), data('b'), data('m'), data('n')
    # check if xe[a]/a is better suited (has similar size)
    if circular:
        # for circle geometry
        center = np.mean(data.sensorPositions(), axis=0)
        r = data.sensors()[0].distance(center)
        s0 = data.sensors()[0] - center
        s1 = data.sensors()[1] - center
        p0 = np.arctan2(s0[1], s0[0])
        p1 = np.arctan2(s1[1], s1[0])
        if p1 > p0:
            # rotate left
            x = np.cos(np.linspace(0, 2 * pi,
                                   data.sensorCount() + 1) + p0)[:-1] * r
            y = np.sin(np.linspace(0, 2 * pi,
                                   data.sensorCount() + 1) + p0)[:-1] * r
        else:
            x = np.cos(np.linspace(2 * pi, 0,
                                   data.sensorCount() + 1) + p0)[:-1] * r
            y = np.sin(np.linspace(2 * pi, 0,
                                   data.sensorCount() + 1) + p0)[:-1] * r

        a = np.array([np.arctan2(y[i], x[i]) for i in data['a']])
        b = np.array([np.arctan2(y[i], x[i]) for i in data['b']])
        m = np.array([np.arctan2(y[i], x[i]) for i in data['m']])
        n = np.array([np.arctan2(y[i], x[i]) for i in data['n']])

        a = np.unwrap(a) % (np.pi * 2)
        b = np.unwrap(b) % (np.pi * 2)
        m = np.unwrap(m) % (np.pi * 2)
        n = np.unwrap(n) % (np.pi * 2)

    else:
        a = np.array([ne[int(i)] for i in data('a')])
        b = np.array([ne[int(i)] for i in data('b')])
        m = np.array([ne[int(i)] for i in data('m')])
        n = np.array([ne[int(i)] for i in data('n')])

    if ind is not None:
        a = a[ind]
        b = b[ind]
        m = m[ind]
        n = n[ind]

    anan = np.isnan(a)
    a[anan] = b[anan]
    b[anan] = np.nan
    ab, am, an = np.abs(a - b), np.abs(a - m), np.abs(a - n)
    bm, bn, mn = np.abs(b - m), np.abs(b - n), np.abs(m - n)

    if circular:
        for v in [ab, mn, bm, an]:
            v[v > pi] = 2 * pi - v[v > pi]

    # 2-point (default) 00000
    sep = np.abs(a - m)
    mid = (a + m) / 2

    # 3-point (PD, DP) (now only b==-1 or n==-<1, check also for a and m)
    imn = np.isfinite(n) * np.isnan(b)
    mid[imn] = (m[imn] + n[imn]) / 2
    sep[imn] = np.minimum(am[imn], an[imn]) + 10000 + 100 * (mn[imn]-1) + \
        (np.sign(a[imn]-m[imn])/2+0.5) * 10000
    iab = np.isfinite(b) * np.isnan(n)
    mid[iab] = (a[iab] + b[iab]) / 2  # better 20000 or -10000?
    sep[iab] = np.minimum(am[iab], bm[iab]) + 10000 + 100 * (ab[iab]-1) + \
        (np.sign(a[iab]-n[iab])/2+0.5) * 10000
    #  + 10000*(a-m)

    # 4-point alpha: 30000 (WE) or 4000 (SL)
    iabmn = np.isfinite(a) & np.isfinite(b) & np.isfinite(m) & np.isfinite(n)
    ialfa = np.copy(iabmn)
    ialfa[iabmn] = (ab[iabmn] >= mn[iabmn] + 2)  # old
    mnmid = (m[iabmn] + n[iabmn]) / 2
    ialfa[iabmn] = np.sign((a[iabmn] - mnmid) * (b[iabmn] - mnmid)) < 0

    mid[ialfa] = (m[ialfa] + n[ialfa]) / 2
    spac = np.minimum(bn[ialfa], bm[ialfa])
    abmn3 = np.round((3 * mn[ialfa] - ab[ialfa]) * 10000) / 10000
    sep[ialfa] = spac + (mn[ialfa]-1)*100*(abmn3 != 0) + \
        30000 + (abmn3 < 0)*10000
    # gradient

    # %% 4-point beta
    ibeta = np.copy(iabmn)
    ibeta[iabmn] = (bm[iabmn] >= mn[iabmn]) & (~ialfa[iabmn])

    if circular:
        # print(ab[ibeta])
        ibeta = np.copy(iabmn)

        def _averageAngle(vs):
            sumsin = 0
            sumcos = 0

            for v in vs:
                sumsin += np.sin(v)
                sumcos += np.cos(v)

            return np.arctan2(sumsin, sumcos)

        abC = _averageAngle([a[ibeta], b[ibeta]])
        mnC = _averageAngle([m[ibeta], n[ibeta]])

        mid[ibeta] = _averageAngle([abC, mnC])

        # special case when dipoles are completely opposite
        iOpp = abs(abs((mnC - abC)) - np.pi) < 1e-3
        mid[iOpp] = _averageAngle([b[iOpp], m[iOpp]])

        minAb = min(ab[ibeta])
        sep[ibeta] = 50000 + (np.round(ab[ibeta]/minAb)) * 100 + \
            np.round(np.minimum(np.minimum(am[ibeta], an[ibeta]),
                                np.minimum(bm[ibeta], bn[ibeta])) / minAb)
    else:
        mid[ibeta] = (a[ibeta] + b[ibeta] + m[ibeta] + n[ibeta]) / 4

        sep[ibeta] = 50000 + (ab[ibeta] - 1) * 100 + np.minimum(
            np.minimum(am[ibeta], an[ibeta]), np.minimum(bm[ibeta], bn[ibeta]))

    # %% 4-point gamma
    # multiply with electrode distance and add first position
    if not circular:
        mid *= de
        mid += x0
    return mid, sep
Пример #13
0
def drawBoundaryMarkers(ax, mesh, clipBoundaryMarkers=False, **kwargs):
    """Draw boundary markers for mesh.boundaries with marker != 0

    Args
    ----
    mesh: :gimliapi:`GIMLI::Mesh`
        Mesh that have the boundary markers.

    clipBoundaryMarkers: bool [False]
        Clip boundary marker to the axes limits if needed.

    Keyword Arguments
    ----------------
    **kwargs
        Forwarded to plot

    Examples
    --------
    >>> import pygimli as pg
    >>> import pygimli.meshtools as mt
    >>> c0 = mt.createCircle(pos=(0.0, 0.0), radius=1, segments=4)
    >>> l0 = mt.createPolygon([[-0.5, 0.0], [.5, 0.0]], boundaryMarker=2)
    >>> l1 = mt.createPolygon([[-0.25, -0.25], [0.0, -0.5], [0.25, -0.25]],
    ...                       interpolate='spline', addNodes=4,
    ...                       boundaryMarker=3)
    >>> l2 = mt.createPolygon([[-0.25, 0.25], [0.0, 0.5], [0.25, 0.25]],
    ...                       interpolate='spline', addNodes=4,
    ...                       isClosed=True, boundaryMarker=3)
    >>> mesh = mt.createMesh([c0, l0, l1, l2], area=0.01)
    >>> ax, _ = pg.show(mesh)
    >>> pg.viewer.mpl.drawBoundaryMarkers(ax, mesh)
    """
    ms = pg.unique(pg.sort(
        mesh.boundaryMarkers()[mesh.boundaryMarkers() != 0]))

    # cMap = plt.cm.get_cmap("Set3", len(ms))
    kwargs['lw'] = kwargs.pop('lw', 4)

    for i, m in enumerate(ms):
        bs = mesh.findBoundaryByMarker(m)
        paths = mesh.findPaths(bs)
        col = 'C' + str(i)

        for p in paths:

            xs = pg.x(mesh.nodes(p))
            ys = pg.y(mesh.nodes(p))
            path = np.array([xs, ys]).T

            ax.plot(xs, ys, color=col, **kwargs)

            center = pg.meshtools.interpolateAlongCurve(
                path, [pg.utils.cumDist(path)[-1] / 2])[0]

            x = center[0]
            y = center[1]
            bbox_props = dict(boxstyle="circle,pad=0.2", fc="w", ec=col)
            txt = ax.text(x,
                          y,
                          str(m),
                          color=col,
                          va="center",
                          ha="center",
                          zorder=20,
                          bbox=bbox_props,
                          fontsize=9,
                          fontdict={'weight': 'bold'})

            # clipping avoid visuablity outside axes.
            # Needed if the axes limits do not match mesh size.
            txt.set_clip_on(clipBoundaryMarkers)

            ax.plot(xs[0], ys[0], 'o', color='k')
            ax.plot(xs[-1], ys[-1], 'o', color='k')
Пример #14
0
def main( argv ):
    from optparse import OptionParser

    parser = OptionParser( "usage: %prog [options] mesh" )
    parser.add_option("-v", "--verbose", dest="verbose", action="store_true"
                            , help="be verbose", default=False )
    parser.add_option("-o", "--output", dest="outFileName",
                            help="filename for the resulting mesh", metavar="File" )
    parser.add_option("-p", "--paramesh", dest="paraMesh",
                            help="name for reference parameter mesh", metavar="File", default='' )
    (options, args) = parser.parse_args()

    if options.verbose:
        print(options, args)

    if len( args ) == 0:
        parser.print_help()
        print("Please add a mesh or model name.")
        sys.exit( 2 )
    else:
        meshname = args[ 0 ];

    mesh = g.Mesh( meshname )
    
    if options.verbose:
        print("input mesh:", mesh)
        print("nModel(input):", end=' ')
        for m in g.unique( g.sort( mesh.cellMarker() ) ):
            print(m)

    if len( options.paraMesh ) == 0:
        print("no reference mesh given")
        sys.exit(0)

    refParaIn = g.Mesh( options.paraMesh )
    refPara = g.Mesh()
    refPara.createH2Mesh( refParaIn )
    refModel = g.unique( g.sort( refPara.cellMarker() ) )
    if options.verbose:
        print("reference mesh:", refPara)
        print("nModel(ref):", len( refModel ), refModel[ 0] , refModel[1], refModel[2], '...', refModel[-1])
    
    swatch = g.Stopwatch( True )
    lastTime = 0
    for c in mesh.cells():
        cell = refPara.findCell( c.center(), False )
        
        if swatch.duration() - lastTime > 1:
            print("\r", c.id())
            lastTime = swatch.duration()
        
        if cell is not None:
            c.setMarker( cell.marker() )
        else:
            # set background
            c.setMarker( -1 )

    newModel = g.unique( g.sort( mesh.cellMarker() ) )
    
    if options.verbose:
        print("convert:", swatch.duration())
        print("nModel(out):", len( newModel ), newModel[ 0 ], newModel[ 1 ], newModel[ 2 ], ' ... ' , newModel[ -1 ])
        print("diff should be 1(background)", len( newModel ) -len( refModel ))

    missing = g.stdVectorI()
    missingMesh = g.Mesh()
    for i in refModel:
        if i-1 not in newModel:
            for c in refPara.findCellByMarker( i-1 ):
                missing.append( c.id() )
    print(len( missing ), missing)
    
    missingMesh.createMeshByCellIdx( refPara, missing );
    missingMesh.exportVTK( 'missing' )
    
    if options.outFileName:
        mesh.save( options.outFileName )
        if options.verbose:
            print("wrote: ", options.outFileName)
Пример #15
0
def drawPLC(axes, mesh, fillRegion=True, boundaryMarker=False, **kwargs):
    """
    Draw 2D PLC into the given axes.

    Parameters
    ----------

    fillRegion: bool [True]
        Fill the regions with default colormap.

    boundaryMarker: bool [False]
        show boundary marker

    **kwargs

    Examples
    --------
    """

    eCircles = []
    cols = []

    if fillRegion and mesh.boundaryCount() > 0:
        tmpMesh = pg.meshtools.createMesh(mesh, quality=20)
        if tmpMesh.cellCount() == 0:
            pass
        else:
            drawModel(axes=axes, mesh=tmpMesh, data=tmpMesh.cellMarkers(),
                    nLevs=len(pg.unique(pg.sort(tmpMesh.cellMarkers()))),
                    levels=pg.utils.unique(tmpMesh.cellMarkers()),
                    tri=True, alpha=0.5, linewidth=0.0, edgecolors='k',
                    snap=False)

    for n in mesh.nodes():
        col = (0.0, 0.0, 0.0)
        if n.marker() == pg.MARKER_NODE_SENSOR:
            col = (1.0, 0.0, 0.0)

#        eCircles.append(mpl.patches.Circle((n.pos()[0], n.pos()[1])))
        ms = kwargs.pop('markersize', 5)
        axes.plot(n.pos()[0], n.pos()[1], 'bo', markersize=ms,
                  color='black')
#        eCircles.append(mpl.patches.Circle((n.pos()[0], n.pos()[1]), 0.1))
        cols.append(col)

    if boundaryMarker:
        for b in mesh.boundaries():
            axes.text(b.center()[0], b.center()[1], str(b.marker()),
                      color='red', verticalalignment='center',
                      horizontalalignment='center')  # 'white'

    p = mpl.collections.PatchCollection(eCircles, color=cols)
    axes.add_collection(p)

    for reg in mesh.regionMarker():
        axes.text(reg[0], reg[1],
                  str(reg.marker()) + ": " + str(reg.area()),
                  color='black',
                  verticalalignment='center',
                  horizontalalignment='left'
                  )  # 'white'

    for hole in mesh.holeMarker():
        axes.text(hole[0], hole[1], 'H', color='black')

    updateAxes_(axes)
Пример #16
0
def drawBlockMatrix(ax, mat, **kwargs):
    """Draw a view of a matrix into the axes.

    Arguments
    ---------

    ax : mpl axis instance, optional
        Axis instance where the matrix will be plotted.

    mat: pg.Matrix.BlockMatrix

    Keyword Arguments
    -----------------
    spy: bool [False]
        Draw all matrix entries instead of colored blocks

    Returns
    -------
    ax:

    Examples
    --------
    >>> import numpy as np
    >>> import pygimli as pg
    >>> I = pg.matrix.IdentityMatrix(10)
    >>> SM = pg.matrix.SparseMapMatrix()
    >>> for i in range(10):
    ...     SM.setVal(i, 10 - i, 5.0)
    ...     SM.setVal(i,  i, 5.0)
    >>> B = pg.matrix.BlockMatrix()
    >>> B.add(I, 0, 0)
    0
    >>> B.add(SM, 10, 10)
    1
    >>> print(B)
    pg.matrix.BlockMatrix of size 20 x 21 consisting of 2 submatrices.
    >>> fig, (ax1, ax2) = pg.plt.subplots(1, 2, sharey=True)
    >>> _ = pg.show(B, ax=ax1)
    >>> _ = pg.show(B, spy=True, ax=ax2)
    """
    if kwargs.pop('spy', False):
        gci = []
        ids = pg.unique([e.matrixID for e in mat.entries()])
        cMap = pg.plt.cm.get_cmap("Set3", len(ids))

        for e in mat.entries():
            mid = e.matrixID

            mati = mat.mat(mid)
            if isinstance(mati, pg.core.IdentityMatrix):
                mati = np.eye(mati.size())

            gci.append(
                drawSparseMatrix(ax,
                                 mati,
                                 rowOffset=e.rowStart,
                                 colOffset=e.colStart,
                                 color=cMap(mid)))

        return gci, None
    else:
        plcs = []
        for e in mat.entries():
            mid = e.matrixID
            widthy = mat.mat(mid).rows(
            ) - 0.1  # to make sure non-matrix regions are not connected in the plot
            widthx = mat.mat(mid).cols() - 0.1
            plc = pg.meshtools.createRectangle(
                [e.colStart, e.rowStart],
                [e.colStart + widthx, e.rowStart + widthy],
                marker=mid)
            plcs.append(plc)

        bm = pg.meshtools.mergePLC(plcs)
        gci, cBar = pg.viewer.mpl.drawPLC(ax, bm, fitView=False)
        ax.invert_yaxis()
        ax.xaxis.tick_top()
        cBar.set_label("Matrix ID")

        if len(mat.entries()) > 10:
            gci.set_cmap("viridis")
        return gci, cBar