Giter Site home page Giter Site logo

Sparse Cocycle Issue about ripser.py HOT 3 CLOSED

scikit-tda avatar scikit-tda commented on May 21, 2024
Sparse Cocycle Issue

from ripser.py.

Comments (3)

ctralie avatar ctralie commented on May 21, 2024

@ubauer
Hi Uli,
I have pasted some code below that I use to test the cocycles. First, here's how it should look on this example, and how it looks if you compile what's on master (python setup.py install). You can visually verify the cocycle condition below. Vertex indices are plotted in red, cocycle values in blue. Orientation goes from white to black along the edge. In the list below, I show what gets returned from the program, which is vertex index 1, vertex index 2, and cocycle value in each row

[[11 2 16]
[ 4 1 1]
[ 3 1 1]
[ 3 0 1]
[ 2 1 1]
[ 2 0 1]]

cocycleworking

Here's what happens when you checkout the sparsedm branch. Notice how some of the simplices have more than one, conflicting value. For instance, edge 2->1 has both 1, 2, and 0

[[2 1 1]
[2 1 2]
[0 1 2]
[1 2 0]
[3 1 1]
[2 1 2]]

cocyclebug

import numpy as np
import matplotlib.pyplot as plt
from ripser import Rips

def drawLineColored(X, C):
    for i in range(X.shape[0]-1):
        plt.plot(X[i:i+2, 0], X[i:i+2, 1], c=C[i, :], lineWidth = 3)

def plotCocycle2D(r, X, cocycle, thresh):
    """
    Given a 2D point cloud X, display a cocycle projected
    onto edges under a given threshold "thresh"
    """
    D = r.dm_
    #Plot all edges under the threshold
    N = X.shape[0]
    t = np.linspace(0, 1, 10)
    c = plt.get_cmap('Greys')
    C = c(np.array(np.round(np.linspace(0, 255, len(t))), dtype=np.int32))
    C = C[:, 0:3]

    for i in range(N):
        for j in range(N):
            if D[i, j] <= thresh:
                Y = np.zeros((len(t), 2))
                Y[:, 0] = X[i, 0] + t*(X[j, 0] - X[i, 0])
                Y[:, 1] = X[i, 1] + t*(X[j, 1] - X[i, 1])
                drawLineColored(Y, C)
    #Plot cocycle projected to edges under the chosen threshold
    for k in range(cocycle.shape[0]):
        [i, j, val] = cocycle[k, :]
        if D[i, j] <= thresh:
            [i, j] = [min(i, j), max(i, j)]
            a = 0.5*(X[i, :] + X[j, :])
            plt.text(a[0], a[1], '%g'%val, color='b')
    #Plot vertex labels
    for i in range(N):
        plt.text(X[i, 0], X[i, 1], '%i'%i, color='r')
    plt.axis('equal')

def testCocycle():
    #Setup noisy circle with 12 points
    np.random.seed(5)
    n = 12
    t = np.linspace(0, 2*np.pi, n+1)[0:n]
    x = np.zeros((n, 2))
    x[:, 0] = np.cos(t)
    x[:, 1] = np.sin(t)
    x += 0.1*np.random.randn(n, 2)

    r = Rips(coeff=17, do_cocycles=True)
    diagrams = r.fit_transform(x)
    
    plt.figure(figsize=(12, 6))
    #Extract the index of the most persistence point in H1
    dgm1 = diagrams[1]
    idx = np.argmax(dgm1[:, 1] - dgm1[:, 0])
    plt.subplot(121)
    r.plot(diagrams, plot_only = [1], show = False)
    #Store cocycle from class we chose
    cocycle = r.cocycles_[1][idx]
    print(cocycle)
    #Project cocycle onto edges less slightly less than the death time and plot
    thresh = dgm1[idx, 1]-0.00001 
    plt.subplot(122)
    plotCocycle2D(r, x, cocycle, thresh)
    plt.title("1-Form Thresh=%g"%thresh)
    plt.show()

testCocycle()

from ripser.py.

ctralie avatar ctralie commented on May 21, 2024

Okay, I have gotten to the point with 631fe33 where the above example is fixed, but I'm finding with larger examples (200 points) that sometimes the code freezes when trying to pop off elements from the working_reduction_column (this doesn't happen in master). @ubauer I noticed you merged together sparse distance matrices and cocycles in
https://github.com/Ripser/ripser/tree/representative-cocycles
Is that branch stable with both sparse matrix support and popping off the representative cocycles? If so, I think I'm going to do my edits over again starting with this branch

from ripser.py.

ubauer avatar ubauer commented on May 21, 2024

Hi Chris,

that branch has both sparse matrix support and representative cocycles. I wouldn't call it stable, I haven't done extensive testing on it yet, but it's the best place to start.

from ripser.py.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.