Ejemplo n.º 1
0
    def save_analyzed_image(self,
                            filename,
                            guard_rails=True,
                            mlc_peaks=True,
                            overlay=True,
                            leaf_error_subplot=False,
                            interactive=False,
                            **kwargs):
        """Save the analyzed figure to a file. See :meth:`~pylinac.picketfence.PicketFence.plot_analyzed_image()` for
        further parameter info.

        interactive : bool
            If False (default), saves the figure as a .png image.
            If True, saves an html file, which can be opened in a browser, etc.

            .. note:: mpld3 must be installed to use this feature.
        """
        self.plot_analyzed_image(guard_rails,
                                 mlc_peaks,
                                 overlay,
                                 leaf_error_subplot=leaf_error_subplot,
                                 interactive=interactive,
                                 show=False)
        if interactive:
            mpld3 = import_mpld3()
            mpld3.save_html(plt.gcf(), filename)
        else:
            plt.savefig(filename, **kwargs)
        if isinstance(filename, str):
            print("Picket fence image saved to: {}".format(
                osp.abspath(filename)))
Ejemplo n.º 2
0
    def save_analyzed_subimage(self,
                               filename,
                               subimage='dmlc',
                               interactive=False,
                               **kwargs):
        """Save a subplot to file.

        Parameters
        ----------
        filename : str, file-object
            Where to save the file to.
        subimage : str
            Which subplot to save.
        interactive : bool
            Only applicable for the profile subplot. If False, saves as a .png image, else saves as an interactive
            HTML file.
        kwargs
            Passed to matplotlib.
        """
        self.plot_analyzed_subimage(subimage, show=False)
        if interactive and (subimage == PROFILE):
            mpld3 = import_mpld3()
            mpld3.save_html(plt.gcf(), filename)
        else:
            plt.savefig(filename, **kwargs)
        if isinstance(filename, str):
            print("VMAT subimage figure saved to {}".format(
                osp.abspath(filename)))
Ejemplo n.º 3
0
    def _add_leaf_error_subplot(self, ax, fig, interactive):
        """Add a bar subplot showing the leaf error."""
        tol_line_height = [self.settings.tolerance, self.settings.tolerance]
        tol_line_width = [0, max(self.image.shape)]

        # make the new axis
        divider = make_axes_locatable(ax)
        if self.settings.orientation == orientations['UD']:
            axtop = divider.append_axes('right', 2, pad=1, sharey=ax)
        else:
            axtop = divider.append_axes('bottom', 2, pad=1, sharex=ax)

        # get leaf positions, errors, standard deviation, and leaf numbers
        pos, vals, err, leaf_nums = self.pickets.error_hist()

        # plot the leaf errors as a bar plot
        if self.settings.orientation == orientations['UD']:
            axtop.barh(pos,
                       vals,
                       xerr=err,
                       height=self.pickets[0].sample_width * 2,
                       alpha=0.4,
                       align='center')
            # plot the tolerance line(s)
            # TODO: replace .plot() calls with .axhline when mpld3 fixes funtionality
            axtop.plot(tol_line_height, tol_line_width, 'r-', linewidth=3)
            if self.settings.action_tolerance is not None:
                axtop.plot(tol_line_height, tol_line_width, 'y-', linewidth=3)

            # reset xlims to comfortably include the max error or tolerance value
            axtop.set_xlim([0, max(max(vals), self.settings.tolerance) + 0.1])
        else:
            axtop.bar(pos,
                      vals,
                      yerr=err,
                      width=self.pickets[0].sample_width * 2,
                      alpha=0.4,
                      align='center')
            axtop.plot(tol_line_width, tol_line_height, 'r-', linewidth=3)
            if self.settings.action_tolerance is not None:
                axtop.plot(tol_line_width, tol_line_height, 'y-', linewidth=3)
            axtop.set_ylim([0, max(max(vals), self.settings.tolerance) + 0.1])

        # add formatting to axis
        axtop.grid('on')
        axtop.set_title("Average Error (mm)")

        # add tooltips if interactive
        if interactive:
            labels = [[
                'Leaf pair {}/{}, Avg Error: {:3.3f}mm, Stdev: {:3.3f}mm'.
                format(leaf_num[0], leaf_num[1], err, std)
            ] for leaf_num, err, std in zip(leaf_nums, vals, err)]
            mpld3 = import_mpld3()
            for num, patch in enumerate(axtop.axes.patches):
                ttip = mpld3.plugins.PointLabelTooltip(patch,
                                                       labels[num],
                                                       location='top left')
                mpld3.plugins.connect(fig, ttip)
Ejemplo n.º 4
0
    def plot_analyzed_image(self, guard_rails=True, mlc_peaks=True, overlay=True, leaf_error_subplot=True, interactive=False, show=True):
        """Plot the analyzed image.

        Parameters
        ----------
        guard_rails : bool
            Do/don't plot the picket "guard rails" around the ideal picket
        mlc_peaks : bool
            Do/don't plot the MLC positions.
        overlay : bool
            Do/don't plot the alpha overlay of the leaf status.
        leaf_error_subplot : bool

            .. versionadded:: 1.0

            If True, plots a linked leaf error subplot adjacent to the PF image plotting the average and standard
            deviation of leaf error.
        interactive : bool

            .. versionadded:: 1.0
            .. note:: mpld3 must be installed to use this feature.

            If False (default), plots a matplotlib figure.
            If True, plots a MPLD3 local server image, which adds some tooltips.
        """
        # plot the image
        fig, ax = plt.subplots(figsize=self.settings.figure_size)
        ax.imshow(self.image.array, cmap=plt.cm.Greys)

        # generate a leaf error subplot if desired
        if leaf_error_subplot:
            self._add_leaf_error_subplot(ax, fig, interactive)

        # plot guard rails and mlc peaks as desired
        for p_num, picket in enumerate(self.pickets):
            if guard_rails:
                picket.add_guards_to_axes(ax.axes)
            if mlc_peaks:
                for idx, mlc_meas in enumerate(picket.mlc_meas):
                    mlc_meas.add_to_axes(ax.axes, width=1.5)

        # plot the overlay if desired.
        if overlay:
            o = Overlay(self.image, self.settings, self.pickets)
            o.add_to_axes(ax)

        # tighten up the plot view
        ax.set_xlim([0, self.image.shape[1]])
        ax.set_ylim([0, self.image.shape[0]])
        ax.axis('off')

        if show:
            if interactive:
                mpld3 = import_mpld3()
                mpld3.show()
            else:
                plt.show()
Ejemplo n.º 5
0
    def _add_leaf_error_subplot(self, ax, fig, interactive):
        """Add a bar subplot showing the leaf error."""
        tol_line_height = [self.settings.tolerance, self.settings.tolerance]
        tol_line_width = [0, max(self.image.shape)]

        # make the new axis
        divider = make_axes_locatable(ax)
        if self.settings.orientation == orientations['UD']:
            axtop = divider.append_axes('right', 2, pad=1, sharey=ax)
        else:
            axtop = divider.append_axes('bottom', 2, pad=1, sharex=ax)

        # get leaf positions, errors, standard deviation, and leaf numbers
        pos, vals, err, leaf_nums = self.pickets.error_hist()

        # plot the leaf errors as a bar plot
        if self.settings.orientation == orientations['UD']:
            axtop.barh(pos, vals, xerr=err, height=self.pickets[0].sample_width * 2, alpha=0.4, align='center')
            # plot the tolerance line(s)
            # TODO: replace .plot() calls with .axhline when mpld3 fixes funtionality
            axtop.plot(tol_line_height, tol_line_width, 'r-', linewidth=3)
            if self.settings.action_tolerance is not None:
                axtop.plot(tol_line_height, tol_line_width, 'y-', linewidth=3)

            # reset xlims to comfortably include the max error or tolerance value
            axtop.set_xlim([0, max(max(vals), self.settings.tolerance) + 0.1])
        else:
            axtop.bar(pos, vals, yerr=err, width=self.pickets[0].sample_width * 2, alpha=0.4, align='center')
            axtop.plot(tol_line_width, tol_line_height,
                       'r-', linewidth=3)
            if self.settings.action_tolerance is not None:
                axtop.plot(tol_line_width, tol_line_height, 'y-', linewidth=3)
            axtop.set_ylim([0, max(max(vals), self.settings.tolerance) + 0.1])

        # add formatting to axis
        axtop.grid('on')
        axtop.set_title("Average Error (mm)")

        # add tooltips if interactive
        if interactive:
            labels = [['Leaf pair {}/{}, Avg Error: {:3.3f}mm, Stdev: {:3.3f}mm'.format(leaf_num[0], leaf_num[1], err, std)]
                      for leaf_num, err, std in zip(leaf_nums, vals, err)]
            mpld3 = import_mpld3()
            for num, patch in enumerate(axtop.axes.patches):
                ttip = mpld3.plugins.PointLabelTooltip(patch, labels[num], location='top left')
                mpld3.plugins.connect(fig, ttip)
Ejemplo n.º 6
0
    def save_analyzed_image(self, filename, guard_rails=True, mlc_peaks=True, overlay=True, leaf_error_subplot=False, interactive=False, **kwargs):
        """Save the analyzed figure to a file. See :meth:`~pylinac.picketfence.PicketFence.plot_analyzed_image()` for
        further parameter info.

        interactive : bool
            If False (default), saves the figure as a .png image.
            If True, saves an html file, which can be opened in a browser, etc.

            .. note:: mpld3 must be installed to use this feature.
        """
        self.plot_analyzed_image(guard_rails, mlc_peaks, overlay, leaf_error_subplot=leaf_error_subplot,
                                 interactive=interactive, show=False)
        if interactive:
            mpld3 = import_mpld3()
            mpld3.save_html(plt.gcf(), filename)
        else:
            plt.savefig(filename, **kwargs)
        if isinstance(filename, str):
            print("Picket fence image saved to: {}".format(osp.abspath(filename)))
Ejemplo n.º 7
0
    def save_analyzed_subimage(self, filename, subimage='dmlc', interactive=False, **kwargs):
        """Save a subplot to file.

        Parameters
        ----------
        filename : str, file-object
            Where to save the file to.
        subimage : str
            Which subplot to save.
        interactive : bool
            Only applicable for the profile subplot. If False, saves as a .png image, else saves as an interactive
            HTML file.
        kwargs
            Passed to matplotlib.
        """
        self.plot_analyzed_subimage(subimage, show=False)
        if interactive and (subimage == PROFILE):
            mpld3 = import_mpld3()
            mpld3.save_html(plt.gcf(), filename)
        else:
            plt.savefig(filename, **kwargs)
        if isinstance(filename, str):
            print("VMAT subimage figure saved to {}".format(osp.abspath(filename)))
Ejemplo n.º 8
0
    def plot_analyzed_image(self,
                            guard_rails=True,
                            mlc_peaks=True,
                            overlay=True,
                            leaf_error_subplot=True,
                            interactive=False,
                            show=True):
        """Plot the analyzed image.

        Parameters
        ----------
        guard_rails : bool
            Do/don't plot the picket "guard rails" around the ideal picket
        mlc_peaks : bool
            Do/don't plot the MLC positions.
        overlay : bool
            Do/don't plot the alpha overlay of the leaf status.
        leaf_error_subplot : bool

            .. versionadded:: 1.0

            If True, plots a linked leaf error subplot adjacent to the PF image plotting the average and standard
            deviation of leaf error.
        interactive : bool

            .. versionadded:: 1.0
            .. note:: mpld3 must be installed to use this feature.

            If False (default), plots a matplotlib figure.
            If True, plots a MPLD3 local server image, which adds some tooltips.
        """
        # plot the image
        fig, ax = plt.subplots(figsize=self.settings.figure_size)
        ax.imshow(self.image.array, cmap=plt.cm.Greys)

        # generate a leaf error subplot if desired
        if leaf_error_subplot:
            self._add_leaf_error_subplot(ax, fig, interactive)

        # plot guard rails and mlc peaks as desired
        for p_num, picket in enumerate(self.pickets):
            if guard_rails:
                picket.add_guards_to_axes(ax.axes)
            if mlc_peaks:
                for idx, mlc_meas in enumerate(picket.mlc_meas):
                    mlc_meas.add_to_axes(ax.axes, width=1.5)

        # plot the overlay if desired.
        if overlay:
            o = Overlay(self.image, self.settings, self.pickets)
            o.add_to_axes(ax)

        # tighten up the plot view
        ax.set_xlim([0, self.image.shape[1]])
        ax.set_ylim([0, self.image.shape[0]])
        ax.axis('off')

        if show:
            if interactive:
                mpld3 = import_mpld3()
                mpld3.show()
            else:
                plt.show()