Exemplo n.º 1
0
    def __init__(self, axes_list):
        super(AxesManager, self).__init__()
        self.events = Events()
        self.events.indices_changed = Event("""
            Event that triggers when the indices of the `AxesManager` changes

            Triggers after the internal state of the `AxesManager` has been
            updated.

            Arguments:
            ----------
            obj : The AxesManager that the event belongs to.
            """, arguments=['obj'])
        self.events.any_axis_changed = Event("""
            Event that trigger when the space defined by the axes transforms.

            Specifically, it triggers when one or more of the folloing
            attributes changes on one or more of the axes:
                `offset`, `size`, `scale`

            Arguments:
            ----------
            obj : The AxesManager that the event belongs to.
            """, arguments=['obj'])
        self.create_axes(axes_list)
        # set_signal_dimension is called only if there is no current
        # view. It defaults to spectrum
        navigates = [i.navigate for i in self._axes]
        if t.Undefined in navigates:
            # Default to Signal1D view if the view is not fully defined
            self.set_signal_dimension(len(axes_list))

        self._update_attributes()
        self._update_trait_handlers()
        self._index = None  # index for the iterator
Exemplo n.º 2
0
    def test_interactive_function_return_None(self):
        e = Event()

        def function_return_None():
            print('function called')

        hs.interactive(function_return_None, e)
        e.trigger()
Exemplo n.º 3
0
 def test_interactive_sum(self):
     s = self.s
     e = Event()
     ss = hs.interactive(s.sum, e, axis=0)
     np.testing.assert_array_equal(ss.data, np.sum(s.data, axis=0))
     s.data += 3.2
     nt.assert_false(np.allclose(ss.data, np.sum(s.data, axis=0)))
     e.trigger()
     np.testing.assert_array_equal(ss.data, np.sum(s.data, axis=0))
Exemplo n.º 4
0
 def test_interactive_sum(self):
     s = self.s
     e = Event()
     ss = hs.interactive(s.sum, e, axis=0)
     np.testing.assert_array_equal(ss.data, np.sum(s.data, axis=0))
     s.data += 3.2
     assert not np.allclose(ss.data, np.sum(s.data, axis=0))
     e.trigger()
     np.testing.assert_array_equal(ss.data, np.sum(s.data, axis=0))
Exemplo n.º 5
0
    def __init__(self,
                 size,
                 index_in_array=None,
                 name=t.Undefined,
                 scale=1.,
                 offset=0.,
                 units=t.Undefined,
                 navigate=t.Undefined):
        super(DataAxis, self).__init__()
        self.events = Events()
        self.events.index_changed = Event("""
            Event that triggers when the index of the `DataAxis` changes

            Triggers after the internal state of the `DataAxis` has been
            updated.

            Arguments:
            ---------
            obj : The DataAxis that the event belongs to.
            index : The new index
            """,
                                          arguments=["obj", 'index'])
        self.events.value_changed = Event("""
            Event that triggers when the value of the `DataAxis` changes

            Triggers after the internal state of the `DataAxis` has been
            updated.

            Arguments:
            ---------
            obj : The DataAxis that the event belongs to.
            value : The new value
            """,
                                          arguments=["obj", 'value'])
        self._suppress_value_changed_trigger = False
        self._suppress_update_value = False
        self.name = name
        self.units = units
        self.scale = scale
        self.offset = offset
        self.size = size
        self.high_index = self.size - 1
        self.low_index = 0
        self.index = 0
        self.update_axis()
        self.navigate = navigate
        self.axes_manager = None
        self.on_trait_change(self.update_axis, ['scale', 'offset', 'size'])
        self.on_trait_change(self._update_slice, 'navigate')
        self.on_trait_change(self.update_index_bounds, 'size')
        # The slice must be updated even if the default value did not
        # change to correctly set its value.
        self._update_slice(self.navigate)
