help scripting to export figures as jpg...

Questions about Anatomist manipulation

Moderators: denghien, riviere

SylvainT
Posts: 73
Joined: Mon Feb 13, 2006 6:37 pm
Location: INT, Marseille, France

help scripting to export figures as jpg...

Post by SylvainT »

Hi there,

I have a pyanatomist script which helps me do systematic visualization of objects I'm creating... I have a lot of them (several hundreds), and I'd like to be save a jpg for each of them within my script, as seen from a Fixed point of view... In details, I need to do three operations that I dunno how to do for now in my script, which are the equivalent of using the following menu items of my 3D Window:

1. Scene - Tools - uncheck 'Display Cursor' (I don't want any cursor visible)
2. Scene - Fixed point of view - choose one of them
3. Window - Save, to export this standardized view as a jpg or png or whatever...

Is this possible? If yes, what are the commands in the pyanatomist api?

Thanks in advance for your help,

Sylvain
User avatar
riviere
Site Admin
Posts: 1361
Joined: Tue Jan 06, 2004 12:21 pm
Location: CEA NeuroSpin, Saint Aubin, France
Contact:

Re: help scripting to export figures as jpg...

Post by riviere »

Hi Sylvain,

Yas, all this is possible.

1. You can disable the cursor display globally in all windows, like in the config panel:

Code: Select all

import anatomist.api as ana
a = ana.Anatomist()
a.config()[ 'linkedCursor' ] = 0
See http://brainvisa.info/doc/anatomist/htm ... onfig.html for config options.

2. Each point of view corrresponds to a specific view and/or volume slice orientation, given by a quaternion. the easiest is to set a window in the wanted orientation, then ask for its orientation:

Code: Select all

win = a.createWindow( '3D' )
# ... let the user set the orientation
winf = win.getInfos()
# view orientation:
worient = winf[ 'view_quaternion' ]
# set it, with zoom and slice orientation, to another window
win2 = a.createWindow( 'Axial' )
win2.camera( view_quaternion=worient, zoom=winf['zoom'], 
  slice_quaternion=winf['slice_quaternion'], observer_position=winf['observer_position'] )
See the pyanatomist API docs: http://brainvisa.info/doc/pyanatomist/sphinx/index.html

3. Snapshot

Code: Select all

a.camera( force_redraw=True ) # may be needed to ensure the view is updated before the snapshot
a.execute( 'WindowConfig', windows=[win2], snapshot='/tmp/ana_snapshot.jpg' )
This is done using a low-level command ( a.execute() ) because we forgot to make a proper python binding to this function...
See http://brainvisa.info/doc/anatomist/htm ... mands.html

Denis
User avatar
riviere
Site Admin
Posts: 1361
Joined: Tue Jan 06, 2004 12:21 pm
Location: CEA NeuroSpin, Saint Aubin, France
Contact:

Re: help scripting to export figures as jpg...

Post by riviere »

Oh I forgot to tell that after setting the config opton 'linkedCursor', you have to update its state:

Code: Select all

a.config()[ 'linkedCursor' ] = 0
a.config().apply()
And you can also disable the cursor in a specific window:

Code: Select all

a.execute( 'WindowConfig', windows=[win2], cursor_visibility=0 )
Denis
SylvainT
Posts: 73
Joined: Mon Feb 13, 2006 6:37 pm
Location: INT, Marseille, France

Re: help scripting to export figures as jpg...

Post by SylvainT »

Thanks Denis for your help! That does everything I need! And I didn't know about some of the links you mentioned, so there's tons of info in there that I'm sure will be useful ;)

Just one detail: this did not work...
riviere wrote:

Code: Select all

a.camera( force_redraw=True )
...but I just had to include the 'force_redraw=True' as an extra argument to the previous a.camera() call
SylvainT
Posts: 73
Joined: Mon Feb 13, 2006 6:37 pm
Location: INT, Marseille, France

Re: help scripting to export figures as jpg...

Post by SylvainT »

another question...

I'm doing something like...

Code: Select all

from soma import aims
import anatomist.direct.api as anatomist
a = anatomist.Anatomist()
mesh = aims.SurfaceGenerator.sphere(aims.Point3df(0,0,0), 1, 500)
asphere = a.toAObject(mesh)
and then I need to delete the asphere object from anatomist (coz in fact I have thousands of them, and at some point, I need to clear things up!); but none of the commands I've tried work... (a.deleteObjects, a.deleteElements; even a.close does not seem to work properly)

it seems a.deleteObjects work when I do import anatomist.api as anatomist (without direct); but then, the method a.toAObject() does not exist so I cannot do what I need to do with my spheres...

any hint?
User avatar
riviere
Site Admin
Posts: 1361
Joined: Tue Jan 06, 2004 12:21 pm
Location: CEA NeuroSpin, Saint Aubin, France
Contact:

Re: help scripting to export figures as jpg...

Post by riviere »

Hi Sylvain,
There is a reference-counting system in Anatomist, with several referencing modes. Normally the anatomist application holds a reference to every object, which forbids to delete them. In python bindings, the python layer also keep references. In pyanatomist we chose to behave like standard python objects: objects are destroyed when no longer unsed in python. This is not the behaviour of the toAObject() function (not very consistent, but it was generally more convenient this way).
However, if you don't understand what I say (which is probably the case), the short solution is to use releaseAppRef() on the object to release the application reference, and thus allow python-like behaviour:

Code: Select all

asphere = a.toAObject(mesh)
asphere.releaseAppRef()
# now if you del asphere, it will be sdestroyed
Denis
SylvainT
Posts: 73
Joined: Mon Feb 13, 2006 6:37 pm
Location: INT, Marseille, France

Re: help scripting to export figures as jpg...

Post by SylvainT »

Thanks Denis, that works!!

by curiosity, what's the difference between

Code: Select all

import anatomist.api as anatomist
and

Code: Select all

import anatomist.direct.api as anatomist
And are there circumstances when one should use one or the other?
User avatar
riviere
Site Admin
Posts: 1361
Joined: Tue Jan 06, 2004 12:21 pm
Location: CEA NeuroSpin, Saint Aubin, France
Contact:

Re: help scripting to export figures as jpg...

Post by riviere »

There are 2 different implementations of the Anatomist API: one using python-C++ bindings, and another using a socket connection.
See http://brainvisa.info/doc/pyanatomist/sphinx/index.html for docs and explanations.
import anatomist.api selects a default implementation, normally the direct one, and in brainvisa we use the socket API on Unix.
import anatomist.direct.api or import anatomist.socket.api forces a specific implementation.
The direct implementation brings more functionalities: direct library access in shared memory, which allows low level operations such as links with pyaims. The socket implementation is more flexible and allows a client app to survive to a crash in Anatomist (on server side), and is thread-safe. A third pseudo-implementation (import anatomist.threaded.api) allows to use the direct implementation through a thread-safe (single threaded, actually) wrapping.
Here you need the direct (or threaded) implementation since you are building low-level meshes.

Denis
SylvainT
Posts: 73
Joined: Mon Feb 13, 2006 6:37 pm
Location: INT, Marseille, France

Re: help scripting to export figures as jpg...

Post by SylvainT »

Hi Denis,

I revive an old thread because I need an aditional feature for exporting my figures (I still use everything you explained above regularly, so thanks again!!)... I just need to activate the texture option available in the Texturing contextual menu, which is "RGB space interpolation" (coz I need to display a parcelation, i.e a label texture)... Can I do that via scripting? I spotted the texture_object.glSetTexRGBInterpolation() function; I set it to True, but there's no update of the display and I dunno what to do next...

Cheers,

Sylvain

ps: havn't had time to follow up on the BrainVisa / Freesurfer problem in BV 4.4, sorry... will do at some point...
User avatar
riviere
Site Admin
Posts: 1361
Joined: Tue Jan 06, 2004 12:21 pm
Location: CEA NeuroSpin, Saint Aubin, France
Contact:

Re: help scripting to export figures as jpg...

Post by riviere »

Hi Sylvain,
You can either:
- use the glSetTexRGBInterpolation() method, and then use texture_object.notifyObservers() to trigger update of views and other objects
- or use Anatomist().execute("TexturingParams", objects=[texture_object], interpolation=True)
Denis
SylvainT
Posts: 73
Joined: Mon Feb 13, 2006 6:37 pm
Location: INT, Marseille, France

Re: help scripting to export figures as jpg...

Post by SylvainT »

thanks a lot, this works perfectly !
;)
riviere wrote:Hi Sylvain,
You can either:
- use the glSetTexRGBInterpolation() method, and then use texture_object.notifyObservers() to trigger update of views and other objects
- or use Anatomist().execute("TexturingParams", objects=[texture_object], interpolation=True)
Denis
SylvainT
Posts: 73
Joined: Mon Feb 13, 2006 6:37 pm
Location: INT, Marseille, France

