-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathviewupdt.go
260 lines (238 loc) · 6.46 KB
/
viewupdt.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
// Copyright (c) 2019, 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.
package netview
import (
"strings"
"github.com/emer/emergent/v2/etime"
)
// ViewUpdate manages time scales for updating the NetView
type ViewUpdate struct {
// View is the network view.
View *NetView `display:"-"`
// whether in testing mode -- can be set in advance to drive appropriate updating
Testing bool `display:"-"`
// text to display at the bottom of the view
Text string `display:"-"`
// toggles update of display on
On bool
// SkipInvis means do not record network data when the NetView is invisible.
// This speeds up running when not visible, but the NetView display will
// not show the current state when switching back to it.
SkipInvis bool
// at what time scale to update the display during training?
Train etime.Times
// at what time scale to update the display during testing?
Test etime.Times
}
// Config configures for given NetView and default train, test times
func (vu *ViewUpdate) Config(nv *NetView, train, test etime.Times) {
vu.View = nv
vu.On = true
vu.Train = train
vu.Test = test
vu.SkipInvis = true // more often running than debugging probably
}
// GetUpdateTime returns the relevant update time based on testing flag
func (vu *ViewUpdate) GetUpdateTime(testing bool) etime.Times {
if testing {
return vu.Test
}
return vu.Train
}
// GoUpdate does an update if view is On, visible and active,
// including recording new data and driving update of display.
// This version is only for calling from a separate goroutine,
// not the main event loop (see also Update).
func (vu *ViewUpdate) GoUpdate() {
if !vu.On || vu.View == nil {
return
}
if !vu.View.IsVisible() && vu.SkipInvis {
vu.View.RecordCounters(vu.Text)
return
}
vu.View.Record(vu.Text, -1) // -1 = use a dummy counter
// note: essential to use Go version of update when called from another goroutine
if vu.View.IsVisible() {
vu.View.GoUpdateView()
}
}
// Update does an update if view is On, visible and active,
// including recording new data and driving update of display.
// This version is only for calling from the main event loop
// (see also GoUpdate).
func (vu *ViewUpdate) Update() {
if !vu.On || vu.View == nil {
return
}
if !vu.View.IsVisible() && vu.SkipInvis {
vu.View.RecordCounters(vu.Text)
return
}
vu.View.Record(vu.Text, -1) // -1 = use a dummy counter
// note: essential to use Go version of update when called from another goroutine
if vu.View.IsVisible() {
vu.View.UpdateView()
}
}
// UpdateWhenStopped does an update when the network updating was stopped
// either via stepping or hitting the stop button.
// This has different logic for the raster view vs. regular.
// This is only for calling from a separate goroutine,
// not the main event loop.
func (vu *ViewUpdate) UpdateWhenStopped() {
if !vu.On || vu.View == nil {
return
}
if !vu.View.IsVisible() && vu.SkipInvis {
vu.View.RecordCounters(vu.Text)
return
}
if !vu.View.Options.Raster.On { // always record when not in raster mode
vu.View.Record(vu.Text, -1) // -1 = use a dummy counter
}
// todo: updating is not available here -- needed?
// if vu.View.Scene.Is(core.ScUpdating) {
// return
// }
vu.View.GoUpdateView()
}
// UpdateTime triggers an update at given timescale.
func (vu *ViewUpdate) UpdateTime(time etime.Times) {
if !vu.On || vu.View == nil {
return
}
viewUpdate := vu.GetUpdateTime(vu.Testing)
if viewUpdate == time {
vu.GoUpdate()
} else {
if viewUpdate < etime.Trial && time == etime.Trial {
if vu.View.Options.Raster.On { // no extra rec here
vu.View.Data.RecordLastCtrs(vu.Text)
if vu.View.IsVisible() {
vu.View.GoUpdateView()
}
} else {
vu.GoUpdate()
}
}
}
}
// IsCycleUpdating returns true if the view is updating at a cycle level,
// either from raster or literal cycle level.
func (vu *ViewUpdate) IsCycleUpdating() bool {
if !vu.On || vu.View == nil || !(vu.View.IsVisible() || !vu.SkipInvis) {
return false
}
viewUpdate := vu.GetUpdateTime(vu.Testing)
if viewUpdate > etime.ThetaCycle {
return false
}
if viewUpdate == etime.Cycle {
return true
}
if vu.View.Options.Raster.On {
return true
}
return false
}
// IsViewingSynapse returns true if netview is actively viewing synapses.
func (vu *ViewUpdate) IsViewingSynapse() bool {
if !vu.On || vu.View == nil || !(vu.View.IsVisible() || !vu.SkipInvis) {
return false
}
vvar := vu.View.Var
if strings.HasPrefix(vvar, "r.") || strings.HasPrefix(vvar, "s.") {
return true
}
return false
}
// UpdateCycle triggers an update at the Cycle (Millisecond) timescale,
// using given text to display at bottom of view
func (vu *ViewUpdate) UpdateCycle(cyc int) {
if !vu.On || vu.View == nil {
return
}
viewUpdate := vu.GetUpdateTime(vu.Testing)
if viewUpdate > etime.ThetaCycle {
return
}
if vu.View.Options.Raster.On {
vu.UpdateCycleRaster(cyc)
return
}
switch viewUpdate {
case etime.Cycle:
vu.GoUpdate()
case etime.FastSpike:
if cyc%10 == 0 {
vu.GoUpdate()
}
case etime.GammaCycle:
if cyc%25 == 0 {
vu.GoUpdate()
}
case etime.BetaCycle:
if cyc%50 == 0 {
vu.GoUpdate()
}
case etime.AlphaCycle:
if cyc%100 == 0 {
vu.GoUpdate()
}
case etime.ThetaCycle:
if cyc%200 == 0 {
vu.GoUpdate()
}
}
}
// UpdateCycleRaster raster version of Cycle update
func (vu *ViewUpdate) UpdateCycleRaster(cyc int) {
if !vu.View.IsVisible() && vu.SkipInvis {
vu.View.RecordCounters(vu.Text)
return
}
viewUpdate := vu.GetUpdateTime(vu.Testing)
vu.View.Record(vu.Text, cyc)
switch viewUpdate {
case etime.Cycle:
vu.View.GoUpdateView()
case etime.FastSpike:
if cyc%10 == 0 {
vu.View.GoUpdateView()
}
case etime.GammaCycle:
if cyc%25 == 0 {
vu.View.GoUpdateView()
}
case etime.BetaCycle:
if cyc%50 == 0 {
vu.View.GoUpdateView()
}
case etime.AlphaCycle:
if cyc%100 == 0 {
vu.View.GoUpdateView()
}
case etime.ThetaCycle:
if cyc%200 == 0 {
vu.View.GoUpdateView()
}
}
}
// RecordSyns records synaptic data -- stored separate from unit data
// and only needs to be called when synaptic values are updated.
// Should be done when the DWt values have been computed, before
// updating Wts and zeroing.
// NetView displays this recorded data when Update is next called.
func (vu *ViewUpdate) RecordSyns() {
if !vu.On || vu.View == nil {
return
}
if !vu.View.IsVisible() {
if vu.SkipInvis || !vu.IsViewingSynapse() {
return
}
}
vu.View.RecordSyns()
}