-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.go
177 lines (159 loc) · 4.65 KB
/
runner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"bytes"
"flag"
"fmt"
"github.com/olekukonko/tablewriter"
"io/ioutil"
"log"
"os"
"os/exec"
"strconv"
"strings"
"sync"
)
const header = "\033[95m"
const blue = "\033[94m"
const green = "\033[92m"
const orange = "\033[93m"
const red = "\033[91m"
const endc = "\033[0m"
const bold = "\033[1m"
const underline = "\033[4m"
type result struct {
oldScore int
newScore int
status string
}
func makebold(s string) string {
return bold + s + endc
}
func okgreen(s string) string {
return green + s + endc
}
func okblue(s string) string {
return blue + s + endc
}
func warnorange(s string) string {
return orange + s + endc
}
func nokred(s string) string {
return red + s + endc
}
func main() {
datasets := flag.String("datasets", "A", "datasets to evaluate")
model := flag.String("model", "./model.sh", "model model executable")
scorer := flag.String("scorer", "./scorer.sh", "scorer executable")
datafolder := flag.String("datafolder", "data", "folder containing datasets")
submissionsfolder := flag.String("submissionsfolder", "submissions", "folder containing submissions")
realtimeoutput := flag.Bool("realtimeoutput", false, "print the output of the models in stdout in real time (always the case if there is only one dataset)")
flag.Parse()
results := make([]result, len(*datasets))
if len(*datasets) < 2 {
*realtimeoutput = true
}
// Run the computations in parallel
{
wg := sync.WaitGroup{}
for i, dataset := range *datasets {
results[i] = result{-1, -1, nokred("worse")}
wg.Add(1)
go func(res *result, dataset string) {
defer wg.Done()
testDataset(dataset, *model, *scorer, res, *datafolder, *submissionsfolder, *realtimeoutput)
}(&results[i], string(dataset))
}
wg.Wait()
}
resTable := tablewriter.NewWriter(os.Stdout)
resTable.SetHeader([]string{"Test case", "Old score", "New score", "Status"})
resTable.SetRowLine(true)
for i, dataset := range *datasets {
r := results[i]
resTable.Append([]string{makebold(string(dataset)), strconv.Itoa(r.oldScore), strconv.Itoa(r.newScore), r.status})
}
resTable.Render()
}
func testDataset(dataset string, model string, scorer string, res *result, datafolder string, submissionsfolder string, realtimeoutput bool) {
// Compute file names
inputFileName := fmt.Sprintf("%s/%s.in", datafolder, dataset)
scoreFileName := fmt.Sprintf("%s/%s.score", submissionsfolder, dataset)
outputFileName := fmt.Sprintf("%s/%s.out", submissionsfolder, dataset)
tmpOutputFileName := fmt.Sprintf("%s-tmp/%s.out.tmp", submissionsfolder, dataset)
// Read oldScore
var oldScore int
{
oldscore, err := ioutil.ReadFile(scoreFileName)
if err != nil {
log.Println("Couldn't load ", scoreFileName, ":", err)
} else {
oldScore, err = strconv.Atoi(strings.TrimSpace(string(oldscore)))
if err != nil {
log.Println("Couldn't parse", scoreFileName, "as int: ", oldscore)
return
}
res.oldScore = oldScore
}
}
// Run the model
var modelOutput []byte
{
modelCmd := exec.Command(model, inputFileName, tmpOutputFileName)
if realtimeoutput {
modelCmd.Stdout, modelCmd.Stderr = os.Stdout, os.Stderr
err := modelCmd.Run()
if err != nil {
log.Println("Error executing model", dataset, ":", err)
return
}
modelOutput = []byte("See above\n")
} else {
modelOutput, err := modelCmd.CombinedOutput()
if err != nil {
log.Println("Error executing model", dataset, ":", err)
log.Println("model output for dataset", dataset, ":\n", string(modelOutput), "---")
return
}
}
}
// Compute new score
var newScore int
var scorerStderr bytes.Buffer
{
scorerCmd := exec.Command(scorer, inputFileName, tmpOutputFileName)
scorerCmd.Stderr = &scorerStderr
scoreout, err := scorerCmd.Output()
if err != nil {
log.Println("Error computing new score:", err)
return
}
newScore, err = strconv.Atoi(strings.TrimSpace(string(scoreout)))
if err != nil {
log.Println("Couldn't parse new score as int: ", string(scoreout))
return
}
if newScore > oldScore {
err := swapFiles(tmpOutputFileName, outputFileName)
if err != nil {
log.Println("Failed to swap output with the submission:", err)
return
}
err = ioutil.WriteFile(scoreFileName, scoreout, 0777)
if err != nil {
log.Println("Error writing new score:", err)
return
}
res.status = okgreen("better")
} else if newScore == oldScore {
res.status = warnorange("same")
}
}
res.newScore = newScore
// Output infos
fmt.Printf("%s\n%s\n%s%s\n%s",
okblue(makebold(fmt.Sprintf("Dataset %s finished with a score of %d.", dataset, newScore))),
makebold("- model stdout & stderr:"),
modelOutput,
makebold("- scorer stderr:"),
scorerStderr.Bytes())
}