emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Trouble with Tables and Python
@ 2014-04-15 21:18 Martin Schöön
  2014-04-15 22:46 ` Nick Dokos
  2014-04-16  1:29 ` John Kitchin
  0 siblings, 2 replies; 5+ messages in thread
From: Martin Schöön @ 2014-04-15 21:18 UTC (permalink / raw)
  To: emacs-orgmode


[-- Attachment #1.1: Type: text/plain, Size: 2055 bytes --]

This is my first post here...

I have been using org-mode as a pure TODO-tool for some time but this
winter I realised it could be used for much more and I have been
experimenting with mixing in LaTeX and Python for fun and because I find
literate programming a particularly sane idea (I am a HW engineer).

Less than I week ago I started to look into using tables for input and
output to/from Python scripts. I was inspired by something I found at
http://kitchingroup.cheme.cmu.edu/ . The enclosed example 1 file is a
pruned version of the org-file I found there. If I run the embedded Python
script using C-c C-c I don't get the table shown in the example. Instead I
get "none" on the row following "#+RESULTS:"

If I change ":results raw" to ":results output" I do get the table but
'wrapped' in "#+begin_example" and "#+end_example" as shown in  the
enclosed example 2 file. I have done some further testing/changes in that
file (adding some stuff to the top of the file and changing the formatting
of the print statements) none of which changed the extra wrapping of the
table. When I export to LaTeX -> PDF the table does not show up in the end
result.

Let's move over to the third example file and the real mystery. This all my
own code. Reading data from the first table works just fine and the same
goes for the calculations. The output table, however, is different from
that of example 2. The 'wrapper' is gone but each row is starting with ":
". Where does that come from? Why does it differ from example 2? If my
results differ from someone else's (different install/versions etc) -- OK.
But how can I get differences like this on my own? To my  un-trained eyes
the print statements of examples 2 and 3 look very similar. (But, the first
things that go blind are the eyes... probably something blatantly obvious
once pointed out to me.)

OS: #! Linux (Based on Debian stable, 64-bit)
Emacs: 23.4.1
Org-mode: 8.2.5h
Python: 2.7.3

TIA,
-- 
Martin Schöön

http://hem.bredband.net/b262106/index.html

[-- Attachment #1.2: Type: text/html, Size: 3124 bytes --]

[-- Attachment #2: table_making_example_1.org --]
[-- Type: application/octet-stream, Size: 878 bytes --]

* DONE Using data in a table in another org-file
  CLOSED: [2013-12-22 Sun 13:42]
  :PROPERTIES:
  :categories: org-mode
  :date:     2013/12/22 13:42:20
  :updated:  2014/01/16 07:30:30
  :END:

This file is an extract from Using-data-in-a-table-in-another-org-file.org Originally downloaded from [[http://kitchingroup.cheme.cmu.edu/blog/category/org-mode/4/]]

#+BEGIN_SRC python :results raw
import numpy as np

print '#+tblname: cos-data'
print '| x | cos(x)|'
print '|-'

for x in np.linspace(0, 2*np.pi, 10):
    print '|{0}|{1}|'.format(x, np.cos(x))
#+END_SRC

#+RESULTS:
#+tblname: cos-data
| x | cos(x)|
|-
|0.0|1.0|
|0.698131700798|0.766044443119|
|1.3962634016|0.173648177667|
|2.09439510239|-0.5|
|2.79252680319|-0.939692620786|
|3.49065850399|-0.939692620786|
|4.18879020479|-0.5|
|4.88692190558|0.173648177667|
|5.58505360638|0.766044443119|
|6.28318530718|1.0|


[-- Attachment #3: table_making_example_2.org --]
[-- Type: application/octet-stream, Size: 840 bytes --]

#+LATEX_CLASS: article
#+LATEX_CLASS_OPTIONS: [a4paper]
#+OPTIONS: toc:nil ^:{}
#+STARTUP: hideblocks
#+TITLE: Playing with Python and tables
#+AUTHOR: Martin Schöön

This file is a modified extract from Using-data-in-a-table-in-another-org-file.org Originally downloaded from [[http://kitchingroup.cheme.cmu.edu/blog/category/org-mode/4/]]

#+BEGIN_SRC python :results output
import numpy as np

print '#+tblname: cos-data'
print '| x | cos(x)|'
print '|-'

for x in np.linspace(0, 2*np.pi, 10):
    print '| %5.4f | %5.4f |' % (x,np.cos(x))
#+END_SRC

#+RESULTS:
#+begin_example
#+tblname: cos-data
| x | cos(x)|
|-
| 0.0000 | 1.0000 |
| 0.6981 | 0.7660 |
| 1.3963 | 0.1736 |
| 2.0944 | -0.5000 |
| 2.7925 | -0.9397 |
| 3.4907 | -0.9397 |
| 4.1888 | -0.5000 |
| 4.8869 | 0.1736 |
| 5.5851 | 0.7660 |
| 6.2832 | 1.0000 |
#+end_example



[-- Attachment #4: table_making_example_3.org --]
[-- Type: application/octet-stream, Size: 1642 bytes --]

#+LATEX_CLASS: article
#+LATEX_CLASS_OPTIONS: [a4paper]
#+OPTIONS: toc:nil ^:{}
#+STARTUP: hideblocks
#+TITLE: Reading and writing tables using Python
#+AUTHOR: Martin Schöön

* Input

#+tblname: input_table
| boat speed | 11 | knots |
| apparent wind angle | 21 | degrees |
| true wind speed | 14 | knots |

* Output

#+begin_src python -n :results output :var indata=input_table
from scipy import optimize
import numpy as np

def f1(x):
    '''First equation of equation system.'''
    return x[0] * np.sin((awa + x[1]) / 57.296) \
           - bs * np.sin(x[1] / 57.296) - tws

def f2(x):
    '''Second equation of equation system.'''
    return x[0] * np.cos((awa + x[1]) / 57.296) \
           - bs * np.cos(x[1] / 57.296)

def vf(x):
    '''Preparing equation system for fsolve.'''
    return [f1(x), f2(x)]


# Indata read from input_table in beginning of document.
awa = indata[1][1] # Apparent wind angle.
bs = indata[0][1]  # Boat speed.
tws = indata[2][1] # True wind speed.

# fsolve uses Powell's hybrid method to find a local zero.
# An initial guess, x0, is provided.
xx = optimize.fsolve(vf, x0 = [24., 52.])

# Return Apparent wind speed, b, VMG and tacking angle.
print '#+tblname: results'
print '| variable | value | unit |'
print '|-'
print '| AWS | %3.1f | knots |' % xx[0]
print '| b | %3.1f | degrees |' % xx[1]
print '| VMG | %2.1f | knots |' % (11. * np.sin(xx[1] / 57.296))
print '| tack | %3.1f | degrees |' % (2. * (90. - xx[1]))
#+end_src

#+RESULTS:
: #+tblname: results
: | variable | value | unit |
: |-
: | AWS | 23.7 | knots |
: | b | 52.6 | degrees |
: | VMG | 8.7 | knots |
: | tack | 74.7 | degrees |


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

* Re: Trouble with Tables and Python
  2014-04-15 21:18 Trouble with Tables and Python Martin Schöön
@ 2014-04-15 22:46 ` Nick Dokos
  2014-04-16  1:29 ` John Kitchin
  1 sibling, 0 replies; 5+ messages in thread
From: Nick Dokos @ 2014-04-15 22:46 UTC (permalink / raw)
  To: emacs-orgmode

Martin Schöön <martin.schoon@gmail.com> writes:

> ...  Let's move over to the third example file and the real
> mystery. This all my own code. Reading data from the first table works
> just fine and the same goes for the calculations. The output table,
> however, is different from that of example 2. The 'wrapper' is gone
> but each row is starting with ": ". Where does that come from? Why
> does it differ from example 2? If my results differ from someone
> else's (different install/versions etc) -- OK. But how can I get
> differences like this on my own? To my  un-trained eyes the print
> statements of examples 2 and 3 look very similar. (But, the first
> things that go blind are the eyes... probably something blatantly
> obvious once pointed out to me.)
>

2) and 3) are exactly the same thing (see section 11.3, "Literal
examples" in the org manual.)

The only difference is the length of the output - short output is
prepended by a colon, long output is wrapped in
#+BEGIN_EXAMPLE/#+END_EXAMPLE. What is short and what is long is
determined by the value of org-babel-min-lines-for-block-output
(default: 10) which you can customize.

--
Nick

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

* Re: Trouble with Tables and Python
  2014-04-15 21:18 Trouble with Tables and Python Martin Schöön
  2014-04-15 22:46 ` Nick Dokos
@ 2014-04-16  1:29 ` John Kitchin
  2014-04-16 18:58   ` Martin Schöön
  2014-04-18 13:09   ` Sebastien Vauban
  1 sibling, 2 replies; 5+ messages in thread
From: John Kitchin @ 2014-04-16  1:29 UTC (permalink / raw)
  To: Martin Schöön; +Cc: emacs-orgmode@gnu.org

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

I should probably confess to having this in my .emacs files:

;; language specific headers. I think this comes before the defaults
(setq org-babel-default-header-args:emacs-lisp
      (cons '(:results . "value replace")
        (assq-delete-all :results org-babel-default-header-args)))

;; set default :results to output
(setq org-babel-default-header-args
      (cons '(:results . "output replace")
        (assq-delete-all :results org-babel-default-header-args)))

;; set default exports to both code and results
(setq org-babel-default-header-args
      (cons '(:exports . "both")
        (assq-delete-all :exports org-babel-default-header-args)))

These things change the default behaviors for the results. I almost always
want output as results, and not value (the regular default), except when
coding in emacs-lisp where the value make sense to me.

That would certainly be a point of confusion ;)

I also find you want :results output raw

if you are printing a table or printing org. This is something I have never
found satisfying, especially for long tables or outputs it is tedious to
have to delete the old output by hand before rerunning it. I have not found
any other sets of options that do what I want, e.g. changing raw to org
results in the section being wrapped in #+begin_org/end_org, which is
usually not what I want. I usually do want the raw output.

I hope that clarifies where some of your differences might be coming from.

John

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



On Tue, Apr 15, 2014 at 5:18 PM, Martin Schöön <martin.schoon@gmail.com>wrote:

> This is my first post here...
>
> I have been using org-mode as a pure TODO-tool for some time but this
> winter I realised it could be used for much more and I have been
> experimenting with mixing in LaTeX and Python for fun and because I find
> literate programming a particularly sane idea (I am a HW engineer).
>
> Less than I week ago I started to look into using tables for input and
> output to/from Python scripts. I was inspired by something I found at
> http://kitchingroup.cheme.cmu.edu/ . The enclosed example 1 file is a
> pruned version of the org-file I found there. If I run the embedded Python
> script using C-c C-c I don't get the table shown in the example. Instead I
> get "none" on the row following "#+RESULTS:"
>
> If I change ":results raw" to ":results output" I do get the table but
> 'wrapped' in "#+begin_example" and "#+end_example" as shown in  the
> enclosed example 2 file. I have done some further testing/changes in that
> file (adding some stuff to the top of the file and changing the formatting
> of the print statements) none of which changed the extra wrapping of the
> table. When I export to LaTeX -> PDF the table does not show up in the
> end result.
>
> Let's move over to the third example file and the real mystery. This all
> my own code. Reading data from the first table works just fine and the same
> goes for the calculations. The output table, however, is different from
> that of example 2. The 'wrapper' is gone but each row is starting with ":
> ". Where does that come from? Why does it differ from example 2? If my
> results differ from someone else's (different install/versions etc) --
> OK. But how can I get differences like this on my own? To my  un-trained
> eyes the print statements of examples 2 and 3 look very similar. (But, the
> first things that go blind are the eyes... probably something blatantly
> obvious once pointed out to me.)
>
> OS: #! Linux (Based on Debian stable, 64-bit)
> Emacs: 23.4.1
> Org-mode: 8.2.5h
> Python: 2.7.3
>
> TIA,
> --
> Martin Schöön
>
> http://hem.bredband.net/b262106/index.html
>

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

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

* Re: Trouble with Tables and Python
  2014-04-16  1:29 ` John Kitchin
@ 2014-04-16 18:58   ` Martin Schöön
  2014-04-18 13:09   ` Sebastien Vauban
  1 sibling, 0 replies; 5+ messages in thread
From: Martin Schöön @ 2014-04-16 18:58 UTC (permalink / raw)
  To: emacs-orgmode@gnu.org

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

Thanks for the explanations and guiding. Now this works the way I want it
to.

-- 
Martin Schöön

http://hem.bredband.net/b262106/index.html

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

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

* Re: Trouble with Tables and Python
  2014-04-16  1:29 ` John Kitchin
  2014-04-16 18:58   ` Martin Schöön
@ 2014-04-18 13:09   ` Sebastien Vauban
  1 sibling, 0 replies; 5+ messages in thread
From: Sebastien Vauban @ 2014-04-18 13:09 UTC (permalink / raw)
  To: emacs-orgmode-mXXj517/zsQ

John Kitchin wrote:
> I also find you want :results output raw
>
> if you are printing a table or printing org. This is something I have never
> found satisfying, especially for long tables or outputs it is tedious to
> have to delete the old output by hand before rerunning it. I have not found
> any other sets of options that do what I want, e.g. changing raw to org
> results in the section being wrapped in #+begin_org/end_org, which is
> usually not what I want. I usually do want the raw output.

IIUC, that's one good reason to use the ":results drawer" thing.

Best regards,
  Seb

-- 
Sebastien Vauban

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

end of thread, other threads:[~2014-04-18 13:09 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-04-15 21:18 Trouble with Tables and Python Martin Schöön
2014-04-15 22:46 ` Nick Dokos
2014-04-16  1:29 ` John Kitchin
2014-04-16 18:58   ` Martin Schöön
2014-04-18 13:09   ` Sebastien Vauban

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