Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Overflow error with maxdim>0 #120

Open
pierre-guillou opened this issue Apr 28, 2021 · 3 comments
Open

Overflow error with maxdim>0 #120

pierre-guillou opened this issue Apr 28, 2021 · 3 comments

Comments

@pierre-guillou
Copy link

Hello and thank you for your work!

I am trying to use ripser to compute the persistence diagrams of 3D images. Looking at the Python code in ripser/ripser.py and especially at the ripser.lower_star_img function, I have been able to feed ripser distance matrices corresponding to my datasets. However, when I call the ripser.ripser function with maxdim=1 or above (ideally 2), the program aborts with an overflow error.

This problem also occurs using the Cell.jpg 2D image from the Lower Star Image Filtrations tutorial. When I use maxdim=1 when calling ripser.ripser in the ripser.lower_star_img function (or remove it since the default value is 1), the overflow exception happens here too.

If I understand correctly the underlaying C++ code in ripser/ripser.cpp, this overflow exception is caused by the computation of binomial coefficients that depend on the size of the distance matrix and the maxdim parameter.

Is this an inherent limitation of ripser? What's the right way to compute higher dimension persistence pairs?

Thank you in advance for your help,
Best regards,
Pierre

@sauln
Copy link
Member

sauln commented Apr 28, 2021

This is concerning. Thanks for notifying us. I'll try to take a look within the next week, but am totally swamped with work, so no guarantees. In the meantime if you're able to find the bug and fix, a PR would be immensely appreciated.

@ctralie
Copy link
Member

ctralie commented Apr 28, 2021 via email

@pierre-guillou
Copy link
Author

Thank you for answering me about this issue. For reference, here is the Python script I used to trigger this overflow (the Cells.jpg is from here):

import numpy as np
import PIL.Image
from ripser import ripser
from scipy import sparse


def lower_star_img(img):
    m, n = img.shape

    idxs = np.arange(m * n).reshape((m, n))

    I = idxs.flatten()
    J = idxs.flatten()
    V = img.flatten()

    # Connect 8 spatial neighbors
    tidxs = np.ones((m + 2, n + 2), dtype=np.int64) * np.nan
    tidxs[1:-1, 1:-1] = idxs

    tD = np.ones_like(tidxs) * np.nan
    tD[1:-1, 1:-1] = img

    for di in [-1, 0, 1]:
        for dj in [-1, 0, 1]:

            if di == 0 and dj == 0:
                continue

            thisJ = np.roll(np.roll(tidxs, di, axis=0), dj, axis=1)
            thisD = np.roll(np.roll(tD, di, axis=0), dj, axis=1)
            thisD = np.maximum(thisD, tD)

            # Deal with boundaries
            boundary = ~np.isnan(thisD)
            thisI = tidxs[boundary]
            thisJ = thisJ[boundary]
            thisD = thisD[boundary]

            I = np.concatenate((I, thisI.flatten()))
            J = np.concatenate((J, thisJ.flatten()))
            V = np.concatenate((V, thisD.flatten()))

    sparseDM = sparse.coo_matrix((V, (I, J)), shape=(idxs.size, idxs.size))

    return ripser(sparseDM, distance_matrix=True, maxdim=1)["dgms"][0]


if __name__ == "__main__":
    cells_grey = np.asarray(PIL.Image.open("Cells.jpg").convert("L"))
    dgm = lower_star_img(-cells_grey)
    print(dgm)

I copied the lower_star_img method from ripser.py and used maxdim=1 instead of 0. The rest comes from the Lower Star Image Filtrations tutorial.

In the meantime, I've looked into the C++ code and I'm trying to check if raising max_simplex_index to std::numeric_limits<index_t>::max() (

static const index_t max_simplex_index =
) might be enough.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants