Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add bindings for collapser #469

Merged
merged 27 commits into from Sep 8, 2020
Merged
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
05b99fd
[WIP] Add bindings for collapser
Aug 28, 2020
23dc59d
Attempt fix of collapser bindings
ulupo Aug 29, 2020
88bfbfc
Split flag_complex_collapse_edges into a _sparse and a _dense version
ulupo Aug 30, 2020
53f9c0e
Clean Cech test docstring
ulupo Aug 30, 2020
6141e69
Linting in ripser_interface
ulupo Aug 30, 2020
085d9b9
Make more collapser bindings which compile
ulupo Aug 30, 2020
55d8c58
Add collapse_edges kwarg to ripser to enable edge collapser
ulupo Aug 30, 2020
0b6663c
Update collapser bindings
Aug 31, 2020
ce7f513
Add test for the collapser bindings
Aug 31, 2020
0365642
Update azure-pipeline ccache to allow building them again
Aug 31, 2020
57abf4f
Update bindings names and add threshold parameter
Sep 1, 2020
50a9948
Remove unused function from bindings
Sep 1, 2020
c375597
Refactor ripser_interface
Sep 1, 2020
ead89ed
Add test cases for ripser bindings with collapser
Sep 1, 2020
ca01386
Remove unnecessary backslash
ulupo Sep 1, 2020
4fb660a
Simplify code for upper diagonal extraction in dense case
ulupo Sep 2, 2020
533aba7
Add Flag to indicate when to sort data before computing persistent ho…
Sep 3, 2020
b4b9404
Fix test for ripser
Sep 3, 2020
f35c820
Add threshold directly into the collapser
Sep 3, 2020
071b168
Add references to edge collapse paper
ulupo Sep 7, 2020
b87897e
Guess number of points using max of shape tuple instead of first shape
ulupo Sep 7, 2020
dc083ba
Simplify lexsort flag logic
ulupo Sep 7, 2020
fe30c82
Small refactor of ripser interface to restrict collapse to data witho…
ulupo Sep 7, 2020
701f6f9
Add hypothesis tests for collapser
ulupo Sep 7, 2020
0565a38
Place hypothesis tests in test_ripser and remove superseded tests the…
ulupo Sep 8, 2020
4f91a1b
Fix array shape
ulupo Sep 8, 2020
2e9e574
Disable computation of cocycles by default
Sep 8, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
74 changes: 28 additions & 46 deletions gtda/externals/python/ripser_interface.py
Expand Up @@ -213,29 +213,30 @@ def ripser(X, maxdim=1, thresh=np.inf, coeff=2, metric="euclidean",
# births
dm = sparse.coo_matrix(dm)

if sparse.issparse(dm):
if sparse.isspmatrix_coo(dm) and not collapse_edges:
# 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 = _lexsort_coo_data(dm.row, dm.col, dm.data)
else:
# Lexicographic sorting is performed by scipy upon conversion
coo = dm.tocoo()
row, col, data = coo.row, coo.col, coo.data
if sparse.issparse(dm) or collapse_edges:
if collapse_edges:
# Threshold first. Ideally, this would be implemented as a
# modification of the C++ code
if thresh < np.inf:
thresholded_idx = np.flatnonzero(data <= thresh)
data = data[thresholded_idx]
row = row[thresholded_idx]
col = col[thresholded_idx]
row, col, data = \
gtda_collapser.flag_complex_collapse_edges_from_coo_data_to_coo_data(row, col, data)
row, col, data = _lexsort_coo_data(
np.asarray(row), np.asarray(col), np.asarray(data)
)
if not sparse.issparse(dm):
row, col, data = \
gtda_collapser.flag_complex_collapse_edges_dense(dm)
else:
coo = dm.tocoo()
row, col, data = \
gtda_collapser.flag_complex_collapse_edges_coo(coo.row,
coo.col,
coo.data)
else:
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 = _lexsort_coo_data(dm.row, dm.col, dm.data)
ulupo marked this conversation as resolved.
Show resolved Hide resolved
else:
coo = dm.tocoo()
row, col, data = coo.row, coo.col, coo.data
ulupo marked this conversation as resolved.
Show resolved Hide resolved

row, col, data = _lexsort_coo_data(np.asarray(row),
np.asarray(col),
np.asarray(data))

res = DRFDMSparse(
row.astype(dtype=np.int32, order="C"),
Expand All @@ -247,30 +248,11 @@ def ripser(X, maxdim=1, thresh=np.inf, coeff=2, metric="euclidean",
coeff
)
else:
ulupo marked this conversation as resolved.
Show resolved Hide resolved
if collapse_edges:
# Threshold first. Ideally, this would be implemented as a
# modification of the C++ code
if thresh < np.inf:
dm = np.where(dm <= thresh, dm, np.inf)
row, col, data = \
gtda_collapser.flag_complex_collapse_edges_from_dense_to_coo_data(dm)
row, col, data = \
_lexsort_coo_data(np.asarray(row), np.asarray(col),
np.asarray(data))
res = DRFDMSparse(
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:
# Only consider upper diagonal
I, J = np.meshgrid(np.arange(n_points), np.arange(n_points))
DParam = np.array(dm[I > J], dtype=np.float32)
res = DRFDM(DParam, maxdim, thresh, coeff)
# Only consider upper diagonal
I, J = np.meshgrid(np.arange(n_points), np.arange(n_points))
DParam = np.array(dm[I > J], dtype=np.float32)
print(DParam)
res = DRFDM(DParam, maxdim, thresh, coeff)

# Unwrap persistence diagrams
dgms = res["births_and_deaths_by_dim"]
Expand Down