Ejemplo n.º 1
0
def quic_graph_lasso_ebic(X, gamma=0):
    '''Run QuicGraphLassoEBIC with gamma.

    QuicGraphLassoEBIC is a convenience class.  Results should be identical to
    those obtained via quic_graph_lasso_ebic_manual.
    '''
    print 'QuicGraphLassoEBIC with:'
    print '   mode: path'
    print '   gamma: {}'.format(gamma)
    model = QuicGraphLassoEBIC(lam=1.0, init_method='cov', gamma=gamma)
    model.fit(X)
    print '   len(path lams): {}'.format(len(model.path_))
    print '   lam_scale_: {}'.format(model.lam_scale_)
    print '   lam_: {}'.format(model.lam_)
    return model.covariance_, model.precision_, model.lam_
Ejemplo n.º 2
0
def adaptive_graph_lasso(X, model_selector, method):
    '''Run QuicGraphLassoCV or QuicGraphLassoEBIC as a two step adaptive fit
    with method of choice (currently: 'binary', 'inverse', 'inverse_squared').

    Compare the support and values to the model-selection estimator.
    '''
    metric = 'log_likelihood'
    print 'Adaptive {} with:'.format(model_selector)
    print '   adaptive-method: {}'.format(method)
    if model_selector == 'QuicGraphLassoCV':
        print '   metric: {}'.format(metric)
        model = AdaptiveGraphLasso(
            estimator=QuicGraphLassoCV(
                cv=2,  # cant deal w more folds at small size
                n_refinements=6,
                init_method='cov',
                score_metric=metric),
            method=method,
        )

    elif model_selector == 'QuicGraphLassoEBIC':
        model = AdaptiveGraphLasso(
            estimator=QuicGraphLassoEBIC(),
            method=method,
        )
    model.fit(X)
    lam_norm_ = np.linalg.norm(model.estimator_.lam_)
    print '   ||lam_||_2: {}'.format(lam_norm_)
    return model.estimator_.covariance_, model.estimator_.precision_, lam_norm_
Ejemplo n.º 3
0
class TestStatisticalPower(object):
    @pytest.mark.parametrize("params_in", [
        ({
            'model_selection_estimator': QuicGraphLassoCV(),
            'n_trials': 20,
            'n_features': 25,
        }),
        ({
            'model_selection_estimator': QuicGraphLassoEBIC(),
            'n_trials': 20,
            'n_features': 10,
            'n_jobs': 2,
        }),
        ({
            'model_selection_estimator': GraphLassoCV(),
            'n_trials': 20,
            'n_features': 20,
            'penalty_': 'alpha_',
        }),
    ])
    def test_integration_statistical_power(self, params_in):
        '''
        Just tests inputs/outputs (not validity of result).
        '''
        X = datasets.load_diabetes().data
        sp = StatisticalPower(**params_in)
        sp.fit(X)

        num_k = 5

        assert np.sum(sp.results_.flat) > 0
        assert sp.results_.shape == (num_k, sp.n_grid_points)
        assert len(sp.ks_) == num_k
        assert len(sp.grid_) == sp.n_grid_points
Ejemplo n.º 4
0
    def test_integration_quic_graph_lasso_ebic(self, params_in, expected):
        '''
        Just tests inputs/outputs (not validity of result).
        '''
        X = datasets.load_diabetes().data
        ic = QuicGraphLassoEBIC(**params_in)
        ic.fit(X)

        result_vec = [
            np.linalg.norm(ic.covariance_),
            np.linalg.norm(ic.precision_),
        ]
        if isinstance(ic.lam_, float):
            result_vec.append(ic.lam_)
        elif isinstance(ic.lam_, np.ndarray):
            assert ic.lam_.shape == params_in['lam'].shape

        print result_vec
        assert_allclose(expected, result_vec, rtol=1e-1)
