<o:p> </o:p>
Hi Sasha,<o:p></o:p>
<o:p> </o:p>
>>> Why are you reinventing the wheel? Doesn’t PyQ do what you need already?<o:p></o:p>
<o:p> </o:p>
I do miss that PyQ is able to execute python inside q. <o:p></o:p>
<o:p> </o:p>
cat p.k<o:p></o:p>
PYVER:“2.7”<o:p></o:p>
SOEXT:“.so\000”<o:p></o:p>
PSO:`p<o:p></o:p>
lib:“libpython”,PYVER,SOEXT<o:p></o:p>
PYTHON:“python.py”<o:p></o:p>
<o:p> </o:p>
`QVER setenv$_.Q.k<o:p></o:p>
\d .p<o:p></o:p>
@[.[
PSO]2:(i;1);
. `lib]<o:p></o:p>
e:{e0 x,“\000”;}<o:p></o:p>
\d .<o:p></o:p>
p)import sys, os<o:p></o:p>
p)from pyq import q<o:p></o:p>
p)sys.executable = str(q.PYTHON)<o:p></o:p>
p)script = str(q(“.z.f”))<o:p></o:p>
p)sys.argv = [script] + [str(a) for a in q(“.z.x”)]<o:p></o:p>
p)sys.modules[‘__main__’].__file__ = script<o:p></o:p>
p)sys.path.insert(0, os.path.dirname(script))<o:p></o:p>
p)del sys, os, script<o:p></o:p>
<o:p> </o:p>
It looks like that pyq is just using one function to execute python inside q and unfortunately I was missing get and set functions like in r/q-server to pass data seamlessly between python and q. As you might agree to be able to execute python inside q is one thing but you also need to be able to get the results back from your python calculation. <o:p></o:p>
<o:p> </o:p>
How is it possible to get results from a python calculation back in q using pyq?<o:p></o:p>
I can see that you can use sd0/sd1 from the _k module and does it help?<o:p></o:p>
<o:p> </o:p>
cat test_sd.q<o:p></o:p>
p)import os<o:p></o:p>
p)import sys<o:p></o:p>
p)from pyq import _k<o:p></o:p>
p)def f(d):<o:p></o:p>
x = os.read(d, 1)<o:p></o:p>
print(x.decode())<o:p></o:p>
sys.stdout.flush()<o:p></o:p>
_k.sd0(d)<o:p></o:p>
p)r,w = os.pipe()<o:p></o:p>
p)_k.sd1(r, None)<o:p></o:p>
p)os.write(w, b’X’)<o:p></o:p>
<o:p> </o:p>
Nevertheless the module that I have invented is able to execute the following q code:<o:p></o:p>
<o:p> </o:p>
cat plot_cv_predict.q <o:p></o:p>
n) “Plotting Cross-Validated Predictions”<o:p></o:p>
<o:p> </o:p>
/ loading required libraries from python<o:p></o:p>
n)from sklearn import datasets<o:p></o:p>
from sklearn.cross_validation import cross_val_predict<o:p></o:p>
from sklearn import linear_model<o:p></o:p>
import matplotlib.pyplot as plt<o:p></o:p>
<o:p> </o:p>
/ execute the function in python<o:p></o:p>
<o:p> </o:p>
n) lr = linear_model.LinearRegression()<o:p></o:p>
boston = datasets.load_boston()<o:p></o:p>
<o:p> </o:p>
/ get result back from python<o:p></o:p>
y:neval"boston.target"<o:p></o:p>
<o:p> </o:p>
/ # cross_val_predict returns an array of the same size as y
where each entry<o:p></o:p>
/ # is a prediction obtained by cross validated:<o:p></o:p>
<o:p> </o:p>
/ pass y back to python using backtick<o:p></o:p>
n) predicted = cross_val_predict(lr, boston.data, `y, cv=10)<o:p></o:p>
<o:p> </o:p>
n) fig, ax = plt.subplots()<o:p></o:p>
n) ax.scatter(y, predicted)<o:p></o:p>
n) ax.plot([y.min(), y.max()], [y.min(), y.max()], ‘k–’, lw=4)<o:p></o:p>
n) ax.set_xlabel(‘Measured’)<o:p></o:p>
n) ax.set_ylabel(‘Predicted’)<o:p></o:p>
n) plt.show()<o:p></o:p>
<o:p> </o:p>
qpredicted:neval “predicted”<o:p></o:p>
<o:p> </o:p>
(::)t:(a:y;b:qpredicted)<o:p></o:p>
<o:p> </o:p>
n) fig = plt.figure()<o:p></o:p>
n) `t.plot(x=‘a’,y=‘b’)<o:p></o:p>
n) plt.show()<o:p></o:p>
<o:p> </o:p>
/ a table in q can be passed as panda in python<o:p></o:p>
neval"str(type(t))"<o:p></o:p>
/ “<class ‘pandas.core.frame.DataFrame’>”<o:p></o:p>
<o:p> </o:p>
/ pandas can be retrieved as table from python<o:p></o:p>
t~neval"t"<o:p></o:p>
/ 1b<o:p></o:p>
<o:p> </o:p>
Kim<o:p></o:p>
<o:p> </o:p>
Von: personal-kdbplus@googlegroups.com [mailto:personal-kdbplus@googlegroups.com] Im Auftrag von Sasha
Gesendet: Montag, 13. Juni 2016 21:41
An: Kdb+ Personal Developers
Betreff: [personal kdb+] Re: Using python inside kdb+; anyone would like to participate for code review, writing documentation and test cases?<o:p></o:p>
<o:p> </o:p>