Skip to content

Commit 75300a5

Browse files
committed
fix: tests for mapnode crash
1 parent 2ec0b0f commit 75300a5

File tree

2 files changed

+18
-22
lines changed

2 files changed

+18
-22
lines changed

nipype/pipeline/engine/nodes.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
from hashlib import sha1
3737

3838
from ... import config, logging
39-
from ...utils.misc import (flatten, unflatten, package_check, str2bool)
39+
from ...utils.misc import (flatten, unflatten, str2bool)
4040
from ...utils.filemanip import (save_json, FileNotFoundError,
4141
filename_to_list, list_to_filename,
4242
copyfiles, fnames_presuffix, loadpkl,
@@ -54,7 +54,6 @@
5454
get_print_name, merge_dict, evaluate_connect_function)
5555
from .base import EngineBase
5656

57-
package_check('networkx', '1.3')
5857
logger = logging.getLogger('workflow')
5958

6059
class Node(EngineBase):
@@ -607,6 +606,7 @@ def _run_command(self, execute, copyfiles=True):
607606
try:
608607
result = self._interface.run()
609608
except Exception as msg:
609+
self._save_results(result, cwd)
610610
self._result.runtime.stderr = msg
611611
raise
612612

@@ -1124,6 +1124,7 @@ def _make_nodes(self, cwd=None):
11241124
yield i, node
11251125

11261126
def _node_runner(self, nodes, updatehash=False):
1127+
old_cwd = os.getcwd()
11271128
for i, node in nodes:
11281129
err = None
11291130
try:
@@ -1133,6 +1134,7 @@ def _node_runner(self, nodes, updatehash=False):
11331134
if str2bool(self.config['execution']['stop_on_first_crash']):
11341135
raise
11351136
finally:
1137+
os.chdir(old_cwd)
11361138
yield i, node, err
11371139

11381140
def _collate_results(self, nodes):
@@ -1275,12 +1277,8 @@ def _run_interface(self, execute=True, updatehash=False):
12751277
nitems = len(filename_to_list(getattr(self.inputs,
12761278
self.iterfield[0])))
12771279
nodenames = ['_' + self.name + str(i) for i in range(nitems)]
1278-
nodes = list(self._make_nodes(cwd))
1279-
node_results = list(self._node_runner(nodes))
1280-
self._collate_results(node_results)
1281-
# map-reduce formulation
1282-
#self._collate_results(self._node_runner(self._make_nodes(cwd),
1283-
# updatehash=updatehash))
1280+
self._collate_results(self._node_runner(self._make_nodes(cwd),
1281+
updatehash=updatehash))
12841282
self._save_results(self._result, cwd)
12851283
# remove any node directories no longer required
12861284
dirs2remove = []

nipype/pipeline/engine/tests/test_utils.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -338,36 +338,37 @@ def test_provenance(tmpdir):
338338
assert len(psg.bundles) == 2
339339
assert len(psg.get_records()) == 7
340340

341+
342+
def dummy_func(value):
343+
return value + 1
344+
345+
341346
def test_mapnode_crash(tmpdir):
342347
"""Test mapnode crash when stop_on_first_crash is True"""
343348
cwd = os.getcwd()
344-
def myfunction(string):
345-
return string + 'meh'
346349
node = pe.MapNode(niu.Function(input_names=['WRONG'],
347350
output_names=['newstring'],
348-
function=myfunction),
351+
function=dummy_func),
349352
iterfield=['WRONG'],
350353
name='myfunc')
351-
node.inputs.WRONG = ['string' + str(i) for i in range(3)]
354+
node.inputs.WRONG = ['string{}'.format(i) for i in range(3)]
352355
node.config = deepcopy(config._sections)
353356
node.config['execution']['stop_on_first_crash'] = True
354357
node.base_dir = str(tmpdir)
355358
with pytest.raises(TypeError):
356359
node.run()
357360
os.chdir(cwd)
358361

362+
359363
def test_mapnode_crash2(tmpdir):
360364
"""Test mapnode crash when stop_on_first_crash is False"""
361365
cwd = os.getcwd()
362-
def myfunction(string):
363-
return string + 'meh'
364-
365366
node = pe.MapNode(niu.Function(input_names=['WRONG'],
366367
output_names=['newstring'],
367-
function=myfunction),
368+
function=dummy_func),
368369
iterfield=['WRONG'],
369370
name='myfunc')
370-
node.inputs.WRONG = ['string' + str(i) for i in range(3)]
371+
node.inputs.WRONG = ['string{}'.format(i) for i in range(3)]
371372
node.base_dir = str(tmpdir)
372373

373374
with pytest.raises(Exception):
@@ -377,15 +378,12 @@ def myfunction(string):
377378

378379
def test_mapnode_crash3(tmpdir):
379380
"""Test mapnode crash when mapnode is embedded in a workflow"""
380-
381-
def myfunction(string):
382-
return string + 'meh'
383381
node = pe.MapNode(niu.Function(input_names=['WRONG'],
384382
output_names=['newstring'],
385-
function=myfunction),
383+
function=dummy_func),
386384
iterfield=['WRONG'],
387385
name='myfunc')
388-
node.inputs.WRONG = ['string' + str(i) for i in range(3)]
386+
node.inputs.WRONG = ['string{}'.format(i) for i in range(3)]
389387
wf = pe.Workflow('testmapnodecrash')
390388
wf.add_nodes([node])
391389
wf.base_dir = str(tmpdir)

0 commit comments

Comments
 (0)