Exemplo n.º 6
0
    def test_interactive_sum_no_out(self):
        s = self.s

        def sumf(axis):
            return s.sum(axis=axis)
        e = Event()
        ss = hs.interactive(sumf, e, axis=0)
        np.testing.assert_array_equal(ss.data, np.sum(s.data, axis=0))
        s.data += 3.2
        assert not np.allclose(ss.data, np.sum(s.data, axis=0))
        e.trigger()
        np.testing.assert_array_equal(ss.data, np.sum(s.data, axis=0))
Exemplo n.º 7
0
    def test_interactive_sum_no_out(self):
        s = self.s

        def sumf(axis):
            return s.sum(axis=axis)
        e = Event()
        ss = hs.interactive(sumf, e, axis=0)
        np.testing.assert_array_equal(ss.data, np.sum(s.data, axis=0))
        s.data += 3.2
        nt.assert_false(np.allclose(ss.data, np.sum(s.data, axis=0)))
        e.trigger()
        np.testing.assert_array_equal(ss.data, np.sum(s.data, axis=0))
Exemplo n.º 8
0
 def test_chained_interactive(self):
     s = self.s
     e1, e2 = Event(), Event()
     ss = hs.interactive(s.sum, e1, axis=0)
     sss = hs.interactive(ss.sum, e2, axis=0)
     np.testing.assert_allclose(sss.data, np.sum(s.data, axis=(0, 1)))
     s.data += 3.2
     nt.assert_false(np.allclose(ss.data, np.sum(s.data, axis=(1))))
     e1.trigger()
     np.testing.assert_allclose(ss.data, np.sum(s.data, axis=(1)))
     nt.assert_false(np.allclose(sss.data, np.sum(s.data, axis=(0, 1))))
     e2.trigger()
     np.testing.assert_allclose(sss.data, np.sum(s.data, axis=(0, 1)))
Exemplo n.º 9
0
 def test_chained_interactive(self):
     s = self.s
     e1, e2 = Event(), Event()
     ss = hs.interactive(s.sum, e1, axis=0)
     sss = hs.interactive(ss.sum, e2, axis=0)
     np.testing.assert_allclose(sss.data, np.sum(s.data, axis=(0, 1)))
     s.data += 3.2
     assert not np.allclose(ss.data, np.sum(s.data, axis=(1)))
     e1.trigger()
     np.testing.assert_allclose(ss.data, np.sum(s.data, axis=(1)))
     assert not np.allclose(sss.data, np.sum(s.data, axis=(0, 1)))
     e2.trigger()
     np.testing.assert_allclose(sss.data, np.sum(s.data, axis=(0, 1)))
Exemplo n.º 10
0
    def __init__(self, ax, **kwargs):
        onselect = kwargs.pop('onselect', self.dummy)
        direction = kwargs.pop('direction', 'horizontal')
        useblit = kwargs.pop('useblit', ax.figure.canvas.supports_blit)
        SpanSelector.__init__(self,
                              ax,
                              onselect,
                              direction=direction,
                              useblit=useblit,
                              span_stays=False,
                              **kwargs)
        # The tolerance in points to pick the rectangle sizes
        self.tolerance = preferences.Plot.pick_tolerance
        self.on_move_cid = None
        self._range = None
        self.step_ax = None
        self.bounds_check = False
        self._button_down = False
        self.snap_size = False
        self.snap_position = False
        self.events = Events()
        self.events.changed = Event(doc="""
            Event that triggers when the widget was changed.

            Arguments:
            ----------
                obj:
                    The widget that changed
            """,
                                    arguments=['obj'])
        self.events.moved = Event(doc="""
            Event that triggers when the widget was moved.

            Arguments:
            ----------
                obj:
                    The widget that changed
            """,
                                  arguments=['obj'])
        self.events.resized = Event(doc="""
            Event that triggers when the widget was resized.

            Arguments:
            ----------
                obj:
                    The widget that changed
            """,
                                    arguments=['obj'])
        self.can_switch = False
