next up previous contents
Next: CachedView Up: Views Previous: RowPCAView   Contents

SortedView

A SortedView provides a mechanism to dynamically alter the order in which data appears. To this end, the SortedView provides the following additional methods:

permuteCols(plist)
Sets the ordering of the columns to an arbitrary permutation relative to the order of the parent dataset.
permuteRows(plist)
Sets the ordering of the rows to an arbitrary permutation relative to the order of the parent dataset.
sortColsByFunction(func)
Sorts the columns in order according to the results of the function. This is not a comparator, but a function which maps a column vector of data to a single scalar value suitable for sorting. The vector may be mapped to a string, integer, real, tuple, or list.
sortRowsByFunction(func)
Sorts the rows of the view in a manner identical to sortColsByFunction().
sort(func)
Left in for backwards-compatibility. Calls sortRowsByFunction().
reset()
Resets the view to its original ordering.

Let's try a few simple examples of sorting a 3x3 random dataset. The first example sorts the rows in 'natural order'. That is, comparing on the first column, then the second, and finally the third. The second example sorts the rows only on the second column. The final example sorts the columns based on their magnitude, and finally the view is reset to its original state and the rows permuted once.

>>> ds = Dataset(MLab.rand(3,3))
>>> sv = SortedView(ds)
>>> sv.getData()
[[ 0.70310301, 0.61336708, 0.29631215,]
 [ 0.942316  , 0.32991922, 0.58734894,]
 [ 0.01950742, 0.57061231, 0.17892206,]]
>>> sv.sortRowsByFunction(lambda x : x.tolist())
>>> sv.getData()
[[ 0.01950742, 0.57061231, 0.17892206,]
 [ 0.70310301, 0.61336708, 0.29631215,]
 [ 0.942316  , 0.32991922, 0.58734894,]]
>>> sv.sortColsByFunction(lambda x : 
... Numeric.sqrt(Numeric.sum(x*x)))
>>> sv.getData()
[[ 0.58734894, 0.32991922, 0.942316  ,]
 [ 0.17892206, 0.57061231, 0.01950742,]
 [ 0.29631215, 0.61336708, 0.70310301,]]
>>> sv.reset()
>>> sv.getData()
[[ 0.70310301, 0.61336708, 0.29631215,]
 [ 0.942316  , 0.32991922, 0.58734894,]
 [ 0.01950742, 0.57061231, 0.17892206,]]
>>> sv.permuteRows([2,1,0])
>>> sv.getData()
[[ 0.01950742, 0.57061231, 0.17892206,]
 [ 0.942316  , 0.32991922, 0.58734894,]
 [ 0.70310301, 0.61336708, 0.29631215,]]

The ability to sort views by arbitrary functions is a powerful feature whose functionality is limited only by the complexity of the sorting functions. The ability to sort based on lists and tuples, allows the functions to provide primary, secondary and tertiary keys for prioritization.


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