Omega_0 = Omega_sol.copy() Theta_0 = Theta_sol.copy() AIC[g1, g2] = aic(S, Theta_sol, n.mean()) BIC[g1, g2] = ebic(S, Theta_sol, n.mean(), gamma=0.1) ix = np.unravel_index(np.nanargmin(BIC), BIC.shape) ix2 = np.unravel_index(np.nanargmin(AIC), AIC.shape) lambda1 = L1[ix] lambda2 = L2[ix] print("Optimal lambda values: (l1,l2) = ", (lambda1, lambda2)) #%% singleGL = GraphicalLasso(alpha=1.5 * lambda1, tol=1e-2, max_iter=4000, verbose=True) res = np.zeros((K, p, p)) for k in np.arange(K): #model = quic.fit(S[k,:,:], verbose = 1) model = singleGL.fit(samples[k, :, :]) res[k, :, :] = model.precision_ results['GLASSO'] = {'Theta': res} #%% start = time() sol, info = ADMM_MGL(S, lambda1, lambda2, reg, Omega_0, rho = 1, max_iter = 100, \ eps_admm = 1e-5, verbose = True, measure = True)
def block_glasso(data, eps=1e-8, COLLECT=True): criterion = nn.MSELoss() # input, target theta_true, X = data # ############################################################################# # Estimate the covariance print('Using the lars method') S = np.dot(X.T, X) / args.M # model = GraphicalLassoCV(cv=2, alphas=5, n_refinements=5, tol=1e-6, # max_iter=100, mode='lars', n_jobs=-1) model = GraphicalLasso(alpha=args.rho, mode='lars', tol=1e-7, enet_tol=1e-6, max_iter=args.MAX_EPOCH, verbose=True, assume_centered=True) # model = GraphLasso(alpha=args.rho, mode='lars', tol=1e-8, enet_tol=1e-6, # max_iter=100, verbose=False, assume_centered=False) # print('Using the cd method') # model = GraphicalLassoCV(cv=2, alphas=5, n_refinements=5, tol=1e-6, # max_iter=100, mode='cd', n_jobs=-1) model.fit(X) cov_ = model.covariance_ theta_pred = model.precision_ # ############################################################################# fdr, tpr, fpr, shd, nnz, nnz_true, ps = metrics.report_metrics( theta_true, theta_pred) cond_theta_pred, cond_theta_true = np.linalg.cond( theta_pred), np.linalg.cond(theta_true) num_itr = model.n_iter_ rho_obtained = args.rho # the L1 penalty parameter print('Accuracy metrics: fdr ', fdr, ' tpr ', tpr, ' fpr ', fpr, ' shd ', shd, ' nnz ', nnz, ' nnz_true ', nnz_true, ' sign_match ', ps, ' pred_cond ', cond_theta_pred, ' true_cond ', cond_theta_true, 'total itr: ', num_itr, ' penalty_rho: ', rho_obtained) # Getting the NMSE and objective value # results of convergence res_conv = [] theta_true = convert_to_torch(theta_true, TESTING_FLAG=True) theta_pred = convert_to_torch(theta_pred, TESTING_FLAG=True) S = convert_to_torch(S, TESTING_FLAG=True) obj_true = get_obj_val(theta_true, S) if COLLECT: theta_pred_diag = torch.diag_embed( torch.diagonal(theta_pred, offset=0, dim1=-2, dim2=-1)) theta_true_diag = torch.diag_embed( torch.diagonal(theta_true, offset=0, dim1=-2, dim2=-1)) cv_loss, cv_loss_off_diag, obj_pred = get_convergence_loss( theta_pred, theta_true), get_convergence_loss( theta_pred - theta_pred_diag, theta_true - theta_true_diag), get_obj_val(theta_pred, S) res_conv.append([cv_loss, obj_pred, obj_true, cv_loss_off_diag]) return [ fdr, tpr, fpr, shd, nnz, nnz_true, ps, cond_theta_pred, cond_theta_true, num_itr, rho_obtained ], res_conv # result of convergence
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 BaseException: 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 BaseException: 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) # assert coords.shape[0] == labels.shape[0] == conn_matrix.shape[0] 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, )
import pandas as pd import time seed = int(snakemake.wildcards["replicate"]) np.random.seed(seed) data = snakemake.input["data"] filename = snakemake.output["adjmat"] df = pd.read_csv(data) X = df.values start = time.perf_counter() cov = GraphicalLasso(alpha=float(snakemake.wildcards["alpha"]), mode=snakemake.wildcards["mode"], tol=float(snakemake.wildcards["tol"]), enet_tol=float(snakemake.wildcards["enet_tol"]), max_iter=int(snakemake.wildcards["max_iter"]), verbose=bool(snakemake.wildcards["verbose"]), assume_centered=bool( snakemake.wildcards["assume_centered"])).fit(X) #adjmat = np.around(np.abs(cov.precision_), decimals=3) adjmat = ((np.around(np.abs(cov.precision_), decimals=3) > float( snakemake.wildcards["precmat_threshold"])) * 1 - np.identity(X.shape[1])).astype(int) tottime = time.perf_counter() - start time_filename = snakemake.output["time"] np.savetxt(time_filename, [tottime]) dfadj = pd.DataFrame(adjmat) dfadj.columns = df.columns
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, c_boot, norm, binary, hpass): """ 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. c_boot : int Number of bootstraps if user specified circular-block bootstrapped resampling of the node-extracted time-series. 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. 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. c_boot : int Number of bootstraps if user specified circular-block bootstrapped resampling of the node-extracted time-series. 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. """ 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 == '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, c_boot, norm, binary, hpass
# Solving group sparse problems with SGL # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # # We now solve K independent SGL problems and find the best :math:`\lambda_1` parameter. # # ALPHA = np.logspace(start = 0, stop = -1.5, num = 15, base = 10) FPR_GL = np.zeros(len(ALPHA)) TPR_GL = np.zeros(len(ALPHA)) DFPR_GL = np.zeros(len(ALPHA)) DTPR_GL = np.zeros(len(ALPHA)) for a in np.arange(len(ALPHA)): singleGL = GraphicalLasso(alpha = ALPHA[a], tol = 1e-4, max_iter = 50, verbose = False) singleGL_sol = np.zeros((K,p,p)) for k in np.arange(K): model = singleGL.fit(sample[k,:,:].T) singleGL_sol[k,:,:] = model.precision_ dr = discovery_rate(singleGL_sol, Theta) TPR_GL[a] = dr['TPR'] FPR_GL[a] = dr['FPR'] DTPR_GL[a] = dr['TPR_DIFF'] DFPR_GL[a] = dr['FPR_DIFF'] # %% # Solving # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# %% # Solving time-varying problems with SGL # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # # We now solve K independent SGL problems and find the best :math:`\lambda_1` parameter. # # ALPHA = 2 * np.logspace(start=-3, stop=-1, num=10, base=10) SGL_BIC = np.zeros(len(ALPHA)) all_res = list() for j in range(len(ALPHA)): res = np.zeros((K, p, p)) singleGL = GraphicalLasso(alpha=ALPHA[j], tol=1e-3, max_iter=20, verbose=False) for k in np.arange(K): model = singleGL.fit(sample[k, :, :].T) res[k, :, :] = model.precision_ all_res.append(res) SGL_BIC[j] = ebic(S, res, N, gamma=0.1) ix_SGL = np.argmin(SGL_BIC) results['SGL'] = {'Theta': all_res[ix_SGL]} # %% # Solve with ADMM # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.covariance import GraphicalLasso filename = "expr_ceph_utah_1000.txt" data = pd.read_csv(filename, delimiter='\t', index_col=0) n_rows = data.shape[0] n_cols = data.shape[1] cov_matrix = np.cov(data.T) np.savetxt("cov_matrix.csv", cov_matrix, delimiter=',') model = GraphicalLasso(alpha=0.55) model.fit(data) prec_matrix = model.get_precision() # print(prec_matrix) np.savetxt("precision_matrix.csv", prec_matrix, delimiter=',') n = n_cols adj_matrix = np.zeros((n, n)) n_edges = 0 for i in range(n): for j in range(i, n): if prec_matrix[i, j] != 0: adj_matrix[i, j] = 1 adj_matrix[j, i] = 1 n_edges += 1 np.savetxt("glasso_adj_matrix.csv", adj_matrix, delimiter=',') degree_list = np.sum(adj_matrix, axis=0) - 1
from sklearn.covariance import ( EmpiricalCovariance, GraphicalLasso, GraphicalLassoCV, LedoitWolf, MinCovDet, OAS, ShrunkCovariance, ) from sklearn.utils import shuffle from tqdm.auto import trange COV_ESTIMATORS = { "empirical": EmpiricalCovariance(), "graphical_lasso": GraphicalLasso(), "graphical_lasso_cv": GraphicalLassoCV(), "ledoit_wolf": LedoitWolf(), "min_cov_det": MinCovDet(), "oas": OAS(), "shrunk": ShrunkCovariance(), } def _quacks_like_estimator(instance): """Return True if the instance quacks like an sklearn estimator.""" required_attrs = [ hasattr(instance, "fit"), hasattr(instance, "predict"), hasattr(instance, "get_params"), ]