1

I am currently using pweave but will eventually need to switch to org mode on emacs. The biggest issue i encounter is the persistence of variables and packages imported. Here is the pweave code:

#' Import module and create a variable:
import numpy as np
from numpy import pi, sin
x = np.arange(-2*pi, 2*pi, 0.1)
print(x[0:5])

#' now we calculate sin(x)
y = sin(x)
print(y[0:5]  

#' let's graph that
import matplotlib.pyplot as plt
plt.plot(x,y)

That is no problem in python's pweave, but in org mode, it fails to keep the variable and its type and the imported modules from one block code to the next.

Here is the org mode attempt:

* Import numpy module and create a numpy array:
#+BEGIN_SRC python :exports both 
import numpy as np
from numpy import pi, sin 
x = np.arange(-2*pi, 2*pi, 0.1)
return(x[0:5])
#+END_SRC

* Then we calculate the sine of the numpy array created in previous block.
  At this stage, in pweave, both the array created and the module imported
  are already stored and available for use in the next block. And this is
  precisely where the bock code hits the road block in org mode because it
  did not cache the module nor the array for future use.
#+BEGIN_SRC python :exports both :results file 
y = sin(x)
import matplotlib.pyplot as plt
fig = plt.figure()
plt.plot(x,y)
plt.savefig('~/fig.pdf')
return '~/fig.pdf'
#+END_SRC

* finally, plotting x vs. y:
#+BEGIN_SRC python :exports both 
return(y[0:5])
#+END_SRC

It also raises the following question: do i really need to open a new block code just for the purpose of printing y? How the previous block code handle 2 returns,the file and the print of variable y?

Anyone has a solution for this, please ?

Thanks

  • 1
    Can we see the org-mode example? – Jules Nov 30 '16 at 0:52
  • 1
    Did you put :session in sec block header? – John Kitchin Nov 30 '16 at 4:08
4

Use the :session option in the source header

#+BEGIN_SRC python :session :results none
 i = 10
#+END_SRC

#+BEGIN_SRC python :session :results output
 i+1
#+END_SRC

#+RESULTS:
: 11

See also http://orgmode.org/worg/org-contrib/babel/languages/ob-doc-python.html for an introduction on python code blocks.

and http://orgmode.org/worg/org-contrib/babel/intro.html#orgheadline14 for a general introduction on org-babel.


PS: To set the option for all python blocks in your file, add these lines to the end of it.

# Local Variables:
# eval: (setq-local org-babel-default-header-args:python '((:session . "*Python*")))
# End:

and reload it (i.e. via revert-buffer). You will be asked if local variables should be loaded, answer yes.

Alternatively, put the (setq-local ...) somewhere in the file (like an elisp block) and evaluate it with C-x C-e.


PPS: Applied to the listing of the OP given in their recent edit, after adapting the code a little ...

* Import numpy module and create a numpy array:
#+BEGIN_SRC python :session :exports both 
 import numpy as npx
 from numpy import pi, sin 
 x = np.arange(-2*pi, 2*pi, 0.1)
 x[0:5]
#+END_SRC

#+RESULTS:
| -6.28318531 | -6.18318531 | -6.08318531 | -5.98318531 | -5.88318531 |

* Then we calculate the sine of the numpy array created in previous block.
  At this stage, ....
 #+BEGIN_SRC python :session :exports both :results file 
  y = sin(x)
  import matplotlib.pyplot as plt
  fig = plt.figure()
  plt.plot(x,y)
  fname="fig.pdf"
  plt.savefig(fname)
  fname
#+END_SRC

#+RESULTS:
[[file:fig.pdf]] # shows a nice sine curve
  • Thanks guys, and my apologies i should have put the code in org mode as well to provide a best illustration of the trouble. * First we import a module and create a numpy array: #+BEGIN_SRC python :exports both import numpy as np x = np.arange(1, 10) return(x) #+END_SRC * Then a calculation with the np array created in previous block. . And this is precisely where the bock code hits the road block in org mode because it did not cache the module nor the array for future use. #+BEGIN_SRC python :exports both y = x.dot(x) return(y) #+END_SRC – Cy Bu Nov 30 '16 at 15:57
  • @CyBu, 1st, you can edit your question if you feel it can be improved. 2nd, as the python repl just continues to run as an inferior process (attached to the buffer *Python*) when :session is set, it should not make a difference if the variables you want to keep are integers or complex numpy types. Do you observe a difference? -- I regularly handle ROOT trees this way. 3rd, did the solution work for you at all? – Vera Johanna Nov 30 '16 at 20:58
  • @ Pingi - thanks i posted an edit, hopefully it will help better. Unfortunately, the solution proposed did not work out. The issue is that i need to have all imported modules and variables available throughout all org mode block codes. – Cy Bu Nov 30 '16 at 22:39
  • @CyBu: Everything imported should stay imported as well when all considered code blocks use the :session option. As I said, the python repl just keeps running. What happened when you tried it out? – Vera Johanna Nov 30 '16 at 22:46
  • i see the results of the first block, then tells me that 'sin()' is undefined and i don't see the plot of the second block, and finally the 3rd block gives me that y is undefined.... So that means that numpy did not stay imported, and the x and consequently y variables vanished as well. – Cy Bu Nov 30 '16 at 23:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.