Example #1
0
 def save_splits(self):
     '''Save element splits as new cut files.
     '''
     self.__element_losses_folder_clean_up()
     dirtyinteger = 0
     count = self.cut_splits.count()
     for key in self.cut_splits.get_keys():
         # Do not split elemental losses again.
         # TODO: Should elemental losses be possible to split again?
         if len(key.split('.')) == 3:
             continue
         main_cut = self.cut_splits.get_cut(key)
         splits = self.cut_splits.get_splits(key)
         
         split_count = len(splits)
         split_number = 0
         for split in splits:
             new_cut = CutFile(elem_loss=True,
                               split_number=split_number,
                               split_count=split_count)
             new_cut.copy_info(main_cut, split, split_count)
             new_cut.save(main_cut.element_number)
             split_number += 1
             self.progress_bar.setValue((100 / count) * dirtyinteger \
                 + (100 / count) * (split_number / split_count))
             QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents)
             # Mac requires event processing to show progress bar and its
             # process.
         dirtyinteger += 1
Example #2
0
 def __load_cut_splits(self, save=False):
     '''Loads the checked cut files and splits them in smaller cuts.
     
     Args:
         save: Boolean representing whether we save splits or not.
     '''
     self.reference_cut = CutFile()
     self.reference_cut.load_file(self.reference_cut_file)
     
     # Remove old (element losses) cut files
     if save:
         self.__element_losses_folder_clean_up()
                 
     dirtyinteger = 0
     count = len(self.checked_cuts)
     for file in self.checked_cuts: 
         self.progress_bar.setValue((dirtyinteger / count) * 80)
         QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents)
         # Mac requires event processing to show progress bar and its process.
         cut = CutFile()
         cut.load_file(file)
         filename_split = file.split('.')
         element = Element(filename_split[1])
         if len(filename_split) == 4:  # Regular cut file
             key = "{0}.{1}".format(element, filename_split[2])
         else:  # Elemental Losses cut file
             key = "{0}.{1}.{2}".format(element,
                                        filename_split[2],
                                        filename_split[3])
         self.cut_splits.add_splits(key, cut,
                                    cut.split(self.reference_cut,
                                              self.partition_count,
                                              save=save))
         dirtyinteger += 1
Example #3
0
class ElementLosses:
    '''Element Losses class.
    '''
    def __init__(self, directory_cuts, directory_elemloss, reference_cut_file,
                 checked_cuts, partition_count, progress_bar=Null()):
        '''Inits Element Losses class.
        
        Args:
            directory_cuts: String representing cut file directory.
            directory_elemloss: String representing elemental losses directory.
            reference_cut_file: String representing reference cut file.
            checked_cuts: String list of cut files to be graphed.
            partition_count: Integer representing split count.
            progress_bar: QtGui.QProgressBar or Null() if not given.
        '''
        self.directory_cuts = directory_cuts
        self.directory_elemloss = directory_elemloss
        self.partition_count = partition_count
        self.checked_cuts = checked_cuts
        self.progress_bar = progress_bar
        # self.cut_splits_dict = {}
        self.reference_cut_file = reference_cut_file
        filename_split = reference_cut_file.split('.')
        element = Element(filename_split[1])
        self.reference_key = "{0}.{1}".format(element, filename_split[2])
        self.cut_splits = ElementLossesSplitHolder()
    
    
    def count_element_cuts(self, save_splits=False):
        '''Count data points in splits based on reference file.
        
        Args:
            save_splits: Boolean representing whether to save element losses splits.
        
        Return:
            Returns dictionary of elements and their counts within splits.
        '''
        self.__load_cut_splits(save_splits)
        split_counts = self.__count_element_cuts()
        return split_counts
    
    
    def save_splits(self):
        '''Save element splits as new cut files.
        '''
        self.__element_losses_folder_clean_up()
        dirtyinteger = 0
        count = self.cut_splits.count()
        for key in self.cut_splits.get_keys():
            # Do not split elemental losses again.
            # TODO: Should elemental losses be possible to split again?
            if len(key.split('.')) == 3:
                continue
            main_cut = self.cut_splits.get_cut(key)
            splits = self.cut_splits.get_splits(key)
            
            split_count = len(splits)
            split_number = 0
            for split in splits:
                new_cut = CutFile(elem_loss=True,
                                  split_number=split_number,
                                  split_count=split_count)
                new_cut.copy_info(main_cut, split, split_count)
                new_cut.save(main_cut.element_number)
                split_number += 1
                self.progress_bar.setValue((100 / count) * dirtyinteger \
                    + (100 / count) * (split_number / split_count))
                QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents)
                # Mac requires event processing to show progress bar and its
                # process.
            dirtyinteger += 1
    
    
    def __load_cut_splits(self, save=False):
        '''Loads the checked cut files and splits them in smaller cuts.
        
        Args:
            save: Boolean representing whether we save splits or not.
        '''
        self.reference_cut = CutFile()
        self.reference_cut.load_file(self.reference_cut_file)
        
        # Remove old (element losses) cut files
        if save:
            self.__element_losses_folder_clean_up()
                    
        dirtyinteger = 0
        count = len(self.checked_cuts)
        for file in self.checked_cuts: 
            self.progress_bar.setValue((dirtyinteger / count) * 80)
            QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents)
            # Mac requires event processing to show progress bar and its process.
            cut = CutFile()
            cut.load_file(file)
            filename_split = file.split('.')
            element = Element(filename_split[1])
            if len(filename_split) == 4:  # Regular cut file
                key = "{0}.{1}".format(element, filename_split[2])
            else:  # Elemental Losses cut file
                key = "{0}.{1}.{2}".format(element,
                                           filename_split[2],
                                           filename_split[3])
            self.cut_splits.add_splits(key, cut,
                                       cut.split(self.reference_cut,
                                                 self.partition_count,
                                                 save=save))
            dirtyinteger += 1
    
    
    def __element_losses_folder_clean_up(self):
        for the_file in os.listdir(self.directory_elemloss):
            file_path = os.path.join(self.directory_elemloss, the_file)
            try:
                if os.path.isfile(file_path):
                    os.unlink(file_path)
            except Exception:
                # TODO: logger.error?
                print("HELP! File unlink does something bad (elemloss)")
    
    
    def __count_element_cuts(self):
        '''Counts the number of sublists' elements to another list and puts it under
        corresponding dictionary's key value.
        
        Counts the number of sublists' elements to another list and puts it 
        under corresponding dictionary's key value. For example: 
        cuts['H'] = [[13,4,25,6],[1,3,4,1],[2,3,2]] --> 
                    __count_element_cuts['H'] = [4,4,3]
            
        Return:
            Returns dictionary of elements and their counts within splits.
        '''
        split_counts_dict = {}
        dirtyinteger = 0
        count = self.cut_splits.count()
        # for key in self.cut_splits_dict.keys():
        for key in self.cut_splits.get_keys():
            self.progress_bar.setValue((dirtyinteger / count) * 20 + 80)
            QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents)
            # Mac requires event processing to show progress bar and its process.
            # Reference cut is not counted, excluded from graph.
            if key != self.reference_key:  
                split_counts_dict[key] = [len(split) 
                                          for split 
                                          in self.cut_splits.get_splits(key)]
            dirtyinteger += 1
        return split_counts_dict
