question about visualization/animation of surfaces

6 views
Skip to first unread message

ggrafendorfer

unread,
Dec 8, 2008, 6:47:57 PM12/8/08
to sage-support
Hi,
I have a list of surfaces (btw: volatility surfaces), each of which
represented by a list of 3-Tuples (numbers),
I would like to visualize them in an animation showing a surface
evolving in time.

Any suggestions which tools I could use for this?

Thank you,
Georg



Marshall Hampton

unread,
Dec 8, 2008, 9:33:35 PM12/8/08
to sage-support
The nicest looking solution I have found is to make frames with
tachyon and then animate the results with ffmpeg. I have meant to
create some how-to documentation for the things I have done so far but
I do not have the time right now, hopefully I will soon. The options
for ffmpeg are somewhat confusing, at least for me, but I have gotten
good results by just using the flag "-q 2" which keeps the quality
high.

Hope that helps,
M. Hampton

ggrafendorfer

unread,
Dec 9, 2008, 6:02:24 AM12/9/08
to sage-support
Hi Marshall,
thanks for the answer, if you once have the frames I also know these
two projects, but I did not try them out yet:

http://www.onyxbits.de/giftedmotion

http://www.lcdf.org/~eddietwo/gifsicle/

Is it possible to make the frames, i.e. .jpg or .png files with
tachyon from within sage or python?

Actually I was hoping for an easy "on the fly" solution, to keep all
the data of all surfaces in one file and produce the moving surface
directly from that file

thanks,
Georg

Marshall Hampton

unread,
Dec 17, 2008, 12:54:51 PM12/17/08
to sage-support
Here is an example which might help. For it to work you need to
install ffmpeg. It generates a rotating icosahedron movie. For what
you want to do list_plot3d might be more appropriate:

