msmbuilder / msmexplorer

Data visualizations for biomolecular dynamics
http://msmbuilder.org/msmexplorer/
MIT License
17 stars 17 forks source link

Use of undirected graph in plot_msm_network #110

Open jeiros opened 6 years ago

jeiros commented 6 years ago

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.

msultan commented 6 years ago

@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).

jeiros commented 6 years ago

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).

cxhernandez commented 6 years ago

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.

jeiros commented 6 years ago

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.