-
Notifications
You must be signed in to change notification settings - Fork 22
/
record.go
284 lines (255 loc) · 6.46 KB
/
record.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package muniverse
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"strconv"
"time"
"github.com/unixpickle/essentials"
"github.com/unixpickle/muniverse/chrome"
)
const recordingPerm = 0755
// Recording is an on-disk record of observations and
// actions in an environment rollout.
//
// It is not safe to run methods on a Recording from more
// than one Goroutine at a time.
// Further, it is not safe to open a Recording if another
// Goroutine or program is writing to it.
type Recording struct {
dir string
info recordingInfo
}
// OpenRecording opens an existing Recording from a
// directory at the given path.
func OpenRecording(path string) (rec *Recording, err error) {
defer essentials.AddCtxTo("open recording", &err)
rec = &Recording{dir: path}
infoData, err := ioutil.ReadFile(rec.infoPath())
if err != nil {
return
}
err = json.Unmarshal(infoData, &rec.info)
return
}
// CreateRecording creates a new Recording at the given
// path.
func CreateRecording(path string) (rec *Recording, err error) {
defer essentials.AddCtxTo("create recording", &err)
if err = os.Mkdir(path, recordingPerm); err != nil {
return
}
defer func() {
if err != nil {
os.RemoveAll(path)
}
}()
rec = &Recording{dir: path}
if err = os.Mkdir(rec.framesPath(), recordingPerm); err != nil {
return
}
data, err := json.Marshal(rec.info)
if err != nil {
return
}
err = ioutil.WriteFile(rec.infoPath(), data, recordingPerm)
return
}
// WriteObs adds an observation to the Recording.
func (r *Recording) WriteObs(obs Obs) (err error) {
defer essentials.AddCtxTo("write observation", &err)
data, err := ObsPNG(obs)
if err != nil {
return
}
name := strconv.Itoa(r.info.NumObs) + ".png"
imgPath := filepath.Join(r.framesPath(), name)
err = ioutil.WriteFile(imgPath, data, recordingPerm)
if err != nil {
return
}
r.info.NumObs++
if err = r.writeInfo(); err != nil {
r.info.NumObs--
os.Remove(imgPath)
}
return
}
// WriteStep adds a step to the Recording.
func (r *Recording) WriteStep(info *StepInfo) (err error) {
step := recordingStep{
Time: info.Time,
Reward: info.Reward,
Done: info.Done,
}
for _, event := range info.Events {
switch event := event.(type) {
case *chrome.KeyEvent:
step.Events = append(step.Events, recordingEvent{KeyEvent: event})
case *chrome.MouseEvent:
step.Events = append(step.Events, recordingEvent{MouseEvent: event})
default:
return fmt.Errorf("unsupported event type: %T", event)
}
}
oldSteps := r.info.Steps
r.info.Steps = append(r.info.Steps, step)
err = r.writeInfo()
if err != nil {
r.info.Steps = oldSteps
}
return
}
// NumObs returns the number of recorded observations.
func (r *Recording) NumObs() int {
return r.info.NumObs
}
// NumSteps returns the number of recorded steps.
func (r *Recording) NumSteps() int {
return len(r.info.Steps)
}
// ReadObs reads the observation at the given index.
// It fails if the index is out of range.
func (r *Recording) ReadObs(idx int) (obs Obs, err error) {
defer essentials.AddCtxTo("read observation", &err)
if idx < 0 || idx >= r.NumObs() {
return nil, errors.New("index out of range")
}
name := strconv.Itoa(idx) + ".png"
imgPath := filepath.Join(r.framesPath(), name)
data, err := ioutil.ReadFile(imgPath)
if err != nil {
return
}
return pngObs(data), nil
}
// ReadStep reads the step at the given index.
// It fails if the index is out of range.
func (r *Recording) ReadStep(idx int) (info *StepInfo, err error) {
defer essentials.AddCtxTo("read step", &err)
if idx < 0 || idx >= r.NumSteps() {
err = errors.New("index out of range")
return
}
step := r.info.Steps[idx]
info = &StepInfo{
Time: step.Time,
Reward: step.Reward,
Done: step.Done,
}
for _, evt := range step.Events {
if evt.KeyEvent != nil {
info.Events = append(info.Events, evt.KeyEvent)
} else if evt.MouseEvent != nil {
info.Events = append(info.Events, evt.MouseEvent)
}
}
return
}
func (r *Recording) writeInfo() error {
data, err := json.Marshal(r.info)
if err != nil {
return err
}
return ioutil.WriteFile(r.infoPath(), data, recordingPerm)
}
func (r *Recording) infoPath() string {
return filepath.Join(r.dir, "info.json")
}
func (r *Recording) framesPath() string {
return filepath.Join(r.dir, "frames")
}
type recordEnv struct {
Env
destDir string
curRec *Recording
gen *rand.Rand
}
// RecordEnv creates an Env wrapper which stores a new
// Recording for every episode.
//
// Recordings are stored inside the given directory.
// Each recording is assigned a pseudo-random directory
// name to prevent collisions.
// However, the names all begin with "recording_".
//
// Closing the resulting Env will automatically close e.
func RecordEnv(e Env, dir string) Env {
return &recordEnv{
Env: e,
destDir: dir,
gen: rand.New(rand.NewSource(rand.Int63() ^ time.Now().UnixNano())),
}
}
func (r *recordEnv) Reset() (err error) {
defer essentials.AddCtxTo("reset recorded environment", &err)
if err = r.createDir(); err != nil {
return
}
name := fmt.Sprintf("recording_%d_%d", time.Now().UnixNano(), r.gen.Int63())
rec, err := CreateRecording(filepath.Join(r.destDir, name))
if err != nil {
return
}
r.curRec = rec
return r.Env.Reset()
}
func (r *recordEnv) Observe() (obs Obs, err error) {
defer essentials.AddCtxTo("observe recorded environment", &err)
obs, err = r.Env.Observe()
if err != nil {
return
}
err = r.curRec.WriteObs(obs)
return
}
func (r *recordEnv) Step(t time.Duration, events ...interface{}) (reward float64,
done bool, err error) {
defer essentials.AddCtxTo("step recorded environment", &err)
reward, done, err = r.Env.Step(t, events...)
if err != nil {
return
}
err = r.curRec.WriteStep(&StepInfo{
Time: t,
Events: events,
Reward: reward,
Done: done,
})
return
}
func (r *recordEnv) createDir() error {
info, err := os.Stat(r.destDir)
if os.IsNotExist(err) {
return os.Mkdir(r.destDir, recordingPerm)
} else if !info.IsDir() {
return errors.New("record to " + r.destDir + ": not a directory")
}
return nil
}
// StepInfo stores information about a step in an
// environment and the result of that step.
type StepInfo struct {
Time time.Duration
Events []interface{}
Reward float64
Done bool
}
type recordingInfo struct {
NumObs int
Steps []recordingStep
}
type recordingStep struct {
Time time.Duration
Reward float64
Done bool
Events []recordingEvent
}
type recordingEvent struct {
KeyEvent *chrome.KeyEvent
MouseEvent *chrome.MouseEvent
}