-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulations_linear.py
132 lines (117 loc) · 4.34 KB
/
simulations_linear.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import warnings
import numpy as np
from msapy import msa
import utils as ut
import scipy
import pandas as pd
from netneurotools import datasets
import networkx as nx
from numba import prange
import bct
from itertools import product
warnings.filterwarnings("ignore")
SEED = 2023
rng = np.random.default_rng(seed=SEED)
networks = [
"HumanSC"
] # 'HumanSC','macaqueSC','mouseSC', 'netneuro_mouse', 'lattice', 'sneppen', 'null', 'weight_shuffle'
for network in networks:
if network == "HumanSC":
consensus_mat = scipy.io.loadmat(
"Consensus_Connectomes.mat",
simplify_cells=True,
squeeze_me=True,
chars_as_strings=True,
)
connectivity = consensus_mat["LauConsensus"]["Matrices"][2][0]
elif network == "netneuro_mouse":
connectivity = datasets.fetch_connectome("mouse")["conn"]
elif network == "lattice":
consensus_mat = scipy.io.loadmat(
"Consensus_Connectomes.mat",
simplify_cells=True,
squeeze_me=True,
chars_as_strings=True,
)
connectivity = consensus_mat["LauConsensus"]["Matrices"][2][0]
G = nx.from_numpy_array(connectivity)
connectivity = nx.to_numpy_array(nx.lattice_reference(G, seed=SEED))
elif network == "sneppen":
consensus_mat = scipy.io.loadmat(
"Consensus_Connectomes.mat",
simplify_cells=True,
squeeze_me=True,
chars_as_strings=True,
)
connectivity = consensus_mat["LauConsensus"]["Matrices"][2][0]
G = nx.from_numpy_array(connectivity)
connectivity = nx.to_numpy_array(nx.random_reference(G, seed=SEED))
elif network == "null":
consensus_mat = scipy.io.loadmat(
"Consensus_Connectomes.mat",
simplify_cells=True,
squeeze_me=True,
chars_as_strings=True,
)
connectivity = consensus_mat["LauConsensus"]["Matrices"][2][0]
connectivity = bct.null_model_und_sign(connectivity, seed=SEED)[0]
elif network == "weight_shuffle":
consensus_mat = scipy.io.loadmat(
"Consensus_Connectomes.mat",
simplify_cells=True,
squeeze_me=True,
chars_as_strings=True,
)
connectivity = consensus_mat["LauConsensus"]["Matrices"][2][0]
binary_connectivity = np.where(connectivity != 0, 1, 0)
shuffled_connectomes = np.zeros((len(connectivity), len(connectivity), 10))
for trial in range(10):
weights = connectivity.flatten()[connectivity.flatten() != 0].copy()
rng.shuffle(weights)
ix = 0
temp = binary_connectivity.copy().astype(float)
for i, j in product(range(len(connectivity)), range(len(connectivity))):
if binary_connectivity[i, j] == 1:
shuffled_connectomes[i, j, trial] = weights[ix]
ix += 1
else:
connectivity = np.loadtxt(f"{network}")
connectivity = ut.spectral_normalization(1, connectivity)
N_NODES = len(connectivity)
NOISE_STRENGTH = 0.05 # 2.0 for nonlinear
DELTA = 0.01
TAU = 0.02
G = None # 0.74 for Human, 0.64 for macaque, 0.79 for mouse, 0.1 for the "misfitted" network
DURATION = 1
N_TRIALS = 10
N_CORES = -1
input_tensor = rng.normal(
0, NOISE_STRENGTH, (N_NODES, int(DURATION / DELTA) + 1, N_TRIALS)
)
lesion_params = {
"adjacency_matrix": connectivity,
"model_kwargs": {
"timeconstant": TAU,
"dt": DELTA,
"coupling": G,
"duration": DURATION,
},
}
for trial in prange(N_TRIALS):
if network == "weight_shuffle":
lesion_params["adjacency_matrix"] = ut.spectral_normalization(
1, shuffled_connectomes[:, :, trial]
)
lesion_params["input"] = input_tensor[:, :, trial]
ci_mat = msa.estimate_causal_influences(
elements=list(range(N_NODES)),
objective_function=ut.lesion_simple_nodes,
objective_function_params=lesion_params,
n_permutations=1_000,
n_cores=N_CORES,
parallelize_over_games=False,
permutation_seed=trial,
)
ci_mat.to_pickle(
f"results/{network}_causal_modes_l_w_{len(connectivity)}_{trial}.pickle"
)