示例#1
0
 def test_file_csv_no_converter(self):
     r = utilities.txt2np_array(file_name="files/test_check_txt_file_test_csv.csv",
                                 columns_str="4,6",
                                 substract_first_value="False",
                                 converters={},
                                 column_converter={})
     assert r[0] == False
     assert r[1] == "could not convert string to float: '14:38:58'"
     assert list(r[2].shape) == [0]
示例#2
0
 def test_file_csv_no_converter(self):
     r = utilities.txt2np_array(file_name="files/test_check_txt_file_test_csv.csv",
                                 columns_str="4,6",
                                 substract_first_value="False",
                                 converters={},
                                 column_converter={})
     assert r[0] == False
     assert r[1] == "could not convert string to float: '14:38:58'"
     assert list(r[2].shape) == [0]
示例#3
0
 def test_no_file(self):
     r = utilities.txt2np_array(file_name="files/xxx",
                                 columns_str="4,6",
                                 substract_first_value="False",
                                 converters={},
                                 column_converter={})
     # print(r)
     assert r[0] == False
     assert r[1] == "[Errno 2] No such file or directory: 'files/xxx'"
     assert list(r[2].shape) == [0]
示例#4
0
 def test_no_file(self):
     r = utilities.txt2np_array(file_name="files/xxx",
                                 columns_str="4,6",
                                 substract_first_value="False",
                                 converters={},
                                 column_converter={})
     # print(r)
     assert r[0] == False
     assert r[1] == "[Errno 2] No such file or directory: 'files/xxx'"
     assert list(r[2].shape) == [0]
示例#5
0
    def test_file_csv_converter(self):
        r = utilities.txt2np_array(file_name="files/test_check_txt_file_test_csv.csv",
                                    columns_str="4,6",
                                    substract_first_value="False",
                                    converters={
  "HHMMSS_2_seconds":{
   "name":"HHMMSS_2_seconds",
   "description":"convert HH:MM:SS in seconds since 1970-01-01",
   "code":"\nh, m, s = INPUT.split(':')\nOUTPUT = int(h) * 3600 + int(m) * 60 + int(s)\n\n"
  }
 },
                                    column_converter={4: "HHMMSS_2_seconds"})
        assert r[0] == True
        assert r[1] == ""
        assert r[2][0, 0] == 52738.0
        assert r[2][1, 0] == 52740.0
        assert r[2][0, 1] == 12.4144278
        assert list(r[2].shape) == [10658, 2]
示例#6
0
    def test_file_csv_converter(self):
        r = utilities.txt2np_array(file_name="files/test_check_txt_file_test_csv.csv",
                                    columns_str="4,6",
                                    substract_first_value="False",
                                    converters={
  "HHMMSS_2_seconds":{
   "name":"HHMMSS_2_seconds",
   "description":"convert HH:MM:SS in seconds since 1970-01-01",
   "code":"\nh, m, s = INPUT.split(':')\nOUTPUT = int(h) * 3600 + int(m) * 60 + int(s)\n\n"
  }
 },
                                    column_converter={4: "HHMMSS_2_seconds"})
        assert r[0] == True
        assert r[1] == ""
        assert r[2][0, 0] == 52738.0
        assert r[2][1, 0] == 52740.0
        assert r[2][0, 1] == 12.4144278
        assert list(r[2].shape) == [10658, 2]
