Skip to content

Commit

Permalink
armaze and chnoose updated
Browse files Browse the repository at this point in the history
  • Loading branch information
rcoreilly committed Jun 17, 2024
1 parent 71c99ed commit 41c1678
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 56 deletions.
117 changes: 64 additions & 53 deletions examples/choose/armaze/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ type GUI struct {
EnvName string

// 3D visualization of the Scene
SceneView *xyzcore.SceneEditor
SceneEditor *xyzcore.SceneEditor

// 2D visualization of the Scene
Scene2D *core.SVG
Expand Down Expand Up @@ -255,10 +255,9 @@ func (vw *GUI) ConfigWorldGUI(ev *Env) *core.Body {

vw.ConfigWorld()

vw.SceneView = xyzcore.NewSceneEditor(scfr)
vw.SceneView.Name = "sceneview"
vw.SceneView.Config()
se := vw.SceneView.SceneXYZ()
vw.SceneEditor = xyzcore.NewSceneEditor(scfr)
vw.SceneEditor.UpdateWidget()
se := vw.SceneEditor.SceneXYZ()
vw.ConfigView3D(se)

se.Camera.Pose.Pos = math32.Vec3(0, 29, -4)
Expand Down Expand Up @@ -335,8 +334,8 @@ func (vw *GUI) ConfigWorldGUI(ev *Env) *core.Body {
func (vw *GUI) ConfigWorld() {
ev := vw.Env

vw.World = &physics.Group{}
vw.World.InitName(vw.World, "ArmMaze")
vw.World = physics.NewGroup()
vw.World.SetName("RoomWorld")

vw.Geom.Config(ev.Config.NArms, ev.MaxLength)

Expand All @@ -345,7 +344,7 @@ func (vw *GUI) ConfigWorld() {
vw.Stims = vw.ConfigStims(vw.World, "stims", .9, .1)

vw.Emery = vw.ConfigEmery(vw.World, 1)
vw.EyeR = vw.Emery.ChildByName("head", 1).ChildByName("eye-r", 2).(physics.Body)
vw.EyeR = vw.Emery.ChildByName("head", 1).AsTree().ChildByName("eye-r", 2).(physics.Body)

vw.World.WorldInit()

Expand All @@ -356,32 +355,36 @@ func (vw *GUI) ConfigWorld() {
func (vw *GUI) AddFloor(par *physics.Group, name string) *physics.Group {
ge := &vw.Geom
dp := ge.Depth + 3*ge.LengthScale
rm := physics.NewGroup(par, name)
physics.NewBox(rm, "floor").SetSize(math32.Vec3(ge.Width, ge.Thick, dp)).
SetColor("grey").SetInitPos(math32.Vec3(0, -ge.Thick/2, -ge.Depth/2-ge.LengthScale))
rm := physics.NewGroup(par)
rm.SetName(name)
physics.NewBox(rm).SetSize(math32.Vec3(ge.Width, ge.Thick, dp)).
SetColor("grey").SetInitPos(math32.Vec3(0, -ge.Thick/2, -ge.Depth/2-ge.LengthScale)).SetName("floor")

return rm
}

// ConfigArms adds all the arms
func (vw *GUI) ConfigArms(par *physics.Group) *physics.Group {
ev := vw.Env
rm := physics.NewGroup(par, "arms")
rm := physics.NewGroup(par)
rm.SetName("arms")
ge := &vw.Geom
exln := ge.LengthScale
halfarm := .5 * ge.ArmWidth
halfht := .5 * ge.Height
for i, arm := range ev.Config.Arms {
anm := fmt.Sprintf("arm_%d\n", i)
agp := physics.NewGroup(rm, anm)
agp := physics.NewGroup(rm)
agp.SetName(anm)
x, _ := ge.Pos(i, 0)
ln := ge.LengthScale * float32(arm.Length)
halflen := .5*ln + exln

physics.NewBox(agp, "left-wall").SetSize(math32.Vec3(ge.Thick, ge.Height, ln)).
SetColor("black").SetInitPos(math32.Vec3(x-halfarm, halfht, -halflen))
physics.NewBox(agp).SetSize(math32.Vec3(ge.Thick, ge.Height, ln)).
SetColor("black").SetInitPos(math32.Vec3(x-halfarm, halfht, -halflen)).SetName("left-wall")

physics.NewBox(agp, "right-wall").SetSize(math32.Vec3(ge.Thick, ge.Height, ln)).
SetColor("black").SetInitPos(math32.Vec3(x+halfarm, halfht, -halflen))
physics.NewBox(agp).SetSize(math32.Vec3(ge.Thick, ge.Height, ln)).
SetColor("black").SetInitPos(math32.Vec3(x+halfarm, halfht, -halflen)).SetName("right-wall")
}
return rm
}
Expand All @@ -390,7 +393,8 @@ func (vw *GUI) ConfigArms(par *physics.Group) *physics.Group {
func (vw *GUI) ConfigStims(par *physics.Group, name string, width, height float32) *physics.Group {
ev := vw.Env
ge := &vw.Geom
stms := physics.NewGroup(par, name)
stms := physics.NewGroup(par)
stms.SetName(name)
exln := ge.LengthScale
// halfarm := .5 * ge.ArmWidth
usHt := ge.Height
Expand All @@ -403,19 +407,19 @@ func (vw *GUI) ConfigStims(par *physics.Group, name string, width, height float3
usnm := fmt.Sprintf("us_%d\n", i)
csnm := fmt.Sprintf("cs_%d\n", i)

physics.NewBox(stms, usnm).SetSize(math32.Vec3(ge.ArmWidth, usHt, usDp)).
SetColor(vw.MatColors[arm.US]).SetInitPos(math32.Vec3(x, 0.5*usHt, -ln-1.1*exln))
physics.NewBox(stms).SetSize(math32.Vec3(ge.ArmWidth, usHt, usDp)).
SetColor(vw.MatColors[arm.US]).SetInitPos(math32.Vec3(x, 0.5*usHt, -ln-1.1*exln)).SetName(usnm)

physics.NewBox(stms, csnm).SetSize(math32.Vec3(ge.ArmWidth, csHt, ge.Thick)).
SetColor(vw.MatColors[arm.CS]).SetInitPos(math32.Vec3(x, usHt+0.5*csHt, -ln-2*exln))
physics.NewBox(stms).SetSize(math32.Vec3(ge.ArmWidth, csHt, ge.Thick)).
SetColor(vw.MatColors[arm.CS]).SetInitPos(math32.Vec3(x, usHt+0.5*csHt, -ln-2*exln)).SetName(csnm)
}
return stms
}

func (vw *GUI) UpdateStims() {
var updts []string
ev := vw.Env
stms := *vw.Stims.Children
stms := vw.Stims.Children
for i, moi := range stms {
mo := moi.(*physics.Box)
if i%2 == 1 { // CS
Expand All @@ -424,42 +428,46 @@ func (vw *GUI) UpdateStims() {
clr := vw.MatColors[arm.CS]
if mo.Color != clr {
mo.Color = clr
updts = append(updts, mo.Name())
updts = append(updts, mo.Name)
}
}
}
if len(updts) > 0 {
vw.View3D.UpdateBodyView(updts)
vw.View3D.UpdateBodyView(updts...)
}
}

// ConfigEmery constructs a new Emery virtual hamster
func (vw *GUI) ConfigEmery(par *physics.Group, length float32) *physics.Group {
emr := physics.NewGroup(par, "emery")
emr := physics.NewGroup(par)
emr.SetName("emery")
height := length / 2
width := height

physics.NewBox(emr, "body").SetSize(math32.Vec3(width, height, length)).
SetColor("purple").SetDynamic().
SetInitPos(math32.Vec3(0, height/2, 0))
physics.NewBox(emr).SetSize(math32.Vec3(width, height, length)).
SetColor("purple").SetDynamic(true).
SetInitPos(math32.Vec3(0, height/2, 0)).SetName("body")

headsz := height * 0.75
hhsz := .5 * headsz
hgp := physics.NewGroup(emr, "head").SetInitPos(math32.Vec3(0, hhsz, -(length/2 + hhsz)))

physics.NewBox(hgp, "head").SetSize(math32.Vec3(headsz, headsz, headsz)).
SetColor("tan").SetDynamic().SetInitPos(math32.Vec3(0, 0, 0))
hgp := physics.NewGroup(emr).SetInitPos(math32.Vec3(0, hhsz, -(length/2 + hhsz)))
hgp.SetName("head")
physics.NewBox(hgp).SetSize(math32.Vec3(headsz, headsz, headsz)).
SetColor("tan").SetDynamic(true).SetInitPos(math32.Vec3(0, 0, 0)).
SetName("head")

eyesz := headsz * .2

physics.NewBox(hgp, "eye-l").SetSize(math32.Vec3(eyesz, eyesz*.5, eyesz*.2)).
SetColor("green").SetDynamic().
SetInitPos(math32.Vec3(-hhsz*.6, headsz*.1, -(hhsz + eyesz*.3)))
physics.NewBox(hgp).SetSize(math32.Vec3(eyesz, eyesz*.5, eyesz*.2)).
SetColor("green").SetDynamic(true).
SetInitPos(math32.Vec3(-hhsz*.6, headsz*.1, -(hhsz + eyesz*.3))).
SetName("eye-l")

// note: centering this in head for now to get straight-on view
physics.NewBox(hgp, "eye-r").SetSize(math32.Vec3(eyesz, eyesz*.5, eyesz*.2)).
SetColor("green").SetDynamic().
SetInitPos(math32.Vec3(0, headsz*.1, -(hhsz + eyesz*.3)))
physics.NewBox(hgp).SetSize(math32.Vec3(eyesz, eyesz*.5, eyesz*.2)).
SetColor("green").SetDynamic(true).
SetInitPos(math32.Vec3(0, headsz*.1, -(hhsz + eyesz*.3))).
SetName("eye-r")

return emr
}
Expand All @@ -473,7 +481,8 @@ func (vw *GUI) ConfigView3D(se *xyz.Scene) {
dir.Pos.Set(0, 2, 1) // default: 0,1,1 = above and behind us (we are at 0,0,X)

// sc.MultiSample = 1 // we are using depth grab so we need this = 1
wgp := xyz.NewGroup(se, "world")
wgp := xyz.NewGroup(se)
wgp.SetName("world")
vw.View3D = world.NewView(vw.World, se, wgp)
vw.View3D.InitLibrary() // this makes a basic library based on body shapes, sizes
// at this point the library can be updated to configure custom visualizations
Expand All @@ -482,22 +491,24 @@ func (vw *GUI) ConfigView3D(se *xyz.Scene) {
}

func (vw *GUI) ConfigUSPlots() {
dp := table.NewTable(vw.Env.Config.NDrives + 1)
dp := table.NewTable()
dp.AddStringColumn("US")
dp.AddFloat64Column("Drive")
dp.AddFloat64Column("OFC")
dp.AddFloat64Column("USin")
dp.SetNumRows(vw.Env.Config.NDrives + 1)

vw.USposData = dp
vw.USposPlot.Params.Type = plotcore.Bar
vw.USposPlot.Params.Title = "Positive USs"
vw.USposPlot.Params.Scale = 1
vw.USposPlot.Params.XAxisColumn = "US"

dn := table.NewTable(vw.Env.Config.NNegUSs + 2)
dn := table.NewTable()
dn.AddStringColumn("US")
dn.AddFloat64Column("OFC")
dn.AddFloat64Column("USin")
dn.SetNumRows(vw.Env.Config.NNegUSs + 2)

vw.USnegData = dn
vw.USnegPlot.Params.Type = plotcore.Bar
Expand Down Expand Up @@ -581,23 +592,23 @@ func (vw *GUI) ConfigWorldView(tg *tensorcore.TensorGrid) {
}
colormap.AvailableMaps[cnm] = cm
}
tg.Disp.Defaults()
tg.Disp.ColorMap = core.ColorMapName(cnm)
tg.Disp.GridFill = 1
tg.Display.Defaults()
tg.Display.ColorMap = core.ColorMapName(cnm)
tg.Display.GridFill = 1
}

func (vw *GUI) UpdateWorld(ctx *axon.Context, ev *Env, net *axon.Network, state TraceStates) {
vw.State = state
vw.Trace.AddRec(ctx, uint32(ev.Di), ev, net, state)
if vw.SceneView == nil || !vw.Disp {
if vw.SceneEditor == nil || !vw.Disp {
return
}

if vw.Env != ev {
vw.Env = ev
vw.EnvName = ev.Nm
vw.Trace = nil
vw.StructView.UpdateFields()
vw.StructView.Update()
}

vw.UpdateWorldGUI()
Expand All @@ -612,24 +623,24 @@ func (vw *GUI) SetEmeryPose() {
}

func (vw *GUI) UpdateWorldGUI() {
if vw.SceneView == nil || !vw.Disp {
if vw.SceneEditor == nil || !vw.Disp {
return
}
vw.SceneView.AsyncLock()
defer vw.SceneView.AsyncUnlock()
vw.SceneEditor.AsyncLock()
defer vw.SceneEditor.AsyncUnlock()

// update state:
vw.SetEmeryPose()
vw.UpdateStims()
vw.World.WorldRelToAbs()
vw.View3D.UpdatePose()
vw.View3D.UpdateBodyView([]string{"body"})
vw.View3D.UpdateBodyView("body")
// vw.View2D.UpdatePose()

// update views:
vw.GrabEyeImg()
if vw.SceneView.IsVisible() {
vw.SceneView.NeedsRender()
if vw.SceneEditor.IsVisible() {
vw.SceneEditor.NeedsRender()
}
// if vw.Scene2D.IsVisible() {
// vw.Scene2D.SetNeedsRender(true)
Expand Down
6 changes: 3 additions & 3 deletions examples/choose/choose.go
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,7 @@ func (ss *Sim) ConfigLogItems() {
Write: elog.WriteMap{
etime.Scope(etime.Train, etime.Epoch): func(ctx *elog.Context) {
ix := ctx.Logs.IndexView(ctx.Mode, etime.Trial)
spl := split.GroupBy(ix, []string{"Instinct"})
spl := split.GroupBy(ix, "Instinct")
split.AggColumnTry(spl, "ActMatch", stats.Mean)
ags := spl.AggsToTable(table.ColumnNameOnly)
ss.Logs.MiscTables["ActCor"] = ags
Expand Down Expand Up @@ -1140,7 +1140,7 @@ func (ss *Sim) EpochCSBadStats() {
ix.Filter(func(et *table.Table, row int) bool {
return !math.IsNaN(et.Float("BadCSGate", row)) // && (et.StringValue("ActAction", row) == "Consume")
})
spl := split.GroupBy(ix, []string{"BadCSGate"})
spl := split.GroupBy(ix, "BadCSGate")
for _, ts := range ix.Table.ColumnNames {
col := ix.Table.ColumnByName(ts)
if col.DataType() == reflect.String || ts == "BadCSGate" {
Expand Down Expand Up @@ -1185,7 +1185,7 @@ func (ss *Sim) EpochUSBadStats() {
ix.Filter(func(et *table.Table, row int) bool {
return !math.IsNaN(et.Float("BadUSGate", row)) // && (et.StringValue("ActAction", row) == "Consume")
})
spl := split.GroupBy(ix, []string{"BadUSGate"})
spl := split.GroupBy(ix, "BadUSGate")
for _, ts := range ix.Table.ColumnNames {
col := ix.Table.ColumnByName(ts)
if col.DataType() == reflect.String || ts == "BadUSGate" {
Expand Down

0 comments on commit 41c1678

Please sign in to comment.