-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGeneralChangeParameters.py
More file actions
74 lines (58 loc) · 2.4 KB
/
Copy pathGeneralChangeParameters.py
File metadata and controls
74 lines (58 loc) · 2.4 KB
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
from neuron import h, gui
import re
import numpy as np
# Reads names of parameters for the model provided by the mod file and changes parameters to specified
def ChangeParameters(cell, NEW_PARAMS, MOD_FILE):
param_list, suffix, neat_data = ReadParameterNames(MOD_FILE)
global_vars = []
range_vars = []
for line in neat_data['NEURON']:
if re.match('^GLOBAL', line):
line = re.split('^GLOBAL\s+', line)[1]
global_vars.extend(re.split('\s*,\s*', line))
elif re.match('^RANGE', line):
line = re.split('^RANGE\s+', line)[1]
range_vars.extend(re.split('\s*,\s*', line))
global_vars = [s + "_" + suffix for s in global_vars]
range_vars = [s + "_" + suffix for s in range_vars]
for variable in NEW_PARAMS.keys():
try:
param_list[variable] = NEW_PARAMS.get(variable)
if variable in global_vars:
exec("cell.h." + variable + "=" + str(NEW_PARAMS.get(variable)))
elif variable in range_vars:
exec("cell.soma." + variable + "=" + str(NEW_PARAMS.get(variable)))
except:
print("Variable " + variable + " is not a parameter of " + suffix)
return cell
# Reads the names and inital values of parameters for the model
def ReadParameterNames(MOD_FILE):
param_list = {}
data = open(MOD_FILE, 'r').readlines()
for n in np.arange(len(data)):
data[n] = data[n].rstrip()
data[n] = re.sub('\t*', '', data[n])
data[n] = re.sub('^\s*', '', data[n])
if re.match('\r\n', data[n]):
data[n].remove()
neat_data = {}
category = ""
cat_data = []
for line in data:
if re.match("[A-Z]+\s*\{", line):
category = re.search("[A-Z]+", line).group(0)
elif re.match("\s*\}", line):
neat_data[category] = cat_data
cat_data = []
category = ""
else:
if category != "" and line != "":
cat_data.append(line)
suffix = re.sub('SUFFIX[\s]*', '', neat_data['NEURON'][0])
for param in neat_data['PARAMETER']:
try:
param_list[re.search("^[A-Za-z0-9_]+", param).group(0) + "_" + suffix] \
= float(re.search("(=\s*)([-]?[\d]*\.?\d+(?:e[-+]\d*)?)", param).group(2))
except:
continue
return param_list, suffix, neat_data