-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpgpyml_try--0.3.1.sql
More file actions
319 lines (248 loc) · 9.1 KB
/
pgpyml_try--0.3.1.sql
File metadata and controls
319 lines (248 loc) · 9.1 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
/**
* Create a new schema to be used by the extension.
* By default the extension is not installed in this schema
*/
CREATE SCHEMA IF NOT EXISTS pgpyml;
/**
* Read the model from the disk to the memory and return it as an byte array.
* The model will be stored on the special GD dictionary.
* The model path will be prefixed with 'model_' and will be used as key to access to model in the GD dictionary.
*/
CREATE OR REPLACE FUNCTION pgpyml.load_model(model_path text) RETURNS BYTEA AS
$$
import pickle
import io
from joblib import load
model_key = 'model_' + model_path
if not model_path.endswith('.joblib'):
plpy.error('The model_path must be a joblib file')
return None
clf = load(model_path)
GD[model_key] = clf
model_buffer = io.BytesIO()
pickle.dump(clf, model_buffer)
return model_buffer.getbuffer()
$$ LANGUAGE plpython3u;
/**
* Get the models loaded in the memory
*/
CREATE OR REPLACE FUNCTION pgpyml.get_loaded_models() RETURNS
TABLE (
model_key TEXT,
model_path TEXT,
model BYTEA
) AS
$$
import pickle
import io
models = []
for k in GD:
if k.startswith('model_'):
model_buffer = io.BytesIO()
pickle.dump(GD[k], model_buffer)
models.append({
'model_key': k,
'model_path': k[6:],
'model': model_buffer
})
return models
$$ LANGUAGE plpython3u;
/**
* Check if the model is already loaded to the memory.
*/
CREATE OR REPLACE FUNCTION pgpyml.is_model_loaded(model_path text) RETURNS BOOL AS
$$
model_key = 'model_' + model_path
if model_key in GD:
return True
return False
$$ LANGUAGE plpython3u;
/**
* Remove the model from the memory if it is loaded.
* The first argument is the model path that was loaded to the memory
*/
CREATE OR REPLACE FUNCTION pgpyml.unload_model(model_path text) RETURNS VOID AS
$$
model_key = 'model_' + model_path
if not model_path.endswith('.joblib'):
plpy.error('The model_path must be a joblib file')
return None
if model_key in GD:
del GD[model_key]
$$ LANGUAGE plpython3u;
/**
* Predict the data and raise an exception if the prediction is equals the value informed.
* The prediction will not be stored
* The first argument is the model path
* The second argurment is the value to be compared. If the prediction is equals to this value it will raise an exception
* Any other argument will be used as the columns name with the features to be predicted.
*/
CREATE OR REPLACE FUNCTION trigger_abort_if_prediction_is() RETURNS trigger AS
$$
model_path = TD['args'][0]
compare_value = TD['args'][1]
features_columns_names = TD['args'][2:]
features = []
for feature_column_name in features_columns_names:
features.append(TD['new'][feature_column_name])
stmt = plpy.prepare("SELECT pgpyml.predict($1, $2)", ['text', 'real[]'])
results = plpy.execute(stmt, [model_path, [features]], 1)
prediction = results[0]['predict'][0]
if prediction == compare_value:
plpy.error('The value {} is not allowed'.format(prediction))
return 'MODIFY'
$$ LANGUAGE plpython3u;
/**
* Predict the data and raise an exception if the prediction is equals the value informed.
* The prediction will be stored
* The first argument is the model path
* The second argument is the name of the column where the prediction will be stored
* The third argurment is the value to be compared. If the prediction is equals to this value it will raise an exception
* Any other argument will be used as the columns name with the features to be predicted.
*/
CREATE OR REPLACE FUNCTION trigger_classification_or_abort_if_prediction_is() RETURNS trigger AS
$$
model_path = TD['args'][0]
target_column_name = TD['args'][1]
compare_value = TD['args'][2]
features_columns_names = TD['args'][3:]
features = []
for feature_column_name in features_columns_names:
features.append(TD['new'][feature_column_name])
stmt = plpy.prepare("SELECT pgpyml.predict($1, $2)", ['text', 'real[]'])
results = plpy.execute(stmt, [model_path, [features]], 1)
prediction = results[0]['predict'][0]
if prediction == compare_value:
plpy.error('The value {} is not allowed'.format(prediction))
TD['new'][target_column_name] = prediction
return 'MODIFY'
$$ LANGUAGE plpython3u;
/**
* This funtion use the informed model to predict a value and abort the transaction unless the predicted values is equals
* the value passed as argument. The prediction will not be stored.
* The first argument is the model path
* The second argument is the expected value. The transaction will be aborted if the value is different of this value.
* Any other argument will be used as the columns name with the features to be predicted.
*/
CREATE OR REPLACE FUNCTION trigger_abort_unless_prediction_is() RETURNS trigger AS
$$
model_path = TD['args'][0]
expected_value = TD['args'][1]
features_columns_names = TD['args'][2:]
features = []
for feature_column_name in features_columns_names:
features.append(TD['new'][feature_column_name])
stmt = plpy.prepare("SELECT pgpyml.predict($1, $2)", ['text', 'real[]'])
results = plpy.execute(stmt, [model_path, [features]], 1)
prediction = results[0]['predict'][0]
if prediction != expected_value:
plpy.error('The value {} is not allowed'.format(prediction))
return None
return 'MODIFY'
$$ LANGUAGE plpython3u;
/**
* This funtion use the informed model to predict a value and abort the transaction unless the predicted values is equals
* the value passed as argument. The prediction will be stored.
* The first argument is the model path
* The second argument is the name of the column where the prediction will be stored
* The third argurment is the value to be compared. If the prediction is equals to this value it will raise an exception
* Any other argument will be used as the columns name with the features to be predicted.
*/
CREATE OR REPLACE FUNCTION pgpyml.trigger_classification_or_abort_unless_prediction_is() RETURNS trigger AS
$$
model_path = TD['args'][0]
target_column_name = TD['args'][1]
compare_value = TD['args'][2]
features_columns_names = TD['args'][3:]
features = []
for feature_column_name in features_columns_names:
features.append(TD['new'][feature_column_name])
stmt = plpy.prepare("SELECT pgpyml.predict($1, $2)", ['text', 'real[]'])
results = plpy.execute(stmt, [model_path, [features]], 1)
prediction = results[0]['predict'][0]
if prediction != compare_value:
plpy.error('The value {} is not allowed'.format(prediction))
return None
TD['new'][target_column_name] = prediction
return 'MODIFY'
$$ LANGUAGE plpython3u;
/**
* Create a funtction to be used in triggers to classify data and save in a column
* The first parameter must be the model path
* The second parameter must be to column where the classification result will be saved
* The others arguments will be used as features columns names
*/
CREATE OR REPLACE FUNCTION pgpyml.classification_trigger() RETURNS trigger AS
$$
model_path = TD['args'][0]
target_column_name = TD['args'][1]
features_columns_names = TD['args'][2:]
features = []
for feature_column_name in features_columns_names:
features.append(TD['new'][feature_column_name])
stmt = plpy.prepare("SELECT pgpyml.predict($1, $2)", ['text', 'real[]'])
results = plpy.execute(stmt, [model_path, [features]], 1)
prediction = results[0]['predict'][0]
TD['new'][target_column_name] = prediction
return 'MODIFY'
$$ LANGUAGE plpython3u;
\echo Use "CREATE EXTENSION pgpyml" to load this file. \quit
/**
* Predict the classification of the given features values
*/
CREATE OR REPLACE FUNCTION predict(model_path text, input_values real[]) RETURNS TEXT[] AS
$$
model_key = 'model_' + model_path
if model_key in GD:
clf = GD[model_key]
else:
from joblib import load
clf = load(model_path)
GD[model_key] = clf
prediction = clf.predict(input_values)
return prediction
$$ LANGUAGE plpython3u;
/**
* Predict the classification of the given features as int values
*/
CREATE OR REPLACE FUNCTION predict_int(model_path text, input_values real[]) RETURNS INT[] AS
$$
model_key = 'model_' + model_path
if model_key in GD:
clf = GD[model_key]
else:
from joblib import load
clf = load(model_path)
GD[model_key] = clf
prediction = clf.predict(input_values)
return prediction
$$ LANGUAGE plpython3u;
/**
* Predict the classification of the given features as real values
*/
CREATE OR REPLACE FUNCTION predict_real(model_path text, input_values real[]) RETURNS REAL[] AS
$$
model_key = 'model_' + model_path
if model_key in GD:
clf = GD[model_key]
else:
from joblib import load
clf = load(model_path)
GD[model_key] = clf
prediction = clf.predict(input_values)
return prediction
$$ LANGUAGE plpython3u;
/**
* Use the model to predict data already stored on the table
*/
CREATE OR REPLACE FUNCTION predict_table_row(model_path text, table_name text, columns_name text[], id int) RETURNS TEXT AS
$$
features = ','.join([plpy.quote_ident(c_name) + '::real' for c_name in columns_name])
selected_values = plpy.execute('SELECT * FROM predict(%s, (SELECT ARRAY[[%s]] FROM %s WHERE id = %d))' % (plpy.quote_literal(model_path), features, plpy.quote_ident(table_name), id), 1)
return selected_values[0]['predict'][0]
$$ LANGUAGE plpython3u;
CREATE OR REPLACE FUNCTION pytorch_version() RETURNS TEXT AS
$$
import torch
return torch.__version__
$$ LANGUAGE plpython3u;