Skip to content

Commit

Permalink
np: use asarray where possible with atleast_Xd to mimic ndmin feature
Browse files Browse the repository at this point in the history
  • Loading branch information
MuellerSeb committed Apr 5, 2024
1 parent 4de1557 commit 7971261
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
4 changes: 1 addition & 3 deletions src/gstools/krige/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,7 @@ def _pre_ext_drift(self, pnt_cnt, ext_drift=None, set_cond=False):
the drift values at the given positions
"""
if ext_drift is not None:
ext_drift = np.array(
ext_drift, dtype=np.double, ndmin=2, copy=False
)
ext_drift = np.atleast_2d(np.asarray(ext_drift, dtype=np.double))
if ext_drift.size == 0: # treat empty array as no ext_drift
return np.array([])
if set_cond:
Expand Down
6 changes: 3 additions & 3 deletions src/gstools/tools/geometric.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ def generate_st_grid(pos, time, mesh_type="unstructured"):
if mesh_type != "unstructured":
pos = generate_grid(pos)
else:
pos = np.array(pos, dtype=np.double, ndmin=2, copy=False)
pos = np.atleast_2d(np.asarray(pos, dtype=np.double))
out = [np.repeat(p.reshape(-1), np.size(time)) for p in pos]
out.append(np.tile(time, np.size(pos[0])))
return np.asarray(out, dtype=np.double)
Expand Down Expand Up @@ -552,7 +552,7 @@ def format_unstruct_pos_shape(pos, shape, check_stacked_shape=False):
# now we try to be smart
pre_len = len(np.atleast_1d(pos))
# care about 1D: pos can be given as 1D array here -> convert to 2D array
pos = np.array(pos, dtype=np.double, ndmin=2, copy=False)
pos = np.atleast_2d(np.asarray(pos, dtype=np.double))
post_len = len(pos)
# first array dimension should be spatial dimension (1D is special case)
dim = post_len if pre_len == post_len else 1
Expand Down Expand Up @@ -606,7 +606,7 @@ def ang2dir(angles, dtype=np.double, dim=None):
the array of direction vectors
"""
pre_dim = np.asanyarray(angles).ndim
angles = np.array(angles, ndmin=2, dtype=dtype, copy=False)
angles = np.atleast_2d(np.asarray(angles, dtype=dtype))
if len(angles.shape) > 2:
raise ValueError(f"Can't interpret angles array {angles}")
dim = angles.shape[1] + 1 if dim is None else dim
Expand Down
6 changes: 3 additions & 3 deletions src/gstools/variogram/variogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def vario_estimate(
John Wiley & Sons. (2007)
"""
if bin_edges is not None:
bin_edges = np.array(bin_edges, ndmin=1, dtype=np.double, copy=False)
bin_edges = np.atleast_1d(np.asarray(bin_edges, dtype=np.double))
bin_centers = (bin_edges[:-1] + bin_edges[1:]) / 2.0
# allow multiple fields at same positions (ndmin=2: first axis -> field ID)
# need to convert to ma.array, since list of ma.array is not recognised
Expand Down Expand Up @@ -315,7 +315,7 @@ def vario_estimate(
# set directions
dir_no = 0
if direction is not None and dim > 1:
direction = np.array(direction, ndmin=2, dtype=np.double, copy=False)
direction = np.atleast_2d(np.asarray(direction, dtype=np.double))
if len(direction.shape) > 2:
raise ValueError(f"Can't interpret directions: {direction}")
if direction.shape[1] != dim:
Expand Down Expand Up @@ -473,7 +473,7 @@ def vario_estimate_axis(
if not config.USE_RUST:
mask = np.asarray(mask, dtype=np.int32)
else:
field = np.array(field, ndmin=1, dtype=np.double, copy=False)
field = np.atleast_1d(np.asarray(field, dtype=np.double))
missing_mask = None # free space

axis_to_swap = AXIS_DIR[direction] if direction in AXIS else int(direction)
Expand Down

0 comments on commit 7971261

Please sign in to comment.