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

Changed the plot functions. We now have a basic plot from which the two real...

Changed the plot functions. We now have a basic plot from which the two real plots start and then append their specific information.
parent 02e7e64e
No related branches found
No related tags found
No related merge requests found
...@@ -8,3 +8,6 @@ clean: ...@@ -8,3 +8,6 @@ clean:
go clean go clean
rm -rf */*_delme.* rm -rf */*_delme.*
rm -rf */test_tmp* rm -rf */test_tmp*
rm -rf /tmp/go-build[0-9]*
rm -rf /tmp/go.*.mod
rm -rf /tmp/go.*.sum
...@@ -7,3 +7,6 @@ In dorun.go ...@@ -7,3 +7,6 @@ In dorun.go
* Change the tests so all files are written to a temporary directory * Change the tests so all files are written to a temporary directory
Note.. Benchmarking suggests that most of the time is spent in Ackley function, and, to be exact, in the cosine and exp() function. Note.. Benchmarking suggests that most of the time is spent in Ackley function, and, to be exact, in the cosine and exp() function.
In the plottin functions,
Put a secondary axis on the X trajectory or check out multiplots so that the two plots line up with each other.
...@@ -268,7 +268,7 @@ func doRun(mcPrm *mcPrm) error { ...@@ -268,7 +268,7 @@ func doRun(mcPrm *mcPrm) error {
saveStep(&cprm, mcPrm.nStep, tmprtr, x, fOld) saveStep(&cprm, mcPrm.nStep, tmprtr, x, fOld)
} }
defer fmt.Println("n accepted:", nAcc, "of", mcPrm.nStep+1) defer fmt.Println("n accepted:", nAcc, "of", mcPrm.nStep+1)
breaker()
if err := plotfWrt(&cprm); err != nil { if err := plotfWrt(&cprm); err != nil {
return err return err
} }
...@@ -285,7 +285,7 @@ func doRun(mcPrm *mcPrm) error { ...@@ -285,7 +285,7 @@ func doRun(mcPrm *mcPrm) error {
fmt.Println ("xBuf for plotting is this big", len(xBuf.Bytes())) fmt.Println ("xBuf for plotting is this big", len(xBuf.Bytes()))
xdata = xBuf.Bytes() xdata = xBuf.Bytes()
} }
breaker(fdata, xdata) nothing (fdata, xdata)
// ui.Scrnplt(fdata, xdata) // ui.Scrnplt(fdata, xdata)
} }
......
...@@ -22,6 +22,11 @@ import ( ...@@ -22,6 +22,11 @@ import (
"gitlab.rrz.uni-hamburg.de/Bae5157/axticks" "gitlab.rrz.uni-hamburg.de/Bae5157/axticks"
) )
const (
chrtWdth = 800
chrtHt = 400
)
// maketicks gives reasonable default tick locations // maketicks gives reasonable default tick locations
func maketicks(axisDscrpt axticks.AxisDscrpt) []chart.Tick { func maketicks(axisDscrpt axticks.AxisDscrpt) []chart.Tick {
xmin, xmax, delta, prcsn := axisDscrpt.Xmin, axisDscrpt.Xmax, axisDscrpt.Delta, axisDscrpt.Prcsn xmin, xmax, delta, prcsn := axisDscrpt.Xmin, axisDscrpt.Xmax, axisDscrpt.Delta, axisDscrpt.Prcsn
...@@ -41,22 +46,6 @@ func maketicks(axisDscrpt axticks.AxisDscrpt) []chart.Tick { ...@@ -41,22 +46,6 @@ func maketicks(axisDscrpt axticks.AxisDscrpt) []chart.Tick {
return t return t
} }
// maketicks this version works, but usually leaves you wanting one tick more.
/*func alt_maketicks(axisDscrpt axticks.AxisDscrpt, prcsn int) []chart.Tick {
xmin, xmax, delta, prcsn := axisDscrpt.Xmin, axisDscrpt.Xmax, axisDscrpt.Delta, axisDscrpt.Prcsn
var t []chart.Tick
const fstr = "%.*f"
xv, xl := xmin, fmt.Sprintf(fstr, prcsn, xmin)
if xmin > 3.56 && xmin < 3.58 {
fmt.Println (axisDscrpt)
}
for ; xv <= xmax; xv, xl = xv+delta, fmt.Sprintf("%.*f", prcsn, xv) {
t = append(t, chart.Tick {Value: xv, Label: xl})
}
return t
}*/
// range2 just lets us append methods to a range/continuousrange structure // range2 just lets us append methods to a range/continuousrange structure
type range2 struct { type range2 struct {
chart.ContinuousRange chart.ContinuousRange
...@@ -77,27 +66,14 @@ func (rng *range2) GetTicks(r chart.Renderer, cs chart.Style, vf chart.ValueForm ...@@ -77,27 +66,14 @@ func (rng *range2) GetTicks(r chart.Renderer, cs chart.Style, vf chart.ValueForm
} }
} }
// plotfWrt writes out a plot of function values to the given // plotbase returns a chart that serves as the basis for both the function
// io.Writer. It used to ask for a string. // values and the X trajectories. This lets us set up the x-axis once and
func plotfWrt(cprm *cprm) error { // put the temperature on both plots so they come out the same size.
if cprm.fplotWrt == nil { func plotbase(cprm *cprm) *chart.Chart {
return nil
}
xdata := cprm.plotnstp // We just unpack for readability
ydata := cprm.plotf
tmprtrData := cprm.plotTmprtr
xaxis := chart.XAxis{ xaxis := chart.XAxis{
Name: "step", Name: "step",
Range: &range2{}, Range: &range2{},
} }
fvalAxis := chart.YAxis{
Name: "cost (arb units)",
NameStyle: chart.Style{TextRotationDegrees: 360}, // Zero does not work
AxisType: chart.YAxisSecondary,
Range: &range2{},
}
var tmprtrAxis chart.YAxis var tmprtrAxis chart.YAxis
if cprm.coolme { // If we are not cooling, we do not plot if cprm.coolme { // If we are not cooling, we do not plot
tmprtrAxis = chart.YAxis{ // the temperature axis. tmprtrAxis = chart.YAxis{ // the temperature axis.
...@@ -109,39 +85,54 @@ func plotfWrt(cprm *cprm) error { ...@@ -109,39 +85,54 @@ func plotfWrt(cprm *cprm) error {
tmprtrAxis = chart.YAxis{} // to mess up the scaling on the tmprtrAxis = chart.YAxis{} // to mess up the scaling on the
tmprtrAxis.Style = chart.Hidden() // other axis tmprtrAxis.Style = chart.Hidden() // other axis
} }
xdata := cprm.plotnstp
leftAxis := chart.YAxis{
NameStyle: chart.Style{TextRotationDegrees: 360}, // Zero does not work
AxisType: chart.YAxisSecondary,
Range: &range2{},
}
graph := chart.Chart{ graph := chart.Chart{
Width: 800, Width: chrtWdth, Height: chrtHt,
Title: "cost function",
Series: []chart.Series{ Series: []chart.Series{
chart.ContinuousSeries{
Name: "temperature",
XValues: xdata,
YValues: cprm.plotTmprtr,
},
chart.ContinuousSeries{ // The function values chart.ContinuousSeries{ // The function values
Name: "cost", Name: "specify me",
YAxis: chart.YAxisSecondary, YAxis: chart.YAxisSecondary,
XValues: xdata, XValues: xdata,
YValues: ydata,
Style: chart.Style{ Style: chart.Style{
StrokeWidth: 5, StrokeWidth: 4,
DotWidth: 0, DotWidth: 0,
}, },
}, },
chart.ContinuousSeries{
Name: "temperature",
XValues: xdata,
YValues: tmprtrData,
},
}, },
YAxis: tmprtrAxis, YAxis: tmprtrAxis,
YAxisSecondary: fvalAxis, YAxisSecondary: leftAxis,
XAxis: xaxis, XAxis: xaxis,
Background: chart.Style{ Background: chart.Style{
Padding: chart.Box{ Padding: chart.Box{Left: 75, Right: 75},
Left: 50,
Right: 75,
},
FillColor: drawing.ColorTransparent, FillColor: drawing.ColorTransparent,
}, },
} }
return &graph
}
// plotfWrt plots the function values and temperature to an io.Writer that
// it finds in the cprm structure. It might be writing to a file or a buffer.
func plotfWrt(cprm *cprm) error {
graph := plotbase(cprm)
graph.YAxisSecondary.Name = "cost (arb units)"
if funcSeries, ok := graph.Series[1].(chart.ContinuousSeries); ok {
funcSeries.Name = "cost"
funcSeries.YValues = cprm.plotf
graph.Series[1] = funcSeries
} else {
panic("prog bug type assertion function plot")
}
if err := graph.Render(chart.PNG, cprm.fplotWrt); err != nil { if err := graph.Render(chart.PNG, cprm.fplotWrt); err != nil {
return fmt.Errorf("Render: %w", err) return fmt.Errorf("Render: %w", err)
...@@ -151,16 +142,21 @@ func plotfWrt(cprm *cprm) error { ...@@ -151,16 +142,21 @@ func plotfWrt(cprm *cprm) error {
} }
// plotxWrt // plotxWrt
// For each dimension, allocate space. Copy elements over from the long array. // For each dimension, allocate space. The X's are stored in a single
// Then call the plotter on each series in turn. I think I have to make a // array, so here we allocate an array of arrays for each dimension and
// slice of continuous series and then add them into the chart structure. // copy the elements. We then take the graph object and toss out the first
// continuousSeries. Then, we make a series of ContinousSeries, appending
// each in turn to the []graph.Series.
func plotxWrt(cprm *cprm, ndim int) error { func plotxWrt(cprm *cprm, ndim int) error {
if cprm.xplotWrt == nil { return nil } if cprm.xplotWrt == nil {
return nil
}
graph := plotbase(cprm)
type f64 []float64 type f64 []float64
len_used := len(cprm.plotnstp) len_used := len(cprm.plotnstp)
xdata := make([]f64, ndim) xdata := make([]f64, ndim)
for i := 0; i < ndim; i++ { for i := 0; i < ndim; i++ {
xdata[i] = make([]float64, len_used) xdata[i] = make([]float64, len_used, len_used)
} }
var n int var n int
for i := 0; i < len_used; i++ { for i := 0; i < len_used; i++ {
...@@ -169,29 +165,18 @@ func plotxWrt(cprm *cprm, ndim int) error { ...@@ -169,29 +165,18 @@ func plotxWrt(cprm *cprm, ndim int) error {
n++ n++
} }
} }
series := make([]chart.Series, ndim) graph.Title = fmt.Sprintf("X trajectories %d dimension", ndim)
graph.YAxisSecondary.Name = "X coord"
if baseSeries, ok := graph.Series[1].(chart.ContinuousSeries); ok {
graph.Series = graph.Series[:1]
for i := 0; i < ndim; i++ { for i := 0; i < ndim; i++ {
series[i] = chart.ContinuousSeries{ tmp := baseSeries
Name: fmt.Sprintf("dimension %d", i), tmp.Name = fmt.Sprintf("dim %d", i)
XValues: cprm.plotnstp, tmp.YValues = xdata[i]
YValues: xdata[i], graph.Series = append(graph.Series, tmp)
}
} }
graph := chart.Chart{ } else {
Title: fmt.Sprintf("X trajectories %d dimension", ndim), panic("program bug, type assertion on y axis xtrajectories")
XAxis: chart.XAxis{Name: "step", Range: &range2{}},
YAxis: chart.YAxis{
Name: "x coord",
Range: &range2{},
AxisType: chart.YAxisSecondary,
NameStyle: chart.Style{TextRotationDegrees: 360},
},
Series: series,
Background: chart.Style{
Padding: chart.Box{
Left: 75,
},
},
} }
if err := graph.Render(chart.PNG, cprm.xplotWrt); err != nil { if err := graph.Render(chart.PNG, cprm.xplotWrt); err != nil {
return fmt.Errorf("plotting X trajectories: %w", err) return fmt.Errorf("plotting X trajectories: %w", err)
......
...@@ -48,3 +48,5 @@ func removeQuotes(s string) string { ...@@ -48,3 +48,5 @@ func removeQuotes(s string) string {
} }
return s return s
} }
func nothing (...interface{}){}
...@@ -17,7 +17,10 @@ import ( ...@@ -17,7 +17,10 @@ import (
func breaker(...interface{}) {} func breaker(...interface{}) {}
func Scrnplt(fdata []byte, xdata []byte) { func Scrnplt(fdata []byte, xdata []byte) {
if len(fdata) == 0 && len (xdata) == 0 { return }
fmt.Println("scrnplt says fbuf size", len(fdata), "first few bytes:", string(fdata[:10])) 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])) fmt.Println("scrnplt says xbuf size", len(xdata), "first few bytes:", string(xdata[:10]))
a := app.New() a := app.New()
w := a.NewWindow("container") w := a.NewWindow("container")
...@@ -31,8 +34,8 @@ func Scrnplt(fdata []byte, xdata []byte) { ...@@ -31,8 +34,8 @@ func Scrnplt(fdata []byte, xdata []byte) {
text2 := canvas.NewText("There", green) text2 := canvas.NewText("There", green)
//top, bottom, left, right //top, bottom, left, right
// content = container.NewMax(image, text1) // content = container.NewMax(image, text1)
content := container.NewHSplit(fImage, xImage) content := container.NewVSplit(fImage, xImage)
content.SetOffset(0.95) content.SetOffset(0.50)
breaker(text1, content, text2) breaker(text1, content, text2)
w.Resize(fyne.NewSize(500, 320)) w.Resize(fyne.NewSize(500, 320))
w.SetContent(content) w.SetContent(content)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment