-
Notifications
You must be signed in to change notification settings - Fork 70
/
distsplot.go
149 lines (119 loc) · 3.59 KB
/
distsplot.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
// Copyright (c) 2020, Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Command distsplot plots histograms of random distributions
package main
//go:generate core generate -add-types
import (
"strconv"
"cogentcore.org/core/base/randx"
"cogentcore.org/core/core"
"cogentcore.org/core/events"
"cogentcore.org/core/icons"
"cogentcore.org/core/math32/minmax"
"cogentcore.org/core/plot/plotview"
"cogentcore.org/core/tensor"
"cogentcore.org/core/tensor/stats/histogram"
"cogentcore.org/core/tensor/table"
"cogentcore.org/core/views"
)
func main() {
TheSim.Config()
TheSim.ConfigGUI()
}
// LogPrec is precision for saving float values in logs
const LogPrec = 4
// Sim holds the params, table, etc
type Sim struct {
// random params
Dist randx.RandParams
// number of samples
NSamp int
// number of bins in the histogram
NBins int
// range for histogram
Range minmax.F64
// table for raw data
Table *table.Table `view:"no-inline"`
// histogram of data
Hist *table.Table `view:"no-inline"`
// the plot
Plot *plotview.PlotView `view:"-"`
}
// 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.Dist.Defaults()
ss.Dist.Dist = randx.Gaussian
ss.Dist.Mean = 0.5
ss.Dist.Var = 0.15
ss.NSamp = 1000000
ss.NBins = 100
ss.Range.Set(0, 1)
ss.Update()
ss.Table = &table.Table{}
ss.Hist = &table.Table{}
ss.ConfigTable(ss.Table)
ss.Run()
}
// Update updates computed values
func (ss *Sim) Update() {
}
// Run generates the data and plots a histogram of results
func (ss *Sim) Run() {
ss.Update()
dt := ss.Table
dt.SetNumRows(ss.NSamp)
for vi := 0; vi < ss.NSamp; vi++ {
vl := ss.Dist.Gen()
dt.SetFloat("Val", vi, float64(vl))
}
histogram.F64Table(ss.Hist, dt.Columns[0].(*tensor.Float64).Values, ss.NBins, ss.Range.Min, ss.Range.Max)
if ss.Plot != nil {
ss.Plot.UpdatePlot()
}
}
func (ss *Sim) ConfigTable(dt *table.Table) {
dt.SetMetaData("name", "Data")
dt.SetMetaData("read-only", "true")
dt.SetMetaData("precision", strconv.Itoa(LogPrec))
dt.AddFloat64Column("Val")
}
func (ss *Sim) ConfigPlot(plt *plotview.PlotView, dt *table.Table) *plotview.PlotView {
plt.Params.Title = "Rand Dist Histogram"
plt.Params.XAxisColumn = "Value"
plt.Params.Type = plotview.Bar
plt.Params.XAxisRotation = 45
plt.SetTable(dt)
// order of params: on, fixMin, min, fixMax, max
plt.SetColParams("Value", plotview.Off, plotview.FloatMin, 0, plotview.FloatMax, 0)
plt.SetColParams("Count", plotview.On, plotview.FixMin, 0, plotview.FloatMax, 0)
return plt
}
// ConfigGUI configures the Cogent Core GUI interface for this simulation.
func (ss *Sim) ConfigGUI() *core.Body {
b := core.NewBody("distplot")
split := core.NewSplits(b, "split")
sv := views.NewStructView(split, "sv")
sv.SetStruct(ss)
tv := core.NewTabs(split, "tv")
pt := tv.NewTab("Histogram")
plt := plotview.NewPlotView(pt)
ss.Plot = ss.ConfigPlot(plt, ss.Hist)
split.SetSplits(.3, .7)
b.AddAppBar(func(tb *core.Toolbar) {
core.NewButton(tb).SetText("Run").SetIcon(icons.Update).
SetTooltip("Generate data and plot histogram.").
OnClick(func(e events.Event) {
ss.Run()
})
core.NewButton(tb).SetText("README").SetIcon(icons.FileMarkdown).
SetTooltip("Opens your browser on the README file that contains instructions for how to run this model.").
OnClick(func(e events.Event) {
core.TheApp.OpenURL("https://github.com/emer/emergent/v2/blob/master/randx/distplot/README.md")
})
})
b.RunMainWindow()
return b
}