Пример #1
0
    def __init__(self, hdulist, figure, rect):
        self.hdulist = hdulist
        self.figure = figure
        self.axes = self._create_axes(rect)
        #self.figure.add_axes(self.axes)

        self.marker = None

        self.display_changed = Signal()
        self.xy_changed = Signal()
        self.focus_released = Signal()

        self._colormap = GrayscaleColorMap()
        self._mpl_event_handlers = {}
        self._interaction_context = None
        self.number_of_images_displayed = 0
        self.frame_number = None
Пример #2
0
def main():
    img = create_test_image()
    colormap = GrayscaleColorMap()

    plt.subplot(1, 1, 1)
    plt.subplots_adjust(left=0.15, bottom=0.25)

    axcolor = "lightgoldenrodyellow"
    contrast_ax = plt.axes([0.15, 0.15, 0.65, 0.03], axisbg=axcolor)
    bias_ax = plt.axes([0.15, 0.1, 0.65, 0.03], axisbg=axcolor)

    contrast_slider = Slider(contrast_ax, "Contrast", 0.0, 1.0, valinit=0.5)
    bias_slider = Slider(bias_ax, "Bias", 0.0, 1.0, valinit=0.5)

    cmap = colormap.as_mpl_cmap()
    plt.subplot(1, 1, 1)
    img_ax = plt.imshow(img, cmap=cmap)
    plt.colorbar(orientation="horizontal")

    def update_colormap():
        cmap = colormap.as_mpl_cmap()
        img_ax.set_cmap(cmap)

    def update_contrast(contrast):
        print "Setting contrast to %f" % contrast
        colormap.set_contrast(contrast)
        update_colormap()

    def update_bias(bias):
        print "Setting bias to %f" % bias
        colormap.set_bias(bias)
        update_colormap()

    contrast_slider.on_changed(update_contrast)
    bias_slider.on_changed(update_bias)

    plt.show()
Пример #3
0
def main():
    img = create_test_image()
    colormap = GrayscaleColorMap()

    plt.subplot(1, 1, 1)
    plt.subplots_adjust(left=0.15, bottom=0.25)

    axcolor = 'lightgoldenrodyellow'
    contrast_ax = plt.axes([0.15, 0.15, 0.65, 0.03], axisbg=axcolor)
    bias_ax = plt.axes([0.15, 0.1, 0.65, 0.03], axisbg=axcolor)

    contrast_slider = Slider(contrast_ax, "Contrast", 0.0, 1.0, valinit=0.5)
    bias_slider = Slider(bias_ax, "Bias", 0.0, 1.0, valinit=0.5)

    cmap = colormap.as_mpl_cmap()
    plt.subplot(1, 1, 1)
    img_ax = plt.imshow(img, cmap=cmap)
    plt.colorbar(orientation="horizontal")

    def update_colormap():
        cmap = colormap.as_mpl_cmap()
        img_ax.set_cmap(cmap)

    def update_contrast(contrast):
        print("Setting contrast to %f" % contrast)
        colormap.set_contrast(contrast)
        update_colormap()

    def update_bias(bias):
        print("Setting bias to %f" % bias)
        colormap.set_bias(bias)
        update_colormap()

    contrast_slider.on_changed(update_contrast)
    bias_slider.on_changed(update_bias)

    plt.show()
Пример #4
0
    def __init__(self, hdulist, figure, rect):
        self.hdulist = hdulist

        self.figure = figure
        self.axes = self._create_axes(rect)
        self.figure.add_axes(self.axes)

        self.marker = None

        self.display_changed = Signal()
        self.xy_changed = Signal()
        self.focus_released = Signal()

        self._colormap = GrayscaleColorMap()
        self._mpl_event_handlers = {}
        self._interaction_context = None
