notebook questions

Hello!

Could you help me? I used notebook and in it I wrote a program in SAGE which generated a huge dictionary. I would like to save only this dictionary because I need to use this dictionary in another SAGE program. Unfortunately, I am not very familiar in using the notebook, so I don't know how to do this... Another (very simple) question when there is a bug in my program in the notebook, the computer wrotes me which line the bug is. But I don't how to find this line with the given number. Is there a line-numerator in the notebook?

edit retag close merge delete

The question is not very clear. Can you write a sample code to explain what you mean by dictionary and how you use it in another sage program.

( 2011-04-04 11:00:40 +0100 )edit

Hello!

I show you an example. My dictionary is the following:

square_sum={}

for a in range(1000):

   for b in range(1000):

square_sum[a,b]=a^2+b^2


Now in another notebook worksheet I would like to upload only this dictionary (or is it called vocabulary?) square_sum. So the question is how to save the dictionary and how to upload it in another worksheet?

( 2011-04-04 11:15:23 +0100 )edit

Sort by ยป oldest newest most voted

Katika, you should be able to save any object in Sage, as well as a whole session. If you click "Help" on the notebook at the top, the page you get should have

Use "save obj1 obj2 ..." and "load obj1 obj2 ...". This allows for easy moving of objects from one worksheet to another, and saving of objects for later use.


toward the bottom. The syntax is wrong, though.This is now Ticket 11124.

This should work.

A = matrix([[2,3],[3,4]])
save(A,'test')


And then a link to the actual .sobj file should be created, and you could use that. The help for the DATA variable should help as well. If you want to use your home directory, then

save(A,os.path.expanduser('~/')+'test')


might help you; it will show up in your home directory, if you use a system which supports the tilde. Maybe that should be in the help as well...

more

Another (very simple) question when there is a bug in my program in the notebook, the computer wrotes me which line the bug is. But I don't how to find this line with the given number. Is there a line-numerator in the notebook?

Hmm, that is harder. At least for simple examples, the line does seem to be the line I made the mistake in:

for i in range(10):
print i+1
print i^2
a=i[0]
print a+i


But multicell ones don't seem to be as nice. You may just have to look at the code that causes the problem and find it in your input. We don't have interactive markup (e.g. coloring) yet, because the javascript really slows things down.

more

Thank you very much :-)

( 2011-04-05 06:07:11 +0100 )edit

If any one of the answers was best, be sure to help future users and mark it as such, so that it's easily searchable. Doesn't have to be my answer, either :-)

( 2011-04-05 13:25:35 +0100 )edit

I secretly voted :-) (Or voting is different from marking... ?)

( 2011-04-06 05:15:55 +0100 )edit

Yes. There should be a large check mark that shows up on each answer, which can be clicked. I don't know if you can vote for your own, but you can vote for others. It also adds to your 'karma' a small amount.

( 2011-04-06 09:29:07 +0100 )edit
def square_sum(a,b):
return(a*a+b*b)
fname="filename.dat"
fp=open(fname,'w')
for a in range(1000):
for b in range(1000):
s=str(a)+' '+str(b)+' '+str(square_sum(a,b))+'\n'
fp.write(s)
fp.close()


This will write the data to a filename named "filename.dat". You can also give a path if you wish.

To read it from another notebook use the following code

import numpy as np
array=np.genfromtxt('/path-to-file/filename.dat',delimiter='')


Of course you have to replace 'path-to-file' by whatever path it is. Now you can use array the way we usually use arrays.

I hope this helps.

more

If Sage is the only program that needs to read the data, you can use the "save" and "load" commands:

sage: square_sum = dict(((a,b),a^2+b^2) for a,b in CartesianProduct([1..5],[1..5]))
sage: square_sum
{(1, 3): 10, (5, 4): 41, (2, 1): 5, (5, 1): 26, (2, 5): 29, (1, 2): 5, (3, 3): 18, (4, 4): 32, (1, 5): 26, (2, 2): 8, (5, 3): 34, (4, 1): 17, (1, 1): 2, (3, 2): 13, (4, 5): 41, (5, 5): 50, (1, 4): 17, (2, 3): 13, (4, 2): 20, (3, 5): 34, (3, 4): 25, (3, 1): 10, (4, 3): 25, (5, 2): 29, (2, 4): 20}
sage: save(square_sum, "ssum")
sage: s2
{(1, 3): 10, (3, 2): 13, (2, 1): 5, (5, 1): 26, (2, 5): 29, (1, 2): 5, (3, 3): 18, (4, 4): 32, (1, 5): 26, (2, 2): 8, (3, 5): 34, (4, 1): 17, (1, 1): 2, (5, 4): 41, (4, 5): 41, (1, 4): 17, (2, 3): 13, (4, 2): 20, (5, 5): 50, (5, 3): 34, (5, 2): 29, (3, 1): 10, (4, 3): 25, (3, 4): 25, (2, 4): 20}
sage: s2 == square_sum
True


(Remember that dictionaries don't have an order, so the fact that the keys show up differently in s2 than in the original square_sum doesn't matter.)

more

I tried this, but it doesn't work if I would like to load "ssum" in another notebook cell... Is there a way to tell the path to the ssum.obj? I think something load('~/sage/ssum.sobj')?

( 2011-04-05 10:29:52 +0100 )edit

kcrisman's example shows how you can do this: save(A,"some-hardcoded-path"), or save(A,os.path.expanduser('~/')+'test') if you want to use a tilde. (Tilde expansion isn't automatic).

( 2011-04-05 11:44:43 +0100 )edit