Skip to content

Instantly share code, notes, and snippets.

@kingjr
Created June 19, 2015 00:21
Show Gist options
  • Select an option

  • Save kingjr/0cf3fdc5bc38f4846e42 to your computer and use it in GitHub Desktop.

Select an option

Save kingjr/0cf3fdc5bc38f4846e42 to your computer and use it in GitHub Desktop.
To perform parallel operation on pairwise matrices
def pairwise(X, Y, func, n_jobs=-1):
from mne.parallel import parallel_func
if X.shape != Y.shape:
raise ValueError('X and Y must have identical shapes')
parallel, p_pairwise, n_jobs = parallel_func(_pairwise, n_jobs)
dims = X.shape
X = np.reshape(X, [dims[0], np.prod(dims[1:])])
Y = np.reshape(Y, [dims[0], np.prod(dims[1:])])
n_cols = X.shape[1]
n_chunks = min(n_cols, n_jobs)
chunks = np.array_split(range(n_cols), n_chunks)
out = parallel(p_pairwise(X[:, chunk], Y[:, chunk], func)
for chunk in chunks)
# unpack
out = np.reshape(out + list(), dims[1:] + np.shape(out)[2:])
return out
def _pairwise(X, Y, func):
n_cols = X.shape[1]
out_ = func(X[:, 0], Y[:, 0])
out = np.empty((n_cols, len(out_)))
for col in range(n_cols):
out[col, :] = func(X[:, col], Y[:, col])
return out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment