-
Notifications
You must be signed in to change notification settings - Fork 2
/
candle_db
416 lines (355 loc) · 12.8 KB
/
candle_db
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#!/usr/bin/env python
# CANDLE DB
from __future__ import print_function
import sys
import argparse
import pysolr
import logging
<<<<<<< HEAD
# Change the level to logging.INFO for more verbosity, logging.DEBUG for testing
=======
# Change the level to logging.INFO for regular use, logging.DEBUG for testing
# Add file=candle_db.log to log to a file
>>>>>>> 7fb7b96fb02d909b121bb9f65f597252d5435eb8
logging.basicConfig(level=logging.WARNING)
# Currently the only way to modify these is to edit them here
# solr listens on port 8983 by default
<<<<<<< HEAD
# Return up to 1B results (default is 10):
=======
>>>>>>> 7fb7b96fb02d909b121bb9f65f597252d5435eb8
HOSTPORT = "localhost:8983"
TIMEOUT = 10
MAX_ROWS = 1000*1000*1000
# =============================================================================
# DEFAULT_VALUES: keys are the allowed cores in solr
# each key indexes a dictionary of default values for that core
# must match each core's schema.xml file; must edit if schema changes
# =============================================================================
UNK = "unknown"
# default values to be used with each allowed core
<<<<<<< HEAD
DEFAULT_VALUES = {
=======
DEFAULT_VALUES = {
>>>>>>> 7fb7b96fb02d909b121bb9f65f597252d5435eb8
"run" : dict(
run_id = None,
parameters = "",
benchmark_id = UNK,
dataset_id = UNK,
experiment_id = UNK,
start_time = None,
end_time = None,
runtime_hours = None,
status = "SUCCESS",
run_progress = None,
training_accuracy = None,
training_loss = None,
validation_accuracy = None,
validation_loss = None,
model_checkpoint_file = None,
model_description_file = None,
model_weight_file = None,
model_result_files = None
),
"experiment" : dict(
run_id = None,
experiment_id = None,
benchmark_id = UNK,
dataset_id = UNK,
experiment_title = "untitled",
description = "_blank",
optimization_package_name = UNK,
optimization_package_version = UNK,
objective_function = UNK,
search_space = UNK,
search_strategy = UNK,
max_runs = 1000000,
status = None,
start_time = None,
end_time = None,
system_description = UNK,
keys = None
)
}
<<<<<<< HEAD
# =============================================================================
# For each core, gives the set of fields which the schema defines as float
# =============================================================================
SCHEMA_FLOAT_FIELDS = {
=======
# =============================================================================
# For each core, gives the set of fields which the schema defines as float
# =============================================================================
SCHEMA_FLOAT_FIELDS = {
>>>>>>> 7fb7b96fb02d909b121bb9f65f597252d5435eb8
"run" : set(("training_accuracy", "training_loss",
"validation_accuracy", "validation_loss")) ,
"experiment" : set()
}
<<<<<<< HEAD
# =============================================================================
# For each core, gives the set of fields which the schema defines as int
# =============================================================================
SCHEMA_INT_FIELDS = {
"run" : set() ,
=======
# =============================================================================
# For each core, gives the set of fields which the schema defines as int
# =============================================================================
SCHEMA_INT_FIELDS = {
"run" : set() ,
>>>>>>> 7fb7b96fb02d909b121bb9f65f597252d5435eb8
"experiment" : set(("max_runs", ))
}
#==============================================================================
# TODO: find a better mechanism than global variables to embed into CandleDB
<<<<<<< HEAD
#==============================================================================
=======
#==============================================================================
def abort(msg):
logging.critical("candle_db: " + msg)
sys.exit(1)
>>>>>>> 7fb7b96fb02d909b121bb9f65f597252d5435eb8
def abort(msg):
logging.critical("candle_db: " + msg)
sys.exit(1)
#==============================================================================
# # Keyword arguments in Python:
#
# def f(**kwargs):
# # kwargs is a dictionary
# # ... do something useful with kwargs ...
#
# Calling f(a=1, b=2, c=3) is equivalent to:
#
# D = {"a":1, "b":2, "c":3}
# f(**D)
#
# # D can be accessed within the body of f as kwargs
#==============================================================================
class CandleDB:
"""Encapsulate communication with Solr"""
url_template = "http://%s/solr/%s"
def __init__(self, core, hostport=HOSTPORT, timeout=TIMEOUT, max_rows=MAX_ROWS):
"""Setup a Solr instance. The timeout is optional."""
<<<<<<< HEAD
if core not in DEFAULT_VALUES.keys():
abort("unknown core name: %s" % core)
self.core = core
core_url = CandleDB.url_template % (hostport, core)
logging.debug("CandleDB: {}".format(core_url))
self.max_rows = max_rows
=======
if core not in DEFAULT_VALUES.keys():
abort("unknown core name: %s", core)
self.core = core
core_url = CandleDB.url_template % (hostport, core)
logging.debug("CandleDB: {}".format(core_url))
>>>>>>> 7fb7b96fb02d909b121bb9f65f597252d5435eb8
self.solr = pysolr.Solr(core_url, timeout=timeout)
def add(self, **kwargs):
"""Add or update record in solr"""
args = DEFAULT_VALUES[self.core].copy()
kwargs = self._validate_fields(kwargs)
args.update(kwargs)
logging.debug("Arguments: {}".format(args))
self.solr.add([args])
def delete(self, query="*:*", id_=None):
"""delete records from solr, defaults to all records ("*:*")
<<<<<<< HEAD
=======
>>>>>>> 7fb7b96fb02d909b121bb9f65f597252d5435eb8
records can be specified by query, or by unique key
(solr uses id which is reserved in Python, hence id_ is used here)"""
if id_:
# TODO: probably need to enforce str or list of string...
self.solr.delete(id=id_)
elif query is not None:
self.solr.delete(q=query)
else:
logging.warning("Solr delete: must specify either id or q")
def _validate_fields(self, kwargs):
"""Enforce validity of inputs according to schema.
Invalid values will be replaced by defaults."""
update_set = set(kwargs.keys())
<<<<<<< HEAD
=======
>>>>>>> 7fb7b96fb02d909b121bb9f65f597252d5435eb8
for field in SCHEMA_FLOAT_FIELDS[self.core].intersection(update_set):
try:
kwargs[field] = float(kwargs[field])
except:
logging.debug("{} requires a float".format(field))
# remove the offending field from the update
del kwargs[field]
for field in SCHEMA_INT_FIELDS[self.core].intersection(update_set):
try:
kwargs[field] = int(kwargs[field])
except:
logging.debug("{} requires an integer".format(field))
# remove the offending field from the update
del kwargs[field]
return kwargs
def query(self, q="*.*"):
# Return up to 1B results (default is 10):
results = self.solr.search(q=q, rows=self.max_rows)
return results
def update_run(**kwargs):
assert kwargs["run_id"] is not None, "run_id is required"
assert kwargs["parameters"], "parameters is required"
# Special handling for parameters
# split parameters on "," into strings of ","-separated values
<<<<<<< HEAD
# e.g. ["N1=1", "NE=6"]
=======
# e.g. ["N1=1", "NE=6"]
>>>>>>> 7fb7b96fb02d909b121bb9f65f597252d5435eb8
# the keyword arguments dictionary will be updated with this list
kwargs["parameters"] = kwargs["parameters"].split(",")
# =============================================================================
# # enforce float values, per schema.xml for run
# float_fields = set(("training_accuracy", "training_loss",
# "validation_accuracy", "validation_loss"))
<<<<<<< HEAD
#
=======
#
>>>>>>> 7fb7b96fb02d909b121bb9f65f597252d5435eb8
# for ff in float_fields.intersection(set(kwargs.keys())):
# try:
# kwargs[ff] = float(kwargs[ff])
# except:
# msg = "{} requires a float".format(ff)
# logging.debug(msg)
# print(msg)
# # remove the offending field from the update
# del kwargs[ff]
# =============================================================================
<<<<<<< HEAD
=======
>>>>>>> 7fb7b96fb02d909b121bb9f65f597252d5435eb8
db = CandleDB(core="run")
db.add(**kwargs)
def experiment_insert(**kwargs):
assert kwargs["experiment_id"] is not None, "experiment_id is required!"
# Special handling for keys:
# Expecting a list of "K=V" which can be unpacked into a dictionary
# Values for K should be recognized by the schema or solr will object
if "keys" in kwargs.keys():
keys = kwargs["keys"]
if keys is not None:
kwargs.update(kv2dict(keys))
del kwargs["keys"]
# =============================================================================
# # according to the schema max_runs is an int
# if "max_runs" in kwargs.keys():
# try:
# kwargs["max_runs"] = int(kwargs["max_runs"])
# except:
# logging.warning("max_runs requires an int, got {}{".format(kwargs["max_runs"]))
# # revert to whatever the default value is
# del kwargs["max_runs"]
<<<<<<< HEAD
#
=======
#
>>>>>>> 7fb7b96fb02d909b121bb9f65f597252d5435eb8
# =============================================================================
db = CandleDB(core="experiment")
db.add(**kwargs)
def params2string(N1, NE):
return "N1=%i,NE=%i" % (N1, NE)
def kv2dict(L):
""" Convert list L of [ K=V... ] to dict { K:V ... } """
logging.debug("kv2dict called with:\n".format(L))
result = {}
for kv in L:
tokens = kv.split('=')
key = tokens[0]
if len(tokens) == 1:
result[key] = ""
else:
value = tokens[1]
result[key] = value
return result
def update(remainder):
if len(remainder) < 1:
abort("update: requires core name!")
core = remainder[0]
db = CandleDB(core=core)
kv = kv2dict(remainder[1:])
if core == "run":
db.add(**kv)
def query(args):
if len(args) < 1:
abort("query: requires core name!")
db = CandleDB(core=args[0])
q = "*:*" # Default
if len(args) == 2:
q = args[1]
results = db.query(q=q)
return results
def query_print(args):
results = query(args)
print("results: " + str(len(results.docs)))
for result in results:
print("----")
print_result(result)
def print_result(result):
copy = result.copy()
del copy["_version_"]
print_table(copy)
def print_table(D):
"""D is a dict"""
n = max(len(k) for k in D.keys()) # Length of longest key
for k,v in D.items(): #D.iteritems():
print("%*s = %s" % (-n, k, v))
def delete(args):
if len(args) != 1:
abort("delete: requires core name!")
db = CandleDB(core=args[0])
db.delete()
def ls(args):
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--no-count', default=False, action='store_true')
parser.add_argument("remainder", nargs="*")
ns = parser.parse_args(args)
if len(ns.remainder) < 1:
abort("ls: requires core name!")
db = ns.remainder[0]
results = query(ns.remainder)
if not ns.no_count:
print("results: " + str(len(results.docs)))
if db == "experiment":
ls_experiment(ns.remainder[1:], results)
elif db == "run":
ls_run(ns.remainder[1:], results)
def ls_experiment(args, results):
for result in results:
print(result["experiment_id"])
def ls_run(args, results):
table = {}
for result in results:
parameters = result["parameters"][0]
table[result["run_id"]] = parameters
print_table(table)
if __name__ == "__main__":
if len(sys.argv) < 2:
logging.critical("Requires subcommand arguments...")
print("Requires: subcommand arguments...")
sys.exit(1)
subcommand = sys.argv[1]
remainder = sys.argv[2:]
if subcommand == "delete":
delete(remainder)
elif subcommand == "update":
update(remainder)
elif subcommand == "query":
query_print(remainder)
elif subcommand == "ls":
ls(remainder)
else:
abort("unknown subcommand: " + subcommand)