Пример #5
0
class ImageSinglet(object):
    """
    A single image on a matplotlib axes.  Provides interaction and is markable.

    """

    def __init__(self, hdulist, figure, rect):
        self.hdulist = hdulist
        self.figure = figure
        self.axes = self._create_axes(rect)
        #self.figure.add_axes(self.axes)

        self.marker = None

        self.display_changed = Signal()
        self.xy_changed = Signal()
        self.focus_released = Signal()

        self._colormap = GrayscaleColorMap()
        self._mpl_event_handlers = {}
        self._interaction_context = None
        self.number_of_images_displayed = 0
        self.frame_number = None

    @property
    def width(self):
        return _image_width(self.hdulist)

    @property
    def height(self):
        return _image_height(self.hdulist)

    def show_image(self, colorbar=False):
        # start xpans if needed
        ds9.ds9_xpans()
        # start ds9 if need, or connect to existing
        display = ds9.ds9(target='validate')
        if self.frame_number is None:
            # display.set('frame delete all')
            display.set('frame new')
            display.set('scale zscale')
            display.set('cmap invert yes')
            f = StringIO()
            self.hdulist.writeto(f)
            f.flush()
            f.seek(0)
            hdulist = fits.open(f)
            for hdu in hdulist:
                del(hdu.header['PV*'])
            display.set_pyfits(hdulist)
            self.frame_number = display.get('frame frameno')
            display.set('frame center {}'.format(self.frame_number))
            display.set('zoom to fit')
            display.set('wcs align yes')
        display.set('frame frameno {}'.format(self.frame_number))

        self._interaction_context = InteractionContext(self)

        self.number_of_images_displayed += 1

    def clear_markers(self):
        display = ds9.ds9(target='valdiate')
        display.set('regions delete all')

    def place_marker(self, x, y, radius, colour="b"):
        """
        Draws a marker with the specified dimensions.  Only one marker can
        be on the image at a time, so any existing marker will be replaced.
        """
        display = ds9.ds9(target='validate')
        colour_string = {'r': 'red', 'b': 'blue'}.get(colour, 'green')
        display.set('regions', 'image; circle({},{},{}) # color={}'.format(x,y,radius,colour_string))

        #if self.marker is not None:
        #    self.marker.remove_from_axes(self.axes)
        #
        #self.marker = Marker(x, y, radius, colour=colour)
        #self.marker.add_to_axes(self.axes)

        self.display_changed.fire()

    def place_error_ellipse(self, x, y, a, b, pa, color='b'):
        """
        Draws an ErrorEllipse with the given dimensions.  Can not be moved later.
        """
        display = ds9.ds9(target='validate')
        # display.set('regions delete all')
        display.set('regions', 'image; ellipse({},{},{},{},{}'.format(x,y,a,b,pa+90))
        #self.error_ellipse = ErrEllipse(x, y, a, b, pa, color=color)
        #self.error_ellipse.add_to_axes(self.axes)
        self.display_changed.fire()

    def update_marker(self, x, y, radius=None):
        if self.marker is None:
            if radius is None:
                raise MPLViewerError("No marker to update.")
            else:
                # For convenience go ahead and make one
                self.place_marker(x, y, radius)

        self.marker.center = (x, y)

        if radius is not None:
            self.marker.radius = radius

        self.xy_changed.fire(x, y)
        self.display_changed.fire()

    def release_focus(self):
        self.focus_released.fire()

    def update_colormap(self, dx, dy):
        contrast_diff = float(-dy) / self.height
        bias_diff = float(dx) / self.width

        self._colormap.update_contrast(contrast_diff)
        self._colormap.update_bias(bias_diff)

        self._refresh_displayed_colormap()

    def reset_colormap(self):
        self._colormap.set_defaults()
        self._refresh_displayed_colormap()

    def toggle_reticule(self):
        self.marker.toggle_reticule()
        self.display_changed.fire()

    def is_event_in_axes(self, event):
        return self.axes == event.inaxes

    def register_mpl_event_handler(self, eventname, handler):
        return 0
#        handler_id = self.figure.canvas.mpl_connect(eventname, handler)
#        self._mpl_event_handlers[handler_id] = (eventname, handler)
#        return handler_id

    def deregister_mpl_event_handler(self, id_):
        return
#        self.figure.canvas.mpl_disconnect(id_)
#        del self._mpl_event_handlers[id_]

    def apply_event_handlers(self, canvas):
        for eventname, handler in self._mpl_event_handlers.itervalues():
            canvas.mpl_connect(eventname, handler)

    def _create_axes(self, rect):
        """
        Args:
          rect: [left, bottom, width, height]
            Used to construct the matplotlib axes.
        """
        return None

    def _refresh_displayed_colormap(self):
        self.axes_image.set_cmap(self._colormap.as_mpl_cmap())
        self.axes_image.changed()
        self.display_changed.fire()
