forked from chipkent/prototype-deephaven-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prototype-rowbased.py
64 lines (46 loc) · 2.46 KB
/
prototype-rowbased.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
# This example demonstrates a case where a user function creates tensors for each row.
# The model function is then evaluated for each row to create results for the row.
################################################################################################################################
# Everything here would be part of a DH library
################################################################################################################################
from deephaven import QueryScope
import jpy
#TODO: clearly in production code there would need to be extensive testing of inputs and outputs (e.g. no null, correct size, ...)
#TODO: ths is a static example, real time requires more work
#TODO: this is not written in an efficient way. it is written quickly to get something to look at
def ai_eval(table=None, model=None, inputs=[], outputs=[]):
print("SETUP")
n = table.size()
#TODO: not sure what the right output column types are. Maybe some sort of python object?
outs = [ jpy.array("java.lang.Object", n) for o in outputs ]
columns = [ table.getColumn(col) for col in inputs ]
print("COMPUTE NEW DATA")
#TODO: python looping is slow. should avoid or numba it
for i in range(n):
input_values = [ col.get(i) for col in columns ]
output_values = model(*input_values)
for (j,ov) in enumerate(output_values):
outs[j][i] = ov
print("POPULATE OUTPUT TABLE")
rst = table.by()
for (col,data) in zip(outputs, outs):
QueryScope.addParam("__temp", data)
rst = rst.update(f"{col} = __temp")
return rst.ungroup()
################################################################################################################################
# Everything here would be user created -- or maybe part of a DH library if it is common functionality
################################################################################################################################
import numpy as np
from deephaven.TableTools import emptyTable
def make_tensor(x):
return np.random.rand(3,2) + x
def model_func(a,b,c):
return 3*a, b+11, b + 32
t = emptyTable(10).update("X = i", "Y = sqrt(X)")
t2 = t.update("Z = make_tensor(X)")
t3 = ai_eval(table=t2, model=model_func, inputs=["X", "Y", "Z"], outputs=["A", "B", "C"])
#TODO: dropping weird column types to avoid some display bugs
meta2 = t2.getMeta()
t2 = t2.dropColumns("Z")
meta3 = t3.getMeta()
t3 = t3.dropColumns("Z", "B", "C")