Ejemplo n.º 5
0
class TestAverageError(object):
    @pytest.mark.parametrize("params_in", [
        ({
            'model_selection_estimator': QuicGraphLassoCV(),
            'n_trials': 20,
            'n_features': 25,
        }),
        ({
            'model_selection_estimator': QuicGraphLassoEBIC(),
            'n_trials': 20,
            'n_features': 10,
            'n_jobs': 2,
        }),
        ({
            'model_selection_estimator': GraphLassoCV(),
            'n_trials': 20,
            'n_features': 20,
            'penalty_': 'alpha_',
        }),
    ])
    def test_integration_statistical_power(self, params_in):
        '''
        Just tests inputs/outputs (not validity of result).
        '''
        X = datasets.load_diabetes().data
        ae = AverageError(**params_in)
        ae.fit(X)

        num_k = 3

        assert np.sum(ae.error_fro_.flat) > 0
        assert ae.error_fro_.shape == (num_k, ae.n_grid_points)
        assert np.sum(ae.error_supp_.flat) > 0
        assert ae.error_supp_.shape == (num_k, ae.n_grid_points)
        assert np.sum(ae.error_fp_.flat) > 0
        assert ae.error_fp_.shape == (num_k, ae.n_grid_points)
        assert np.sum(ae.error_fn_.flat) > 0
        assert ae.error_fn_.shape == (num_k, ae.n_grid_points)
        assert len(ae.ks_) == num_k
        assert len(ae.grid_) == ae.n_grid_points
