Ejemplo n.º 1
0
 def __init__(self, parent, use_cuts, width):
     '''Inits widget.
     
     Args:
         parent: A MeasurementTabWidget.
         use_cuts: A string list representing Cut files.
         width: A float representing Energy Spectrum histogram's bin width.
     '''
     try:
         super().__init__()
         self.parent = parent
         self.icon_manager = parent.icon_manager
         self.measurement = self.parent.measurement
         self.use_cuts = use_cuts
         self.width = width
         if self.measurement.statusbar:
             self.progress_bar = QtGui.QProgressBar()
             self.measurement.statusbar.addWidget(self.progress_bar, 1) 
             self.progress_bar.show()
             QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents)
             # Mac requires event processing to show progress bar and its 
             # process.
         else:
             self.progress_bar = None
         self.ui = uic.loadUi(os.path.join("ui_files",
                                           "ui_energy_spectrum.ui"),
                              self)
         title = "{0} - Bin Width: {1}".format(self.ui.windowTitle(), width)
         self.ui.setWindowTitle(title)
         
         # Generate new tof.in file for external programs
         self.measurement.generate_tof_in()
         # Do energy spectrum stuff on this
         self.energy_spectrum = EnergySpectrum(self.measurement,
                                               use_cuts,
                                               width,
                                               progress_bar=self.progress_bar)
         self.energy_spectrum_data = self.energy_spectrum.calculate_spectrum()
         
         # Check for RBS selections.
         rbs_list = {}
         for cut in self.use_cuts:
             filename = os.path.basename(cut)
             split = filename.split('.')
             if is_rbs(cut):
                 # This should work for regular cut and split.
                 key = "{0}.{1}.{2}".format(split[1], split[2], split[3])
                 rbs_list[key] = get_scatter_element(cut)
         
         # Graph in matplotlib widget and add to window
         self.matplotlib = MatplotlibEnergySpectrumWidget(
                                              self,
                                              self.energy_spectrum_data,
                                              rbs_list)
     except:
         import traceback
         msg = "Could not create Energy Spectrum graph. "
         err_file = sys.exc_info()[2].tb_frame.f_code.co_filename
         str_err = ", ".join([sys.exc_info()[0].__name__ + ": " + \
                       traceback._some_str(sys.exc_info()[1]),
                       err_file,
                       str(sys.exc_info()[2].tb_lineno)])
         msg += str_err
         logging.getLogger(self.measurement.measurement_name).error(msg)
         if hasattr(self, "matplotlib"):
             self.matplotlib.delete()
     finally:
         if self.progress_bar:
             self.measurement.statusbar.removeWidget(self.progress_bar)
             self.progress_bar.hide()
Ejemplo n.º 2
0
class EnergySpectrumWidget(QtGui.QWidget):
    '''Energy spectrum widget which is added to measurement tab.
    '''
    save_file = "widget_energy_spectrum.save"
    
    def __init__(self, parent, use_cuts, width):
        '''Inits widget.
        
        Args:
            parent: A MeasurementTabWidget.
            use_cuts: A string list representing Cut files.
            width: A float representing Energy Spectrum histogram's bin width.
        '''
        try:
            super().__init__()
            self.parent = parent
            self.icon_manager = parent.icon_manager
            self.measurement = self.parent.measurement
            self.use_cuts = use_cuts
            self.width = width
            if self.measurement.statusbar:
                self.progress_bar = QtGui.QProgressBar()
                self.measurement.statusbar.addWidget(self.progress_bar, 1) 
                self.progress_bar.show()
                QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents)
                # Mac requires event processing to show progress bar and its 
                # process.
            else:
                self.progress_bar = None
            self.ui = uic.loadUi(os.path.join("ui_files",
                                              "ui_energy_spectrum.ui"),
                                 self)
            title = "{0} - Bin Width: {1}".format(self.ui.windowTitle(), width)
            self.ui.setWindowTitle(title)
            
            # Generate new tof.in file for external programs
            self.measurement.generate_tof_in()
            # Do energy spectrum stuff on this
            self.energy_spectrum = EnergySpectrum(self.measurement,
                                                  use_cuts,
                                                  width,
                                                  progress_bar=self.progress_bar)
            self.energy_spectrum_data = self.energy_spectrum.calculate_spectrum()
            
            # Check for RBS selections.
            rbs_list = {}
            for cut in self.use_cuts:
                filename = os.path.basename(cut)
                split = filename.split('.')
                if is_rbs(cut):
                    # This should work for regular cut and split.
                    key = "{0}.{1}.{2}".format(split[1], split[2], split[3])
                    rbs_list[key] = get_scatter_element(cut)
            
            # Graph in matplotlib widget and add to window
            self.matplotlib = MatplotlibEnergySpectrumWidget(
                                                 self,
                                                 self.energy_spectrum_data,
                                                 rbs_list)
        except:
            import traceback
            msg = "Could not create Energy Spectrum graph. "
            err_file = sys.exc_info()[2].tb_frame.f_code.co_filename
            str_err = ", ".join([sys.exc_info()[0].__name__ + ": " + \
                          traceback._some_str(sys.exc_info()[1]),
                          err_file,
                          str(sys.exc_info()[2].tb_lineno)])
            msg += str_err
            logging.getLogger(self.measurement.measurement_name).error(msg)
            if hasattr(self, "matplotlib"):
                self.matplotlib.delete()
        finally:
            if self.progress_bar:
                self.measurement.statusbar.removeWidget(self.progress_bar)
                self.progress_bar.hide()
        
    
    def delete(self):
        '''Delete variables and do clean up.
        '''
        self.energy_spectrum = None
        self.progress_bar = None
        self.matplotlib.delete()
        self.matplotlib = None
        self.ui.close()
        self.ui = None
        self.close()


    def closeEvent(self, evnt):
        '''Reimplemented method when closing widget.
        '''
        self.parent.energy_spectrum_widget = Null()
        file = os.path.join(self.parent.measurement.directory, self.save_file)
        try:
            if os.path.isfile(file):
                os.unlink(file)
        except:
            pass
        super().closeEvent(evnt)
        
        
    def save_to_file(self):
        '''Save object information to file.
        '''
        files = "\t".join([tmp.replace(self.parent.measurement.directory + "\\",
                                       "") 
                           for tmp in self.use_cuts])
        file = os.path.join(self.parent.measurement.directory, self.save_file)
        fh = open(file, 'wt')
        fh.write("{0}\n".format(files))
        fh.write("{0}".format(self.width))
        fh.close()