Skip to content

adding import error in cmtk.nbs and imprve KeyError when specific att… #2243

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

Merged
merged 1 commit into from
Oct 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion nipype/interfaces/cmtk/nbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ def ntwks_to_matrices(in_files, edge_key):
for idx, name in enumerate(in_files):
graph = nx.read_gpickle(name)
for u, v, d in graph.edges(data=True):
graph[u][v]['weight'] = d[edge_key] # Setting the edge requested edge value as weight value
try:
graph[u][v]['weight'] = d[edge_key] # Setting the edge requested edge value as weight value
except:
raise KeyError("the graph edges do not have {} attribute".format(edge_key))
matrix[:, :, idx] = nx.to_numpy_matrix(graph) # Retrieve the matrix
return matrix

Expand Down Expand Up @@ -77,6 +80,10 @@ class NetworkBasedStatistic(BaseInterface):
output_spec = NetworkBasedStatisticOutputSpec

def _run_interface(self, runtime):

if not have_cv:
raise ImportError("cviewer library is not available")

THRESH = self.inputs.threshold
K = self.inputs.number_of_permutations
TAIL = self.inputs.t_tail
Expand Down
58 changes: 58 additions & 0 deletions nipype/interfaces/cmtk/tests/test_nbs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from __future__ import unicode_literals
from ..nbs import NetworkBasedStatistic
from ....utils.misc import package_check
import numpy as np
import networkx as nx
import pytest

have_cv = True
try:
package_check('cviewer')
except Exception as e:
have_cv = False

@pytest.fixture()
def creating_graphs(tmpdir):
graphlist = []
graphnames = ["name"+str(i) for i in range(6)]
for idx, name in enumerate(graphnames):
graph = np.random.rand(10,10)
G = nx.from_numpy_matrix(graph)
out_file = str(tmpdir) + graphnames[idx] + '.pck'
# Save as pck file
nx.write_gpickle(G, out_file)
graphlist.append(out_file)
return graphlist


@pytest.mark.skipif(have_cv, reason="tests for import error, cviewer available")
def test_importerror(creating_graphs):
graphlist = creating_graphs
group1 = graphlist[:3]
group2 = graphlist[3:]

nbs = NetworkBasedStatistic()
nbs.inputs.in_group1 = group1
nbs.inputs.in_group2 = group2
nbs.inputs.edge_key = "weight"

with pytest.raises(ImportError) as e:
nbs.run()
assert "cviewer library is not available" == str(e.value)


@pytest.mark.skipif(not have_cv, reason="cviewer has to be available")
def test_keyerror(creating_graphs):
graphlist =creating_graphs

group1 = graphlist[:3]
group2 = graphlist[3:]

nbs = NetworkBasedStatistic()
nbs.inputs.in_group1 = group1
nbs.inputs.in_group2 = group2
nbs.inputs.edge_key = "Your_edge"

with pytest.raises(KeyError) as e:
nbs.run()
assert "the graph edges do not have Your_edge attribute" in str(e.value)