Ejemplo n.º 1
0
def lasso(xs):
    '''Use GraphicalLassoCV.

    Parameters
    ----------
    xs : array_like
        N samples of X.

    Returns
    -------
    C : array_like
        Covariance matrix estimate.

    Notes
    -----
    This implementation uses cross-validation to find the correct
    weight for Lasso.  Graphical Lasso is a method for finding a
    sparse inverse covariance matrix, so this is additional
    information that might not follow from having a Toeplitz
    covariance matrix...
    '''
    model = GraphicalLassoCV(cv=3)
    model.fit(xs)
    C = model.covariance_
    return C
Ejemplo n.º 2
0
 def create_prior_from_samples(self, samples):
     from sklearn.covariance import GraphicalLassoCV
     from numpy import asarray, linalg
     model = GraphicalLassoCV()
     model.fit(asarray(samples))
     return th_Mahalanobis(
         asarray(samples).mean(axis=0), linalg.cholesky(model.precision_),
         self.prefix)
Ejemplo n.º 3
0
def est_connectivity(X, gm="Glasso", assume_centered=False):
    if gm == "QuicGlasso-CV":
        quic = QuicGraphicalLassoCV(cv=5)
        quic.fit(X)
        return quic.covariance_, quic.precision_, quic.lam_

    elif gm == "QuicGlasso-BIC":
        quic_bic = QuicGraphicalLassoEBIC(gamma=0)
        quic_bic.fit(X)
        return quic_bic.covariance_, quic_bic.precision_, quic_bic.lam_

    else:  # Default: Glasso
        glasso = GraphicalLassoCV(assume_centered=assume_centered, cv=5).fit(X)
        return glasso.covariance_, glasso.get_precision(), glasso.alpha_
Ejemplo n.º 4
0
    def _infer_network(self, data):
        """
        Infer the network.

        Args:
            data (pd.DataFrame): data to be used for the inference.
        """
        entities = data.columns
        model = GraphicalLassoCV(**self.parameters)
        model.fit(data.values)
        self.graph = Graph(adjacency=pd.DataFrame(
            from_precision_matrix_partial_correlations(model.precision_),
            index=entities,
            columns=entities))
        logger.debug('inferred with {}'.format(self.method))
Ejemplo n.º 5
0
def test_graphical_lasso_cv_grid_scores_and_cv_alphas_deprecated():
    splits = 4
    n_alphas = 5
    n_refinements = 3
    true_cov = np.array([
        [0.8, 0.0, 0.2, 0.0],
        [0.0, 0.4, 0.0, 0.0],
        [0.2, 0.0, 0.3, 0.1],
        [0.0, 0.0, 0.1, 0.7],
    ])
    rng = np.random.RandomState(0)
    X = rng.multivariate_normal(mean=[0, 0, 0, 0], cov=true_cov, size=200)
    cov = GraphicalLassoCV(cv=splits,
                           alphas=n_alphas,
                           n_refinements=n_refinements).fit(X)

    total_alphas = n_refinements * n_alphas + 1
    msg = (r"The `grid_scores_` attribute is deprecated in version 0\.24 in "
           r"favor of `cv_results_` and will be removed in version 1\.1 "
           r"\(renaming of 0\.26\).")
    with pytest.warns(FutureWarning, match=msg):
        assert cov.grid_scores_.shape == (total_alphas, splits)

    msg = (r"The `cv_alphas_` attribute is deprecated in version 0\.24 in "
           r"favor of `cv_results_\['alpha'\]` and will be removed in version "
           r"1\.1 \(renaming of 0\.26\)")
    with pytest.warns(FutureWarning, match=msg):
        assert len(cov.cv_alphas_) == total_alphas
Ejemplo n.º 6
0
def test_graphical_lasso_cv_scores_deprecated():
    """Check that the following keys in cv_results_ are deprecated: `mean_score`,
    `std_score`, and `split(k)_score`."""
    splits = 4
    n_alphas = 5
    n_refinements = 3
    true_cov = np.array([
        [0.8, 0.0, 0.2, 0.0],
        [0.0, 0.4, 0.0, 0.0],
        [0.2, 0.0, 0.3, 0.1],
        [0.0, 0.0, 0.1, 0.7],
    ])
    rng = np.random.RandomState(0)
    X = rng.multivariate_normal(mean=[0, 0, 0, 0], cov=true_cov, size=200)
    cov = GraphicalLassoCV(cv=splits,
                           alphas=n_alphas,
                           n_refinements=n_refinements).fit(X)
    cv_results = cov.cv_results_

    deprecated_keys = ["mean_score", "std_score"
                       ] + [f"split{k}_score" for k in range(splits)]

    for deprecated_key in deprecated_keys:
        new_key = deprecated_key.replace("_score", "_test_score")
        msg = (
            f"Key: '{deprecated_key}', is deprecated in 1.0 and will be removed in 1.2."
            f" Use '{new_key}' instead")
        with pytest.warns(FutureWarning, match=msg):
            cv_results[deprecated_key]
Ejemplo n.º 7
0
def test_graphical_lasso_cv_scores():
    splits = 4
    n_alphas = 5
    n_refinements = 3
    true_cov = np.array([[0.8, 0.0, 0.2, 0.0], [0.0, 0.4, 0.0, 0.0],
                         [0.2, 0.0, 0.3, 0.1], [0.0, 0.0, 0.1, 0.7]])
    rng = np.random.RandomState(0)
    X = rng.multivariate_normal(mean=[0, 0, 0, 0], cov=true_cov, size=200)
    cov = GraphicalLassoCV(cv=splits,
                           alphas=n_alphas,
                           n_refinements=n_refinements).fit(X)

    cv_results = cov.cv_results_
    # alpha and one for each split

    total_alphas = n_refinements * n_alphas + 1
    keys = ['alphas']
    split_keys = ['split{}_score'.format(i) for i in range(splits)]
    for key in keys + split_keys:
        assert key in cv_results
        assert len(cv_results[key]) == total_alphas

    cv_scores = np.asarray([cov.cv_results_[key] for key in split_keys])
    expected_mean = cv_scores.mean(axis=0)
    expected_std = cv_scores.std(axis=0)

    assert_allclose(cov.cv_results_["mean_score"], expected_mean)
    assert_allclose(cov.cv_results_["std_score"], expected_std)
def getConnectome(imgPath=None,
                  atlasPath=None,
                  viewInBrowser=False,
                  displayCovMatrix=False):
    """
    Gets the connectome of a functional MRI scan
    imgPath -> absolute or relative path to the .nii file
    atlasPath -> download path for the reference MSDL atlas
    viewInBrowser (optional, default=False) -> if True, opens up an interactive viewer in the browser
    displayCovMatrix (optional, default=False) -> display the inverse covariance matrix
    Returns a tuple of shape (estimator, atlas)
    """
    # Download the reference atlas
    atlas = datasets.fetch_atlas_msdl(data_dir=atlasPath)
    # Loading atlas image stored in 'maps'
    atlasFilename = atlas['maps']
    # Get the time series for the fMRI scan
    masker = NiftiMapsMasker(maps_img=atlasFilename,
                             standardize=True,
                             memory='nilearn_cache',
                             verbose=5)
    timeSeries = masker.fit_transform(imgPath)
    # Compute the connectome using sparse inverse covariance
    estimator = GraphicalLassoCV()
    estimator.fit(timeSeries)
    if (displayCovMatrix):
        labels = atlas['labels']
        plotting.plot_matrix(estimator.covariance_,
                             labels=labels,
                             figure=(9, 7),
                             vmax=1,
                             vmin=-1,
                             title='Covariance')
        plotting.plot_matrix(estimator.precision_,
                             labels=labels,
                             figure=(9, 7),
                             vmax=1,
                             vmin=-1,
                             title='Inverse covariance (Precision)')
        #covPlot.get_figure().savefig('Covariance.png')
        # precPlot.get_figure().savefig('Inverse Covariance.png')
    if (viewInBrowser):
        coords = atlas.region_coords
        view = plotting.view_connectome(-estimator.precision_, coords, '60.0%')
        #view.save_as_html(file_name='Connectome Test.html')
        view.open_in_browser()
    return (estimator, atlas)
