-
Notifications
You must be signed in to change notification settings - Fork 24
/
plots.go
182 lines (160 loc) · 4.78 KB
/
plots.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
// Copyright (c) 2022, 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 egui
import (
"fmt"
"log"
"cogentcore.org/core/colors"
"cogentcore.org/core/errors"
"github.com/emer/emergent/v2/elog"
"github.com/emer/emergent/v2/etime"
"github.com/emer/etable/v2/eplot"
"github.com/emer/etable/v2/etview"
)
// AddPlots adds plots based on the unique tables we have,
// currently assumes they should always be plotted
func (gui *GUI) AddPlots(title string, lg *elog.Logs) {
gui.Plots = make(map[etime.ScopeKey]*eplot.Plot2D)
// for key, table := range Log.Tables {
for _, key := range lg.TableOrder {
modes, times := key.ModesAndTimes()
time := times[0]
mode := modes[0]
lt := lg.Tables[key] // LogTable struct
if doplot, has := lt.Meta["Plot"]; has {
if doplot == "false" {
continue
}
}
plt := gui.NewPlotTab(key, mode+" "+time+" Plot")
plt.SetTable(lt.Table)
plt.Params.FromMetaMap(lt.Meta)
ConfigPlotFromLog(title, plt, lg, key)
}
}
func ConfigPlotFromLog(title string, plt *eplot.Plot2D, lg *elog.Logs, key etime.ScopeKey) {
_, times := key.ModesAndTimes()
time := times[0]
lt := lg.Tables[key] // LogTable struct
for _, item := range lg.Items {
_, ok := item.Write[key]
if !ok {
continue
}
cp := plt.SetColParams(item.Name, item.Plot, item.FixMin, item.Range.Min, item.FixMax, item.Range.Max)
if item.Color != "" {
cp.Color = errors.Log1(colors.FromString(item.Color, nil))
}
cp.TensorIndex = item.TensorIndex
cp.ErrCol = item.ErrCol
plt.Params.Title = title + " " + time + " Plot"
plt.Params.XAxisCol = time
if xaxis, has := lt.Meta["XAxisCol"]; has {
plt.Params.XAxisCol = xaxis
}
if legend, has := lt.Meta["LegendCol"]; has {
plt.Params.LegendCol = legend
}
}
plt.ColsFromMetaMap(lt.Table.MetaData)
plt.ColsFromMetaMap(lt.Meta)
plt.Update()
}
// Plot returns plot for mode, time scope
func (gui *GUI) Plot(mode etime.Modes, time etime.Times) *eplot.Plot2D {
return gui.PlotScope(etime.Scope(mode, time))
}
// PlotScope returns plot for given scope
func (gui *GUI) PlotScope(scope etime.ScopeKey) *eplot.Plot2D {
if !gui.Active {
return nil
}
plot, ok := gui.Plots[scope]
if !ok {
// fmt.Printf("egui Plot not found for scope: %s\n", scope)
return nil
}
return plot
}
// SetPlot stores given plot in Plots map
func (gui *GUI) SetPlot(scope etime.ScopeKey, plt *eplot.Plot2D) {
if gui.Plots == nil {
gui.Plots = make(map[etime.ScopeKey]*eplot.Plot2D)
}
gui.Plots[scope] = plt
}
// UpdatePlot updates plot for given mode, time scope
func (gui *GUI) UpdatePlot(mode etime.Modes, time etime.Times) *eplot.Plot2D {
plot := gui.Plot(mode, time)
if plot != nil {
plot.GoUpdatePlot()
}
return plot
}
// UpdatePlotScope updates plot at given scope
func (gui *GUI) UpdatePlotScope(scope etime.ScopeKey) *eplot.Plot2D {
plot := gui.PlotScope(scope)
if plot != nil {
plot.GoUpdatePlot()
}
return plot
}
// UpdateCyclePlot updates cycle plot for given mode.
// only updates every CycleUpdateInterval
func (gui *GUI) UpdateCyclePlot(mode etime.Modes, cycle int) *eplot.Plot2D {
plot := gui.Plot(mode, etime.Cycle)
if plot == nil {
return plot
}
if (gui.CycleUpdateInterval > 0) && (cycle%gui.CycleUpdateInterval == 0) {
plot.GoUpdatePlot()
}
return plot
}
// NewPlotTab adds a new plot with given key for Plots lookup
// and using given tab label. For ad-hoc plots, you can
// construct a ScopeKey from any two strings using etime.ScopeStr.
func (gui *GUI) NewPlotTab(key etime.ScopeKey, tabLabel string) *eplot.Plot2D {
plt := eplot.NewSubPlot(gui.Tabs.NewTab(tabLabel))
gui.Plots[key] = plt
return plt
}
// AddTableView adds a table view of given log,
// typically particularly useful for Debug logs.
func (gui *GUI) AddTableView(lg *elog.Logs, mode etime.Modes, time etime.Times) {
if gui.TableViews == nil {
gui.TableViews = make(map[etime.ScopeKey]*etview.TableView)
}
key := etime.Scope(mode, time)
lt, ok := lg.Tables[key]
if !ok {
log.Printf("ERROR: in egui.AddTableView, log: %s not found\n", key)
return
}
tt := gui.Tabs.NewTab(mode.String() + " " + time.String() + " ")
tv := etview.NewTableView(tt)
gui.TableViews[key] = tv
tv.SetTable(lt.Table)
}
// TableView returns TableView for mode, time scope
func (gui *GUI) TableView(mode etime.Modes, time etime.Times) *etview.TableView {
if !gui.Active {
return nil
}
scope := etime.Scope(mode, time)
tv, ok := gui.TableViews[scope]
if !ok {
fmt.Printf("egui TableView not found for scope: %s\n", scope)
return nil
}
return tv
}
// UpdateTableView updates TableView for given mode, time scope
func (gui *GUI) UpdateTableView(mode etime.Modes, time etime.Times) *etview.TableView {
tv := gui.TableView(mode, time)
if tv != nil {
tv.GoUpdateView()
}
return tv
}