Skip to content

neurostatx.network.utils

Functions

NameDescription
construct_attributes_dictBuild a node-attribute dictionary from selected DataFrame columns.
extract_subject_percentileLabel subjects whose membership delta exceeds a percentile.
get_nodes_and_edgesBuild an edgelist from a subject-by-cluster membership table.

construct_attributes_dictfunction#

def construct_attributes_dict(df, labels, id_column)

Build a node-attribute dictionary from selected DataFrame columns.

Parameters

NameTypeDescription
dfDataFrameTable of node attributes.
labelslistColumn names to store as node attributes.
id_columnstrColumn used as the node identifier.

Returns

attributes_dictdict
Mapping of node ID to attribute dictionary.

Examples

>>> import pandas as pd
>>> from neurostatx.network.utils import construct_attributes_dict
>>> df = pd.DataFrame({"id": ["s1", "s2"], "age": [20, 30]})
>>> construct_attributes_dict(df, ["age"], "id")["s1"]["age"]
20

extract_subject_percentilefunction#

def extract_subject_percentile(mat, percentile)

Label subjects whose membership delta exceeds a percentile.

Parameters

NameTypeDescription
matarrayFuzzy membership matrix of shape (n_clusters, n_samples).
percentilefloatPercentile of the first-minus-second membership gap.

Returns

label_dictdict
Mapping of cluster keys to arrays of cluster index or 0.

Examples

>>> import numpy as np
>>> from neurostatx.network.utils import extract_subject_percentile
>>> mat = np.array([[0.9, 0.2], [0.1, 0.8]])
>>> labels = extract_subject_percentile(mat, 50)
>>> sorted(labels)
['c1', 'c2']

get_nodes_and_edgesfunction#

def get_nodes_and_edges(df, edge_attr='membership')

Build an edgelist from a subject-by-cluster membership table.

Parameters

NameTypeDefaultDescription
dfDataFramerequiredTable whose first column is subject IDs and remaining columns are cluster memberships.
edge_attrstr'membership'Name of the weight column in the returned edgelist. Defaults to "membership".

Returns

edgesDataFrame
Edgelist with node1, node2, and edge_attr.
subject_listSeries
Subject identifiers from the first column.
center_listlist
Cluster-centroid labels (c1, c2, …).

Examples

>>> import pandas as pd
>>> from neurostatx.network.utils import get_nodes_and_edges
>>> df = pd.DataFrame({"id": ["s1", "s2"], "c1": [0.8, 0.2],
... "c2": [0.2, 0.8]})
>>> edges, subjects, centers = get_nodes_and_edges(df)
>>> list(centers)
['c1', 'c2']