Ejemplo n.º 9
0
def main():
    mean = torch.tensor(np.ones(16), dtype=torch.float32)
    diag = torch.tensor(np.ones(16), dtype=torch.float32)

    population = Gaussian_Distribution(mean=mean,
                                       diag=diag,
                                       sub=0.3,
                                       type='chain',
                                       slash=1)
    truth = population.invcov.numpy()
    n = 1000
    d = population.dim

    print(truth)
    dist, sample, _, S = population.generate(n, numpy_like=True)
    #print(S)
    #print(np.array(sample))
    print(sample_mean(np.array(sample)))
    print(sample_cov(np.array(sample)))

    R = np.linalg.inv(S)
    #print(R)
    #print(sample)
    np.random.seed(0)
    model = GraphicalLassoCV()
    model.fit(np.array(sample))
    cov_ = model.covariance_
    prec_ = model.precision_

    heatmap(prec_)

    plt.figure(figsize=(4, 3))
    plt.axes([.2, .15, .75, .7])
    plt.plot(model.cv_alphas_, np.mean(model.grid_scores_, axis=1), 'o-')
    plt.axvline(model.alpha_, color='.5')
    plt.title('Model selection')
    plt.ylabel('Cross-validation score')
    plt.xlabel('alpha')

    plt.show()
    print(model.cv_alphas_, model.grid_scores_)

    model = GraphicalLasso()
    model.fit(sample)
    heatmap(model.precision_, 0.055)

    score = dict()
    score['log_lik'] = []
    score['AIC'] = []
    alpha_list = np.hstack((np.arange(0, 0.1,
                                      0.001), np.arange(0.11, 0.3, 0.01)))
    data = np.array(sample)
    for alpha in alpha_list:
        out_dict = cross_val_score_GLasso(data, alpha=alpha)
        score['log_lik'].append(out_dict['log_lik'])
        score['AIC'].append(out_dict['AIC'])
    plt.plot(alpha_list, score['log_lik'], 'o-')
    plt.show()
    plt.plot(alpha_list, score['AIC'])
    plt.show()
Ejemplo n.º 10
0
def get_optimal_cov_estimator(time_series):
    from sklearn.covariance import GraphicalLassoCV

    estimator = GraphicalLassoCV(cv=5, assume_centered=True)
    print("\nSearching for best Lasso estimator...\n")
    try:
        estimator.fit(time_series)
        return estimator
    except BaseException:
        ix = 0
        print("\nModel did not converge on first attempt. "
              "Varying tolerance...\n")
        while not hasattr(estimator, 'covariance_') and \
            not hasattr(estimator, 'precision_') and ix < 3:
            for tol in [0.1, 0.01, 0.001, 0.0001]:
                print(f"Tolerance={tol}")
                estimator = GraphicalLassoCV(cv=5,
                                             max_iter=200,
                                             tol=tol,
                                             assume_centered=True)
                try:
                    estimator.fit(time_series)
                    return estimator
                except BaseException:
                    ix += 1
                    continue

    if not hasattr(estimator, 'covariance_') and not hasattr(
            estimator, 'precision_'):
        print("Unstable Lasso estimation. Applying shrinkage to empirical "
              "covariance...")
        from sklearn.covariance import (
            GraphicalLasso,
            empirical_covariance,
            shrunk_covariance,
        )
        try:
            emp_cov = empirical_covariance(time_series, assume_centered=True)
            for i in np.arange(0.8, 0.99, 0.01):
                print(f"Shrinkage={i}:")
                shrunk_cov = shrunk_covariance(emp_cov, shrinkage=i)
                alphaRange = 10.0**np.arange(-8, 0)
                for alpha in alphaRange:
                    print(f"Auto-tuning alpha={alpha}...")
                    estimator_shrunk = GraphicalLasso(alpha,
                                                      assume_centered=True)
                    try:
                        estimator_shrunk.fit(shrunk_cov)
                        return estimator_shrunk
                    except BaseException:
                        continue
        except BaseException:
            return None
    else:
        return estimator
Ejemplo n.º 11
0
def test_graphical_lasso_cv(random_state=1):
    # Sample area_data from a sparse multivariate normal
    dim = 5
    n_samples = 6
    random_state = check_random_state(random_state)
    prec = make_sparse_spd_matrix(dim, alpha=.96, random_state=random_state)
    cov = linalg.inv(prec)
    X = random_state.multivariate_normal(np.zeros(dim), cov, size=n_samples)
    # Capture stdout, to smoke test the verbose mode
    orig_stdout = sys.stdout
    try:
        sys.stdout = StringIO()
        # We need verbose very high so that Parallel prints on stdout
        GraphicalLassoCV(verbose=100, alphas=5, tol=1e-1).fit(X)
    finally:
        sys.stdout = orig_stdout

    # Smoke test with specified alphas
    GraphicalLassoCV(alphas=[0.8, 0.5], tol=1e-1, n_jobs=1).fit(X)
Ejemplo n.º 12
0
def glasso(data, alphas=5, n_jobs=None, mode='cd'):
    """
        Estimates the graph with graphical lasso finding the best alpha based on cross validation

        Parameters
        ----------
        data: numpy ndarray
            The input data for to reconstruct/estimate a graph on. Features as columns and observations as rows.
        alphas: int or array-like of shape (n_alphas,), dtype=float, default=5
            Non-negative. If an integer is given, it fixes the number of points on the grids of alpha to be used. If a list is given, it gives the grid to be used. 
        Returns
        -------
        adjacency matrix : the estimated adjacency matrix.
    """
    scaler = StandardScaler()
    data = scaler.fit_transform(data)
    cov = GraphicalLassoCV(alphas=alphas, n_jobs=n_jobs).fit(data)
    precision_matrix = cov.get_precision()
    adjacency_matrix = precision_matrix.astype(bool).astype(int)
    adjacency_matrix[np.diag_indices_from(adjacency_matrix)] = 0
    return adjacency_matrix
Ejemplo n.º 13
0
def helper_graphical_lasso(X, theta_true, tf_names=[]):
    # Estimate the covariance
    if args.mode == 'cv':
        model = GraphicalLassoCV()
    else:
        model = GraphicalLasso(alpha=args.alpha_l1,
                               mode=args.mode,
                               tol=1e-7,
                               enet_tol=1e-6,
                               max_iter=100,
                               verbose=False,
                               assume_centered=False)
    model.fit(X)
    #    cov_ = model.covariance_
    prec_ = model.precision_
    if args.USE_TF_NAMES == 'yes' and len(tf_names) != 0:
        prec_ = postprocess_tf(prec_, tf_names)
    recovery_metrics = report_metrics(np.array(theta_true), prec_)
    print(
        'GLASSO: FDR, TPR, FPR, SHD, nnz_true, nnz_pred, precision, recall, Fb, aupr, auc'
    )
    print('GLASSO: TEST: Recovery of true theta: ',
          *np.around(recovery_metrics, 3))
    return list(recovery_metrics)
Ejemplo n.º 14
0
def test_deprecated_grid_scores(random_state=1):
    dim = 5
    n_samples = 6
    random_state = check_random_state(random_state)
    prec = make_sparse_spd_matrix(dim, alpha=.96, random_state=random_state)
    cov = linalg.inv(prec)
    X = random_state.multivariate_normal(np.zeros(dim), cov, size=n_samples)
    graphical_lasso = GraphicalLassoCV(alphas=[0.8, 0.5], tol=1e-1, n_jobs=1)
    graphical_lasso.fit(X)

    depr_message = ("Attribute grid_scores was deprecated in version "
                    "0.19 and will be removed in 0.21. Use "
                    "``grid_scores_`` instead")

    with pytest.warns(DeprecationWarning, match=depr_message):
        assert_equal(graphical_lasso.grid_scores, graphical_lasso.grid_scores_)
def test_graphical_lasso_cv_alphas_iterable(alphas_container_type):
    """Check that we can pass an array-like to `alphas`.

    Non-regression test for:
    https://github.com/scikit-learn/scikit-learn/issues/22489
    """
    true_cov = np.array([
        [0.8, 0.0, 0.2, 0.0],
        [0.0, 0.4, 0.0, 0.0],
        [0.2, 0.0, 0.3, 0.1],
        [0.0, 0.0, 0.1, 0.7],
    ])
    rng = np.random.RandomState(0)
    X = rng.multivariate_normal(mean=[0, 0, 0, 0], cov=true_cov, size=200)
    alphas = _convert_container([0.02, 0.03], alphas_container_type)
    GraphicalLassoCV(alphas=alphas, tol=1e-1, n_jobs=1).fit(X)
def estimate_covariance(x, method):
    """
    Covariance estimator wrapper
    :param x:
    :param method:
    :return:
    """
    cov = None
    if method == 'shrunk':
        cov = ShrunkCovariance().fit(x)
    elif method == 'glasso':
        cov = GraphicalLassoCV(cv=5, alphas=10, n_refinements=10).fit(x)
    else:
        ValueError('Covariance method not in [shrunk,glasso]')

    return cov.covariance_, cov.precision_
Ejemplo n.º 17
0
def main():
    mean = torch.tensor(np.ones(16), dtype=torch.float32)
    diag = torch.tensor(np.ones(16), dtype=torch.float32)

    population = Gaussian_Distribution(mean=mean,
                                       diag=diag,
                                       sub=0.25,
                                       type='chain',
                                       slash=1)
    truth = population.invcov.numpy()
    n = 1000
    d = population.dim

    print(truth)
    dist, sample, _, S = population.generate(n, numpy_like=True)

    # #############################################################################
    # Generate the data
    n_samples = 60
    n_features = 20

    prng = np.random.RandomState(1)
    prec = make_sparse_spd_matrix(n_features,
                                  alpha=.98,
                                  smallest_coef=.4,
                                  largest_coef=.7,
                                  random_state=prng)
    cov = linalg.inv(prec)
    d = np.sqrt(np.diag(cov))
    cov /= d
    cov /= d[:, np.newaxis]
    prec *= d
    prec *= d[:, np.newaxis]
    X = prng.multivariate_normal(np.zeros(n_features), cov, size=n_samples)
    X -= X.mean(axis=0)
    X /= X.std(axis=0)

    #prec = population.invcov
    # #############################################################################
    # Estimate the covariance
    emp_cov = np.dot(X.T, X) / n_samples

    model = GraphicalLassoCV()
    model.fit(sample)
    cov_ = model.covariance_
    prec_ = model.precision_

    lw_cov_, _ = ledoit_wolf(X)
    lw_prec_ = linalg.inv(lw_cov_)

    # #############################################################################
    # Plot the results
    plt.figure(figsize=(10, 6))
    plt.subplots_adjust(left=0.02, right=0.98)

    # plot the covariances
    covs = [('Empirical', emp_cov), ('Ledoit-Wolf', lw_cov_),
            ('GraphicalLassoCV', cov_), ('True', cov)]
    vmax = cov_.max()
    for i, (name, this_cov) in enumerate(covs):
        plt.subplot(2, 4, i + 1)
        plt.imshow(this_cov,
                   interpolation='nearest',
                   vmin=-vmax,
                   vmax=vmax,
                   cmap=plt.cm.RdBu_r)
        plt.xticks(())
        plt.yticks(())
        plt.title('%s covariance' % name)

    # plot the precisions
    precs = [('Empirical', linalg.inv(emp_cov)), ('Ledoit-Wolf', lw_prec_),
             ('GraphicalLasso', prec_), ('True', prec)]
    vmax = .9 * prec_.max()
    for i, (name, this_prec) in enumerate(precs):
        ax = plt.subplot(2, 4, i + 5)
        plt.imshow(np.ma.masked_equal(this_prec, 0),
                   interpolation='nearest',
                   vmin=-vmax,
                   vmax=vmax,
                   cmap=plt.cm.RdBu_r)
        plt.xticks(())
        plt.yticks(())
        plt.title('%s precision' % name)
        if hasattr(ax, 'set_facecolor'):
            ax.set_facecolor('.7')
        else:
            ax.set_axis_bgcolor('.7')

    # plot the model selection metric
    plt.figure(figsize=(4, 3))
    plt.axes([.2, .15, .75, .7])
    plt.plot(model.cv_alphas_, np.mean(model.grid_scores_, axis=1), 'o-')
    plt.axvline(model.alpha_, color='.5')
    plt.title('Model selection')
    plt.ylabel('Cross-validation score')
    plt.xlabel('alpha')

    plt.show()
Ejemplo n.º 18
0
    ns = nitk.NeighbourhoodSelectionColumnwiseCV()
    ns.fit(X)
    tpr, fpr, prec = nitk.methods.calculate_matrix_accuracy(K, ns.precision_)
    ns_f1[i] = nitk.methods.calculate_f1_score(tpr, prec)

    te = nitk.ThresholdEstimatorCV()
    te.fit(X)
    tpr, fpr, prec = nitk.methods.calculate_matrix_accuracy(K, te.covariance_)
    ts_f1[i] = nitk.methods.calculate_f1_score(tpr, prec)

    sc = nitk.SCIOColumnwiseCV()
    sc.fit(X)
    tpr, fpr, prec = nitk.methods.calculate_matrix_accuracy(K, sc.precision_)
    sc_f1[i] = nitk.methods.calculate_f1_score(tpr, prec)

    gl = GraphicalLassoCV()
    gl.fit(X)
    tpr, fpr, prec = nitk.methods.calculate_matrix_accuracy(K, gl.precision_)
    gl_f1[i] = nitk.methods.calculate_f1_score(tpr, prec)

    sli = nitk.ScaledLassoInference()
    sli.fit(X)
    tpr, fpr, prec = nitk.methods.calculate_matrix_accuracy(K, sli.precision_)
    sli_f1[i] = nitk.methods.calculate_f1_score(tpr, prec)

    cli = nitk.CLIMECV()
    cli.fit(X)
    tpr, fpr, prec = nitk.methods.calculate_matrix_accuracy(K, cli.precision_)
    cli_f1[i] = nitk.methods.calculate_f1_score(tpr, prec)
