Beginner's tutorial

Requirement

Download & Install OpenAlea : download

First step

Start Visualea

  • On Windows : Start Menu → OpenAlea → Visualea
  • On Linux (in a shell) : [prompt ~]$visualea

Create and save your first graph

This video shows:

  • How to create a simple expression by connecting nodes
  • How to save your work

A more complete example

In this tutorial, we will see how to implement a simple modeling problem in Visualea.

The problem

We measured some tree data, saved in a tabbed editor (like excel). These data have been exported in a CSV file.

⇒ We want to have a simple 3D representation of the measured tree.

The data

The CSV data are :

X	Y	crown_up	crown_bot	trunk_diameter
0	0	10	        20      	2
10	12	12	        18      	3
20	22	8	        23	        3,4
0	18	14	        22      	2,5
  • Download :stand.csv
  • Alternatively, within openalea, this data set is also available as a data node (type stand.csv in the search panel).

Prerequisite

You need to intall on your system the following openalea packages :

  • OpenAlea.Visualea
  • OpenAlea.Catalog
  • PlantGL
  • OpenAlea.Plottools

Use OpenAlea installer to install these packages.

You will need also standard scientific python packages :

  • Matplotlib
  • Scipy

Use OpenAlea installer / Install Other Egg to install these packages

Step 1 : Create your own package

First of all, we need to create a package where to put your work (dataflow, node definition, data, …). A package is in fact a simple directory containing python files.

Create a package
  1. Select Menu package Manager → Create → Package
  2. Fill the form:
    • Name : standbuilder
    • Description : build stand representation from measured data
    • Version : 0.1
    • License : Cecill-C
    • Authors : All collaborators and package writer
    • Institutes : …
    • URL : …
    • Path : /home/myhome/openalea_pkg
  3. Click OK

Your new package should appear in the package manager.

Nota : the path correspond to directory where the python file will be written. Choose it carefully in order to be able to find it later.

Step 2 : Read CSV data

Create a dataflow to read and view a file
  1. Open the openalea folder in the package manager (left column)
  2. Open the file sub folder : should see a list of node like read or filename
  3. Let the mouse pointer on a node : a tip shows informations about the object
  4. Drag the filename node to the workspace (the white page on the right)
  5. The filename box is on the workspace. It has one input and one output (yellow circle)
  6. Let the mouse pointer on the node : a tip shows informations about the component
  7. Let the mouse pointer on an input or on an output (yellow circle) : you can read, the port description and its value
  8. Drag the read node from the package openalea.file to the workspace
  9. Connect the filename output to the read input : drag the mouse pointer from a port to the other.
  10. Do the same operation in order to add the node openalea.data structure.text on the workspace
  11. Connect its input to the output of the read node

View the file contents
  1. Right click on the the filename and select Open Widget
  2. Select the file you want to read (the csv file)
  3. Right click on the text node, and select Run
  4. Right click on the text node, and select Open Widget
Build a CSV object

In order to manipulate the CSV data, we are going to build a CSV object.

  1. Select the search tab in the package manager
  2. Type CSV
  3. Drag the read csv node on the workspace
  4. Do the same to instantiate the getitem object (catalog.python.getitem)
  5. Connect the read output to the read csv input, and its output to the getitem first input
  6. Add an int object on the workspace, and connect its output the second input of getitem
  7. Execute the graph by selecting Run in the context menu of the getitem node
  8. Print the output in the shell : Right click on the output port, and select print

Save your work
  1. Menu Workspace → Save as composite node (CTRL + E)
  2. In the selector dialog, click 'New' Button
  3. In the new dialog,
    • Select the standbuilder package in the combo box
    • Enter the name : readcsv_1
    • Add a description : Read data file
    • Click Ok
  4. In the selector, click Ok button
  5. The new graph should appear in the standbuilder package.

Step 3 : Create a simple 3D representation of one tree

Before displaying the whole stand, we must rebuild a tree. In this tutorial we build a very simple tree representation composed by a sphere for the crown and a cylinder for the trunk.

Create a 3D object

This simple dataflow shows how to display a scene object.

  1. First step, we create a new workspace : Select Menu Workspace → New Empty Workspace (CTRL+W)
  2. Create the following dataflow by using PlantGL nodes
    • plantgl.objects.cylinder creates a cylinder
    • plantgl.objects.translated moves the input object
    • catalog.data.tuple3 to set the translation vector
    • plantgl.visualization.plot3d to view the result

Create a simple tree

To build our tree, we must construct a PlantGL scene containing a cylinder and a sphere.

  1. Modify the previous dataflow as follow:
    • Add a plantgl.objects.sphere object
    • Add a plantgl.objects.translated object
    • Add a plantgl.objects.scene object
    • Connect the 2 translated objects to a plantgl.objects.scene object
  2. Save this dataflow in your standbuilder package as simple_tree

Step 4 : Create a Macro Node / Group Nodes

We will need to use the previous dataflow to build trees. To simplify this procedure, we would like to use a simple node and not a complex dataflow. For that we are going to embed the previous dataflow in a composite node (also named macro node).

Transform simple_tree to a reusable composite node
  1. Open simple_tree in a new workspace (double click in the package manager on the simple_tree graph)
  2. Select Menu Workspace → Configure I/O
  3. Add 5 inputs with the + button :
    • Name - Type - Default value - Description
    • X - IInt - 0 - X position
    • Y - IInt - 0 - Y position
    • crown_up - IFloat - 16.0 - Top of the crown
    • crown_bot - IFloat - 8.0 - Bottom of the crown
    • trunk_dia - IFloat - 3.0 - Trunk diameter
  4. Add 1 output with the + button
    • scene - None - PlanGL scene
  5. Click OK
  6. Modify the graph as follow
    • Connect input 0 and 1 to the X and Y nodes
    • Connect input 2 and 3 to a minus node catalog.math.-, and connect the result to the crown radius
    • Connect input 5 to the trunk radius
    • Connect input 3 to the crown bottom
  7. Save your work as a new composite node in standbuilder named tree_scene

Using the new composite node in a dataflow
  1. Open our first dataflow readcsv_1 in the standbuilder package (doucke click)
  2. Drag the node standbuilder.tree_scene on the new workspace
  3. Add 5x getitem and 5x string object
  4. Connect the nodes as the picture in order to retrieve to different object properties
  5. Add a plangl.visualization.plot3D object and connect it to the output of tree_scene
  6. Run the dataflow several times and change the value of the first getitem (object index)
  7. Save the dataflow in the standbuilder package as readcsv_2

Create a composite node by grouping nodes
  1. Select the 5 get item and their associated string object
  2. Click on Menu Workspace → group (CTRL+G)
  3. Run the dataflow
  4. Save it in the standbuilder package as readcsv_3

Step 5 : Get the spatial distribution of the trees

We want to extract from the csv object the X and Y properties and plot them in 2D.

Extract data
  1. Create a new workspace (CTRL+W)
  2. Add a read node and a csv2objs node to read a csv file.
  3. Set the file to read by opening the read widget (open widget)
  4. Run and Display the output (output port context menu → print or tooltip) : it's a list of obj
  5. Add a getitem node and a int node to select an object in the list
  6. Add an extract node and 2 strings node to select properties in a particular object.
  7. Set the 2 string object to X and Y
  8. Run and display the output (output port context menu → print or tooltip) : it's a list containing the X and the Y properties of the selected object.

Implement iterative process

We want to do the same thing, but for all the CSV objects contained in the file.

  1. Remove the getitem and the int nodes (with suppr)
  2. Add a catalog.functioanl.map
  3. Connect the output of extract to the first input of map
  4. Connect the output of csv2objs to the second input of map
  5. Add a system.X node and connect its output the first input of extract
  6. Run the map object and display the result

Nota : The X object represents a function variable. The map apply a function to each element received in its second input.

Plot 2D
  1. Add the nodes plottools.VS Plot and plottools.tuples2seq on the workspace
  2. Connect the map output the input of tuples2seq and the last output the VS Plot node.
  3. Run the dataflow
  4. Save it in the standbuilder package as plot_csv

Step 6 : Apply the process to multiple trees

In this step, we used the same method to build the entire stand

  1. Open the readcsv_3 dataflow
  2. Modify it in order to plot in 3D all the tree contained in the file and not only one
    • Use a system.X node and a catalog.functional.map node
  3. Save this work in your standbuilder package as plot_stand

Step 7 : Generate a PNG image

