Example #1
0
def plt_diagram(diagram, homology_dimensions=None):
    """Plot a single persistence diagram.

    Parameters
    ----------
    diagram : ndarray, shape (n_points, 3)
        The persistence diagram to plot, where the third dimension along axis 1
        contains homology dimensions, and the other two contain (birth, death)
        pairs to be used as coordinates in the two-dimensional plot.

    homology_dimensions : list of int or None, default: ``None``
        Homology dimensions which will appear on the plot. If ``None``, all
        homology dimensions which appear in `diagram` will be plotted.

    """
    if homology_dimensions is None:
        homology_dimensions = np.unique(diagram[:, 2])

    maximum_persistence = np.where(np.isinf(diagram), -np.inf, diagram).max()

    plt.plot(np.array([-1 * maximum_persistence,
                            1 * maximum_persistence]),
            np.array([-1 * maximum_persistence,
                            1 * maximum_persistence]),
             'bo-')

    colors = ['r', 'C2', 'c']

    for i, dimension in enumerate(homology_dimensions):
        name = "H{}".format(int(dimension))
        subdiagram = _subdiagrams(np.asarray([diagram]), [dimension],
                                  remove_dim=True)[0]
        diff = (subdiagram[:, 1] != subdiagram[:, 0])
        subdiagram = subdiagram[diff]
        plt.scatter(subdiagram[:, 0], subdiagram[:, 1], c=colors[i])

    plt.show()
Example #2
0
def plot_diagram(diagram, homology_dimensions=None):
    """Plot a single persistence diagram.

    Parameters
    ----------
    diagram : ndarray, shape (n_points, 3)
        The persistence diagram to plot, where the third dimension along axis 1
        contains homology dimensions, and the other two contain (birth, death)
        pairs to be used as coordinates in the two-dimensional plot.

    homology_dimensions : list of int or None, default: ``None``
        Homology dimensions which will appear on the plot. If ``None``, all
        homology dimensions which appear in `diagram` will be plotted.

    """
    if homology_dimensions is None:
        homology_dimensions = np.unique(diagram[:, 2])

    maximum_persistence = np.where(np.isinf(diagram), -np.inf, diagram).max()

    layout = {
        "title": "Persistence diagram",
        "width": 500,
        "height": 500,
        "xaxis1": {
            "title": "Birth",
            "side": "bottom",
            "type": "linear",
            "range": [0, 1.1 * maximum_persistence],
            "ticks": "outside",
            "anchor": "y1",
            "showline": True,
            "zeroline": True,
            "showexponent": "all",
            "exponentformat": "e",
        },
        "yaxis1": {
            "title": "Death",
            "side": "left",
            "type": "linear",
            "range": [0, 1.1 * maximum_persistence],
            "ticks": "outside",
            "anchor": "x1",
            "showline": True,
            "zeroline": True,
            "showexponent": "all",
            "exponentformat": "e",
        },
        "plot_bgcolor": "white",
    }

    fig = gobj.Figure(layout=layout)
    fig.update_xaxes(zeroline=True,
                     linewidth=1,
                     linecolor="black",
                     mirror=False)
    fig.update_yaxes(zeroline=True,
                     linewidth=1,
                     linecolor="black",
                     mirror=False)

    fig.add_trace(
        gobj.Scatter(
            x=np.array([-100 * maximum_persistence,
                        100 * maximum_persistence]),
            y=np.array([-100 * maximum_persistence,
                        100 * maximum_persistence]),
            mode="lines",
            line=dict(dash="dash", width=1, color="black"),
            showlegend=False,
            hoverinfo="none",
        ))

    for i, dimension in enumerate(homology_dimensions):
        name = "H{}".format(int(dimension))
        subdiagram = _subdiagrams(np.asarray([diagram]), [dimension],
                                  remove_dim=True)[0]
        diff = subdiagram[:, 1] != subdiagram[:, 0]
        subdiagram = subdiagram[diff]
        fig.add_trace(
            gobj.Scatter(x=subdiagram[:, 0],
                         y=subdiagram[:, 1],
                         mode="markers",
                         name=name))

    fig.show()
Example #3
0
 def feature_5(X, dim):
     X = _subdiagrams(X, [dim])
     return np.max(yi_minus_xi(X), axis=1) / X.shape[1]
Example #4
0
 def feature_4(X, dim):
     X = _subdiagrams(X, [dim])
     return np.sum(((np.max(X[:, :, 1], axis=1).reshape((X.shape[0], 1)) - X[:, :, 1]) ** 2) * (yi_minus_xi(X) ** 4),
                   axis=1) / X.shape[1]
Example #5
0
 def feature_3(X, dim):
     X = _subdiagrams(X, [dim])
     return np.sum(((X[:, :, 0] ** 2) * (yi_minus_xi(X) ** 4)), axis=1) / X.shape[1]
Example #6
0
def plot_diagram(diagram, homology_dimensions=None):
    """Plots one persistence diagram.

    Parameters
    ----------
    homology_dimensions : list of int, default: ``None``
        The list of homology dimensions that will appear on the plot. None means that all the homology dimensions
        contained in diagram will be plotted.
        
    Input
    -----
    diagram : ndarray of shape (n_points, 3)
    The persistence diagram to plot, where the keys of the dict correspond to homology dimensions and each
    entry is the collection of (birth,death) points in R^2 of the corresponding homology dimension.
    """
    if homology_dimensions is None:
        homology_dimensions = np.unique(diagram[:,2])
    
    maximum_persistence = np.where(np.isinf(diagram),-np.inf,diagram).max()

    layout = {
        "title": "Persistence diagram", 
        "width": 500,
        "height": 500,
        "xaxis1": {
            "title": "Birth",
            "side": "bottom", 
            "type": "linear", 
            "range": [0, 1.1*maximum_persistence], 
            "ticks": "outside", 
            "anchor": "y1",  
            "showline": True, 
            "zeroline": True,
            "showexponent": "all",
            "exponentformat" : "e"
        }, 
        "yaxis1": {
            "title": "Death",
            "side": "left", 
            "type": "linear", 
            "range": [0, 1.1*maximum_persistence], 
            "ticks": "outside", 
            "anchor": "x1",  
            "showline": True, 
            "zeroline": True,
            "showexponent": "all",
            "exponentformat" : "e"
        }, 
        "plot_bgcolor": "white"
    }

    fig = gobj.Figure(layout=layout)
    fig.update_xaxes(zeroline=True, linewidth=1, linecolor='black', mirror=False)
    fig.update_yaxes(zeroline=True, linewidth=1, linecolor='black', mirror=False)

    fig.add_trace(gobj.Scatter(x=np.array([-100*maximum_persistence,100*maximum_persistence]),
    y=np.array([-100*maximum_persistence,100*maximum_persistence]), mode='lines',
    line=dict(dash='dash',width=1,color='black'), showlegend=False, hoverinfo='none'))
    
    for i, dimension in enumerate(homology_dimensions):
        name = 'H'+str(int(dimension))
        subdiagram = _subdiagrams(np.asarray([diagram]),[dimension],remove_dim=True)[0]
        diff = (subdiagram[:, 1] != subdiagram[:, 0])
        subdiagram = subdiagram[diff]
        fig.add_trace(gobj.Scatter(x=subdiagram[:,0], y=subdiagram[:,1], mode='markers', name=name))

    fig.show()