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