Skip to content

Commit

Permalink
FIX: use cleaner import for joblib
Browse files Browse the repository at this point in the history
  • Loading branch information
jasmainak committed May 14, 2019
1 parent e366c26 commit ade7bb1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
5 changes: 5 additions & 0 deletions README.rst
Expand Up @@ -20,6 +20,7 @@ Dependencies
* scipy
* numpy
* matplotlib
* joblib (optional for parallel processing)

Installation
============
Expand All @@ -28,6 +29,10 @@ We recommend the `Anaconda Python distribution <https://www.continuum.io/downloa

$ conda install numpy matplotlib scipy

For joblib, you can do::

$ pip install joblib

Additionally, you would need Neuron which is available here: `https://neuron.yale.edu/neuron/ <https://neuron.yale.edu/neuron/>`_

If you want to install the latest version of the code (nightly) use::
Expand Down
6 changes: 4 additions & 2 deletions examples/plot_simulate_evoked.py
Expand Up @@ -33,14 +33,16 @@

###############################################################################
# Now let's simulate the dipole
# You can simulate multiple trials in parallel by using n_jobs > 1
net = Network(params)
dpl = simulate_dipole(net, n_jobs=3, n_trials=3)
dpls = simulate_dipole(net, n_jobs=1, n_trials=2)

###############################################################################
# and then plot it
import matplotlib.pyplot as plt
fig, axes = plt.subplots(2, 1, sharex=True, figsize=(6, 6))
dpl.plot(ax=axes[0])
for dpl in dpls:
dpl.plot(ax=axes[0])

###############################################################################
# Finally, we can also plot the spikes.
Expand Down
16 changes: 10 additions & 6 deletions mne_neuron/dipole.py
Expand Up @@ -6,7 +6,7 @@
import numpy as np
from numpy import convolve, hamming

from joblib import Parallel, delayed
from .parallel import _parallel_func


def _hammfilt(x, winsz):
Expand Down Expand Up @@ -87,6 +87,11 @@ def prsimtime():
pc.gid_clear()
pc.done()
dpl = Dipole(np.array(t_vec.to_python()), dpl_data)
if rank == 0:
dpl.baseline_renormalize(net.params)
dpl.convert_fAm_to_nAm()
dpl.scale(net.params['dipole_scalefctr'])
dpl.smooth(net.params['dipole_smooth_win'] / h.dt)
return dpl


Expand All @@ -105,12 +110,11 @@ def simulate_dipole(net, n_trials=1, n_jobs=1):
Returns
-------
dpl: instance of Dipole
The dipole object
dpl: list | instance of Dipole
The dipole object or list of dipole objects if n_trials > 1
"""
dpl = Parallel(n_jobs=n_jobs)(
delayed(_simulate_single_trial)(net.params)
for i in range(n_trials))
parallel, myfunc = _parallel_func(_simulate_single_trial, n_jobs=n_jobs)
dpl = parallel(myfunc(net.params) for _ in range(n_trials))

return dpl

Expand Down
20 changes: 20 additions & 0 deletions mne_neuron/parallel.py
Expand Up @@ -2,6 +2,8 @@

# Authors: Blake Caldwell <blake_caldwell@brown.edu>

from warnings import warn

from neuron import h

rank = 0
Expand All @@ -18,3 +20,21 @@ def create_parallel_context(n_jobs=1):

if rank == 0:
pc.gid_clear()


def _parallel_func(func, n_jobs):
if n_jobs != 1:
try:
from joblib import Parallel, delayed
except ImportError:
warn('joblib not installed. Cannot run in parallel.')
n_jobs = 1
if n_jobs == 1:
n_jobs = 1
my_func = func
parallel = list
else:
parallel = Parallel(n_jobs)
my_func = delayed(func)

return parallel, my_func

0 comments on commit ade7bb1

Please sign in to comment.