-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.py
165 lines (149 loc) · 5.07 KB
/
render.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
import numpy as np
import string, csv, traceback, re
class TableFrame():
def __init__(self):
while len(data) % len(columns) != 0:
data.append("NaN")
self.html = ""
self.csize = None
self.cols = columns
self.table = np.array(columns)
self.table = np.concatenate((self.table, data), axis=0)
self.table.resize(self.table.size/len(columns), len(columns))
try:
import pyopencl as cl
self.ctx = cl.create_some_context()
self.queue = cl.CommandQueue(self.ctx)
self.mf = cl.mem_flags
self.ocl = True
except ImportError:
self.ocl = False
def build(self):
tb = "<style>text-align: center</style><table border=1 style='width:100%'>"
if self.table.shape[0] < 1000:
for row in self.table:
tb += "<tr>"
for col in row:
if col != "":
tb += "<td>%s</td>" % col
tb += "</tr>"
else:
for row in self.table[0:14]:
tb += "<tr>"
for col in row:
if col != "":
tb += "<td>%s</td>" % col
tb += "</tr>"
tb += "</table><h3>...</h3>"
tb += "<table border=1 style='width:100%'>"
for row in self.table[self.table.shape[0] - 14: self.table.shape[0]]:
tb += "<tr>"
for col in row:
if col != "":
tb += "<td>%s</td>" % col
tb += "</tr>"
self.html = tb + "</table>"
def show(self):
return self.html
def append(self, data, headers):
data = np.array(data)
data.resize(data.size, len(self.cols))
self.table = np.concatenate((self.table, data), axis=0)
def refactor(self, data, name=None):
if name:
data = [name] + data
while len(data) < self.table.shape[0]:
data.append('')
if len(data) > self.table.shape[0]:
data = data[0:self.table.shape[0]]
data = np.array([data]).reshape(len(data), 1)
try:
self.table = np.hstack((self.table, data))
return 0
except ValueError:
return -1
def export(self):
io = open("static/export.csv", "w")
writer = csv.writer(io)
writer.writerows(self.table)
io.close()
def getcol(self, name, dtype=None):
size = self.csize
try:
index = list(self.table[0]).index(name)
except ValueError:
return -1
col = []
if size:
for row in self.table[1:size+1]:
col.append(row[index])
else:
for row in self.table[1:self.table.shape[0]]:
col.append(row[index])
if dtype:
arr = np.array(col)
return arr.astype(dtype)
return col
def delcol(self, name):
try:
index = np.where(self.table == name)[1][0]
self.table = np.delete(self.table, index, 1)
return 0
except:
return -1
def calc(self, cols, expr, size=None):
global OPENCL_SUPPORT
size = self.table.shape[0]
try:
lmb = getlambda(expr, cols)
vector = [np.array(self.getcol(c, dtype=np.float)) for c in cols]
self.csize = None
if self.ocl:
kernel = getkernel(expr, args)
return self.compute_opencl(kernel, *vector)
else:
return list(map(lmb, *vector))
except Exception:
self.csize = None
traceback.print_exc()
return -1
def compute_opencl(self, kernel, *args):
prg = cl.Program(self.ctx, kernel).build()
mf = cl.mem_flags
buffers = []
for arg in args:
buffers.append(cl.Buffer(self.ctx, mf.READ_ONLY, hostbuf=arg))
buffers.append(cl.Buffer(self.ctx, mf.WRITE_ONLY, args[0].nbytes))
prg.run(self.queue, args[0].size(), None, *buffers)
result = np.empty_like(args[0])
cl.enqueue_copy(self.queue, result, buffers[-1])
return result
def opencsv(fl):
fl = open(fl, "r")
data = []
headers = []
for hd in fl.readline().split(","):
headers.append(hd.strip())
for line in fl:
for i in line.split(","):
data.append(i.strip())
fl.close()
return data, headers
def getlambda(code, args):
blacklist = ['eval', 'exec','import']
lmb = "lambda "
for word in blacklist:
code = code.replace(word, "")
for i in args:
lmb += i+","
lmb = lmb.rstrip(",") + ": "
lmb += code
return eval(lmb)
def getkernel(code, args):
kernel = "__kernel void run(float *" + ', float *'.join(args) + ", float *result){"
kernel += chr(10) + "int tid = get_global_id(0);"
for a in args:
code = re.sub(a, a + "[tid]", code)
kernel += chr(10) + "result[tid] = " + code + ";"
kernel += chr(10) + "}"
return kernel