From mboxrd@z Thu Jan 1 00:00:00 1970 From: William Henney Subject: Re: Problem with python session Date: Thu, 6 Oct 2016 12:54:49 -0500 Message-ID: References: Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=001a114e63c2760bd6053e35fd21 Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:58552) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bsCsf-00023e-B4 for emacs-orgmode@gnu.org; Thu, 06 Oct 2016 13:54:55 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bsCsd-00070l-Rb for emacs-orgmode@gnu.org; Thu, 06 Oct 2016 13:54:53 -0400 Received: from mail-vk0-x22c.google.com ([2607:f8b0:400c:c05::22c]:33992) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bsCsd-00070Z-Hg for emacs-orgmode@gnu.org; Thu, 06 Oct 2016 13:54:51 -0400 Received: by mail-vk0-x22c.google.com with SMTP id b186so25028090vkb.1 for ; Thu, 06 Oct 2016 10:54:50 -0700 (PDT) In-Reply-To: List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: Florian Lindner Cc: emacs-org --001a114e63c2760bd6053e35fd21 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hi Florian I can reproduce your problem. This is (arguably) a bug in ob-python when using the vanilla python interpreter together with the :session argument. You can work around it by putting a blank line after the for-loop in your second code block. I say that it is arguable that this is a bug or not since you would have exactly the same error if you were to literally type your code block in at the python interactive prompt. That is, you have to give a second newline in order to close the loop and return to the top-level prompt. However, it is admittedly confusing to have different behavior with and without the ":session" argument. I had never come across this bug myself, since I use ob-ipython for interactive python sessions (https://github.com/gregsexton/ob-ipython) Here is a minimal example that shows the problem. Cheers Will * Test of ob-python in session mode with vanilla python interpreter ** FAILS: Without blank line after indented loop #+BEGIN_SRC python :session *ob-python session* for x in 1, 2: pass x #+END_SRC #+RESULTS: An error message appears in the =3D*ob-python session*=3D buffer, which can= be visited via =3DC-c C-v C-z=3D with point inside the code block. #+BEGIN_EXAMPLE >>> 'org_babel_python_eoe' >>> 'org_babel_python_eoe' >>> for x in 1, 2: ... pass ... x File "", line 3 x ^ SyntaxError: invalid syntax #+END_EXAMPLE ** SUCCEEDS: With blank line after indented loop #+BEGIN_SRC python :session *ob-python session* for x in 1, 2: pass x #+END_SRC #+RESULTS: : 2 ** SUCCEEDS: Without using a session #+BEGIN_SRC python :return x for x in 1, 2: pass x #+END_SRC #+RESULTS: : 2 ** SUCCEEDS: Using ob-ipython instead of ob-python #+BEGIN_SRC ipython :session for x in 1, 2: pass x #+END_SRC #+RESULTS: : 2 On Thu, Oct 6, 2016 at 7:41 AM, Florian Lindner wrote= : > Hello, > > I have an org file: > > * Overview of available basis functions > #+BEGIN_SRC python :session generateBFpics :exports results :results file > import matplotlib.pyplot as plt > import numpy as np > > def set_plotoptions(): > plt.xlabel("x") > plt.ylabel("$\phi(x)$") > plt.grid() > > > np.seterr(invalid=3D'ignore') > > x =3D np.linspace(-3, 3, 1000) > > plt.plot(x, np.log(abs(x))*np.power(x, 2)) > plt.suptitle("Thin Plate Splines") > plt.title("$\phi(|x|) =3D \log(x) \cdot x^2$") > set_plotoptions() > plt.savefig("bf-tps.pdf") > plt.close() > "bf-tps.pdf" > #+END_SRC > > #+RESULTS: > [[file:bf-tps.pdf]] > > #+BEGIN_SRC python :session generateBFpics :exports results :results file > for shape in [1, 2, 3, 4]: > plt.plot(x, np.power(shape, 2) + np.power(x,2), label =3D "s =3D %i= " % > shape) > plt.suptitle("Multi Quadrics") > plt.title("$\phi(|x|) =3D s^2 + x^2$") > plt.legend() > set_plotoptions() > plt.savefig("bf-multiquadrics.pdf") > plt.close() > "bf-multiquadrics.pdf" > #+END_SRC > > #+RESULTS: > [[file:bf-multiquadrics.pdf]] > > > > Both PDFs are generated. But only the first one has the content I expect, > the othe one is an empty plot (it's a plot, > yes, but empty axes. > > When I copy these pieces of code into on .py file it works just great. To > my understanding that just how session mode works. > > What could be the problem here? > > Thanks, > Florian > > > --=20 Dr William Henney, Instituto de Radioastronom=C3=ADa y Astrof=C3=ADsica, Universidad Nacional Aut=C3=B3noma de M=C3=A9xico, Campus Morelia --001a114e63c2760bd6053e35fd21 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Hi Florian

I can reproduce your problem= .=C2=A0 This is (arguably) a bug in ob-python when using the vanilla python= interpreter together with the :session argument.=C2=A0 You can work around= it by putting a blank line after the for-loop in your second code block. = =C2=A0

I say that it is arguable that this is a bu= g or not since you would have exactly the same error if you were to literal= ly type your code block in at the python interactive prompt.=C2=A0 That is,= you have to give a second newline in order to close the loop and return to= the top-level prompt.=C2=A0 However, it is admittedly confusing to have di= fferent behavior with and without the ":session" argument.=C2=A0<= /div>

I had never come across this bug myself, since I u= se ob-ipython for interactive python sessions (https://github.com/gregsexton/ob-ipython) =C2= =A0=C2=A0

Here is a minimal example that shows the= problem.

Cheers

Will

* Test of ob-python in session mode with vanilla= python interpreter

** FAILS: Without blank line a= fter indented loop
#+BEGIN_SRC python :session *ob-python session= *=C2=A0
for x in 1, 2:
=C2=A0 =C2=A0 pass
x
#+END_SRC

#+RESULTS:

=
An error message appears in the =3D*ob-python session*=3D buffer, whic= h can be visited via =3DC-c C-v C-z=3D with point inside the code block.=C2= =A0

#+BEGIN_EXAMPLE
>>> 'or= g_babel_python_eoe'
>>> 'org_babel_python_eoe= 9;
>>> for x in 1, 2:
... =C2=A0 =C2=A0 pass
... x
=C2=A0 File "<stdin>", line 3
=C2=A0 =C2=A0 x
=C2=A0 =C2=A0 ^
SyntaxError: inva= lid syntax
#+END_EXAMPLE

** SUCCEEDS: Wi= th blank line after indented loop
#+BEGIN_SRC python :session *ob= -python session*=C2=A0
for x in 1, 2:
=C2=A0 =C2=A0 pas= s

x
#+END_SRC

#= +RESULTS:
: 2

** SUCCEEDS: Without using= a session=C2=A0
#+BEGIN_SRC python :return x
for x in = 1, 2:
=C2=A0 =C2=A0 pass
x
#+END_SRC

#+RESULTS:
: 2

** SUCCE= EDS: Using ob-ipython instead of ob-python
#+BEGIN_SRC ipython :s= ession
for x in 1, 2:
=C2=A0 =C2=A0 pass
x
#+END_SRC

#+RESULTS:
: 2


On Thu, Oct 6, 2016 at 7:41 AM, Florian Lindner &= lt;mailinglists@xg= m.de> wrote:
Hello,

I have an org file:

* Overview of available basis functions
#+BEGIN_SRC python :session generateBFpics :exports results :results file =C2=A0 import matplotlib.pyplot as plt
=C2=A0 import numpy as np

=C2=A0 def set_plotoptions():
=C2=A0 =C2=A0 =C2=A0 plt.xlabel("x")
=C2=A0 =C2=A0 =C2=A0 plt.ylabel("$\phi(x)$")
=C2=A0 =C2=A0 =C2=A0 plt.grid()


=C2=A0 np.seterr(invalid=3D'ignore')

=C2=A0 x =3D np.linspace(-3, 3, 1000)

=C2=A0 plt.plot(x,=C2=A0 np.log(abs(x))*np.power(x, 2))
=C2=A0 plt.suptitle("Thin Plate Splines")
=C2=A0 plt.title("$\phi(|x|) =3D \log(x) \cdot x^2$")
=C2=A0 set_plotoptions()
=C2=A0 plt.savefig("bf-tps.pdf")
=C2=A0 plt.close()
=C2=A0 "bf-tps.pdf"
#+END_SRC

#+RESULTS:
[[file:bf-tps.pdf]]

#+BEGIN_SRC python :session generateBFpics :exports results :results file =C2=A0 for shape in [1, 2, 3, 4]:
=C2=A0 =C2=A0 =C2=A0 plt.plot(x, np.power(shape, 2) + np.power(x,2), label = =3D "s =3D %i" % shape)
=C2=A0 plt.suptitle("Multi Quadrics")
=C2=A0 plt.title("$\phi(|x|) =3D s^2 + x^2$")
=C2=A0 plt.legend()
=C2=A0 set_plotoptions()
=C2=A0 plt.savefig("bf-multiquadrics.pdf")
=C2=A0 plt.close()
=C2=A0 "bf-multiquadrics.pdf"
#+END_SRC

#+RESULTS:
[[file:bf-multiquadrics.pdf]]



Both PDFs are generated. But only the first one has the content I expect, t= he othe one is an empty plot (it's a plot,
yes, but empty axes.

When I copy these pieces of code into on .py file it works just great. To m= y understanding that just how session mode works.

What could be the problem here?

Thanks,
Florian





--

=C2=A0 Dr William Henney, Instituto de Radioastronom=C3=ADa y Astrof= =C3=ADsica,
=C2=A0 Universidad Nacional Aut=C3=B3noma de M=C3=A9xico, Ca= mpus Morelia
--001a114e63c2760bd6053e35fd21--