print("Graphical Lasso & %s & %s & %6.3f $\pm$ %6.3f" %
      (p, n, np.mean(gl_f1), np.std(gl_f1)))
Ejemplo n.º 19
0
def main():
    # 'Date', 'Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume'
    df = pd.read_csv(pt.join(DATA_ROOT, '1999_2018_complete.csv'))

    # drop malaysia and venezuela
    df.drop(['malaysia', 'venezuela'], axis=1, inplace=True)
    # print(df.columns[2:])

    data = df.values[:, 2:]
    data = preprocessing.scale(data, axis=1)
    print(data.shape)

    # plt.plot(data[:, 6], c='b', label='Japan')
    # plt.plot(data[:, 13], c='g', label='Sri Lanka')
    # plt.legend()
    # plt.show()

    # Estimate the covariance
    emp_cov = np.dot(data.T, data) / data.shape[0]

    for count in range(20):
        temp_data = data[count * 242:(count + 1) * 242]

        # GraphicalLasso
        model = GraphicalLassoCV()
        model.fit(temp_data)
        cov_ = model.covariance_
        prec_ = model.precision_

        # print(model.alpha_)
        # print(model.cv_alphas_)
        # print(model.grid_scores_)
        # print(model.n_iter_)

        # Ledoit-Wolf
        lw_cov_, _ = ledoit_wolf(data)
        lw_prec_ = linalg.inv(lw_cov_)

        # #############################################################################
        # Plot the results
        # plt.figure(figsize=(8, 6))
        # plt.subplots_adjust(left=0.02, right=0.98)
        #
        # # plot the covariances
        # covs = [('Empirical', emp_cov), ('Ledoit-Wolf',
        #                                  lw_cov_), ('GraphicalLassoCV', cov_)]
        # vmax = cov_.max()
        # for i, (name, this_cov) in enumerate(covs):
        #     plt.subplot(2, 3, i + 1)
        #     plt.imshow(this_cov, interpolation='nearest', vmin=-vmax, vmax=vmax,
        #                cmap=plt.cm.RdBu_r)
        #     plt.xticks(())
        #     plt.yticks(())
        #     plt.title('%s covariance' % name)
        #
        # # plot the precisions
        # precs = [('Empirical', linalg.inv(emp_cov)), ('Ledoit-Wolf', lw_prec_),
        #          ('GraphicalLasso', prec_)]
        # vmax = .9 * prec_.max()
        # for i, (name, this_prec) in enumerate(precs):
        #     ax = plt.subplot(2, 3, i + 4)
        #     plt.imshow(np.ma.masked_equal(this_prec, 0),
        #                interpolation='nearest', vmin=-vmax, vmax=vmax,
        #                cmap=plt.cm.RdBu_r)
        #     plt.xticks(())
        #     plt.yticks(())
        #     plt.title('%s precision' % name)
        #     if hasattr(ax, 'set_facecolor'):
        #         ax.set_facecolor('.7')
        #     else:
        #         ax.set_axis_bgcolor('.7')
        # plt.show()
        # print(prec_)
        name = 'GraphicalLasso'
        this_prec = prec_
        vmax = .9 * prec_.max()
        plt.figure()
        ax = plt.subplot(1, 1, 1)
        plt.imshow(np.ma.masked_equal(this_prec, 0),
                   interpolation='nearest',
                   vmin=-vmax,
                   vmax=vmax,
                   cmap=plt.cm.RdBu_r)
        plt.xticks(())
        plt.yticks(())
        plt.title('year: %d' % (1999 + count))
        if hasattr(ax, 'set_facecolor'):
            ax.set_facecolor('.7')
        else:
            ax.set_axis_bgcolor('.7')
        plt.show()
Ejemplo n.º 20
0
def main():
    mean = torch.tensor(np.zeros(32), dtype=torch.float32)
    diag = torch.tensor(np.ones(32), dtype=torch.float32)
    X = torch.eye(32, dtype=torch.float32)
    X[5, 10] = X[10, 5] = X[19, 20] = X[20, 19] = X[1, 31] = X[31, 1] = -0.5

    population = Gaussian_Distribution(mean=mean,
                                       diag=diag,
                                       sub=-0.2,
                                       type='DIY',
                                       slash=1,
                                       prec=X)
    truth = population.invcov.numpy()
    n = 400
    p = population.dim

    print(truth)
    heatmap(truth)

    data = pd.read_csv("chain.csv")
    sample = data.values[1:, 1:]
    sample = z_score(sample)
    emp_cov = sample_cov(sample)

    score = dict()
    score['log_lik'] = []
    score['AIC'] = []
    score['non_zero'] = []
    alpha_list = np.hstack((np.arange(1e-5, 0.1,
                                      0.002), np.arange(0.1, 0.3, 0.01)))
    data = np.array(sample)
    for alpha in alpha_list:
        out_dict = cross_val_score_ProxGrad(data, alpha=alpha, type='FISTA')
        score['log_lik'].append(out_dict['log_lik'])
        score['AIC'].append(out_dict['AIC'])
        score['non_zero'].append(out_dict['non_zero'])
    plt.plot(alpha_list, score['log_lik'])
    plt.show()
    plt.plot(alpha_list, score['AIC'])
    plt.show()
    plt.plot(alpha_list, score['non_zero'])
    plt.show()

    model = ProxGrad()
    l = len(alpha_list)
    alpha = 0
    log_lik = -1e12
    for i in range(0, l):
        if score['log_lik'][i] > log_lik:
            alpha = alpha_list[i]
            log_lik = score['log_lik'][i]
    print(alpha)
    prec = model.fit_FISTA(emp_cov, alpha)
    heatmap(prec)
    print('nonzero:', L0_penal(prec))

    alpha = 0
    aic = 1e12
    for i in range(0, l):
        if score['AIC'][i] < aic:
            alpha = alpha_list[i]
            aic = score['AIC'][i]
    print(alpha)
    prec = model.fit_FISTA(emp_cov, alpha)
    heatmap(prec)
    print('nonzero:', L0_penal(prec))

    model = GraphicalLassoCV(tol=1e-8)
    model.fit(sample)
    heatmap(model.precision_)
    print('nonzero:', L0_penal(model.precision_))
