-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheval_network.py
385 lines (305 loc) · 14.7 KB
/
eval_network.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/usr/bin/env python
"""
Dentate Gyrus model script for evaluation of synaptic parameters
"""
import os, sys, logging, datetime, gc, pprint
from functools import partial
import click
import numpy as np
from collections import defaultdict, namedtuple
from mpi4py import MPI
import h5py
from neuroh5.io import scatter_read_cell_attribute_selection, read_cell_attribute_info, append_cell_attributes
import dentate
from dentate import network, network_clamp, synapses, spikedata, stimulus, utils, io_utils, optimization
from dentate.env import Env
from dentate.utils import read_from_yaml, write_to_yaml, list_find, viewitems, get_module_logger, config_logging
from dentate.synapses import (SynParam, syn_param_from_dict)
from dentate.optimization import (optimization_params,
update_network_params, network_features)
from dentate.stimulus import rate_maps_from_features
ParamSpec = namedtuple("ParamSpec", ['param_names', 'param_tuples', ])
logger = get_module_logger(__name__)
def mpi_excepthook(type, value, traceback):
"""
:param type:
:param value:
:param traceback:
:return:
"""
sys_excepthook(type, value, traceback)
sys.stdout.flush()
sys.stderr.flush()
if MPI.COMM_WORLD.size > 1:
MPI.COMM_WORLD.Abort(1)
sys_excepthook = sys.excepthook
sys.excepthook = mpi_excepthook
def h5_get_group (h, groupname):
if groupname in h.keys():
g = h[groupname]
else:
g = h.create_group(groupname)
return g
def h5_get_dataset (g, dsetname, **kwargs):
if dsetname in g.keys():
dset = g[dsetname]
else:
dset = g.create_dataset(dsetname, (0,), **kwargs)
return dset
def parse_flat_syn_params_with_index(index_params_dict):
"""Parses synaptic parameters of the form:
- index:
- postsyn population
- presyn population list
- section type
- synaptic mechanism
- synaptic mechanism parameter path
- phenotype id [optional]
- parameter value
"""
params_tuple_dict = {}
for this_index, this_index_params in viewitems(index_params_dict):
this_param_tuples = []
for this_param in this_index_params:
(
this_population,
source,
sec_type,
syn_name,
param_path,
phenotype,
param_val,
) = this_param
syn_param = SynParam(
this_population,
source,
sec_type,
syn_name,
param_path,
None,
None if phenotype == 'null' else phenotype,
)
this_param_tuples.append(
(syn_param, param_val)
)
params_tuple_dict[this_index] = this_param_tuples
return params_tuple_dict
@click.command(context_settings=dict(
ignore_unknown_options=True,
allow_extra_args=True,
))
@click.option("--config-path", required=True, type=click.Path(exists=True, file_okay=True, dir_okay=False), \
help='path to evaluation configuration file')
@click.option("--params-id", required=False, type=int, help='index of parameter set')
@click.option("--n-samples", required=False, type=int,
help='number of samples per parameter')
@click.option("--target-features-path", required=False, type=click.Path(),
help='path to neuroh5 file containing target rate maps used for rate optimization')
@click.option("--target-features-namespace", type=str, required=False, default='Input Spikes',
help='namespace containing target rate maps used for rate optimization')
@click.option("--output-file-dir", type=click.Path(exists=True, file_okay=False, dir_okay=True), default='results')
@click.option("--output-file-name", type=click.Path(exists=False, file_okay=True, dir_okay=False))
@click.option("--verbose", '-v', is_flag=True)
def main(config_path, params_id, n_samples, target_features_path, target_features_namespace, output_file_dir, output_file_name, verbose):
config_logging(verbose)
logger = utils.get_script_logger(os.path.basename(__file__))
if params_id is None:
if n_samples is not None:
logger.info("Generating parameter lattice ...")
generate_param_lattice(config_path, n_samples, output_file_dir, output_file_name, verbose)
return
network_args = click.get_current_context().args
network_config = {}
for arg in network_args:
kv = arg.split("=")
if len(kv) > 1:
k,v = kv
network_config[k.replace('--', '').replace('-', '_')] = v
else:
k = kv[0]
network_config[k.replace('--', '').replace('-', '_')] = True
run_ts = datetime.datetime.today().strftime('%Y%m%d_%H%M')
if output_file_name is None:
output_file_name=f"network_features_{run_ts}.h5"
output_path = f'{output_file_dir}/{output_file_name}'
eval_config = read_from_yaml(config_path, include_loader=utils.IncludeLoader)
eval_config['run_ts'] = run_ts
if target_features_path is not None:
eval_config['target_features_path'] = target_features_path
if target_features_namespace is not None:
eval_config['target_features_namespace'] = target_features_namespace
network_param_spec_src = eval_config.get('param_spec', None)
network_param_values_src = eval_config.get('param_values', {})
network_param_values = parse_flat_syn_params_with_index(network_param_values_src)
feature_names = eval_config['feature_names']
target_populations = eval_config['target_populations']
network_config.update(eval_config.get('kwargs', {}))
if params_id is None:
network_config['results_file_id'] = f"DG_eval_network_{eval_config['run_ts']}"
else:
network_config['results_file_id'] = f"DG_eval_network_{params_id}_{eval_config['run_ts']}"
env = init_network(comm=MPI.COMM_WORLD, kwargs=network_config)
gc.collect()
t_start = 50.
t_stop = env.tstop
time_range = (t_start, t_stop)
target_trj_rate_map_dict = {}
target_features_arena = env.arena_id
target_features_trajectory = env.trajectory_id
for pop_name in target_populations:
if (f'{pop_name} snr') not in feature_names:
continue
my_cell_index_set = set(env.biophys_cells[pop_name].keys())
trj_rate_maps = {}
trj_rate_maps = rate_maps_from_features(env, pop_name,
cell_index_set=list(my_cell_index_set),
input_features_path=target_features_path,
input_features_namespace=target_features_namespace,
time_range=time_range)
target_trj_rate_map_dict[pop_name] = trj_rate_maps
network_param_spec = None
if network_param_spec_src is not None:
network_param_spec = make_param_spec(target_populations, network_param_spec_src)
eval_network(env, network_config,
network_param_values, params_id,
target_trj_rate_map_dict, t_start, t_stop,
target_populations, output_path)
def generate_param_lattice(config_path, n_samples, output_file_dir, output_file_name, maxiter=5, verbose=False):
from dmosopt import sampling
logger = utils.get_script_logger(os.path.basename(__file__))
output_path = None
if output_file_name is not None:
output_path = f'{output_file_dir}/{output_file_name}'
eval_config = read_from_yaml(config_path, include_loader=utils.IncludeLoader)
network_param_spec_src = eval_config['param_spec']
target_populations = eval_config['target_populations']
network_param_spec = make_param_spec(target_populations, network_param_spec_src)
param_tuples = network_param_spec.param_tuples
param_names = network_param_spec.param_names
n_params = len(param_tuples)
n_init = n_params * n_samples
Xinit = sampling.glp(n_init, n_params, maxiter=maxiter)
ub = []
lb = []
for param_name, param_tuple in zip(param_names, param_tuples):
param_range = param_tuple.param_range
ub.append(param_range[1])
lb.append(param_range[0])
ub = np.asarray(ub)
lb = np.asarray(lb)
for i in range(n_init):
Xinit[i,:] = Xinit[i,:] * (ub - lb) + lb
output_dict = {}
for i in range(Xinit.shape[0]):
output_dict[i] = list([float(x) for x in Xinit[i, :]])
if output_path is not None:
write_to_yaml(output_path, output_dict)
else:
pprint.pprint(output_dict)
def make_param_spec(pop_names, param_dict):
"""Constructs a flat list representation of synaptic parameters."""
param_names = []
param_initial_dict = {}
param_tuples = []
for pop_name in pop_names:
param_specs = param_dict[pop_name]
keyfun = lambda kv: str(kv[0])
for source, source_dict in sorted(viewitems(param_specs), key=keyfun):
for sec_type, sec_type_dict in sorted(viewitems(source_dict), key=keyfun):
for syn_name, syn_mech_dict in sorted(viewitems(sec_type_dict), key=keyfun):
for param_fst, param_rst in sorted(viewitems(syn_mech_dict), key=keyfun):
if isinstance(param_rst, dict):
for const_name, const_range in sorted(viewitems(param_rst)):
param_path = (param_fst, const_name)
param_tuples.append(SynParam(pop_name, source, sec_type, syn_name, param_path, const_range))
param_key = '%s.%s.%s.%s.%s.%s' % (pop_name, str(source), sec_type, syn_name, param_fst, const_name)
param_names.append(param_key)
else:
param_name = param_fst
param_range = param_rst
param_tuples.append(SynParam(pop_name, source, sec_type, syn_name, param_name, param_range))
param_key = '%s.%s.%s.%s.%s' % (pop_name, source, sec_type, syn_name, param_name)
param_names.append(param_key)
return ParamSpec(param_names=param_names, param_tuples=param_tuples)
def init_network(comm, kwargs):
np.seterr(all='raise')
env = Env(comm=comm, **kwargs)
network.init(env)
env.comm.barrier()
return env
def eval_network(env, network_config, network_param_values, params_id, target_trj_rate_map_dict, t_start, t_stop, target_populations, output_path):
param_tuple_values = None
if params_id is not None:
param_tuple_values = network_param_values[params_id]
if env.comm.rank == 0:
logger.info("*** Updating network parameters ...")
logger.info(pprint.pformat(param_tuple_values))
update_network_params(env, param_tuple_values)
env.checkpoint_clear_data = False
env.checkpoint_interval = None
env.tstop = t_stop
network.run(env, output=network_config.get('output_results', False), shutdown=False)
for pop_name in target_trj_rate_map_dict:
append_cell_attributes(env.results_file_path, pop_name, target_trj_rate_map_dict[pop_name],
namespace='Target Trajectory Rate Map', comm=env.comm, io_size=env.io_size)
local_features = network_features(env, target_trj_rate_map_dict, t_start, t_stop, target_populations)
return collect_network_features(env, local_features, target_populations, output_path, params_id, param_tuple_values)
def collect_network_features(env, local_features, target_populations, output_path, params_id, param_tuple_values):
all_features = env.comm.gather(local_features, root=0)
collected_features = {}
if env.comm.rank == 0:
for pop_name in target_populations:
pop_features_dicts = [ features_dict[pop_name] for features_dict in all_features ]
sum_mean_rate = 0.
sum_snr = 0.
n_total = 0
n_active = 0
n_target_rate_map = 0
for pop_feature_dict in pop_features_dicts:
n_active_local = pop_feature_dict['n_active']
n_total_local = pop_feature_dict['n_total']
n_target_rate_map_local = pop_feature_dict['n_target_rate_map']
sum_mean_rate_local = pop_feature_dict['sum_mean_rate']
sum_snr_local = pop_feature_dict['sum_snr']
n_total += n_total_local
n_active += n_active_local
n_target_rate_map += n_target_rate_map_local
sum_mean_rate += sum_mean_rate_local
if sum_snr_local is not None:
sum_snr += sum_snr_local
if n_active > 0:
mean_rate = sum_mean_rate / n_active
else:
mean_rate = 0.
if n_total > 0:
fraction_active = n_active / n_total
else:
fraction_active = 0.
mean_snr = None
if n_target_rate_map > 0:
mean_snr = sum_snr / n_target_rate_map
logger.info(f'population {pop_name}: n_active = {n_active} n_total = {n_total} mean rate = {mean_rate}')
logger.info(f'population {pop_name}: n_target_rate_map = {n_target_rate_map} snr: sum = {sum_snr} mean = {mean_snr}')
collected_features[f'{pop_name} fraction active'] = fraction_active
collected_features[f'{pop_name} firing rate'] = mean_rate
if mean_snr is not None:
collected_features[f'{pop_name} snr'] = mean_snr
output_file = h5py.File(output_path, "a")
network_grp = h5_get_group(output_file, 'DG_eval_network')
param_grp = h5_get_group(network_grp, f'{params_id}')
if param_tuple_values is not None:
dset = h5_get_dataset(param_grp, 'param_values', maxshape=(len(param_tuple_values),), dtype=np.float32)
param_vals = np.asarray([v[1] for v in param_tuple_values], dtype=np.float32)
dset.resize((len(param_vals),))
dset[:] = param_vals
feature_grp = h5_get_group(param_grp, 'feature_values')
for feature_name in sorted(collected_features):
feature_val = collected_features[feature_name]
dset = h5_get_dataset(feature_grp, feature_name, maxshape=(1,), dtype=np.float32)
dset.resize((1,))
dset[0] = feature_val
output_file.close()
env.comm.barrier()
return collected_features
if __name__ == '__main__':
main(args=sys.argv[(list_find(lambda x: os.path.basename(x) == os.path.basename(__file__), sys.argv) + 1):])