Пример #1
0
def reboot_database():
    """Delete and recreate the database between test classes"""
    settings = RaseSettings()
    if os.path.isdir(settings.getSampleDirectory()):
        shutil.rmtree(settings.getSampleDirectory())
    if os.path.isfile(settings.getDatabaseFilepath()):
        os.remove(settings.getDatabaseFilepath())
        initializeDatabase(settings.getDatabaseFilepath())
Пример #2
0
def complete_tests():
    """Make sure no sample spectra are left after the final test is run"""
    settings = RaseSettings()
    original_data_dir = settings.getDataDirectory()
    settings.setDataDirectory(os.path.join(os.getcwd(), '__temp_test_rase'))
    yield None  # anything before this line will be run prior to the tests
    print('CLEANING UP')  # run after the last test
    settings = RaseSettings()
    if os.path.isdir(settings.getSampleDirectory()):
        shutil.rmtree(settings.getSampleDirectory())
    if os.path.isfile(settings.getDatabaseFilepath()):
        os.remove(settings.getDatabaseFilepath())
    settings.setDataDirectory(original_data_dir)
    print('CLEANUP COMPLETE')
Пример #3
0
def make_scenIdall(session, input_data):
    settings = RaseSettings()
    detector = session.query(Detector).filter_by(
        name=input_data['instrument']).first()

    repeat_scens = []
    for bkgd_data in input_data['background']:
        bkgd = bkgd_data[0]
        repeat_scens = repeat_scens + session.query(Scenario).join(
            ScenarioMaterial).join(ScenarioBackgroundMaterial).filter(
                Scenario.acq_time == float(input_data['dwell_time'])).filter(
                    ScenarioMaterial.fd_mode == input_data['source_fd'],
                    ScenarioMaterial.material_name
                    == input_data['source']).filter(
                        ScenarioBackgroundMaterial.fd_mode == bkgd[0],
                        ScenarioBackgroundMaterial.material_name
                        == bkgd[1].name, ScenarioBackgroundMaterial.dose
                        == bkgd[2]).all()

    common_scens = [
        x for x in set(repeat_scens)
        if repeat_scens.count(x) == len(input_data['background'])
    ]

    scenIdall = []
    for scen in common_scens:
        results_dir = get_results_dir(settings.getSampleDirectory(), detector,
                                      scen.id)
        if files_endswith_exists(results_dir, (".n42", ".res")):
            scenIdall.append(scen.id)

    return scenIdall
Пример #4
0
def check_edge_ids(session, replications, start_list, end_list, edge_ids,
                   detector):
    """Check if there are results ready for values on the rising edge of the S-curve"""
    settings = RaseSettings()
    counter = [0] * 3
    for i, ids_list in enumerate([edge_ids, start_list, end_list]):
        for id in ids_list:
            scen = session.query(Scenario).filter_by(id=id).first()
            results_dir = get_results_dir(settings.getSampleDirectory(),
                                          detector, id)
            if files_endswith_exists(
                    results_dir,
                (".n42", ".res")) and scen.replication >= replications:
                counter[i] += 1

    return counter[0], counter[1], counter[2]
Пример #5
0
def cleanup_scenarios(rangefind_rep, scenIds):
    """Remove scenarios from the database that were rangefinders, i.e.: low replication scenarios"""
    settings = RaseSettings()
    session = Session()

    scenarios = []
    for scen in scenIds:
        scenarios.append(session.query(Scenario).filter_by(id=scen).first())

    scens_to_delete = []
    for scen in scenarios:
        if scen.replication == rangefind_rep:
            scens_to_delete.append(scen.id)

    delete_scenario(scens_to_delete, settings.getSampleDirectory())

    session.commit()
