From a6f5be8bfd2db1f002e7889ecb8e9a037ea08886 Mon Sep 17 00:00:00 2001 From: MuellerSeb Date: Tue, 3 Sep 2019 16:39:46 +0200 Subject: [PATCH] better conversion for pos2xyz; 1D special case --- gstools/field/condition.py | 4 ++-- gstools/field/plot.py | 6 +++--- gstools/krige/ordinary.py | 17 ++++++++++++----- gstools/krige/simple.py | 17 ++++++++++++----- gstools/tools/geometric.py | 2 ++ 5 files changed, 31 insertions(+), 15 deletions(-) diff --git a/gstools/field/condition.py b/gstools/field/condition.py index fddef4533..176be302b 100644 --- a/gstools/field/condition.py +++ b/gstools/field/condition.py @@ -41,7 +41,7 @@ def ordinary(srf): krige_field, krige_var = krige_ok(srf.pos, srf.mesh_type) # evaluate the field at the conditional points - x, y, z = pos2xyz(srf.cond_pos) + x, y, z = pos2xyz(srf.cond_pos, max_dim=srf.model.dim) if srf.model.do_rotation: x, y, z = unrotate_mesh(srf.model.dim, srf.model.angles, x, y, z) y, z = make_isotropic(srf.model.dim, srf.model.anis, y, z) @@ -84,7 +84,7 @@ def simple(srf): krige_field, krige_var = krige_sk(srf.pos, srf.mesh_type) # evaluate the field at the conditional points - x, y, z = pos2xyz(srf.cond_pos) + x, y, z = pos2xyz(srf.cond_pos, max_dim=srf.model.dim) if srf.model.do_rotation: x, y, z = unrotate_mesh(srf.model.dim, srf.model.angles, x, y, z) y, z = make_isotropic(srf.model.dim, srf.model.anis, y, z) diff --git a/gstools/field/plot.py b/gstools/field/plot.py index 526fb15fb..acaf15dae 100644 --- a/gstools/field/plot.py +++ b/gstools/field/plot.py @@ -58,7 +58,7 @@ def _plot_1d(pos, field, fig=None, ax=None): # pragma: no cover """Plot a 1d field.""" fig, ax = _get_fig_ax(fig, ax) title = "Field 1D: " + str(field.shape) - x, __, __ = pos2xyz(pos) + x, __, __ = pos2xyz(pos, max_dim=1) x = x.flatten() arg = np.argsort(x) ax.plot(x[arg], field.ravel()[arg]) @@ -73,7 +73,7 @@ def _plot_2d(pos, field, mesh_type, fig=None, ax=None): # pragma: no cover """Plot a 2d field.""" fig, ax = _get_fig_ax(fig, ax) title = "Field 2D " + mesh_type + ": " + str(field.shape) - x, y, __ = pos2xyz(pos) + x, y, __ = pos2xyz(pos, max_dim=2) if mesh_type == "unstructured": cont = ax.tricontourf(x, y, field.ravel(), levels=256) else: @@ -242,7 +242,7 @@ def plot_vec_field(fld, field="field", fig=None, ax=None): # pragma: no cover fig, ax = _get_fig_ax(fig, ax) title = "Field 2D " + fld.mesh_type + ": " + str(plot_field.shape) - x, y, __ = pos2xyz(fld.pos) + x, y, __ = pos2xyz(fld.pos, max_dim=2) sp = plt.streamplot( x, diff --git a/gstools/krige/ordinary.py b/gstools/krige/ordinary.py index 65172621c..42ceb3f44 100644 --- a/gstools/krige/ordinary.py +++ b/gstools/krige/ordinary.py @@ -49,6 +49,7 @@ def __init__(self, model, cond_pos, cond_val): self.krige_var = None # initialize private attributes self._value_type = "scalar" + self._c_x = self._c_y = self._c_z = None self._cond_pos = None self._cond_val = None self.set_condition(cond_pos, cond_val) @@ -78,9 +79,7 @@ def __call__(self, pos, mesh_type="unstructured"): """ # internal conversation x, y, z = pos2xyz(pos, dtype=np.double, max_dim=self.model.dim) - c_x, c_y, c_z = pos2xyz( - self.cond_pos, dtype=np.double, max_dim=self.model.dim - ) + c_x, c_y, c_z = self.cond_xyz self.pos = xyz2pos(x, y, z) self.mesh_type = mesh_type # format the positional arguments of the mesh @@ -157,8 +156,16 @@ def set_condition(self, cond_pos, cond_val): cond_val : :class:`numpy.ndarray` the values of the conditions """ - self._cond_pos = cond_pos - self._cond_val = np.array(cond_val, dtype=np.double) + self._c_x, self._c_y, self._c_z = pos2xyz( + cond_pos, dtype=np.double, max_dim=self.model.dim + ) + self._cond_pos = xyz2pos(self._c_x, self._c_y, self._c_z) + self._cond_val = np.array(cond_val, dtype=np.double).reshape(-1) + + @property + def cond_xyz(self): + """:class:`list`: The position coordinates of the conditions.""" + return self._c_x, self._c_y, self._c_z @property def cond_pos(self): diff --git a/gstools/krige/simple.py b/gstools/krige/simple.py index 5e2d38095..226b6db50 100644 --- a/gstools/krige/simple.py +++ b/gstools/krige/simple.py @@ -52,6 +52,7 @@ def __init__(self, model, mean, cond_pos, cond_val): self.krige_var = None # initialize private attributes self._value_type = "scalar" + self._c_x = self._c_y = self._c_z = None self._cond_pos = None self._cond_val = None self.set_condition(cond_pos, cond_val) @@ -81,9 +82,7 @@ def __call__(self, pos, mesh_type="unstructured"): """ # internal conversation x, y, z = pos2xyz(pos, dtype=np.double, max_dim=self.model.dim) - c_x, c_y, c_z = pos2xyz( - self.cond_pos, dtype=np.double, max_dim=self.model.dim - ) + c_x, c_y, c_z = self.cond_xyz self.pos = xyz2pos(x, y, z) self.mesh_type = mesh_type # format the positional arguments of the mesh @@ -144,8 +143,16 @@ def set_condition(self, cond_pos, cond_val): cond_val : :class:`numpy.ndarray` the values of the conditions """ - self._cond_pos = cond_pos - self._cond_val = np.array(cond_val, dtype=np.double) + self._c_x, self._c_y, self._c_z = pos2xyz( + cond_pos, dtype=np.double, max_dim=self.model.dim + ) + self._cond_pos = xyz2pos(self._c_x, self._c_y, self._c_z) + self._cond_val = np.array(cond_val, dtype=np.double).reshape(-1) + + @property + def cond_xyz(self): + """:class:`list`: The position coordinates of the conditions.""" + return self._c_x, self._c_y, self._c_z @property def cond_pos(self): diff --git a/gstools/tools/geometric.py b/gstools/tools/geometric.py index 3e39d3348..de297a4e1 100644 --- a/gstools/tools/geometric.py +++ b/gstools/tools/geometric.py @@ -113,6 +113,8 @@ def pos2xyz(pos, dtype=None, calc_dim=False, max_dim=3): ----- If len(pos) > 3, everything after pos[2] will be ignored. """ + if max_dim == 1: # sanity check + pos = np.array(pos, ndmin=2) x = np.array(pos[0], dtype=dtype).reshape(-1) dim = 1 y = z = None