From 3906be4e271fde0d5b8e8611063bffb72854e906 Mon Sep 17 00:00:00 2001 From: James Kerns Date: Thu, 1 Aug 2024 16:36:33 -0500 Subject: [PATCH] docstrings, PR comment --- pylinac/acr.py | 18 +++++++++++++++--- pylinac/cheese.py | 17 +++++++++++++++-- pylinac/core/image.py | 27 ++++++++++++++++++++++----- pylinac/core/plotly_utils.py | 33 ++++++++++++++------------------- pylinac/ct.py | 20 ++++++++++++++++++++ pylinac/picketfence.py | 13 +++++++------ pylinac/planar_imaging.py | 17 ++++++++++++++--- pylinac/quart.py | 30 +++++++++++++++++++++++++++--- pylinac/starshot.py | 17 +++++++++++++++-- pylinac/vmat.py | 20 +++++++++++++++++++- pylinac/winston_lutz.py | 31 ++++++++++++++++++++++++------- 11 files changed, 192 insertions(+), 51 deletions(-) diff --git a/pylinac/acr.py b/pylinac/acr.py index e2c53588..241ed0c4 100644 --- a/pylinac/acr.py +++ b/pylinac/acr.py @@ -1260,12 +1260,24 @@ def plotly_analyzed_image( show_legend: bool = True, **kwargs, ) -> dict[str, go.Figure]: - """Plot the analyzed image using Plotly + """Plot the analyzed set of images to Plotly figures. Parameters ---------- - show - Whether to show the image. + show : bool + Whether to show the plot. + show_colorbar : bool + Whether to show the colorbar on the plot. + show_legend : bool + Whether to show the legend on the plot. + kwargs + Additional keyword arguments to pass to the plot. + + Returns + ------- + dict + A dictionary of the Plotly figures where the key is the name of the + image and the value is the figure. """ figs = {} # plot the images diff --git a/pylinac/cheese.py b/pylinac/cheese.py index 55da00ba..3b9d76cf 100644 --- a/pylinac/cheese.py +++ b/pylinac/cheese.py @@ -310,12 +310,25 @@ def plotly_analyzed_image( show_legend: bool = True, **kwargs, ) -> dict[str, go.Figure]: - """Plot the images used in the calculation and summary data. + """Plot the analyzed set of images to Plotly figures. + Parameters ---------- show : bool - Whether to plot the image or not. + Whether to show the plot. + show_colorbar : bool + Whether to show the colorbar on the plot. + show_legend : bool + Whether to show the legend on the plot. + kwargs + Additional keyword arguments to pass to the plot. + + Returns + ------- + dict + A dictionary of the Plotly figures where the key is the name of the + image and the value is the figure. """ figs = { self.module.common_name: self.module.plotly( diff --git a/pylinac/core/image.py b/pylinac/core/image.py index 53454dbb..033c642c 100644 --- a/pylinac/core/image.py +++ b/pylinac/core/image.py @@ -49,6 +49,7 @@ retrieve_dicom_file, retrieve_filenames, ) +from .plotly_utils import add_title from .profile import stretch as stretcharray from .scale import MachineScale, convert, wrap360 from .utilities import decode_binary, is_close, simple_round @@ -505,14 +506,29 @@ def plotly( show_colorbar: bool = True, **kwargs, ) -> go.Figure: - """Plot the image in a plotly figure""" + """Plot the image in a plotly figure. + + Parameters + ---------- + fig: plotly.graph_objects.Figure + The figure to plot to. If None, a new figure is created. + colorscale: str + The colorscale to use on the plot. See https://plotly.com/python/builtin-colorscales/ + show : bool + Whether to show the plot. Set to False if performing later adjustments to the plot. + show_metrics : bool + Whether to show the metrics on the image. + title: str + The title of the plot. + show_colorbar : bool + Whether to show the colorbar on the plot. + kwargs + Additional keyword arguments to pass to the plot. + """ + if fig is None: fig = go.Figure() fig.update_layout( - title={ - "text": title, - "x": 0.5, - }, xaxis_showticklabels=False, yaxis_showticklabels=False, # this inverts the y axis so 0 is at the top @@ -526,6 +542,7 @@ def plotly( legend={"x": 0}, showlegend=kwargs.pop("show_legend", True), ) + add_title(fig, title) fig.add_heatmap(z=self.array, colorscale=colorscale, **kwargs) fig.update_traces(showscale=show_colorbar) if show_metrics: diff --git a/pylinac/core/plotly_utils.py b/pylinac/core/plotly_utils.py index ceae82ec..279f9d8e 100644 --- a/pylinac/core/plotly_utils.py +++ b/pylinac/core/plotly_utils.py @@ -3,36 +3,26 @@ from plotly import graph_objects as go -def add_title(fig: go.Figure, title: str): +def add_title(fig: go.Figure, title: str) -> None: """Set the title of a plotly figure at the center of the image""" fig.update_layout(title_text=title, title_x=0.5) -def set_axis_range(fig: go.Figure, x: Sequence[float], y: Sequence[float]): - """Set the axis range of a plotly figure. There's some bug in ploty that won't +def set_axis_range(fig: go.Figure, x: Sequence[float], y: Sequence[float]) -> None: + """Set the axis range of a plotly figure. There's some bug in plotly that won't correctly range the Y axis if autorange is already set. This works around that bug and in one spot vs trying to remember on each call.""" fig.update_layout(xaxis_range=x, yaxis_range=y, yaxis_autorange=False) -def fixed_aspect_ratio(fig: go.Figure): - """Set the aspect ratio of a plotly figure to be fixed.""" - fig.update_layout( - yaxis_scaleanchor="x", - yaxis_constrain="domain", - xaxis_scaleanchor="y", - xaxis_constrain="domain", - ) - - def add_vertical_line( fig: go.Figure, x: float, - color="black", - width=1, - opacity=1, + color: str = "black", + width: int = 1, + opacity: float = 1, name: str = "", -): +) -> None: """Add a vertical line to a plotly figure.""" # get the current data limits # otherwise this can fall outside the image plot @@ -53,8 +43,13 @@ def add_vertical_line( def add_horizontal_line( - fig: go.Figure, y: float, color="black", width=1, opacity=1, name: str = "" -): + fig: go.Figure, + y: float, + color: str = "black", + width: int = 1, + opacity: float = 1, + name: str = "", +) -> None: """Add a horizontal line to a plotly figure.""" d = None for trace in fig.data: diff --git a/pylinac/ct.py b/pylinac/ct.py index 7e3b2c85..55646fdc 100644 --- a/pylinac/ct.py +++ b/pylinac/ct.py @@ -1813,6 +1813,26 @@ def plotly_analyzed_image( show_legend: bool = True, **kwargs, ) -> dict[str, go.Figure]: + """Plot the analyzed set of images to Plotly figures. + + + Parameters + ---------- + show : bool + Whether to show the plot. + show_colorbar : bool + Whether to show the colorbar on the plot. + show_legend : bool + Whether to show the legend on the plot. + kwargs + Additional keyword arguments to pass to the plot. + + Returns + ------- + dict + A dictionary of the Plotly figures where the key is the name of the + image and the value is the figure. + """ figs = {} figs["CTP404"] = self.ctp404.plotly() figs["HU Linearity"] = self.ctp404.plotly_linearity() diff --git a/pylinac/picketfence.py b/pylinac/picketfence.py index 2820cf2f..ec920dc0 100644 --- a/pylinac/picketfence.py +++ b/pylinac/picketfence.py @@ -900,21 +900,22 @@ def plotly_analyzed_image( show_legend: bool = True, **kwargs, ) -> dict[str, go.Figure]: - """Plot the analyzed image. + """Plot the analyzed image, leaf error plots, and error histogram. Parameters ---------- - guard_rails - Do/don't plot the picket "guard rails" around the ideal picket mlc_peaks Do/don't plot the detected MLC peak positions. overlay Do/don't plot the alpha overlay of the leaf status. - leaf_error_subplot - If True, plots a linked leaf error subplot adjacent to the PF image plotting the average and standard - deviation of leaf error. show Whether to display the plot. Set to false for saving to a figure, etc. + show_colorbar + Whether to show the colorbar. + show_legend + Whether to show the legend. + kwargs + Keyword arguments to pass to the plotly image plot. """ if not self._is_analyzed: raise RuntimeError("The image must be analyzed first. Use .analyze().") diff --git a/pylinac/planar_imaging.py b/pylinac/planar_imaging.py index 4cdf8668..bcefc3b5 100644 --- a/pylinac/planar_imaging.py +++ b/pylinac/planar_imaging.py @@ -598,14 +598,25 @@ def plotly_analyzed_images( show_colorbar: bool = True, **kwargs, ) -> dict[str, go.Figure]: - """Plot the analyzed image. + """Plot the analyzed set of images to Plotly figures. + Parameters ---------- show : bool - Whether to actually show the image when called. + Whether to show the plot. + show_colorbar : bool + Whether to show the colorbar on the plot. show_legend : bool - Whether to show the legend of ROIs on the image plot. + Whether to show the legend on the plot. + kwargs + Additional keyword arguments to pass to the plot. + + Returns + ------- + dict + A dictionary of the Plotly figures where the key is the name of the + image and the value is the figure. """ figs = {} # plot the marked image diff --git a/pylinac/quart.py b/pylinac/quart.py index 53ea69fc..7db9544e 100644 --- a/pylinac/quart.py +++ b/pylinac/quart.py @@ -448,13 +448,37 @@ def plotly_analyzed_images( show_colorbar: bool = True, **kwargs, ) -> dict[str, go.Figure]: + """Plot the analyzed set of images to Plotly figures. + + + Parameters + ---------- + show : bool + Whether to show the plot. + show_colorbar : bool + Whether to show the colorbar on the plot. + show_legend : bool + Whether to show the legend on the plot. + kwargs + Additional keyword arguments to pass to the plot. + + Returns + ------- + dict + A dictionary of the Plotly figures where the key is the name of the + image and the value is the figure. + """ figs = {} figs[self.hu_module.common_name] = self.hu_module.plotly( - show_colorbar=show_colorbar, show_legend=show_legend + show_colorbar=show_colorbar, show_legend=show_legend, **kwargs ) figs["HU Linearity plot"] = self.hu_module.plotly_linearity() - figs[self.uniformity_module.common_name] = self.uniformity_module.plotly() - figs[self.geometry_module.common_name] = self.geometry_module.plotly() + figs[self.uniformity_module.common_name] = self.uniformity_module.plotly( + show_colorbar=show_colorbar, show_legend=show_legend, **kwargs + ) + figs[self.geometry_module.common_name] = self.geometry_module.plotly( + show_colorbar=show_colorbar, show_legend=show_legend, **kwargs + ) figs["Side View"] = self.plotly_side_view(offset=-5) if show: diff --git a/pylinac/starshot.py b/pylinac/starshot.py index 198a6e55..9bb65e63 100644 --- a/pylinac/starshot.py +++ b/pylinac/starshot.py @@ -432,12 +432,25 @@ def plotly_analyzed_images( show_legend: bool = True, **kwargs, ) -> dict[str, go.Figure]: - """Plot the analyzed image with plotly. Will produce two figures: the whole image and one zoomed into the wobble circle. + """Plot the analyzed set of images to Plotly figures. Will plot a zoomed-out image and a zoomed-in image. + Parameters ---------- show : bool - Whether to actually show the image. Set to False if doing further annotations. + Whether to show the plot. + show_colorbar : bool + Whether to show the colorbar on the plot. + show_legend : bool + Whether to show the legend on the plot. + kwargs + Additional keyword arguments to pass to the plot. + + Returns + ------- + dict + A dictionary of the Plotly figures where the key is the name of the + image and the value is the figure. """ figs = {} for name, zoom in zip(("Image", "Wobble"), (False, True)): diff --git a/pylinac/vmat.py b/pylinac/vmat.py index 7e062cb0..c0b8a10e 100644 --- a/pylinac/vmat.py +++ b/pylinac/vmat.py @@ -421,7 +421,25 @@ def plotly_analyzed_images( show_legend: bool = True, **kwargs, ) -> dict[str, go.Figure]: - """Plot both images and the median profiles in 3 Plotly figures.""" + """Plot the analyzed set of images to Plotly figures. + + Parameters + ---------- + show : bool + Whether to show the plot. + show_colorbar : bool + Whether to show the colorbar on the plot. + show_legend : bool + Whether to show the legend on the plot. + kwargs + Additional keyword arguments to pass to the plot. + + Returns + ------- + dict + A dictionary of the Plotly figures where the key is the name of the + image and the value is the figure. + """ # images fig_open = self.open_image.plotly( diff --git a/pylinac/winston_lutz.py b/pylinac/winston_lutz.py index 32f812b5..d6f30f47 100644 --- a/pylinac/winston_lutz.py +++ b/pylinac/winston_lutz.py @@ -1708,12 +1708,22 @@ def plotly_analyzed_images( Parameters ---------- + zoomed : bool + Whether to zoom in on the BBs of the 2D images. + show_legend : bool + Whether to show the legend on the plot. + show : bool + Whether to show the plot. + show_colorbar : bool + Whether to show the colorbar on the plot. kwargs - Keyword arguments to pass to the plotly figure. + Additional keyword arguments to pass to the plot. Returns ------- - go.Figure + dict + A dictionary of the Plotly figures where the key is the name of the + image and the value is the figure. """ figs = {} for idx, wl_image in enumerate(self.images): @@ -2817,20 +2827,27 @@ def plotly_analyzed_images( show_colorbar: bool = True, **kwargs, ) -> dict[str, go.Figure]: - """Plot the analyzed images using Plotly. + """Plot the analyzed set of images to Plotly figures. + Parameters ---------- zoomed : bool - Whether to zoom in on the BBs. - show_legend : bool - Whether to show the legend on the plot. + Whether to zoom in on the 2D image plots. show : bool Whether to show the plot. show_colorbar : bool Whether to show the colorbar on the plot. + show_legend : bool + Whether to show the legend on the plot. kwargs - Additional keyword arguments to pass to the plot + Additional keyword arguments to pass to the plot. + + Returns + ------- + dict + A dictionary of the Plotly figures where the key is the name of the + image and the value is the figure. """ figs = {} for idx, wl_image in enumerate(self.images):