Repository: nipy/nipype

Issue:
Bunch has no attribute insert (from neurostars)
[Just seen this in neurostars](https://neurostars.org/p/4283/):

```
161023-16:13:46,511 workflow INFO:
         Running: antsApplyTransforms --default-value 0 --input /home/contematto/exp/fmri_pipelines/workdir/bids_preprocess/featpreproc/_subject_id_sub-sid000003_task_id_1back/addmean/mapflow/_addmean0/sub-sid000003_task-1back_run-01_bold_dtype_mcf_mask_smooth_mask_gms_tempfilt_maths.nii.gz --input-image-type 3 --interpolation Linear --output sub-sid000003_task-1back_run-01_bold_dtype_mcf_mask_smooth_mask_gms_tempfilt_maths_trans.nii.gz --reference-image /usr/share/fsl/5.0/data/standard/MNI152_T1_2mm.nii.gz --transform [ /home/contematto/exp/fmri_pipelines/workdir/bids_preprocess/registration/_subject_id_sub-sid000003_task_id_1back/antsRegister/output_Composite.h5, 0 ] --transform [ /home/contematto/exp/fmri_pipelines/workdir/bids_preprocess/registration/_subject_id_sub-sid000003_task_id_1back/convert2itk/affine.txt, 0 ]
161023-16:13:46,512 interface DEBUG:
         default_value_0.0
161023-16:13:46,512 interface DEBUG:
         input_image_/home/contematto/exp/fmri_pipelines/workdir/bids_preprocess/featpreproc/_subject_id_sub-sid000003_task_id_1back/addmean/mapflow/_addmean0/sub-sid000003_task-1back_run-01_bold_dtype_mcf_mask_smooth_mask_gms_tempfilt_maths.nii.gz
161023-16:13:46,512 interface DEBUG:
         input_image_type_3
161023-16:13:46,512 interface DEBUG:
         reference_image_/usr/share/fsl/5.0/data/standard/MNI152_T1_2mm.nii.gz
161023-16:13:46,513 interface DEBUG:
         {'DISPLAY': ':1'}
161023-16:15:40,447 interface DEBUG:
         Not a string literal
161023-16:15:40,511 interface DEBUG:
         Not a string literal
Traceback (most recent call last):
  File "preprocess.py", line 1129, in <module>
    wf.run(args.plugin)
  File "/home/contematto/exp/fmri_pipelines/venv2/local/lib/python2.7/site-packages/nipype/pipeline/engine/workflows.py", line 597, in run
    runner.run(execgraph, updatehash=updatehash, config=self.config)
  File "/home/contematto/exp/fmri_pipelines/venv2/local/lib/python2.7/site-packages/nipype/pipeline/plugins/linear.py", line 39, in run
    node.run(updatehash=updatehash)
  File "/home/contematto/exp/fmri_pipelines/venv2/local/lib/python2.7/site-packages/nipype/pipeline/engine/nodes.py", line 394, in run
    self._run_interface()
  File "/home/contematto/exp/fmri_pipelines/venv2/local/lib/python2.7/site-packages/nipype/pipeline/engine/nodes.py", line 1306, in _run_interface
    updatehash=updatehash))
  File "/home/contematto/exp/fmri_pipelines/venv2/local/lib/python2.7/site-packages/nipype/pipeline/engine/nodes.py", line 1170, in _collate_results
    self._result.runtime.insert(i, None)
AttributeError: 'Bunch' object has no attribute 'insert'
```



Discussion:
@oesteban just following up here instead of on neurostars: if I disable the debug mode, the pipeline seems to execute that node

And for reference:

```
(venv2)contematto@head1 ~/exp/fmri_pipelines (master*) $ python -c "import nipype; print(nipype.__version__)"
0.12.1
```

Any word on this?  I'm also running into it when trying anything that uses a MapNode

@mvdoc - any chance you could test this with current master?

I get the same error on 0.13.0-g5e0597b.dev

@mattcieslak - thanks for the update. it would really help if you can create a simple example? because we do run many pipelines with mapnodes in our test suite, so we are trying to figure out why we don't see this error.

@satra I found something interesting and subtle while writing up a small example. It seems that if there is an error in the pipeline, and `config.enable_debug_mode()` is set, the pipeline fails with `AttributeError: 'Bunch' object has no attribute 'insert'`. But if the pipeline exits fine, no such error is thrown. Example below:

This is the small working example

``` python
from nipype import MapNode, config
from nipype.interfaces import Function
config.enable_debug_mode()


def myfunction(string):
    return string + 'meh'

node = MapNode(Function(input_names=['string'],
                        output_names=['newstring'],
                        function=myfunction),
               iterfield=['string'],
               name='myfunc')

node.inputs.string = ['string' + str(i) for i in range(3)]
node.run()
```

And this is the output, running fine with `enable_debug_mode` set

``` shell
contematto@talete ~/Documents/experiments_work/pipelines (master*) $ python example-mapnode.py
161107-19:43:37,85 workflow INFO:
         Executing node myfunc in dir: /var/folders/gs/4v1cn8mj4n52qrr9r6xv4ckm0000gn/T/tmp43HUxH/myfunc
161107-19:43:37,97 workflow INFO:
         Executing node _myfunc0 in dir: /var/folders/gs/4v1cn8mj4n52qrr9r6xv4ckm0000gn/T/tmp43HUxH/myfunc/mapflow/_myfunc0
161107-19:43:37,123 workflow INFO:
         Runtime memory and threads stats unavailable
161107-19:43:37,125 workflow INFO:
         Executing node _myfunc1 in dir: /var/folders/gs/4v1cn8mj4n52qrr9r6xv4ckm0000gn/T/tmp43HUxH/myfunc/mapflow/_myfunc1
161107-19:43:37,139 workflow INFO:
         Runtime memory and threads stats unavailable
161107-19:43:37,141 workflow INFO:
         Executing node _myfunc2 in dir: /var/folders/gs/4v1cn8mj4n52qrr9r6xv4ckm0000gn/T/tmp43HUxH/myfunc/mapflow/_myfunc2
161107-19:43:37,154 workflow INFO:
         Runtime memory and threads stats unavailable
```

Now if I introduce a small error in the code, and run the following code (note the `input_names=['WRONG']`), it results in the `AttributeError`:

``` python
from nipype import MapNode, config
from nipype.interfaces import Function
config.enable_debug_mode()


def myfunction(string):
    return string + 'meh'

node = MapNode(Function(input_names=['WRONG'],
                        output_names=['newstring'],
                        function=myfunction),
               iterfield=['WRONG'],
               name='myfunc')

node.inputs.WRONG = ['string' + str(i) for i in range(3)]
node.run()
```

here's the output

``` shell
contematto@talete ~/Documents/experiments_work/pipelines (master*) $ python example-mapnode.py
161107-19:46:02,754 workflow INFO:
         Executing node myfunc in dir: /var/folders/gs/4v1cn8mj4n52qrr9r6xv4ckm0000gn/T/tmpZUoT2V/myfunc
161107-19:46:02,767 workflow INFO:
         Executing node _myfunc0 in dir: /var/folders/gs/4v1cn8mj4n52qrr9r6xv4ckm0000gn/T/tmpZUoT2V/myfunc/mapflow/_myfunc0
Traceback (most recent call last):
  File "example-mapnode.py", line 16, in <module>
    node.run()
  File "/Users/contematto/anaconda/lib/python2.7/site-packages/nipype/pipeline/engine/nodes.py", line 394, in run
    self._run_interface()
  File "/Users/contematto/anaconda/lib/python2.7/site-packages/nipype/pipeline/engine/nodes.py", line 1306, in _run_interface
    updatehash=updatehash))
  File "/Users/contematto/anaconda/lib/python2.7/site-packages/nipype/pipeline/engine/nodes.py", line 1170, in _collate_results
    self._result.runtime.insert(i, None)
AttributeError: 'Bunch' object has no attribute 'insert'
```

If I disable the `enable_debug_mode()`, it pukes and that's it:

``` shell
contematto@talete ~/Documents/experiments_work/pipelines (master*) $ python example-mapnode.py
161107-19:47:19,859 workflow INFO:
         Executing node myfunc in dir: /var/folders/gs/4v1cn8mj4n52qrr9r6xv4ckm0000gn/T/tmp5fBpWk/myfunc
161107-19:47:19,876 workflow INFO:
         Executing node _myfunc0 in dir: /var/folders/gs/4v1cn8mj4n52qrr9r6xv4ckm0000gn/T/tmp5fBpWk/myfunc/mapflow/_myfunc0
161107-19:47:19,909 workflow INFO:
         Executing node _myfunc1 in dir: /var/folders/gs/4v1cn8mj4n52qrr9r6xv4ckm0000gn/T/tmp5fBpWk/myfunc/mapflow/_myfunc1
161107-19:47:19,919 workflow INFO:
         Executing node _myfunc2 in dir: /var/folders/gs/4v1cn8mj4n52qrr9r6xv4ckm0000gn/T/tmp5fBpWk/myfunc/mapflow/_myfunc2
Traceback (most recent call last):
  File "example-mapnode.py", line 16, in <module>
    node.run()
  File "/Users/contematto/anaconda/lib/python2.7/site-packages/nipype/pipeline/engine/nodes.py", line 394, in run
    self._run_interface()
  File "/Users/contematto/anaconda/lib/python2.7/site-packages/nipype/pipeline/engine/nodes.py", line 1306, in _run_interface
    updatehash=updatehash))
  File "/Users/contematto/anaconda/lib/python2.7/site-packages/nipype/pipeline/engine/nodes.py", line 1211, in _collate_results
    (self.name, '\n'.join(msg)))
Exception: Subnodes of node: myfunc failed:
Subnode 0 failed
Error:
myfunction() got an unexpected keyword argument 'WRONG'
Interface Function failed to run. 
Subnode 1 failed
Error:
myfunction() got an unexpected keyword argument 'WRONG'
Interface Function failed to run. 
Subnode 2 failed
Error:
myfunction() got an unexpected keyword argument 'WRONG'
Interface Function failed to run. 
```

And for reference, this is with 0.12.1 

``` shell
contematto@talete ~/Documents/experiments_work/pipelines (master*) $ python -c "import nipype; print(nipype.__version__)"
0.12.1
```

@mvdoc - awesome that helps a lot - we need to create a pipeline+mapnode test with an error :) - i'll add this as a test. should be a relatively easy fix.

