From aff9cd31a7c6324fbca8004ca31d85b4442e1b7e Mon Sep 17 00:00:00 2001 From: Dominik Demuth Date: Fri, 10 Nov 2023 18:56:13 +0100 Subject: [PATCH] add "index" keyword to ascii reader to use row number; part of #135 --- src/gui_qt/_py/asciidialog.py | 23 ++++++++++++++++++++--- src/gui_qt/io/asciireader.py | 10 +++++++--- src/nmreval/io/asciireader.py | 25 ++++++++++++++++++++----- src/resources/_ui/asciidialog.ui | 23 ++++++++++++++++++++--- 4 files changed, 67 insertions(+), 14 deletions(-) diff --git a/src/gui_qt/_py/asciidialog.py b/src/gui_qt/_py/asciidialog.py index 404f5c0..099ff18 100644 --- a/src/gui_qt/_py/asciidialog.py +++ b/src/gui_qt/_py/asciidialog.py @@ -312,7 +312,23 @@ class Ui_ascii_reader(object): self.tabWidget.setCurrentIndex(0) self.buttonbox.rejected.connect(ascii_reader.close) # type: ignore QtCore.QMetaObject.connectSlotsByName(ascii_reader) - ascii_reader.setTabOrder(self.tabWidget, self.ascii_table) + ascii_reader.setTabOrder(self.tabWidget, self.column_checkBox) + ascii_reader.setTabOrder(self.column_checkBox, self.line_spinBox) + ascii_reader.setTabOrder(self.line_spinBox, self.preview_spinBox) + ascii_reader.setTabOrder(self.preview_spinBox, self.pts_radioButton) + ascii_reader.setTabOrder(self.pts_radioButton, self.dsc_radioButton) + ascii_reader.setTabOrder(self.dsc_radioButton, self.FID_radioButton) + ascii_reader.setTabOrder(self.FID_radioButton, self.spectrum_radioButton) + ascii_reader.setTabOrder(self.spectrum_radioButton, self.bds_radioButton) + ascii_reader.setTabOrder(self.bds_radioButton, self.x_lineedit) + ascii_reader.setTabOrder(self.x_lineedit, self.y_lineedit) + ascii_reader.setTabOrder(self.y_lineedit, self.deltay_lineEdit) + ascii_reader.setTabOrder(self.deltay_lineEdit, self.re_button) + ascii_reader.setTabOrder(self.re_button, self.regex_input) + ascii_reader.setTabOrder(self.regex_input, self.re_match_index) + ascii_reader.setTabOrder(self.re_match_index, self.custom_button) + ascii_reader.setTabOrder(self.custom_button, self.custom_input) + ascii_reader.setTabOrder(self.custom_input, self.ascii_table) ascii_reader.setTabOrder(self.ascii_table, self.delay_textfield) ascii_reader.setTabOrder(self.delay_textfield, self.delay_lineedit) ascii_reader.setTabOrder(self.delay_lineedit, self.start_lineedit) @@ -321,6 +337,7 @@ class Ui_ascii_reader(object): ascii_reader.setTabOrder(self.log_checkBox, self.staggered_checkBox) ascii_reader.setTabOrder(self.staggered_checkBox, self.stag_lineEdit) ascii_reader.setTabOrder(self.stag_lineEdit, self.pushButton) + ascii_reader.setTabOrder(self.pushButton, self.skippy_checkbox) def retranslateUi(self, ascii_reader): _translate = QtCore.QCoreApplication.translate @@ -336,9 +353,9 @@ class Ui_ascii_reader(object): self.spectrum_radioButton.setText(_translate("ascii_reader", "Spectrum")) self.bds_radioButton.setText(_translate("ascii_reader", "BDS")) self.label_7.setText(_translate("ascii_reader", "Use columns as")) - self.y_lineedit.setToolTip(_translate("ascii_reader", "

