sagemath / ob-sagemath

org-babel integration with SageMath
GNU General Public License v3.0
34 stars 8 forks source link

writing output to file #6

Open matthuszagh opened 4 years ago

matthuszagh commented 4 years ago

I'm having trouble writing the output of execution to a local file. For example

#+header: :results output file silent
#+header: :session nil
#+header: :file analog-type-I-phase-detector.dat :output-dir data/
#+begin_src sage
import numpy as np
from sage.symbolic.integration.integral import definite_integral

x = var("x")
phi = var("phi")
fref = sin(x)
fsig = sin(x + phi)
for phase in np.linspace(0, 2 * np.pi, num=10):
    avg = definite_integral(fref * fsig(phi=phase), x, 0, 2 * pi)
    print("{:.3f}\t{:.3f}".format(phase, float(avg)))
#+end_src

Does not write the data/analog-type-I-phase-detector.dat file. It simply echos the file link in the minibuffer. A similar python example works fine:

#+begin_src python :results output silent file :file test.txt
print("hello")
#+end_src

Is there a way to write stdout to a local file with sage? thanks.

EDIT: Something like this works, but it would be nice to be able to use the file header directly instead of delegating to python logic.

#+begin_src sage :results output silent :var cwd=(file-name-directory buffer-file-name)
import numpy as np
from os.path import abspath
from sage.symbolic.integration.integral import definite_integral

fname = cwd + "data/analog-type-I-phase-detector.dat"

x = var("x")
phi = var("phi")
fref = sin(x)
fsig = sin(x - phi)
with open(fname, "w") as f:
    f.write("phi vout\n")
    for phase in np.linspace(0, 2 * np.pi, num=1000):
        avg = definite_integral(fref * fsig(phi=phase), x, 0, 2 * pi)
        f.write("{:.3f}\t{:.3f}\n".format(phase, float(avg)))
#+end_src
EmmanuelCharpentier commented 2 years ago

:output-dir is not currently listed as a supported header argument for Sage. It's a nice whishlist item, but not supported code...

I suggest closing this issue.

matthuszagh commented 2 years ago

Perhaps we change this to a feature request then?