Пример #6
0
def run_scenarios(r,
                  scenIds,
                  detector,
                  replay_name,
                  condition=False,
                  expand=0,
                  first_run=False):
    settings = RaseSettings()

    if condition:
        len_prog = len(scenIds)
        progress = QProgressDialog(
            'S-curve range found!\n Generating higher statistics scenarios...',
            'Abort', 0, len(scenIds), r)
    else:
        if not len(scenIds):
            return
        elif len(scenIds) == 1:
            len_prog = 3
            progress = QProgressDialog(
                'Expanding S-curve search...\n Steps taken = ' +
                str(expand - 1) + ', Scenario ID = ' + scenIds[0], 'Abort', 0,
                len_prog, r)
        else:
            if first_run:
                len_prog = len(scenIds)
                progress = QProgressDialog(
                    'Generating range-finding S-curve scenarios...', 'Abort',
                    0, len_prog, r)
            else:
                len_prog = len(scenIds)
                progress = QProgressDialog(
                    'Adding scenarios to rising edge...', 'Abort', 0, len_prog,
                    r)

    progress.setMaximum(len_prog)
    count = 0
    progress.setMinimumDuration(0)
    progress.setValue(count)
    progress.resize(QSize(300, 50))
    progress.setWindowModality(Qt.WindowModal)
    for scenId in scenIds:
        if progress.wasCanceled():
            progress.close()
            return True
        results_dir = get_results_dir(settings.getSampleDirectory(), detector,
                                      scenId)
        # do not regenerate already existing results
        # using scenIds instead of scenIds_no_persist in case the scenario exists but with no results
        if not files_endswith_exists(results_dir, (".n42", ".res")):
            r.genSpectra([scenId], [detector.name], False)
            if len(scenIds) == 1:
                count += 1
                progress.setValue(count)
            r.runReplay([scenId], [detector.name], False)
            if len(scenIds) == 1:
                count += 1
                progress.setValue(count)
            r.runTranslator([scenId], [detector.name], [replay_name], False)
        count += 1
        progress.setValue(count)
    progress.setValue(len_prog)
Пример #7
0
def gen_scens(input_data,
              advanced,
              session,
              scenIdall,
              scenIds_no_persist,
              group_name,
              reps=1,
              condition=False):
    settings = RaseSettings()
    """Generate the scenarios, including source, background, dwell time, and replications"""

    scenGroup = session.query(ScenarioGroup).filter_by(name=group_name).first()

    test_points = logspace_gen(num_points=advanced['num_points'],
                               start_g=advanced['min_guess'],
                               end_g=advanced['max_guess'])

    if advanced['add_points'] and condition:
        # high-stats run
        test_points += advanced['add_points']

    m = session.query(Material).filter_by(name=input_data['source']).first()

    scenIds = [
    ]  # scenarios that would be generated here regardless of persistence
    # to prevent accidental duplicates
    for d in set(test_points):
        sm = ScenarioMaterial(material=m,
                              fd_mode=input_data['source_fd'],
                              dose=d)
        sb = []
        for [(mode, mat, dose)] in input_data['background']:
            bm = session.query(Material).filter_by(name=mat.name).first()
            sb.append(
                ScenarioBackgroundMaterial(material=bm,
                                           fd_mode=mode,
                                           dose=float(dose)))

        persist = session.query(Scenario).filter_by(id=Scenario.scenario_hash(
            input_data['dwell_time'], [sm], sb)).first()
        if persist:
            if persist.replication < reps:
                scenDelete = session.query(Scenario).filter(
                    Scenario.id == persist.id)
                matDelete = session.query(ScenarioMaterial).filter(
                    ScenarioMaterial.scenario_id == persist.id)
                backgMatDelete = session.query(
                    ScenarioBackgroundMaterial).filter(
                        ScenarioBackgroundMaterial.scenario_id == persist.id)
                matDelete.delete()
                backgMatDelete.delete()
                scenDelete.delete()

                folders = [
                    fd for fd in glob.glob(
                        os.path.join(settings.getSampleDirectory(), "*" +
                                     persist.id + "*"))
                ]
                for folder in folders:
                    shutil.rmtree(folder)

                scens = Scenario(input_data['dwell_time'], reps, [sm], sb, [],
                                 [])
                scenIds.append(scens.id)
                scenIds_no_persist.append(scens.id)
            else:
                scenIds.append(persist.id)
        else:
            scens = Scenario(input_data['dwell_time'], reps, [sm], sb, [], [])
            scenIds.append(scens.id)
            scenIds_no_persist.append(scens.id)
            scenGroup.scenarios.append(scens)

    scenIdall = scenIdall + [s for s in scenIds if not s in scenIdall]

    session.commit()

    return scenIds, scenIds_no_persist, scenIdall
