Skip to content

Commit

Permalink
fix goal transpile auto-add tensor functions paths
Browse files Browse the repository at this point in the history
  • Loading branch information
rcoreilly committed Dec 23, 2024
1 parent a30a391 commit 0e0b8dc
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 2 deletions.
139 changes: 139 additions & 0 deletions examples/random/random.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// 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.

// Package random plots histograms of random distributions.
package main

//go:generate core generate -add-types

import (
"cogentcore.org/core/base/metadata"
"cogentcore.org/core/core"
"cogentcore.org/core/events"
"cogentcore.org/core/icons"
"cogentcore.org/core/math32/minmax"
"cogentcore.org/core/styles"
"cogentcore.org/core/tree"
"cogentcore.org/lab/base/randx"
"cogentcore.org/lab/plot"
"cogentcore.org/lab/plot/plots"
"cogentcore.org/lab/plotcore"
"cogentcore.org/lab/stats/histogram"
"cogentcore.org/lab/table"
"cogentcore.org/lab/tensor"
)

// Random is the random distribution plotter widget.
type Random struct { //types:add
core.Frame
Data
}

// Data contains the random distribution plotter data and options.
type Data struct { //types:add
// random params
Dist randx.RandParams `display:"add-fields"`

// number of samples
NumSamples int

// number of bins in the histogram
NumBins int

// range for histogram
Range minmax.F64

// table for raw data
Table *table.Table `display:"no-inline"`

// histogram of data
Histogram *table.Table `display:"no-inline"`

// the plot
plot *plotcore.PlotEditor `display:"-"`
}

// logPrec is the precision for saving float values in logs.
const logPrec = 4

func (rd *Random) Init() {
rd.Frame.Init()

rd.Dist.Defaults()
rd.Dist.Dist = randx.Gaussian
rd.Dist.Mean = 0.5
rd.Dist.Var = 0.15
rd.NumSamples = 1000000
rd.NumBins = 100
rd.Range.Set(0, 1)
rd.Table = table.New()
rd.Histogram = table.New()
rd.ConfigTable(rd.Table)
rd.Plot()

rd.Styler(func(s *styles.Style) {
s.Grow.Set(1, 1)
})
tree.AddChild(rd, func(w *core.Splits) {
w.SetSplits(0.3, 0.7)
tree.AddChild(w, func(w *core.Form) {
w.SetStruct(&rd.Data)
w.OnChange(func(e events.Event) {
rd.Plot()
})
})
tree.AddChild(w, func(w *plotcore.PlotEditor) {
w.SetTable(rd.Histogram)
rd.plot = w
})
})
}

// Plot generates the data and plots a histogram of results.
func (rd *Random) Plot() { //types:add
dt := rd.Table

dt.SetNumRows(rd.NumSamples)
for vi := 0; vi < rd.NumSamples; vi++ {
vl := rd.Dist.Gen()
dt.Column("Value").SetFloat(float64(vl), vi)
}

histogram.F64Table(rd.Histogram, dt.Columns.Values[0].(*tensor.Float64).Values, rd.NumBins, rd.Range.Min, rd.Range.Max)
if rd.plot != nil {
rd.plot.UpdatePlot()
}
}

func (rd *Random) ConfigTable(dt *table.Table) {
metadata.SetName(dt, "Data")
// metadata.SetReadOnly(dt, true)
tensor.SetPrecision(dt, logPrec)
val := dt.AddFloat64Column("Value")
plot.SetFirstStylerTo(val, func(s *plot.Style) {
s.Role = plot.X
s.Plotter = plots.BarType
s.Plot.XAxis.Rotation = 45
s.Plot.Title = "Random distribution histogram"
})
}

func (rd *Random) MakeToolbar(p *tree.Plan) {
tree.Add(p, func(w *core.FuncButton) {
w.SetFunc(rd.Plot).SetIcon(icons.ScatterPlot)
})
tree.Add(p, func(w *core.Separator) {})
if rd.plot != nil {
rd.plot.MakeToolbar(p)
}
}

func main() {
b := core.NewBody("Random numbers")
rd := NewRandom(b)
b.AddTopBar(func(bar *core.Frame) {
core.NewToolbar(bar).Maker(rd.MakeToolbar)
})
b.RunMainWindow()
}
11 changes: 11 additions & 0 deletions examples/random/typegen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion goal/transpile/addfuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,22 @@ func init() {
AddYaegiTensorFuncs()
}

var yaegiTensorPackages = []string{"/lab/tensor", "/lab/stats", "/lab/vector", "/lab/matrix"}

// AddYaegiTensorFuncs grabs all tensor* package functions registered
// in yaegicore and adds them to the `tensor.Funcs` map so we can
// properly convert symbols to either tensors or basic literals,
// depending on the arg types for the current function.
func AddYaegiTensorFuncs() {
for pth, symap := range nogui.Symbols {
if !strings.Contains(pth, "/core/tensor/") {
has := false
for _, p := range yaegiTensorPackages {
if strings.Contains(pth, p) {
has = true
break
}
}
if !has {
continue
}
_, pkg := path.Split(pth)
Expand Down
2 changes: 1 addition & 1 deletion goal/transpile/transpile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ Label: "Reset RunLog",
func TestCur(t *testing.T) {
// logx.UserLevel = slog.LevelDebug
tests := []exIn{
{`Label: "Reset RunLog",`, `Label: "Reset RunLog",`},
{"# x := 1", `x := tensor.Tensor(tensor.NewIntScalar(1))`},
}
st := NewState()
st.MathRecord = false
Expand Down

0 comments on commit 0e0b8dc

Please sign in to comment.