What's a good way to save matrix objects to file for later use? I just need something quick for debugging. I saw two suggestions on this list from Francesc Altet (2006-05-22): 1. Use tofile and fromfile and save the meta data yourself. 2. pytables Any suggestions for #3? If I do #1, how would I put multiple matrices in a single file? Make everything a 1-d array, first element is number of matrices, followed by number of rows and columns for each matrix?
On 11/30/06, Keith Goodman <kwgoodman@gmail.com> wrote:
What's a good way to save matrix objects to file for later use? I just need something quick for debugging.
I saw two suggestions on this list from Francesc Altet (2006-05-22):
1. Use tofile and fromfile and save the meta data yourself.
2. pytables
Any suggestions for #3?
Is this what you want? In [14]: a Out[14]: matrix([[2, 3], [4, 5]]) In [15]: b Out[15]: matrix([[2, 3], [4, 5]]) In [16]: f = open('dump.pkl','w') In [17]: pickle.dump(a,f) In [18]: pickle.dump(b,f) In [19]: f.close() In [20]: f = open('dump.pkl','r') In [21]: x = pickle.load(f) In [22]: y = pickle.load(f) In [23]: f.close() In [24]: x Out[24]: matrix([[2, 3], [4, 5]]) In [25]: y Out[25]: matrix([[2, 3], [4, 5]]) Chuck
On 11/30/06, Charles R Harris <charlesr.harris@gmail.com> wrote:
On 11/30/06, Keith Goodman <kwgoodman@gmail.com> wrote:
What's a good way to save matrix objects to file for later use? I just need something quick for debugging.
I saw two suggestions on this list from Francesc Altet (2006-05-22):
1. Use tofile and fromfile and save the meta data yourself.
2. pytables
Any suggestions for #3? Is this what you want?
In [14]: a Out[14]: matrix([[2, 3], [4, 5]])
In [15]: b Out[15]: matrix([[2, 3], [4, 5]])
In [16]: f = open(' dump.pkl','w')
In [17]: pickle.dump(a,f)
In [18]: pickle.dump(b,f)
In [19]: f.close()
In [20]: f = open('dump.pkl','r')
In [21]: x = pickle.load(f)
In [22]: y = pickle.load(f)
In [23]: f.close()
In [24]: x Out[24]: matrix([[2, 3], [4, 5]])
In [25]: y Out[25]: matrix([[2, 3], [4, 5]])
Yes. That will do very well. You got me out of a pickle.
On 11/30/06, Charles R Harris <charlesr.harris@gmail.com> wrote:
On 11/30/06, Keith Goodman <kwgoodman@gmail.com> wrote:
What's a good way to save matrix objects to file for later use? I just need something quick for debugging.
I saw two suggestions on this list from Francesc Altet (2006-05-22):
1. Use tofile and fromfile and save the meta data yourself.
2. pytables
Any suggestions for #3?
Is this what you want?
In [14]: a Out[14]: matrix([[2, 3], [4, 5]])
In [15]: b Out[15]: matrix([[2, 3], [4, 5]])
In [16]: f = open(' dump.pkl','w')
In [17]: pickle.dump(a,f)
In [18]: pickle.dump(b,f)
In [19]: f.close()
In [20]: f = open('dump.pkl','r')
In [21]: x = pickle.load(f)
In [22]: y = pickle.load(f)
In [23]: f.close()
In [24]: x Out[24]: matrix([[2, 3], [4, 5]])
In [25]: y Out[25]: matrix([[2, 3], [4, 5]])
It is also possible to put the variables of interest in a dictionary, then pickle the dictionary. That way you can also store the variable names. In [27]: f = open('dump.pkl','w') In [28]: pickle.dump( {'a':a,'b':b}, f) In [29]: f.close() In [30]: f = open('dump.pkl','r') In [31]: mystuff = pickle.load(f) In [32]: f.close() In [34]: mystuff Out[34]: {'a': matrix([[2, 3], [4, 5]]), 'b': matrix([[2, 3], [4, 5]])} I think you can actually pickle the whole evironment, but I don't recall how. Chuck
On 11/30/06, Charles R Harris <charlesr.harris@gmail.com> wrote:
It is also possible to put the variables of interest in a dictionary, then pickle the dictionary. That way you can also store the variable names.
In [27]: f = open(' dump.pkl','w')
In [28]: pickle.dump( {'a':a,'b':b}, f)
In [29]: f.close()
In [30]: f = open('dump.pkl','r')
In [31]: mystuff = pickle.load(f)
In [32]: f.close()
In [34]: mystuff Out[34]: {'a': matrix([[2, 3], [4, 5]]), 'b': matrix([[2, 3], [4, 5]])}
I think I could use that to write a function savematrix(filename, a, b, c,...) Is there a way to write a loadmatrix(filename) that doesn't return anything but makes the matrices a, b, c, ... available? Probably not a good function design. But useful for quick things.
On 11/30/06, Keith Goodman <kwgoodman@gmail.com> wrote:
On 11/30/06, Charles R Harris <charlesr.harris@gmail.com> wrote:
It is also possible to put the variables of interest in a dictionary,
then
pickle the dictionary. That way you can also store the variable names.
In [27]: f = open(' dump.pkl','w')
In [28]: pickle.dump( {'a':a,'b':b}, f)
In [29]: f.close()
In [30]: f = open('dump.pkl','r')
In [31]: mystuff = pickle.load(f)
In [32]: f.close()
In [34]: mystuff Out[34]: {'a': matrix([[2, 3], [4, 5]]), 'b': matrix([[2, 3], [4, 5]])}
I think I could use that to write a function savematrix(filename, a, b, c,...)
Is there a way to write a loadmatrix(filename) that doesn't return anything but makes the matrices a, b, c, ... available?
I think there is, that is why I mentioned the saving the environment thingee. IIRC, I saw code for something like that a couple of years back but I don't recall the details. Maybe something like: In [80]: globals()['x'] = [1,2] In [81]: x Out[81]: [1, 2] Then you just have to merge the pickled dictionary with globals(). Like this:
globals().update(mystuff)
where mystuff is the dictionary where you have your stuff. This could probably also go something like
globals().update(load(f))
where f contains the pickled dictionary. Chuck
On 11/30/06, Charles R Harris <charlesr.harris@gmail.com> wrote:
On 11/30/06, Keith Goodman <kwgoodman@gmail.com> wrote:
On 11/30/06, Charles R Harris <charlesr.harris@gmail.com> wrote:
It is also possible to put the variables of interest in a dictionary,
then
pickle the dictionary. That way you can also store the variable names.
In [27]: f = open(' dump.pkl','w')
In [28]: pickle.dump( {'a':a,'b':b}, f)
In [29]: f.close()
In [30]: f = open('dump.pkl','r')
In [31]: mystuff = pickle.load(f)
In [32]: f.close()
In [34]: mystuff Out[34]: {'a': matrix([[2, 3], [4, 5]]), 'b': matrix([[2, 3], [4, 5]])}
I think I could use that to write a function savematrix(filename, a, b, c,...)
Is there a way to write a loadmatrix(filename) that doesn't return anything but makes the matrices a, b, c, ... available?
I think there is, that is why I mentioned the saving the environment thingee. IIRC, I saw code for something like that a couple of years back but I don't recall the details. Maybe something like:
In [80]: globals()['x'] = [1,2]
In [81]: x Out[81]: [1, 2]
Then you just have to merge the pickled dictionary with globals(). Like this:
globals().update(mystuff)
where mystuff is the dictionary where you have your stuff. This could probably also go something like
globals().update(load(f))
where f contains the pickled dictionary.
You could probably dump the entire environment from a subroutine, cPickle.dump(globals(), f), which might be a good way to save everything off, but not very efficient. Chuck
participants (2)
-
Charles R Harris -
Keith Goodman