/
saccade.go
411 lines (349 loc) · 14.2 KB
/
saccade.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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// 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.
package main
import (
"fmt"
"log"
"math"
"math/rand"
"github.com/emer/emergent/env"
"github.com/emer/emergent/erand"
"github.com/emer/emergent/evec"
"github.com/emer/etable/etable"
"github.com/emer/etable/etensor"
"github.com/emer/etable/minmax"
"github.com/goki/mat32"
)
// Saccade implements saccading logic for generating visual saccades
// around a 2D World plane, with a moving object that must remain
// in view. Generates the track of the object.
// World size is defined as -1..1 in normalized units.
type Saccade struct {
// range of trajectory lengths (time steps)
TrajLenRange minmax.Int `desc:"range of trajectory lengths (time steps)"`
// range of fixation durations
FixDurRange minmax.Int `desc:"range of fixation durations"`
// if true, randomize the object position at start of every trajectory -- this can be confusing for prediction however
RandObjPos bool `desc:"if true, randomize the object position at start of every trajectory -- this can be confusing for prediction however"`
// maximum saccade size
SacGenMax float32 `desc:"maximum saccade size"`
// maximum object velocity
VelGenMax float32 `desc:"maximum object velocity"`
// probability of zero velocity object motion as a discrete option prior to computing random velocity
ZeroVelP float64 `desc:"probability of zero velocity object motion as a discrete option prior to computing random velocity"`
// edge around World to not look past
Margin float32 `desc:"edge around World to not look past"`
// size of view as proportion of -1..1 world size
ViewPct float32 `desc:"size of view as proportion of -1..1 world size"`
// visualization size of world -- for debug visualization
WorldVisSz evec.Vec2i `desc:"visualization size of world -- for debug visualization"`
// visualization size of view -- for debug visualization
ViewVisSz evec.Vec2i `desc:"visualization size of view -- for debug visualization"`
// add rows to Table for each step (for debugging) -- else re-use 0
AddRows bool `desc:"add rows to Table for each step (for debugging) -- else re-use 0"`
// table showing visualization of state
Table *etable.Table `desc:"table showing visualization of state"`
// tensor state showing world position of obj
WorldTsr *etensor.Float32 `inactive:"+" desc:"tensor state showing world position of obj"`
// tensor state showing eye position
EyePosTsr *etensor.Float32 `inactive:"+" desc:"tensor state showing eye position"`
// tensor state showing saccade plan
SacPlanTsr *etensor.Float32 `inactive:"+" desc:"tensor state showing saccade plan"`
// tensor state showing view position of obj
ViewTsr *etensor.Float32 `inactive:"+" desc:"tensor state showing view position of obj"`
// current trajectory length
TrajLen int `inactive:"+" desc:"current trajectory length"`
// current fixation duration
FixDur int `inactive:"+" desc:"current fixation duration"`
// tick counter within trajectory, counts up from 0..TrajLen-1
Tick env.Ctr `inactive:"+" desc:"tick counter within trajectory, counts up from 0..TrajLen-1"`
// tick counter within current fixation
SacTick env.Ctr `inactive:"+" desc:"tick counter within current fixation"`
// World minus margin
World minmax.F32 `inactive:"+" desc:"World minus margin"`
// View minus margin
View minmax.F32 `inactive:"+" desc:"View minus margin"`
// object position, in world coordinates
ObjPos mat32.Vec2 `inactive:"+" desc:"object position, in world coordinates"`
// object position, in view coordinates
ObjViewPos mat32.Vec2 `inactive:"+" desc:"object position, in view coordinates"`
// object velocity, in world coordinates
ObjVel mat32.Vec2 `inactive:"+" desc:"object velocity, in world coordinates"`
// next object position, in world coordinates
ObjPosNext mat32.Vec2 `inactive:"+" desc:"next object position, in world coordinates"`
// next object velocity, in world coordinates
ObjVelNext mat32.Vec2 `inactive:"+" desc:"next object velocity, in world coordinates"`
// eye position, in world coordinates
EyePos mat32.Vec2 `inactive:"+" desc:"eye position, in world coordinates"`
// eye movement plan, in world coordinates
SacPlan mat32.Vec2 `inactive:"+" desc:"eye movement plan, in world coordinates"`
// current trial eye movement, in world coordinates
Saccade mat32.Vec2 `inactive:"+" desc:"current trial eye movement, in world coordinates"`
// true if new trajectory started on this trial
NewTraj bool `inactive:"+" desc:"true if new trajectory started on this trial"`
// true if new saccade was made on this trial
NewSac bool `inactive:"+" desc:"true if new saccade was made on this trial"`
// true if next trial will be a new trajectory
NewTrajNext bool `inactive:"+" desc:"true if next trial will be a new trajectory"`
}
// Defaults sets generic defaults -- use ParamSet to override
func (sc *Saccade) Defaults() {
sc.TrajLenRange.Set(4, 4)
sc.FixDurRange.Set(2, 2)
sc.RandObjPos = false
sc.SacGenMax = 0.4
sc.VelGenMax = 0.4
sc.ZeroVelP = 0
sc.Margin = 0.1
sc.ViewPct = 0.5
sc.WorldVisSz.Set(24, 24)
sc.ViewVisSz.Set(16, 16)
}
// Init must be called at start prior to generating saccades
func (sc *Saccade) Init() {
sc.World.Max = 1 - sc.Margin
sc.World.Min = -1 + sc.Margin
sc.View.Max = sc.ViewPct - sc.Margin
sc.View.Min = -sc.ViewPct + sc.Margin
if sc.Table == nil {
sc.Table = &etable.Table{}
sc.ConfigTable(sc.Table)
yx := []string{"Y", "X"}
sc.WorldTsr = etensor.NewFloat32([]int{sc.WorldVisSz.Y, sc.WorldVisSz.X}, nil, yx)
sc.EyePosTsr = etensor.NewFloat32([]int{sc.WorldVisSz.Y, sc.WorldVisSz.X}, nil, yx)
sc.SacPlanTsr = etensor.NewFloat32([]int{sc.WorldVisSz.Y, sc.WorldVisSz.X}, nil, yx)
sc.ViewTsr = etensor.NewFloat32([]int{sc.ViewVisSz.Y, sc.ViewVisSz.X}, nil, yx)
}
sc.Table.SetNumRows(1)
sc.Tick.Cur = -1 // will increment to 0
sc.NextTraj() // start with a trajectory ready
sc.Tick.Scale = env.Tick
sc.Tick.Max = sc.TrajLen
sc.SacTick.Scale = env.Tick
sc.SacTick.Max = sc.FixDur
sc.SacTick.Cur = sc.SacTick.Max - 1 // ensure that we saccade next time
}
func (sc *Saccade) ConfigTable(dt *etable.Table) {
yx := []string{"Y", "X"}
sch := etable.Schema{
{"TrialName", etensor.STRING, nil, nil},
{"Tick", etensor.INT64, nil, nil},
{"SacTick", etensor.INT64, nil, nil},
{"World", etensor.FLOAT32, []int{sc.WorldVisSz.Y, sc.WorldVisSz.X}, yx},
{"Eye", etensor.FLOAT32, []int{sc.WorldVisSz.Y, sc.WorldVisSz.X}, yx},
{"Plan", etensor.FLOAT32, []int{sc.WorldVisSz.Y, sc.WorldVisSz.X}, yx},
{"View", etensor.FLOAT32, []int{sc.ViewVisSz.Y, sc.ViewVisSz.X}, yx},
{"ObjPos", etensor.FLOAT32, []int{2}, nil},
{"ObjViewPos", etensor.FLOAT32, []int{2}, nil},
{"ObjVel", etensor.FLOAT32, []int{2}, nil},
{"ObjPosNext", etensor.FLOAT32, []int{2}, nil},
{"EyePos", etensor.FLOAT32, []int{2}, nil},
{"SacPlan", etensor.FLOAT32, []int{2}, nil},
{"Saccade", etensor.FLOAT32, []int{2}, nil},
}
dt.SetFromSchema(sch, 0)
}
func (sc *Saccade) WriteToTable(dt *etable.Table) {
row := 0
if sc.AddRows {
row = dt.Rows
}
dt.SetNumRows(row + 1)
nm := fmt.Sprintf("t %d, s %d, x %+4.2f, y %+4.2f", sc.Tick.Cur, sc.SacTick.Cur, sc.ObjPos.X, sc.ObjPos.Y)
dt.SetCellString("TrialName", row, nm)
dt.SetCellFloat("Tick", row, float64(sc.Tick.Cur))
dt.SetCellFloat("SacTick", row, float64(sc.SacTick.Cur))
sc.WorldTsr.SetZeros()
opx := int(math.Floor(float64(0.5 * (sc.ObjPos.X + 1) * float32(sc.WorldVisSz.X))))
opy := int(math.Floor(float64(0.5 * (sc.ObjPos.Y + 1) * float32(sc.WorldVisSz.Y))))
idx := []int{opy, opx}
if sc.WorldTsr.IdxIsValid(idx) {
sc.WorldTsr.SetFloat(idx, 1)
} else {
log.Printf("Saccade: World index invalid: %v\n", idx)
}
sc.ViewTsr.SetZeros()
opx = int(math.Floor(float64((0.5 * (sc.ObjViewPos.X + sc.ViewPct) / sc.ViewPct) * float32(sc.ViewVisSz.X))))
opy = int(math.Floor(float64((0.5 * (sc.ObjViewPos.Y + sc.ViewPct) / sc.ViewPct) * float32(sc.ViewVisSz.Y))))
idx = []int{opy, opx}
if sc.ViewTsr.IdxIsValid(idx) {
sc.ViewTsr.SetFloat(idx, 1)
} else {
log.Printf("Saccade: View index invalid: %v\n", idx)
}
sc.EyePosTsr.SetZeros()
opx = int(math.Floor(float64(0.5 * (sc.EyePos.X + 1) * float32(sc.WorldVisSz.X))))
opy = int(math.Floor(float64(0.5 * (sc.EyePos.Y + 1) * float32(sc.WorldVisSz.Y))))
idx = []int{opy, opx}
if sc.EyePosTsr.IdxIsValid(idx) {
sc.EyePosTsr.SetFloat(idx, 1)
} else {
log.Printf("Saccade: EyePos index invalid: %v\n", idx)
}
sc.SacPlanTsr.SetZeros()
opx = int(math.Floor(float64(0.5 * (sc.SacPlan.X + 1) * float32(sc.WorldVisSz.X))))
opy = int(math.Floor(float64(0.5 * (sc.SacPlan.Y + 1) * float32(sc.WorldVisSz.Y))))
idx = []int{opy, opx}
if sc.SacPlanTsr.IdxIsValid(idx) {
sc.SacPlanTsr.SetFloat(idx, 1)
} else {
log.Printf("Saccade: SacPlan index invalid: %v\n", idx)
}
dt.SetCellTensor("World", row, sc.WorldTsr)
dt.SetCellTensor("Eye", row, sc.EyePosTsr)
dt.SetCellTensor("Plan", row, sc.SacPlanTsr)
dt.SetCellTensor("View", row, sc.ViewTsr)
dt.SetCellTensorFloat1D("ObjPos", row, 0, float64(sc.ObjPos.X))
dt.SetCellTensorFloat1D("ObjPos", row, 1, float64(sc.ObjPos.Y))
dt.SetCellTensorFloat1D("ObjViewPos", row, 0, float64(sc.ObjViewPos.X))
dt.SetCellTensorFloat1D("ObjViewPos", row, 1, float64(sc.ObjViewPos.Y))
dt.SetCellTensorFloat1D("ObjVel", row, 0, float64(sc.ObjVel.X))
dt.SetCellTensorFloat1D("ObjVel", row, 1, float64(sc.ObjVel.Y))
dt.SetCellTensorFloat1D("ObjPosNext", row, 0, float64(sc.ObjPosNext.X))
dt.SetCellTensorFloat1D("ObjPosNext", row, 1, float64(sc.ObjPosNext.Y))
dt.SetCellTensorFloat1D("EyePos", row, 0, float64(sc.EyePos.X))
dt.SetCellTensorFloat1D("EyePos", row, 1, float64(sc.EyePos.Y))
dt.SetCellTensorFloat1D("SacPlan", row, 0, float64(sc.SacPlan.X))
dt.SetCellTensorFloat1D("SacPlan", row, 1, float64(sc.SacPlan.Y))
dt.SetCellTensorFloat1D("Saccade", row, 0, float64(sc.Saccade.X))
dt.SetCellTensorFloat1D("Saccade", row, 1, float64(sc.Saccade.Y))
}
func (sc *Saccade) LimitVel(vel, start, trials float32) float32 {
if trials <= 0 {
return vel
}
end := start + vel*trials
if end > sc.World.Max {
vel = (sc.World.Max - start) / trials
} else if end < sc.World.Min {
vel = (sc.World.Min - start) / trials
}
return vel
}
func (sc *Saccade) LimitPos(pos, max float32) float32 {
if pos > max {
pos = max
}
if pos < -max {
pos = -max
}
return pos
}
func (sc *Saccade) LimitSac(sacDev, start, objPos, objVel, trials float32) float32 {
objEnd := objPos + objVel*trials
eyep := start + sacDev
lowView := eyep + sc.View.Min
highView := eyep + sc.View.Max
// do obj_end first then pos so it has stronger constraint
if objEnd < lowView {
sacDev += (objEnd - lowView)
} else if objEnd > highView {
sacDev += objEnd - highView
}
eyep = start + sacDev
if eyep < sc.World.Min {
sacDev += sc.World.Min - eyep
} else if eyep > sc.World.Max {
sacDev += sc.World.Max - eyep
}
eyep = start + sacDev
lowView = eyep + sc.View.Min
highView = eyep + sc.View.Max
if objPos < lowView {
sacDev += objPos - lowView
} else if objPos > highView {
sacDev += objPos - highView
}
eyep = start + sacDev
if eyep < sc.World.Min {
sacDev += sc.World.Min - eyep
} else if eyep > sc.World.Max {
sacDev += sc.World.Max - eyep
}
return sacDev
}
// NextTraj computes the next object position and trajectory, at start of a new object sequence
func (sc *Saccade) NextTraj() {
sc.TrajLen = sc.TrajLenRange.Min + rand.Intn(sc.TrajLenRange.Range()+1)
zeroVel := erand.BoolP(sc.ZeroVelP, -1)
if sc.RandObjPos {
// note: resetting object position disrupts predictability..
sc.ObjPosNext.X = sc.World.Min + rand.Float32()*sc.World.Range()
sc.ObjPosNext.Y = sc.World.Min + rand.Float32()*sc.World.Range()
}
if zeroVel {
sc.ObjVelNext.SetZero()
} else {
sc.ObjVelNext.X = -sc.VelGenMax + 2*rand.Float32()*sc.VelGenMax
sc.ObjVelNext.Y = -sc.VelGenMax + 2*rand.Float32()*sc.VelGenMax
sc.ObjVelNext.X = sc.LimitVel(sc.ObjVelNext.X, sc.ObjPosNext.X, float32(sc.TrajLen))
sc.ObjVelNext.Y = sc.LimitVel(sc.ObjVelNext.Y, sc.ObjPosNext.Y, float32(sc.TrajLen))
}
if sc.RandObjPos {
sc.SacPlan = sc.ObjPosNext.Sub(sc.EyePos)
} else {
sc.ObjPosNext = sc.ObjPos.Add(sc.ObjVelNext)
sc.SacPlan = sc.ObjPosNext.Sub(sc.EyePos)
}
// saccade directly to position of new object at start -- set duration too
sc.FixDur = sc.FixDurRange.Min + rand.Intn(sc.FixDurRange.Range()+1)
sc.SacTick.Cur = sc.SacTick.Max - 1 // ensure that we saccade next time
sc.NewTrajNext = true
}
// NextSaccade generates next saccade plan
func (sc *Saccade) NextSaccade() {
sc.FixDur = sc.FixDurRange.Min + rand.Intn(sc.FixDurRange.Range()+1)
sc.SacPlan.X = -sc.SacGenMax + 2*rand.Float32()*sc.SacGenMax
sc.SacPlan.Y = -sc.SacGenMax + 2*rand.Float32()*sc.SacGenMax
sc.SacPlan.X = sc.LimitSac(sc.SacPlan.X, sc.EyePos.X, sc.ObjPosNext.X, sc.ObjVelNext.X, float32(sc.FixDur))
sc.SacPlan.Y = sc.LimitSac(sc.SacPlan.Y, sc.EyePos.Y, sc.ObjPosNext.Y, sc.ObjVelNext.Y, float32(sc.FixDur))
}
// DoSaccade updates current eye position with planned saccade, resets plan
func (sc *Saccade) DoSaccade() {
sc.EyePos.X = sc.EyePos.X + sc.SacPlan.X
sc.EyePos.Y = sc.EyePos.Y + sc.SacPlan.Y
sc.Saccade.X = sc.SacPlan.X
sc.Saccade.Y = sc.SacPlan.Y
sc.SacPlan.X = 0
sc.SacPlan.Y = 0
}
// DoneSaccade clears saccade state
func (sc *Saccade) DoneSaccade() {
sc.Saccade.X = 0
sc.Saccade.Y = 0
}
// Step is primary method to call -- generates next state and
// outputs currents tate to table
func (sc *Saccade) Step() {
sc.NewTraj = sc.Tick.Incr()
sc.NewSac = sc.SacTick.Incr()
if sc.NewTrajNext {
sc.NewTrajNext = false
}
if sc.NewTraj {
sc.Tick.Max = sc.TrajLen // was computed last time
sc.ObjVel = sc.ObjVelNext
}
if sc.NewSac { // actually move eyes according to plan
sc.DoSaccade()
sc.SacTick.Max = sc.FixDur // was computed last time
} else {
sc.DoneSaccade()
}
// increment state -- next has already been computed
sc.ObjPos = sc.ObjPosNext
sc.ObjViewPos = sc.ObjPos.Sub(sc.EyePos)
// now make new plans
// if we will exceed traj next time, prepare new trajectory
if sc.Tick.Cur+1 >= sc.Tick.Max {
sc.NextTraj()
} else { // otherwise, move object along and see if we need to plan saccade
sc.ObjPosNext = sc.ObjPos.Add(sc.ObjVel)
if sc.SacTick.Cur+1 >= sc.SacTick.Max {
sc.NextSaccade()
}
}
// write current state to table
sc.WriteToTable(sc.Table)
}