From 9d7ce625b42af8d16f5af26a17a2f1dce3e82aea Mon Sep 17 00:00:00 2001 From: Umberto Lupo <46537483+ulupo@users.noreply.github.com> Date: Tue, 25 Aug 2020 08:43:01 +0200 Subject: [PATCH] Fix handling of COO matrices by ripser mirroring https://github.com/scikit-tda/ripser.py/pull/104 (#465) --- gtda/externals/python/ripser_interface.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/gtda/externals/python/ripser_interface.py b/gtda/externals/python/ripser_interface.py index 1ec65e411..92904699c 100644 --- a/gtda/externals/python/ripser_interface.py +++ b/gtda/externals/python/ripser_interface.py @@ -207,16 +207,27 @@ def ripser(X, maxdim=1, thresh=np.inf, coeff=2, metric="euclidean", dm = sparse.coo_matrix(dm) if sparse.issparse(dm): - coo = dm.tocoo() + if sparse.isspmatrix_coo(dm): + # If the matrix is already COO, we need to order the row and column + # indices lexicographically to avoid errors. See + # https://github.com/scikit-tda/ripser.py/issues/103 + row, col, data = dm.row, dm.col, dm.data + lex_sort_idx = np.lexsort((col, row)) + row, col, data = \ + row[lex_sort_idx], col[lex_sort_idx], data[lex_sort_idx] + else: + # Lexicographic sorting is performed by scipy upon conversion + coo = dm.tocoo() + row, col, data = coo.row, coo.col, coo.data res = DRFDMSparse( - coo.row.astype(dtype=np.int32, order="C"), - coo.col.astype(dtype=np.int32, order="C"), - np.array(coo.data, dtype=np.float32, order="C"), + row.astype(dtype=np.int32, order="C"), + col.astype(dtype=np.int32, order="C"), + np.array(data, dtype=np.float32, order="C"), n_points, maxdim, thresh, coeff, - ) + ) else: I, J = np.meshgrid(np.arange(n_points), np.arange(n_points)) DParam = np.array(dm[I > J], dtype=np.float32)