We want to generate an image of the 3D scene. Unfortunately there is no PlantGL node to achieve this task. We need to implement our own python function to do that.

Create a python node
  1. Select Menu Package Manager → Create → Python node
  2. Fill the dialog :
    • Package : standbuilder
    • Name : scene writer
    • Category : visualisation
    • Description : write a PlantGL scene to a Png file.
  3. Click on Inputs/Outputs
  4. Add 2 inputs with the + button
    • scene - None - None - the scene to write
    • filename - IFileStr - None - The file to write
  5. Add 1 output with the + button
    • filename - IFileStr - None - The written file
  6. Click OK on both dialog
  7. The new node should appear in the package manager
Edit the code of the node
  1. Rigth click on the node in the package manager
  2. Select Edit : it opens a code editor
    • If your want to use your own editor, use preference dialog (Menu Window→Preferences)) to set it up.
  3. Edit the code :
 
from openalea.plantgl.all import Viewer
 
class scene_writer(object):
    """  Write a plantgl scene to a PNG file """ 
 
    def __call__(self, *inputs):
 
        scene = inputs[0]
        fname = inputs[1]
        Viewer.display(scene)
        Viewer.camera.lookAt((0,0,300),(0,0,0))
        Viewer.frameGL.saveImage(fname)
 
        return fname
Use the node
  1. Open the plot_stand dataflow (double click in the package tree)
  2. Drag the node scene writer from the package manager to the workspace.
  3. Connect the Scene output to the first input of scene_writer
  4. Add and Connect catalog.file.filename node to the second input of scene_writer
  5. Choose a filename
  6. Execute the dataflow by clicking on Run in the context menu of scene_writer
  7. The image file have been created
  8. Save the composite node as plot_stant (overwrite existing one)

Modify the node

Now we want to modify the code of the node. For instance, we want to change the camera position.

  1. Edit the node code by doing a right click on the node in the package tree and select Edit
  2. Modify the code : for instance, change the 300 by a 500
  3. Save the python file
  4. Reload the package : Right click on the standbuilder package in the tree view, and click Reload : this action reload all python modules in the package, but doesn't affect the nodes in the workspace.
  5. Reload the workspace : Menu Workspace → Reload (Warning : all the modifications which are not saved will be lost).
Exercise
  • Modify the python node in order to be able to set the camera position.

Step 8 : Apply the process to several files

Create a composite node to generate an PNG image from a CSV
  1. Open the plot_stand dataflow
  2. Save it under a new name : plot_stand_func
  3. Add an input and an output
    • The input is the CSV filename (interface IFileStr)
    • The output is the generated PNG
  4. Connect the dataflow input (in)) to filename
  5. Connect the scene_writer output the dataflow output (out))
  6. Add a catalog.string.join node and connect it to the scene_writer second input
  7. Open the join widget, and delete the join character (by default, it is a 'space')
  8. Add a catalog.data.string node, set its value to '.png' and connect it to the first input of join
  9. Save the modification (CTRL+E)

Get a set of file
  1. Open a new workspace (CTRL+W)
  2. Add nodes to get a list of filenames contained in a directory :
    • catalog.file.glob
    • catalog.file.joinpath
    • catalog.file.dirname
    • catalog.data.string
  3. Connect them as shown on the picture
  4. Set the string to *.csv
  5. Set the dirname to directory to scan
  6. Run the glob and print the result

Create an image for each file
  1. Add a plot_stand_func node
  2. Add a catalog.functional.map and a system.X
  3. Connect them in order to execute the plot_stand_func node for each file (see picture)
  4. Save the dataflow in the standbuilder package as gen_images
  5. Execute the dataflow. For each csv file in the specified directory, a png image is generated.

Step 9 : Share your work with your colleagues

Share the ''standbuilder'' package
  1. Get the directory you saved your package (Right click on the package → meta informations)
  2. Copy this directory, or zip/tar it
Import a package
  1. Copy the directory on an other system, or unzip/untar it
  2. Select Menu Package Manager → Load Package/Directory : the package should appear in the tree view.
 
documentation/tutorials/beginners.txt · Last modified: 2008/12/19 14:02 by user   Back to top
INRIA   INRA     CIRAD
INRIA GForge RSS feed Valid XHTML 1.0 Valid CSS Driven by DokuWiki