emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* question about chaining function calls in org-babel
@ 2010-08-05  0:19 Inquisitive Scientist
  2010-08-05  2:08 ` Dan Davison
  0 siblings, 1 reply; 3+ messages in thread
From: Inquisitive Scientist @ 2010-08-05  0:19 UTC (permalink / raw)
  To: emacs-orgmode


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

Dear Experts,

I think org-babel is awesome but I'm having some trouble chaining
together multiple function calls.

Specifically, I would like to do something like the following
#+tblname: fancier
| mean   |
|--------|
| #ERROR |
#+TBLFM: @2$1='(sbe "python-mean" (x (sbe "tbl-example-data" (seed 4) (size
(sbe "square" (x 4))))))
but it doesn't work.

Can someone help me understand:
  1. How to make this work?
  2. How to debug things like this (e.g., how do I make something
     appearing in a table get its own session so I can use
     org-babel-pop-to-session)?

Supporting code and building up examples to try to make the above work
are shown below.

The following builds up examples for multiple function calls. Save
this in org-mode and see what happens.

Create a block to generate random data:
#+srcname: tbl-example-data(seed=2, size=3)
#+begin_src python :
import random
random.seed(seed)
return [i+random.gauss(0,.1) for i in range(size)]
#+end_src

Create a block to compute the mean
#+srcname: python-mean(x)
#+begin_src python
return sum(x)/float(len(x))
#+end_src

Create a table to use the data. To update the table do "C-u C-c *" in
the table.
#+tblname: summaries
|          mean |
|---------------|
| 1.06900574831 |
#+TBLFM: @2$1='(sbe "python-mean" (x "tbl-example-data"))

Create a table to use the data where we pass arguments to
tbl-example-data. To update the table do "C-u C-c *" in the table.
#+tblname: fancy
|          mean |
|---------------|
| 3.53014887192 |
#+TBLFM: @2$1='(sbe "python-mean" (x "tbl-example-data(seed=4,size=8)"))

Define a square function:
#+srcname: square(x)
#+begin_src python
return x*x
#+end_src

Test squaring:
#+tblname: testSquaring
| mean |
|------|
|   16 |
#+TBLFM: @2$1='(sbe "square" (x 4))


Try to do something even fancier and it doesn't work:
#+tblname: fancier
| mean |
|------|
|      |
#+TBLFM: @2$1='(sbe "python-mean" (x
"tbl-example-data(seed=4,size=square(4))"))

Try to do something even fancier with sbe and it doesn't work.
#+tblname: fancier
| mean |
|------|
|      |
#+TBLFM: @2$1='(sbe "python-mean" (x (sbe "tbl-example-data" (seed 4) (size
square(4)))))

One more try:
#+tblname: fancier
| mean   |
|--------|
| #ERROR |
#+TBLFM: @2$1='(sbe "python-mean" (x (sbe "tbl-example-data" (seed 4) (size
(sbe "square" (x 4))))))

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

[-- Attachment #2: Type: text/plain, Size: 201 bytes --]

_______________________________________________
Emacs-orgmode mailing list
Please use `Reply All' to send replies to the list.
Emacs-orgmode@gnu.org
http://lists.gnu.org/mailman/listinfo/emacs-orgmode

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

* Re: question about chaining function calls in org-babel
  2010-08-05  0:19 question about chaining function calls in org-babel Inquisitive Scientist
@ 2010-08-05  2:08 ` Dan Davison
  2010-08-06  0:41   ` Eric Schulte
  0 siblings, 1 reply; 3+ messages in thread
From: Dan Davison @ 2010-08-05  2:08 UTC (permalink / raw)
  To: Inquisitive Scientist; +Cc: emacs-orgmode

Hi I.S.,

Inquisitive Scientist <inquisitive.scientist@gmail.com> writes:

> Dear Experts,
>
> I think org-babel is awesome but I'm having some trouble chaining
> together multiple function calls.
>
> Specifically, I would like to do something like the following
> #+tblname: fancier
> | mean   |
> |--------|
> | #ERROR |
> #+TBLFM: @2$1='(sbe "python-mean" (x (sbe "tbl-example-data" (seed 4) (size
> (sbe "square" (x 4))))))
> but it doesn't work.

OK, first off, personally I would use a dedicated src block to construct
the complex chain of calls. So your most complex example can be done with

