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

markov_clustering, possible error in instructions for computing of modularity Q #24

Open
paolotgithub opened this issue May 25, 2020 · 3 comments

Comments

@paolotgithub
Copy link

In calculating modularity Q I get the following error in my script that uses the markov_clustering package:

Traceback (most recent call last):
  File "mcl_3_inflat_test.py", line 48, in <module>
    Q = mc.modularity(matrix=result, clusters=clusters)
  File "/usr/local/lib/python3.7/site-packages/markov_clustering/modularity.py", line 74, in modularity
    matrix = convert_to_adjacency_matrix(matrix)
  File "/usr/local/lib/python3.7/site-packages/markov_clustering/modularity.py", line 39, in convert_to_adjacency_matrix
    coeff = max( Fraction(c).limit_denominator().denominator for c in col )
TypeError: 'float' object is not iterable

Shouldn't

Q = mc.modularity(matrix=result, clusters=clusters)

as it reads in the instructions be instead:

Q = mc.modularity(matrix, clusters=clusters) ?

Or otherwise...?

Thanks a lot.

@paolotgithub paolotgithub changed the title Possible error in instructions for markov_clustering, possible error in instructions for computing of modularity Q May 25, 2020
@grgmiller
Copy link

I also experienced this issue when running

for inflation in [i / 10 for i in range(15, 26)]:
    result = mc.run_mcl(matrix, inflation=inflation)
    clusters = mc.get_clusters(result)
    Q = mc.modularity(matrix=result, clusters=clusters)
    print("inflation:", inflation, "modularity:", Q)

If you remove the [0] from the following line, the code seems to work, but I'm not sure if that is functionally correct (i.e. if you're supposed to be plugging int the result matrix or the original matrix as @plttvmt suggested)

col = matrix[:,i].T.tolist()[0]

@BerkeAltiparmak
Copy link

Problem

I've noticed that this issue still exists when matrix is a numpy array, so I proceeded to confirm whether @grgmiller 's approach is functionally correct, as he shared his concerns. Here is my procedure to confirm it.

Procedure

  1. First, create a new file called modularity.py (or whatever name you want to give to it).
  2. Copy-paste the contents of the modularity.py file from https://github.com/GuyAllard/markov_clustering/blob/28787cf64ef06bf024ff915246008c767ea830cf/markov_clustering/modularity.py
  3. Make the changes as proposed by @grgmiller, which is removing [0] from the line col = matrix[:,i].T.tolist()[0]
  4. Create a new file to test if this approach actually works.
  5. You can use this code to test the approach:

Screen Shot 2022-11-27 at 20 36 32

6. You can compare the outputs and notice that the one that uses the original approach as a sparse matrix gives approximately equal result to this approach that works with numpy arrays.

Screen Shot 2022-11-27 at 20 38 12

Conclusion

The solution provided by @grgmiller works for similarity matrices that are of numpy arrays type.

@cpouchon
Copy link

cpouchon commented Feb 5, 2024

Dear all,
how can you import modularity from your modularity.py file ?
I give this error:
7 from itertools import permutations
9 from scipy.sparse import isspmatrix, dok_matrix, find
---> 10 from .mcl import sparse_allclose
12 def is_undirected(matrix):
13 """
14 Determine if the matrix reprensents a directed graph
15
16 :param matrix: The matrix to tested
17 :returns: boolean
18 """

ImportError: attempted relative import with no known parent package

Thank you,
best regards
Charles

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

No branches or pull requests

4 participants