def test_group_sparse_covariance():
    # run in debug mode. Should not fail
    # without debug mode: cost must decrease.

    signals, _, _ = generate_group_sparse_gaussian_graphs(
        density=0.1, n_subjects=5, n_features=10,
        min_n_samples=100, max_n_samples=151,
        random_state=np.random.RandomState(0))

    alpha = 0.1

    # These executions must hit the tolerance limit
    emp_covs, omega = group_sparse_covariance(signals, alpha, max_iter=20,
                                              tol=1e-2, debug=True, verbose=0)
    emp_covs, omega2 = group_sparse_covariance(signals, alpha, max_iter=20,
                                               tol=1e-2, debug=True, verbose=0)

    np.testing.assert_almost_equal(omega, omega2, decimal=4)

    class Probe(object):
        def __init__(self):
            self.objective = []

        def __call__(self, emp_covs, n_samples, alpha, max_iter, tol, n, omega,
                     omega_diff):
            if n >= 0:
                _, objective = group_sparse_scores(omega, n_samples, emp_covs,
                                                   alpha)
                self.objective.append(objective)

    # Use a probe to test for number of iterations and decreasing objective.
    probe = Probe()
    emp_covs, omega = group_sparse_covariance(
        signals, alpha, max_iter=4, tol=None, verbose=0, probe_function=probe)
    objective = probe.objective
    # check number of iterations
    assert_equal(len(objective), 4)

    # np.testing.assert_array_less is a strict comparison.
    # Zeros can occur in np.diff(objective).
    assert_true(np.all(np.diff(objective) <= 0))
    assert_equal(omega.shape, (10, 10, 5))

    # Test input argument checking
    assert_raises(ValueError, group_sparse_covariance, signals, "")
    assert_raises(ValueError, group_sparse_covariance, 1, alpha)
    assert_raises(ValueError, group_sparse_covariance,
                  [np.ones((2, 2)), np.ones((2, 3))], alpha)

    # Check consistency between classes
    gsc1 = GroupSparseCovarianceCV(alphas=4, tol=1e-1, max_iter=20, verbose=0,
                                   early_stopping=True)
    gsc1.fit(signals)

    gsc2 = GroupSparseCovariance(alpha=gsc1.alpha_, tol=1e-1, max_iter=20,
                                 verbose=0)
    gsc2.fit(signals)

    np.testing.assert_almost_equal(gsc1.precisions_, gsc2.precisions_,
                                   decimal=4)
Ejemplo n.º 2
0
def cv_object_study(early_stopping=True, output_dir="_early_stopping"):
    """Convenience function for running GroupSparseCovarianceCV. """
    parameters = {'n_tasks': 10, 'tol': 1e-3, 'max_iter': 50, "n_jobs": 7,
                  "cv": 4}
    parameters["tol_cv"] = parameters["tol"]
    parameters["max_iter_cv"] = parameters["max_iter"]

    synthetic = False

    print("-- Getting signals")
    if synthetic:
        parameters["n_features"] = 50
        parameters["density"] = 0.2
        signals, _, _ = testing.generate_group_sparse_gaussian_graphs(
            n_subjects=parameters["n_tasks"],
            n_features=parameters["n_features"],
            min_n_samples=100, max_n_samples=150,
            density=parameters["density"])
    else:
        mem = joblib.Memory(".")
        signals = []
        for n in range(parameters["n_tasks"]):
            signals.append(mem.cache(region_signals)(n))

    print("-- Optimizing")
    gsc = GroupSparseCovarianceCV(early_stopping=early_stopping,
                                  cv=parameters["cv"],
                                  n_jobs=parameters["n_jobs"],
                                  tol=parameters["tol"],
                                  tol_cv=parameters["tol_cv"],
                                  max_iter=parameters["max_iter"],
                                  max_iter_cv=parameters["max_iter_cv"],
                                  verbose=1)
    t0 = time.time()
    gsc.fit(signals)
    t1 = time.time()
    print("\nTime spent in fit(): %.1f s" % (t1 - t0))
    print("\n-- selected alpha: %.3e" % gsc.alpha_)
    print("-- cv_alphas_:")
    print(repr(np.asarray(gsc.cv_alphas_)))
    print("-- cv_scores_:")
    print(repr(np.asarray(gsc.cv_scores_)))

    out_filename = os.path.join(output_dir, "cv_object_study.pickle")
    pickle.dump([gsc.alpha_, gsc.cv_alphas_, gsc.cv_scores_, gsc.covariances_,
                 gsc.precisions_], open(out_filename, "wb"))
