Question

I'm working with Chaco library. I make an interactive plot and in this plot, with chaco.tools.api RangeSelection, I can select a part of this. Now, I like know in what posision is this rectangle. With the follow code I can do all what I need, but I have the following error, why?? In the error, the points to I nedd are the selection_masks

This is the code:

# Major library imports
from numpy import arange
from scipy.special import jn

# Enthought library imports
from enable.api import Component, ComponentEditor
from traits.api import HasTraits, Instance, Any, on_trait_change
from traitsui.api import Item, Group, View

# Chaco imports
from chaco.api import create_line_plot, add_default_axes, add_default_grids
from chaco.tools.api import RangeSelection, RangeSelectionOverlay

# # Create the Chaco plot.
def _create_plot_component(self):

    numpoints = 100
    low = -5
    high = 15.001
    x = arange(low, high, (high-low)/numpoints)

    # Plot a bessel function
    y = jn(0, x)
    plot = create_line_plot((x,y), color=(0,0,1,1), width=2.0, index_sort="ascending")
    plot.active_tool = RangeSelection(plot, left_button_selects = True,     auto_handle_event = False)
    plot.overlays.append(RangeSelectionOverlay(component=plot))
    plot.bgcolor = "white"
    plot.padding = 50
    add_default_grids(plot)
    add_default_axes(plot)
    self.times_ds = plot.index
    self.times_ds.on_trait_change(self._selections_changed, 'metadata_changed')

    return plot

# Attributes to use for the plot view.
size=(600,500)
title="Simple line plot"

class Demo(HasTraits):
    times_ds = Any()
    plot = Instance(Component)
    corr_renderer = Any()

    traits_view = View(
                    Group(
                        Item('plot', editor=ComponentEditor(size=size),
                             show_label=False),
                        orientation = "vertical"),
                    resizable=True, title=title,
                    width=size[0], height=size[1]
                    )

    def _plot_default(self):
         return _create_plot_component(self)

    def _selections_changed(self, event):
        selections = event["selections"]
        low, high = selections
        print low, high

demo = Demo()
if __name__ == "__main__":
    demo.configure_traits()

And this is part of the error (in the error are the point of the rectangle to I nedd)

Exception occurred in traits notification handler for object:   <chaco.array_data_source.ArrayDataSource object at 0x66a94d0>, trait: metadata_changed, old  value: <undefined>, new value: True
Traceback (most recent call last):
  File "/home/franco/Python/Canopy/appdata/canopy-1.0.3.1262.rh5-x86_64/lib/python2.7/site-packages/traits/trait_notifiers.py", line 521, in rebind_call_1
    self.dispatch( getattr( obj(), self.name ), new )
  File "/home/franco/Python/Canopy/appdata/canopy-1.0.3.1262.rh5-x86_64/lib/python2.7/site-packages/traits/trait_notifiers.py", line 455, in dispatch
handler( *args )
  File "/home/franco/Desktop/Proyecto/Codigo/analisis_mas_range_relections.py", line 59, in _selections_changed
    selections = event["selections"]
TypeError: 'bool' object has no attribute '__getitem__'
Exception occurred in traits notification handler for object:  <chaco.array_data_source.ArrayDataSource object at 0x66a94d0>, trait: metadata_changed, old  value: <undefined>, new value: True

#the error continue but is the same error change the selection_masks....

TypeError: 'bool' object has no attribute '__getitem__'
Exception occurred in traits notification handler for object:  <chaco.array_data_source.ArrayDataSource object at 0x6758290>, trait: metadata_changed, old  value: <undefined>, new value: {'selection_masks': (-0.5953709619238516, 10.991581102204394)}

#the selection_masks are the point to I need

Thanks

Was it helpful?

Solution

I'm not sure where the first traceback is coming from since it appears to quote code that is not posted here. However, the second traceback is clear.

The metadata_changed Event does not get assigned the dictionary of metadata. Or at least, not always. The Tools that modify the metadata have been inconsistently written over the years. Just grab the metadata dictionary directly from the data source instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top