ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 09 Nov 2021 20:22:34 +0100Save tensor in filehttps://ask.sagemath.org/question/59632/save-tensor-in-file/I define the following $3+1$ manifold and chart:
n = 3
M = Manifold(1+n, 'M', structure='Lorentzian')
X.<t,r,th,ph> = M.chart(r"t r:(0,+oo) th:(0,pi):\theta ph:(0,2*pi):\phi")
with a variable $eps$ and function $h$:
eps = var('eps')
h = function('h')(r,th)
The metric of this space is:
gdd = M.metric()
gdd[0,0] = -eps; gdd[1,1] = r; gdd[2,2] = h; gdd[3,3] = 1
With this metric, I calculate numerous tensors (Riemann, Ricci, ...), which take a long time to compute. Therefore, I would like to save those tensors in a single file and then import them back every time I need them. I have tried using
> np.save('file.npy', gdd)
but when I open file.npy I read the following:
> Error! /Users/.../file.npy is not UTF-8 encoded
Is there a way I can save various symbolic tensors (which depend on functions and variables, as the one shown above) in a file, and load them back separately?Sun, 07 Nov 2021 23:09:57 +0100https://ask.sagemath.org/question/59632/save-tensor-in-file/Comment by keko for <p>I define the following $3+1$ manifold and chart:</p>
<pre><code>n = 3
M = Manifold(1+n, 'M', structure='Lorentzian')
X.<t,r,th,ph> = M.chart(r"t r:(0,+oo) th:(0,pi):\theta ph:(0,2*pi):\phi")
</code></pre>
<p>with a variable $eps$ and function $h$:</p>
<pre><code>eps = var('eps')
h = function('h')(r,th)
</code></pre>
<p>The metric of this space is:</p>
<pre><code>gdd = M.metric()
gdd[0,0] = -eps; gdd[1,1] = r; gdd[2,2] = h; gdd[3,3] = 1
</code></pre>
<p>With this metric, I calculate numerous tensors (Riemann, Ricci, ...), which take a long time to compute. Therefore, I would like to save those tensors in a single file and then import them back every time I need them. I have tried using</p>
<blockquote>
<p>np.save('file.npy', gdd)</p>
</blockquote>
<p>but when I open file.npy I read the following:</p>
<blockquote>
<p>Error! /Users/.../file.npy is not UTF-8 encoded</p>
</blockquote>
<p>Is there a way I can save various symbolic tensors (which depend on functions and variables, as the one shown above) in a file, and load them back separately?</p>
https://ask.sagemath.org/question/59632/save-tensor-in-file/?comment=59636#post-id-59636Yes. However, if I save it this way:
tensors = {'metric': gdd}
outfile = open('file','wb')
pickle.dump(tensors,outfile)
outfile.close()
when I import it back as:
infile = open('file','rb')
new_dict = pickle.load(infile)['metric']
infile.close()
and I print(new_dict), I get:
"Lorentzian metric g on the 4-dimensional Lorentzian manifold M"
I am not capable of recovering the whole tensor. If I save the metric as
tensors = {'metric': gdd[:]} or tensors = {'metric': gdd.expr())
I get errors...Mon, 08 Nov 2021 11:19:20 +0100https://ask.sagemath.org/question/59632/save-tensor-in-file/?comment=59636#post-id-59636Comment by Max Alekseyev for <p>I define the following $3+1$ manifold and chart:</p>
<pre><code>n = 3
M = Manifold(1+n, 'M', structure='Lorentzian')
X.<t,r,th,ph> = M.chart(r"t r:(0,+oo) th:(0,pi):\theta ph:(0,2*pi):\phi")
</code></pre>
<p>with a variable $eps$ and function $h$:</p>
<pre><code>eps = var('eps')
h = function('h')(r,th)
</code></pre>
<p>The metric of this space is:</p>
<pre><code>gdd = M.metric()
gdd[0,0] = -eps; gdd[1,1] = r; gdd[2,2] = h; gdd[3,3] = 1
</code></pre>
<p>With this metric, I calculate numerous tensors (Riemann, Ricci, ...), which take a long time to compute. Therefore, I would like to save those tensors in a single file and then import them back every time I need them. I have tried using</p>
<blockquote>
<p>np.save('file.npy', gdd)</p>
</blockquote>
<p>but when I open file.npy I read the following:</p>
<blockquote>
<p>Error! /Users/.../file.npy is not UTF-8 encoded</p>
</blockquote>
<p>Is there a way I can save various symbolic tensors (which depend on functions and variables, as the one shown above) in a file, and load them back separately?</p>
https://ask.sagemath.org/question/59632/save-tensor-in-file/?comment=59633#post-id-59633Did you try pickle? https://docs.python.org/3/library/pickle.htmlSun, 07 Nov 2021 23:45:43 +0100https://ask.sagemath.org/question/59632/save-tensor-in-file/?comment=59633#post-id-59633Answer by rburing for <p>I define the following $3+1$ manifold and chart:</p>
<pre><code>n = 3
M = Manifold(1+n, 'M', structure='Lorentzian')
X.<t,r,th,ph> = M.chart(r"t r:(0,+oo) th:(0,pi):\theta ph:(0,2*pi):\phi")
</code></pre>
<p>with a variable $eps$ and function $h$:</p>
<pre><code>eps = var('eps')
h = function('h')(r,th)
</code></pre>
<p>The metric of this space is:</p>
<pre><code>gdd = M.metric()
gdd[0,0] = -eps; gdd[1,1] = r; gdd[2,2] = h; gdd[3,3] = 1
</code></pre>
<p>With this metric, I calculate numerous tensors (Riemann, Ricci, ...), which take a long time to compute. Therefore, I would like to save those tensors in a single file and then import them back every time I need them. I have tried using</p>
<blockquote>
<p>np.save('file.npy', gdd)</p>
</blockquote>
<p>but when I open file.npy I read the following:</p>
<blockquote>
<p>Error! /Users/.../file.npy is not UTF-8 encoded</p>
</blockquote>
<p>Is there a way I can save various symbolic tensors (which depend on functions and variables, as the one shown above) in a file, and load them back separately?</p>
https://ask.sagemath.org/question/59632/save-tensor-in-file/?answer=59642#post-id-59642See the built-in [save](https://doc.sagemath.org/html/en/reference/misc/sage/misc/persist.html#sage.misc.persist.save) and [load](https://doc.sagemath.org/html/en/reference/misc/sage/misc/persist.html#sage.misc.persist.load) which are made for this purpose. In one session with `gdd` defined you can do
sage: save(gdd, 'gdd')
and in another you can do
sage: gdd = load('gdd.sobj')
sage: gdd[0,0]
-eps
It uses pickling internally. If you want you can specify a directory as part of the filename.Mon, 08 Nov 2021 18:56:33 +0100https://ask.sagemath.org/question/59632/save-tensor-in-file/?answer=59642#post-id-59642Comment by rburing for <p>See the built-in <a href="https://doc.sagemath.org/html/en/reference/misc/sage/misc/persist.html#sage.misc.persist.save">save</a> and <a href="https://doc.sagemath.org/html/en/reference/misc/sage/misc/persist.html#sage.misc.persist.load">load</a> which are made for this purpose. In one session with <code>gdd</code> defined you can do</p>
<pre><code>sage: save(gdd, 'gdd')
</code></pre>
<p>and in another you can do</p>
<pre><code>sage: gdd = load('gdd.sobj')
sage: gdd[0,0]
-eps
</code></pre>
<p>It uses pickling internally. If you want you can specify a directory as part of the filename.</p>
https://ask.sagemath.org/question/59632/save-tensor-in-file/?comment=59663#post-id-59663@keko this looks like a bug to me, reported as [trac ticket #32843](https://trac.sagemath.org/ticket/32843).Tue, 09 Nov 2021 20:22:34 +0100https://ask.sagemath.org/question/59632/save-tensor-in-file/?comment=59663#post-id-59663Comment by keko for <p>See the built-in <a href="https://doc.sagemath.org/html/en/reference/misc/sage/misc/persist.html#sage.misc.persist.save">save</a> and <a href="https://doc.sagemath.org/html/en/reference/misc/sage/misc/persist.html#sage.misc.persist.load">load</a> which are made for this purpose. In one session with <code>gdd</code> defined you can do</p>
<pre><code>sage: save(gdd, 'gdd')
</code></pre>
<p>and in another you can do</p>
<pre><code>sage: gdd = load('gdd.sobj')
sage: gdd[0,0]
-eps
</code></pre>
<p>It uses pickling internally. If you want you can specify a directory as part of the filename.</p>
https://ask.sagemath.org/question/59632/save-tensor-in-file/?comment=59654#post-id-59654If I print gdd[0,0] or gdd[1,1], everything goes fine. However, when printing gdd[2,2], which is the one containing the function h, I get the error:
"TypeError: sequence item 0: expected str instance, bytes found"
and the kernel restarts:
"The kernel appears to have died. It will restart automatically."
If it works fine for you maybe the reason is that I have installed some packages that are in conflict with save and load...Tue, 09 Nov 2021 09:27:46 +0100https://ask.sagemath.org/question/59632/save-tensor-in-file/?comment=59654#post-id-59654