Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(get_model_paths): fix model order within scenario #156

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading