-
Notifications
You must be signed in to change notification settings - Fork 8
/
mahp_plot.go
319 lines (254 loc) · 8.7 KB
/
mahp_plot.go
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
// Copyright (c) 2020, The Emergent Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// mahp_plot plots an equation updating over time in a etable.Table and Plot2D.
package main
import (
"strconv"
"github.com/emer/axon/chans"
"github.com/emer/etable/eplot"
"github.com/emer/etable/etable"
"github.com/emer/etable/etensor"
_ "github.com/emer/etable/etview" // include to get gui views
"github.com/goki/gi/gi"
"github.com/goki/gi/gimain"
"github.com/goki/gi/giv"
"github.com/goki/ki/ki"
"github.com/goki/mat32"
)
func main() {
TheSim.Config()
gimain.Main(func() { // this starts gui -- requires valid OpenGL display connection (e.g., X11)
guirun()
})
}
func guirun() {
TheSim.VmRun()
win := TheSim.ConfigGui()
win.StartEventLoop()
}
// LogPrec is precision for saving float values in logs
const LogPrec = 4
// Sim holds the params, table, etc
type Sim struct {
// [view: inline] mAHP function
Mahp chans.MahpParams `view:"inline" desc:"mAHP function"`
// [def: -100] starting voltage
Vstart float32 `def:"-100" desc:"starting voltage"`
// [def: 100] ending voltage
Vend float32 `def:"100" desc:"ending voltage"`
// [def: 1] voltage increment
Vstep float32 `def:"1" desc:"voltage increment"`
// number of time steps
TimeSteps int `desc:"number of time steps"`
// do spiking instead of voltage ramp
TimeSpike bool `desc:"do spiking instead of voltage ramp"`
// spiking frequency
SpikeFreq float32 `desc:"spiking frequency"`
// time-run starting membrane potential
TimeVstart float32 `desc:"time-run starting membrane potential"`
// time-run ending membrane potential
TimeVend float32 `desc:"time-run ending membrane potential"`
// [view: no-inline] table for plot
Table *etable.Table `view:"no-inline" desc:"table for plot"`
// [view: -] the plot
Plot *eplot.Plot2D `view:"-" desc:"the plot"`
// [view: no-inline] table for plot
TimeTable *etable.Table `view:"no-inline" desc:"table for plot"`
// [view: -] the plot
TimePlot *eplot.Plot2D `view:"-" desc:"the plot"`
// [view: -] main GUI window
Win *gi.Window `view:"-" desc:"main GUI window"`
// [view: -] the master toolbar
ToolBar *gi.ToolBar `view:"-" desc:"the master toolbar"`
}
// TheSim is the overall state for this simulation
var TheSim Sim
// Config configures all the elements using the standard functions
func (ss *Sim) Config() {
ss.Mahp.Defaults()
ss.Mahp.Gbar = 1
ss.Vstart = -100
ss.Vend = 100
ss.Vstep = 1
ss.TimeSteps = 300
ss.TimeSpike = true
ss.SpikeFreq = 50
ss.TimeVstart = -70
ss.TimeVend = -50
ss.Update()
ss.Table = &etable.Table{}
ss.ConfigTable(ss.Table)
ss.TimeTable = &etable.Table{}
ss.ConfigTimeTable(ss.TimeTable)
}
// Update updates computed values
func (ss *Sim) Update() {
}
// VmRun plots the equation as a function of V
func (ss *Sim) VmRun() {
ss.Update()
dt := ss.Table
mp := &ss.Mahp
nv := int((ss.Vend - ss.Vstart) / ss.Vstep)
dt.SetNumRows(nv)
for vi := 0; vi < nv; vi++ {
vbio := ss.Vstart + float32(vi)*ss.Vstep
var ninf, tau float32
mp.NinfTauFmV(vbio, &ninf, &tau)
dt.SetCellFloat("V", vi, float64(vbio))
dt.SetCellFloat("Ninf", vi, float64(ninf))
dt.SetCellFloat("Tau", vi, float64(tau))
}
ss.Plot.Update()
}
func (ss *Sim) ConfigTable(dt *etable.Table) {
dt.SetMetaData("name", "mAHPplotTable")
dt.SetMetaData("read-only", "true")
dt.SetMetaData("precision", strconv.Itoa(LogPrec))
sch := etable.Schema{
{"V", etensor.FLOAT64, nil, nil},
{"Ninf", etensor.FLOAT64, nil, nil},
{"Tau", etensor.FLOAT64, nil, nil},
}
dt.SetFromSchema(sch, 0)
}
func (ss *Sim) ConfigPlot(plt *eplot.Plot2D, dt *etable.Table) *eplot.Plot2D {
plt.Params.Title = "mAHP V Function Plot"
plt.Params.XAxisCol = "V"
plt.SetTable(dt)
// order of params: on, fixMin, min, fixMax, max
plt.SetColParams("V", eplot.Off, eplot.FloatMin, 0, eplot.FloatMax, 0)
plt.SetColParams("Ninf", eplot.On, eplot.FixMin, 0, eplot.FixMax, 1)
plt.SetColParams("Tau", eplot.On, eplot.FixMin, 0, eplot.FloatMax, 1)
return plt
}
/////////////////////////////////////////////////////////////////
// TimeRun runs the equation over time.
func (ss *Sim) TimeRun() {
ss.Update()
dt := ss.TimeTable
mp := &ss.Mahp
var n, tau float32
mp.NinfTauFmV(ss.TimeVstart, &n, &tau)
kna := float32(0)
msdt := float32(0.001)
v := ss.TimeVstart
vinc := float32(2) * (ss.TimeVend - ss.TimeVstart) / float32(ss.TimeSteps)
isi := int(1000 / ss.SpikeFreq)
dt.SetNumRows(ss.TimeSteps)
for ti := 1; ti <= ss.TimeSteps; ti++ {
vnorm := chans.VFmBio(v)
t := float32(ti) * msdt
var ninf, tau float32
mp.NinfTauFmV(v, &ninf, &tau)
dn := mp.DNFmV(vnorm, n)
g := mp.GmAHP(n)
dt.SetCellFloat("Time", ti, float64(t))
dt.SetCellFloat("V", ti, float64(v))
dt.SetCellFloat("GmAHP", ti, float64(g))
dt.SetCellFloat("N", ti, float64(n))
dt.SetCellFloat("dN", ti, float64(dn))
dt.SetCellFloat("Ninf", ti, float64(ninf))
dt.SetCellFloat("Tau", ti, float64(tau))
dt.SetCellFloat("Kna", ti, float64(kna))
if ss.TimeSpike {
si := ti % isi
if si == 0 {
v = ss.TimeVend
kna += 0.05 * (1 - kna)
} else {
v = ss.TimeVstart + (float32(si)/float32(isi))*(ss.TimeVend-ss.TimeVstart)
kna -= kna / 50
}
} else {
v += vinc
if v > ss.TimeVend {
v = ss.TimeVend
}
}
n += dn
}
ss.TimePlot.Update()
}
func (ss *Sim) ConfigTimeTable(dt *etable.Table) {
dt.SetMetaData("name", "mAHPplotTable")
dt.SetMetaData("read-only", "true")
dt.SetMetaData("precision", strconv.Itoa(LogPrec))
sch := etable.Schema{
{"Time", etensor.FLOAT64, nil, nil},
{"V", etensor.FLOAT64, nil, nil},
{"GmAHP", etensor.FLOAT64, nil, nil},
{"N", etensor.FLOAT64, nil, nil},
{"dN", etensor.FLOAT64, nil, nil},
{"Ninf", etensor.FLOAT64, nil, nil},
{"Tau", etensor.FLOAT64, nil, nil},
{"Kna", etensor.FLOAT64, nil, nil},
}
dt.SetFromSchema(sch, 0)
}
func (ss *Sim) ConfigTimePlot(plt *eplot.Plot2D, dt *etable.Table) *eplot.Plot2D {
plt.Params.Title = "Time Function Plot"
plt.Params.XAxisCol = "Time"
plt.SetTable(dt)
// order of params: on, fixMin, min, fixMax, max
plt.SetColParams("Time", eplot.Off, eplot.FloatMin, 0, eplot.FloatMax, 0)
plt.SetColParams("V", eplot.Off, eplot.FloatMin, 0, eplot.FloatMax, 0)
plt.SetColParams("GmAHP", eplot.On, eplot.FixMin, 0, eplot.FloatMax, 0)
plt.SetColParams("N", eplot.On, eplot.FixMin, 0, eplot.FloatMax, 0)
plt.SetColParams("dN", eplot.Off, eplot.FloatMin, 0, eplot.FloatMax, 0)
plt.SetColParams("Ninf", eplot.Off, eplot.FixMin, 0, eplot.FloatMax, 0)
plt.SetColParams("Tau", eplot.Off, eplot.FixMin, 0, eplot.FloatMax, 0)
plt.SetColParams("Kna", eplot.Off, eplot.FixMin, 0, eplot.FloatMax, 1)
return plt
}
// ConfigGui configures the GoGi gui interface for this simulation,
func (ss *Sim) ConfigGui() *gi.Window {
width := 1600
height := 1200
// gi.WinEventTrace = true
gi.SetAppName("mahp_plot")
gi.SetAppAbout(`This plots an equation. See <a href="https://github.com/emer/emergent">emergent on GitHub</a>.</p>`)
win := gi.NewMainWindow("mahp_plot", "Plotting Equations", width, height)
ss.Win = win
vp := win.WinViewport2D()
updt := vp.UpdateStart()
mfr := win.SetMainFrame()
tbar := gi.AddNewToolBar(mfr, "tbar")
tbar.SetStretchMaxWidth()
ss.ToolBar = tbar
split := gi.AddNewSplitView(mfr, "split")
split.Dim = mat32.X
split.SetStretchMax()
sv := giv.AddNewStructView(split, "sv")
sv.SetStruct(ss)
tv := gi.AddNewTabView(split, "tv")
plt := tv.AddNewTab(eplot.KiT_Plot2D, "V-G Plot").(*eplot.Plot2D)
ss.Plot = ss.ConfigPlot(plt, ss.Table)
plt = tv.AddNewTab(eplot.KiT_Plot2D, "TimePlot").(*eplot.Plot2D)
ss.TimePlot = ss.ConfigTimePlot(plt, ss.TimeTable)
split.SetSplits(.3, .7)
tbar.AddAction(gi.ActOpts{Label: "V-G Run", Icon: "update", Tooltip: "Run the equations and plot results."}, win.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
ss.VmRun()
vp.SetNeedsFullRender()
})
tbar.AddAction(gi.ActOpts{Label: "Time Run", Icon: "update", Tooltip: "Run the equations and plot results."}, win.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
ss.TimeRun()
vp.SetNeedsFullRender()
})
tbar.AddAction(gi.ActOpts{Label: "README", Icon: "file-markdown", Tooltip: "Opens your browser on the README file that contains instructions for how to run this model."}, win.This(),
func(recv, send ki.Ki, sig int64, data interface{}) {
gi.OpenURL("https://github.com/emer/axon/blob/master/chans/mahp_plot/README.md")
})
vp.UpdateEndNoSig(updt)
// main menu
appnm := gi.AppName()
mmen := win.MainMenu
mmen.ConfigMenus([]string{appnm, "File", "Edit", "Window"})
amen := win.MainMenu.ChildByName(appnm, 0).(*gi.Action)
amen.Menu.AddAppMenu(win)
emen := win.MainMenu.ChildByName("Edit", 1).(*gi.Action)
emen.Menu.AddCopyCutPaste(win)
win.MainMenuUpdated()
return win
}