Exemplo n.º 11
0
    def __init__(self):
        self.events = Events()
        self.events.closed = Event("""
            Event that triggers when the line is closed.

            Arguments:
                obj:  Signal1DLine instance
                    The instance that triggered the event.
            """,
                                   arguments=["obj"])
        self.sf_lines = None
        self.ax = None
        # Data attributes
        self.data_function = None
        # args to pass to `__call__`
        self.data_function_kwargs = {}
        self.axis = None
        self.axes_manager = None
        self._plot_imag = False
        self.norm = 'linear'

        # Properties
        self.auto_update = True
        self.autoscale = 'v'
        self._y_min = np.nan
        self._y_max = np.nan
        self.line = None
        self.plot_indices = False
        self.text = None
        self.text_position = (
            -0.1,
            1.05,
        )
        self._line_properties = {}
        self.type = "line"
Exemplo n.º 12
0
    def __init__(self):
        self.events = Events()
        self.events.closed = Event("""
            Event that triggers when the line is closed.

            Arguments:
                obj:  Signal1DLine instance
                    The instance that triggered the event.
            """, arguments=["obj"])
        self.sf_lines = None
        self.ax = None
        # Data attributes
        self.data_function = None
        self.axis = None
        self.axes_manager = None
        self.auto_update = True
        self.get_complex = False

        # Properties
        self.line = None
        self.autoscale = False
        self.plot_indices = False
        self.text = None
        self.text_position = (-0.1, 1.05,)
        self._line_properties = {}
        self.type = "line"
Exemplo n.º 13
0
    def __init__(self, axes_manager, **kwargs):
        super(DraggableWidgetBase, self).__init__(axes_manager, **kwargs)
        self.events.moved = Event(doc="""
            Event that triggers when the widget was moved.

            The event triggers after the internal state of the widget has been
            updated. This event does not differentiate on how the position of
            the widget was changed, so it is the responsibility of the user
            to suppress events as neccessary to avoid closed loops etc.

            Arguments:
            ----------
                obj:
                    The widget that was moved.
            """,
                                  arguments=['obj'])
        self._snap_position = True

        # Set default axes
        if self.axes_manager is not None:
            if self.axes_manager.navigation_dimension > 0:
                self.axes = self.axes_manager.navigation_axes[0:1]
            else:
                self.axes = self.axes_manager.signal_axes[0:1]
        else:
            self._pos = np.array([0.])
Exemplo n.º 14
0
    def __init__(self):
        # Data attributes
        self.data = None
        self.axes_manager = None
        self.ax = None
        self.auto_update = True

        # Properties
        self.marker = None
        self._marker_properties = {}
        self.signal = None
        self._plot_on_signal = True
        self.name = ''
        self.plot_marker = True

        # Events
        self.events = Events()
        self.events.closed = Event("""
            Event triggered when a marker is closed.

            Arguments
            ---------
            marker : Marker
                The marker that was closed.
            """, arguments=['obj'])
        self._closing = False
Exemplo n.º 15
0
    def __init__(self, ax, **kwargs):
        onsel = kwargs.pop('onselect', self.dummy)
        matplotlib.widgets.SpanSelector.__init__(self,
                                                 ax,
                                                 onsel,
                                                 direction='horizontal',
                                                 useblit=False,
                                                 **kwargs)
        # The tolerance in points to pick the rectangle sizes
        self.tolerance = 1
        self.on_move_cid = None
        self._range = None
        self.step_ax = None
        self.bounds_check = False
        self.buttonDown = False
        self.snap_size = False
        self.snap_position = False
        self.events = Events()
        self.events.changed = Event(doc="""
            Event that triggers when the widget was changed.

            Arguments:
            ----------
                obj:
                    The widget that changed
            """,
                                    arguments=['obj'])
        self.events.moved = Event(doc="""
            Event that triggers when the widget was moved.

            Arguments:
            ----------
                obj:
                    The widget that changed
            """,
                                  arguments=['obj'])
        self.events.resized = Event(doc="""
            Event that triggers when the widget was resized.

            Arguments:
            ----------
                obj:
                    The widget that changed
            """,
                                    arguments=['obj'])
        self.can_switch = False
