Skip to content

Commit

Permalink
replace param1, param2 with params
Browse files Browse the repository at this point in the history
  • Loading branch information
patel-zeel authored and tom-andersson committed Sep 6, 2023
1 parent e68e0d9 commit b072d1d
Showing 1 changed file with 22 additions and 29 deletions.
51 changes: 22 additions & 29 deletions deepsensor/data/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ def check_params_computed(self, var_ID, method):
if (
var_ID in self.norm_params
and self.norm_params[var_ID]["method"] == method
and "param1" in self.norm_params[var_ID]
and "param2" in self.norm_params[var_ID]
and "params" in self.norm_params[var_ID]
):
return True
else:
Expand All @@ -169,9 +168,8 @@ def get_norm_params(self, var_ID, data, method=None):
)

if self.check_params_computed(var_ID, method):
# Already have "param1" and "param2" in norm_params with `"method": method` - load them
param1 = self.norm_params[var_ID]["param1"]
param2 = self.norm_params[var_ID]["param2"]
# Already have "params" in norm_params with `"method": method` - load them
params = self.norm_params[var_ID]["params"]
else:
# Params not computed - compute them now
if self.verbose:
Expand All @@ -182,19 +180,16 @@ def get_norm_params(self, var_ID, data, method=None):
)
DataProcessor.load_dask(data)
if method == "mean_std":
param1 = float(data.mean())
param2 = float(data.std())
params = {"mean": float(data.mean()), "std": float(data.std())}
elif method == "min_max":
param1 = float(data.min())
param2 = float(data.max())
params = {"min": float(data.min()), "max": float(data.max())}
if self.verbose:
print(
f"Done. {var_ID} {method} param1={param1:.3f}, param2={param2:.3f}"
)
print(f"Done. {var_ID} {method} params={params}")
self.add_to_norm_params(
var_ID, **{"method": method, "param1": param1, "param2": param2}
var_ID,
**{"method": method, "params": params},
)
return param1, param2
return params

def map_coord_array(self, coord_array: np.ndarray, unnorm: bool = False):
"""Normalise or unnormalise a coordinate array
Expand Down Expand Up @@ -325,30 +320,28 @@ def map_array(
f"Method {method} not recognised. Use one of {self.valid_methods}"
)

param1, param2 = self.get_norm_params(var_ID, data, method)
params = self.get_norm_params(var_ID, data, method)

if method == "mean_std":
if unnorm:
scale = param2
offset = param1
data = data * params["std"]
if add_offset:
data = data + params["mean"]
else:
scale = 1 / param2
offset = -param1 / param2
data = data * scale
if add_offset:
data = data + offset
if add_offset:
data = data - params["mean"]
data = data / params["std"]
return data

elif method == "min_max":
if unnorm:
scale = (param2 - param1) / 2
offset = (param2 + param1) / 2
data = data * (params["max"] - params["min"])
if add_offset:
data = data + params["min"]
else:
scale = 2 / (param2 - param1)
offset = -(param2 + param1) / (param2 - param1)
data = data * scale
if add_offset:
data = data + offset
if add_offset:
data = data - params["min"]
data = data / (params["max"] - params["min"])
return data

def map(
Expand Down

0 comments on commit b072d1d

Please sign in to comment.