Skip to content

Commit

Permalink
Fix networkx export of nodes without links
Browse files Browse the repository at this point in the history
Fix networkx export of nodes without links. The creation of the DiGraph
object was done by interating over links in the results. This did not
pick up nodes with no inferred links.

Fixes #50.
  • Loading branch information
pwollstadt committed Feb 25, 2021
1 parent 9980e17 commit 09bf21f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions idtxl/idtxl_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ class for directed graphs (DiGraph). Multiple options for the weight are
# use 'weights' parameter (string) as networkx edge property name and use
# adjacency matrix entries as edge property values
G = nx.DiGraph()
G.add_nodes_from(np.arange(adjacency_matrix.n_nodes()))
G.add_weighted_edges_from(adjacency_matrix.get_edge_list(), weights)
return G

Expand Down
29 changes: 29 additions & 0 deletions test/test_idtxl_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from idtxl.network_comparison import NetworkComparison
from idtxl.multivariate_te import MultivariateTE

SEED = 0

# Generate data and load network inference results.
n_nodes = 5
data_0 = Data()
Expand Down Expand Up @@ -42,6 +44,33 @@

def test_export_networkx():
"""Test export to networkx DiGrap() object."""
# Test export of graph with unconnected nodes.
max_lag = 3
data = Data(seed=SEED)
data.generate_mute_data(500, 5)
settings = {
'cmi_estimator': 'JidtKraskovCMI',
'noise_level': 0,
'n_perm_max_stat': 21,
'n_perm_min_stat': 21,
'n_perm_max_seq': 21,
'n_perm_omnibus': 21,
'max_lag_sources': max_lag,
'min_lag_sources': 1,
'max_lag_target': max_lag}
target = 3
sources = [0, 4]
te = MultivariateTE()
results = te.analyse_single_target(
settings, data, target=target, sources=sources)
weights = 'binary'
adj_matrix = results.get_adjacency_matrix(weights=weights, fdr=False)
digraph = io.export_networkx_graph(
adjacency_matrix=adj_matrix, weights=weights)
np.testing.assert_array_equal(
np.sort(digraph.nodes), np.arange(data.n_processes),
err_msg='Wrong nodes in exported DiGraph.')

# raise AssertionError('Test not yet implemented.')
# Test export of networx graph for network inference results.
weights = 'binary'
Expand Down

0 comments on commit 09bf21f

Please sign in to comment.