How to use the pylinac.core.pdf.PylinacCanvas function in pylinac

To help you get started, we’ve selected a few pylinac examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jrkerns / pylinac / pylinac / log_analyzer.py View on Github external
The file to write the results to.
        notes : str, list of strings
            Text; if str, prints single line.
            If list of strings, each list item is printed on its own line.
        open_file : bool
            Whether to open the file using the default program after creation.
        metadata : dict
            Extra data to be passed and shown in the PDF. The key and value will be shown with a colon.
            E.g. passing {'Author': 'James', 'Unit': 'TrueBeam'} would result in text in the PDF like:
            --------------
            Author: James
            Unit: TrueBeam
            --------------
        """
        self.fluence.gamma.calc_map()
        canvas = pdf.PylinacCanvas(filename, page_title="Dynalog Analysis", metadata=metadata)
        canvas.add_text(
                      text=['Dynalog results:',
                            f'Average RMS (mm): {self.axis_data.mlc.get_RMS_avg()*10:2.2f}',
                            f'Max RMS (mm): {self.axis_data.mlc.get_RMS_max()*10:2.2f}',
                            f'95th Percentile error (mm): {self.axis_data.mlc.get_error_percentile(95)*10:2.2f}',
                            f'Number of beam holdoffs: {self.num_beamholds}',
                            f'Gamma pass (%): {self.fluence.gamma.pass_prcnt:2.1f}',
                            f'Gamma average: {self.fluence.gamma.avg_gamma:2.2f}',
                            ],
                        location=(10, 25.5))
        for idx, (x, y, graph) in enumerate(zip((2, 11, 2, 11), (14, 14, 6, 6), ('actual', 'expected', 'gamma', ''))):
            data = BytesIO()
            if idx != 3:
                self.save_subimage(data, graph, fontsize=20)
            else:
                self.save_subgraph(data, 'gamma', fontsize=20, labelsize=12)
github jrkerns / pylinac / pylinac / starshot.py View on Github external
filename : (str, file-like object}
            The file to write the results to.
        notes : str, list of strings
            Text; if str, prints single line.
            If list of strings, each list item is printed on its own line.
        open_file : bool
            Whether to open the file using the default program after creation.
        metadata : dict
            Extra data to be passed and shown in the PDF. The key and value will be shown with a colon.
            E.g. passing {'Author': 'James', 'Unit': 'TrueBeam'} would result in text in the PDF like:
            --------------
            Author: James
            Unit: TrueBeam
            --------------
        """
        canvas = pdf.PylinacCanvas(filename, page_title="Starshot Analysis", metadata=metadata)
        for img, height in zip(('wobble', 'asdf'), (2, 11.5)):
            data = io.BytesIO()
            self.save_analyzed_subimage(data, img)
            canvas.add_image(data, location=(4, height), dimensions=(13, 13))
        text = ['Starshot results:',
                f'Source-to-Image Distance (mm): {self.image.sid:2.0f}',
                f'Tolerance (mm): {self.tolerance:2.1f}',
                f"Minimum circle diameter (mm): {self.wobble.radius_mm*2:2.2f}",
                ]
        canvas.add_text(text=text, location=(10, 25.5), font_size=12)
        if notes is not None:
            canvas.add_text(text="Notes:", location=(1, 5.5), font_size=14)
            canvas.add_text(text=notes, location=(1, 5))
        canvas.finish()

        if open_file:
