Ejemplo n.º 1
0
 def test_tsv(self):
     r = utilities.check_txt_file("files/test_check_txt_file_test_tsv.tsv")
     assert r == {
         'homogeneous': True,
         'fields number': 5,
         'separator': '\t'
     }
Ejemplo n.º 2
0
    def view_data_file_head(self):
        """
        view first parts of data file
        """
        if self.tw_data_files.selectedIndexes() or self.tw_data_files.rowCount(
        ) == 1:
            if self.tw_data_files.rowCount() == 1:
                data_file_path = project_functions.media_full_path(
                    self.tw_data_files.item(0, 0).text(), self.project_path)
            else:
                data_file_path = project_functions.media_full_path(
                    self.tw_data_files.item(
                        self.tw_data_files.selectedIndexes()[0].row(),
                        0).text(), self.project_path)

            file_parameters = utilities.check_txt_file(data_file_path)
            if "error" in file_parameters:
                QMessageBox.critical(
                    self, programName,
                    f"Error on file {data_file_path}: {file_parameters['error']}"
                )
                return
            header = utilities.return_file_header(data_file_path)

            if header:

                w = dialog.View_data_head()
                w.setWindowTitle(f"Data file: {Path(data_file_path).name}")
                w.setWindowFlags(Qt.WindowStaysOnTopHint)
                w.label.setVisible(False)
                w.le.setVisible(False)

                w.tw.setColumnCount(file_parameters["fields number"])
                w.tw.setRowCount(len(header))

                for row in range(len(header)):
                    for col, v in enumerate(header[row].split(
                            file_parameters["separator"])):
                        w.tw.setItem(row, col, QTableWidgetItem(v))

                w.exec_()

            else:
                QMessageBox.critical(self, programName,
                                     f"Error on file {data_file_path}")

        else:
            QMessageBox.warning(self, programName, "Select a data file")
Ejemplo n.º 3
0
    def add_data_file(self, flag_path=True):
        """
        user select a data file to be plotted synchronously with media file

        Args:
            flag_path (bool): True to store path of data file else False
        """

        # check if project saved
        if (not flag_path) and (not self.project_file_name):
            QMessageBox.critical(self, programName, ("It is not possible to add data file without full path "
                                                     "if the project is not saved"))
            return

        # limit to 2 files
        if self.tw_data_files.rowCount() >= 2:
            QMessageBox.warning(self, programName, ("It is not yet possible to plot more than 2 external data sources"
                                                    "This limitation will be removed in future"))
            return

        fd = QFileDialog()
        fd.setDirectory(os.path.expanduser("~") if flag_path else str(Path(self.project_path).parent))

        fn = fd.getOpenFileName(self, "Add data file", "", "All files (*)")
        file_name = fn[0] if type(fn) is tuple else fn

        if file_name:

            columns_to_plot = "1,2"  # columns to plot by default

            # check data file
            r = utilities.check_txt_file(file_name)  # check_txt_file defined in utilities

            if "error" in r:
                QMessageBox.critical(self, programName, r["error"])
                return

            if not r["homogeneous"]:  # not all rows have 2 columns
                QMessageBox.critical(self, programName, "This file does not contain a constant number of columns")
                return

            header = utilities.return_file_header(file_name, row_number=10)

            if header:
                w = dialog.View_data_head()
                w.setWindowTitle(f"Data file: {Path(file_name).name}")
                #w.setWindowFlags(Qt.WindowStaysOnTopHint)

                w.tw.setColumnCount(r["fields number"])
                w.tw.setRowCount(len(header))

                for row in range(len(header)):
                    for col, v in enumerate(header[row].split(r["separator"])):
                        item = QTableWidgetItem(v)
                        item.setFlags(Qt.ItemIsEnabled)
                        w.tw.setItem(row, col, item)

                while True:
                    flag_ok = True
                    if w.exec_():
                        columns_to_plot = w.le.text().replace(" ", "")
                        for col in columns_to_plot.split(","):
                            try:
                                col_idx = int(col)
                            except ValueError:
                                QMessageBox.critical(self, programName, f"<b>{col}</b> does not seem to be a column index")
                                flag_ok = False
                                break
                            if col_idx <= 0 or col_idx > r["fields number"]:
                                QMessageBox.critical(self, programName, f"<b>{col}</b> is not a valid column index")
                                flag_ok = False
                                break
                        if flag_ok:
                            break
                    else:
                        return

                else:
                    return

            else:
                return # problem with header

            self.tw_data_files.setRowCount(self.tw_data_files.rowCount() + 1)

            if not flag_path:
                file_name = str(Path(file_name).name)

            for col_idx, value in zip([PLOT_DATA_FILEPATH_IDX, PLOT_DATA_COLUMNS_IDX,
                                       PLOT_DATA_PLOTTITLE_IDX, PLOT_DATA_VARIABLENAME_IDX,
                                       PLOT_DATA_CONVERTERS_IDX, PLOT_DATA_TIMEINTERVAL_IDX,
                                       PLOT_DATA_TIMEOFFSET_IDX],
                                      [file_name, columns_to_plot,
                                       "", "",
                                       "", "60",
                                       "0"]):
                item = QTableWidgetItem(value)
                if col_idx == PLOT_DATA_CONVERTERS_IDX:
                    item.setFlags(Qt.ItemIsEnabled)
                    item.setBackground(QColor(230, 230, 230))
                self.tw_data_files.setItem(self.tw_data_files.rowCount() - 1, col_idx, item)

            # substract first value
            combobox = QComboBox()
            combobox.addItems(["True", "False"])
            self.tw_data_files.setCellWidget(self.tw_data_files.rowCount() - 1, PLOT_DATA_SUBSTRACT1STVALUE_IDX, combobox)

            # plot line color
            combobox = QComboBox()
            combobox.addItems(DATA_PLOT_STYLES)
            self.tw_data_files.setCellWidget(self.tw_data_files.rowCount() - 1, PLOT_DATA_PLOTCOLOR_IDX, combobox)
Ejemplo n.º 4
0
 def test_empty_file(self):
     r = utilities.check_txt_file("files/empty_file")
     assert r == {'error': 'Could not determine delimiter'}
Ejemplo n.º 5
0
 def test_file_does_not_exists(self):
     r = utilities.check_txt_file("files/xxx")
     assert r == {
         'error': "[Errno 2] No such file or directory: 'files/xxx'"
     }
Ejemplo n.º 6
0
 def test_no_homogeneous(self):
     r = utilities.check_txt_file("files/test.boris")
     assert r == {'homogeneous': False}