forked from fabridamicelli/ser
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimulation_htc.py
executable file
·180 lines (152 loc) · 6.21 KB
/
simulation_htc.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env python3
import sys
import os
from shutil import copyfile
import time
import configparser
import getpass
import timeit
# import networkx as nx
import numpy as np
# import numpy.linalg as LA
# from scipy.io import loadmat
# find the model.py package:
sys.path.append(".")
sys.path.append("../")
sys.path.append("../../")
from model import SERModel
from func import determine_unique_postfix
def simulation(
n_steps: int,
n_therm: int,
ri: float,
rf: float,
prop_active: float,
T: float,
conn_matrix: np.ndarray,
seed: int
) -> np.ndarray:
# set random seed:
np.random.seed(seed)
# set up model parameters
model = SERModel(n_steps=n_steps,
n_transient=n_therm,
prob_spont_act=ri,
prob_recovery=rf,
prop_e=prop_active,
threshold=T
)
# run simulation using given connectome
activation_matrix = model.simulate(adj_mat=conn_matrix)
activation_matrix[activation_matrix == -1] = 2 # return refractory nodes to 2
return activation_matrix
if __name__ == '__main__':
# Print initial message:
initial_time = time.asctime()
hostname = os.uname()[1].split(".")[0]
print("Python script started on: {}".format(initial_time))
print("{:>24}: {}".format('from', hostname))
print("Name of python script: {}".format(os.path.abspath(__file__)))
print("Script run by: {}\n".format(getpass.getuser()))
# Get the file with parameters and read them:
config_file = sys.argv[1]
print("Configuration file: {}\n".format(config_file))
parser = configparser.ConfigParser()
parser.read(config_file)
parameters = parser["Parameters"]
t_max = parameters.getint("t_max", 2000)
t_th = parameters.getint("t_th", 200)
ri = parameters.getfloat("ri", 0.001)
rf = parameters.getfloat("rf", 0.2)
#T = parameters.getfloat("T", 0.05)
T_init = parameters.getfloat("T_init", 0.05)
T_final = parameters.getfloat("T_final", 0.05)
T_n = parameters.getint("T_n", 1)
seed = parameters.getint("seed", 124)
frac_init_active_neurons = parameters.getfloat("frac_init_active_neurons", 0.01)
connectome_file = parameters.get("connectome_file", 'sample.dat')
run_name = parameters.get("run_name", 'test_run')
flags = parser['Flags']
r_rocha = flags.getboolean("r_rocha", False)
connectome_normalization = flags.getboolean("connectome_normalization", False)
skip_calculated = flags.getboolean("skip_calculated", True)
# create unique directory
postfix = determine_unique_postfix(run_name)
if postfix != '':
if not skip_calculated:
run_name += postfix
print("Run name changed: {}".format(run_name))
else:
print("Found a run, skipping: {}".format(run_name))
# create run directory
os.makedirs(run_name, exist_ok=False)
connectome_name = os.path.split(connectome_file)[-1]
# write the config file
with open(os.path.join(run_name, 'sim_config.ini'), 'w') as config:
parser.write(config)
# load connectome:
try:
connectome = np.loadtxt(connectome_file)
except:
sys.exit("Connectome file {} not found".format(connectome_file))
# apply flags:
if connectome_normalization:
for row in connectome:
s = np.sum(row)
if s>0:
row /= s
# define size of the network
Nsize = len(connectome)
if r_rocha:
ri = 2.0 / Nsize
rf = ri ** 0.2
# print the parameters:
print("Parameters read from the file:")
print("Number of time steps: {}".format(t_max))
print("Number of discarded time steps (thermalization): {}".format(t_th))
print("Number of neurons in the model: {}".format(Nsize))
print("Fraction of initially active neurons: {}".format(frac_init_active_neurons))
print("Spontaneous activation probability: {}".format(ri))
print("Relaxation probability: {}".format(rf))
# print("Value of the threshold (temperature): {}".format(T))
print(f"Thresholds (temperatures): np.linspace({T_init},{T_final},{T_n})")
print("Connectome file: {}".format(connectome_name))
print("Normalization of the connectome: {}".format(connectome_normalization))
print("Rocha's ri and rf: {}".format(r_rocha))
print(f"Skip finished calculation: {skip_calculated}")
# only now enter run's directory
os.chdir(run_name)
# main simulation goes here:
start_time = timeit.default_timer()
am_tab = None
Ts = np.linspace(T_init,T_final,T_n)
for T in Ts:
print(f"T = {T}")
activation_matrix = simulation(n_steps=t_max,
n_therm=t_th,
ri=ri,
rf=rf,
prop_active=frac_init_active_neurons,
T=T,
conn_matrix=connectome,
seed=seed)
activation_matrix = np.expand_dims(activation_matrix,axis=0)
am_tab = activation_matrix if am_tab is None else np.concatenate((am_tab,activation_matrix),axis=0)
# truncate the extension of the connectome filename
# connectome_name_wo_ext = os.path.splitext(connectome_name)[0]
# output_filename = 'activation_matrix_{}'.format(connectome_name_wo_ext)
# save the activation matrix
#np.savetxt(output_filename, activation_matrix, delimiter=",")
#np.save(output_filename, activation_matrix)
output_filename = 'output.npz'
np.savez_compressed(output_filename, activation_matrix = am_tab, Ts = Ts)
# save the connectome
# copyfile(connectome_file, os.path.join(run_name, connectome_name))
connectome_name = 'connection_matrix.dat'
np.savetxt(connectome_name,connectome)
end_time = time.asctime()
final_time = timeit.default_timer()
print()
print()
print("Python script ended on: {}".format(end_time))
print("Total time: {:.2f} seconds".format(final_time - start_time))