Example #4
0
    def __init__(self, measurements, settings, masses, parent_settings_dialog=None):
        """Inits the calibration dialog class.
        
        Args:
            measurements: A string list representing measurements files.
            settings: A Settings class object.
            masses: A Masses class object.
            parent_settings_dialog: A representing from which dialog this was 
                                    opened from.
        """
        super().__init__()
        self.measurements = measurements
        # TODO: Settings should be loaded from the measurement depending on is the 
        # calibration dialog opened from the project settings (measurement's 
        # project settings is loaded) or the measurement specific settings
        # (measurement's measurement settings is loaded). This has to be done for
        # better architecture.
        self.settings = settings 
        self.__cut_file = CutFile()
        self.__cut_files = {}
        
        self.ui = uic.loadUi(os.path.join("ui_files",
                                          "ui_calibration_dialog.ui"), self)
        self.parent_settings_dialog = parent_settings_dialog 
        self.tof_calibration = TOFCalibration()
        
        measurement = None
        
        # Go through all the measurements and their cut files and list them.
        for measurement in self.measurements:
            item = QtGui.QTreeWidgetItem([measurement.measurement_name])
            
            cuts, unused_cuts_elemloss = measurement.get_cut_files()
            # May also return a list of cut file's element losses 
            # cut files as one of the list elements
            for cut_file in cuts:
                subitem = QtGui.QTreeWidgetItem([cut_file])
                subitem.directory = measurement.directory_cuts
                subitem.file_name = cut_file
                item.addChild(subitem)
                cut_object = CutFile()
                cut_object.load_file(os.path.join(measurement.directory_cuts,
                                                  cut_file))
                self.__cut_files[cut_file] = cut_object
            self.ui.cutFilesTreeWidget.addTopLevelItem(item)
            item.setExpanded(True)
        # Resize columns to fit the content nicely
        for column in range(0, self.ui.cutFilesTreeWidget.columnCount()):
            self.ui.cutFilesTreeWidget.resizeColumnToContents(column)

        self.curveFittingWidget = CalibrationCurveFittingWidget(self,
                                                    self.__cut_file,
                                                    self.tof_calibration,
                                                    self.settings,
                                                    self.ui.binWidthSpinBox.value(),
                                                    1,
                                                    masses)
        
        old_params = None
        if parent_settings_dialog:  # Get old parameters from the parent dialog
            try:
                f1 = float(self.parent_settings_dialog.ui.slopeLineEdit.text())
                f2 = float(self.parent_settings_dialog.ui.offsetLineEdit.text())
                old_params = f1, f2
            except:
                m = "Can't get old calibration parameters from the settings dialog."
                print(m)
                
        self.linearFittingWidget = CalibrationLinearFittingWidget(self,
                                                      self.tof_calibration,
                                                      old_params)
        
        self.ui.fittingResultsLayout.addWidget(self.curveFittingWidget)
        self.ui.calibrationResultsLayout.addWidget(self.linearFittingWidget)        
        
        # Set up connections
        self.ui.cutFilesTreeWidget.itemSelectionChanged.connect(
            lambda: self.change_current_cut(
                self.ui.cutFilesTreeWidget.currentItem()))
        self.ui.pointsTreeWidget.itemClicked.connect(self.__set_state_for_point)
        self.ui.acceptPointButton.clicked.connect(self.__accept_point)
        self.ui.binWidthSpinBox.valueChanged.connect(self.__update_curve_fit)
        self.ui.binWidthSpinBox.setKeyboardTracking(False)
        self.ui.acceptCalibrationButton.clicked.connect(self.accept_calibration)
        self.ui.cancelButton.clicked.connect(self.close)
        self.ui.removePointButton.clicked.connect(self.remove_selected_points)
        self.ui.tofChannelLineEdit.editingFinished.connect(
            lambda: self.set_calibration_point(
                float(self.ui.tofChannelLineEdit.text())))
        
        # Set the validator for lineEdit so user can't give invalid values
        double_validator = QtGui.QDoubleValidator()
        self.ui.tofChannelLineEdit.setValidator(double_validator)
        
        self.timer = QtCore.QTimer(interval=1500, timeout=self.timeout)
        self.exec_()
Example #5
0
    def save_cuts(self):
        """Save cut files
        
        Saves data points within selections into cut files.
        """
        if self.selector.is_empty():
            return 0
        if not os.path.exists(self.directory_cuts):
            os.makedirs(self.directory_cuts)

        progress_bar = QtGui.QProgressBar()
        self.statusbar.addWidget(progress_bar, 1)
        progress_bar.show()
        QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents)
        # Mac requires event processing to show progress bar and its
        # process.

        starttime = time.time()

        self.__remove_old_cut_files()

        # Initializes the list size to match the number of selections.
        points_in_selection = [[] for unused_i in range(self.selector.count())]

        # Go through all points in measurement data
        data_count = len(self.data)
        for n in range(data_count):  # while n < data_count:
            if n % 5000 == 0:
                # Do not always update UI to make it faster.
                progress_bar.setValue((n / data_count) * 90)
                QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents)
                # Mac requires event processing to show progress bar and its
                # process.
            point = self.data[n]
            # Check if point is within selectors' limits for faster processing.
            if not self.selector.axes_limits.is_inside(point):
                continue

            dirtyinteger = 0  # Lazyway
            for selection in self.selector.selections:
                if selection.point_inside(point):
                    points_in_selection[dirtyinteger].append(point)
                dirtyinteger += 1

        # Save all found data points into appropriate element cut files
        # Firstly clear old cut files so those won't be accidentally
        # left there.

        dirtyinteger = 0  # Increases with for, for each selection
        content_length = len(points_in_selection)
        for points in points_in_selection:
            if points:  # If not empty selection -> save
                selection = self.selector.get_at(dirtyinteger)
                cut_file = CutFile(self.directory)
                cut_file.set_info(selection, points)
                cut_file.save()
            dirtyinteger += 1
            progress_bar.setValue(90 + (dirtyinteger / content_length) * 10)
            QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents)
            # Mac requires event processing to show progress bar and its
            # process.

        self.statusbar.removeWidget(progress_bar)
        progress_bar.hide()

        log_msg = "Saving finished in {0} seconds.".format(time.time() - starttime)
        logging.getLogger(self.measurement_name).info(log_msg)