Just run with master and got the same error

```
contematto@talete ~/Documents/experiments_work/pipelines (master*) $ python -c "import nipype; print(nipype.__version__)"
0.13.0-dev
```


Resolving patch:
diff --git a/nipype/interfaces/bru2nii.py b/nipype/interfaces/bru2nii.py
--- a/nipype/interfaces/bru2nii.py
+++ b/nipype/interfaces/bru2nii.py
@@ -43,7 +43,7 @@ class Bru2(CommandLine):
     >>> converter = Bru2()
     >>> converter.inputs.input_dir = "brukerdir"
     >>> converter.cmdline  # doctest: +ELLIPSIS +IGNORE_UNICODE
-    'Bru2 -o .../nipype/nipype/testing/data/brukerdir brukerdir'
+    'Bru2 -o .../nipype/testing/data/brukerdir brukerdir'
     """
     input_spec = Bru2InputSpec
     output_spec = Bru2OutputSpec
diff --git a/nipype/pipeline/engine/nodes.py b/nipype/pipeline/engine/nodes.py
--- a/nipype/pipeline/engine/nodes.py
+++ b/nipype/pipeline/engine/nodes.py
@@ -1129,7 +1129,6 @@ def _node_runner(self, nodes, updatehash=False):
                 node.run(updatehash=updatehash)
             except Exception as err:
                 if str2bool(self.config['execution']['stop_on_first_crash']):
-                    self._result = node.result
                     raise
             finally:
                 yield i, node, err
diff --git a/nipype/utils/config.py b/nipype/utils/config.py
--- a/nipype/utils/config.py
+++ b/nipype/utils/config.py
@@ -58,7 +58,7 @@
 stop_on_unknown_version = false
 write_provenance = false
 parameterize_dirs = true
-poll_sleep_duration = 60
+poll_sleep_duration = 2
 xvfb_max_wait = 10
 profile_runtime = false
 


Test patch:
diff --git a/nipype/pipeline/engine/tests/test_utils.py b/nipype/pipeline/engine/tests/test_utils.py
--- a/nipype/pipeline/engine/tests/test_utils.py
+++ b/nipype/pipeline/engine/tests/test_utils.py
@@ -333,6 +333,7 @@ def test_multi_disconnected_iterable():
     yield assert_equal, len(eg.nodes()), 60
     rmtree(out_dir)
 
+
 def test_provenance():
     out_dir = mkdtemp()
     metawf = pe.Workflow(name='meta')
@@ -345,3 +346,28 @@ def test_provenance():
     yield assert_equal, len(psg.bundles), 2
     yield assert_equal, len(psg.get_records()), 7
     rmtree(out_dir)
+
+
+def test_mapnode_crash():
+    def myfunction(string):
+        return string + 'meh'
+    node = pe.MapNode(niu.Function(input_names=['WRONG'],
+                                   output_names=['newstring'],
+                                   function=myfunction),
+                      iterfield=['WRONG'],
+                      name='myfunc')
+
+    node.inputs.WRONG = ['string' + str(i) for i in range(3)]
+    node.config = deepcopy(config._sections)
+    node.config['execution']['stop_on_first_crash'] = True
+    cwd = os.getcwd()
+    node.base_dir = mkdtemp()
+
+    error_raised = False
+    try:
+        node.run()
+    except TypeError as e:
+        error_raised = True
+    os.chdir(cwd)
+    rmtree(node.base_dir)
+    yield assert_true, error_raised