2022-07-12 22:31:29 +02:00
|
|
|
// Copyright 2022 wanderer <a_mirre at utb dot cz>
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
package report
|
|
|
|
|
|
|
|
import (
|
2022-07-17 11:36:59 +02:00
|
|
|
_ "embed"
|
2022-07-12 22:31:29 +02:00
|
|
|
"log"
|
|
|
|
"os"
|
2022-07-16 22:05:15 +02:00
|
|
|
"sort"
|
2022-07-12 22:31:29 +02:00
|
|
|
"text/template"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.dotya.ml/wanderer/math-optim/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Pic holds path to pic along with its caption.
|
2022-07-27 17:45:07 +02:00
|
|
|
// Bench field is set optionally (e.g. in case of the pic depicting
|
|
|
|
// comparison of means).
|
2022-07-12 22:31:29 +02:00
|
|
|
type Pic struct {
|
|
|
|
Caption string
|
|
|
|
FilePath string
|
2022-07-27 17:45:07 +02:00
|
|
|
Bench string // optionally set.
|
2022-07-12 22:31:29 +02:00
|
|
|
}
|
|
|
|
|
2022-07-27 23:19:02 +02:00
|
|
|
type PlotPics []Pic
|
|
|
|
|
2022-07-12 22:31:29 +02:00
|
|
|
// PicList is a structure holding a slice of pics and {bench,algo} metadata.
|
|
|
|
type PicList struct {
|
|
|
|
Algo string
|
|
|
|
Bench string
|
2022-07-27 23:19:02 +02:00
|
|
|
Pics PlotPics
|
2022-07-12 22:31:29 +02:00
|
|
|
}
|
|
|
|
|
2022-07-17 11:36:59 +02:00
|
|
|
//go:embed pics.tmpl
|
|
|
|
var tmplPicsFile []byte
|
|
|
|
|
2022-07-27 23:19:02 +02:00
|
|
|
// Len implements the sort.Interface.
|
|
|
|
func (p PlotPics) Len() int {
|
|
|
|
return len(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Less implements the sort.Interface.
|
|
|
|
// note: sorting based on filename
|
|
|
|
func (p PlotPics) Less(i, j int) bool {
|
|
|
|
return p[i].FilePath < p[j].FilePath
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap implements the sort.Interface.
|
|
|
|
func (p PlotPics) Swap(i, j int) {
|
|
|
|
p[i], p[j] = p[j], p[i]
|
|
|
|
}
|
|
|
|
|
2022-07-12 22:31:29 +02:00
|
|
|
// NewPic returns a new copy of Pic.
|
|
|
|
func NewPic() *Pic {
|
|
|
|
return &Pic{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPicList returns a new copy of PicList.
|
|
|
|
func NewPicList() *PicList {
|
|
|
|
return &PicList{}
|
|
|
|
}
|
|
|
|
|
2022-07-16 21:40:53 +02:00
|
|
|
// SavePicsToFile saves each pic list for all bench funcs of a specified algo
|
|
|
|
// to a file.
|
2022-07-18 22:36:47 +02:00
|
|
|
func SavePicsToFile(pls, plsMean []PicList, algoName string) {
|
2022-07-16 22:05:15 +02:00
|
|
|
var paths []string
|
|
|
|
|
|
|
|
ptf := picTexFiles{Algo: algoName}
|
|
|
|
|
2022-07-18 22:36:47 +02:00
|
|
|
for i, p := range pls {
|
2022-07-16 21:40:53 +02:00
|
|
|
safeName := util.SanitiseFName(p.Algo + "-" + p.Bench)
|
|
|
|
texPicsFile := GetTexDir() + "pics-" + safeName + ".tex"
|
2022-07-17 11:36:59 +02:00
|
|
|
tmplPics := template.New("pics")
|
2022-07-13 23:47:37 +02:00
|
|
|
|
2022-07-17 11:36:59 +02:00
|
|
|
tmplPics = template.Must(tmplPics.Parse(string(tmplPicsFile)))
|
2022-07-12 22:31:29 +02:00
|
|
|
|
2022-07-16 22:05:15 +02:00
|
|
|
paths = append(paths, texPicsFile)
|
|
|
|
// keep the slice sorted.
|
|
|
|
sort.Strings(paths)
|
|
|
|
|
2022-07-16 21:40:53 +02:00
|
|
|
// make sure the output dir exists, else die early.
|
|
|
|
if err := util.CreatePath(GetTexDir()); err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
2022-07-12 22:31:29 +02:00
|
|
|
|
2022-07-16 21:40:53 +02:00
|
|
|
f, err := os.Create(texPicsFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2022-07-12 22:31:29 +02:00
|
|
|
|
2022-07-16 21:40:53 +02:00
|
|
|
err = tmplPics.Execute(f, struct {
|
|
|
|
Algo string
|
|
|
|
Bench string
|
|
|
|
Pics []Pic
|
2022-07-18 22:36:47 +02:00
|
|
|
PicsMean []Pic
|
2022-07-16 21:40:53 +02:00
|
|
|
Timestamp time.Time
|
|
|
|
}{
|
|
|
|
Algo: p.Algo,
|
|
|
|
Bench: p.Bench,
|
|
|
|
Pics: p.Pics,
|
2022-07-18 22:36:47 +02:00
|
|
|
PicsMean: plsMean[i].Pics,
|
2022-07-16 21:40:53 +02:00
|
|
|
Timestamp: time.Now(),
|
|
|
|
})
|
2022-07-12 22:31:29 +02:00
|
|
|
|
2022-07-16 21:40:53 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2022-07-12 22:31:29 +02:00
|
|
|
}
|
2022-07-16 22:05:15 +02:00
|
|
|
|
|
|
|
ptf.FilePaths = paths
|
|
|
|
allPics.TexFiles = append(allPics.TexFiles, ptf)
|
2022-07-12 22:31:29 +02:00
|
|
|
}
|