~ubuntu-branches/ubuntu/trusty/python-traitsui/trusty

« back to all changes in this revision

Viewing changes to examples/tutorials/traitsui_4.0/editors/tabular_editor/numpy_array.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2011-07-09 13:57:39 UTC
  • Revision ID: james.westby@ubuntu.com-20110709135739-x5u20q86huissmn1
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#--(NumPy Array Example)--------------------------------------------------------
 
2
"""
 
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.
 
6
 
 
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.
 
10
 
 
11
As with the other tabular editor tutorials, this example shows how to set up a
 
12
**TabularEditor** and create an appropriate **TabularAdapter** subclass.
 
13
 
 
14
In this case, it also shows:
 
15
 
 
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).
 
21
"""
 
22
 
 
23
#--<Imports>--------------------------------------------------------------------
 
24
 
 
25
from os.path \
 
26
    import join, dirname
 
27
 
 
28
from numpy \
 
29
    import sqrt
 
30
 
 
31
from numpy.random \
 
32
    import random
 
33
 
 
34
from traits.api \
 
35
    import HasTraits, Property, Array
 
36
 
 
37
from traitsui.api \
 
38
    import View, Item, TabularEditor
 
39
 
 
40
from traitsui.tabular_adapter \
 
41
    import TabularAdapter
 
42
 
 
43
from traitsui.menu \
 
44
    import NoButtons
 
45
 
 
46
from pyface.image_resource \
 
47
    import ImageResource
 
48
 
 
49
#--<Constants>------------------------------------------------------------------
 
50
 
 
51
# Necessary because of the dynamic way in which the demos are loaded:
 
52
import traitsui.api
 
53
 
 
54
search_path = [ join( dirname( traitsui.api.__file__ ),
 
55
                      'demo', 'Advanced' ) ]
 
56
 
 
57
#--[Tabular Adapter Definition]-------------------------------------------------
 
58
 
 
59
class ArrayAdapter ( TabularAdapter ):
 
60
 
 
61
    columns = [ ( 'i', 'index' ), ( 'x', 0 ), ( 'y', 1 ),  ( 'z', 2 ) ]
 
62
 
 
63
    font        = 'Courier 10'
 
64
    alignment   = 'right'
 
65
    format      = '%.4f'
 
66
    index_text  = Property
 
67
    index_image = Property
 
68
 
 
69
    def _get_index_text ( self ):
 
70
        return str( self.row )
 
71
 
 
72
    def _get_index_image ( self ):
 
73
        x, y, z = self.item
 
74
        if sqrt( (x - 0.5) ** 2 + (y - 0.5) ** 2 + (z - 0.5) ** 2 ) <= 0.25:
 
75
            return 'red_flag'
 
76
        return None
 
77
 
 
78
#--[Tabular Editor Definition]--------------------------------------------------
 
79
 
 
80
tabular_editor = TabularEditor(
 
81
    adapter = ArrayAdapter(),
 
82
    images  = [ ImageResource( 'red_flag', search_path = search_path ) ]
 
83
)
 
84
 
 
85
#--[ShowArray Class]------------------------------------------------------------
 
86
 
 
87
class ShowArray ( HasTraits ):
 
88
 
 
89
    data = Array
 
90
 
 
91
    view = View(
 
92
        Item( 'data', editor = tabular_editor, show_label = False ),
 
93
        title     = 'Array Viewer',
 
94
        width     = 0.3,
 
95
        height    = 0.8,
 
96
        resizable = True,
 
97
        buttons   = NoButtons
 
98
    )
 
99
 
 
100
#--[Example Code*]--------------------------------------------------------------
 
101
 
 
102
demo = ShowArray( data = random( ( 100000, 3 ) ) )
 
103