Пример #6
0
class ImageSinglet(object):
    """
    A single image on a matplotlib axes.  Provides interaction and is
    markable.
    """

    def __init__(self, hdulist, figure, rect):
        self.hdulist = hdulist

        self.figure = figure
        self.axes = self._create_axes(rect)
        self.figure.add_axes(self.axes)

        self.marker = None

        self.display_changed = Signal()
        self.xy_changed = Signal()
        self.focus_released = Signal()

        self._colormap = GrayscaleColorMap()
        self._mpl_event_handlers = {}
        self._interaction_context = None

    @property
    def image_data(self):
        return _image_data(self.hdulist)

    @property
    def width(self):
        return _image_width(self.hdulist)

    @property
    def height(self):
        return _image_height(self.hdulist)

    def show_image(self, colorbar=False):
        self._interaction_context = InteractionContext(self)

        extent = (1, self.width, 1, self.height)
        self.axes_image = self.axes.imshow(zscale(self.image_data),
                                           origin="lower",
                                           extent=extent,
                                           cmap=self._colormap.as_mpl_cmap())

        if colorbar:
            # Create axes for colorbar.  Make it tightly fit the image.
            divider = make_axes_locatable(self.axes)
            cax = divider.append_axes("bottom", size="5%", pad=0.05)
            self.figure.colorbar(self.axes_image, orientation="horizontal",
                                 cax=cax)

    def place_marker(self, x, y, radius, colour="b"):
        """
        Draws a marker with the specified dimensions.  Only one marker can
        be on the image at a time, so any existing marker will be replaced.
        """
        if self.marker is not None:
            self.marker.remove_from_axes(self.axes)

        self.marker = Marker(x, y, radius, colour=colour)
        self.marker.add_to_axes(self.axes)

        self.display_changed.fire()

    def place_error_ellipse(self, x, y, a, b, pa, color='b'):
        """
        Draws an ErrorEllipse with the given dimensions.  Can not be moved later.
        """
        self.error_ellipse = ErrEllipse(x, y, a, b, pa, color=color)
        self.error_ellipse.add_to_axes(self.axes)
        self.display_changed.fire()

    def update_marker(self, x, y, radius=None):
        if self.marker is None:
            if radius is None:
                raise MPLViewerError("No marker to update.")
            else:
                # For convenience go ahead and make one
                self.place_marker(x, y, radius)

        self.marker.center = (x, y)

        if radius is not None:
            self.marker.radius = radius

        self.xy_changed.fire(x, y)
        self.display_changed.fire()

    def release_focus(self):
        self.focus_released.fire()

    def update_colormap(self, dx, dy):
        contrast_diff = float(-dy) / self.height
        bias_diff = float(dx) / self.width

        self._colormap.update_contrast(contrast_diff)
        self._colormap.update_bias(bias_diff)

        self._refresh_displayed_colormap()

    def reset_colormap(self):
        self._colormap.set_defaults()
        self._refresh_displayed_colormap()

    def toggle_reticule(self):
        self.marker.toggle_reticule()
        self.display_changed.fire()

    def is_event_in_axes(self, event):
        return self.axes == event.inaxes

    def register_mpl_event_handler(self, eventname, handler):
        handler_id = self.figure.canvas.mpl_connect(eventname, handler)
        self._mpl_event_handlers[handler_id] = (eventname, handler)
        return handler_id

    def deregister_mpl_event_handler(self, id_):
        self.figure.canvas.mpl_disconnect(id_)
        del self._mpl_event_handlers[id_]

    def apply_event_handlers(self, canvas):
        for eventname, handler in self._mpl_event_handlers.itervalues():
            canvas.mpl_connect(eventname, handler)

    def _create_axes(self, rect):
        """
        Args:
          rect: [left, bottom, width, height]
            Used to construct the matplotlib axes.
        """
        axes = plt.Axes(self.figure, rect)

        # Don't draw tick marks and labels
        axes.set_axis_off()

        # FITS images start at pixel 1,1 in the bottom-left corner
        axes.set_xlim([1, self.width])
        axes.set_ylim([1, self.height])

        # Add a border around the image.
        axes.add_patch(plt.Rectangle((1, 1), self.width - 1, self.height - 1,
                       linewidth=3, edgecolor="black", fill=False))

        return axes

    def _refresh_displayed_colormap(self):
        self.axes_image.set_cmap(self._colormap.as_mpl_cmap())
        self.axes_image.changed()
        self.display_changed.fire()
Пример #7
0
class ImageSinglet(object):
    """
    A single image on a matplotlib axes.  Provides interaction and is markable.

    """

    def __init__(self, hdulist, figure, rect):
        self.hdulist = hdulist
        self.figure = figure
        self.axes = self._create_axes(rect)
        #self.figure.add_axes(self.axes)

        self.marker = None

        self.display_changed = Signal()
        self.xy_changed = Signal()
        self.focus_released = Signal()

        self._colormap = GrayscaleColorMap()
        self._mpl_event_handlers = {}
        self._interaction_context = None
        self.number_of_images_displayed = 0
        self.frame_number = None

    @property
    def width(self):
        return _image_width(self.hdulist)

    @property
    def height(self):
        return _image_height(self.hdulist)

    def show_image(self, colorbar=False):
        # start xpans if needed
        ds9.ds9_xpans()
        # start ds9 if need, or connect to existing
        display = ds9.ds9(target='validate')
        if self.frame_number is None:
            # display.set('frame delete all')
            display.set('frame new')
            display.set('scale zscale')
            display.set('cmap invert yes')
            f = StringIO()
            self.hdulist.writeto(f)
            f.flush()
            f.seek(0)
            hdulist = fits.open(f)
            for hdu in hdulist:
                del(hdu.header['PV*'])
            display.set_pyfits(hdulist)
            f.close()
            del(hdulist)
            self.frame_number = display.get('frame frameno')
            display.set('frame center {}'.format(self.frame_number))
            display.set('zoom to fit')
            display.set('wcs align yes')
        display.set('frame frameno {}'.format(self.frame_number))

        self._interaction_context = InteractionContext(self)

        self.number_of_images_displayed += 1

    def clear_markers(self):
        display = ds9.ds9(target='valdiate')
        display.set('regions delete all')

    def place_marker(self, x, y, radius, colour="b"):
        """
        Draws a marker with the specified dimensions.  Only one marker can
        be on the image at a time, so any existing marker will be replaced.
        """
        display = ds9.ds9(target='validate')
        colour_string = {'r': 'red', 'b': 'blue'}.get(colour, 'green')
        display.set('regions', 'image; circle({},{},{}) # color={}'.format(x,y,radius,colour_string))

        #if self.marker is not None:
        #    self.marker.remove_from_axes(self.axes)
        #
        #self.marker = Marker(x, y, radius, colour=colour)
        #self.marker.add_to_axes(self.axes)

        self.display_changed.fire()

    def place_error_ellipse(self, x, y, a, b, pa, color='b'):
        """
        Draws an ErrorEllipse with the given dimensions.  Can not be moved later.
        """
        display = ds9.ds9(target='validate')
        # display.set('regions delete all')
        display.set('regions', 'image; ellipse({},{},{},{},{}'.format(x,y,a,b,pa+90))
        #self.error_ellipse = ErrEllipse(x, y, a, b, pa, color=color)
        #self.error_ellipse.add_to_axes(self.axes)
        self.display_changed.fire()

    def update_marker(self, x, y, radius=None):
        if self.marker is None:
            if radius is None:
                raise MPLViewerError("No marker to update.")
            else:
                # For convenience go ahead and make one
                self.place_marker(x, y, radius)

        self.marker.center = (x, y)

        if radius is not None:
            self.marker.radius = radius

        self.xy_changed.fire(x, y)
        self.display_changed.fire()

    def release_focus(self):
        self.focus_released.fire()

    def update_colormap(self, dx, dy):
        contrast_diff = float(-dy) / self.height
        bias_diff = float(dx) / self.width

        self._colormap.update_contrast(contrast_diff)
        self._colormap.update_bias(bias_diff)

        self._refresh_displayed_colormap()

    def reset_colormap(self):
        self._colormap.set_defaults()
        self._refresh_displayed_colormap()

    def toggle_reticule(self):
        self.marker.toggle_reticule()
        self.display_changed.fire()

    def is_event_in_axes(self, event):
        return self.axes == event.inaxes

    def register_mpl_event_handler(self, eventname, handler):
        return 0
#        handler_id = self.figure.canvas.mpl_connect(eventname, handler)
#        self._mpl_event_handlers[handler_id] = (eventname, handler)
#        return handler_id

    def deregister_mpl_event_handler(self, id_):
        return
#        self.figure.canvas.mpl_disconnect(id_)
#        del self._mpl_event_handlers[id_]

    def apply_event_handlers(self, canvas):
        for eventname, handler in self._mpl_event_handlers.itervalues():
            canvas.mpl_connect(eventname, handler)

    def _create_axes(self, rect):
        """
        Args:
          rect: [left, bottom, width, height]
            Used to construct the matplotlib axes.
        """
        return None

    def _refresh_displayed_colormap(self):
        self.axes_image.set_cmap(self._colormap.as_mpl_cmap())
        self.axes_image.changed()
        self.display_changed.fire()