示例#7
0
    def __init__(
        self,
        file_name,
        interval,
        time_offset,
        plot_style,
        plot_title,
        y_label,
        columns_to_plot,
        substract_first_value,
        converters,
        column_converter,
        log_level="",
    ):
        super().__init__()

        if log_level:
            logging.basicConfig(level=log_level)

        self.installEventFilter(self)

        self.setWindowTitle("External data: " + plot_title)

        d = {}
        # convert dict keys in int:
        for k in column_converter:
            d[int(k)] = column_converter[k]
        column_converter = dict(d)

        self.myplot = MyMplCanvas(self)

        self.button_plus = QPushButton("+", self)
        self.button_plus.clicked.connect(lambda: self.zoom(-1))

        self.button_minus = QPushButton("-", self)
        self.button_minus.clicked.connect(lambda: self.zoom(1))

        self.layout = QVBoxLayout()

        self.hlayout1 = QHBoxLayout()
        self.hlayout1.addWidget(QLabel("Zoom"))
        self.hlayout1.addWidget(self.button_plus)
        self.hlayout1.addWidget(self.button_minus)
        self.hlayout1.addItem(
            QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum))

        self.hlayout2 = QHBoxLayout()
        self.hlayout2.addWidget(QLabel("Value"))
        self.lb_value = QLabel("")
        self.hlayout2.addWidget(self.lb_value)
        self.hlayout2.addItem(
            QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum))

        self.layout.addLayout(self.hlayout1)
        self.layout.addLayout(self.hlayout2)
        self.layout.addWidget(self.myplot)

        self.setLayout(self.layout)

        self.plot_style = plot_style
        self.plot_title = plot_title
        self.time_offset = time_offset
        self.y_label = y_label
        self.error_msg = ""

        result, error_msg, data = txt2np_array(
            file_name,
            columns_to_plot,
            substract_first_value,
            converters=converters,
            column_converter=column_converter,
        )  # txt2np_array defined in utilities.py

        if not result:
            self.error_msg = error_msg
            return

        logging.debug("data[50]: {}".format(data[:50]))
        logging.debug("shape: {}".format(data.shape))

        if data.shape == (0, ):
            self.error_msg = "Empty input file"
            return

        # sort data by time ascending
        data = data[data[:, 0].argsort()]

        # unique
        u, idx = np.unique(data[:, 0], return_index=True)
        data = data[idx]

        # time
        min_time_value, max_time_value = min(data[:, 0]), max(data[:, 0])

        # variable
        min_var_value, max_var_value = min(data[:, 1]), max(data[:, 1])

        # check if time is linear
        diff = set(np.round(np.diff(data, axis=0)[:, 0], 4))
        if min(diff) == 0:
            self.error_msg = "more values for same time"
            return

        logging.debug("diff: {}".format(diff))
        min_time_step = min(diff)
        logging.debug("min_time_step: {}".format(min_time_step))

        # check if sampling rate is not constant
        if len(diff) != 1:
            logging.debug("len diff != 1")
            min_time_step = min(diff)
            logging.debug("min_time_step: {}".format(min_time_step))

            # increase value for low sampling rate (> 1 s)
            if min_time_step > 1:
                min_time_step = 1
            '''
            x2 = np.arange(min_time_value, max_time_value + min_time_step, min_time_step)
            y2 = np.interp(x2, data[:,0], data[:,1])
            data = np.array((x2, y2)).T
            del x2, y2
            '''

            x2 = np.arange(min_time_value, max_time_value + min_time_step,
                           min_time_step)
            data = np.array((x2, np.interp(x2, data[:, 0], data[:, 1]))).T
            del x2

            logging.debug("data[:,0]: {}".format(data[:, 0]))

            # time
            min_time_value, max_time_value = min(data[:, 0]), max(data[:, 0])
            # variable
            min_var_value, max_var_value = min(data[:, 1]), max(data[:, 1])

            diff = set(np.round(np.diff(data, axis=0)[:, 0], 4))
            min_time_step = min(diff)

        # subsampling
        if min_time_step < 0.04:
            data = data[0::int(round(0.04 / min_time_step, 2))]
            min_time_step = 0.04

        logging.debug("new data after subsampling: {}".format(data[:50]))

        min_value, max_value = min(data[:, 1]), max(data[:, 1])

        max_frequency = 1 / min_time_step

        self.time_interval = interval * max_frequency

        # plotter and thread are none at the beginning
        self.plotter = Plotter()
        self.plotter.data = data
        self.plotter.max_frequency = max_frequency

        #self.plotter.time_interval = time_interval
        self.plotter.min_value = min_var_value
        self.plotter.max_value = max_var_value

        self.plotter.min_time_value = min_time_value
        self.plotter.max_time_value = max_time_value

        self.plotter.min_time_step = min_time_step

        # interval must be even
        interval += 1 if interval % 2 else 0
        self.plotter.interval = interval

        self.thread = QThread()

        # connect signals
        self.send_fig.connect(self.plotter.replot)
        self.plotter.return_fig.connect(self.plot)
        #move to thread and start
        self.plotter.moveToThread(self.thread)
        self.thread.start()

        if min_time_step < .2:
            self.time_out = 200
        else:
            self.time_out = min_time_step * 1000
