mrzv / dionysus

Library for computing persistent homology
http://mrzv.org/software/dionysus2
Other
144 stars 31 forks source link

fill_rips() works strange when using condensed distance matrices #51

Closed Chunhui-Shao closed 2 years ago

Chunhui-Shao commented 2 years ago

I am testing my code using a toy data looks like below: C{ W`%(C6}ZQR7_P T~9$T4 Its adjacency matrix is: ,1,,1,,,,,,,,, 1,,1,,1,,,,,,,, ,1,,,,1,,,,,,, 1,,,,1,,1,1,,,,, ,1,,1,,1,,,,,,, ,,1,,1,,,,2,,,, ,,,1,,,,1,,,,, ,,,1,,,1,,1,1,1,,1 ,,,,,2,,1,,1,1,1, ,,,,,,,1,1,,,1,1 ,,,,,,,1,1,,,3,1 ,,,,,,,,1,1,3,,1 ,,,,,,,1,,1,1,1, then I take its reciprocal as distance matrix. Then I transformed it to a condensed distance matrix and if use fill_rips(cdmat, 3, 1.1) obviously it will have 9 2-simplexes, but actually it returns only 7, which means that the topology of the network have changed, so strange. Can I know if I did something wrong?

mrzv commented 2 years ago

It's difficult to tell what's going on without seeing the code.

Chunhui-Shao commented 2 years ago

Here's the code, thanks.

import numpy as np
import dionysus as d

dist_mat = np.array([[np.inf,1.,np.inf,1.,np.inf,np.inf,np.inf,np.inf,np.inf,np.inf,np.inf,np.inf,np.inf],
[1.,np.inf,1.,np.inf,1.,np.inf,np.inf,np.inf,np.inf,np.inf,np.inf,np.inf,np.inf],
[np.inf,1.,np.inf,np.inf,np.inf,1.,np.inf,np.inf,np.inf,np.inf,np.inf,np.inf,np.inf],
[1.,np.inf,np.inf,np.inf,1.,np.inf,1.,1.,np.inf,np.inf,np.inf,np.inf,np.inf],
[np.inf,1.,np.inf,1.,np.inf,1.,np.inf,np.inf,np.inf,np.inf,np.inf,np.inf,np.inf],
[np.inf,np.inf,1.,np.inf,1.,np.inf,np.inf,np.inf,0.5,np.inf,np.inf,np.inf,np.inf],
[np.inf,np.inf,np.inf,1.,np.inf,np.inf,np.inf,1.,np.inf,np.inf,np.inf,np.inf,np.inf],
[np.inf,np.inf,np.inf,1.,np.inf,np.inf,1.,np.inf,1.,1.,1.,np.inf,1.],
[np.inf,np.inf,np.inf,np.inf,np.inf,0.5,np.inf,1.,np.inf,1.,1.,1.,np.inf],
[np.inf,np.inf,np.inf,np.inf,np.inf,np.inf,np.inf,1.,1.,np.inf,np.inf,1.,1.],
[np.inf,np.inf,np.inf,np.inf,np.inf,np.inf,np.inf,1.,1.,np.inf,np.inf,0.33333333,1.],
[np.inf,np.inf,np.inf,np.inf,np.inf,np.inf,np.inf,np.inf,1.,1.,0.33333333,np.inf,1.],
[np.inf,np.inf,np.inf,np.inf,np.inf,np.inf,np.inf,1.,np.inf,1.,1.,1.,np.inf]])

lower_tri_dist_cdmat = dist_mat[np.tril_indices(dist_mat.shape[0], k=-1)]

f = d.fill_rips(lower_tri_dist_cdmat, 3, 1.1)

for s in f:
    print(s)
Chunhui-Shao commented 2 years ago

and it's the result:

<0> 0
<1> 0
<2> 0
<3> 0
<4> 0
<5> 0
<6> 0
<7> 0
<8> 0
<9> 0
<10> 0
<11> 0
<12> 0
<7,10> 0.333333
<3,4> 0.5
<0,1> 1
<0,3> 1
<0,4> 1
<0,8> 1
<0,10> 1
<1,2> 1
<1,4> 1
<1,8> 1
<2,4> 1
<2,7> 1
<3,6> 1
<4,6> 1
<4,7> 1
<5,8> 1
<5,9> 1
<7,8> 1
<7,9> 1
<9,11> 1
<10,11> 1
<10,12> 1
<11,12> 1
<0,1,4> 1
<0,1,8> 1
<0,3,4> 1
<1,2,4> 1
<2,4,7> 1
<3,4,6> 1
<10,11,12> 1
mrzv commented 2 years ago

I don't know what tril_indices does or why you are using it, but it doesn't produce the same output as squareform, given in the documentation.

Chunhui-Shao commented 2 years ago

I know what's going wrong, I didn't noticed the function squareform but only saw

fill_rips() also accepts condensed distance matrices (linearized lower triangular part of a symmetric matrix)

then I transfered my matrix manually to the linearized lower triangular matrix. Actually condenced distance matrix should be linearized upper triangular matrix(row by row).

Thanks a lot!

mrzv commented 2 years ago

Ah, I see, it's the column vs row confusion. But sounds like it's been resolved.