next up previous contents
Next: ColumnFunctionView Up: Views Previous: FunctionView   Contents

RowFunctionView

The RowFunctionView is analogous to the FunctionView, except that it operates on a full row of data at a time. The function passed into the construction must be capable of taking in a dataset object along with a row number and returning a vector equal in length to the number of columns in the view.

For our first example, we create a function which normalizes the rows. For this we'll need the Numeric module. This can be imported via the following command.

>>> import Numeric

Now let's construct our view and give it a name. Notice how similar this is to the creating of the FunctionView described in the previous example.

>>> def row_norm(ds, row):
       x = ds.getRowData(row)
       return x / Numeric.sqrt(Numeric.sum(x*x))
>>> rfv = RowFunctionView(ds, row_norm)
>>> rfv.getData()
[[ 0.57735027, 0.57735027, 0.57735027,]
 [ 0.57735027, 0.57735027, 0.57735027,]
 [ 0.57735027, 0.57735027, 0.57735027,]]
>>> rfv.setName('Row Normalized')
>>> rfv
RowFunctionView: Row Normalized, 3 by 3

All the row vectors were normalized to the same vector, which is correct since they were scalar multiples of each other. Now, let's create another, more specialized RowFunctionView. This view will only operate on vectors in $\mathbb{R}^3$ and convert from Cartesian to Spherical coordinates. We'll also give it a name.

>>> def cart2sph(ds, row):
       x = ds.getRowData(row)
       rho = Numeric.sqrt(Numeric.sum(x*x))
       theta = math.atan2(x[1], x[0])
       phi = math.acos(x[2] / rho)
       return [rho, theta, phi]
>>> c2s_view = RowFunctionView(ds, cart2sph)
>>> c2s_view.getData()
[[ 1.73205081, 0.78539816, 0.95531662,]
 [ 3.46410162, 0.78539816, 0.95531662,]
 [ 5.19615242, 0.78539816, 0.95531662,]]
>>> c2s_view.setName('Cartesian to Spherical')
>>> c2s_view
RowFunctionView: Cartesian to Spherical, 3 by 3

Just by inspecting this view we can see that the original dataset differ only in their magnitudes. Of course, this is the same information gotten from the row normalization, but it does help to illustrate that there are multiple ways to extract the same information from a dataset.

Now, if we look at the original dataset, it has two views attached to it. In the previous section we removed the views by passing in a reference to the target view. However, if we have lost or overwritten the variable that contains the view instance, how can we properly remove it? As alluded to, we can find a view by name using the getView() method. So, removing our two views, even if we don't have a reference to them, is easy.

>>> ds.getViews()
[RowFunctionView: Row Normalized, 3 by 3, 
 RowFunctionView: Cartesian to Spherical, 3 by 3]
>>> ds.removeView(ds.getView('Row Normalized'))
>>> ds.removeView(ds.getView('Cartesian to Spherical'))
>>> ds.getViews()
[]


next up previous contents
Next: ColumnFunctionView Up: Views Previous: FunctionView   Contents
Lucas Scharenbroich 2003-08-27