Skip to content

Commit

Permalink
fix(get_model_paths): fix model order within scenario (MODFLOW-USGS#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
wpbonelli committed May 21, 2024
1 parent 87c8fe6 commit 0e3120a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
8 changes: 4 additions & 4 deletions autotest/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ def get_expected_namefiles(path, pattern="mfsim.nam") -> List[Path]:
def test_get_model_paths_examples():
expected_paths = get_expected_model_dirs(_examples_path)
paths = get_model_paths(_examples_path)
assert paths == sorted(list(set(paths))) # no duplicates
assert sorted(paths) == sorted(list(set(paths))) # no duplicates
assert set(expected_paths) == set(paths)

expected_paths = get_expected_model_dirs(_examples_path, "*.nam")
paths = get_model_paths(_examples_path, namefile="*.nam")
assert paths == sorted(list(set(paths)))
assert sorted(paths) == sorted(list(set(paths)))
assert set(expected_paths) == set(paths)


Expand All @@ -166,12 +166,12 @@ def test_get_model_paths_examples():
def test_get_model_paths_largetestmodels():
expected_paths = get_expected_model_dirs(_examples_path)
paths = get_model_paths(_examples_path)
assert paths == sorted(list(set(paths)))
assert sorted(paths) == sorted(list(set(paths)))
assert set(expected_paths) == set(paths)

expected_paths = get_expected_model_dirs(_examples_path)
paths = get_model_paths(_examples_path)
assert paths == sorted(list(set(paths)))
assert sorted(paths) == sorted(list(set(paths)))
assert set(expected_paths) == set(paths)


Expand Down
42 changes: 35 additions & 7 deletions modflow_devtools/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,41 @@ def get_model_paths(
Find model directories recursively in the given location.
A model directory is any directory containing one or more
namefiles. Model directories can be filtered or excluded,
by prefix, pattern, namefile name, or packages used.
"""

namefile_paths = get_namefile_paths(
path, prefix, namefile, excluded, selected, packages
)
model_paths = sorted(list(set([p.parent for p in namefile_paths if p.parent.name])))
by prefix, pattern, namefile name, or packages used. The
directories are returned in order within scenario folders
such that groundwater flow model workspaces precede other
model types. This allows models which depend on the flow
model's outputs to consume its head or budget, and models
should successfully run in the sequence returned provided
input files (e.g. FMI) refer to output via relative paths.
"""

def keyfunc(v):
v = str(v)
if "gwf" in v:
return 0
else:
return 1

model_paths = []
globbed = path.rglob(f"{prefix if prefix else ''}*")
example_paths = [p for p in globbed if p.is_dir()]
for p in example_paths:
for mp in sorted(
list(
set(
[
p.parent
for p in get_namefile_paths(
p, prefix, namefile, excluded, selected, packages
)
]
)
),
key=keyfunc,
):
if mp not in model_paths:
model_paths.append(mp)
return model_paths


Expand Down

0 comments on commit 0e3120a

Please sign in to comment.