Module connectome.preprocessing.graph_metrics
functions to compute graph metrics for a given brain connectivity dataset based on the bct(py) package
Functions
def explain_graph_metrics() ‑> None-
function to explain the different implemented grap metrics
def get_gms_from_pd(data: pandas.core.frame.DataFrame, regions: list = None, cols: list = None, **kwargs) ‑> pandas.core.frame.DataFrame-
computes the graph metrics for connectivity data supplied in the form of a DataFrame
Examples:
>>> import numpy as np >>> import pandas as pd >>> from connectome.preprocessing.graph_metrics import get_gms_from_pd >>> >>> # checking the get_gms_from_pd function >>> k = 8 # dim of the conn matrix >>> obs = 10 # observations >>> conn = pd.DataFrame( >>> np.random.normal( >>> loc=0.1, >>> scale=1.2, >>> size=int((k*(k-1)/2)*obs)).reshape(obs, int((k*(k-1)/2))), >>> columns=[str(i) + "_" + str(j) >>> for i in range(k) >>> for j in range(i+1, k)]) >>> conn_data_list = [flat_to_mat(conn.iloc[i, :]) for i in range(obs)] >>> print(get_gms_from_pd(conn, list(conn.columns)))Args
data- dataFrame containing the conn data
regions- list of names of the regions of the conn matrix
cols- list of columns of the DataFrame data which contain conn data
**kwargs- anything that´s passed to "get_grap_metrics"
Returns
DataFrame containing the computed graph metrics
def get_graph_metrics(conns: list, threshold: float = 0.65, col_names: list = None, use_abs: bool = False, gms: tuple = ('Degrees', 'Modularity', 'Community Structure', 'Clustering Coefficient', 'Characteristic Path Length', 'Node Betweenness', 'Density', 'Component Vectors', 'Transitivity')) ‑> tuple-
- computes graph metrics for the given connectivity data
-
retransforms the connectivity matrices to pearson correlation before computing the adjacency matrices
Examples:
>>> import numpy as np >>> import pandas as pd >>> from connectome.preprocessing.graph_metrics import get_graph_metrics >>> >>> # checking the get_gms_from_pd function >>> k = 8 # dim of the conn matrix >>> obs = 10 # observations >>> conn = pd.DataFrame( >>> np.random.normal( >>> loc=0.1, >>> scale=1.2, >>> size=int((k*(k-1)/2)*obs)).reshape(obs, int((k*(k-1)/2))), >>> columns=[str(i) + "_" + str(j) >>> for i in range(k) >>> for j in range(i+1, k)]) >>> conn_data_list = [flat_to_mat(conn.iloc[i, :]) for i in range(obs)] >>> print(get_graph_metrics(conn_data_list))Args
conns- list of numpy arrays containing the connectivity data
threshold- threshold of correlation to compute adjacency matrices
col_names- colnames of the connectivity matrices in case those are reordered IMPORTANT: refers to the regions - e.g. "1", "4" instead of "1_4", "1_5"
use_abs- whether absolute connectivity values should be used
gms- graph metrics to be computed - can be any of: ['Degrees', 'Modularity', 'Community Structure', 'Clustering Coefficient', 'Characteristic Path Length', 'Node Betweenness', 'Density', 'Component Vectors', 'Transitivity']
Returns
pd.DataFrame containing the computed graph metrics
def is_conn_col(x: str) ‑> bool-
checks whether the given column name corresponds to the format of the connectivity data columns, i.e. "1_245", which refers to the connectivity between region 1 and region 245
Examples:
>>> test_names = ["1_2", "asf", "as_asd"] >>> print([is_conn_col(x) for x in test_names])Args
x- the provided column name
Returns
True if the column corresponds to the connectivity data format
def pd_to_arrays(data: pandas.core.frame.DataFrame, cols: list = None) ‑> list-
extracts the connectivity data from a DataFrame and converts those into matrices / numpy arrays
Args
data- the DataFrame from which the connectivity data should be extracted
cols- the columns of the DataFrame which contain the data of interest
Returns
list of the connectivity matrices / numpy arrays