From 15c959fd71b62293c21fd5476601633f9a4f4193 Mon Sep 17 00:00:00 2001 From: Dominik Demuth Date: Mon, 8 May 2023 19:54:55 +0200 Subject: [PATCH] save fit parameter adds name to file as extra comment; possible solution for #59 --- src/nmreval/fit/result.py | 4 +++- src/nmreval/io/asciireader.py | 31 ++++++++++++++++++++----------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/nmreval/fit/result.py b/src/nmreval/fit/result.py index 813a7bb..74c1c47 100644 --- a/src/nmreval/fit/result.py +++ b/src/nmreval/fit/result.py @@ -292,7 +292,7 @@ class FitResult(Points): with path.open(writemode) as f: if overwrite or not path.exists(): - f.write('# label(1)\t') + f.write('# xvalue(1)\t') for i, pname in enumerate(self.parameter.keys()): raw_name = convert(pname, old='tex', new='str') f.write(f'{raw_name}({2*i+2})\t{raw_name}_err({2*i+3})\t') @@ -311,6 +311,8 @@ class FitResult(Points): for k, v in self.fun_kwargs.items(): f.write(f"{convert(k, old='tex', new='str')}: {convert(str(v), old='tex', new='str')}\t") f.write('\n') + f.write(f'# line above from: {self.name} (model: {self.model_name})') + f.write('\n') def f_test(self, chi2: float, dof: float): if 'red. chi^2' not in self.statistics or dof == self.dof: diff --git a/src/nmreval/io/asciireader.py b/src/nmreval/io/asciireader.py index 58a5881..04fd19e 100644 --- a/src/nmreval/io/asciireader.py +++ b/src/nmreval/io/asciireader.py @@ -18,9 +18,11 @@ class AsciiReader: def __init__(self, fname): self.fname = None self.header = [] - self.data = [] + self.lines = [] + self.num_data = [] self.delays = None self.width = [] + self.num_width = [] self.line_comment = [] self._last_read_pos = 0 @@ -40,15 +42,17 @@ class AsciiReader: break def make_preview(self, num_lines: int): - if num_lines <= len(self.data): - return self.data[:num_lines], max(self.width[:num_lines]) + if num_lines <= len(self.lines): + return self.lines[:num_lines], max(self.width[:num_lines]) num_lines += len(self.header) with self.fname.open('r') as f: - for i, line in enumerate(islice(f, len(self.header)+len(self.data), num_lines)): + for i, line in enumerate(islice(f, len(self.header)+len(self.lines), num_lines)): line = line.rstrip('\n\t\r, ') - line = re.split('[\s,;]', line) + is_empty = len(line) == 0 + + line = re.split(r'[\s,;]', line) try: comment_start = line.index('#') self.line_comment.append(' '.join(line[comment_start:])) @@ -56,10 +60,14 @@ class AsciiReader: except ValueError: self.line_comment.append('') - self.width.append(len(line)) - self.data.append(line) + if not is_empty: + self.width.append(len(line)) + self.lines.append(line) + if not line[0].startswith('#'): + self.num_data.append(line) + self.num_width.append(len(line)) - return self.data, max(self.width) + return self.lines, max(self.width) def look_for_delay(self, fname=None): if fname is None: @@ -97,16 +105,17 @@ class AsciiReader: raise ValueError(f'x is {type(x)} not int') if y is None: - y = list(range(1, max(self.width))) + y = list(range(1, max(self.num_width))) cols = x + y + yerr with self.fname.open('rb') as fh: tmp_ = re.sub(b'[;,]', b' ', fh.read()) raw_data = np.genfromtxt(BytesIO(tmp_), usecols=cols, missing_values='--') del tmp_ + if raw_data.ndim == 1: # only one row or column - if len(self.data) == 1: + if len(self.num_data) == 1: # one row raw_data = raw_data.reshape(1, -1) else: @@ -150,7 +159,7 @@ class AsciiReader: for j in range(1, num_y+1, stepsize): if col_names is not None: # predefined name - kwargs['name'] = col_names[j-1] + kwargs['name'] = col_names[j] elif num_y > single_len: # more than one axis, append column number kwargs['name'] = filename + '_' + str(y[j-1])