Ejemplo n.º 21
0
def get_conn_matrix(time_series, conn_model, dir_path, node_size, smooth, dens_thresh, network, ID, roi, min_span_tree,
                    disp_filt, parc, prune, atlas, uatlas, labels, coords, norm, binary,
                    hpass, extract_strategy):
    """
    Computes a functional connectivity matrix based on a node-extracted time-series array.
    Includes a library of routines across Nilearn, scikit-learn, and skggm packages, among others.

    Parameters
    ----------
    time_series : array
        2D m x n array consisting of the time-series signal for each ROI node where m = number of scans and
        n = number of ROI's.
    conn_model : str
       Connectivity estimation model (e.g. corr for correlation, cov for covariance, sps for precision covariance,
       partcorr for partial correlation). sps type is used by default.
    dir_path : str
        Path to directory containing subject derivative data for given run.
    node_size : int
        Spherical centroid node size in the case that coordinate-based centroids
        are used as ROI's.
    smooth : int
        Smoothing width (mm fwhm) to apply to time-series when extracting signal from ROI's.
    dens_thresh : bool
        Indicates whether a target graph density is to be used as the basis for
        thresholding.
    network : str
        Resting-state network based on Yeo-7 and Yeo-17 naming (e.g. 'Default') used to filter nodes in the study of
        brain subgraphs.
    ID : str
        A subject id or other unique identifier.
    roi : str
        File path to binarized/boolean region-of-interest Nifti1Image file.
    min_span_tree : bool
        Indicates whether local thresholding from the Minimum Spanning Tree
        should be used.
    disp_filt : bool
        Indicates whether local thresholding using a disparity filter and
        'backbone network' should be used.
    parc : bool
        Indicates whether to use parcels instead of coordinates as ROI nodes.
    prune : bool
        Indicates whether to prune final graph of disconnected nodes/isolates.
    atlas : str
        Name of atlas parcellation used.
    uatlas : str
        File path to atlas parcellation Nifti1Image in MNI template space.
    labels : list
        List of string labels corresponding to ROI nodes.
    coords : list
        List of (x, y, z) tuples corresponding to a coordinate atlas used or
        which represent the center-of-mass of each parcellation node.
    norm : int
        Indicates method of normalizing resulting graph.
    binary : bool
        Indicates whether to binarize resulting graph edges to form an
        unweighted graph.
    hpass : bool
        High-pass filter values (Hz) to apply to node-extracted time-series.
    extract_strategy : str 
        The name of a valid function used to reduce the time-series region extraction.

    Returns
    -------
    conn_matrix : array
        Adjacency matrix stored as an m x n array of nodes and edges.
    conn_model : str
       Connectivity estimation model (e.g. corr for correlation, cov for covariance, sps for precision covariance,
       partcorr for partial correlation). sps type is used by default.
    dir_path : str
        Path to directory containing subject derivative data for given run.
    node_size : int
        Spherical centroid node size in the case that coordinate-based centroids
        are used as ROI's for tracking.
    smooth : int
        Smoothing width (mm fwhm) to apply to time-series when extracting signal from ROI's.
    dens_thresh : bool
        Indicates whether a target graph density is to be used as the basis for
        thresholding.
    network : str
        Resting-state network based on Yeo-7 and Yeo-17 naming (e.g. 'Default') used to filter nodes in the study of
        brain subgraphs.
    ID : str
        A subject id or other unique identifier.
    roi : str
        File path to binarized/boolean region-of-interest Nifti1Image file.
    min_span_tree : bool
        Indicates whether local thresholding from the Minimum Spanning Tree
        should be used.
    disp_filt : bool
        Indicates whether local thresholding using a disparity filter and
        'backbone network' should be used.
    parc : bool
        Indicates whether to use parcels instead of coordinates as ROI nodes.
    prune : bool
        Indicates whether to prune final graph of disconnected nodes/isolates.
    atlas : str
        Name of atlas parcellation used.
    uatlas : str
        File path to atlas parcellation Nifti1Image in MNI template space.
    labels : list
        List of string labels corresponding to graph nodes.
    coords : list
        List of (x, y, z) tuples corresponding to a coordinate atlas used or
        which represent the center-of-mass of each parcellation node.
    norm : int
        Indicates method of normalizing resulting graph.
    binary : bool
        Indicates whether to binarize resulting graph edges to form an
        unweighted graph.
    hpass : bool
        High-pass filter values (Hz) to apply to node-extracted time-series.
    extract_strategy : str 
        The name of a valid function used to reduce the time-series region extraction.

    References
    ----------
    .. [1] Varoquaux, G., & Craddock, R. C. (2013). Learning and comparing functional connectomes
      across subjects. NeuroImage. https://doi.org/10.1016/j.neuroimage.2013.04.007
    .. [2] Jason Laska, Manjari Narayan, 2017. skggm 0.2.7: A scikit-learn compatible package
      for Gaussian and related Graphical Models. doi:10.5281/zenodo.830033

    """
    from nilearn.connectome import ConnectivityMeasure
    from sklearn.covariance import GraphicalLassoCV

    conn_matrix = None
    if conn_model == 'corr' or conn_model == 'cor' or conn_model == 'correlation':
        # credit: nilearn
        print('\nComputing correlation matrix...\n')
        conn_measure = ConnectivityMeasure(kind='correlation')
        conn_matrix = conn_measure.fit_transform([time_series])[0]
    elif conn_model == 'partcorr' or conn_model == 'parcorr' or conn_model == 'partialcorrelation':
        # credit: nilearn
        print('\nComputing partial correlation matrix...\n')
        conn_measure = ConnectivityMeasure(kind='partial correlation')
        conn_matrix = conn_measure.fit_transform([time_series])[0]
    elif conn_model == 'cov' or conn_model == 'covariance' or conn_model == 'covar' or conn_model == 'sps' or \
        conn_model == 'sparse' or conn_model == 'precision':
        # Fit estimator to matrix to get sparse matrix
        estimator_shrunk = None
        estimator = GraphicalLassoCV(cv=5)
        try:
            print('\nComputing covariance...\n')
            estimator.fit(time_series)
        except:
            print('Unstable Lasso estimation--Attempting to re-run by first applying shrinkage...')
            try:
                from sklearn.covariance import GraphicalLasso, empirical_covariance, shrunk_covariance
                emp_cov = empirical_covariance(time_series)
                for i in np.arange(0.8, 0.99, 0.01):
                    shrunk_cov = shrunk_covariance(emp_cov, shrinkage=i)
                    alphaRange = 10.0 ** np.arange(-8, 0)
                    for alpha in alphaRange:
                        try:
                            estimator_shrunk = GraphicalLasso(alpha)
                            estimator_shrunk.fit(shrunk_cov)
                            print(f"Retrying covariance matrix estimate with alpha={alpha}")
                            if estimator_shrunk is None:
                                pass
                            else:
                                break
                        except:
                            print(f"Covariance estimation failed with shrinkage at alpha={alpha}")
                            continue
            except ValueError:
                print('Unstable Lasso estimation! Shrinkage failed. A different connectivity model may be needed.')
        if estimator is None and estimator_shrunk is None:
            raise RuntimeError('\nERROR: Covariance estimation failed.')
        if conn_model == 'sps' or conn_model == 'sparse' or conn_model == 'precision':
            if estimator_shrunk is None:
                print('\nFetching precision matrix from covariance estimator...\n')
                conn_matrix = -estimator.precision_
            else:
                print('\nFetching shrunk precision matrix from covariance estimator...\n')
                conn_matrix = -estimator_shrunk.precision_
        elif conn_model == 'cov' or conn_model == 'covariance' or conn_model == 'covar':
            if estimator_shrunk is None:
                print('\nFetching covariance matrix from covariance estimator...\n')
                conn_matrix = estimator.covariance_
            else:
                conn_matrix = estimator_shrunk.covariance_
    elif conn_model == 'QuicGraphicalLasso':

        try:
            from inverse_covariance import QuicGraphicalLasso
        except ImportError:
            print('Cannot run QuicGraphLasso. Skggm not installed!')

        # Compute the sparse inverse covariance via QuicGraphLasso
        # credit: skggm
        model = QuicGraphicalLasso(
            init_method='cov',
            lam=0.5,
            mode='default',
            verbose=1)
        print('\nCalculating QuicGraphLasso precision matrix using skggm...\n')
        model.fit(time_series)
        conn_matrix = -model.precision_
    elif conn_model == 'QuicGraphicalLassoCV':
        try:
            from inverse_covariance import QuicGraphicalLassoCV
        except ImportError:
            print('Cannot run QuicGraphLassoCV. Skggm not installed!')

        # Compute the sparse inverse covariance via QuicGraphLassoCV
        # credit: skggm
        model = QuicGraphicalLassoCV(
            init_method='cov',
            verbose=1)
        print('\nCalculating QuicGraphLassoCV precision matrix using skggm...\n')
        model.fit(time_series)
        conn_matrix = -model.precision_
    elif conn_model == 'QuicGraphicalLassoEBIC':
        try:
            from inverse_covariance import QuicGraphicalLassoEBIC
        except ImportError:
            print('Cannot run QuicGraphLassoEBIC. Skggm not installed!')

        # Compute the sparse inverse covariance via QuicGraphLassoEBIC
        # credit: skggm
        model = QuicGraphicalLassoEBIC(
            init_method='cov',
            verbose=1)
        print('\nCalculating QuicGraphLassoEBIC precision matrix using skggm...\n')
        model.fit(time_series)
        conn_matrix = -model.precision_
    elif conn_model == 'AdaptiveQuicGraphicalLasso':
        try:
            from inverse_covariance import AdaptiveQuicGraphicalLasso, QuicGraphicalLassoEBIC
        except ImportError:
            print('Cannot run AdaptiveGraphLasso. Skggm not installed!')

        # Compute the sparse inverse covariance via
        # AdaptiveGraphLasso + QuicGraphLassoEBIC + method='binary'
        # credit: skggm
        model = AdaptiveQuicGraphicalLasso(
            estimator=QuicGraphicalLassoEBIC(
                init_method='cov',
            ),
            method='binary',
        )
        print('\nCalculating AdaptiveQuicGraphLasso precision matrix using skggm...\n')
        model.fit(time_series)
        conn_matrix = -model.estimator_.precision_
    else:
        raise ValueError('\nERROR! No connectivity model specified at runtime. Select a valid estimator using the '
                         '-mod flag.')

    # Enforce symmetry
    conn_matrix = np.maximum(conn_matrix, conn_matrix.T)

    if conn_matrix.shape < (2, 2):
        raise RuntimeError('\nERROR! Matrix estimation selection yielded an empty or 1-dimensional graph. '
                           'Check time-series for errors or try using a different atlas')

    coords = np.array(coords)
    labels = np.array(labels)

    del time_series

    return (conn_matrix, conn_model, dir_path, node_size, smooth, dens_thresh, network, ID, roi, min_span_tree,
            disp_filt, parc, prune, atlas, uatlas, labels, coords, norm, binary, hpass, extract_strategy)