{{{
c = polytopes.icosahedron()
label = 'Ico_movie'

# Numerical pi predefined for some speed
npi = RDF(pi)

#A rotation matrix
def rot(i,j,th):
q = matrix(RDF,3)
for k in range(3):
q[k,k] = 1.0
q[i,i] = cos(th)
q[j,j] = cos(th)
q[i,j] = sin(th)
q[j,i] = -sin(th)
return q

index = 0
for theta in srange(0,2*npi,npi/10):
vs = c.vertices()[:]
#rotate the vertices:
vs = [rot(0,1,theta)*vector(v) for v in vs]
edgedata = c.vertex_adjacencies()
facedata = c.triangulated_facial_incidences()

#Set up Tachyon.
#use small xres, yres for speed. antialiasing helps things look
better.
t = Tachyon(xres = 250, yres = 250, antialiasing = True)
t.texture('e1', color = (0,0,1))
t.texture('f1',opacity = .5, color = (1,0,0))
t.light((2,2,2), 0.1, (1,1,1))

#Draw edges:
edges = []
for anedge in edgedata:
v0 = anedge[0]
for v1 in anedge[1]:
if v1 > v0:
edges.append([vs[v0],vs[v1]])
for anedge in edges:
t.fcylinder(anedge[0],anedge[1],.01,'e1')

#Draw transparent faces:
for atri in facedata:
t.triangle(vs[atri[1][0]],vs[atri[1][1]],vs[atri[1][2]],'f1')

#Make file names in order for ffmpeg
str_index = '0'*(3-len(str(index)))+str(index)
t.save(DATA+label+ str_index + '.png')
index += 1

#Add a black final frame, since otherwise the compression in ffmpeg
gets confused.
t = Tachyon(xres = 250, yres = 250, antialiasing = True)
str_index = '0'*(3-len(str(index)))+str(index)
t.save(DATA+label+ str_index + '.png')

#Use ffmpeg to animate:
os.system('ffmpeg -qmax 2 -i ' + DATA + label + '%3d.png ./test.mp4')

ggrafendorfer

unread,
Dec 17, 2008, 3:25:58 PM12/17/08
to sage-support
Thanks a lot !!
Wonderfull, this a really nice example containing many things to learn
from ...

greetings,
Georg

calc...@aol.com

unread,
Jan 15, 2009, 5:53:03 PM1/15/09
to sage-support
Is this a python program independent of Sage or is it meant to be run
in a Sage notebook()?

HTH,
A. Jorge Garcia

Teacher & Professor
Applied Mathematics, Physics & Computer Science
Baldwin Senior High School & Nassau Community College

calc...@aol.com
http://calcpage.tripod.com
ftp://centauri.baldwinschools.net

Jason Grout

unread,
Jan 15, 2009, 5:58:10 PM1/15/09
to sage-s...@googlegroups.com
calc...@aol.com wrote:
> Is this a python program independent of Sage or is it meant to be run
> in a Sage notebook()?

The program in the message you are replying to contains calls to many
Sage objects and functions, so you will need Sage to run the program.

Jason

Calc...@aol.com

unread,
Jan 15, 2009, 7:11:46 PM1/15/09
to sage-s...@googlegroups.com
That's what I thought but I ran it on www.sagenb.org and got errors.  Should I run it in a Sage shell instead of a Sage notebook()?
 
TIA,

A. Jorge Garcia

Teacher & Professor
Applied Mathematics, Physics & Computer Science
Baldwin Senior High School & Nassau Community College




A Good Credit Score is 700 or Above. See yours in just 2 easy steps!

Marshall Hampton

unread,
Jan 15, 2009, 7:30:22 PM1/15/09
to sage-support
The final step, using ffmpeg, requires that ffmpeg be installed and in
the path, which probably isn't the case on sagenb.org. Otherwise I
would think it would be OK. If you cut and paste you might get syntax
errors from comment lines being broken, they just need to be on the
same line or re-commented out.

-Marshall

On Jan 15, 6:11 pm, CalcP...@aol.com wrote:
> That's what I thought but I ran it on _www.sagenb.org_
> (http://www.sagenb.org) and got errors. Should I run it in a Sage shell instead of a Sage
> notebook()?
>
> TIA,
> A. Jorge Garcia
>
> Teacher & Professor
> Applied Mathematics, Physics & Computer Science
> Baldwin Senior High School & Nassau Community College
>
> _calcp...@aol.com_ (mailto:calcp...@aol.com)
> _http://calcpage.tripod.com_(http://calcpage.tripod.com/)
> _ftp://centauri.baldwinschools.net_(ftp://centauri.baldwinschools.net/)
>
> **************A Good Credit Score is 700 or Above. See yours in just 2 easy
> steps!
> (http://pr.atwola.com/promoclk/100000075x1215855013x1201028747/aol?red...
> cemailfooterNO62)

Calc...@aol.com

unread,
Jan 15, 2009, 7:47:39 PM1/15/09
to sage-s...@googlegroups.com
Yup, its the comment lines, let me play with that.
 
Regards,

A. Jorge Garcia

Teacher & Professor
Applied Mathematics, Physics & Computer Science
Baldwin Senior High School & Nassau Community College

Marshall Hampton

unread,
Jan 15, 2009, 9:49:19 PM1/15/09
to sage-support
William Stein added ffmpeg to sagenb, so that isn't a problem now, but
there is a permissions issue with writing to the DATA directory. So
the code still won't work as is on sagenb. I haven't figured out what
a permissible directory is to dump the frames into, maybe someone else
can comment on that.

Cheers,
Marshall

On Jan 15, 6:47 pm, CalcP...@aol.com wrote:
> Yup, its the comment lines, let me play with that.
>
> Regards,
> A. Jorge Garcia
>
> Teacher & Professor
> Applied Mathematics, Physics & Computer Science
> Baldwin Senior High School & Nassau Community College
>
> **************A Good Credit Score is 700 or Above. See yours in just 2 easy
> steps!
> (http://pr.atwola.com/promoclk/100000075x1215855013x1201028747/aol?red...
> cemailfooterNO62)

mabshoff

unread,
Jan 15, 2009, 10:02:57 PM1/15/09
to sage-support


On Jan 15, 6:49 pm, Marshall Hampton <hampto...@gmail.com> wrote:
Hi,

> William Stein added ffmpeg to sagenb, so that isn't a problem now, but
> there is a permissions issue with writing to the DATA directory.  So
> the code still won't work as is on sagenb.  I haven't figured out what
> a permissible directory is to dump the frames into, maybe someone else
> can comment on that.

Just use /tmp or DOT_SAGE/tmp, but create a directory in it so you
don't clobber other jobs of the same kind. It might be a good idea to
add some create_tmpdir() command in Sage that is guaranteed to be
unique and is in a writable directory, i.e. it should first try
DOT_SAGE/tmp, then /tmp and so on. IIRC there is even some env
variable where we can point a per user tmp dir to. So you might want
to open a ticket for the above in order to avoid various people doing
the same thing in their own code over and over again.

> Cheers,
> Marshall

Cheers,

Michael

Calc...@aol.com

unread,
Jan 15, 2009, 10:17:19 PM1/15/09
to sage-s...@googlegroups.com
OK, so I'll just run it on a local notebook().  Either I install Sage on my server or I can try your program on the Sage 3.1.1 live CD?
 
TIA,

A. Jorge Garcia

Teacher & Professor
Applied Mathematics, Physics & Computer Science
Baldwin Senior High School & Nassau Community College
In a message dated 1/15/2009 9:49:45 P.M. Eastern Standard Time, hamp...@gmail.com writes:

William Stein added ffmpeg to sagenb, so that isn't a problem now, but
there is a permissions issue with writing to the DATA directory.  So
the code still won't work as is on sagenb.  I haven't figured out what
a permissible directory is to dump the frames into, maybe someone else
can comment on that.

Justin C. Walker

unread,
Jan 15, 2009, 10:31:30 PM1/15/09
to sage-s...@googlegroups.com

Python has several calls to make temporary files that will not be
clobbered by other users or uses. No need to make directories.

Justin

--
Justin C. Walker, Curmudgeon at Large
Institute for the Absorption of Federal Funds
--
Democracy is two wolves and a lamb
voting on what to have for lunch.
Liberty is a well-armed lamb contesting
the vote.

mabshoff

unread,
Jan 15, 2009, 10:34:19 PM1/15/09
to sage-support


On Jan 15, 7:31 pm, "Justin C. Walker" <jus...@mac.com> wrote:
> On Jan 15, 2009, at 19:02 , mabshoff wrote:

<SNIP>

> > Just use /tmp or DOT_SAGE/tmp, but create a directory in it so you
> > don't clobber other jobs of the same kind. It might be a good idea to
> > add some create_tmpdir() command in Sage that is guaranteed to be
> > unique and is in a writable directory, i.e. it should first try
> > DOT_SAGE/tmp, then /tmp and so on. IIRC there is even some env
> > variable where we can point a per user tmp dir to. So you might want
> > to open a ticket for the above in order to avoid various people doing
> > the same thing in their own code over and over again.
>
> Python has several calls to make temporary files that will not be  
> clobbered by other users or uses.  No need to make directories.

Yes, but we are ending up calling command line tools which to not obey
Python's convention :)

And having all the files in one directory makes debugging a lot easier
and it assures that you get rid of all files by just wiping the one
tmp directory.

> Justin

Cheers,

Michael

Marshall Hampton

unread,
Jan 15, 2009, 10:43:19 PM1/15/09
to sage-support
ok, so this should work on sagenb:

{{{
%sh
mkdir /tmp/username_temp
}}}

{{{
c = polytopes.icosahedron()
label = 'Ico_movie'
DATA = '/tmp/mh_temp/'
# Numerical pi predefined for some speed
npi = RDF(pi)

#A rotation matrix
def rot(i,j,th):
q = matrix(RDF,3)
for k in range(3):
q[k,k] = 1.0
q[i,i] = cos(th)
q[j,j] = cos(th)
q[i,j] = sin(th)
q[j,i] = -sin(th)
return q

index = 0
for theta in srange(0,2*npi,npi/10):
vs = c.vertices()[:]
#rotate the vertices:
vs = [rot(0,1,theta)*vector(v) for v in vs]
edgedata = c.vertex_adjacencies()
facedata = c.triangulated_facial_incidences()

#Set up Tachyon.
#use small xres, yres for speed.
#antialiasing helps things lookbetter.
t = Tachyon(xres = 250, yres = 250, antialiasing = True)
t.texture('e1', color = (0,0,1))
t.texture('f1',opacity = .5, color = (1,0,0))
t.light((2,2,2), 0.1, (1,1,1))

#Draw edges:
edges = []
for anedge in edgedata:
v0 = anedge[0]
for v1 in anedge[1]:
if v1 > v0:
edges.append([vs[v0],vs[v1]])
for anedge in edges:
t.fcylinder(anedge[0],anedge[1],.01,'e1')

#Draw transparent faces:
for atri in facedata:
t.triangle(vs[atri[1][0]],vs[atri[1][1]],vs[atri[1][2]],'f1')

#Make file names in order for ffmpeg
str_index = '0'*(3-len(str(index)))+str(index)
t.save(DATA+label+ str_index + '.png')
index += 1

#Add a black final frame,
#since otherwise the compression in ffmpeggets confused.
t = Tachyon(xres = 250, yres = 250, antialiasing = True)
str_index = '0'*(3-len(str(index)))+str(index)
t.save(DATA+label+ str_index + '.png')

#Use ffmpeg to animate:
os.system('ffmpeg -qmax 2 -i ' + DATA + label + '%3d.png ./test.mp4')
}}}


On Jan 15, 9:34 pm, mabshoff <Michael.Absh...@mathematik.uni-

William Stein

unread,
Jan 15, 2009, 10:52:27 PM1/15/09
to sage-s...@googlegroups.com
On Thu, Jan 15, 2009 at 7:34 PM, mabshoff
<Michael...@mathematik.uni-dortmund.de> wrote:
>
>
>
> On Jan 15, 7:31 pm, "Justin C. Walker" <jus...@mac.com> wrote:
>> On Jan 15, 2009, at 19:02 , mabshoff wrote:
>
> <SNIP>
>
>> > Just use /tmp or DOT_SAGE/tmp, but create a directory in it so you
>> > don't clobber other jobs of the same kind. It might be a good idea to
>> > add some create_tmpdir() command in Sage that is guaranteed to be
>> > unique and is in a writable directory, i.e. it should first try
>> > DOT_SAGE/tmp, then /tmp and so on. IIRC there is even some env
>> > variable where we can point a per user tmp dir to. So you might want
>> > to open a ticket for the above in order to avoid various people doing
>> > the same thing in their own code over and over again.
>>
>> Python has several calls to make temporary files that will not be
>> clobbered by other users or uses. No need to make directories.
>
> Yes, but we are ending up calling command line tools which to not obey
> Python's convention :)
>
> And having all the files in one directory makes debugging a lot easier
> and it assures that you get rid of all files by just wiping the one
> tmp directory.

Just to add to this (or subtract, I'm not sure). Sage doesn't use the
Python infrastructure for creating temporary directories (say under
/tmp), since Sage or its subprocesses tend to get brutally kill -9'd a
lot, etc., and if we used some standard tmp system without care, sage
would end up leaving a lot of crap around. Right now, whenever a Sage
process creates temp directories, it uses the function tmp_dir()
which creates the temp directory in
$DOT_SAGE/temp/hostname/pid/
The Sage cleaner, which is a totally different python process from any
particular Sage you run, will look for processes that have died but
left around temp files, and it will delete them all (it also kills all
subprocesses that were left around). It's finally very good at this,
though it used to not be.

-- William

William Stein

unread,
Jan 15, 2009, 10:54:02 PM1/15/09
to sage-s...@googlegroups.com
On Thu, Jan 15, 2009 at 7:43 PM, Marshall Hampton <hamp...@gmail.com> wrote:
>
> ok, so this should work on sagenb:

For the record, I would be *very* happy if you made it so the
sage animate(...) command could do this automatically. It's
fine that ffmpeg would have to be installed -- just give a graceful
error message if it isn't. Is there a trac ticket for this yet?
It would probably be easy to implement, using the example below and
the current animate code (which does 2d plots and uses imagemagick to
make animated gifs).
--
William Stein
Associate Professor of Mathematics
University of Washington
http://wstein.org

Justin C. Walker

unread,
Jan 16, 2009, 1:49:16 AM1/16/09
to sage-s...@googlegroups.com

On Jan 15, 2009, at 19:34 , mabshoff wrote:

>
>
>
> On Jan 15, 7:31 pm, "Justin C. Walker" <jus...@mac.com> wrote:
>> On Jan 15, 2009, at 19:02 , mabshoff wrote:
>
> <SNIP>
>
>>> Just use /tmp or DOT_SAGE/tmp, but create a directory in it so you
>>> don't clobber other jobs of the same kind. It might be a good idea
>>> to
>>> add some create_tmpdir() command in Sage that is guaranteed to be
>>> unique and is in a writable directory, i.e. it should first try
>>> DOT_SAGE/tmp, then /tmp and so on. IIRC there is even some env
>>> variable where we can point a per user tmp dir to. So you might want
>>> to open a ticket for the above in order to avoid various people
>>> doing
>>> the same thing in their own code over and over again.
>>
>> Python has several calls to make temporary files that will not be
>> clobbered by other users or uses. No need to make directories.
>
> Yes, but we are ending up calling command line tools which to not obey
> Python's convention :)
>
> And having all the files in one directory makes debugging a lot easier
> and it assures that you get rid of all files by just wiping the one
> tmp directory.

I think that the calls to create temp files put all files in the same
directory. But perhaps that isn't quite what you meant :-}

Justin

--
Justin C. Walker, Curmudgeon at Large

Director
Institute for the Enhancement of the Director's Income
-----------
Nobody knows the trouble I've been
-----------

Justin C. Walker

unread,
Jan 16, 2009, 1:52:49 AM1/16/09
to sage-s...@googlegroups.com

If the temp files are truly temp files (i.e., not of interest when the
process that creates them exits), then there are Python calls that
help: the temp files can be created and unlinked, so that at exit,
they vanish. In fact, they are not visible in the file system at all.

Justin

--
Justin C. Walker, Curmudgeon-At-Large


Institute for the Absorption of Federal Funds

--------
If you're not confused,
You're not paying attention
--------

mabshoff

unread,
Jan 16, 2009, 1:58:06 AM1/16/09
to sage-support


On Jan 15, 10:52 pm, "Justin C. Walker" <jus...@mac.com> wrote:
> On Jan 15, 2009, at 19:52 , William Stein wrote:

<SNIP>

Hi,

> If the temp files are truly temp files (i.e., not of interest when the  
> process that creates them exits), then there are Python calls that  
> help: the temp files can be created and unlinked, so that at exit,  
> they vanish.  In fact, they are not visible in the file system at all.

Do you mean this?

"os.tmpfile()
Return a new file object opened in update mode (w+b). The file has no
directory entries associated with it and will be automatically deleted
once there are no file descriptors for the file. Availability: Unix,
Windows"

I don't see how this will work when I run "kill -9 $SOME_PID"

> Justin

Cheers,

Michael

Justin C. Walker

unread,
Jan 16, 2009, 2:32:12 AM1/16/09
to sage-s...@googlegroups.com

On Jan 15, 2009, at 22:58 , mabshoff wrote:

>
>
>
> On Jan 15, 10:52 pm, "Justin C. Walker" <jus...@mac.com> wrote:
>> On Jan 15, 2009, at 19:52 , William Stein wrote:
>
> <SNIP>
>
> Hi,
>
>> If the temp files are truly temp files (i.e., not of interest when
>> the
>> process that creates them exits), then there are Python calls that
>> help: the temp files can be created and unlinked, so that at exit,
>> they vanish. In fact, they are not visible in the file system at
>> all.
>
> Do you mean this?
>
> "os.tmpfile()
> Return a new file object opened in update mode (w+b). The file has no
> directory entries associated with it and will be automatically deleted
> once there are no file descriptors for the file. Availability: Unix,
> Windows"
>
> I don't see how this will work when I run "kill -9 $SOME_PID"

It works because there are no directory entries for it. It is just
held open by a file descriptor in the kernel. When the process exits,
the link count goes to zero (virtually), and the file structure [which
is on-disk, but not represented in any directory on-disk] is deleted.
Pas problem.

Justin

--
Justin C. Walker, Curmudgeon-At-Large

Institute for the Enhancement of the Director's Income
--------

Experience is what you get
when you don't get what you want.
--------

Marshall Hampton

unread,
Jan 16, 2009, 9:02:12 AM1/16/09
to sage-support
We do have ticket #1483, http://trac.sagemath.org/sage_trac/ticket/1483,
which could be warped into adding support for ffmpeg. I am also
interested in learning how to use javascript to do it but my
javascript skills are still too low. One of my goals for this year is
to become much better at javascript to help more with the notebook.

I simply don't have the time to work on this right now but I would
like to soon (i.e. in the next 3 months or so). I will assign #1483
to myself in order to keep it on my radar.

Marshall

On Jan 15, 9:54 pm, William Stein <wst...@gmail.com> wrote:

Carl Witty

unread,
Jan 16, 2009, 12:36:10 PM1/16/09
to sage-support
On Jan 15, 10:52 pm, "Justin C. Walker" <jus...@mac.com> wrote:
> If the temp files are truly temp files (i.e., not of interest when the  
> process that creates them exits), then there are Python calls that  
> help: the temp files can be created and unlinked, so that at exit,  
> they vanish.  In fact, they are not visible in the file system at all.

This works for files which are meant to be written and read inside the
same process (some sort of manual swap file, say); but in our case the
goal is to send the file from the Sage process to the ffmpeg process,
and this technique doesn't work in that case.

Carl

mabshoff

unread,
Jan 16, 2009, 12:40:58 PM1/16/09
to sage-support


On Jan 16, 9:36 am, Carl Witty <carl.wi...@gmail.com> wrote:
> On Jan 15, 10:52 pm, "Justin C. Walker" <jus...@mac.com> wrote:

Hi,

> > If the temp files are truly temp files (i.e., not of interest when the  
> > process that creates them exits), then there are Python calls that  
> > help: the temp files can be created and unlinked, so that at exit,  
> > they vanish.  In fact, they are not visible in the file system at all.
>
> This works for files which are meant to be written and read inside the
> same process (some sort of manual swap file, say); but in our case the
> goal is to send the file from the Sage process to the ffmpeg process,
> and this technique doesn't work in that case.

Yep, unfortunately this is the case. I did not know about the above
trick and it is neat, but kind of difficult in case you actually have
to debug the problem. I can also imagine all kinds of nefarious things
one could do with that type of tmpfile.

> Carl

Cheers,

Michael

Justin C. Walker

unread,
Jan 16, 2009, 1:09:44 PM1/16/09
to sage-s...@googlegroups.com

Yup. If you need the files outside the creating process, you do need
to use a different call. But there is a tempfile procedure in Python
that does just what is needed (creates a visible-in-the-filesystem
file with a unique name).

Justin C. Walker

unread,
Jan 16, 2009, 1:10:28 PM1/16/09
to sage-s...@googlegroups.com

Yeah; those old Unix guys (Ken and Dennis) thought of everything :-}

Justin

--
Justin C. Walker, Curmudgeon at Large
Director
Institute for the Enhancement of the Director's income
-----------
--
They said it couldn't be done, but sometimes,
it doesn't work out that way.
- Casey Stengel
--

Reply all
Reply to author
Forward
0 new messages