save fit parameter adds name to file as extra comment; possible solution for #59

This commit is contained in:
Dominik Demuth 2023-05-08 19:54:55 +02:00
parent 1378cf6ac7
commit 15c959fd71
2 changed files with 23 additions and 12 deletions

View File

@ -292,7 +292,7 @@ class FitResult(Points):
with path.open(writemode) as f: with path.open(writemode) as f:
if overwrite or not path.exists(): if overwrite or not path.exists():
f.write('# label(1)\t') f.write('# xvalue(1)\t')
for i, pname in enumerate(self.parameter.keys()): for i, pname in enumerate(self.parameter.keys()):
raw_name = convert(pname, old='tex', new='str') raw_name = convert(pname, old='tex', new='str')
f.write(f'{raw_name}({2*i+2})\t{raw_name}_err({2*i+3})\t') 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(): 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(f"{convert(k, old='tex', new='str')}: {convert(str(v), old='tex', new='str')}\t")
f.write('\n') 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): def f_test(self, chi2: float, dof: float):
if 'red. chi^2' not in self.statistics or dof == self.dof: if 'red. chi^2' not in self.statistics or dof == self.dof:

View File

@ -18,9 +18,11 @@ class AsciiReader:
def __init__(self, fname): def __init__(self, fname):
self.fname = None self.fname = None
self.header = [] self.header = []
self.data = [] self.lines = []
self.num_data = []
self.delays = None self.delays = None
self.width = [] self.width = []
self.num_width = []
self.line_comment = [] self.line_comment = []
self._last_read_pos = 0 self._last_read_pos = 0
@ -40,15 +42,17 @@ class AsciiReader:
break break
def make_preview(self, num_lines: int): def make_preview(self, num_lines: int):
if num_lines <= len(self.data): if num_lines <= len(self.lines):
return self.data[:num_lines], max(self.width[:num_lines]) return self.lines[:num_lines], max(self.width[:num_lines])
num_lines += len(self.header) num_lines += len(self.header)
with self.fname.open('r') as f: 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 = line.rstrip('\n\t\r, ')
line = re.split('[\s,;]', line) is_empty = len(line) == 0
line = re.split(r'[\s,;]', line)
try: try:
comment_start = line.index('#') comment_start = line.index('#')
self.line_comment.append(' '.join(line[comment_start:])) self.line_comment.append(' '.join(line[comment_start:]))
@ -56,10 +60,14 @@ class AsciiReader:
except ValueError: except ValueError:
self.line_comment.append('') self.line_comment.append('')
if not is_empty:
self.width.append(len(line)) self.width.append(len(line))
self.data.append(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): def look_for_delay(self, fname=None):
if fname is None: if fname is None:
@ -97,16 +105,17 @@ class AsciiReader:
raise ValueError(f'x is {type(x)} not int') raise ValueError(f'x is {type(x)} not int')
if y is None: if y is None:
y = list(range(1, max(self.width))) y = list(range(1, max(self.num_width)))
cols = x + y + yerr cols = x + y + yerr
with self.fname.open('rb') as fh: with self.fname.open('rb') as fh:
tmp_ = re.sub(b'[;,]', b' ', fh.read()) tmp_ = re.sub(b'[;,]', b' ', fh.read())
raw_data = np.genfromtxt(BytesIO(tmp_), usecols=cols, missing_values='--') raw_data = np.genfromtxt(BytesIO(tmp_), usecols=cols, missing_values='--')
del tmp_ del tmp_
if raw_data.ndim == 1: if raw_data.ndim == 1:
# only one row or column # only one row or column
if len(self.data) == 1: if len(self.num_data) == 1:
# one row # one row
raw_data = raw_data.reshape(1, -1) raw_data = raw_data.reshape(1, -1)
else: else:
@ -150,7 +159,7 @@ class AsciiReader:
for j in range(1, num_y+1, stepsize): for j in range(1, num_y+1, stepsize):
if col_names is not None: if col_names is not None:
# predefined name # predefined name
kwargs['name'] = col_names[j-1] kwargs['name'] = col_names[j]
elif num_y > single_len: elif num_y > single_len:
# more than one axis, append column number # more than one axis, append column number
kwargs['name'] = filename + '_' + str(y[j-1]) kwargs['name'] = filename + '_' + str(y[j-1])