def compute_group_sparse_covariance(dataset, session='func1',
                                    preprocessing_folder='pipeline_1',
                                    plot=True, save=True, save_file=True,
                                    msdl=False):
    """Returns Group sparse covariance for all subjects
    """

    ts = []
    # load timeseries
    if msdl:
        for subject_id in dataset.subjects:
            ts.append(load_dynacomp_msdl_timeseries(subject_id,\
                      session=session, preprocessing_folder=preprocessing_folder))
        roi_names, roi_coords = load_msdl_names_and_coords()
    else:
        for subject_id in dataset.subjects:
            ts.append(load_dynacomp_roi_timeseries(subject_id, session=session,\
                      preprocessing_folder=preprocessing_folder))
        # load rois
        roi_names, roi_coords = load_roi_names_and_coords(subject_id)

    gsc = GroupSparseCovarianceCV(verbose=2)
    gsc.fit(ts)

    if plot:
        for i in range(len(dataset.subjects)):
            if not msdl:
                # load rois
                roi_names,\
                roi_coords = load_roi_names_and_coords(dataset.subjects[i])

            plot_connectivity_matrix(dataset.subjects[i], dataset.group[i],
                                     gsc.covariances_[..., i],
                                     roi_names, 'gsc_covariance', session,
                                     preprocessing_folder, save, msdl)

            plot_connectivity_matrix(dataset.subjects[i], dataset.group[i],
                                     gsc.precisions_[..., i],
                                     roi_names, 'gsc_precision', session,
                                     preprocessing_folder, save, msdl)

            sparsity = (gsc.precisions_[..., i] == 0)
            plot_connectivity_matrix(dataset.subjects[i], dataset.group[i],
                                     sparsity,
                                     roi_names, 'gsc_sparsity', session,
                                     preprocessing_folder, save, msdl)

            plot_connectivity_glassbrain(dataset.subjects[i], dataset.group[i],
                                         gsc.covariances_[..., i],
                                         roi_coords, 'gsc_covariance', session,
                                         preprocessing_folder, save, msdl)

    for i in range(len(dataset.subjects)):
        if not msdl:
            # load rois
            roi_names,\
            roi_coords = load_roi_names_and_coords(dataset.subjects[i])
        sparsity = (gsc.precisions_[..., i] == 0)            
        CONN_DIR = set_data_base_dir('Dynacomp/connectivity')
        subject_id = dataset.subjects[i]
        if not os.path.isdir(os.path.join(CONN_DIR, subject_id)):
            os.mkdir(os.path.join(CONN_DIR, subject_id))
        output_file = os.path.join(CONN_DIR, subject_id,
                                   'gsc_' + session + '_' + preprocessing_folder)
        if msdl:
            output_file += '_msdl'
        np.savez(output_file, covariance=gsc.covariances_[..., i],
                 precision=gsc.precisions_[..., i], sparsity=sparsity,
                 roi_names=roi_names, roi_coords=roi_coords)

    return gsc, roi_names, roi_coords
    density=0.1)

fig = plt.figure(figsize=(10, 7))
plt.subplots_adjust(hspace=0.4)
for n in range(n_displayed):
    plt.subplot(n_displayed, 4, 4 * n + 1)
    plot_matrix(precisions[n])
    if n == 0:
        plt.title("ground truth")
    plt.ylabel("subject %d" % n)


# Run group-sparse covariance on all subjects
from nilearn.group_sparse_covariance import GroupSparseCovarianceCV
gsc = GroupSparseCovarianceCV(max_iter=50, verbose=1)
gsc.fit(subjects)

for n in range(n_displayed):
    plt.subplot(n_displayed, 4, 4 * n + 2)
    plot_matrix(gsc.precisions_[..., n])
    if n == 0:
        plt.title("group-sparse\n$\\alpha=%.2f$" % gsc.alpha_)


# Fit one graph lasso per subject
from sklearn.covariance import GraphLassoCV
gl = GraphLassoCV(verbose=1)

for n, subject in enumerate(subjects[:n_displayed]):
    gl.fit(subject)
                                            confound_filenames):
    print("Processing file %s" % func_filename)

    # Computing some confounds
    hv_confounds = mem.cache(image.high_variance_confounds)(
        func_filename)

    region_ts = masker.transform(func_filename,
                                 confounds=[hv_confounds, confound_filename])
    subject_time_series.append(region_ts)

# Computing group-sparse precision matrices ###################################
print("-- Computing group-sparse precision matrices ...")
from nilearn.group_sparse_covariance import GroupSparseCovarianceCV
gsc = GroupSparseCovarianceCV(verbose=2)
gsc.fit(subject_time_series)

print("-- Computing graph-lasso precision matrices ...")
from sklearn import covariance
gl = covariance.GraphLassoCV(verbose=2)
gl.fit(np.concatenate(subject_time_series))

# Displaying results ##########################################################
atlas_imgs = image.iter_img(msdl_atlas_dataset.maps)
atlas_region_coords = [plotting.find_xyz_cut_coords(img) for img in atlas_imgs]

title = "GraphLasso"
plotting.plot_connectome(-gl.precision_, atlas_region_coords,
                         edge_threshold='90%',
                         title="Sparse inverse covariance (GraphLasso)")
plotting.plot_connectome(gl.covariance_,
Ejemplo n.º 6
0
    max_n_samples=50,
    density=0.1)

fig = plt.figure(figsize=(10, 7))
plt.subplots_adjust(hspace=0.4)
for n in range(n_displayed):
    plt.subplot(n_displayed, 4, 4 * n + 1)
    plot_matrix(precisions[n])
    if n == 0:
        plt.title("ground truth")
    plt.ylabel("subject %d" % n)