Ejemplo n.º 6
0
def get_conn_matrix(time_series, conn_model):
    import warnings
    warnings.simplefilter("ignore")
    from nilearn.connectome import ConnectivityMeasure
    from sklearn.covariance import GraphLassoCV
    try:
        from brainiak.fcma.util import compute_correlation
    except ImportError:
        pass

    if conn_model == 'corr':
        # credit: nilearn
        print('\nComputing correlation matrix...\n')
        conn_measure = ConnectivityMeasure(kind='correlation')
        conn_matrix = conn_measure.fit_transform([time_series])[0]
    elif conn_model == 'corr_fast':
        # credit: brainiak
        try:
            print('\nComputing accelerated fcma correlation matrix...\n')
            conn_matrix = compute_correlation(time_series, time_series)
        except RuntimeError:
            print(
                'Cannot run accelerated correlation computation due to a missing dependency. You need brainiak installed!'
            )
    elif conn_model == 'partcorr':
        # 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 == 'tangent':
        # credit: nilearn
        print('\nComputing tangent matrix...\n')
        conn_measure = ConnectivityMeasure(kind='tangent')
        conn_matrix = conn_measure.fit_transform([time_series])[0]
    elif conn_model == 'cov' or conn_model == 'sps':
        ##Fit estimator to matrix to get sparse matrix
        estimator = GraphLassoCV()
        try:
            print('\nComputing covariance...\n')
            estimator.fit(time_series)
        except:
            try:
                print(
                    'Unstable Lasso estimation--Attempting to re-run by first applying shrinkage...'
                )
                from sklearn.covariance import GraphLasso, 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 = GraphLasso(alpha)
                            estimator_shrunk.fit(shrunk_cov)
                            print(
                                "Calculated graph-lasso covariance matrix for alpha=%s"
                                % alpha)
                            break
                        except FloatingPointError:
                            print("Failed at alpha=%s" % alpha)
                    if estimator_shrunk == None:
                        pass
                    else:
                        break
            except:
                raise ValueError(
                    'Unstable Lasso estimation! Shrinkage failed.')

        if conn_model == 'sps':
            try:
                print(
                    '\nFetching precision matrix from covariance estimator...\n'
                )
                conn_matrix = -estimator.precision_
            except:
                print(
                    '\nFetching shrunk precision matrix from covariance estimator...\n'
                )
                conn_matrix = -estimator_shrunk.precision_
        elif conn_model == 'cov':
            try:
                print(
                    '\nFetching covariance matrix from covariance estimator...\n'
                )
                conn_matrix = estimator.covariance_
            except:
                conn_matrix = estimator_shrunk.covariance_
    elif conn_model == 'QuicGraphLasso':
        from inverse_covariance import QuicGraphLasso
        # Compute the sparse inverse covariance via QuicGraphLasso
        # credit: skggm
        model = QuicGraphLasso(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':
        from inverse_covariance import QuicGraphLassoCV
        # Compute the sparse inverse covariance via QuicGraphLassoCV
        # credit: skggm
        model = QuicGraphLassoCV(init_method='cov', verbose=1)
        print(
            '\nCalculating QuicGraphLassoCV precision matrix using skggm...\n')
        model.fit(time_series)
        conn_matrix = -model.precision_

    elif conn_model == 'QuicGraphLassoEBIC':
        from inverse_covariance import QuicGraphLassoEBIC
        # Compute the sparse inverse covariance via QuicGraphLassoEBIC
        # credit: skggm
        model = QuicGraphLassoEBIC(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':
        from inverse_covariance import AdaptiveGraphLasso, QuicGraphLassoEBIC
        # Compute the sparse inverse covariance via
        # AdaptiveGraphLasso + QuicGraphLassoEBIC + method='binary'
        # credit: skggm
        model = AdaptiveGraphLasso(
            estimator=QuicGraphLassoEBIC(init_method='cov', ),
            method='binary',
        )
        print(
            '\nCalculating AdaptiveQuicGraphLasso precision matrix using skggm...\n'
        )
        model.fit(time_series)
        conn_matrix = -model.estimator_.precision_

    return (conn_matrix)
Ejemplo n.º 7
0
class TestAdaptiveGraphLasso(object):
    @pytest.mark.parametrize("params_in", [
        ({
            'estimator':
            QuicGraphLassoCV(
                cv=2,
                n_refinements=6,
                init_method='cov',
                score_metric='log_likelihood',
            ),
            'method':
            'binary',
        }),
        ({
            'estimator':
            QuicGraphLassoCV(
                cv=2,
                n_refinements=6,
                init_method='cov',
                score_metric='log_likelihood',
            ),
            'method':
            'inverse',
        }),
        ({
            'estimator':
            QuicGraphLassoCV(
                cv=2,
                n_refinements=6,
                init_method='cov',
                score_metric='log_likelihood',
            ),
            'method':
            'inverse_squared',
        }),
        ({
            'estimator': QuicGraphLassoEBIC(),
            'method': 'binary',
        }),
        ({
            'estimator': QuicGraphLassoEBIC(),
            'method': 'inverse',
        }),
        ({
            'estimator': QuicGraphLassoEBIC(),
            'method': 'inverse_squared',
        }),
    ])
    def test_integration_adaptive_graph_lasso(self, params_in):
        '''
        Just tests inputs/outputs (not validity of result).
        '''
        X = datasets.load_diabetes().data
        n_examples, n_features = X.shape

        model = AdaptiveGraphLasso(**params_in)
        model.fit(X)
        assert model.estimator_ is not None
        assert model.lam_ is not None

        assert np.sum(model.lam_[np.diag_indices(n_features)]) == 0

        if params_in['method'] == 'binary':
            uvals = set(model.lam_.flat)
            assert len(uvals) == 2
            assert 0 in uvals
            assert 1 in uvals
        elif params_in['method'] == 'inverse' or\
                params_in['method'] == 'inverse_squared':
            uvals = set(model.lam_.flat[model.lam_.flat != 0])
            assert len(uvals) > 0
Ejemplo n.º 8
0
def get_conn_matrix(time_series, conn_model):
    from nilearn.connectome import ConnectivityMeasure
    from sklearn.covariance import GraphLassoCV

    conn_matrix = None
    if conn_model == 'corr':
        # 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':
        # 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 == 'sps':
        # Fit estimator to matrix to get sparse matrix
        estimator_shrunk = None
        estimator = GraphLassoCV()
        try:
            print('\nComputing covariance...\n')
            estimator.fit(time_series)
        except RuntimeWarning:
            print('Unstable Lasso estimation--Attempting to re-run by first applying shrinkage...')
            try:
                from sklearn.covariance import GraphLasso, 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 = GraphLasso(alpha)
                            estimator_shrunk.fit(shrunk_cov)
                            print("Retrying covariance matrix estimate with alpha=%s" % alpha)
                            if estimator_shrunk is None:
                                pass
                            else:
                                break
                        except RuntimeWarning:
                            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('ERROR: Covariance estimation failed.')
        if conn_model == 'sps':
            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':
            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 == 'QuicGraphLasso':
        from inverse_covariance import QuicGraphLasso
        # Compute the sparse inverse covariance via QuicGraphLasso
        # credit: skggm
        model = QuicGraphLasso(
            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':
        from inverse_covariance import QuicGraphLassoCV
        # Compute the sparse inverse covariance via QuicGraphLassoCV
        # credit: skggm
        model = QuicGraphLassoCV(
            init_method='cov',
            verbose=1)
        print('\nCalculating QuicGraphLassoCV precision matrix using skggm...\n')
        model.fit(time_series)
        conn_matrix = -model.precision_
    elif conn_model == 'QuicGraphLassoEBIC':
        from inverse_covariance import QuicGraphLassoEBIC
        # Compute the sparse inverse covariance via QuicGraphLassoEBIC
        # credit: skggm
        model = QuicGraphLassoEBIC(
            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':
        from inverse_covariance import AdaptiveGraphLasso, QuicGraphLassoEBIC
        # Compute the sparse inverse covariance via
        # AdaptiveGraphLasso + QuicGraphLassoEBIC + method='binary'
        # credit: skggm
        model = AdaptiveGraphLasso(
                estimator=QuicGraphLassoEBIC(
                    init_method='cov',
                ),
                method='binary',
            )
        print('\nCalculating AdaptiveQuicGraphLasso precision matrix using skggm...\n')
        model.fit(time_series)
        conn_matrix = -model.estimator_.precision_

    return conn_matrix
Ejemplo n.º 9
0
class TestAdaptiveGraphLasso(object):
    @pytest.mark.parametrize("params_in", [
        ({
            'estimator':
            QuicGraphLassoCV(
                cv=2,
                n_refinements=6,
                init_method='cov',
                score_metric='log_likelihood',
            ),
            'method':
            'binary',
        }),
        ({
            'estimator':
            QuicGraphLassoCV(
                cv=2,
                n_refinements=6,
                init_method='cov',
                score_metric='log_likelihood',
            ),
            'method':
            'inverse',
        }),
        ({
            'estimator':
            QuicGraphLassoCV(
                cv=2,
                n_refinements=6,
                init_method='cov',
                score_metric='log_likelihood',
            ),
            'method':
            'inverse_squared',
        }),
        ({
            'estimator': QuicGraphLassoEBIC(),
            'method': 'binary',
        }),
        ({
            'estimator': QuicGraphLassoEBIC(),
            'method': 'inverse',
        }),
        ({
            'estimator': QuicGraphLassoEBIC(),
            'method': 'inverse_squared',
        }),
    ])
    def test_integration_adaptive_graph_lasso(self, params_in):
        '''
        Just tests inputs/outputs (not validity of result).
        '''
        n_features = 20
        n_samples = 25
        cov, prec, adj = ClusterGraph(
            n_blocks=1,
            chain_blocks=False,
            seed=1,
        ).create(n_features, 0.8)
        prng = np.random.RandomState(2)
        X = prng.multivariate_normal(np.zeros(n_features), cov, size=n_samples)

        model = AdaptiveGraphLasso(**params_in)
        model.fit(X)
        assert model.estimator_ is not None
        assert model.lam_ is not None

        assert np.sum(model.lam_[np.diag_indices(n_features)]) == 0

        if params_in['method'] == 'binary':
            uvals = set(model.lam_.flat)
            assert len(uvals) == 2
            assert 0 in uvals
            assert 1 in uvals
        elif params_in['method'] == 'inverse' or\
                params_in['method'] == 'inverse_squared':
            uvals = set(model.lam_.flat[model.lam_.flat != 0])
            assert len(uvals) > 0
Ejemplo n.º 10
0
plot(g) #, layout = "fr3d")

#%% save array to txt
np.savetxt("firstLASSO.csv", np.array(np.average(lassoTime1, axis=0)), delimiter = ",")
np.savetxt("correlationMatrixFirstRun.csv", np.array(np.average(ket1_corr, axis=0)), delimiter = ",")

#%% 
# run analysis on all subjects
# build timeseries for first scan (before)
totalSub_series_1 = timeSeries(func_files=fileList(totalSub,'1'), confound_files=confList(totalSub, '1'))
totalSub_series_2 = timeSeries(func_files=fileList(totalSub,'2'), confound_files=confList(totalSub, '2'))
# In[skggm]:
# run EBIC LASSO on each subject
from inverse_covariance import QuicGraphLassoEBIC
# call estimator EBIC
estimator = QuicGraphLassoEBIC(init_method="cov", verbose=1)
# create emtpy array
ebicMat = []
ebicMat2 = []
# run loop over timeseries and create precision matrix for each subject, append to a list of matrices
for i in totalSub_series_1:
    estimator.fit(i)
    ebicMat.append(-estimator.precision_)
    

for i in totalSub_series_2:
    estimator.fit(i)
    ebicMat2.append(-estimator.precision_)

ket1_forLASSO.shape
#%%
if estimator_type == 'QuicGraphLasso':
    # Compute the sparse inverse covariance via QuicGraphLasso
    estimator = QuicGraphLasso(init_method='cov',
                               lam=0.5,
                               mode='default',
                               verbose=1)

elif estimator_type == 'QuicGraphLassoCV':
    # Compute the sparse inverse covariance via QuicGraphLassoCV
    estimator = QuicGraphLassoCV(init_method='cov', verbose=1)
    estimator.fit(timeseries)

elif estimator_type == 'QuicGraphLassoEBIC':
    # Compute the sparse inverse covariance via QuicGraphLassoEBIC
    estimator = QuicGraphLassoEBIC(init_method='cov', verbose=1)
    estimator.fit(timeseries)

elif estimator_type == 'AdaptiveQuicGraphLasso':
    # Compute the sparse inverse covariance via
    # AdaptiveGraphLasso + QuicGraphLassoEBIC + method='binary'
    model = AdaptiveGraphLasso(
        estimator=QuicGraphLassoEBIC(init_method='cov', ),
        method='binary',
    )
    model.fit(timeseries)
    estimator = model.estimator_

# Display the sparse inverse covariance
plt.figure(figsize=(7.5, 7.5))
plt.imshow(np.triu(-estimator.precision_, 1),
Ejemplo n.º 12
0
import sys
sys.path.append('..')

from inverse_covariance import (QuicGraphLassoCV, QuicGraphLassoEBIC,
                                ModelAverage)
from inverse_covariance.profiling import AverageError
from matplotlib import pyplot as plt

n_features = 50
n_trials = 100
verbose = True

# average plots for QuicGraphLassoEBIC
ae = AverageError(
    model_selection_estimator=QuicGraphLassoEBIC(gamma=0.0),
    n_features=n_features,
    n_trials=n_trials,
    verbose=verbose,
)
ae.fit()
ae.show()
plt.suptitle('QuicGraphLassoEBIC (BIC)')

# average plots for QuicGraphLassoCV
ae = AverageError(
    model_selection_estimator=QuicGraphLassoCV(),
    n_features=n_features,
    n_trials=n_trials,
    verbose=verbose,
)
ae.fit()