~ubuntu-branches/ubuntu/utopic/python-traitsui/utopic

« back to all changes in this revision

Viewing changes to examples/demo/Advanced/NumPy_array_tabular_editor_demo.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
#  Copyright (c) 2007, Enthought, Inc.
 
2
#  License: BSD Style.
 
3
 
 
4
"""
 
5
A demonstration of how the TabularEditor can be used to display (large) NumPy
 
6
arrays, in this case 100,000 random 3D points from a unit cube.
 
7
 
 
8
In addition to showing the coordinates of each point, it also displays the
 
9
index of each point in the array, as well as a red flag if the point lies within
 
10
0.25 of the center of the cube.
 
11
"""
 
12
 
 
13
#-- Imports --------------------------------------------------------------------
 
14
 
 
15
from numpy \
 
16
    import sqrt
 
17
 
 
18
from numpy.random \
 
19
    import random
 
20
 
 
21
from traits.api \
 
22
    import HasTraits, Property, Array, Font
 
23
 
 
24
from traitsui.api \
 
25
    import View, Item, TabularEditor
 
26
 
 
27
from traitsui.tabular_adapter \
 
28
    import TabularAdapter
 
29
 
 
30
#-- Tabular Adapter Definition -------------------------------------------------
 
31
 
 
32
class ArrayAdapter ( TabularAdapter ):
 
33
 
 
34
    columns = [ ( 'i', 'index' ), ( 'x', 0 ), ( 'y', 1 ),  ( 'z', 2 ) ]
 
35
 
 
36
    font        = Font('Courier 10')
 
37
    alignment   = 'right'
 
38
    format      = '%.4f'
 
39
    index_text  = Property
 
40
    index_image = Property
 
41
 
 
42
    def _get_index_text ( self ):
 
43
        return str( self.row )
 
44
 
 
45
    def _get_index_image ( self ):
 
46
        x, y, z = self.item
 
47
        if sqrt( (x - 0.5) ** 2 + (y - 0.5) ** 2 + (z - 0.5) ** 2 ) <= 0.25:
 
48
            return '@icons:red_ball'
 
49
 
 
50
        return None
 
51
 
 
52
#-- ShowArray Class Definition -------------------------------------------------
 
53
 
 
54
class ShowArray ( HasTraits ):
 
55
 
 
56
    data = Array
 
57
 
 
58
    view = View(
 
59
        Item( 'data',
 
60
              show_label = False,
 
61
              style      = 'readonly',
 
62
              editor     = TabularEditor( adapter = ArrayAdapter() )
 
63
        ),
 
64
        title     = 'Array Viewer',
 
65
        width     = 0.3,
 
66
        height    = 0.8,
 
67
        resizable = True
 
68
    )
 
69
 
 
70
# Create the demo:
 
71
demo = ShowArray( data = random( ( 100000, 3 ) ) )
 
72
 
 
73
# Run the demo (if invoked from the command line):
 
74
if __name__ == '__main__':
 
75
    demo.configure_traits()
 
76