Exemplo n.º 16
0
 def test_recompute(self):
     s = self.s
     e1 = Event()
     e2 = Event()
     ss = hs.interactive(s.sum, e1, recompute_out_event=e2, axis=0)
     # Check eveything as normal first
     np.testing.assert_equal(ss.data, np.sum(s.data, axis=1))
     # Modify axes and data in-place
     s.crop(1, 1)  # data shape (2, 3, 50)
     # Check that data is no longer comparable
     nt.assert_not_equal(ss.data.shape, np.sum(s.data, axis=1).shape)
     # Check that normal event raises an exception due to the invalid shape
     nt.assert_raises(ValueError, e1.trigger)
     # Check that recompute event fixes issue
     e2.trigger()
     np.testing.assert_equal(ss.data, np.sum(s.data, axis=1))
     # Finally, check that axes are updated as they should
     nt.assert_equal(ss.axes_manager.navigation_axes[0].offset, 1)
Exemplo n.º 17
0
 def test_recompute(self):
     s = self.s
     e1 = Event()
     e2 = Event()
     ss = hs.interactive(s.sum, e1, recompute_out_event=e2, axis=0)
     # Check eveything as normal first
     np.testing.assert_equal(ss.data, np.sum(s.data, axis=1))
     # Modify axes and data in-place
     s.crop(1, 1)  # data shape (2, 3, 50)
     # Check that data is no longer comparable
     nt.assert_not_equal(ss.data.shape, np.sum(s.data, axis=1).shape)
     # Check that normal event raises an exception due to the invalid shape
     nt.assert_raises(ValueError, e1.trigger)
     # Check that recompute event fixes issue
     e2.trigger()
     np.testing.assert_equal(ss.data, np.sum(s.data, axis=1))
     # Finally, check that axes are updated as they should
     nt.assert_equal(ss.axes_manager.navigation_axes[0].offset, 1)
Exemplo n.º 18
0
    def __init__(self):
        self.events = Events()
        self.events.closed = Event("""
            Event that triggers when the figure window is closed.

            Arguments:
                obj:  SpectrumFigure instances
                    The instance that triggered the event.
            """, arguments=["obj"])
Exemplo n.º 19
0
    def __init__(self, axes_manager=None, color='red', alpha=1.0, **kwargs):
        self.axes_manager = axes_manager
        self._axes = list()
        self.ax = None
        self.picked = False
        self.selected = False
        self._selected_artist = None
        self._size = 1.
        self._pos = np.array([0.])
        self.__is_on = True
        self.background = None
        self.patch = []
        self.color = color
        self.alpha = alpha
        self.cids = list()
        self.blit = True
        self.events = Events()
        self.events.changed = Event(doc="""
            Event that triggers when the widget has a significant change.

            The event triggers after the internal state of the widget has been
            updated.

            Arguments:
            ----------
                widget:
                    The widget that changed
            """,
                                    arguments=['obj'])
        self.events.closed = Event(doc="""
            Event that triggers when the widget closed.

            The event triggers after the widget has already been closed.

            Arguments:
            ----------
                widget:
                    The widget that closed
            """,
                                   arguments=['obj'])
        self._navigating = False
        super(WidgetBase, self).__init__(**kwargs)
Exemplo n.º 20
0
 def test_two_recompute_events(self):
     s = self.s
     e1 = Event()
     e2 = Event()
     ss = hs.interactive(s.sum,
                         event=None,
                         recompute_out_event=(e1, e2),
                         axis=0)
     s.data[:] = 0
     e1.trigger()
     np.testing.assert_equal(ss.data, np.sum(s.data, axis=1))
     s.data[:] = 1
     e2.trigger()
     np.testing.assert_equal(ss.data, np.sum(s.data, axis=1))
Exemplo n.º 21
0
    def __init__(self):
        self._draw_event_cid = None
        self._background = None
        self.events = Events()
        self.events.closed = Event("""
            Event that triggers when the figure window is closed.

            Arguments:
                obj:  SpectrumFigure instances
                    The instance that triggered the event.
            """, arguments=["obj"])
        self.title = ""
        self.ax_markers = list()
