Skip to content
Snippets Groups Projects
Commit 02e7e64e authored by Andrew E. Torda's avatar Andrew E. Torda
Browse files

Now makes two plots, function values and X trajectory

parent 77de8d18
No related branches found
No related tags found
No related merge requests found
Makefile 0 → 100644
# Use the normal go build system for everything, but we want to automate some simple
# commands.
all:
go build ./...
clean:
go clean
rm -rf */*_delme.*
rm -rf */test_tmp*
When playing with the axis ticks,
replace gitlab.rrz.uni-hamburg.de/Bae5157/axticks => ../axticks
in the go.mod file
A chart from go-chart in png format can be displayed by fyne.
A chart in svg format leads to parse errors.
\ No newline at end of file
ini_temp 0.18
final_temp 0.01
x_ini 7,1,7
n_step 100000
......@@ -3,7 +3,6 @@ module example.com/ackley_mc
go 1.16
require (
fyne.io/fyne v1.4.3
fyne.io/fyne/v2 v2.1.2
github.com/wcharczuk/go-chart/v2 v2.1.0
gitlab.rrz.uni-hamburg.de/Bae5157/axticks v0.0.0-20220120103412-d663ebb46145
......
In dorun.go
* do we have the random number seed as a settable parameter ?
* make the csv files optional
* Change the tests so all files are written to a temporary directory
......
......@@ -32,7 +32,7 @@ func getSeed() int64 {
seedLocker.Unlock()
return r
}
func breaker (...interface{}) {}
type withBytes interface {
Bytes() []byte
}
......@@ -49,7 +49,7 @@ type cprm struct { // parameters calculated from input
plotXtrj []float32 // for plotting trajectories
coolme bool // Cooling ? or just constant temperature ?
fplotWrt io.Writer // Where to write the plot of function values
xplotme bool // Are we plotting X trajectories
xplotWrt io.Writer // Where to write the plot of x trajectories
}
// isNotSane checks for obviously silly parameters
......@@ -89,7 +89,7 @@ func setupRun(mcPrm *mcPrm, cprm *cprm) error {
coolrate *= math.Log(fnlTmp / iniTmp)
cprm.coolMult = math.Exp(-coolrate)
}
if mcPrm.fOutName != "" { // We are going to write to a file
if mcPrm.fOutName != "" { // We are going to write text, .csv
if mcPrm.fOutName, err = setSuffix(mcPrm.fOutName, ".csv"); err != nil {
return err
}
......@@ -100,8 +100,8 @@ func setupRun(mcPrm *mcPrm, cprm *cprm) error {
cprm.doTxtFval = true // other parts of code can see that we are writing to file
}
if mcPrm.fPltName != "" { // Then we are writing to a file
if mcPrm.fPltName, err = setSuffix(mcPrm.fPltName, ".svg"); err != nil {
if mcPrm.fPltName != "" { // Function value plot to file
if mcPrm.fPltName, err = setSuffix(mcPrm.fPltName, ".png"); err != nil {
return fmt.Errorf("plot filename: %w", err)
}
if cprm.fplotWrt, err = os.Create(mcPrm.fPltName); err != nil {
......@@ -119,13 +119,17 @@ func setupRun(mcPrm *mcPrm, cprm *cprm) error {
}
if mcPrm.xPltName != "" {
if mcPrm.xPltName, err = setSuffix(mcPrm.xPltName, ".svg"); err != nil {
if mcPrm.xPltName, err = setSuffix(mcPrm.xPltName, ".png"); err != nil {
return fmt.Errorf("plot filename: %w", err)
}
cprm.xplotme = true
n_dim := len(mcPrm.xIni)
cprm.plotXtrj = make([]float32, 0, int(n_alloc)*n_dim)
if cprm.xplotWrt, err = os.Create(mcPrm.xPltName); err != nil {
return err
}
} else { // Write to buffer, to plot on screen
var w bytes.Buffer
cprm.xplotWrt = &w // I think we can remove the preceding line
}
cprm.plotXtrj = make([]float32, 0, int(n_alloc)*len(mcPrm.xIni))
if err = isNotSane(mcPrm); err != nil {
return err
}
......@@ -166,14 +170,12 @@ func printfVal(fOut io.Writer, x []float32, n uint32, tmprtr float64, fOld float
// saveStep is called when we accept a change. It writes or saves for
// later plotting.
func saveStep(cprm *cprm, n uint32, tmprtr float64, x []float32, fTrial float64) {
if cprm.fplotWrt != nil || cprm.xplotme {
cprm.plotnstp = append(cprm.plotnstp, float64(n))
}
if cprm.fplotWrt != nil { // Function values
cprm.plotnstp = append(cprm.plotnstp, float64(n)) // num steps
if cprm.fplotWrt != nil { // Function values // function values
cprm.plotf = append(cprm.plotf, fTrial)
cprm.plotTmprtr = append(cprm.plotTmprtr, tmprtr)
}
if cprm.xplotme { // The trajectory
if cprm.xplotWrt != nil { // The trajectory
cprm.plotXtrj = append(cprm.plotXtrj, x...)
}
if cprm.doTxtFval {
......@@ -266,16 +268,32 @@ func doRun(mcPrm *mcPrm) error {
saveStep(&cprm, mcPrm.nStep, tmprtr, x, fOld)
}
defer fmt.Println("n accepted:", nAcc, "of", mcPrm.nStep+1)
breaker()
if err := plotfWrt(&cprm); err != nil {
return err
}
if bBuf, ok := cprm.fplotWrt.(withBytes); ok {
fmt.Println("bBuf for plotting is this big", len(bBuf.Bytes()))
}
if err := plotxWrt(&cprm, mcPrm.xPltName, len(mcPrm.xIni)); err != nil {
if err := plotxWrt (&cprm, len(mcPrm.xIni)); err != nil {
return err
}
{
var fdata, xdata []byte
if funcBuf, ok := cprm.fplotWrt.(withBytes); ok {
fmt.Println("funcBuf for plotting is this big", len(funcBuf.Bytes()))
fdata = funcBuf.Bytes()
}
if xBuf, ok := cprm.xplotWrt.(withBytes); ok {
fmt.Println ("xBuf for plotting is this big", len(xBuf.Bytes()))
xdata = xBuf.Bytes()
}
breaker(fdata, xdata)
// ui.Scrnplt(fdata, xdata)
}
/* if bBuf, := plotxWrt(&cprm, mcPrm.xPltName, len(mcPrm.xIni)); err != nil {
return err
} */
return nil
}
......@@ -143,7 +143,7 @@ func plotfWrt(cprm *cprm) error {
},
}
if err := graph.Render(chart.SVG, cprm.fplotWrt); err != nil {
if err := graph.Render(chart.PNG, cprm.fplotWrt); err != nil {
return fmt.Errorf("Render: %w", err)
}
......@@ -154,11 +154,8 @@ func plotfWrt(cprm *cprm) error {
// For each dimension, allocate space. Copy elements over from the long array.
// Then call the plotter on each series in turn. I think I have to make a
// slice of continuous series and then add them into the chart structure.
func plotxWrt(cprm *cprm, xPltName string, ndim int) error {
if !cprm.xplotme {
return nil
}
func plotxWrt(cprm *cprm, ndim int) error {
if cprm.xplotWrt == nil { return nil }
type f64 []float64
len_used := len(cprm.plotnstp)
xdata := make([]f64, ndim)
......@@ -196,12 +193,7 @@ func plotxWrt(cprm *cprm, xPltName string, ndim int) error {
},
},
}
xPlt, err := os.Create(xPltName)
if err != nil {
return err
}
defer xPlt.Close()
if err := graph.Render(chart.SVG, xPlt); err != nil {
if err := graph.Render(chart.PNG, cprm.xplotWrt); err != nil {
return fmt.Errorf("plotting X trajectories: %w", err)
}
return nil
......
......@@ -12,20 +12,29 @@ import (
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
// "fyne.io/fyne/v2/layout"
)
func Scrnplt (b []byte) {
fmt.Println ("scrnplt says buf size", len(b), "first few bytes:", string(b[:10]))
func breaker(...interface{}) {}
func Scrnplt(fdata []byte, xdata []byte) {
fmt.Println("scrnplt says fbuf size", len(fdata), "first few bytes:", string(fdata[:10]))
fmt.Println("scrnplt says xbuf size", len(xdata), "first few bytes:", string(xdata[:10]))
a := app.New()
w := a.NewWindow("container")
image := canvas.NewImageFromReader(bytes.NewReader(b), "boo.svg")
fImage := canvas.NewImageFromReader(bytes.NewReader(fdata), "func.png")
fImage.FillMode = canvas.ImageFillContain
xImage := canvas.NewImageFromReader(bytes.NewReader(xdata), "xdata.png")
xImage.FillMode = canvas.ImageFillContain
green := color.NRGBA{R: 0, G: 180, B: 0, A: 255}
text1 := canvas.NewText("Hello", green); print(text1)
text1 := canvas.NewText("Hello", green)
text2 := canvas.NewText("There", green)
text2.Move(fyne.NewPos(20, 20))
content := container.NewWithoutLayout( image) // no more text 1, text2
// content := container.New(layout.NewGridLayout(2), text1, text2)
//top, bottom, left, right
// content = container.NewMax(image, text1)
content := container.NewHSplit(fImage, xImage)
content.SetOffset(0.95)
breaker(text1, content, text2)
w.Resize(fyne.NewSize(500, 320))
w.SetContent(content)
w.ShowAndRun()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment