示例#1
0
    class Detailed2DWidget(QWidget):
        def __init__(self, x_scale_factor=1.0, y_scale_factor=1.0):
            super(SRWPlot.Detailed2DWidget, self).__init__()

            self.x_scale_factor = x_scale_factor
            self.y_scale_factor = y_scale_factor

            self.plot_canvas = ImageView(parent=self)

            self.plot_canvas.setColormap({
                "name": "gray",
                "normalization": "linear",
                "autoscale": True,
                "vmin": 0,
                "vmax": 0,
                "colors": 256
            })
            self.plot_canvas.setMinimumWidth(590 * x_scale_factor)
            self.plot_canvas.setMaximumWidth(590 * y_scale_factor)

            self.info_box = SRWPlot.InfoBoxWidget(x_scale_factor,
                                                  y_scale_factor)

            layout = QGridLayout()

            layout.addWidget(self.info_box, 0, 1, 1, 1)
            layout.addWidget(self.plot_canvas, 0, 0, 1, 1)

            layout.setColumnMinimumWidth(0, 600 * x_scale_factor)
            layout.setColumnMinimumWidth(1, 230 * x_scale_factor)

            self.setLayout(layout)

        def plot_2D(self,
                    ticket,
                    var_x,
                    var_y,
                    title,
                    xtitle,
                    ytitle,
                    xum="",
                    yum="",
                    plotting_range=None,
                    use_default_factor=True):

            matplotlib.rcParams['axes.formatter.useoffset'] = 'False'

            if use_default_factor:
                factor1 = SRWPlot.get_factor(var_x)
                factor2 = SRWPlot.get_factor(var_y)
            else:
                factor1 = 1.0
                factor2 = 1.0

            if plotting_range == None:
                xx = ticket['bin_h']
                yy = ticket['bin_v']

                nbins_h = ticket['nbins_h']
                nbins_v = ticket['nbins_v']

                histogram = ticket['histogram']
            else:
                range_x = numpy.where(
                    numpy.logical_and(ticket['bin_h'] >= plotting_range[0],
                                      ticket['bin_h'] <= plotting_range[1]))
                range_y = numpy.where(
                    numpy.logical_and(ticket['bin_v'] >= plotting_range[2],
                                      ticket['bin_v'] <= plotting_range[3]))

                xx = ticket['bin_h'][range_x]
                yy = ticket['bin_v'][range_y]

                histogram = []
                for row in ticket['histogram'][range_x]:
                    histogram.append(row[range_y])

                nbins_h = len(xx)
                nbins_v = len(yy)

            if len(xx) == 0 or len(yy) == 0:
                raise Exception("Nothing to plot in the given range")

            xmin, xmax = xx.min(), xx.max()
            ymin, ymax = yy.min(), yy.max()

            origin = (xmin * factor1, ymin * factor2)
            scale = (abs(
                (xmax - xmin) / nbins_h) * factor1, abs(
                    (ymax - ymin) / nbins_v) * factor2)

            # PyMCA inverts axis!!!! histogram must be calculated reversed
            data_to_plot = []
            for y_index in range(0, nbins_v):
                x_values = []
                for x_index in range(0, nbins_h):
                    x_values.append(histogram[x_index][y_index])

                data_to_plot.append(x_values)

            self.plot_canvas.setImage(numpy.array(data_to_plot),
                                      origin=origin,
                                      scale=scale)

            if xtitle is None: xtitle = SRWPlot.get_SRW_label(var_x)
            if ytitle is None: ytitle = SRWPlot.get_SRW_label(var_y)

            self.plot_canvas.setGraphXLabel(xtitle)
            self.plot_canvas.setGraphYLabel(ytitle)
            self.plot_canvas.setGraphTitle(title)

            self.plot_canvas._histoHPlot.setGraphYLabel('Frequency')

            self.plot_canvas._histoHPlot._backend.ax.xaxis.get_label(
            ).set_color('white')
            self.plot_canvas._histoHPlot._backend.ax.xaxis.get_label(
            ).set_fontsize(1)
            for label in self.plot_canvas._histoHPlot._backend.ax.xaxis.get_ticklabels(
            ):
                label.set_color('white')
                label.set_fontsize(1)

            self.plot_canvas._histoVPlot.setGraphXLabel('Frequency')

            self.plot_canvas._histoVPlot._backend.ax.yaxis.get_label(
            ).set_color('white')
            self.plot_canvas._histoVPlot._backend.ax.yaxis.get_label(
            ).set_fontsize(1)
            for label in self.plot_canvas._histoVPlot._backend.ax.yaxis.get_ticklabels(
            ):
                label.set_color('white')
                label.set_fontsize(1)

            if ticket['fwhm_h'] == None: ticket['fwhm_h'] = 0.0
            if ticket['fwhm_v'] == None: ticket['fwhm_v'] = 0.0

            n_patches = len(self.plot_canvas._histoHPlot._backend.ax.patches)
            if (n_patches > 0):
                self.plot_canvas._histoHPlot._backend.ax.patches.remove(
                    self.plot_canvas._histoHPlot._backend.ax.patches[n_patches
                                                                     - 1])

            if not ticket['fwhm_h'] == 0.0:
                x_fwhm_i, x_fwhm_f = ticket['fwhm_coordinates_h']
                x_fwhm_i, x_fwhm_f = x_fwhm_i * factor1, x_fwhm_f * factor1
                y_fwhm = max(ticket['histogram_h']) * 0.5

                self.plot_canvas._histoHPlot._backend.ax.add_patch(
                    FancyArrowPatch([x_fwhm_i, y_fwhm], [x_fwhm_f, y_fwhm],
                                    arrowstyle=ArrowStyle.CurveAB(
                                        head_width=2, head_length=4),
                                    color='b',
                                    linewidth=1.5))

            n_patches = len(self.plot_canvas._histoVPlot._backend.ax.patches)
            if (n_patches > 0):
                self.plot_canvas._histoVPlot._backend.ax.patches.remove(
                    self.plot_canvas._histoVPlot._backend.ax.patches[n_patches
                                                                     - 1])

            if not ticket['fwhm_v'] == 0.0:
                y_fwhm_i, y_fwhm_f = ticket['fwhm_coordinates_v']
                y_fwhm_i, y_fwhm_f = y_fwhm_i * factor2, y_fwhm_f * factor2
                x_fwhm = max(ticket['histogram_v']) * 0.5

                self.plot_canvas._histoVPlot._backend.ax.add_patch(
                    FancyArrowPatch([x_fwhm, y_fwhm_i], [x_fwhm, y_fwhm_f],
                                    arrowstyle=ArrowStyle.CurveAB(
                                        head_width=2, head_length=4),
                                    color='r',
                                    linewidth=1.5))

            self.plot_canvas._histoHPlot.replot()
            self.plot_canvas._histoVPlot.replot()
            self.plot_canvas.replot()

            self.info_box.total.setText("{:.3e}".format(
                decimal.Decimal(ticket['total'])))
            self.info_box.fwhm_h.setText("{:5.4f}".format(ticket['fwhm_h'] *
                                                          factor1))
            self.info_box.fwhm_v.setText("{:5.4f}".format(ticket['fwhm_v'] *
                                                          factor2))
            self.info_box.label_h.setText("FWHM " + xum)
            self.info_box.label_v.setText("FWHM " + yum)

        def clear(self):
            self.plot_canvas.clear()

            self.plot_canvas._histoHPlot.clear()
            self.plot_canvas._histoVPlot.clear()

            self.plot_canvas._histoHPlot._backend.ax.xaxis.get_label(
            ).set_color('white')
            self.plot_canvas._histoHPlot._backend.ax.xaxis.get_label(
            ).set_fontsize(1)
            for label in self.plot_canvas._histoHPlot._backend.ax.xaxis.get_ticklabels(
            ):
                label.set_color('white')
                label.set_fontsize(1)

            self.plot_canvas._histoVPlot._backend.ax.yaxis.get_label(
            ).set_color('white')
            self.plot_canvas._histoVPlot._backend.ax.yaxis.get_label(
            ).set_fontsize(1)
            for label in self.plot_canvas._histoVPlot._backend.ax.yaxis.get_ticklabels(
            ):
                label.set_color('white')
                label.set_fontsize(1)

            self.plot_canvas._histoHPlot.setGraphYLabel('Frequency')
            self.plot_canvas._histoVPlot.setGraphXLabel('Frequency')

            self.plot_canvas._histoHPlot.replot()
            self.plot_canvas._histoVPlot.replot()

            self.info_box.clear()
示例#2
0
class ImageViewWithFWHM(QWidget):
    def __init__(self, x_scale_factor=1.0, y_scale_factor=1.0):
        super(ImageViewWithFWHM, self).__init__()

        self.plot_canvas = ImageView(parent=self)
        self.set_plot_canvas_default_settings()

        self.info_box = InfoBoxWidget(x_scale_factor, y_scale_factor)

        layout = QGridLayout()

        layout.addWidget(self.info_box, 0, 1, 1, 1)
        layout.addWidget(self.plot_canvas, 0, 0, 1, 1)

        layout.setColumnMinimumWidth(0, 600 * x_scale_factor)
        layout.setColumnMinimumWidth(1, 230 * x_scale_factor)

        self.setLayout(layout)

    def get_ImageView(self):
        return self.plot_canvas

    def get_InfoBoxWidhet(self):
        return self.info_box

    def set_plot_canvas_default_settings(self):
        self.get_ImageView().resetZoom()
        self.get_ImageView().setXAxisAutoScale(True)
        self.get_ImageView().setYAxisAutoScale(True)
        self.get_ImageView().setGraphGrid(False)
        self.get_ImageView().setKeepDataAspectRatio(True)
        self.get_ImageView().yAxisInvertedAction.setVisible(False)
        self.get_ImageView().setXAxisLogarithmic(False)
        self.get_ImageView().setYAxisLogarithmic(False)
        self.get_ImageView().getMaskAction().setVisible(False)
        self.get_ImageView().getRoiAction().setVisible(False)
        self.get_ImageView().getColormapAction().setVisible(True)
        self.get_ImageView().setKeepDataAspectRatio(False)

    def plot_2D(self,
                histogram,
                xx=None,
                yy=None,
                title="",
                xtitle="",
                ytitle="",
                xum="[mm]",
                yum="[mm]",
                plotting_range=None,
                factor1=1.0,
                factor2=1.0,
                colormap=None):

        if xx is None:
            xx = numpy.arange(histogram.shape[0])

        if yy is None:
            yy = numpy.arange(histogram.shape[1])

        if plotting_range == None:
            nbins_h = xx.size
            nbins_v = yy.size
        else:
            range_x = numpy.where(
                numpy.logical_and(xx >= plotting_range[0],
                                  xx <= plotting_range[1]))
            range_y = numpy.where(
                numpy.logical_and(yy >= plotting_range[2],
                                  yy <= plotting_range[3]))

            xx = xx[range_x]
            yy = yy[range_y]

            nbins_h = xx.size
            nbins_v = yy.size

        if len(xx) == 0 or len(yy) == 0:
            raise Exception("Nothing to plot in the given range")

        xmin, xmax = xx.min(), xx.max()
        ymin, ymax = yy.min(), yy.max()

        origin = (xmin * factor1, ymin * factor2)
        scale = (abs(
            (xmax - xmin) / nbins_h) * factor1, abs(
                (ymax - ymin) / nbins_v) * factor2)

        # silx inverts axis!!!! histogram must be calculated reversed
        data_to_plot = []
        for y_index in range(0, nbins_v):
            x_values = []
            for x_index in range(0, nbins_h):
                x_values.append(histogram[x_index][y_index])

            data_to_plot.append(x_values)

        data_to_plot = numpy.array(data_to_plot)

        histogram_h = numpy.sum(data_to_plot,
                                axis=0)  # data to plot axis are inverted
        histogram_v = numpy.sum(data_to_plot, axis=1)

        ticket = {}
        ticket['total'] = numpy.sum(data_to_plot)

        ticket['fwhm_h'], ticket['fwhm_quote_h'], ticket[
            'fwhm_coordinates_h'] = get_fwhm(histogram_h, xx)
        ticket['sigma_h'] = get_sigma(histogram_h, xx)

        ticket['fwhm_v'], ticket['fwhm_quote_v'], ticket[
            'fwhm_coordinates_v'] = get_fwhm(histogram_v, yy)
        ticket['sigma_v'] = get_sigma(histogram_v, yy)

        self.plot_canvas.setColormap(colormap=colormap)
        self.plot_canvas.setImage(data_to_plot, origin=origin, scale=scale)

        self.plot_canvas.setGraphXLabel(xtitle)
        self.plot_canvas.setGraphYLabel(ytitle)
        self.plot_canvas.setGraphTitle(title)

        self.plot_canvas._histoHPlot.setGraphYLabel('Counts')

        self.plot_canvas._histoHPlot._backend.ax.xaxis.get_label().set_color(
            'white')
        self.plot_canvas._histoHPlot._backend.ax.xaxis.get_label(
        ).set_fontsize(1)
        for label in self.plot_canvas._histoHPlot._backend.ax.xaxis.get_ticklabels(
        ):
            label.set_color('white')
            label.set_fontsize(1)

        self.plot_canvas._histoVPlot.setGraphXLabel('Counts')

        self.plot_canvas._histoVPlot._backend.ax.yaxis.get_label().set_color(
            'white')
        self.plot_canvas._histoVPlot._backend.ax.yaxis.get_label(
        ).set_fontsize(1)
        for label in self.plot_canvas._histoVPlot._backend.ax.yaxis.get_ticklabels(
        ):
            label.set_color('white')
            label.set_fontsize(1)

        n_patches = len(self.plot_canvas._histoHPlot._backend.ax.patches)
        if (n_patches > 0):
            self.plot_canvas._histoHPlot._backend.ax.patches.remove(
                self.plot_canvas._histoHPlot._backend.ax.patches[n_patches -
                                                                 1])

        if not ticket['fwhm_h'] == 0.0:
            x_fwhm_i, x_fwhm_f = ticket['fwhm_coordinates_h']
            x_fwhm_i, x_fwhm_f = x_fwhm_i * factor1, x_fwhm_f * factor1
            y_fwhm = ticket['fwhm_quote_h']

            self.plot_canvas._histoHPlot._backend.ax.add_patch(
                FancyArrowPatch([x_fwhm_i, y_fwhm], [x_fwhm_f, y_fwhm],
                                arrowstyle=ArrowStyle.CurveAB(head_width=2,
                                                              head_length=4),
                                color='b',
                                linewidth=1.5))

        n_patches = len(self.plot_canvas._histoVPlot._backend.ax.patches)
        if (n_patches > 0):
            self.plot_canvas._histoVPlot._backend.ax.patches.remove(
                self.plot_canvas._histoVPlot._backend.ax.patches[n_patches -
                                                                 1])

        if not ticket['fwhm_v'] == 0.0:
            y_fwhm_i, y_fwhm_f = ticket['fwhm_coordinates_v']
            y_fwhm_i, y_fwhm_f = y_fwhm_i * factor2, y_fwhm_f * factor2
            x_fwhm = ticket['fwhm_quote_v']

            self.plot_canvas._histoVPlot._backend.ax.add_patch(
                FancyArrowPatch([x_fwhm, y_fwhm_i], [x_fwhm, y_fwhm_f],
                                arrowstyle=ArrowStyle.CurveAB(head_width=2,
                                                              head_length=4),
                                color='r',
                                linewidth=1.5))

        self.plot_canvas._histoHPlot.replot()
        self.plot_canvas._histoVPlot.replot()
        self.plot_canvas.replot()

        self.info_box.total.setText("{:.3e}".format(
            decimal.Decimal(ticket['total'])))
        self.info_box.fwhm_h.setText("{:5.4f}".format(ticket['fwhm_h'] *
                                                      factor1))
        self.info_box.fwhm_v.setText("{:5.4f}".format(ticket['fwhm_v'] *
                                                      factor2))
        self.info_box.label_h.setText("FWHM " + xum)
        self.info_box.label_v.setText("FWHM " + yum)
        self.info_box.sigma_h.setText("{:5.4f}".format(ticket['sigma_h'] *
                                                       factor1))
        self.info_box.sigma_v.setText("{:5.4f}".format(ticket['sigma_v'] *
                                                       factor2))
        self.info_box.label_s_h.setText("\u03c3 " + xum)
        self.info_box.label_s_v.setText("\u03c3 " + yum)

    def clear(self):
        self.plot_canvas.clear()

        self.plot_canvas._histoHPlot.clear()
        self.plot_canvas._histoVPlot.clear()

        self.plot_canvas._histoHPlot._backend.ax.xaxis.get_label().set_color(
            'white')
        self.plot_canvas._histoHPlot._backend.ax.xaxis.get_label(
        ).set_fontsize(1)
        for label in self.plot_canvas._histoHPlot._backend.ax.xaxis.get_ticklabels(
        ):
            label.set_color('white')
            label.set_fontsize(1)

        self.plot_canvas._histoVPlot._backend.ax.yaxis.get_label().set_color(
            'white')
        self.plot_canvas._histoVPlot._backend.ax.yaxis.get_label(
        ).set_fontsize(1)
        for label in self.plot_canvas._histoVPlot._backend.ax.yaxis.get_ticklabels(
        ):
            label.set_color('white')
            label.set_fontsize(1)

        self.plot_canvas._histoHPlot.setGraphYLabel('')
        self.plot_canvas._histoVPlot.setGraphXLabel('')

        self.plot_canvas._histoHPlot.replot()
        self.plot_canvas._histoVPlot.replot()

        self.info_box.clear()