Ejemplo n.º 22
0
                              random_state=prng)
cov = linalg.inv(prec)
d = np.sqrt(np.diag(cov))
cov /= d
cov /= d[:, np.newaxis]
prec *= d
prec *= d[:, np.newaxis]
X = prng.multivariate_normal(np.zeros(n_features), cov, size=n_samples)
X -= X.mean(axis=0)
X /= X.std(axis=0)

# #############################################################################
# Estimate the covariance
emp_cov = np.dot(X.T, X) / n_samples

model = GraphicalLassoCV()
model.fit(X)
cov_ = model.covariance_
prec_ = model.precision_

lw_cov_, _ = ledoit_wolf(X)
lw_prec_ = linalg.inv(lw_cov_)

# #############################################################################
# Plot the results
plt.figure(figsize=(10, 6))
plt.subplots_adjust(left=0.02, right=0.98)

# plot the covariances
covs = [
    ("Empirical", emp_cov),
                         standardize=True,
                         memory='nilearn_cache',
                         verbose=5)

time_series = masker.fit_transform(data.func[0], confounds=data.confounds)

##############################################################################
# Compute the sparse inverse covariance
# --------------------------------------
try:
    from sklearn.covariance import GraphicalLassoCV
except ImportError:
    # for Scitkit-Learn < v0.20.0
    from sklearn.covariance import GraphLassoCV as GraphicalLassoCV

estimator = GraphicalLassoCV()
estimator.fit(time_series)

##############################################################################
# Display the connectome matrix
# ------------------------------
from nilearn import plotting
# Display the covariance

# The covariance can be found at estimator.covariance_
plotting.plot_matrix(estimator.covariance_,
                     labels=labels,
                     figure=(9, 7),
                     vmax=1,
                     vmin=-1,
                     title='Covariance')