Specify which columns are read for y-values. (\'Points\': Every number creates a new data set;\'FID\'/\'Spectrum\': Numbers at even positions are used for real parts, at odd positions for imaginary parts.)

")) + self.y_lineedit.setToolTip(_translate("ascii_reader", "

Specify which columns are used for y values.

- \'Points\': Every number creates a new data set;
- \'FID\'/\'Spectrum\': Numbers at even positions are used for real parts, at odd positions for imaginary parts.

")) self.y_label.setText(_translate("ascii_reader", "y")) - self.x_lineedit.setToolTip(_translate("ascii_reader", "

Specify which column is used as x-value.

")) + self.x_lineedit.setToolTip(_translate("ascii_reader", "

Specify which column is used for x values, write index to use row numbers (starting with 0).

")) self.label_5.setText(_translate("ascii_reader", "

Δy

")) self.x_label.setText(_translate("ascii_reader", "x")) self.dsdfsf.setText(_translate("ascii_reader", "Numerical value")) diff --git a/src/gui_qt/io/asciireader.py b/src/gui_qt/io/asciireader.py index 4b05a2d..a182693 100644 --- a/src/gui_qt/io/asciireader.py +++ b/src/gui_qt/io/asciireader.py @@ -179,9 +179,13 @@ class QAsciiReader(QtWidgets.QDialog, Ui_ascii_reader): def apply(self): # default row for x is the first row, it will be superseded if an integer number is given. - try: - x = int(self.x_lineedit.text())-1 - except ValueError: + x = self.x_lineedit.text() + if x: + try: + x = int(x)-1 + except ValueError: + pass + else: x = None try: diff --git a/src/nmreval/io/asciireader.py b/src/nmreval/io/asciireader.py index 6f48fec..6e9ceb3 100644 --- a/src/nmreval/io/asciireader.py +++ b/src/nmreval/io/asciireader.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import pathlib import re from io import BytesIO @@ -87,7 +89,7 @@ class AsciiReader: def export( self: 'AsciiReader', - x: int = None, + x: int | str = None, y: list = None, yerr: list = None, mode: str = 'points', @@ -104,18 +106,20 @@ class AsciiReader: elif y is None: raise ValueError('y is None and yerr is not None') - if (x is None) ^ (y is None): - raise ValueError(f'x is {type(x)} and y is {type(y)}, should be both None or both defined') + # if (x is None) ^ (y is None): + # raise ValueError(f'x is {type(x)} and y is {type(y)}, should be both None or both defined') if x is None: x = [0] elif isinstance(x, int): x = [x] + elif isinstance(x, str) and x == 'index': + x = [] else: - raise ValueError(f'x is {type(x)} not int') + raise ValueError(f'type of x is {type(x)} not `int` or `str`') if y is None: - y = list(range(1, max(self.width))) + y = list(range(int(len(x) != 0), max(self.width))) cols = x + y + yerr with self.fname.open('rb') as fh: @@ -138,6 +142,17 @@ class AsciiReader: else: raw_data = raw_data.reshape((1, *raw_data.shape)) + if len(x) == 0 or raw_data.shape[2] == 1: + _temp = np.zeros((raw_data.shape[0], raw_data.shape[1], raw_data.shape[2]+1)) + _temp[:, :, 0] = np.arange(raw_data.shape[1]) + _temp[:, :, 1:] = raw_data + raw_data = _temp + + if y: + y = [i+1 for i in y] + else: + y = [1] + filename = self.fname.stem if self.delays: diff --git a/src/resources/_ui/asciidialog.ui b/src/resources/_ui/asciidialog.ui index 6822011..5492220 100644 --- a/src/resources/_ui/asciidialog.ui +++ b/src/resources/_ui/asciidialog.ui @@ -236,7 +236,7 @@ - <html><head/><body><p>Specify which columns are read for y-values. ('Points': Every number creates a new data set;'FID'/'Spectrum': Numbers at even positions are used for real parts, at odd positions for imaginary parts.)</p></body></html> + <html><head/><body><p>Specify which columns are used for y values.</p><p>- 'Points': Every number creates a new data set;<br/>- 'FID'/'Spectrum': Numbers at even positions are used for real parts, at odd positions for imaginary parts.</p></body></html> Qt::ImhFormattedNumbersOnly|Qt::ImhPreferNumbers @@ -259,7 +259,7 @@ - <html><head/><body><p>Specify which column is used as x-value.</p></body></html> + <html><head/><body><p>Specify which column is used for x values, write <span style=" font-style:italic;">index</span> to use row numbers (starting with 0). </p></body></html> Qt::ImhFormattedNumbersOnly|Qt::ImhPreferNumbers @@ -620,6 +620,22 @@ tabWidget + column_checkBox + line_spinBox + preview_spinBox + pts_radioButton + dsc_radioButton + FID_radioButton + spectrum_radioButton + bds_radioButton + x_lineedit + y_lineedit + deltay_lineEdit + re_button + regex_input + re_match_index + custom_button + custom_input ascii_table delay_textfield delay_lineedit @@ -629,6 +645,7 @@ staggered_checkBox stag_lineEdit pushButton + skippy_checkbox @@ -650,7 +667,7 @@ - +