ob-sagemath icon indicating copy to clipboard operation
ob-sagemath copied to clipboard

writing output to file

Open matthuszagh opened this issue 5 years ago • 2 comments

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

matthuszagh avatar Jan 15 '20 00:01 matthuszagh

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

EmmanuelCharpentier avatar Aug 17 '22 20:08 EmmanuelCharpentier

Perhaps we change this to a feature request then?

matthuszagh avatar Aug 17 '22 20:08 matthuszagh