# Run group-sparse covariance on all subjects
from nilearn.group_sparse_covariance import GroupSparseCovarianceCV
gsc = GroupSparseCovarianceCV(max_iter=50, verbose=1)
gsc.fit(subjects)

for n in range(n_displayed):
    plt.subplot(n_displayed, 4, 4 * n + 2)
    plot_matrix(gsc.precisions_[..., n])
    if n == 0:
        plt.title("group-sparse\n$\\alpha=%.2f$" % gsc.alpha_)

# Fit one graph lasso per subject
from sklearn.covariance import GraphLassoCV
gl = GraphLassoCV(verbose=True)

for n, subject in enumerate(subjects[:n_displayed]):
    gl.fit(subject)

    plt.subplot(n_displayed, 4, 4 * n + 3)
for func_filename, confound_filename in zip(func_filenames,
                                            confound_filenames):
    print("Processing file %s" % func_filename)

    # Computing some confounds
    hv_confounds = mem.cache(image.high_variance_confounds)(func_filename)

    region_ts = masker.transform(func_filename,
                                 confounds=[hv_confounds, confound_filename])
    subject_time_series.append(region_ts)

# Computing group-sparse precision matrices ###################################
print("-- Computing group-sparse precision matrices ...")
from nilearn.group_sparse_covariance import GroupSparseCovarianceCV
gsc = GroupSparseCovarianceCV(verbose=2)
gsc.fit(subject_time_series)

print("-- Computing graph-lasso precision matrices ...")
from sklearn import covariance
gl = covariance.GraphLassoCV(verbose=2)
gl.fit(np.concatenate(subject_time_series))

# Displaying results ##########################################################
atlas_imgs = image.iter_img(msdl_atlas_dataset.maps)
atlas_region_coords = [plotting.find_xyz_cut_coords(img) for img in atlas_imgs]

title = "GraphLasso"
plotting.plot_connectome(-gl.precision_,
                         atlas_region_coords,
                         edge_threshold='90%',
                         title="Sparse inverse covariance (GraphLasso)")
def compute_group_sparse_covariance(dataset,
                                    session='func1',
                                    preprocessing_folder='pipeline_1',
                                    plot=True,
                                    save=True,
                                    save_file=True,
                                    msdl=False):
    """Returns Group sparse covariance for all subjects
    """

    ts = []
    # load timeseries
    if msdl:
        for subject_id in dataset.subjects:
            ts.append(load_dynacomp_msdl_timeseries(subject_id,\
                      session=session, preprocessing_folder=preprocessing_folder))
        roi_names, roi_coords = load_msdl_names_and_coords()
    else:
        for subject_id in dataset.subjects:
            ts.append(load_dynacomp_roi_timeseries(subject_id, session=session,\
                      preprocessing_folder=preprocessing_folder))
        # load rois
        roi_names, roi_coords = load_roi_names_and_coords(subject_id)

    gsc = GroupSparseCovarianceCV(verbose=2)
    gsc.fit(ts)

    if plot:
        for i in range(len(dataset.subjects)):
            if not msdl:
                # load rois
                roi_names,\
                roi_coords = load_roi_names_and_coords(dataset.subjects[i])

            plot_connectivity_matrix(dataset.subjects[i], dataset.group[i],
                                     gsc.covariances_[..., i], roi_names,
                                     'gsc_covariance', session,
                                     preprocessing_folder, save, msdl)

            plot_connectivity_matrix(dataset.subjects[i], dataset.group[i],
                                     gsc.precisions_[..., i], roi_names,
                                     'gsc_precision', session,
                                     preprocessing_folder, save, msdl)

            sparsity = (gsc.precisions_[..., i] == 0)
            plot_connectivity_matrix(dataset.subjects[i], dataset.group[i],
                                     sparsity, roi_names, 'gsc_sparsity',
                                     session, preprocessing_folder, save, msdl)

            plot_connectivity_glassbrain(dataset.subjects[i], dataset.group[i],
                                         gsc.covariances_[..., i], roi_coords,
                                         'gsc_covariance', session,
                                         preprocessing_folder, save, msdl)

    for i in range(len(dataset.subjects)):
        if not msdl:
            # load rois
            roi_names,\
            roi_coords = load_roi_names_and_coords(dataset.subjects[i])
        sparsity = (gsc.precisions_[..., i] == 0)
        CONN_DIR = set_data_base_dir('Dynacomp/connectivity')
        subject_id = dataset.subjects[i]
        if not os.path.isdir(os.path.join(CONN_DIR, subject_id)):
            os.mkdir(os.path.join(CONN_DIR, subject_id))
        output_file = os.path.join(
            CONN_DIR, subject_id,
            'gsc_' + session + '_' + preprocessing_folder)
        if msdl:
            output_file += '_msdl'
        np.savez(output_file,
                 covariance=gsc.covariances_[..., i],
                 precision=gsc.precisions_[..., i],
                 sparsity=sparsity,
                 roi_names=roi_names,
                 roi_coords=roi_coords)

    return gsc, roi_names, roi_coords