[med-svn] [python-mne] 223/376: making permutation_t_test parallel

Yaroslav Halchenko debian at onerussian.com
Fri Nov 27 17:22:44 UTC 2015


This is an automated email from the git hooks/post-receive script.

yoh pushed a commit to annotated tag v0.1
in repository python-mne.

commit 0b8c43f1600cb8577b8e5c00fbbc69466ba02b60
Author: Alexandre Gramfort <alexandre.gramfort at inria.fr>
Date:   Wed Apr 27 17:01:50 2011 -0400

    making permutation_t_test parallel
---
 examples/stats/plot_sensor_permutation_test.py |  2 +-
 mne/stats/permutations.py                      | 36 +++++++++++++++++++++++---
 2 files changed, 33 insertions(+), 5 deletions(-)

diff --git a/examples/stats/plot_sensor_permutation_test.py b/examples/stats/plot_sensor_permutation_test.py
index 65084a1..cdbc2e3 100755
--- a/examples/stats/plot_sensor_permutation_test.py
+++ b/examples/stats/plot_sensor_permutation_test.py
@@ -51,7 +51,7 @@ temporal_mask = np.logical_and(0.04 <= times, times <= 0.06)
 data = np.squeeze(np.mean(data[:, :, temporal_mask], axis=2))
 
 n_permutations = 50000
-T0, p_values, H0 = permutation_t_test(data, n_permutations)
+T0, p_values, H0 = permutation_t_test(data, n_permutations, n_jobs=2)
 
 significant_sensors = picks[p_values <= 0.05]
 significant_sensors_names = [raw.info['ch_names'][k]
diff --git a/mne/stats/permutations.py b/mne/stats/permutations.py
index 53eaa0d..eed57c4 100755
--- a/mne/stats/permutations.py
+++ b/mne/stats/permutations.py
@@ -45,7 +45,16 @@ def bin_perm_rep(ndim, a=0, b=1):
     return perms
 
 
-def permutation_t_test(X, n_permutations=10000, tail=0):
+def _max_stat(X, X2, perms, dof_scaling):
+    """Aux function for permutation_t_test (for parallel comp)"""
+    n_samples = len(X)
+    mus = np.dot(perms, X) / float(n_samples)
+    stds = np.sqrt(X2[None, :] - mus ** 2) * dof_scaling  # std with splitting
+    max_abs = np.max(np.abs(mus) / (stds / sqrt(n_samples)), axis=1)  # t-max
+    return max_abs
+
+
+def permutation_t_test(X, n_permutations=10000, tail=0, n_jobs=1):
     """One sample/paired sample permutation test based on a t-statistic.
 
     This function can perform the test on one variable or
@@ -88,6 +97,8 @@ def permutation_t_test(X, n_permutations=10000, tail=0):
         T-statistic obtained by permutations and t-max trick for multiple
         comparison.
 
+    n_jobs : int
+        Number of CPUs to use for computation.
     Notes
     -----
     A reference (among many) in field of neuroimaging:
@@ -116,9 +127,26 @@ def permutation_t_test(X, n_permutations=10000, tail=0):
     else:
         perms = np.sign(0.5 - np.random.rand(n_permutations, n_samples))
 
-    mus = np.dot(perms, X) / float(n_samples)
-    stds = np.sqrt(X2[None, :] - mus ** 2) * dof_scaling  # std with splitting
-    max_abs = np.max(np.abs(mus) / (stds / sqrt(n_samples)), axis=1)  # t-max
+    try:
+        from scikits.learn.externals.joblib import Parallel, delayed
+        parallel = Parallel(n_jobs)
+        my_max_stat = delayed(_max_stat)
+    except ImportError:
+        print "joblib not installed. Cannot run in parallel."
+        n_jobs = 1
+        my_max_stat = _max_stat
+        parallel = list
+
+    if n_jobs == -1:
+        try:
+            import multiprocessing
+            n_jobs = multiprocessing.cpu_count()
+        except ImportError:
+            print "multiprocessing not installed. Cannot run in parallel."
+            n_jobs = 1
+
+    max_abs = np.concatenate(parallel(my_max_stat(X, X2, p, dof_scaling)
+                                      for p in np.array_split(perms, n_jobs)))
     H0 = np.sort(max_abs)
 
     scaling = float(n_permutations + 1)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-med/python-mne.git



More information about the debian-med-commit mailing list