Skip to content

Commit

Permalink
feat(pathlike): support pathlike in user-facing APIs (#1730)
Browse files Browse the repository at this point in the history
- support pathlike or str paths in public APIs
- add tests for supported path arguments
- update docstrings and add type hints
- update some notebooks to use pathlib
- update most autotests to use pathlib
- add verbose flag to more util functions
- add sim_path property to MFSimulation
- expand run_model() tests to cover #1633
- update resolve_exe(), add tests
  • Loading branch information
wpbonelli committed Mar 3, 2023
1 parent cf3a517 commit 656751a
Show file tree
Hide file tree
Showing 102 changed files with 5,610 additions and 5,525 deletions.
464 changes: 227 additions & 237 deletions autotest/regression/test_mf6.py

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions autotest/regression/test_mf6_examples.py
Expand Up @@ -59,7 +59,7 @@ def run_models():

# load simulation
sim = MFSimulation.load(
namfile_name, version="mf6", exe_name="mf6", sim_ws=str(wrkdir)
namfile_name, version="mf6", exe_name="mf6", sim_ws=wrkdir
)
assert isinstance(sim, MFSimulation)

Expand All @@ -68,7 +68,7 @@ def run_models():
assert success

# change to comparison workspace
sim.simulation_data.mfpath.set_sim_path(str(cmpdir))
sim.simulation_data.mfpath.set_sim_path(cmpdir)

# write simulation files and rerun
sim.write_simulation()
Expand All @@ -87,7 +87,7 @@ def run_models():
text="head",
files1=[str(p) for p in headfiles1],
files2=[str(p) for p in headfiles2],
outfile=str(cmpdir / "head_compare.dat"),
outfile=cmpdir / "head_compare.dat",
)

run_models()
14 changes: 7 additions & 7 deletions autotest/regression/test_mfnwt.py
Expand Up @@ -24,7 +24,7 @@ def get_nfnwt_namfiles():
if "WEL" in value.filetype:
wel = True
if lpf and wel:
namfiles.append(str(namfile))
namfiles.append(namfile)
return namfiles


Expand Down Expand Up @@ -107,16 +107,16 @@ def test_run_mfnwt_model(function_tmpdir, namfile):
wel.iunitramp = 2

# change workspace and write MODFLOW-NWT model
m.change_model_ws(str(function_tmpdir))
m.change_model_ws(function_tmpdir)
m.write_input()
success, buff = m.run_model(silent=False)
assert success, "base model run did not terminate successfully"
fn0 = str(function_tmpdir / namfile)
fn0 = function_tmpdir / namfile

