Skip to content

Commit

Permalink
refactor(recarray_utils): deprecate functions, use numpy builtins (#1960
Browse files Browse the repository at this point in the history
)
  • Loading branch information
wpbonelli authored Sep 27, 2023
1 parent 637632c commit 941a5f1
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .docs/Notebooks/modpath7_structured_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from numpy.lib.recfunctions import repack_fields

# run installed version of flopy or add local path
try:
Expand Down Expand Up @@ -211,7 +212,7 @@
# Get locations to extract pathline data

nodew = m.dis.get_node([wel_loc])
riv_locs = flopy.utils.ra_slice(m.riv.stress_period_data[0], ["k", "i", "j"])
riv_locs = repack_fields(m.riv.stress_period_data[0][["k", "i", "j"]])
nodesr = m.dis.get_node(riv_locs.tolist())

# Pathline data
Expand Down
7 changes: 3 additions & 4 deletions autotest/test_mp6.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pytest
from autotest.conftest import get_example_data_path
from autotest.test_mp6_cases import Mp6Cases1, Mp6Cases2
from modflow_devtools.markers import has_pkg, requires_exe, requires_pkg
from modflow_devtools.markers import requires_exe, requires_pkg
from numpy.lib.recfunctions import repack_fields
from pytest_cases import parametrize_with_cases

import flopy
Expand All @@ -19,7 +19,6 @@
from flopy.plot import PlotMapView
from flopy.utils import EndpointFile, PathlineFile, TimeseriesFile
from flopy.utils.flopy_io import loadtxt
from flopy.utils.recarray_utils import ra_slice

pytestmark = pytest.mark.mf6

Expand Down Expand Up @@ -171,7 +170,7 @@ def test_get_destination_data(function_tmpdir, mp6_test_path):

# check that all starting locations are included in the pathline data
# (pathline data slice not just endpoints)
starting_locs = ra_slice(well_epd, ["k0", "i0", "j0"])
starting_locs = repack_fields(well_epd[["k0", "i0", "j0"]])
pathline_locs = np.array(
np.array(well_pthld)[["k", "i", "j"]].tolist(),
dtype=starting_locs.dtype,
Expand Down
2 changes: 1 addition & 1 deletion flopy/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
from .optionblock import OptionBlock
from .postprocessing import get_specific_discharge, get_transmissivities
from .rasters import Raster
from .recarray_utils import create_empty_recarray, ra_slice
from .recarray_utils import create_empty_recarray, ra_slice, recarray
from .reference import TemporalReference
from .sfroutputfile import SfrFile
from .swroutputfile import (
Expand Down
7 changes: 3 additions & 4 deletions flopy/utils/modpathfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
from typing import Union

import numpy as np
from numpy.lib.recfunctions import append_fields, stack_arrays
from numpy.lib.recfunctions import append_fields, repack_fields, stack_arrays

from ..utils.flopy_io import loadtxt
from ..utils.recarray_utils import ra_slice


class _ModpathSeries:
Expand Down Expand Up @@ -1161,7 +1160,7 @@ def get_destination_endpoint_data(self, dest_cells, source=False):
else:
keys = ["k", "i", "j"]
try:
raslice = ra_slice(ra, keys)
raslice = repack_fields(ra[keys])
except (KeyError, ValueError):
raise KeyError(
"could not extract "
Expand All @@ -1174,7 +1173,7 @@ def get_destination_endpoint_data(self, dest_cells, source=False):
else:
keys = ["node"]
try:
raslice = ra_slice(ra, keys)
raslice = repack_fields(ra[keys])
except (KeyError, ValueError):
msg = f"could not extract '{keys[0]}' key from endpoint data"
raise KeyError(msg)
Expand Down
39 changes: 24 additions & 15 deletions flopy/utils/recarray_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
from numpy.lib.recfunctions import repack_fields


def create_empty_recarray(length, dtype, default_value=0):
Expand All @@ -22,10 +23,11 @@ def create_empty_recarray(length, dtype, default_value=0):
Examples
--------
>>> import numpy as np
>>> import flopy
>>> dtype = np.dtype([('x', np.float32), ('y', np.float32)])
>>> ra = flopy.utils.create_empty_recarray(10, dtype)
>>> from flopy.utils import create_empty_recarray
>>> dt = np.dtype([('x', np.float32), ('y', np.float32)])
>>> create_empty_recarray(1, dt)
rec.array([(0., 0.)],
dtype=[('x', '<f4'), ('y', '<f4')])
"""
r = np.zeros(length, dtype=dtype)
msg = "dtype argument must be an instance of np.dtype, not list."
Expand All @@ -41,6 +43,9 @@ def ra_slice(ra, cols):
"""
Create a slice of a recarray
.. deprecated:: 3.5
Use numpy.lib.recfunctions.repack_fields instead
Parameters
----------
ra : np.recarray
Expand All @@ -55,20 +60,23 @@ def ra_slice(ra, cols):
Examples
--------
>>> import flopy
>>> raslice = flopy.utils.ra_slice(ra, ['x', 'y'])
>>> import numpy as np
>>> from flopy.utils import ra_slice
>>> a = np.core.records.fromrecords([("a", 1, 1.1), ("b", 2, 2.1)])
>>> ra_slice(a, ['f0', 'f1'])
rec.array([('a', 1), ('b', 2)],
dtype=[('f0', '<U1'), ('f1', '<i4')])
"""
raslice = np.column_stack([ra[c] for c in cols])
dtype = [(str(d[0]), str(d[1])) for d in ra.dtype.descr if d[0] in cols]
return np.array([tuple(r) for r in raslice], dtype=dtype).view(np.recarray)
return repack_fields(ra[cols])


def recarray(array, dtype):
"""
Convert a list of lists or tuples to a recarray.
.. deprecated:: 3.5
Use numpy.core.records.fromrecords instead
Parameters
----------
array : list of lists
Expand All @@ -86,10 +94,11 @@ def recarray(array, dtype):
--------
>>> import numpy as np
>>> import flopy
>>> dtype = np.dtype([('x', np.float32), ('y', np.float32)])
>>> arr = [(1., 2.), (10., 20.), (100., 200.)]
>>> ra = flopy.utils.recarray(arr, dtype)
>>> dt = np.dtype([('x', np.float32), ('y', np.float32)])
>>> a = [(1., 2.), (10., 20.), (100., 200.)]
>>> flopy.utils.recarray(a, dt)
rec.array([( 1., 2.), ( 10., 20.), (100., 200.)],
dtype=[('x', '<f4'), ('y', '<f4')])
"""
array = np.atleast_2d(array)

Expand Down

0 comments on commit 941a5f1

Please sign in to comment.