1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
# (C) Copyright 2004-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
"""
Displaying large NumPy arrays with TabularEditor
A demonstration of how the TabularEditor can be used to display (large) NumPy
arrays, in this case 100,000 random 3D points from a unit cube.
In addition to showing the coordinates of each point, it also displays the
index of each point in the array, as well as a red flag if the point lies
within 0.25 of the center of the cube.
"""
from numpy import sqrt
from numpy.random import random
from traits.api import HasTraits, Property, Array
from traitsui.api import View, Item, TabularAdapter, TabularEditor
# -- Tabular Adapter Definition -------------------------------------------
class ArrayAdapter(TabularAdapter):
columns = [('i', 'index'), ('x', 0), ('y', 1), ('z', 2)]
font = 'Courier 10'
alignment = 'right'
format = '%.4f'
index_text = Property()
index_image = Property()
def _get_index_text(self):
return str(self.row)
def _get_index_image(self):
x, y, z = self.item
if sqrt((x - 0.5) ** 2 + (y - 0.5) ** 2 + (z - 0.5) ** 2) <= 0.25:
return '@icons:red_ball'
return None
# -- ShowArray Class Definition -------------------------------------------
class ShowArray(HasTraits):
data = Array
traits_view = View(
Item(
'data',
show_label=False,
editor=TabularEditor(
adapter=ArrayAdapter(),
auto_resize=True,
# Do not allow any kind of editing of the array:
editable=False,
operations=[],
drag_move=False,
),
),
title='Array Viewer',
width=0.3,
height=0.8,
resizable=True,
)
# Create the demo:
demo = ShowArray(data=random((100000, 3)))
# Run the demo (if invoked from the command line):
if __name__ == '__main__':
demo.configure_traits()
|