#+tblname:
|          mean |
|---------------|
| 7.52617964952 |
#+TBLFM: @2$1='(sbe "chain")

#+srcname: chain
#+begin_src emacs-lisp :var x=python-mean(x=tbl-example-data(seed=4, size=square(x=4)))
x
#+end_src

[...]

> Try to do something even fancier and it doesn't work:
> #+tblname: fancier
> | mean |
> |------|
> |      |
> #+TBLFM: @2$1='(sbe "python-mean" (x "tbl-example-data(seed=4,size=square(4))"))

This one works if we name the argument to square:

#+tblname: fancier
|          mean |
|---------------|
| 7.52617964952 |
#+TBLFM: @2$1='(sbe "python-mean" (x "tbl-example-data(seed=4,size=square(x=4))"))

Beyond that, your examples feature one sbe nested inside another. For
example

| mean   |
|--------|
| #ERROR |
#+TBLFM: @2$1='(sbe "python-mean" (x (sbe "tbl-example-data" (seed 4) (size 16))))

I *believe* that there is no expectation for this to work, because sbe
is an elisp function (well, a macro), and not a source block. In other
words, the first sbe is expecting a source block reference, whereas what
it gets is this lisp form:

(sbe "tbl-example-data" (seed 4) (size 16))

My guess is that sbe does not undertake to evaluate a lisp form
occurring in that context. But I admit that I still find these
complicated macros more or less incomprehensible, so we could do with
Eric's input here.

As for debugging, all I know of is to place print statements inside the
macro.

The docstring says

,----
| (defmacro sbe (source-block &rest variables)
|   "Return the results of calling SOURCE-BLOCK with VARIABLES.
| Each element of VARIABLES should be a two
| element list, whose first element is the name of the variable and
| second element is a string of its value.
| [...]
| NOTE: by default string variable names are interpreted as
| references to source-code blocks. To force interpretation of a
| cell's value as a string,
| [...]
`----

Dan


>
> Try to do something even fancier with sbe and it doesn't work.
> #+tblname: fancier
> | mean |
> |------|
> |      |
> #+TBLFM: @2$1='(sbe "python-mean" (x (sbe "tbl-example-data" (seed 4) (size
> square(4)))))
>
> One more try:
> #+tblname: fancier
> | mean   |
> |--------|
> | #ERROR |
> #+TBLFM: @2$1='(sbe "python-mean" (x (sbe "tbl-example-data" (seed 4) (size
> (sbe "square" (x 4))))))
> _______________________________________________
> Emacs-orgmode mailing list
> Please use `Reply All' to send replies to the list.
> Emacs-orgmode@gnu.org
> http://lists.gnu.org/mailman/listinfo/emacs-orgmode

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

* Re: Re: question about chaining function calls in org-babel
  2010-08-05  2:08 ` Dan Davison
@ 2010-08-06  0:41   ` Eric Schulte
  0 siblings, 0 replies; 3+ messages in thread
From: Eric Schulte @ 2010-08-06  0:41 UTC (permalink / raw)
  To: Dan Davison; +Cc: emacs-orgmode, Inquisitive Scientist

Dan Davison <davison@stats.ox.ac.uk> writes:

[...]
>
> | mean   |
> |--------|
> | #ERROR |
> #+TBLFM: @2$1='(sbe "python-mean" (x (sbe "tbl-example-data" (seed 4) (size 16))))
>
> I *believe* that there is no expectation for this to work, because sbe
> is an elisp function (well, a macro), and not a source block. In other
> words, the first sbe is expecting a source block reference, whereas what
> it gets is this lisp form:
>
> (sbe "tbl-example-data" (seed 4) (size 16))
>
> My guess is that sbe does not undertake to evaluate a lisp form
> occurring in that context. But I admit that I still find these
> complicated macros more or less incomprehensible, so we could do with
> Eric's input here.
>

Dan is correct, the example above with nested calls to sbe will not
work, something like the following at least has the possibility of
working.

| mean   |
|--------|
| #ERROR |
#+TBLFM: @2$1='(sbe "python-mean" (x "tbl-example-data(seed=4,size=6)"))

Unfortunately I don't have the time to test this right now.

Best -- Eric

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

end of thread, other threads:[~2010-08-06  0:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-08-05  0:19 question about chaining function calls in org-babel Inquisitive Scientist
2010-08-05  2:08 ` Dan Davison
2010-08-06  0:41   ` Eric Schulte

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