Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: extract ROI time series #9

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions Connectivity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# This is script to calculate connectivity using raw np arrays,
# each assumed to be in the shape (channels/ROI's, time points)

import numpy as np
import time
import os
import dyconnmap
import bct
from scipy import stats
import pathlib
start = time.time()

def operations_to_perform(): # Change as desired
Cal_wpli = True
Cal_graph_metrics = True
return Cal_wpli, Cal_graph_metrics


def setup_dependencies(): # Change as desired
input_dir = r'' # The directory containing files of interest
output_dir = r'' # The output directory
array_type = 'participant_conversation_alpha' # This is the type of array that will be loaded for further calculations, eg. "participant_conversation_alpha"
target_fb = [8,12] # The target frequency band TODO: set this up so variable can be a this can be list of bands and each generates it's own adjacency matrix
fs = 250 # The sampling frequency
saving = True
os.makedirs(output_dir, exist_ok=True)
return input_dir, output_dir, array_type, fs, target_fb, saving


def prep_data(input_dir, array_type, target_fb, fs):
data = []
folder = pathlib.Path(input_dir).glob('*')
for file in folder:
if (os.path.basename(file)).endswith('.npy'):
if array_type in os.path.basename(file):
array = np.load(file)
_, _, filtered_array = dyconnmap.analytic_signal(array, fb=target_fb, fs=fs)
data.append(filtered_array)
return np.array(data)


def wpli_conn(array, target_fb, fs): # (participants, roi's, time_points)
adj_matrix = []
for participant in array:
adj_matrix.append(dyconnmap.fc.wpli(participant, fs=fs, fb=target_fb))
return np.array(adj_matrix)


def graph_metrics(adj_matrix): # TODO: Add in stats, maybe nbs? Could use fdc as well?
# Strength calculator
Strength = []
for participant in adj_matrix:
Strength.append(bct.strengths_und(np.nan_to_num(participant)))
Strength = np.array(Strength)

# Zeroing negative phasing
Strength[Strength < 0] = 0

# Betweenness centrality calculator
Betweenness = []
for participant in adj_matrix:
Betweenness.append(bct.betweenness_wei(np.nan_to_num(participant)))
Betweenness = np.array(Betweenness)

# Eigenvector centrality calculator
Eigenvector = []
for participant in adj_matrix:
Eigenvector.append(bct.eigenvector_centrality_und(np.nan_to_num(participant)))
Eigenvector = np.array(Eigenvector)

# Clustering calculator
Clustering = []
for participant in adj_matrix:
Clustering.append(bct.clustering_coef_wu(np.nan_to_num(participant)))
Clustering = np.array(Clustering)

return Strength, Betweenness, Eigenvector, Clustering


def main(): # TODO: put in the elif statements
# Prep
Cal_wpli, Cal_graph_metrics = operations_to_perform()
input_dir, output_dir, array_type, fs, target_fb, saving = setup_dependencies()
data = prep_data(input_dir, array_type, target_fb, fs)

# Core functions
if Cal_wpli:
adj_matrix = wpli_conn(data, target_fb, fs)
if Cal_graph_metrics:
Strength, Betweenness, Eigenvector, Clustering = graph_metrics(adj_matrix)

# Saving
if saving:
np.save(output_dir + '_All_Strength_' + array_type, Strength)
np.save(output_dir + '_All_Betweenness_' + array_type, Betweenness)
np.save(output_dir + '_All_Eigenvector_' + array_type, Eigenvector)
np.save(output_dir + '_All_Clustering_' + array_type, Clustering)
np.save(output_dir + '_Master_adj_matrix_' + array_type, adj_matrix)


if __name__ == "__main__":
main()

print('\n' + "EXECUTION TIME: " + str(time.time()-start))
Loading