1
#--(NumPy Array Example)--------------------------------------------------------
3
This lesson demonstrates how the **TabularEditor** can be used to display
4
(large) NumPy arrays. In this example, the array consists of 100,000 random 3D
5
points from a unit cube.
7
In addition to showing the coordinates of each point, the example code also
8
displays the index of each point in the array, as well as a red flag if the
9
point lies within 0.25 of the center of the cube.
11
As with the other tabular editor tutorials, this example shows how to set up a
12
**TabularEditor** and create an appropriate **TabularAdapter** subclass.
14
In this case, it also shows:
16
- An example of using array indices as *column_id* values.
17
- Using the *format* trait to format the numeric values for display.
18
- Creating a *synthetic* index column for displaying the point's array index
19
(the *index_text* property), as well as a flag image for points close to the
20
cube's center (the *index_image* property).
23
#--<Imports>--------------------------------------------------------------------
35
import HasTraits, Property, Array
38
import View, Item, TabularEditor
40
from traitsui.tabular_adapter \
46
from pyface.image_resource \
49
#--<Constants>------------------------------------------------------------------
51
# Necessary because of the dynamic way in which the demos are loaded:
54
search_path = [ join( dirname( traitsui.api.__file__ ),
55
'demo', 'Advanced' ) ]
57
#--[Tabular Adapter Definition]-------------------------------------------------
59
class ArrayAdapter ( TabularAdapter ):
61
columns = [ ( 'i', 'index' ), ( 'x', 0 ), ( 'y', 1 ), ( 'z', 2 ) ]
67
index_image = Property
69
def _get_index_text ( self ):
70
return str( self.row )
72
def _get_index_image ( self ):
74
if sqrt( (x - 0.5) ** 2 + (y - 0.5) ** 2 + (z - 0.5) ** 2 ) <= 0.25:
78
#--[Tabular Editor Definition]--------------------------------------------------
80
tabular_editor = TabularEditor(
81
adapter = ArrayAdapter(),
82
images = [ ImageResource( 'red_flag', search_path = search_path ) ]
85
#--[ShowArray Class]------------------------------------------------------------
87
class ShowArray ( HasTraits ):
92
Item( 'data', editor = tabular_editor, show_label = False ),
93
title = 'Array Viewer',
100
#--[Example Code*]--------------------------------------------------------------
102
demo = ShowArray( data = random( ( 100000, 3 ) ) )