github jrkerns / pylinac / pylinac / vmat.py View on Github external
filename : (str, file-like object}
            The file to write the results to.
        notes : str, list of strings
            Text; if str, prints single line.
            If list of strings, each list item is printed on its own line.
        open_file : bool
            Whether to open the file using the default program after creation.
        metadata : dict
            Extra data to be passed and shown in the PDF. The key and value will be shown with a colon.
            E.g. passing {'Author': 'James', 'Unit': 'TrueBeam'} would result in text in the PDF like:
            --------------
            Author: James
            Unit: TrueBeam
            --------------
        """
        canvas = PylinacCanvas(filename=filename, page_title=f"{self._result_short_header} VMAT Analysis", metadata=metadata)
        for y, x, width, img in zip((9, 9, -2), (1, 11, 3), (9, 9, 14), (OPEN, DMLC, PROFILE)):
            data = BytesIO()
            self._save_analyzed_subimage(data, subimage=img)
            canvas.add_image(data, location=(x, y), dimensions=(width, 18))
            # canvas.add_text(text=f"{img} Image", location=(x + 2, y + 10), font_size=18)
        canvas.add_text(text='Open Image', location=(4, 22), font_size=18)
        canvas.add_text(text=f'{self.open_image.base_path}', location=(4, 21.5))
        canvas.add_text(text='DMLC Image', location=(14, 22), font_size=18)
        canvas.add_text(text=f'{self.dmlc_image.base_path}', location=(14, 21.5))
        canvas.add_text(text='Median profiles', location=(8, 12), font_size=18)
        text = [f'{self._result_header} VMAT results:',
                f'Source-to-Image Distance (mm): {self.open_image.sid:2.0f}',
                f'Tolerance (%): {self._tolerance*100:2.1f}',
                f'Absolute mean deviation (%): {self.avg_abs_r_deviation:2.2f}',
                f'Maximum deviation (%): {self.max_r_deviation:2.2f}',
                ]
github jrkerns / pylinac / pylinac / calibration / tg51.py View on Github external
notes : str, list
            Any notes to be added to the report. If a string, adds everything as one line.
            If a list, must be a list of strings; each string item will be a new line.
        open_file : bool
            Whether to open the file after creation. Will use the default PDF program.
        metadata : dict
            Any data that should be appended to every page of the report. This differs from notes in that
            metadata is at the top of every page while notes is at the bottom of the report.
        """
        was_adjusted = 'Yes' if self.output_was_adjusted else 'No'
        title = [
            'TG-51 Electron Report (Legacy)',
            f'{self.unit} - {self.energy} MeV'
        ]

        canvas = PylinacCanvas(filename, page_title=title, metadata=metadata)
        text = [
            'Site Data:',
            f'Institution: {self.institution}',
            f'Performed by: {self.physicist}',
            f'Measurement Date: {self.measurement_date}',
            f'Date of Report: {datetime.now().strftime("%A, %B %d, %Y")}',
            f'Unit: {self.unit}',
            f'Energy: {self.energy} MeV',
            f'Cone: {self.cone}',
            f'MU: {self.mu}',
            '',
            'Instrumentation:',
            f'Chamber chamber: {self.chamber}',
            f'Chamber Calibration Factor Ndw (cGy/nC): {self.n_dw:2.3f}',
            f'Electrometer: {self.electrometer}',
            f'Pelec: {self.p_elec:2.2f}',
github jrkerns / pylinac / pylinac / flatsym.py View on Github external
notes : str, list of strings
            Text; if str, prints single line.
            If list of strings, each list item is printed on its own line.
        open_file : bool
            Whether to open the file using the default program after creation.
        metadata : dict
            Extra data to be passed and shown in the PDF. The key and value will be shown with a colon.
            E.g. passing {'Author': 'James', 'Unit': 'TrueBeam'} would result in text in the PDF like:
            --------------
            Author: James
            Unit: TrueBeam
            --------------
        """
        if not self._is_analyzed:
            raise NotAnalyzed("Image is not analyzed yet. Use analyze() first.")
        canvas = pdf.PylinacCanvas(filename, page_title="Flatness & Symmetry Analysis",
                                   metadata=metadata, metadata_location=(2, 5))
        # draw result text
        text = self.results(as_str=False)
        canvas.add_text(text=text, location=(2, 25.5), font_size=14)
        canvas.add_new_page()
        # draw flatness & symmetry on two pages
        for method in (self._plot_symmetry, self._plot_flatness):
            for height, direction in zip((1, 12.5), ('vertical', 'horizontal')):
                data = io.BytesIO()
                self._save_plot(method, data, direction=direction)
                canvas.add_image(data, location=(-4, height), dimensions=(28, 12))
            canvas.add_new_page()
        # draw image on last page
        data = io.BytesIO()
        self._save_plot(self._plot_image, data, title="Image")
        canvas.add_image(data, location=(1, 2), dimensions=(18, 20))
github jrkerns / pylinac / pylinac / planar_imaging.py View on Github external
filename : (str, file-like object}
            The file to write the results to.
        notes : str, list of strings
            Text; if str, prints single line.
            If list of strings, each list item is printed on its own line.
        open_file : bool
            Whether to open the file using the default program after creation.
        metadata : dict
            Extra data to be passed and shown in the PDF. The key and value will be shown with a colon.
            E.g. passing {'Author': 'James', 'Unit': 'TrueBeam'} would result in text in the PDF like:
            --------------
            Author: James
            Unit: TrueBeam
            --------------
        """
        canvas = pdf.PylinacCanvas(filename, page_title=f'{self.common_name} Phantom Analysis', metadata=metadata)

        # write the text/numerical values
        text = self.results()
        canvas.add_text(text=text, location=(1.5, 25), font_size=14)
        if notes is not None:
            canvas.add_text(text="Notes:", location=(1, 5.5), font_size=12)
            canvas.add_text(text=notes, location=(1, 5))

        # plot the image
        data = io.BytesIO()
        self.save_analyzed_image(data, image=True, low_contrast=False, high_contrast=False)
        canvas.add_image(data, location=(1, 3.5), dimensions=(19, 19))
        # plot the high contrast
        if self.high_contrast_rois:
            canvas.add_new_page()
            data = io.BytesIO()
github jrkerns / pylinac / pylinac / ct.py View on Github external
def _publish_pdf(self, filename, metadata, notes, analysis_title, texts, imgs):
        try:
            date = datetime.strptime(self.dicom_stack[0].metadata.InstanceCreationDate, "%Y%m%d").strftime("%A, %B %d, %Y")
        except:
            date = "Unknown"
        canvas = pdf.PylinacCanvas(filename, page_title=analysis_title, metadata=metadata)
        if notes is not None:
            canvas.add_text(text="Notes:", location=(1, 4.5), font_size=14)
            canvas.add_text(text=notes, location=(1, 4))

        for page, ((img1, img2), text) in enumerate(zip(imgs, texts)):
            for img, offset in zip((img1, img2), (12, 2)):
                if img is not None:
                    data = io.BytesIO()
                    self.save_analyzed_subimage(data, img)
                    canvas.add_image(data, location=(4, offset), dimensions=(15, 10))
            canvas.add_text(text=text, location=(1.5, 23))
            canvas.add_new_page()
        canvas.finish()
github jrkerns / pylinac / pylinac / log_analyzer.py View on Github external
Text; if str, prints single line.
            If list of strings, each list item is printed on its own line.
        open_file : bool
            Whether to open the file using the default program after creation.
        metadata : dict
            Extra data to be passed and shown in the PDF. The key and value will be shown with a colon.
            E.g. passing {'Author': 'James', 'Unit': 'TrueBeam'} would result in text in the PDF like:
            --------------
            Author: James
            Unit: TrueBeam
            --------------
        """
        if self.treatment_type == IMAGING:
            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)
        canvas.add_text(
                      text=['Trajectory Log results:',
                            f'Average RMS (mm): {self.axis_data.mlc.get_RMS_avg()*10:2.2f}',
                            f'Max RMS (mm): {self.axis_data.mlc.get_RMS_max()*10:2.2f}',
                            f'95th Percentile error (mm): {self.axis_data.mlc.get_error_percentile(95)*10:2.2f}',
                            f'Number of beam holdoffs: {self.num_beamholds}',
                            f'Gamma pass (%): {self.fluence.gamma.pass_prcnt:2.1f}',
                            f'Gamma average: {self.fluence.gamma.avg_gamma:2.2f}',
                            ],
                        location=(10, 25.5))
        for idx, (x, y, graph) in enumerate(zip((2, 11, 2, 11), (14, 14, 6, 6), ('actual', 'expected', 'gamma', ''))):
            data = BytesIO()
            if idx != 3:
                self.save_subimage(data, graph, fontsize=20)
            else:
                self.save_subgraph(data, 'gamma', fontsize=20, labelsize=12)