OpenAlea.Numpy User Guide#
Introduction#
This package is a pure VisuAlea package that provides a graphical interface to Numpy , which provides powerful numerical arrays objects, and routines to manipulate them.
The examples below are inspired from NumPy quickstart.
Prerequisites#
Before reading this tutorial you should know a bit of VisuAlea.
An basic example#
We define the following array:
>>> import numpy
>>> a = numpy.arange(10).reshape(2,5)
>>> a
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
In VisuAlea, you first need to use the arange node. Let us drag and drop this node in the workspace (see Fig 1).
By default, the connectors start, stop and step of arange are equal to 0, so set the connector stop to 10 as shown in Fig 1.
Then, you need to gives a new shape to the array without changing its data. For that, you can select the reshape node and set the connector newshape to (2,5).
Now, it is time to run the dataflow. Press Ctrl+R or right click on the reshape node and select run.
Figure 1: Simple creation of an array in VisuAlea#
We have just created an array object. The array a has several attributes –or properties :
>>> a.shape
(2, 5)
>>> a.ndim
2
>>> a.size
10
>>> a.dtype
'float64'
>>> a.itemsize
8
In VisuAlea, you can use the following nodes to get these properties as shown in Fig 2.
Figure 2: Properties of an array in VisuAlea#
Array Creation#
There are many ways to create arrays. For example, you can create an array from a regular Python list or tuple using the array function.
>>> a = array( [2,3,4] )
>>> a
array([2, 3, 4])
In VisuAlea, you can create an array from a regular Python list or tuple using the array node as follow :
Figure 3: Array creation in VisuAlea#
array node transforms sequences of sequences into two dimensional arrays, and it transforms sequences of sequences of sequences into three dimensional arrays, and so on.
Figure 4: Creation of two dimensional array in VisuAlea#
Once we have an array we can take a look at its attributes:
Figure 5: Properties of two dimensional array in VisuAlea#
The type of the array can also be explicitly specified at creation time:
Figure 6: Type of the array in VisuAlea#
The function array is not the only one that creates arrays. Usually the elements of the array are not known from the beginning, and a placeholder array is needed. There are some functions to create arrays with some initial content. By default, the type of the created array is float64.
The function zeros creates an array full of zeros, and the function ones creates an array full of ones.
>>> zeros( (3,4) ) # the parameter specifies the shape
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
>>> ones( (2,3,4), dtype=int16 ) # dtype can also be specified
array([[[ 1, 1, 1, 1],
[ 1, 1, 1, 1],
[ 1, 1, 1, 1]],
[[ 1, 1, 1, 1],
[ 1, 1, 1, 1],
[ 1, 1, 1, 1]]], dtype=int16)
In VisuAlea, the following functions are implemented as shown in Fig 7.
Figure 7: Creation of a zeros array and a ones array in VisuAlea#
The function empty creates an array without filling it in. Then the initial content is random and it depends on the state of the memory.
Figure 8: Empty array in VisuAlea#
To create sequences of numbers, VisuAlea provides the function arange which is analogous to range that returns arrays instead of lists :
Figure 9: Arange array in VisuAlea#
Using arange with floating point arguments, it is generally not possible to predict the number of elements obtained (because of the floating point precision). For this reason, it is usually better to use the function linspace that receives as an argument the number of elements that we want, instead of the step:
Figure 10: Linspace array in VisuAlea#
Meshgrid can easily be created using the meshgrid().
Figure 11: Meshgrid usage in VisuAlea#
Basic Operations#
Arithmetic operators on arrays apply elementwise. A new array is created and filled with the result.
Figure 12: Subtraction of two arrays in VisuAlea#
Figure 13: Multiplication using a integer and the sinus of an array in VisuAlea#
Figure 14: Check if the values of an array is less than the value 35 in VisuAlea#
Unlike in many matrix languages, the product operator * operates elementwise in NumPy arrays. The matrix product can be performed using the dot node :
Figure 15: Matrix product in VisuAlea#
Many unary operations, like computing the sum of all the elements in the array, are implemented as nodes in VisuAlea :
Figure 16: Unary operations in VisuAlea#
By default, these operations apply to the array as if it were a list of numbers, regardless of its shape. However, by specifying the connector axis you can apply an operation along the specified axis of an array:
Figure 17: Using of the connector axis in VisuAlea#
See nodes available within VisuAlea in numpy.math package.
Indexing, Slicing and Iterating#
One dimensional arrays can be indexed, sliced and iterated over pretty much like lists and other Python sequences.
>>> a = arange(10)**3
>>> a
array([ 0, 1, 8, 27, 64, 125, 216, 343, 512, 729])
>>> a[2]
8
In VisuAlea, you need to use the nodes slice and getitem as follow :
Figure 18: Basic slicing in VisuAlea#
As the same way, the following example :
>>> a = arange(10)**3
>>> a
array([ 0, 1, 8, 27, 64, 125, 216, 343, 512, 729])
>>> a[2:5]
array([ 8, 27, 64])
can be done in VisuAlea :
Figure 19: Basic slicing in VisuAlea#
Reversed an array :
Figure 20: Reverse an array in VisuAlea#
Slicing multidimensional arrays :
Figure 21: Slicing multidimensional arrays in VisuAlea#
Iterating over multidimensional arrays is done with respect to the first axis:
Shape Manipulation#
Changing the shape of an array#
An array has a shape, given by the number of elements along each axis. The shape of an array can be changed with various nodes :
Figure 22: Changing the shape of an array in VisuAlea#
Stacking together different arrays#
Several arrays can be stacked together, along different axes:
Figure 23: Stacking together different arrays in VisuAlea#
See nodes available within VisuAlea in numpy.manipulation package.