math-optim/report/pic.go

115 lines
2.3 KiB
Go
Raw Normal View History

2023-01-12 23:35:51 +01:00
// Copyright 2023 wanderer <a_mirre at utb dot cz>
2022-07-12 22:31:29 +02:00
// 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"
"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.
// 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
Bench string // optionally set.
2022-07-12 22:31:29 +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
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
// 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{}
}
// 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-12-24 11:30:22 +01:00
paths := make([]string, 0, len(pls))
ptf := picTexFiles{Algo: algoName}
2022-07-18 22:36:47 +02:00
for i, p := range pls {
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-17 11:36:59 +02:00
tmplPics = template.Must(tmplPics.Parse(string(tmplPicsFile)))
2022-07-12 22:31:29 +02:00
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)
}
2022-07-12 22:31:29 +02:00
f, err := os.Create(texPicsFile)
if err != nil {
log.Println(err)
}
defer f.Close()
2022-07-12 22:31:29 +02:00
err = tmplPics.Execute(f, struct {
Algo string
Bench string
Pics []Pic
2022-07-18 22:36:47 +02:00
PicsMean []Pic
Timestamp time.Time
}{
Algo: p.Algo,
Bench: p.Bench,
Pics: p.Pics,
2022-07-18 22:36:47 +02:00
PicsMean: plsMean[i].Pics,
Timestamp: time.Now(),
})
2022-07-12 22:31:29 +02:00
if err != nil {
log.Println(err)
}
2022-07-12 22:31:29 +02:00
}
ptf.FilePaths = paths
allPics.TexFiles = append(allPics.TexFiles, ptf)
2022-07-12 22:31:29 +02:00
}