Пример #8
0
class DetailedResultsDialog(ui_detailed_results_dialog.Ui_dlgDetailedResults, QDialog):
    def __init__(self, resultMap, scenario, detector):
        QDialog.__init__(self)
        self.setupUi(self)
        self.settings = RaseSettings()
        self.headerLabels = ['File', 'Tp', 'Fn', 'Fp', 'Precision', 'Recall', 'Fscore', 'IDs']
        self.NUM_COLS = len(self.headerLabels)
        self.session = Session()
        self.scenario = scenario
        self.detector = detector
        self.tblDetailedResults.setContextMenuPolicy(Qt.CustomContextMenu)
        self.tblDetailedResults.setColumnCount(self.NUM_COLS)
        self.tblDetailedResults.setHorizontalHeaderLabels(self.headerLabels)
        self.tblDetailedResults.horizontalHeader().setSectionResizeMode(0, QHeaderView.Stretch)
        self.tblDetailedResults.setRowCount(0)
        row=0
        for file in sorted(resultMap, key=natural_keys):
            results = resultMap[file]
            results.insert(0, file)
            self.tblDetailedResults.insertRow(row)
            for col in range(self.NUM_COLS):
                self.tblDetailedResults.setItem(row, col, QTableWidgetItem(results[col]))
            row = row + 1
            results.pop(0)
        self.tblDetailedResults.resizeColumnsToContents()
        self.tblDetailedResults.horizontalHeader().setSectionResizeMode(0, QHeaderView.Stretch)
        self.buttonOK.clicked.connect(self.closeSelected)
        self.buttonExport.clicked.connect(self.handleExport)

    def handleExport(self):
        """
        exports to CSV
        """
        path = QFileDialog.getSaveFileName(self, 'Save File', self.settings.getDataDirectory(), 'CSV (*.csv)')
        if path[0]:
            with open(path[0], mode='w', newline='') as stream:
                writer = csv.writer(stream)
                writer.writerow(self.headerLabels)
                for row in range(self.tblDetailedResults.rowCount()):
                    rowdata = []
                    for column in range(self.tblDetailedResults.columnCount()):
                        item = self.tblDetailedResults.item(row, column)
                        if item is not None:
                            rowdata.append(item.text())
                        else:
                            rowdata.append('')
                    writer.writerow(rowdata)

    def get_selected_cells_as_text(self):
        """
        Returns the selected cells of the table as plain text
        """
        selected_rows =  self.tblDetailedResults.selectedIndexes()
        text = ""
        # show the context menu only if on an a valid part of the table
        if selected_rows:
            cols = set(index.column() for index in self.tblDetailedResults.selectedIndexes())
            for row in set(index.row() for index in self.tblDetailedResults.selectedIndexes()):
                text += "\t".join([self.tblDetailedResults.item(row, col).text() for col in cols])
                text += '\n'
        return text

    def keyPressEvent(self, e):
        if e.key() == Qt.Key_Copy or e.key == QKeySequence(QKeySequence.Copy) or e.key() == 67:
            QApplication.clipboard().setText(self.get_selected_cells_as_text())

    @pyqtSlot(QPoint)
    def on_tblDetailedResults_customContextMenuRequested(self, point):
        """
        Handles "Copy" right click selections on the table
        """
        copy_action = QAction('Copy', self)
        menu = QMenu(self.tblDetailedResults)
        menu.addAction(copy_action)
        action = menu.exec_(self.tblDetailedResults.mapToGlobal(point))
        if action == copy_action:
            QApplication.clipboard().setText(self.get_selected_cells_as_text())

    @pyqtSlot(int, int)
    def on_tblDetailedResults_cellDoubleClicked(self, row, col):
        """
        Listens for cell double click and launches sample spectra viewer
        """
        # FIXME: Note plotting order is guaranteed only if filenames of results files match sample spectra filenames
        if count_files_endwith(get_sample_dir(self.settings.getSampleDirectory(), self.detector, self.scenario.id),
                                 (".n42",)) > row:
            SampleSpectraViewerDialog(self, self.scenario, self.detector, row).exec_()
        else:
            QMessageBox.information(self, 'Information', 'Unable to display spectra.<br>'
                                                         'No sampled spectra available in native RASE format')

    def closeSelected(self):
        """
        Closes dialog
        """
        super().accept()