GuyAllard / markov_clustering

markov clustering in python
MIT License
167 stars 37 forks source link

markov_cluster.modularity errors when 1st arg is an np.ndarray #26

Open Fish-Soup opened 4 years ago

Fish-Soup commented 4 years ago
result = mc.run_mcl(score, **kwargs)
clusters = mc.get_clusters(result) 
modularity = mc.modularity(matrix=result, clusters=clusters)

as score is a np.ndarray (np.matrix is depreciated and data is not sparse) result is also a ndarray. This causes mc.modularity to crash on the function call convert_to_adjacency_matrix().

I found a quick fix which was to

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

however in cases where some of the data doesnt belong to any clusters, this crashes. The code functions correctly if modularity = mc.modularity(matrix=np.matrix(result), clusters=clusters) however np.matrix is depreciated.

the fix is fairly trivial

 if isspmatrix(matrix):
            col = find(matrix[:,i])[2]
        else:
            col = matrix[:,i].T.tolist()[0]

becomes

if isspmatrix(matrix):
    col = find(matrix[:, i])[2]
elif isinstance(matrix, np.ndarray):
    col =  matrix[:, i].T.tolist()
else:
    col = matrix[:, i].T.tolist()[0]

Could raise a PR but its only 2 lines of code, not sure whats easier.

Many thanks