From 5f3b684f2820e153afa12f83dfcceff417b406eb Mon Sep 17 00:00:00 2001 From: Masaru Nagaso Date: Fri, 8 Nov 2024 17:07:49 -0700 Subject: [PATCH] multi value parameter can be written out in one line / avoid conversion from integer list to float list --- pytomoatt/para.py | 5 +++-- pytomoatt/utils/common.py | 35 ++++++++++++++++++++++++++--------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/pytomoatt/para.py b/pytomoatt/para.py index 59ec95c..e9a859e 100644 --- a/pytomoatt/para.py +++ b/pytomoatt/para.py @@ -2,6 +2,7 @@ from .utils.common import init_axis, str2val yaml = YAML() +yaml.default_flow_style = True class ATTPara: """Class for read and write parameter file with ``yaml`` format @@ -24,10 +25,10 @@ def init_axis(self): self.input_params['domain']['n_rtp'], ) return dep, lat, lon, dd, dt, dp - + def update_param(self, key: str, value) -> None: """Update a parameter in the YAML file. - + :param key: The key of parameter file to be set. Use '.' to separate the keys. :type key: str """ diff --git a/pytomoatt/utils/common.py b/pytomoatt/utils/common.py index 4b6b915..c6a2556 100644 --- a/pytomoatt/utils/common.py +++ b/pytomoatt/utils/common.py @@ -136,21 +136,38 @@ def ignore_nan_3d(data): xidx = np.arange(data.shape[2]) zz, xx, yy = np.meshgrid(zidx, yidx, xidx, indexing='ij') interpolated = griddata( - points, values, - (zz, xx, yy), + points, values, + (zz, xx, yy), method='nearest' ) result = interpolated.reshape(data.shape) return result def str2val(str_val): + # single value handling + # return integer try: return int(str_val) except ValueError: - try: - return float(str_val) - except ValueError: - try: - return [float(v) for v in str_val.strip('[]').split(',')] - except ValueError: - return str_val + pass + + # return float + try: + return float(str_val) + except ValueError: + pass + + # list values handling + # return list of integer + try: + return [int(v) for v in str_val.strip('[]').split(',')] + except ValueError: + pass + + # return list of float + try: + return [float(v) for v in str_val.strip('[]').split(',')] + except ValueError: + pass + + return str_val \ No newline at end of file