Re: help scripting to export figures as jpg...

Post by SylvainT »

Hello Denis,

One more time reviving an old thread coz I'd like to have an extra feature to create figures via scripting. Thanks to my script, I display a Fusion object (a textured mesh) in a 3D window... The new thing is that my texture is a time-texture, or a multi-texture (I'm not sure how to call it), with several arrays of data. My window now has a slider on the right to choose which array of the texture should be displayed. And I'd like to be able to control the value of this slider through scripting... Is this possible?

Thanks,

Sylvain
User avatar
riviere
Site Admin
Posts: 1361
Joined: Tue Jan 06, 2004 12:21 pm
Location: CEA NeuroSpin, Saint Aubin, France
Contact:

Re: help scripting to export figures as jpg...

Post by riviere »

Hi Sylvain,
I guess you are speaking of a texture with a timestep cursor (multitexture displays several textures mixed at the same time). Time is th 4th component of coordinates in anatomist, so to display a given timestep you just have to move the "cursor" to any position with this timestep in a window:

Code: Select all

import anatomist.api as ana
a = ana.Anatomist()
mesh = a.loadObject('mesh.gii')
texture = a.loadObject('texture.gii')
tex_mesh = a.fusionObjects([mesh, texture], method='FusionTexSurfMethod')
win = a.createWindow('3D')
win.addObjects(tex_mesh)
for t in xrange(a.toAimsObject(tex2).size()):
    win.moveLinkedCursor((0, 0, 0, t))
    a.execute('WindowConfig', windows=[win], snapshot='/tmp/snapshot_%d.jpg' % t)
