This repository was archived by the owner on May 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathcrayon.lua
More file actions
354 lines (310 loc) · 10.9 KB
/
crayon.lua
File metadata and controls
354 lines (310 loc) · 10.9 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
local requests = require("requests")
local socket = require("socket")
local json = require('cjson.safe')
local __version__ = 0.5
local CrayonClient = {}
local CrayonExperiment = {}
-- CrayonClient class
do
CrayonClient.__index = CrayonClient
local mt = {}
mt.__call = function(cc, hostname, port)
local self = {}
setmetatable(self, CrayonClient)
self.hostname = hostname or "localhost"
self.port = port or 6007
self.url = self.hostname .. ":" .. tostring(self.port)
-- Add http header if missing
function urlstartswith(pattern)
return string.sub(self.url,1,string.len(pattern))==pattern
end
if not (urlstartswith("http://") or urlstartswith("https://")) then
self.url = "http://" .. self.url
end
-- Check that the server is running and at the same version
local ok, r = pcall(requests.get, self.url)
if not ok then
msg = "The server at " .. self.hostname .. ":"
.. tostring(self.port) .. " does not appear to be up!"
error(msg)
end
if r.status_code ~= 200 then
local msg = "Something went wrong. Server sent: " .. r.text .. "."
error(msg)
end
server_version = tonumber(r.text)
if not server_version then
local msg = "The page at " .. self.hostname .. ":"
.. tostring(self.port) .. " doesn't seem to be a Crayon server!"
error(msg)
end
if server_version ~= __version__ then
local msg = "Initialised client version " .. __version__
.. ", however found server running version " .. server_version .. "."
error(msg)
end
return self
end
setmetatable(CrayonClient, mt)
function CrayonClient:get_experiment_names()
local query = "/data"
local r = requests.get(self.url..query)
if r.status_code ~= 200 then
local msg = "Something went wrong. Server sent: " .. r.text .. "."
error(msg)
end
return r.json()
end
function CrayonClient:open_experiment(xp_name)
assert(type(xp_name)=="string", "Expected a string, but got "
.. type(xp_name) .. ".")
return CrayonExperiment(xp_name, self, false)
end
function CrayonClient:create_experiment(xp_name, zip_file)
assert(type(xp_name)=="string", "Expected a string, but got "
.. type(xp_name) .. ".")
return CrayonExperiment(xp_name, self, true, zip_file)
end
function CrayonClient:remove_experiment(xp_name)
assert(type(xp_name)=="string", "Expected a string, but got "
.. type(xp_name) .. ".")
query = "/data"
local r = requests.delete{
url = self.url..query,
params = {xp = xp_name}
}
if r.status_code ~= 200 then
local msg = "Something went wrong. Server sent: " .. r.text .. "."
error(msg)
end
end
function CrayonClient:remove_all_experiments()
local xp_list = self:get_experiment_names()
for i, xp_name in pairs(xp_list) do
self:remove_experiment(xp_name)
end
end
end
-- CrayonExperiment class
do
-- Local functions
local __init_from_file, __init_empty, __init_from_existing
local __update_steps, __get_name_list
local __check_histogram_data
local json_headers = {}
json_headers["Content-Type"] = "application/json"
local zip_headers = {}
zip_headers["Content-Type"] = "application/zip"
CrayonExperiment.__index = CrayonExperiment
local mt = {}
mt.__call = function(ce, xp_name, client, create, zip_file)
local self = {}
setmetatable(self, CrayonExperiment)
self.client = client
self.xp_name = xp_name
self.scalar_steps = {}
self.hist_steps = {}
local mt = {__index = function() return 0 end}
setmetatable(self.scalar_steps, mt)
setmetatable(self.hist_steps, mt)
if zip_file then
if not create then
error("Can only create a new experiment when a zip_file is provided")
end
__init_from_file(self, zip_file, true)
elseif create then
__init_empty(self)
else
__init_from_existing(self)
end
return self
end
setmetatable(CrayonExperiment, mt)
-- Initialisation
__init_empty = function(self)
local query = "/data"
local r = requests.post{
url = self.client.url..query,
data = json.encode(self.xp_name),
headers = json_headers
}
if r.status_code ~= 200 then
local msg = "Something went wrong. Server sent: " .. r.text .. "."
error(msg)
end
end
__init_from_existing = function(self)
local query = "/data"
local r = requests.get{
url = self.client.url..query,
params = {xp = self.xp_name}
}
if r.status_code ~= 200 then
local msg = "Something went wrong. Server sent: " .. r.text .. "."
error(msg)
end
local content = r.json()
__update_steps(content["scalars"], self.scalar_steps, self.get_scalar_values)
__update_steps(content["histograms"], self.hist_steps, self.get_histogram_values)
end
__init_from_file = function(self, zip_file, force)
local file = io.open(zip_file, "rb")
local content = file:read("*all")
assert(file, "File cannot be opened. Check it exists!")
file:close()
local query = "/backup"
local r = requests.post{
url = self.client.url..query,
params = {xp=self.xp_name, force=force},
data = content,
headers = zip_headers
}
if r.status_code ~= 200 then
local msg = "Something went wrong. Server sent: " .. r.text .. "."
error(msg)
end
end
-- Scalar methods
function CrayonExperiment:get_scalar_names()
return __get_name_list(self, "scalars")
end
function CrayonExperiment:add_scalar_value(name, value, wall_time, step)
if not wall_time then
wall_time = socket.gettime()
end
if not step then
step = self.scalar_steps[name]
end
self.scalar_steps[name] = step + 1
local query = "/data/scalars"
local r = requests.post{
url = self.client.url..query,
params = {xp=self.xp_name, name=name},
data = {wall_time, step, value},
headers = json_headers
}
if r.status_code ~= 200 then
local msg = "Something went wrong. Server sent: " .. r.text .. "."
error(msg)
end
end
function CrayonExperiment:add_scalar_dict(data, wall_time, step)
for name, value in pairs(data) do
assert(type(name)=="string", "Expected a string, but got "
.. type(name) .. ".")
self.add_scalar_value(name, value, wall_time, step)
end
end
function CrayonExperiment:get_scalar_values(name)
query = "/data/scalars"
local r = requests.get{
url = self.client.url..query,
params = {xp=self.xp_name, name=name}
}
if r.status_code ~= 200 then
local msg = "Something went wrong. Server sent: " .. r.text .. "."
error(msg)
end
return r.json()
end
-- Histogram methods
function CrayonExperiment:get_histogram_names()
return __get_name_list(self, "histograms")
end
function CrayonExperiment:add_histogram_value(name, hist, tobuild, wall_time, step)
if not wall_time then
wall_time = socket.gettime()
end
if not step then
step = self.hist_steps[name]
end
self.hist_steps[name] = step + 1
__check_histogram_data(hist, tobuild)
local query = "/data/histograms"
local r = requests.post{
url = self.client.url..query,
params = {xp=self.xp_name, name=name},
data = {wall_time, step, hist},
headers = json_headers
}
if r.status_code ~= 200 then
local msg = "Something went wrong. Server sent: " .. r.text .. "."
error(msg)
end
end
function CrayonExperiment:get_histogram_values(name)
query = "/data/histograms"
local r = requests.get{
url = self.client.url..query,
params = {xp=self.xp_name, name=name}
}
if r.status_code ~= 200 then
local msg = "Something went wrong. Server sent: " .. r.text .. "."
error(msg)
end
return r.json()
end
__check_histogram_data = function(data, tobuild)
local required = {bucket=1, bucket_limit=1, max=1, min=1, num=1}
local optionnal = {sum=1, sum_squares=1}
if tobuild then
if #data == 0 then
error("When building the histogram should be provided as a list in a table.")
end
else
for key,_ in pairs(required) do
if not data[key] then
error("Built histogram is missing argument: " .. key)
end
end
for key,_ in pairs(data) do
if (not required[key]) and (not optionnal[key]) then
error("Built histogram has extra parameter: " .. key)
end
end
end
end
-- Backup methods
function CrayonExperiment:to_zip(filename)
filename = filename or "backup_" .. self.xp_name .. "_" .. tostring(socket.gettime()) .. ".zip"
local query = "/backup"
local r = requests.get{
url = self.client.url..query,
params = {xp=self.xp_name}
}
if r.status_code ~= 200 then
local msg = "Something went wrong. Server sent: " .. r.text .. "."
error(msg)
end
local f = io.open(filename, "w")
f:write(r.text)
f:close()
return filename
end
-- helper methods
__get_name_list = function(self, element_type)
local query = "/data"
local r = requests.get{
url = self.client.url..query,
params = {xp=self.xp_name}
}
if r.status_code ~= 200 then
local msg = "Something went wrong. Server sent: " .. r.text .. "."
error(msg)
end
return r.json()[element_type]
end
__update_steps = function(self, elements, steps_table, eval_function)
for _, element in pairs(elements) do
print("element", element)
values = eval_function(element)
print("values", values)
if #values > 0 then
steps_table[element] = values[#values][1] + 1
end
end
end
end
return {
CrayonClient= CrayonClient
}