-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathruntime.py
569 lines (521 loc) · 19.8 KB
/
runtime.py
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
# Copyright 2017 TWO SIGMA OPEN SOURCE, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
if sys.version_info < (3,0):
raise RuntimeError('Python2 was found when trying to start Python3. _beaker_python_mismatch_')
import os, json, pandas, numpy
import urllib.request, urllib.parse, urllib.error, urllib.request, urllib.error, urllib.parse, IPython, datetime, calendar, math, traceback, time
from traitlets import Unicode
from beaker.plot import BaseObject, chart
from ipykernel.comm import Comm
class OutputContainer:
def __init__(self):
self.items = []
def clear(self):
self.items = [ ]
def addItem(self, obj):
self.items.append(obj)
def getItems(self):
return self.items
class BeakerCodeCell:
def __init__(self, cellId, evaluatorId):
self.cellId = cellId
self.evaluatorId = evaluatorId
self.code = ''
self.outputtype = ''
self.output = None
self.tags = ''
def getCellId(self):
return self.cellId
def getEvaluatorId(self):
return self.evaluatorId
def getCode(self):
return self.code
def getOutputType(self):
return self.outputtype
def getOutput(self):
return self.output
def getTags(self):
return self.tags
def convertTypeName(typ):
if typ.startswith("float"):
return "double"
if typ.startswith("int") or typ.startswith("uint") or typ.startswith("short") or typ.startswith("ushort") or typ.startswith("long") or typ.startswith("ulong"):
return "integer"
if typ.startswith("bool"):
return "boolean"
if typ.startswith("date") or typ.startswith("Time"):
return "datetime"
return "string"
def isPrimitiveType(typ):
if typ.startswith("float"):
return True
if typ.startswith("int") or typ.startswith("uint") or typ.startswith("short") or typ.startswith("ushort") or typ.startswith("long") or typ.startswith("ulong"):
return True
if typ.startswith("bool"):
return True
if typ.startswith("date") or typ.startswith("Time"):
return True
if typ.startswith("str"):
return True
return False
def isListOfMaps(data):
if type(data) != list:
return False
for w in data:
if type(w) != dict:
return False
for v in w.values():
if not isPrimitiveType(type(v).__name__):
return False
return True
def isDictionary(data):
if type(data) != dict:
return False
for v in data.values():
if not isPrimitiveType(type(v).__name__):
return False
return True
def transformNaN(obj):
if not isinstance(obj, float):
return obj
if math.isnan(obj):
return "Nan";
if math.isinf(obj):
if obj>0:
return "Infinity"
else:
return "-Infinity"
return obj
def transformNaNs(obj):
for x in range(0,len(obj)):
i = obj[x];
if not isinstance(i, float):
continue
if math.isnan(i):
obj[x] = "NaN";
if math.isinf(i):
if i>0:
obj[x] = "Infinity"
else:
obj[x] = "-Infinity"
def fixNaNBack(obj):
if not isinstance(obj, str):
return obj
if obj == "NaN":
return float('nan')
if obj == "Infinity":
return float('inf')
if obj == "-Infinity":
return float('-inf')
return obj
def fixNaNsBack(obj):
for x in range(0,len(obj)):
i = obj[x];
if not isinstance(i, str):
continue
if i == "NaN":
obj[x] = float('nan')
if i == "Infinity":
obj[x] = float('inf')
if i == "-Infinity":
obj[x] = float('-inf')
def transform(obj):
if type(obj) == bytes:
return str(obj)
if isListOfMaps(obj):
out = {}
out['type'] = "TableDisplay"
out['subtype'] = "ListOfMaps"
cols = []
for l in obj:
cols.extend(l.keys())
cols = list(set(cols))
out['columnNames'] = cols
vals = []
for l in obj:
row = []
for r in cols:
if r in l:
row.append(transform(l[r]))
else:
row.append('')
vals.append(row)
out['values'] = vals
return out
if isDictionary(obj):
out = {}
out['type'] = "TableDisplay"
out['subtype'] = "Dictionary"
out['columnNames'] = [ "Key", "Value" ]
values = []
for k,v in obj.items():
values.append( [k, transform(v)] )
out['values'] = values
return out
if type(obj) == dict:
out = {}
for k,v in obj.items():
out[k] = transformNR(v)
return out
if type(obj) == list:
out = []
for v in obj:
out.append(transformNR(v))
return out
if isinstance(obj, OutputContainer):
out = {}
out['type'] = "OutputContainer"
items = []
for v in obj.getItems():
items.append(transform(v))
out['items'] = items
return out
if isinstance(obj, BeakerCodeCell):
out = {}
out['type'] = "BeakerCodeCell"
out['cellId'] = obj.getCellId()
out['evaluatorId'] = obj.getEvaluatorId()
out['code'] = obj.getCode()
out['outputtype'] = obj.getOutputType()
out['output'] = transformNR(obj.getOutput())
out['tags'] = obj.getTags()
return out
if isinstance(obj, BaseObject):
return obj.transform()
return transformNaN(obj)
def transformNR(obj):
if type(obj) == bytes:
return str(obj)
if type(obj) == dict:
out = {}
for k,v in obj.items():
out[k] = transformNR(v)
return out
if type(obj) == list:
out = []
for v in obj:
out.append(transformNR(v))
return out
if isinstance(obj, OutputContainer):
out = {}
out['type'] = "OutputContainer"
items = []
for v in obj.getItems():
items.append(transform(v))
out['items'] = items
return out
if isinstance(obj, BeakerCodeCell):
out = {}
out['type'] = "BeakerCodeCell"
out['cellId'] = obj.getCellId()
out['evaluatorId'] = obj.getEvaluatorId()
out['code'] = obj.getCode()
out['outputtype'] = obj.getOutputType()
out['output'] = transformNR(obj.getOutput())
out['tags'] = obj.getTags()
return out
if isinstance(obj, BaseObject):
return obj.transform()
return transformNaN(obj)
def transformBack(obj):
if type(obj) == dict:
out = {}
for k,v in obj.items():
out[str(k)] = transformBack(v)
if "type" in out:
if out['type'] == "Plot" \
or out['type'] == "TimePlot" \
or out['type'] == "NanoPlot" \
or out['type'] == "SimpleTimePlot" \
or out['type'] == "CombinedPlot":
return chart.transformBack(out)
if out['type'] == "BeakerCodeCell":
c = BeakerCodeCell(out['cellId'], out['evaluatorId'])
if 'code' in out:
c.code = out['code']
if 'outputtype' in out:
c.outputtype = out['outputtype']
if 'output' in out:
c.output = transformBack(out['output'])
if 'tags' in out:
c.tags = out['tags']
return c
if out['type'] == "OutputContainer":
c = OutputContainer()
if 'items' in out:
for i in out['items']:
c.addItem(i)
return c
if out['type'] == "Date":
return datetime.datetime.fromtimestamp(out["timestamp"]/1000)
if out['type'] == "TableDisplay":
if 'subtype' in out:
if out['subtype'] == "Dictionary":
out2 = { }
for r in out['values']:
out2[r[0]] = fixNaNBack(r[1])
if out['columnNames'][0] == "Index":
return pandas.Series(out2)
return out2
if out['subtype'] == "Matrix":
vals = out['values']
fixNaNsBack(vals)
return numpy.matrix(vals)
if out['subtype'] == "ListOfMaps":
out2 = []
cnames = out['columnNames']
for r in out['values']:
out3 = { }
for i in range(len(cnames)):
if r[i] != '':
out3[ cnames[i] ] = r[i]
out2.append(out3)
return out2
# transform to dataframe
if ('hasIndex' in out) and (out['hasIndex'] == "true"):
# first column becomes the index
vals = out['values']
cnames = out['columnNames'][1:]
index = []
for x in range(0,len(vals)):
index.append(transformBack(vals[x][0]))
v = vals[x][1:]
fixNaNsBack(v)
vals[x] = v
return pandas.DataFrame(data=vals, columns=cnames, index=index)
else:
vals = out['values']
cnames = out['columnNames']
for x in range(0,len(vals)):
v = vals[x]
fixNaNsBack(v)
vals[x] = v
return pandas.DataFrame(data=vals, columns=cnames)
return out
if type(obj) == list:
out = []
for v in obj:
out.append(transformBack(v))
return out
try:
if type(obj) == bytes:
obj = str(obj)
except Exception as e:
return obj
return obj
# should be inner class to Beaker
class DataFrameEncoder(json.JSONEncoder):
def default(self, obj):
# similarly handle Panels.
# make this extensible by the user to handle their own types.
if isinstance(obj, numpy.generic):
return transformNaN(obj.item())
if isinstance(obj, numpy.ndarray) and obj.ndim == 2:
out = {}
out['type'] = "TableDisplay"
out['subtype'] = "Matrix"
cols = [ ]
for i in range(obj.shape[1]):
cols.append( "c" + str(i) )
out['columnNames'] =cols
vars = obj.tolist()
for x in range(0,len(vars)):
transformNaNs(vars[x])
out['values'] = vars
return out
if isinstance(obj, numpy.ndarray):
ret = obj.tolist()
transformNaNs(ret)
return ret
if type(obj) == datetime.datetime or type(obj) == datetime.date or type(obj).__name__ == 'Timestamp':
out = {}
out['type'] = "Date"
out['timestamp'] = time.mktime(obj.timetuple()) * 1000
return out
if type(obj) == pandas.core.frame.DataFrame:
out = {}
out['type'] = "TableDisplay"
out['subtype'] = "TableDisplay"
out['hasIndex'] = "true"
out['columnNames'] = ['Index'] + obj.columns.tolist()
vals = obj.values.tolist()
idx = obj.index.tolist()
for x in range(0,len(vals)):
vals[x] = [ idx[x] ] + vals[x]
ty = []
num = len(obj.columns.tolist())
x = 0;
for x in range(0,num+1):
ty.append( convertTypeName(type(vals[0][x]).__name__))
out['types'] = ty
for x in range(0,len(vals)):
transformNaNs(vals[x])
out['values'] = vals
return out
if type(obj) == pandas.core.series.Series:
basict = True
for i in range(len(obj)):
if not isPrimitiveType(type(obj[i]).__name__):
basict = False
break
if basict:
out = {}
out['type'] = "TableDisplay"
out['subtype'] = "Dictionary"
out['columnNames'] = [ "Index", "Value" ]
values = []
for k,v in obj.items():
values.append( [k, transform(v)] )
out['values'] = values
return out
return obj.to_dict()
if type(obj).__name__ == 'Timedelta' or type(obj).__name__ == 'TimedeltaIndex':
return
return json.JSONEncoder.default(self, obj)
class MyJSONFormatter(IPython.core.formatters.BaseFormatter):
format_type = Unicode('application/json')
def __call__(self, obj):
try:
obj = transform(obj)
return json.dumps(obj, cls=DataFrameEncoder)
except Exception as e:
#print(e)
#traceback.print_exc()
return None
class Beaker:
"""Runtime support for Python code in Beaker."""
_comm = Comm(target_name='beaker.autotranslation')
def set4(self, var, val, unset, sync):
args = {'name': var, 'sync':sync}
if not unset:
val = transform(val)
args['value'] = json.dumps(val, cls=DataFrameEncoder)
self._comm.send(data=args)
def get(self, var):
result = self._getRaw(var)
if not result['defined']:
raise NameError('name \'' + var + '\' is not defined in notebook namespace')
return transformBack(result['value'])
def _getRaw(self, var):
args = {'name': var}
self._comm.send(data=args)
# need to block until we get a callback but the server is single threaded?
# result = self._comm.read()
return result
def set_session(self, id):
self.session_id = id
def register_output(self):
ip = IPython.InteractiveShell.instance()
ip.display_formatter.formatters['application/json'] = MyJSONFormatter(parent=ip.display_formatter)
def set(self, var, val):
return self.set4(var, val, False, True)
def unset(self, var):
return self.set4(var, None, True, True)
def isDefined(self, var):
return self._getRaw(var)['defined']
def createOutputContainer(self):
return OutputContainer()
def showProgressUpdate(self):
return "WARNING: python3 language plugin does not support progress updates"
def evaluate(self,filter):
args = {'filter': filter, 'session':self.session_id}
req = urllib.request.Request(self.core_url + '/rest/notebookctrl/evaluate',
urllib.parse.urlencode(args).encode('utf8'))
conn = self._beaker_url_opener.open(req)
result = json.loads(conn.read().decode())
return transformBack(result)
def evaluateCode(self, evaluator,code):
args = {'evaluator': evaluator, 'code' : code, 'session':self.session_id}
req = urllib.request.Request(self.core_url + '/rest/notebookctrl/evaluateCode',
urllib.parse.urlencode(args).encode('utf8'))
conn = self._beaker_url_opener.open(req)
result = json.loads(conn.read().decode())
return transformBack(result)
def showStatus(self,msg):
args = {'msg': msg, 'session':self.session_id}
req = urllib.request.Request(self.core_url + '/rest/notebookctrl/showStatus',
urllib.parse.urlencode(args).encode('utf8'))
conn = self._beaker_url_opener.open(req)
result = conn.read()
return result=="true"
def clearStatus(self,msg):
args = {'msg': msg, 'session':self.session_id}
req = urllib.request.Request(self.core_url + '/rest/notebookctrl/clearStatus',
urllib.parse.urlencode(args).encode('utf8'))
conn = self._beaker_url_opener.open(req)
result = conn.read()
return result=="true"
def showTransientStatus(self,msg):
args = {'msg': msg, 'session':self.session_id}
req = urllib.request.Request(self.core_url + '/rest/notebookctrl/showTransientStatus',
urllib.parse.urlencode(args).encode('utf8'))
conn = self._beaker_url_opener.open(req)
result = conn.read()
return result=="true"
def getEvaluators(self):
req = urllib.request.Request(self.core_url + '/rest/notebookctrl/getEvaluators?' +
urllib.parse.urlencode({
'session':self.session_id}))
conn = self._beaker_url_opener.open(req)
result = json.loads(conn.read().decode())
return transformBack(result)
def getVersion(self):
req = urllib.request.Request(self.core_url + '/rest/util/version?' + urllib.parse.urlencode({'session':self.session_id}))
conn = self._beaker_url_opener.open(req)
return transformBack(conn.read().decode())
def getVersionNumber(self):
req = urllib.request.Request(self.core_url + '/rest/util/getVersionInfo?' + urllib.parse.urlencode({'session':self.session_id}))
conn = self._beaker_url_opener.open(req)
result = json.loads(conn.read().decode())
return transformBack(result['version'])
def getCodeCells(self,filter):
req = urllib.request.Request(self.core_url + '/rest/notebookctrl/getCodeCells?' +
urllib.parse.urlencode({
'filter':filter}))
conn = self._beaker_url_opener.open(req)
result = json.loads(conn.read().decode())
return transformBack(result)
def setCodeCellBody(self,name,body):
args = {'name': name, 'body':body, 'session':self.session_id}
req = urllib.request.Request(self.core_url + '/rest/notebookctrl/setCodeCellBody',
urllib.parse.urlencode(args).encode('utf8'))
conn = self._beaker_url_opener.open(req)
result = conn.read()
return result=="true"
def setCodeCellEvaluator(self,name,evaluator):
args = {'name': name, 'evaluator':evaluator, 'session':self.session_id}
req = urllib.request.Request(self.core_url + '/rest/notebookctrl/setCodeCellEvaluator',
urllib.parse.urlencode(args).encode('utf8'))
conn = self._beaker_url_opener.open(req)
result = conn.read()
return result=="true"
def setCodeCellTags(self,name,tags):
args = {'name': name, 'tags':tags, 'session':self.session_id}
req = urllib.request.Request(self.core_url + '/rest/notebookctrl/setCodeCellTags',
urllib.parse.urlencode(args).encode('utf8'))
conn = self._beaker_url_opener.open(req)
result = conn.read()
return result=="true"
def __setattr__(self, name, value):
if 'session_id' == name:
self.__dict__['session_id'] = value
return
return self.set(name, value)
def __getattr__(self, name):
return self.get(name)
def __contains__(self, name):
return self.isDefined(name)
def __delattr__(self, name):
return self.unset(name)