emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Problem with python session
@ 2016-10-06 12:41 Florian Lindner
  2016-10-06 17:54 ` William Henney
  0 siblings, 1 reply; 6+ messages in thread
From: Florian Lindner @ 2016-10-06 12:41 UTC (permalink / raw)
  To: emacs-orgmode

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='ignore')

  x = 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|) = \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 = "s = %i" % shape)
  plt.suptitle("Multi Quadrics")
  plt.title("$\phi(|x|) = 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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Problem with python session
  2016-10-06 12:41 Problem with python session Florian Lindner
@ 2016-10-06 17:54 ` William Henney
  2016-10-09  9:30   ` Nicolas Goaziou
  0 siblings, 1 reply; 6+ messages in thread
From: William Henney @ 2016-10-06 17:54 UTC (permalink / raw)
  To: Florian Lindner; +Cc: emacs-org

[-- Attachment #1: Type: text/plain, Size: 3614 bytes --]

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 =*ob-python session*= buffer, which can be
visited via =C-c C-v C-z= with point inside the code block.

#+BEGIN_EXAMPLE
>>> 'org_babel_python_eoe'
>>> 'org_babel_python_eoe'
>>> for x in 1, 2:
...     pass
... x
  File "<stdin>", 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 <mailinglists@xgm.de> 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='ignore')
>
>   x = 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|) = \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 = "s = %i" %
> shape)
>   plt.suptitle("Multi Quadrics")
>   plt.title("$\phi(|x|) = 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
>
>
>


-- 

  Dr William Henney, Instituto de Radioastronomía y Astrofísica,
  Universidad Nacional Autónoma de México, Campus Morelia

[-- Attachment #2: Type: text/html, Size: 5183 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Problem with python session
  2016-10-06 17:54 ` William Henney
@ 2016-10-09  9:30   ` Nicolas Goaziou
  2016-10-10 18:40     ` John Kitchin
  0 siblings, 1 reply; 6+ messages in thread
From: Nicolas Goaziou @ 2016-10-09  9:30 UTC (permalink / raw)
  To: William Henney; +Cc: Florian Lindner, emacs-org

Hello,

William Henney <whenney@gmail.com> writes:

> 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.

Thank you for the analysis. Would you have a suggestion on how to
improve the situation?

Regards,

-- 
Nicolas Goaziou

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Problem with python session
  2016-10-09  9:30   ` Nicolas Goaziou
@ 2016-10-10 18:40     ` John Kitchin
  2016-10-11 15:53       ` Nicolas Goaziou
  0 siblings, 1 reply; 6+ messages in thread
From: John Kitchin @ 2016-10-10 18:40 UTC (permalink / raw)
  To: Nicolas Goaziou; +Cc: emacs-org, Florian Lindner

I am not sure it makes sense to change anything for this. There is
different behavior with scripts and the interpreter independently of
org-mode, e.g. with python -i:

>>> for i in range(3):
... 	print(i)
... i
  File "<stdin>", line 3
    i
    ^
SyntaxError: invalid syntax

Vanilla python sessions are kind of maddening. These seemingly identical blocks
are different, i.e. one works and one doesn't! Spoiler alert, they are not identical.

#+BEGIN_SRC python :results output org drawer :session
for i in range(3):
    for j in range(3):

        pass

    print(i)
#+END_SRC

#+RESULTS:
:RESULTS:

...   File "<stdin>", line 3
    
    ^
IndentationError: expected an indented block
File "<stdin>", line 1
    pass
    ^
IndentationError: unexpected indent
>>>   File "<stdin>", line 1
    print(i)
    ^
IndentationError: unexpected indent
:END:

This block which works has one space at the beginning of the blank lines.

#+BEGIN_SRC python :results output org drawer :session
for i in range(3):
    for j in range(3):
 
        pass
 
    print(i)
#+END_SRC

#+RESULTS:
:RESULTS:

... ... ... ... ... 0
1
2
:END:

This kind of error would be hard to reliably fix IMHO since it would
rely on replacing blank lines with at least a space, and adding a blank
line after indentation changes, except they can not be empty, they need
at least a space in them. It is not clear that is a good idea. Maybe a
test that replaces "\n" with "\n \n" might clear it up, but might also
add a bunch of the ... >>> characters in the output? 

This is not an issue with python scripts or ipython, however.





Nicolas Goaziou writes:

> Hello,
>
> William Henney <whenney@gmail.com> writes:
>
>> 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.
>
> Thank you for the analysis. Would you have a suggestion on how to
> improve the situation?
>
> Regards,


-- 
Professor John Kitchin
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
@johnkitchin
http://kitchingroup.cheme.cmu.edu

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Problem with python session
  2016-10-10 18:40     ` John Kitchin
@ 2016-10-11 15:53       ` Nicolas Goaziou
  2016-10-13  4:10         ` William Henney
  0 siblings, 1 reply; 6+ messages in thread
From: Nicolas Goaziou @ 2016-10-11 15:53 UTC (permalink / raw)
  To: John Kitchin; +Cc: Florian Lindner, emacs-org

Hello,

John Kitchin <jkitchin@andrew.cmu.edu> writes:

> I am not sure it makes sense to change anything for this.

Noted. Thank you for the feedback.

Regards,

-- 
Nicolas Goaziou

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Problem with python session
  2016-10-11 15:53       ` Nicolas Goaziou
@ 2016-10-13  4:10         ` William Henney
  0 siblings, 0 replies; 6+ messages in thread
From: William Henney @ 2016-10-13  4:10 UTC (permalink / raw)
  To: John Kitchin, emacs-org, Florian Lindner

[-- Attachment #1: Type: text/plain, Size: 1071 bytes --]

Yep, I agree. This is a wart in the vanilla python REPL, and org-mode can't
really do much about it.  Except maybe warn people.

As John noted, this is less of a problem if you use ipython as your REPL.
I think it is enough to just do

#+BEGIN_SRC emacs-lisp
(setq org-babel-python-command "ipython")
#+END_SRC

but I haven't tested this extensively since,  as I mentioned, I prefer to
use Greg Sexton's ob-ipython instead.  This integrates with the Jupyter
kernel by sending JSON to a web socket, rather than just pasting the source
block into a buffer, and that seems to be a more robust approach.

Will


On Tue, Oct 11, 2016 at 10:53 AM, Nicolas Goaziou <mail@nicolasgoaziou.fr>
wrote:

> Hello,
>
> John Kitchin <jkitchin@andrew.cmu.edu> writes:
>
> > I am not sure it makes sense to change anything for this.
>
> Noted. Thank you for the feedback.
>
> Regards,
>
> --
> Nicolas Goaziou
>
>


-- 

  Dr William Henney, Instituto de Radioastronomía y Astrofísica,
  Universidad Nacional Autónoma de México, Campus Morelia

[-- Attachment #2: Type: text/html, Size: 1818 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2016-10-13  4:10 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-10-06 12:41 Problem with python session Florian Lindner
2016-10-06 17:54 ` William Henney
2016-10-09  9:30   ` Nicolas Goaziou
2016-10-10 18:40     ` John Kitchin
2016-10-11 15:53       ` Nicolas Goaziou
2016-10-13  4:10         ` William Henney

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).