Repository: mne-tools/mne-bids-pipeline

Issue:
`ValueError: n_jobs must be an integer` when calling freesurfer
Hi,

When I run `python run.py freesurfer --config=~/hMT+/config.py`, I get the following error traceback:

```
Traceback (most recent call last):
  File "/home/merlin/PhD/mne-bids-pipeline/run.py", line 194, in <module>
    fire.Fire(process)
  File "/home/merlin/miniconda3/envs/mne-bids/lib/python3.9/site-packages/fire/core.py", line 141, in Fire
    component_trace = _Fire(component, args, parsed_flag_args, context, name)
  File "/home/merlin/miniconda3/envs/mne-bids/lib/python3.9/site-packages/fire/core.py", line 466, in _Fire
    component, remaining_args = _CallAndUpdateTrace(
  File "/home/merlin/miniconda3/envs/mne-bids/lib/python3.9/site-packages/fire/core.py", line 681, in _CallAndUpdateTrace
    component = fn(*varargs, **kwargs)
  File "/home/merlin/PhD/mne-bids-pipeline/run.py", line 189, in process
    _run_script(script_path, config, root_dir, subject, session, task, run)
  File "/home/merlin/PhD/mne-bids-pipeline/run.py", line 98, in _run_script
    runpy.run_path(script_path, run_name='__main__')
  File "/home/merlin/miniconda3/envs/mne-bids/lib/python3.9/runpy.py", line 268, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "/home/merlin/miniconda3/envs/mne-bids/lib/python3.9/runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/home/merlin/miniconda3/envs/mne-bids/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/merlin/PhD/mne-bids-pipeline/scripts/freesurfer/recon_all.py", line 112, in <module>
    fire.Fire(main)
  File "/home/merlin/miniconda3/envs/mne-bids/lib/python3.9/site-packages/fire/core.py", line 141, in Fire
    component_trace = _Fire(component, args, parsed_flag_args, context, name)
  File "/home/merlin/miniconda3/envs/mne-bids/lib/python3.9/site-packages/fire/core.py", line 466, in _Fire
    component, remaining_args = _CallAndUpdateTrace(
  File "/home/merlin/miniconda3/envs/mne-bids/lib/python3.9/site-packages/fire/core.py", line 681, in _CallAndUpdateTrace
    component = fn(*varargs, **kwargs)
  File "/home/merlin/PhD/mne-bids-pipeline/scripts/freesurfer/recon_all.py", line 94, in main
    parallel, run_func, _ = parallel_func(run_recon, n_jobs=n_jobs)
  File "<decorator-gen-42>", line 24, in parallel_func
  File "/home/merlin/miniconda3/envs/mne-bids/lib/python3.9/site-packages/mne/parallel.py", line 112, in parallel_func
    n_jobs = check_n_jobs(n_jobs)
  File "/home/merlin/miniconda3/envs/mne-bids/lib/python3.9/site-packages/mne/parallel.py", line 159, in check_n_jobs
    raise ValueError('n_jobs must be an integer')
ValueError: n_jobs must be an integer
```

Checking with pdb, it seems that `recon_all` is called with `n_jobs = "freesurfer"`. I'm not sure why that is.

```
19:36:39 Using custom configuration: /home/merlin/hMT+/config.py
19:36:39 [Step-01] Running: Initializing output directories.
19:36:39 [Step-01] Initializing output directories.
19:36:39 [Step-01] Completed: Initializing output directories.
2021-04-14 19:36:39 INFO Successfully finished running: init_derivatives_dir
2021-04-14 19:36:39 INFO Now running: on_all
> /home/merlin/PhD/mne-bids-pipeline/scripts/freesurfer/recon_all.py(88)main()
     87 
---> 88     logger.info('Running FreeSurfer')
     89 

ipdb> n_jobs
'freesurfer'
ipdb>                                                                                                                                       
```

It might be the config needs to be changed, but I can't figure out how.


Discussion:
For reference, here is the config file:
```python
"""
Localizer
"""
study_name = 'localizer'
bids_root = '~/hMT+/'

# sub-05 not in the same format -> projection different
# subjects = [f'{i:02}' for i in range(1, 13) if i not in [4, 6]]
subjects = ['01']

task = 'localizer'
find_flat_channels_meg = True
find_noisy_channels_meg = True
use_maxwell_filter = True
allow_maxshield = True
ch_types = ['meg']

# N_JOBS = 1

l_freq = None
h_freq = 100.
resample_sfreq = 250

# Artifact correction.
use_ssp = False
use_ica = True
ica_max_iterations = 500
ica_l_freq = None
ica_n_components = 0.99
ica_reject_components = 'auto'

# Epochs
tmin = -0.2
tmax = 1.0
baseline = (None, 0)

# Conditions / events to consider when epoching
conditions = ['coherent', 'incoherent']

# Source estiamtion
# run_source_estimation = True

# Decoding
decode = False
contrasts = [('incoherent', 'coherent')]

# Noise estimation
process_er = True
noise_cov = 'emptyroom'

```
this is pretty weird. Can we have a look together in the morning tomorrow?

>

sure!
This is solved by explicitely setting `--n_jobs=1` in the call:
```
python run.py freesurfer --config=~/hMT+/config.py --n_jobs=1
```

It seems Fire thinks `n_jobs` is implicitely `freesurfer` when not explicitely specified.
nice! can you see if you can fix it and send us a PR? thx

>

I'll try to make a PR

Resolving patch:
diff --git a/scripts/freesurfer/recon_all.py b/scripts/freesurfer/recon_all.py
--- a/scripts/freesurfer/recon_all.py
+++ b/scripts/freesurfer/recon_all.py
@@ -60,7 +60,7 @@ def run_recon(root_dir, subject, fs_bids_app) -> None:
     run_subprocess(cmd, env=env, verbose=logger.level)
 
 
-def main(n_jobs: int = 1) -> None:
+def main(*, n_jobs: int = 1) -> None:
     """Run freesurfer recon-all command on BIDS dataset.
 
     The command allows to run the freesurfer recon-all


Test patch: