-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexporter.py
116 lines (101 loc) · 4.66 KB
/
exporter.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
from brian2 import *
from brian2.groups.neurongroup import NeuronGroup
from brian2.core.network import *
from lems.model.component import Parameter
from lems.model.dynamics import *
from lems.model.model import *
import re
def export_to_lems(network, _output):
if (type(network) is not Network):
net = Network(collect(level=1))
else:
net = network
for o in net.objects:
comp = ComponentType(o.name)
if (type(o) is NeuronGroup):
for i in o.namespace:
s = str(o.namespace[i])
l = s.split(".")
if(len(l))>1:
l=dimension(l[1].strip())
else:
l='none'
param = Parameter(i, (l))
comp.add_parameter(param)
dyn = Dynamics()
if(o._refractory):
int_regime = Regime('integrating', dyn, True)
dyn.add_regime(int_regime)
ref_regime = Regime('refractory', dyn)
dyn.add_regime(ref_regime)
ref_oc = OnCondition('t .gt. ' + purge(str(o._refractory)))
ref_trans = Transition("integrating")
ref_oc.add_action(ref_trans)
ref_regime.add_event_handler(ref_oc)
if('spike' in o.event_codes):
ref_oe = OnEntry()
s = str(o.event_codes['spike'])
l = s.split("=")
ref_sa = StateAssignment(l[0],l[1])
ref_oe.add_action(ref_sa)
ref_regime.add_event_handler(ref_oe)
if('spike' in o.events):
thresh_condition = o.events['spike']
oc = OnCondition(purge(replace_expr(thresh_condition)))
evout = EventOut("spike")
oc.add_action(evout)
if('spike' in o.event_codes):
s = str(o.event_codes['spike'])
l = s.split("=")
sa = StateAssignment(l[0],l[1])
oc.add_action(sa)
dim = l[1].split("*")
sv = StateVariable(l[0].strip(),dimension(dim[1]))
dyn.add_state_variable(sv)
for eq in o.equations._equations:
if(o.equations._equations[eq].type=='differential equation'):
print o.equations._equations[eq].expr
td = TimeDerivative(str(o.equations._equations[eq].varname), replace_expr(str(o.equations._equations[eq].expr)))
for svs in dyn.state_variables:
if(svs.name!=str(o.equations._equations[eq].varname)):
sv = StateVariable(str(o.equations._equations[eq].varname), dimension(str(o.equations._equations[eq].unit)))
dyn.add_state_variable(sv)
if(o._refractory):
int_regime.add_time_derivative(td)
int_trans = Transition("refractory")
oc.add_action(int_trans)
int_regime.add_event_handler(oc)
else:
dyn.add_time_derivative(td)
dyn.add_event_handler(oc)
if(o.equations._equations[eq].type=='subexpression' or o.equations._equations[eq].type=='parameter'):
dv = DerivedVariable(str(o.equations._equations[eq].varname))
dv.value = str(o.equations._equations[eq].expr)
dv.dimension = dimension(str(o.equations._equations[eq].unit))
dyn.add_derived_variable(dv)
comp.dynamics = dyn
model = Model()
model.add_component_type(comp)
model.export_to_file(_output + '_lems.xml')
def dimension(unit):
if('V' in unit) : return 'voltage'
if('s' in unit) : return 'time'
if('S' in unit) : return 'conductance'
if('F' in unit) : return 'capacitance'
if('per_' in unit) : return 'per_time'
if('A' in unit) : return 'current'
def replace_expr(expression):
expression = expression.replace(">", ".gt.")
expression = expression.replace("<", ".lt.")
expression = expression.replace("<=", ".leq.")
expression = expression.replace(">=", ".geq.")
expression = expression.replace("==", ".eq.")
expression = expression.replace("!=", ".neq.")
expression = expression.replace("and", ".and.")
expression = expression.replace("or", ".or.")
expression = expression.replace("**", "^")
return expression
def purge(st):
st = re.sub(r'(\d+?\.)(?:\s*)?\*(?:\s*)(\w+)', '\\1\\2' , st)
st = re.sub(r'(\d+?)(?:\s*)?\*(?:\s*)(\w+)', '\\1\\2' , st)
return st