Ejemplo n.º 24
0
def get_conn_matrix(time_series, conn_model, dir_path, node_size, smooth,
                    dens_thresh, network, ID, roi, min_span_tree, disp_filt,
                    parc, prune, atlas_select, uatlas_select, label_names,
                    coords, c_boot, norm, binary):
    from nilearn.connectome import ConnectivityMeasure
    from sklearn.covariance import GraphicalLassoCV

    conn_matrix = None
    if conn_model == 'corr' or conn_model == 'cor' or conn_model == 'correlation':
        # credit: nilearn
        print('\nComputing correlation matrix...\n')
        conn_measure = ConnectivityMeasure(kind='correlation')
        conn_matrix = conn_measure.fit_transform([time_series])[0]
    elif conn_model == 'partcorr' or conn_model == 'parcorr' or conn_model == 'partialcorrelation':
        # credit: nilearn
        print('\nComputing partial correlation matrix...\n')
        conn_measure = ConnectivityMeasure(kind='partial correlation')
        conn_matrix = conn_measure.fit_transform([time_series])[0]
    elif conn_model == 'cov' or conn_model == 'covariance' or conn_model == 'covar' or conn_model == 'sps' or conn_model == 'sparse' or conn_model == 'precision':
        # Fit estimator to matrix to get sparse matrix
        estimator_shrunk = None
        estimator = GraphicalLassoCV(cv=5)
        try:
            print('\nComputing covariance...\n')
            estimator.fit(time_series)
        except:
            print(
                'Unstable Lasso estimation--Attempting to re-run by first applying shrinkage...'
            )
            try:
                from sklearn.covariance import GraphicalLasso, empirical_covariance, shrunk_covariance
                emp_cov = empirical_covariance(time_series)
                for i in np.arange(0.8, 0.99, 0.01):
                    shrunk_cov = shrunk_covariance(emp_cov, shrinkage=i)
                    alphaRange = 10.0**np.arange(-8, 0)
                    for alpha in alphaRange:
                        try:
                            estimator_shrunk = GraphicalLasso(alpha)
                            estimator_shrunk.fit(shrunk_cov)
                            print(
                                "Retrying covariance matrix estimate with alpha=%s"
                                % alpha)
                            if estimator_shrunk is None:
                                pass
                            else:
                                break
                        except:
                            print(
                                "Covariance estimation failed with shrinkage at alpha=%s"
                                % alpha)
                            continue
            except ValueError:
                print(
                    'Unstable Lasso estimation! Shrinkage failed. A different connectivity model may be needed.'
                )
        if estimator is None and estimator_shrunk is None:
            raise RuntimeError('\nERROR: Covariance estimation failed.')
        if conn_model == 'sps' or conn_model == 'sparse' or conn_model == 'precision':
            if estimator_shrunk is None:
                print(
                    '\nFetching precision matrix from covariance estimator...\n'
                )
                conn_matrix = -estimator.precision_
            else:
                print(
                    '\nFetching shrunk precision matrix from covariance estimator...\n'
                )
                conn_matrix = -estimator_shrunk.precision_
        elif conn_model == 'cov' or conn_model == 'covariance' or conn_model == 'covar':
            if estimator_shrunk is None:
                print(
                    '\nFetching covariance matrix from covariance estimator...\n'
                )
                conn_matrix = estimator.covariance_
            else:
                conn_matrix = estimator_shrunk.covariance_
    elif conn_model == 'QuicGraphicalLasso':
        try:
            from inverse_covariance import QuicGraphicalLasso
        except ImportError:
            print('Cannot run QuicGraphLasso. Skggm not installed!')

        # Compute the sparse inverse covariance via QuicGraphLasso
        # credit: skggm
        model = QuicGraphicalLasso(init_method='cov',
                                   lam=0.5,
                                   mode='default',
                                   verbose=1)
        print('\nCalculating QuicGraphLasso precision matrix using skggm...\n')
        model.fit(time_series)
        conn_matrix = -model.precision_
    elif conn_model == 'QuicGraphLassoCV':
        try:
            from inverse_covariance import QuicGraphicalLassoCV
        except ImportError:
            print('Cannot run QuicGraphLassoCV. Skggm not installed!')

        # Compute the sparse inverse covariance via QuicGraphLassoCV
        # credit: skggm
        model = QuicGraphicalLassoCV(init_method='cov', verbose=1)
        print(
            '\nCalculating QuicGraphLassoCV precision matrix using skggm...\n')
        model.fit(time_series)
        conn_matrix = -model.precision_
    elif conn_model == 'QuicGraphicalLassoEBIC':
        try:
            from inverse_covariance import QuicGraphicalLassoEBIC
        except ImportError:
            print('Cannot run QuicGraphLassoEBIC. Skggm not installed!')

        # Compute the sparse inverse covariance via QuicGraphLassoEBIC
        # credit: skggm
        model = QuicGraphicalLassoEBIC(init_method='cov', verbose=1)
        print(
            '\nCalculating QuicGraphLassoEBIC precision matrix using skggm...\n'
        )
        model.fit(time_series)
        conn_matrix = -model.precision_
    elif conn_model == 'AdaptiveQuicGraphLasso':
        try:
            from inverse_covariance import AdaptiveQuicGraphicalLasso, QuicGraphicalLassoEBIC
        except ImportError:
            print('Cannot run AdaptiveGraphLasso. Skggm not installed!')

        # Compute the sparse inverse covariance via
        # AdaptiveGraphLasso + QuicGraphLassoEBIC + method='binary'
        # credit: skggm
        model = AdaptiveQuicGraphicalLasso(
            estimator=QuicGraphicalLassoEBIC(init_method='cov', ),
            method='binary',
        )
        print(
            '\nCalculating AdaptiveQuicGraphLasso precision matrix using skggm...\n'
        )
        model.fit(time_series)
        conn_matrix = -model.estimator_.precision_
    else:
        raise ValueError(
            '\nERROR! No connectivity model specified at runtime. Select a valid estimator using the '
            '-mod flag.')

    if conn_matrix.shape < (2, 2):
        raise RuntimeError(
            '\nERROR! Matrix estimation selection yielded an empty or 1-dimensional graph. '
            'Check time-series for errors or try using a different atlas')

    coords = np.array(coords)
    label_names = np.array(label_names)
    return conn_matrix, conn_model, dir_path, node_size, smooth, dens_thresh, network, ID, roi, min_span_tree, disp_filt, parc, prune, atlas_select, uatlas_select, label_names, coords, c_boot, norm, binary
Ejemplo n.º 25
0
    plotting.plot_matrix(gsc.precisions_[..., n],
                         axes=ax,
                         vmin=-max_precision,
                         vmax=max_precision,
                         colorbar=False)
    if n == 0:
        plt.title("group-sparse\n$\\alpha=%.2f$" % gsc.alpha_)

# Fit one graph lasso per subject
try:
    from sklearn.covariance import GraphicalLassoCV
except ImportError:
    # for Scitkit-Learn < v0.20.0
    from sklearn.covariance import GraphLassoCV as GraphicalLassoCV

gl = GraphicalLassoCV(verbose=1)

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

    ax = plt.subplot(n_displayed, 4, 4 * n + 3)
    max_precision = gl.precision_.max()
    plotting.plot_matrix(gl.precision_,
                         axes=ax,
                         vmin=-max_precision,
                         vmax=max_precision,
                         colorbar=False)
    if n == 0:
        plt.title("graph lasso")
    plt.ylabel("$\\alpha=%.2f$" % gl.alpha_)
Ejemplo n.º 26
0
 def extract_vector(self):
     if self.level == 2:
         self.df_cifti_load = pd.DataFrame(
             self.fmri_data_np_arr.mean(axis=2))
     if type(self.seed_ROI_name) == list and len(self.seed_ROI_name) > 1:
         if self.seed_analysis_output == 'parcellated':
             self.df_cifti_load = pd.DataFrame(
                 self.parcellated_cifti_load.get_fdata())
             self.df_cifti_load.columns = self.parcel_labels
             self.df_cifti_load['avg'] = self.df_cifti_load[
                 self.seed_ROI_name].mean(axis=1)
             self.parcel_labels = self.df_cifti_load.columns.to_list()
         else:
             self.df_cifti_load = pd.DataFrame(self.cifti_load.get_fdata())
             df_parcellated_cifti_load = pd.DataFrame(
                 self.parcellated_cifti_load.get_fdata())
             df_parcellated_cifti_load.columns = self.parcel_labels
             self.df_cifti_load['avg'] = df_parcellated_cifti_load[
                 self.seed_ROI_name].mean(axis=1)
         self.seed_ROI_name = 'avg'
     else:
         if self.seed_analysis_output == 'dense':
             self.df_cifti_load = pd.DataFrame(self.cifti_load.get_fdata())
             df_parcellated_cifti_load = pd.DataFrame(
                 self.parcellated_cifti_load.get_fdata())
             df_parcellated_cifti_load.columns = self.parcel_labels
             self.df_cifti_load[
                 self.seed_ROI_name] = df_parcellated_cifti_load[
                     self.seed_ROI_name]
         else:
             self.df_cifti_load = pd.DataFrame(
                 self.parcellated_cifti_load.get_fdata())
     cifti_np_array = self.df_cifti_load.to_numpy()
     if self.method == 'correlation':
         #Pearson correlation coefficients with LedoitWolf covariance estimator
         #measure = ConnectivityMeasure(kind='correlation',cov_estimator='LedoitWolf')
         #Pearson correlation coefficients based oemperical covariance (i.e. standard)
         measure = ConnectivityMeasure(kind='correlation',
                                       cov_estimator=EmpiricalCovariance())
     elif self.method == 'covariance':
         #LedoitWolf estimator
         measure = ConnectivityMeasure(kind='covariance')
     elif self.method == 'partial_correlation':
         # Partial correlation with LedoitWolf covariance estimator
         measure = ConnectivityMeasure(kind='partial correlation')
     elif self.method == 'precision':
         measure = ConnectivityMeasure(kind='precision')
     elif 'sparse' in self.method:
         measure = GraphicalLassoCV()
     if 'sparse' in self.method:
         measure.fit(cifti_np_array)
         if 'covariance' in self.method:
             network_matrix = measure.covariance_
         elif 'precision' in self.method:
             network_matrix = measure.precision_
     else:
         network_matrix = measure.fit_transform([cifti_np_array])[0]
     df_network_matrix = pd.DataFrame(network_matrix)
     df_network_matrix.columns = self.parcel_labels
     if self.seed_ROI_name == 'avg':
         # take everything except last element, i.e. avg. Need to do this because downstream this object must match grayordinate_file
         self.r_functional_vector = df_network_matrix[
             self.seed_ROI_name][:-1].to_numpy()
     else:
         self.r_functional_vector = np.squeeze(
             df_network_matrix[self.seed_ROI_name].to_numpy())
     self.z_functional_vector = 0.5 * (
         np.log(1 + self.r_functional_vector) -
         np.log(1 - self.r_functional_vector))