Exemplo n.º 22
0
    def __init__(self):
        """Sets up events.changed event, and inits HasTraits.
        """
        super(BaseROI, self).__init__()
        self.events = Events()
        self.events.changed = Event("""
            Event that triggers when the ROI has changed.

            What constitues a change varies from ROI to ROI, but in general it
            should correspond to the region selected by the ROI has changed.

            Arguments:
            ----------
                roi :
                    The ROI that was changed.
            """, arguments=['roi'])
        self.signal_map = dict()
Exemplo n.º 23
0
    def __init__(self, parameter_name_list):
        self.events = Events()
        self.events.active_changed = Event("""
            Event that triggers when the `Component.active` changes.

            The event triggers after the internal state of the `Component` has
            been updated.

            Arguments
            ---------
            obj : Component
                The `Component` that the event belongs to
            active : bool
                The new active state
            """,
                                           arguments=["obj", 'active'])
        self.parameters = []
        self.init_parameters(parameter_name_list)
        self._update_free_parameters()
        self.active = True
        self._active_array = None
        self.isbackground = False
        self.convolved = True
        self.parameters = tuple(self.parameters)
        self._id_name = self.__class__.__name__
        self._id_version = '1.0'
        self._position = None
        self.model = None
        self.name = ''
        self._whitelist = {
            '_id_name': None,
            'name': None,
            'active_is_multidimensional': None,
            '_active_array': None,
            'active': None
        }
        self._slicing_whitelist = {'_active_array': 'inav'}
        self._slicing_order = (
            'active',
            'active_is_multidimensional',
            '_active_array',
        )
Exemplo n.º 24
0
    def __init__(self):
        self._twins = set()
        self.events = Events()
        self.events.value_changed = Event("""
            Event that triggers when the `Parameter.value` changes.

            The event triggers after the internal state of the `Parameter` has
            been updated.

            Arguments
            ---------
            obj : Parameter
                The `Parameter` that the event belongs to
            value : {float | array}
                The new value of the parameter
            """,
                                          arguments=["obj", 'value'])
        self.std = None
        self.component = None
        self.grad = None
        self.name = ''
        self.units = ''
        self._linear = False
        self.map = None
        self.model = None
        self._whitelist = {
            '_id_name': None,
            'value': None,
            'std': None,
            'free': None,
            'units': None,
            'map': None,
            '_bounds': None,
            'ext_bounded': None,
            'name': None,
            '_linear': None,
            'ext_force_positive': None,
            'twin_function_expr': None,
            'twin_inverse_function_expr': None,
            'self': ('id', None),
        }
        self._slicing_whitelist = {'map': 'inav'}
Exemplo n.º 25
0
    def __init__(self, axes_manager, **kwargs):
        super(ResizableDraggableWidgetBase, self).__init__(
            axes_manager, **kwargs)
        if not self.axes:
            self._size = np.array([1])
        self.size_step = 1      # = one step in index space
        self._snap_size = True
        self.events.resized = Event(doc="""
            Event that triggers when the widget was resized.

            The event triggers after the internal state of the widget has been
            updated. This event does not differentiate on how the size of
            the widget was changed, so it is the responsibility of the user
            to suppress events as neccessary to avoid closed loops etc.

            Arguments:
            ----------
                obj:
                    The widget that was resized.
            """, arguments=['obj'])
        self.no_events_while_dragging = False
        self._drag_store = None
Exemplo n.º 26
0
 def test_two_update_events(self):
     s = self.s
     e1 = Event()
     e2 = Event()
     ss = hs.interactive(
         s.sum,
         event=(
             e1,
             e2),
         recompute_out_event=None,
         axis=0)
     s.data[:] = 0
     e1.trigger()
     np.testing.assert_equal(ss.data, np.sum(s.data, axis=1))
     s.data[:] = 1
     e2.trigger()
     np.testing.assert_equal(ss.data, np.sum(s.data, axis=1))