# reload the model just written
m = Modflow.load(
namfile,
model_ws=str(function_tmpdir),
model_ws=function_tmpdir,
version="mfnwt",
verbose=True,
check=False,
Expand All @@ -126,17 +126,17 @@ def test_run_mfnwt_model(function_tmpdir, namfile):
assert m.load_fail is False

# change workspace and write MODFLOW-NWT model
pthf = str(function_tmpdir / "flopy")
pthf = function_tmpdir / "flopy"
m.change_model_ws(pthf)
m.write_input()
success, buff = m.run_model(silent=False)
assert success, "base model run did not terminate successfully"
fn1 = os.path.join(pthf, namfile)

fsum = str(function_tmpdir / f"{base_name}.head.out")
fsum = function_tmpdir / f"{base_name}.head.out"
assert compare_heads(fn0, fn1, outfile=fsum), "head comparison failure"

fsum = str(function_tmpdir / f"{base_name}.budget.out")
fsum = function_tmpdir / f"{base_name}.budget.out"
assert compare_budget(
fn0, fn1, max_incpd=0.1, max_cumpd=0.1, outfile=fsum
), "budget comparison failure"
66 changes: 33 additions & 33 deletions autotest/regression/test_modflow.py
Expand Up @@ -26,7 +26,7 @@ def uzf_example_path(example_data_path):
@pytest.mark.regression
def test_uzf_unit_numbers(function_tmpdir, uzf_example_path):
mfnam = "UZFtest2.nam"
ws = str(function_tmpdir / "ws")
ws = function_tmpdir / "ws"
copytree(uzf_example_path, ws)

m = Modflow.load(
Expand Down Expand Up @@ -73,7 +73,7 @@ def test_uzf_unit_numbers(function_tmpdir, uzf_example_path):
fn1 = join(model_ws2, mfnam)

# compare budget terms
fsum = join(str(function_tmpdir), f"{splitext(mfnam)[0]}.budget.out")
fsum = join(function_tmpdir, f"{splitext(mfnam)[0]}.budget.out")
success = compare_budget(
fn0, fn1, max_incpd=0.1, max_cumpd=0.1, outfile=fsum
)
Expand All @@ -85,7 +85,7 @@ def test_uzf_unit_numbers(function_tmpdir, uzf_example_path):
@pytest.mark.regression
def test_unitnums(function_tmpdir, mf2005_test_path):
mfnam = "testsfr2_tab.nam"
ws = str(function_tmpdir / "ws")
ws = function_tmpdir / "ws"
copytree(mf2005_test_path, ws)

m = Modflow.load(mfnam, verbose=True, model_ws=ws, exe_name="mf2005")
Expand Down Expand Up @@ -125,9 +125,9 @@ def test_gage(function_tmpdir, example_data_path):
"""
test043 load and write of MODFLOW-2005 GAGE example problem
"""
pth = str(example_data_path / "mf2005_test")
pth = example_data_path / "mf2005_test"
fpth = join(pth, "testsfr2_tab.nam")
ws = str(function_tmpdir / "ws")
ws = function_tmpdir / "ws"
copytree(pth, ws)

# load the modflow model
Expand Down Expand Up @@ -169,7 +169,7 @@ def test_gage(function_tmpdir, example_data_path):
@pytest.mark.parametrize(
"namfile",
[
str(__example_data_path / "pcgn_test" / nf)
__example_data_path / "pcgn_test" / nf
for nf in ["twri.nam", "MNW2.nam"]
],
)
Expand All @@ -180,7 +180,7 @@ def test_mf2005pcgn(function_tmpdir, namfile):

m = Modflow.load(
nf,
model_ws=str(ws),
model_ws=ws,
verbose=True,
exe_name="mf2005",
)
Expand All @@ -192,22 +192,22 @@ def test_mf2005pcgn(function_tmpdir, namfile):

success, buff = m.run_model(silent=False)
assert success, "base model run did not terminate successfully"
fn0 = str(ws / nf)
fn0 = ws / nf

# rewrite files
ws2 = function_tmpdir / "ws2"
m.change_model_ws(str(ws2))
m.change_model_ws(ws2)
m.write_input()

success, buff = m.run_model(silent=False)
assert success, "new model run did not terminate successfully"
fn1 = str(ws2 / nf)
fn1 = ws2 / nf

fsum = str(function_tmpdir / f"{Path(namfile).stem}.head.out")
fsum = function_tmpdir / f"{Path(namfile).stem}.head.out"
success = compare_heads(fn0, fn1, outfile=fsum, htol=0.005)
assert success, "head comparison failure"

fsum = str(function_tmpdir / f"{Path(namfile).stem}.budget.out")
fsum = function_tmpdir / f"{Path(namfile).stem}.budget.out"
success = compare_budget(
fn0, fn1, max_incpd=0.1, max_cumpd=0.1, outfile=fsum
)
Expand All @@ -218,7 +218,7 @@ def test_mf2005pcgn(function_tmpdir, namfile):
@pytest.mark.slow
@pytest.mark.regression
@pytest.mark.parametrize(
"namfile", [str(__example_data_path / "secp" / nf) for nf in ["secp.nam"]]
"namfile", [__example_data_path / "secp" / nf for nf in ["secp.nam"]]
)
def test_mf2005gmg(function_tmpdir, namfile):
ws = function_tmpdir / "ws"
Expand All @@ -227,29 +227,29 @@ def test_mf2005gmg(function_tmpdir, namfile):

m = Modflow.load(
namfile,
model_ws=str(ws),
model_ws=ws,
verbose=True,
exe_name="mf2005",
)
assert m.load_fail is False

success, buff = m.run_model(silent=False)
assert success, "base model run did not terminate successfully"
fn0 = str(ws / nf)
fn0 = ws / nf

# rewrite files
m.change_model_ws(str(function_tmpdir))
m.change_model_ws(function_tmpdir)
m.write_input()

success, buff = m.run_model(silent=False)
assert success, "new model run did not terminate successfully"
fn1 = str(function_tmpdir / nf)
fn1 = function_tmpdir / nf

fsum = str(function_tmpdir / f"{Path(namfile).stem}.head.out")
fsum = function_tmpdir / f"{Path(namfile).stem}.head.out"
success = compare_heads(fn0, fn1, outfile=fsum)
assert success, "head comparison failure"

fsum = str(function_tmpdir / f"{Path(namfile).stem}.budget.out")
fsum = function_tmpdir / f"{Path(namfile).stem}.budget.out"
success = compare_budget(
fn0, fn1, max_incpd=0.1, max_cumpd=0.1, outfile=fsum
)
Expand All @@ -260,27 +260,27 @@ def test_mf2005gmg(function_tmpdir, namfile):
@pytest.mark.regression
@pytest.mark.parametrize(
"namfile",
[str(__example_data_path / "freyberg" / nf) for nf in ["freyberg.nam"]],
[__example_data_path / "freyberg" / nf for nf in ["freyberg.nam"]],
)
def test_mf2005(function_tmpdir, namfile):
"""
test045 load and write of MODFLOW-2005 GMG example problem
"""
compth = function_tmpdir / "flopy"
ws = function_tmpdir / "ws"
copytree(Path(namfile).parent, str(ws))
copytree(Path(namfile).parent, ws)

m = Modflow.load(
Path(namfile).name,
model_ws=str(ws),
model_ws=ws,
verbose=True,
exe_name="mf2005",
)
assert m.load_fail is False

success, buff = m.run_model(silent=False)
assert success, "base model run did not terminate successfully"
fn0 = str(ws / Path(namfile).name)
fn0 = ws / Path(namfile).name

# change model workspace
m.change_model_ws(compth)
Expand All @@ -304,28 +304,28 @@ def test_mf2005(function_tmpdir, namfile):

success, buff = m.run_model()
assert success, "new model run did not terminate successfully"
fn1 = str(compth / Path(namfile).name)
fn1 = compth / Path(namfile).name

# compare heads
fsum = str(ws / f"{Path(namfile).stem}.head.out")
fsum = ws / f"{Path(namfile).stem}.head.out"
success = compare_heads(fn0, fn1, outfile=fsum)
assert success, "head comparison failure"

# compare heads
fsum = str(ws / f"{Path(namfile).stem}.ddn.out")
fsum = ws / f"{Path(namfile).stem}.ddn.out"
success = compare_heads(fn0, fn1, outfile=fsum, text="drawdown")
assert success, "head comparison failure"

# compare budgets
fsum = str(ws / f"{Path(namfile).stem}.budget.out")
fsum = ws / f"{Path(namfile).stem}.budget.out"
success = compare_budget(
fn0, fn1, max_incpd=0.1, max_cumpd=0.1, outfile=fsum
)
assert success, "budget comparison failure"


mf2005_namfiles = [
str(__example_data_path / "mf2005_test" / nf)
__example_data_path / "mf2005_test" / nf
for nf in [
"fhb.nam",
"l1a2k.nam",
Expand All @@ -341,7 +341,7 @@ def test_mf2005(function_tmpdir, namfile):
@pytest.mark.regression
@pytest.mark.parametrize("namfile", mf2005_namfiles)
def test_mf2005fhb(function_tmpdir, namfile):
ws = str(function_tmpdir / "ws")
ws = function_tmpdir / "ws"
copytree(Path(namfile).parent, ws)

m = Modflow.load(
Expand All @@ -354,12 +354,12 @@ def test_mf2005fhb(function_tmpdir, namfile):
fn0 = join(ws, Path(namfile).name)

# rewrite files
m.change_model_ws(str(function_tmpdir), reset_external=True)
m.change_model_ws(function_tmpdir, reset_external=True)
m.write_input()

success, buff = m.run_model()
assert success, "new model run did not terminate successfully"
fn1 = join(str(function_tmpdir), Path(namfile).name)
fn1 = join(function_tmpdir, Path(namfile).name)

fsum = join(ws, f"{Path(namfile).stem}.head.out")
success = compare_heads(fn0, fn1, outfile=fsum)
Expand All @@ -377,12 +377,12 @@ def test_mf2005fhb(function_tmpdir, namfile):
@pytest.mark.regression
@pytest.mark.parametrize("namfile", mf2005_namfiles)
def test_mf2005_lake(function_tmpdir, namfile, mf2005_test_path):
ws = str(function_tmpdir / "ws")
ws = function_tmpdir / "ws"

copytree(mf2005_test_path, ws)
m = Modflow.load(
Path(namfile).name,
model_ws=str(ws),
model_ws=ws,
verbose=True,
forgive=False,
exe_name="mf2005",
Expand Down
14 changes: 7 additions & 7 deletions autotest/regression/test_str.py
Expand Up @@ -21,11 +21,11 @@ def test_str_fixed_free(function_tmpdir, example_data_path):
m = Modflow.load(
str_items[0]["mfnam"],
exe_name="mf2005",
model_ws=str(mf2005_model_path),
model_ws=mf2005_model_path,
verbose=False,
check=False,
)
m.change_model_ws(str(function_tmpdir))
m.change_model_ws(function_tmpdir)

# get pointer to str package
mstr = m.str
Expand Down Expand Up @@ -81,7 +81,7 @@ def test_str_fixed_free(function_tmpdir, example_data_path):
m2 = Modflow.load(
str_items[0]["mfnam"],
exe_name="mf2005",
model_ws=str(mf2005_model_path),
model_ws=mf2005_model_path,
verbose=False,
check=False,
)
Expand All @@ -95,7 +95,7 @@ def test_str_fixed_free(function_tmpdir, example_data_path):
for p in function_tmpdir.glob("*"):
p.unlink()

m.change_model_ws(str(function_tmpdir))
m.change_model_ws(function_tmpdir)
m.set_ifrefm()
m.write_input()

Expand All @@ -107,7 +107,7 @@ def test_str_fixed_free(function_tmpdir, example_data_path):
m2 = Modflow.load(
str_items[0]["mfnam"],
exe_name="mf2005",
model_ws=str(function_tmpdir),
model_ws=function_tmpdir,
verbose=False,
check=False,
)
Expand All @@ -119,8 +119,8 @@ def test_str_fixed_free(function_tmpdir, example_data_path):
), "could not load the free format model with aux variables"

# compare the fixed and free format head files
fn1 = str(function_tmpdir / "str.nam")
fn2 = str(function_tmpdir / "str.nam")
fn1 = function_tmpdir / "str.nam"
fn2 = function_tmpdir / "str.nam"
assert compare_heads(
fn1, fn2, verbose=True
), "fixed and free format input output head files are different"
2 changes: 1 addition & 1 deletion autotest/regression/test_swi2.py
Expand Up @@ -21,7 +21,7 @@ def swi_path(example_data_path):
)
def test_mf2005swi2(function_tmpdir, swi_path, namfile):
name = namfile.replace(".nam", "")
ws = str(function_tmpdir / "ws")
ws = function_tmpdir / "ws"
shutil.copytree(swi_path, ws)

m = Modflow.load(namfile, model_ws=ws, verbose=True, exe_name="mf2005")
Expand Down

0 comments on commit 656751a

Please sign in to comment.