Ejemplo n.º 27
0
    subject_time_series.append(region_ts)

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

try:
    from sklearn.covariance import GraphicalLassoCV
except ImportError:
    # for Scitkit-Learn < v0.20.0
    from sklearn.covariance import GraphLassoCV as GraphicalLassoCV

gl = GraphicalLassoCV(verbose=2)
gl.fit(np.concatenate(subject_time_series))

##############################################################################
# Displaying results
# -------------------
atlas_img = msdl_atlas_dataset.maps
atlas_region_coords = plotting.find_probabilistic_atlas_cut_coords(atlas_img)
labels = msdl_atlas_dataset.labels

plotting.plot_connectome(gl.covariance_,
                         atlas_region_coords,
                         edge_threshold='90%',
                         title="Covariance",
                         display_mode="lzr")
plotting.plot_connectome(-gl.precision_,
                              random_state=prng)
cov = linalg.inv(prec)
d = np.sqrt(np.diag(cov))
cov /= d
cov /= d[:, np.newaxis]
prec *= d
prec *= d[:, np.newaxis]
X = prng.multivariate_normal(np.zeros(n_features), cov, size=n_samples)
X -= X.mean(axis=0)
X /= X.std(axis=0)

# #############################################################################
# Estimate the covariance
emp_cov = np.dot(X.T, X) / n_samples

model = GraphicalLassoCV(cv=5)
model.fit(X)
cov_ = model.covariance_
prec_ = model.precision_

lw_cov_, _ = ledoit_wolf(X)
lw_prec_ = linalg.inv(lw_cov_)

# #############################################################################
# Plot the results
plt.figure(figsize=(10, 6))
plt.subplots_adjust(left=0.02, right=0.98)

# plot the covariances
covs = [('Empirical', emp_cov), ('Ledoit-Wolf', lw_cov_),
        ('GraphicalLassoCV', cov_), ('True', cov)]
# ---------------------
#
# We start by estimating the signal **covariance** matrix. Here the
# number of ROIs exceeds the number of samples,
print('time series has {0} samples'.format(timeseries.shape[0]))

###############################################################################
# in which situation the graphical lasso **sparse inverse covariance**
# estimator captures well the covariance **structure**.
try:
    from sklearn.covariance import GraphicalLassoCV
except ImportError:
    # for Scitkit-Learn < v0.20.0
    from sklearn.covariance import GraphLassoCV as GraphicalLassoCV

covariance_estimator = GraphicalLassoCV(cv=3, verbose=1)

###############################################################################
# We just fit our regions signals into the `GraphicalLassoCV` object
covariance_estimator.fit(timeseries)

###############################################################################
# and get the ROI-to-ROI covariance matrix.
matrix = covariance_estimator.covariance_
print('Covariance matrix has shape {0}.'.format(matrix.shape))

###############################################################################
# Plot matrix, graph, and strength
# --------------------------------
#
# We use `:func: nilearn.plotting.plot_matrix` to visualize our correlation matrix
Ejemplo n.º 30
0
def get_covariance(data,
                   method,
                   lambda_val='CV',
                   do_scale=False,
                   n_cv_folds=None):

    # default cov if it is not calculated properly
    cov = -1

    # scale timecourse
    if do_scale:

        data = scale(data, axis=1)

    # select method
    if method == 'QUIC':

        if lambda_val == 'CV':

            # set up model
            model = QuicGraphicalLassoCV(cv=n_cv_folds)

            # fit data to model and return resulting covariance
            model.fit(np.transpose(data))
            return model.covariance_

        elif lambda_val == 'EBIC':

            # set up model
            model = QuicGraphicalLassoEBIC()

            # fit data to model and return resulting covariance
            model.fit(np.transpose(data))
            return model.covariance_

        elif isinstance(lambda_val,
                        float) and lambda_val > 0 and lambda_val < 1:

            # set up model
            model = QuicGraphicalLasso(lam=lambda_val)

            # fit data to model and return resulting covariance
            model.fit(data)
            return model.covariance_

        else:

            print('Error in QUIC covariance:')
            print(
                'lambda_val must be a float between 0 and 1, "CV" to find the best value by cross-validation, or "EBIC" to use extended Bayesian information criterion for model selection.'
            )

    elif method == 'graphLasso':

        # transpose data as graphLasso likes it this way round
        data = np.transpose(data)

        # select whether to use supplied regularisation parameter or find the
        # best regularisation parameter by cross validation and maximum likelihood
        # use scikit-learn implementation of graph lasso and CV graph lasso
        if lambda_val == 'CV':

            try:

                model = GraphicalLassoCV(max_iter=1500,
                                         cv=n_cv_folds,
                                         assume_centered=True)
                model.fit(data)
                cov = model.covariance_

            except:

                print(
                    'An error in cross validated graphLasso calculation occured.'
                )

        elif isinstance(lambda_val,
                        float) and lambda_val > 0 and lambda_val < 1:

            try:

                model = GraphicalLasso(alpha=lambda_val,
                                       mode='cd',
                                       tol=0.0001,
                                       max_iter=1500,
                                       verbose=False)
                model.fit(data)
                cov = model.covariance_

            except (FloatingPointError, e):

                print(
                    'A floating point error in cross validated graphLasso calculation occured.'
                )
                print(e)

        else:

            print('Error in graphLasso covariance:')
            print(
                'lambda_val must be a float between 0 and 1, or "CV" to find the best value by cross-validation'
            )

    # select method
    else:

        print('Method must be one of "graphLasso" or "QUIC".')

    return cov