Beispiel #1
0
 def test_get_rbs_selections(self):
     with tempfile.TemporaryDirectory() as tmp_dir:
         path = Path(tmp_dir, self.rel_dir)
         self._generate_cut_files(path)
         files = [Path(entry.path) for entry in os.scandir(path)]
         rbs = cut_file.get_rbs_selections(files)
         self.assertEqual(
             {
                 "1He.RBS_He.0.cut": mo.get_element(symbol="He"),
                 "He.RBS_Cl.0.cut": mo.get_element(symbol="Cl"),
                 "He.RBS_Cl.1.cut": mo.get_element(symbol="Cl"),
             }, rbs)
Beispiel #2
0
    def __init__(self,
                 parent: BaseTab,
                 output_dir: Path,
                 cut_files: List[Path],
                 elements: List[Element],
                 x_units: DepthProfileUnit,
                 line_zero: bool,
                 used_eff: bool,
                 line_scale: bool,
                 systematic_error: float,
                 eff_files_str: str,
                 progress: Optional[ProgressReporter] = None):
        """Inits widget.
        
        Args:
            parent: a MeasurementTabWidget.
            output_dir: full path to depth file location
            cut_files: A list of Cut files.
            elements: A list of Element objects that are used in depth profile.
            x_units: Units to be used for x-axis of depth profile.
            line_zero: A boolean representing if vertical line is drawn at zero.
            used_eff: A boolean representing if used eff files are shown.
            line_scale: A boolean representing if horizontal line is drawn at 
                        the defined depth scale.
            systematic_error: A double representing systematic error.
        """
        try:
            super().__init__()
            uic.loadUi(gutils.get_ui_dir() / "ui_depth_profile.ui", self)

            self.parent = parent
            self.measurement: Measurement = parent.obj
            self.output_dir = output_dir
            self.elements = elements
            self.x_units = x_units
            self.use_cuts = cut_files
            self._line_zero_shown = line_zero
            self._eff_files_shown = used_eff
            self._eff_files_str = eff_files_str
            self._line_scale_shown = line_scale
            self._systematic_error = systematic_error

            if progress is not None:
                sub_progress = progress.get_sub_reporter(lambda x: 0.5 * x)
            else:
                sub_progress = None

            depth_files.generate_depth_files(self.use_cuts,
                                             self.output_dir,
                                             self.measurement,
                                             progress=sub_progress)

            if progress is not None:
                progress.report(50)

            # Check for RBS selections.
            rbs_list = cut_file.get_rbs_selections(self.use_cuts)

            for rbs in rbs_list:
                # Search and replace instances of Beam element with scatter
                # elements.
                # When loading request, the scatter element is already
                # replaced. This is essentially done only when creating
                # a new Depth Profile graph.
                # TODO seems overly complicated. This stuff should be sorted
                #  before initializing the widget
                element = Element.from_string(rbs.split(".")[0])
                for i, elem in enumerate(elements):
                    if elem == element:
                        elements[i] = rbs_list[rbs]

            if self._line_scale_shown:
                _, _, _, profile, _ = self.measurement.get_used_settings()
                depth_scale = (profile.depth_for_concentration_from,
                               profile.depth_for_concentration_to)
            else:
                depth_scale = None

            if progress is not None:
                sub_progress = progress.get_sub_reporter(
                    lambda x: 50 + 0.5 * x)
            else:
                sub_progress = None

            self.matplotlib = MatplotlibDepthProfileWidget(
                self,
                self.output_dir,
                self.elements,
                rbs_list,
                icon_manager=self.parent.icon_manager,
                selection_colors=self.measurement.selector.get_colors(),
                depth_scale=depth_scale,
                x_units=self.x_units,
                add_line_zero=self._line_zero_shown,
                show_eff_files=self._eff_files_shown,
                used_eff_str=self._eff_files_str,
                systematic_error=self._systematic_error,
                progress=sub_progress)
        except Exception as e:
            msg = f"Could not create Depth Profile graph: {e}"
            self.measurement.log_error(msg)
            if hasattr(self, "matplotlib"):
                self.matplotlib.delete()
        finally:
            if progress is not None:
                progress.report(100)
Beispiel #3
0
    def __init__(self, parent, measurement: Measurement,
                 reference_cut_file: Path, checked_cuts: List[Path],
                 partition_count: int, y_scale: int, statusbar=None,
                 progress=None):
        """Inits widget.
        
        Args:
            parent: A MeasurementTabWidget.
            reference_cut_file: absolute path to cut file.
            checked_cuts: list of absolute paths to cut files.
            partition_count: Integer representing how many splits cut files 
                are divided to.
            y_scale: Integer flag representing how Y axis is scaled.
            progress: a ProgressReporter object
        """
        try:
            super().__init__()
            uic.loadUi(gutils.get_ui_dir() / "ui_element_losses.ui", self)

            self.parent = parent
            self.icon_manager = parent.icon_manager
            self.measurement = measurement
            self.reference_cut_file = reference_cut_file
            self.checked_cuts = checked_cuts
            self.partition_count = partition_count
            self.y_scale = y_scale
            self.statusbar = statusbar

            title = "{0} - Reference cut: {1}".format(
                self.windowTitle(),
                self.reference_cut_file.name)
            self.setWindowTitle(title)

            # Calculate elemental losses
            self.losses = ElementLosses(
                self.measurement.get_cuts_dir(),
                self.measurement.get_composition_changes_dir(),
                self.reference_cut_file,
                self.checked_cuts,
                self.partition_count)

            if progress is not None:
                sub_progress = progress.get_sub_reporter(
                    lambda x: 0.9 * x
                )
            else:
                sub_progress = None

            self.split_counts = self.losses.count_element_cuts(
                progress=sub_progress
            )

            # Check for RBS selections.
            rbs_list = cut_file.get_rbs_selections(self.checked_cuts)
            # Connect buttons
            self.splitSaveButton.clicked.connect(self.__save_splits)

            self.matplotlib = MatplotlibElementLossesWidget(
                self, self.split_counts, legend=True, y_scale=y_scale,
                rbs_list=rbs_list, reference_cut_file=reference_cut_file)
        except Exception as e:
            msg = f"Could not create Elemental Losses graph: {e}"
            self.measurement.log_error(msg)
            if hasattr(self, "matplotlib"):
                self.matplotlib.delete()
        finally:
            if progress is not None:
                progress.report(100)
Beispiel #4
0
    def __init__(self,
                 parent: BaseTab,
                 spectrum_type: str = MEASUREMENT,
                 use_cuts=None,
                 bin_width=0.025,
                 use_efficiency=False,
                 save_file_int=0,
                 statusbar=None,
                 spectra_changed=None):
        """Inits widget.
        
        Args:
            parent: A TabWidget.
            use_cuts: A string list representing Cut files.
            bin_width: A float representing Energy Spectrum histogram's bin
                width.
            use_efficiency: whether efficiency is taken into account when
                measured spectra is calculated
            save_file_int: n integer to have unique save file names for
                simulation energy spectra combinations.
            spectra_changed: pyqtSignal that indicates a change in energy
                spectra.
        """
        sbh = None
        super().__init__()
        uic.loadUi(gutils.get_ui_dir() / "ui_energy_spectrum.ui", self)
        try:
            self.parent = parent
            self.icon_manager = parent.icon_manager
            self.progress_bar = None
            if use_cuts is None:
                use_cuts = []
            self.use_cuts = use_cuts
            self.bin_width = bin_width
            self.energy_spectrum_data = {}
            self.spectrum_type = spectrum_type
            rbs_list = {}

            title = f"{self.windowTitle()} - Bin Width: {bin_width}"
            self.setWindowTitle(title)

            if isinstance(self.parent.obj, Measurement):
                self.measurement = self.parent.obj

                # Removal is done in the finally block so autoremove
                # is set to False
                sbh = StatusBarHandler(statusbar, autoremove=False)

                # Do energy spectrum stuff on this
                self.energy_spectrum_data = \
                    EnergySpectrum.calculate_measured_spectra(
                        self.measurement, use_cuts, bin_width,
                        progress=sbh.reporter, use_efficiency=use_efficiency
                    )

                # Check for RBS selections.
                rbs_list = cut_file.get_rbs_selections(self.use_cuts)
            else:
                self.simulation = self.parent.obj
                self.save_file_int = save_file_int
                self.save_file = f"widget_energy_spectrum_{save_file_int}.save"
                for file in use_cuts:
                    self.energy_spectrum_data[file] = GetEspe.read_espe_file(
                        file)

            # Graph in matplotlib widget and add to window
            self.matplotlib = MatplotlibEnergySpectrumWidget(
                self,
                self.energy_spectrum_data,
                rbs_list,
                spectrum_type,
                spectra_changed=spectra_changed,
                channel_width=bin_width)
        except (PermissionError, IsADirectoryError, FileNotFoundError) as e:
            # If the file path points to directory, this will either raise
            # PermissionError (Windows) or IsADirectoryError (Mac)
            msg = f"Could not create Energy Spectrum graph: {e}"
            self.parent.obj.log_error(msg)

            if hasattr(self, "matplotlib"):
                self.matplotlib.delete()
            self.matplotlib = None
        finally:
            if sbh is not None:
                sbh.remove_progress_bar()