msmexplorer icon indicating copy to clipboard operation
msmexplorer copied to clipboard

Use of undirected graph in plot_msm_network

Open jeiros opened this issue 8 years ago • 4 comments

Is there a reason why an undirected graph is built from the transition matrix in the plot_msm_network function?

I've checked and the edges that are built when using an undirected graph do not match the entries of the transition matrix:

import networkx as nx
import numpy as np
tmat = np.array(
    [
        [0.8, 0.1, 0.1],
        [0.3, 0.6, 0.1],
        [0.0, 0.3, 0.7]
    ]
)
graph_di = nx.DiGraph(tmat)
graph_un = nx.Graph(tmat)
tot_un = 0
for v in graph_un.edge[0].values():
    tot_un += v['weight']
tot_di = 0
for v in graph_di.edge[0].values():
    tot_di += v['weight']
assert tot_di == tmat[0, :].sum()
assert tot_un == tmat[0, :].sum()

Traceback (most recent call last):
  File "/Users/je714/test_issue_tmat.py", line 19, in <module>
    assert tot_un == tmat[0, :].sum()
AssertionError
print(graph_di.edge)
{0: {0: {'weight': 0.8}, 1: {'weight': 0.1}, 2: {'weight': 0.1}},
 1: {0: {'weight': 0.3}, 1: {'weight': 0.6}, 2: {'weight': 0.1}},
 2: {1: {'weight': 0.3}, 2: {'weight': 0.7}}}
print(graph_un.edge)
{0: {0: {'weight': 0.8}, 1: {'weight': 0.3}, 2: {'weight': 0.1}},
 1: {0: {'weight': 0.3}, 1: {'weight': 0.6}, 2: {'weight': 0.3}},
 2: {0: {'weight': 0.1}, 1: {'weight': 0.3}, 2: {'weight': 0.7}}}

Also, as a side question: is there a way to hide the edges below a particular weight? When there are too many connections, the resulting plot is really crowded and is a bit confusing to look at.

jeiros avatar Sep 26 '17 09:09 jeiros

@cxhernandez can speak to this more but my guess is that a directed graph would double the number of edges and would likely look very messy.

For your side question, I dont think we have anything for that yet but I agree it would be a good idea. As a fast hack, you can clip the input transition matrix to be 0 below a certain threshold. Or maybe even feed in the clipped version of log10(matrix).

msultan avatar Sep 26 '17 15:09 msultan

Thanks for replying, I'll try the numpy clip!

As for the undirected/directed, I just noticed it because I didn't see any self-connections in the node, which in turn are usually the biggest weight nodes for each microstate (at least in my simulations, that's usually the case).

jeiros avatar Sep 26 '17 17:09 jeiros

Yeah, it was to cut down on the complexity of the visualization because networkx makes ugly graphs. I'm very open to moving away from networkx plots and use something nicer.

cxhernandez avatar Sep 26 '17 19:09 cxhernandez

That's good to know then, I was just wondering if it was a small bug. I don't really know nice network plotting libraries for python, for the moment what I'm doing is saving the directed graph to disk with networkx and then inspecting it using Gephi or Cytoscape.

jeiros avatar Sep 27 '17 08:09 jeiros