math-optim/report/pic.go
surtur 3c8678a8d3
All checks were successful
continuous-integration/drone/push Build is passing
go,tmpl: plot mean values
2022-07-18 22:36:47 +02:00

94 lines
1.8 KiB
Go

// Copyright 2022 wanderer <a_mirre at utb dot cz>
// SPDX-License-Identifier: GPL-3.0-or-later
package report
import (
_ "embed"
"log"
"os"
"sort"
"text/template"
"time"
"git.dotya.ml/wanderer/math-optim/util"
)
// Pic holds path to pic along with its caption.
type Pic struct {
Caption string
FilePath string
}
// PicList is a structure holding a slice of pics and {bench,algo} metadata.
type PicList struct {
Algo string
Bench string
Pics []Pic
}
//go:embed pics.tmpl
var tmplPicsFile []byte
// NewPic returns a new copy of Pic.
func NewPic() *Pic {
return &Pic{}
}
// NewPicList returns a new copy of PicList.
func NewPicList() *PicList {
return &PicList{}
}
// SavePicsToFile saves each pic list for all bench funcs of a specified algo
// to a file.
func SavePicsToFile(pls, plsMean []PicList, algoName string) {
var paths []string
ptf := picTexFiles{Algo: algoName}
for i, p := range pls {
safeName := util.SanitiseFName(p.Algo + "-" + p.Bench)
texPicsFile := GetTexDir() + "pics-" + safeName + ".tex"
tmplPics := template.New("pics")
tmplPics = template.Must(tmplPics.Parse(string(tmplPicsFile)))
paths = append(paths, texPicsFile)
// keep the slice sorted.
sort.Strings(paths)
// make sure the output dir exists, else die early.
if err := util.CreatePath(GetTexDir()); err != nil {
log.Fatalln(err)
}
f, err := os.Create(texPicsFile)
if err != nil {
log.Println(err)
}
defer f.Close()
err = tmplPics.Execute(f, struct {
Algo string
Bench string
Pics []Pic
PicsMean []Pic
Timestamp time.Time
}{
Algo: p.Algo,
Bench: p.Bench,
Pics: p.Pics,
PicsMean: plsMean[i].Pics,
Timestamp: time.Now(),
})
if err != nil {
log.Println(err)
}
}
ptf.FilePaths = paths
allPics.TexFiles = append(allPics.TexFiles, ptf)
}