示例#8
0
    def __init__(
            self,
            file_name,
            interval,
            time_offset,
            plot_style,
            plot_title,
            y_label,
            columns_to_plot,
            substract_first_value,
            converters,
            column_converter,
            log_level=""):

        super().__init__()

        self.installEventFilter(self)

        self.setWindowTitle(f"External data: {plot_title}")

        d = {}
        # convert dict keys in int:
        for k in column_converter:
            d[int(k)] = column_converter[k]
        column_converter = dict(d)

        self.myplot = MyMplCanvas(self)

        self.button_plus = QPushButton("+", self)
        self.button_plus.clicked.connect(lambda: self.zoom(-1))

        self.button_minus = QPushButton("-", self)
        self.button_minus.clicked.connect(lambda: self.zoom(1))

        self.layout = QVBoxLayout()

        self.hlayout1 = QHBoxLayout()
        self.hlayout1.addWidget(QLabel("Zoom"))
        self.hlayout1.addWidget(self.button_plus)
        self.hlayout1.addWidget(self.button_minus)
        self.hlayout1.addItem(QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum))

        self.hlayout2 = QHBoxLayout()
        self.hlayout2.addWidget(QLabel("Value"))
        self.lb_value = QLabel("")
        self.hlayout2.addWidget(self.lb_value)
        self.hlayout2.addItem(QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum))

        self.layout.addLayout(self.hlayout1)
        self.layout.addLayout(self.hlayout2)
        self.layout.addWidget(self.myplot)

        self.setLayout(self.layout)

        self.plot_style = plot_style
        self.plot_title = plot_title
        try:
            self.time_offset = Decimal(time_offset)
        except Exception:
            self.error_msg = "The offset value {} is not a decimal value".format(time_offset)
            return

        self.y_label = y_label
        self.error_msg = ""

        result, error_msg, data = txt2np_array(
            file_name,
            columns_to_plot,
            substract_first_value,
            converters=converters,
            column_converter=column_converter,
        )  # txt2np_array defined in utilities.py

        if not result:
            self.error_msg = error_msg
            return

        logging.debug("data[50]: {}".format(data[:50]))
        logging.debug("shape: {}".format(data.shape))

        if data.shape == (0, ):
            self.error_msg = "Empty input file"
            return

        # sort data by time ascending
        data = data[data[:, 0].argsort()]

        # unique
        u, idx = np.unique(data[:, 0], return_index=True)
        data = data[idx]

        # time
        min_time_value, max_time_value = min(data[:, 0]), max(data[:, 0])

        # variable
        min_var_value, max_var_value = min(data[:, 1]), max(data[:, 1])

        # check if time is linear
        diff = set(np.round(np.diff(data, axis=0)[:, 0], 4))

        if min(diff) == 0:
            self.error_msg = "more values for same time"
            return

        logging.debug("diff: {}".format(diff))

        min_time_step = min(diff)

        logging.debug("min_time_step: {}".format(min_time_step))

        # check if sampling rate is not constant
        if len(diff) != 1:

            logging.debug("len diff != 1")

            min_time_step = min(diff)

            logging.debug("min_time_step: {}".format(min_time_step))

            # increase value for low sampling rate (> 1 s)
            if min_time_step > 1:
                min_time_step = 1

            x2 = np.arange(min_time_value, max_time_value + min_time_step, min_time_step)
            data = np.array((x2, np.interp(x2, data[:, 0], data[:, 1]))).T
            del x2

            logging.debug("data[:,0]: {}".format(data[:, 0]))

            # time
            min_time_value, max_time_value = min(data[:, 0]), max(data[:, 0])
            # variable
            min_var_value, max_var_value = min(data[:, 1]), max(data[:, 1])

            diff = set(np.round(np.diff(data, axis=0)[:, 0], 4))
            min_time_step = min(diff)

        # subsampling
        if min_time_step < 0.04:
            data = data[0::int(round(0.04 / min_time_step, 2))]
            min_time_step = 0.04

        logging.debug("new data after subsampling: {}".format(data[:50]))

        min_value, max_value = min(data[:, 1]), max(data[:, 1])

        max_frequency = 1 / min_time_step

        self.time_interval = interval * max_frequency

        # plotter and thread are none at the beginning
        self.plotter = Plotter()
        self.plotter.data = data
        self.plotter.max_frequency = max_frequency

        self.plotter.min_value = min_var_value
        self.plotter.max_value = max_var_value

        self.plotter.min_time_value = min_time_value
        self.plotter.max_time_value = max_time_value

        self.plotter.min_time_step = min_time_step

        # interval must be even
        interval += 1 if interval % 2 else 0
        self.plotter.interval = interval

        self.thread = QThread()

        # connect signals
        self.send_fig.connect(self.plotter.replot)
        self.plotter.return_fig.connect(self.plot)
        # move to thread and start
        self.plotter.moveToThread(self.thread)
        self.thread.start()

        if min_time_step < .2:
            self.time_out = 200
        else:
            self.time_out = min_time_step * 1000