| 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 |
Using ArrayViewEditor to display large NumPy arrays in a table. |
| 13 |
|
| 14 |
This example displays 100,000 random 3D points from a unit cube. |
| 15 |
|
| 16 |
Note that the ArrayViewEditor has the following traits:: |
| 17 |
|
| 18 |
# Should an index column be displayed: |
| 19 |
show_index = Bool(True) |
| 20 |
|
| 21 |
# List of (optional) column titles: |
| 22 |
titles = List(Str) |
| 23 |
|
| 24 |
# Should the array be logically transposed: |
| 25 |
transpose = Bool(False) |
| 26 |
|
| 27 |
# The format used to display each array element: |
| 28 |
format = Str('%s') |
| 29 |
|
| 30 |
By default, the array row index will be shown in column one. If 'show_index' |
| 31 |
is False, then the row index column is omitted. |
| 32 |
|
| 33 |
If the list of 'titles' is empty, no column headers will be displayed. |
| 34 |
|
| 35 |
If the number of column headers is less than the number of array columns, then |
| 36 |
there are two cases: |
| 37 |
|
| 38 |
- If (number of array_columns) % (number of titles) == 0, then the titles |
| 39 |
are used to construct a series of repeating column headers with increasing |
| 40 |
subscripts (e.g. an (n x 6) array with titles of ['x','y','z'] would |
| 41 |
result in column headers of: 'x0', 'y0', 'z0', 'x1', 'y1', 'z1'). |
| 42 |
|
| 43 |
- In all other cases the titles are used as the column headers for the |
| 44 |
first set of columns, and the remaining column headers are set to the |
| 45 |
empty string (e.g. an (n x 5) array with titles of ['x','y','z'] would |
| 46 |
result in column headers of: 'x', 'y', 'z', '', ''). |
| 47 |
|
| 48 |
Setting 'transpose' to True will logically transpose the input array (e.g. an |
| 49 |
(3 x n) array will be displayed as an (n x 3) array). |
| 50 |
""" |
| 51 |
|
| 52 |
from numpy.random import random |
| 53 |
from traits.api import HasTraits, Array |
| 54 |
from traitsui.api import View, Item |
| 55 |
from traitsui.ui_editors.array_view_editor import ArrayViewEditor |
| 56 |
|
| 57 |
# -- ShowArray demo class ------------------------------------------------- |
| 58 |
|
| 59 |
|
| 60 |
class ShowArray(HasTraits): |
| 61 |
|
| 62 |
data = Array |
| 63 |
|
| 64 |
view = View( |
| 65 |
Item( |
| 66 |
'data', |
| 67 |
show_label=False, |
| 68 |
editor=ArrayViewEditor( |
| 69 |
titles=['x', 'y', 'z'], |
| 70 |
format='%.4f', |
| 71 |
# Font fails with wx in OSX; |
| 72 |
# see traitsui issue #13: |
| 73 |
# font = 'Arial 8' |
| 74 |
), |
| 75 |
), |
| 76 |
title='Array Viewer', |
| 77 |
width=0.3, |
| 78 |
height=0.8, |
| 79 |
resizable=True, |
| 80 |
) |
| 81 |
|
| 82 |
|
| 83 |
# -- Run the demo --------------------------------------------------------- |
| 84 |
|
| 85 |
# Create the demo: |
| 86 |
demo = ShowArray(data=random((100000, 3))) |
| 87 |
|
| 88 |
# Run the demo (if invoked from the command line): |
| 89 |
if __name__ == '__main__': |
| 90 |
demo.configure_traits() |