Skip to content

Commit

Permalink
Merged in feature/RAM-2254_custom_logo_in_pdfs (pull request #149)
Browse files Browse the repository at this point in the history
RAM-2254 add ability to pass custom logo to PDF reports

Approved-by: Randy Taylor
  • Loading branch information
jrkerns committed Jan 4, 2023
2 parents aae2109 + 53449c8 commit 957bb66
Show file tree
Hide file tree
Showing 13 changed files with 172 additions and 18 deletions.
8 changes: 7 additions & 1 deletion pylinac/acr.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ def publish_pdf(
notes: Optional[str] = None,
open_file: bool = False,
metadata: Optional[dict] = None,
logo: Optional[Union[Path, str]] = None
) -> None:
"""Publish (print) a PDF containing the analysis and quantitative results.
Expand All @@ -484,6 +485,8 @@ def publish_pdf(
Author: James
Unit: TrueBeam
--------------
logo: Path, str
A custom logo to use in the PDF report. If nothing is passed, the default pylinac logo is used.
"""
analysis_title = f"{self._model} Analysis"
texts = [
Expand All @@ -495,7 +498,7 @@ def publish_pdf(
analysis_images = self.save_images(to_stream=True)

canvas = pdf.PylinacCanvas(
filename, page_title=analysis_title, metadata=metadata
filename, page_title=analysis_title, metadata=metadata, logo=logo
)
if notes is not None:
canvas.add_text(text="Notes:", location=(1, 4.5), font_size=14)
Expand Down Expand Up @@ -1073,6 +1076,7 @@ def publish_pdf(
notes: Optional[str] = None,
open_file: bool = False,
metadata: Optional[dict] = None,
logo: Optional[Union[Path, str]] = None
) -> None:
"""Publish (print) a PDF containing the analysis and quantitative results.
Expand All @@ -1092,6 +1096,8 @@ def publish_pdf(
Author: James
Unit: TrueBeam
--------------
logo: Path, str
A custom logo to use in the PDF report. If nothing is passed, the default pylinac logo is used.
"""
analysis_title = f"{self._model} Analysis"
analysis_images = self.save_images(to_stream=True)
Expand Down
22 changes: 12 additions & 10 deletions pylinac/core/pdf.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"""Module for constructing and interacting with PDF reports for Pylinac."""
from datetime import datetime
from typing import List, Union, Sequence, Tuple
import io
import os.path as osp
from datetime import datetime
from pathlib import Path
from typing import List, Union, Sequence, Optional

from PIL import Image
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.units import cm
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import cm
from reportlab.lib.utils import ImageReader
from reportlab.pdfgen.canvas import Canvas

from .. import __version__

Expand All @@ -21,11 +21,13 @@ def __init__(
font: str = "Helvetica",
metadata: dict = None,
metadata_location: tuple = (2, 25.5),
logo: Optional[Union[Path, str]] = None
):
self.canvas = Canvas(filename, pagesize=A4)
self._font = font
self._title = page_title
self._metadata = metadata
self._logo = logo
self._metadata_location = metadata_location
self._generate_pylinac_template_theme()
self._add_metadata()
Expand All @@ -46,12 +48,12 @@ def _add_metadata(self) -> None:

def _generate_pylinac_template_theme(self) -> None:
# draw logo and header separation line
if self._logo is None:
logo = Path(__file__).parent.parent / 'files' / 'Pylinac Full cropped.png'
else:
logo = self._logo
self.canvas.drawImage(
osp.join(
osp.dirname(osp.dirname(osp.abspath(__file__))),
"files",
"Pylinac Full cropped.png",
),
logo,
1 * cm,
26.5 * cm,
width=5 * cm,
Expand Down
7 changes: 6 additions & 1 deletion pylinac/ct.py
Original file line number Diff line number Diff line change
Expand Up @@ -1870,6 +1870,7 @@ def publish_pdf(
notes: Optional[str] = None,
open_file: bool = False,
metadata: Optional[dict] = None,
logo: Optional[Union[Path, str]] = None
) -> None:
"""Publish (print) a PDF containing the analysis and quantitative results.
Expand All @@ -1889,6 +1890,8 @@ def publish_pdf(
Author: James
Unit: TrueBeam
--------------
logo: Path, str
A custom logo to use in the PDF report. If nothing is passed, the default pylinac logo is used.
"""
analysis_title = f"CatPhan {self._model} Analysis"
module_images = [("hu", "lin")]
Expand All @@ -1906,6 +1909,7 @@ def publish_pdf(
analysis_title,
self.results(as_list=True),
module_images,
logo
)
if open_file:
webbrowser.open(filename)
Expand All @@ -1918,6 +1922,7 @@ def _publish_pdf(
analysis_title: str,
texts: Sequence[str],
imgs: Sequence[Tuple[str, str]],
logo: Optional[Union[Path, str]] = None
):
try:
date = datetime.strptime(
Expand All @@ -1926,7 +1931,7 @@ def _publish_pdf(
except:
date = "Unknown"
canvas = pdf.PylinacCanvas(
filename, page_title=analysis_title, metadata=metadata
filename, page_title=analysis_title, metadata=metadata, logo=logo
)
if notes is not None:
canvas.add_text(text="Notes:", location=(1, 4.5), font_size=14)
Expand Down
3 changes: 3 additions & 0 deletions pylinac/field_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,7 @@ def publish_pdf(
notes: Union[str, list] = None,
open_file: bool = False,
metadata: dict = None,
logo: Optional[Union[Path, str]] = None
) -> None:
"""Publish (print) a PDF containing the analysis, images, and quantitative results.
Expand All @@ -934,6 +935,8 @@ def publish_pdf(
Author: James
Unit: TrueBeam
--------------
logo: Path, str
A custom logo to use in the PDF report. If nothing is passed, the default pylinac logo is used.
"""
plt.ioff()
if not self._is_analyzed:
Expand Down
11 changes: 9 additions & 2 deletions pylinac/log_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import webbrowser
import zipfile
from io import BytesIO, BufferedReader
from pathlib import Path
from typing import Union, List, Optional, Tuple, Iterable, Sequence, BinaryIO

import argue
Expand Down Expand Up @@ -2068,6 +2069,7 @@ def publish_pdf(
notes: str = None,
metadata: dict = None,
open_file: bool = False,
logo: Optional[Union[Path, str]] = None
):
"""Publish (print) a PDF containing the analysis and quantitative results.
Expand All @@ -2087,10 +2089,12 @@ def publish_pdf(
Author: James
Unit: TrueBeam
--------------
logo: Path, str
A custom logo to use in the PDF report. If nothing is passed, the default pylinac logo is used.
"""
self.fluence.gamma.calc_map()
canvas = pdf.PylinacCanvas(
filename, page_title="Dynalog Analysis", metadata=metadata
filename, page_title="Dynalog Analysis", metadata=metadata, logo=logo
)
canvas.add_text(
text=[
Expand Down Expand Up @@ -2527,6 +2531,7 @@ def publish_pdf(
metadata: dict = None,
notes: Union[str, list] = None,
open_file: bool = False,
logo: Optional[Union[Path, str]] = None
):
"""Publish (print) a PDF containing the analysis and quantitative results.
Expand All @@ -2546,14 +2551,16 @@ def publish_pdf(
Author: James
Unit: TrueBeam
--------------
logo: Path, str
A custom logo to use in the PDF report. If nothing is passed, the default pylinac logo is used.
"""
if self.treatment_type == TreatmentType.IMAGING.value:
raise ValueError(
"Log is of imaging type (e.g. kV setup) and does not contain relevant gamma/leaf data"
)
self.fluence.gamma.calc_map()
canvas = pdf.PylinacCanvas(
filename, page_title="Trajectory Log Analysis", metadata=metadata
filename, page_title="Trajectory Log Analysis", metadata=metadata, logo=logo
)
canvas.add_text(
text=[
Expand Down
7 changes: 6 additions & 1 deletion pylinac/picketfence.py
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,7 @@ def publish_pdf(
open_file: bool = False,
metadata: dict = None,
bins: int = 10,
logo: Optional[Union[Path, str]] = None
) -> None:
"""Publish (print) a PDF containing the analysis, images, and quantitative results.
Expand All @@ -965,10 +966,14 @@ def publish_pdf(
Author: James
Unit: TrueBeam
--------------
bins: int
Number of bins to show for the histogram
logo: Path, str
A custom logo to use in the PDF report. If nothing is passed, the default pylinac logo is used.
"""
plt.ioff()
canvas = pdf.PylinacCanvas(
filename, page_title="Picket Fence Analysis", metadata=metadata
filename, page_title="Picket Fence Analysis", metadata=metadata, logo=logo
)
data = io.BytesIO()
self.save_analyzed_image(data, leaf_error_subplot=True)
Expand Down
8 changes: 8 additions & 0 deletions pylinac/planar_imaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,7 @@ def publish_pdf(
notes: str = None,
open_file: bool = False,
metadata: Optional[dict] = None,
logo: Optional[Union[Path, str]] = None
):
"""Publish (print) a PDF containing the analysis, images, and quantitative results.
Expand All @@ -687,11 +688,14 @@ def publish_pdf(
Author: James
Unit: TrueBeam
--------------
logo: Path, str
A custom logo to use in the PDF report. If nothing is passed, the default pylinac logo is used.
"""
canvas = pdf.PylinacCanvas(
filename,
page_title=f"{self.common_name} Phantom Analysis",
metadata=metadata,
logo=logo
)

# write the text/numerical values
Expand Down Expand Up @@ -1054,6 +1058,7 @@ def publish_pdf(
notes: str = None,
open_file: bool = False,
metadata: Optional[dict] = None,
logo: Optional[Union[Path, str]] = None
):
"""Publish (print) a PDF containing the analysis, images, and quantitative results.
Expand All @@ -1073,11 +1078,14 @@ def publish_pdf(
Author: James
Unit: TrueBeam
--------------
logo: Path, str
A custom logo to use in the PDF report. If nothing is passed, the default pylinac logo is used.
"""
canvas = pdf.PylinacCanvas(
filename,
page_title=f"{self.common_name} Phantom Analysis",
metadata=metadata,
logo=logo
)

# write the text/numerical values
Expand Down
5 changes: 4 additions & 1 deletion pylinac/quart.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ def publish_pdf(
notes: Optional[str] = None,
open_file: bool = False,
metadata: Optional[dict] = None,
logo: Optional[Union[Path, str]] = None
) -> None:
"""Publish (print) a PDF containing the analysis and quantitative results.
Expand All @@ -491,12 +492,14 @@ def publish_pdf(
Author: James
Unit: TrueBeam
--------------
logo: Path, str
A custom logo to use in the PDF report. If nothing is passed, the default pylinac logo is used.
"""
analysis_title = f"{self._model} Analysis"
analysis_images = self.save_images(to_stream=True)

canvas = pdf.PylinacCanvas(
filename, page_title=analysis_title, metadata=metadata
filename, page_title=analysis_title, metadata=metadata, logo=logo
)
if notes is not None:
canvas.add_text(text="Notes:", location=(1, 4.5), font_size=14)
Expand Down
6 changes: 5 additions & 1 deletion pylinac/starshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io
import webbrowser
from dataclasses import dataclass
from pathlib import Path
from typing import Union, List, Optional, Tuple, BinaryIO

import argue
Expand Down Expand Up @@ -503,6 +504,7 @@ def publish_pdf(
notes: Optional[Union[str, List[str]]] = None,
open_file: bool = False,
metadata: Optional[dict] = None,
logo: Optional[Union[Path, str]] = None
):
"""Publish (print) a PDF containing the analysis, images, and quantitative results.
Expand All @@ -522,9 +524,11 @@ def publish_pdf(
Author: James
Unit: TrueBeam
--------------
logo: Path, str
A custom logo to use in the PDF report. If nothing is passed, the default pylinac logo is used.
"""
canvas = pdf.PylinacCanvas(
filename, page_title="Starshot Analysis", metadata=metadata
filename, page_title="Starshot Analysis", metadata=metadata, logo=logo
)
for img, height in zip(("wobble", "asdf"), (2, 11.5)):
data = io.BytesIO()
Expand Down
5 changes: 5 additions & 0 deletions pylinac/vmat.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import webbrowser
from dataclasses import dataclass
from io import BytesIO
from pathlib import Path
from typing import Union, List, Tuple, Sequence, Optional, BinaryIO

import argue
Expand Down Expand Up @@ -393,6 +394,7 @@ def publish_pdf(
notes: str = None,
open_file: bool = False,
metadata: Optional[dict] = None,
logo: Optional[Union[Path, str]] = None
):
"""Publish (print) a PDF containing the analysis, images, and quantitative results.
Expand All @@ -412,11 +414,14 @@ def publish_pdf(
Author: James
Unit: TrueBeam
--------------
logo: Path, str
A custom logo to use in the PDF report. If nothing is passed, the default pylinac logo is used.
"""
canvas = PylinacCanvas(
filename=filename,
page_title=f"{self._result_short_header} VMAT Analysis",
metadata=metadata,
logo=logo
)
for y, x, width, img in zip(
(9, 9, -2),
Expand Down
5 changes: 4 additions & 1 deletion pylinac/winston_lutz.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,7 @@ def publish_pdf(
notes: Optional[Union[str, List[str]]] = None,
open_file: bool = False,
metadata: Optional[dict] = None,
logo: Optional[Union[Path, str]] = None
):
"""Publish (print) a PDF containing the analysis, images, and quantitative results.
Expand All @@ -796,12 +797,14 @@ def publish_pdf(
Author: James
Unit: TrueBeam
--------------
logo: Path, str
A custom logo to use in the PDF report. If nothing is passed, the default pylinac logo is used.
"""
if not self._is_analyzed:
raise ValueError("The set is not analyzed. Use .analyze() first.")
plt.ioff()
title = "Winston-Lutz Analysis"
canvas = pdf.PylinacCanvas(filename, page_title=title, metadata=metadata)
canvas = pdf.PylinacCanvas(filename, page_title=title, metadata=metadata, logo=logo)
text = self.results(as_list=True)
canvas.add_text(text=text, location=(7, 25.5))
# draw summary image on 1st page
Expand Down
Binary file added tests_basic/core/rm-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 957bb66

Please sign in to comment.