Skip to content

Commit

Permalink
docstrings, PR comment
Browse files Browse the repository at this point in the history
  • Loading branch information
jrkerns committed Aug 1, 2024
1 parent a718901 commit 3906be4
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 51 deletions.
18 changes: 15 additions & 3 deletions pylinac/acr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 15 additions & 2 deletions pylinac/cheese.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
27 changes: 22 additions & 5 deletions pylinac/core/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down
33 changes: 14 additions & 19 deletions pylinac/core/plotly_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
20 changes: 20 additions & 0 deletions pylinac/ct.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
13 changes: 7 additions & 6 deletions pylinac/picketfence.py
Original file line number Diff line number Diff line change
Expand Up @@ -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().")
Expand Down
17 changes: 14 additions & 3 deletions pylinac/planar_imaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 27 additions & 3 deletions pylinac/quart.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
17 changes: 15 additions & 2 deletions pylinac/starshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)):
Expand Down
20 changes: 19 additions & 1 deletion pylinac/vmat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading

0 comments on commit 3906be4

Please sign in to comment.