ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 19 Dec 2011 02:00:23 -0600How to save a list to a file, with sage objects in it?https://ask.sagemath.org/question/8560/how-to-save-a-list-to-a-file-with-sage-objects-in-it/Hi, I am trying to save intermediate step of my calculation into a file, so that next time I can read the file to resume the calculation. Say, I need to save
f = function('a',var('t'))
s = [f] # together with other things in the list
How to save s to a file?
I tried two ways:
(1) Standard python module pickle cannot save sage objects, thus fails
(2) I converted s to a python object:
s = Sequence([f])
Now I can use s.save('file_name'). However, there is a bug that I cannot load using load('file_name')
RuntimeError: unknown function 'a' in archive
It is a known bug: http://trac.sagemath.org/sage_trac/ticket/11919 .
Unfortunately I am not expert enough to fix that bug, and I really want to get my work done before waiting for the fix. Is there any other way to save the list s currently?
In the bug report it is mentioned that an older version of Pynac doesn't have this bug. If there is no other work around, is it possible to downgrade Pynac within sage or I have to downgrade the whole sage?
Thanks!
Thu, 15 Dec 2011 12:59:22 -0600https://ask.sagemath.org/question/8560/how-to-save-a-list-to-a-file-with-sage-objects-in-it/Comment by John Palmieri for <p>Hi, I am trying to save intermediate step of my calculation into a file, so that next time I can read the file to resume the calculation. Say, I need to save</p>
<pre><code>f = function('a',var('t'))
s = [f] # together with other things in the list
</code></pre>
<p>How to save s to a file?</p>
<p>I tried two ways: </p>
<p>(1) Standard python module pickle cannot save sage objects, thus fails</p>
<p>(2) I converted s to a python object:</p>
<pre><code>s = Sequence([f])
</code></pre>
<p>Now I can use s.save('file_name'). However, there is a bug that I cannot load using load('file_name')</p>
<p>RuntimeError: unknown function 'a' in archive</p>
<p>It is a known bug: <a href="http://trac.sagemath.org/sage_trac/ticket/11919">http://trac.sagemath.org/sage_trac/ti...</a> . </p>
<p>Unfortunately I am not expert enough to fix that bug, and I really want to get my work done before waiting for the fix. Is there any other way to save the list s currently?</p>
<p>In the bug report it is mentioned that an older version of Pynac doesn't have this bug. If there is no other work around, is it possible to downgrade Pynac within sage or I have to downgrade the whole sage?</p>
<p>Thanks!</p>
https://ask.sagemath.org/question/8560/how-to-save-a-list-to-a-file-with-sage-objects-in-it/?comment=20704#post-id-20704I don't think it's the fact that `s` is a list, it's that it contains the function `f`. If you had "s = [sin(x), 12, 'hello']", you could do "save(s, 'file_name')" and then "load('file_name.sobj')" works fine.Thu, 15 Dec 2011 17:29:37 -0600https://ask.sagemath.org/question/8560/how-to-save-a-list-to-a-file-with-sage-objects-in-it/?comment=20704#post-id-20704Comment by tririver for <p>Hi, I am trying to save intermediate step of my calculation into a file, so that next time I can read the file to resume the calculation. Say, I need to save</p>
<pre><code>f = function('a',var('t'))
s = [f] # together with other things in the list
</code></pre>
<p>How to save s to a file?</p>
<p>I tried two ways: </p>
<p>(1) Standard python module pickle cannot save sage objects, thus fails</p>
<p>(2) I converted s to a python object:</p>
<pre><code>s = Sequence([f])
</code></pre>
<p>Now I can use s.save('file_name'). However, there is a bug that I cannot load using load('file_name')</p>
<p>RuntimeError: unknown function 'a' in archive</p>
<p>It is a known bug: <a href="http://trac.sagemath.org/sage_trac/ticket/11919">http://trac.sagemath.org/sage_trac/ti...</a> . </p>
<p>Unfortunately I am not expert enough to fix that bug, and I really want to get my work done before waiting for the fix. Is there any other way to save the list s currently?</p>
<p>In the bug report it is mentioned that an older version of Pynac doesn't have this bug. If there is no other work around, is it possible to downgrade Pynac within sage or I have to downgrade the whole sage?</p>
<p>Thanks!</p>
https://ask.sagemath.org/question/8560/how-to-save-a-list-to-a-file-with-sage-objects-in-it/?comment=20702#post-id-20702Hi, John: thanks a lot for your reply! I agree with you. But my actual goal is really want to save a list with a lot of functions like f. In my project, I start from an abstract form of a differential equation, and simplify it to a simpler form (here I want to save my work because the former step is slow, and I want to use it when I start Sage next time), and solve the differential equation with different additional conditions. It's a differential equation thus I have to define a general function instead of put a "sin(x)" in the list.Thu, 15 Dec 2011 23:52:44 -0600https://ask.sagemath.org/question/8560/how-to-save-a-list-to-a-file-with-sage-objects-in-it/?comment=20702#post-id-20702Answer by tririver for <p>Hi, I am trying to save intermediate step of my calculation into a file, so that next time I can read the file to resume the calculation. Say, I need to save</p>
<pre><code>f = function('a',var('t'))
s = [f] # together with other things in the list
</code></pre>
<p>How to save s to a file?</p>
<p>I tried two ways: </p>
<p>(1) Standard python module pickle cannot save sage objects, thus fails</p>
<p>(2) I converted s to a python object:</p>
<pre><code>s = Sequence([f])
</code></pre>
<p>Now I can use s.save('file_name'). However, there is a bug that I cannot load using load('file_name')</p>
<p>RuntimeError: unknown function 'a' in archive</p>
<p>It is a known bug: <a href="http://trac.sagemath.org/sage_trac/ticket/11919">http://trac.sagemath.org/sage_trac/ti...</a> . </p>
<p>Unfortunately I am not expert enough to fix that bug, and I really want to get my work done before waiting for the fix. Is there any other way to save the list s currently?</p>
<p>In the bug report it is mentioned that an older version of Pynac doesn't have this bug. If there is no other work around, is it possible to downgrade Pynac within sage or I have to downgrade the whole sage?</p>
<p>Thanks!</p>
https://ask.sagemath.org/question/8560/how-to-save-a-list-to-a-file-with-sage-objects-in-it/?answer=13036#post-id-13036A partial answer: in Sage 4.7.0, the above attempt (2) works. Thus it is indeed a bug brought in from either 4.7.1 or 4.7.2. I also tried 4.8 alpha 5, the bug is not fixed --looking forward to a fix.Sat, 17 Dec 2011 12:53:37 -0600https://ask.sagemath.org/question/8560/how-to-save-a-list-to-a-file-with-sage-objects-in-it/?answer=13036#post-id-13036Answer by DSM for <p>Hi, I am trying to save intermediate step of my calculation into a file, so that next time I can read the file to resume the calculation. Say, I need to save</p>
<pre><code>f = function('a',var('t'))
s = [f] # together with other things in the list
</code></pre>
<p>How to save s to a file?</p>
<p>I tried two ways: </p>
<p>(1) Standard python module pickle cannot save sage objects, thus fails</p>
<p>(2) I converted s to a python object:</p>
<pre><code>s = Sequence([f])
</code></pre>
<p>Now I can use s.save('file_name'). However, there is a bug that I cannot load using load('file_name')</p>
<p>RuntimeError: unknown function 'a' in archive</p>
<p>It is a known bug: <a href="http://trac.sagemath.org/sage_trac/ticket/11919">http://trac.sagemath.org/sage_trac/ti...</a> . </p>
<p>Unfortunately I am not expert enough to fix that bug, and I really want to get my work done before waiting for the fix. Is there any other way to save the list s currently?</p>
<p>In the bug report it is mentioned that an older version of Pynac doesn't have this bug. If there is no other work around, is it possible to downgrade Pynac within sage or I have to downgrade the whole sage?</p>
<p>Thanks!</p>
https://ask.sagemath.org/question/8560/how-to-save-a-list-to-a-file-with-sage-objects-in-it/?answer=13037#post-id-13037Okay, this really isn't a solution, but might serve as a temporary workaround-- you can't save formal functions, but you *can* convert them to maxima objects, and those can be saved.
For example:
sage: f = function('a',var('t'))
sage: parent(f)
Symbolic Ring
sage: # convert it to maxima (maxima(f) does the same thing)
sage: f._maxima_()
'a(t)
sage: # note the prefix ' now!
sage: parent(_)
Maxima_lib
sage: # convert forward and back
sage: (f._maxima_())._sage_()
a(t)
sage: # do we have equality?
sage: bool((f._maxima_())._sage_() == f)
True
So while we have
sage: loads(dumps(f))
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
[...]
RuntimeError: unknown function 'a' in archive
we're lucky enough to have
sage: bool(loads(dumps(f._maxima_()))._sage_() == f)
True
It's straightforward to bundle the above technique for these guys. Not everything can be converted seamlessly between Sage and maxima, so you might want to only convert some things, but you get the idea.
Sat, 17 Dec 2011 15:02:59 -0600https://ask.sagemath.org/question/8560/how-to-save-a-list-to-a-file-with-sage-objects-in-it/?answer=13037#post-id-13037Comment by tririver for <p>Okay, this really isn't a solution, but might serve as a temporary workaround-- you can't save formal functions, but you <em>can</em> convert them to maxima objects, and those can be saved.</p>
<p>For example:</p>
<pre><code>sage: f = function('a',var('t'))
sage: parent(f)
Symbolic Ring
sage: # convert it to maxima (maxima(f) does the same thing)
sage: f._maxima_()
'a(t)
sage: # note the prefix ' now!
sage: parent(_)
Maxima_lib
sage: # convert forward and back
sage: (f._maxima_())._sage_()
a(t)
sage: # do we have equality?
sage: bool((f._maxima_())._sage_() == f)
True
</code></pre>
<p>So while we have</p>
<pre><code>sage: loads(dumps(f))
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
[...]
RuntimeError: unknown function 'a' in archive
</code></pre>
<p>we're lucky enough to have </p>
<pre><code>sage: bool(loads(dumps(f._maxima_()))._sage_() == f)
True
</code></pre>
<p>It's straightforward to bundle the above technique for these guys. Not everything can be converted seamlessly between Sage and maxima, so you might want to only convert some things, but you get the idea.</p>
https://ask.sagemath.org/question/8560/how-to-save-a-list-to-a-file-with-sage-objects-in-it/?comment=20690#post-id-20690Thanks a lot! Seems a best answer before the bug is fixed.Mon, 19 Dec 2011 02:00:23 -0600https://ask.sagemath.org/question/8560/how-to-save-a-list-to-a-file-with-sage-objects-in-it/?comment=20690#post-id-20690