Denis
SylvainT
Posts: 73
Joined: Mon Feb 13, 2006 6:37 pm
Location: INT, Marseille, France

Re: help scripting to export figures as jpg...

Post by SylvainT »

riviere wrote:Hi Sylvain,
I guess you are speaking of a texture with a timestep cursor (multitexture displays several textures mixed at the same time). Time is th 4th component of coordinates in anatomist, so to display a given timestep you just have to move the "cursor" to any position with this timestep in a window:

Denis
Hi Denis, I finally got back to this... Yes indeed, I meant "time-texture". And thanks, this works perfectly!

Sylvain
SylvainT
Posts: 73
Joined: Mon Feb 13, 2006 6:37 pm
Location: INT, Marseille, France

Re: help scripting to export figures as jpg...

Post by SylvainT »

Hi Denis, one more question... ;)

I need to manually (with the python API) create a color palette that has a transparent component...

I've read everything there and found this command to define the RGB colors of a color palette:

Code: Select all

p.setColors(colors=[100,0,0]*20+[0,100,0]*20+[0,0,100]*20)
If I add a color like [-1,-1,-1], this allows for fusion-type palette (which I already use and is super useful), but now, I need a transparant component to my palette (like with the 'Yellow-red-transp' palette that comes with anatomist). Is this possible?

Thanks,

Sylvain
Post Reply