-
Notifications
You must be signed in to change notification settings - Fork 9
Issue #1320 Add CopyFiles package #1321
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
Merged
JoerivanEngelen
merged 9 commits into
issue_#1260_from_imod5_data_metaswap
from
issue_#1320_copyfiles
Dec 4, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0606aed
Add CopyFiles package and tests
JoerivanEngelen 5585cbb
Log removed files
JoerivanEngelen fad5851
fix mypy error
JoerivanEngelen 8977eab
Update changelog
JoerivanEngelen 7d5e03f
Make CopyFiles available under imod.msw
JoerivanEngelen 1d211c9
Merge branch 'issue_#1260_from_imod5_data_metaswap' into issue_#1320_…
JoerivanEngelen e38befd
Rename setup function
JoerivanEngelen 404ec6e
Better package name
JoerivanEngelen 8c89944
Clearer varnames
JoerivanEngelen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| from pathlib import Path | ||
| from shutil import copy2 | ||
| from typing import cast | ||
|
|
||
| import numpy as np | ||
| import xarray as xr | ||
|
|
||
| from imod.logging import logger | ||
| from imod.logging.loglevel import LogLevel | ||
| from imod.msw.pkgbase import MetaSwapPackage | ||
| from imod.typing import Imod5DataDict | ||
|
|
||
| _LOG_MESSAGE_TEMPLATE = """\ | ||
| Will not copy files {filtered}, these will be generated by iMOD Python | ||
| instead.""" | ||
|
|
||
|
|
||
| class FileCopier(MetaSwapPackage): | ||
| def __init__(self, paths: list[str]): | ||
| super().__init__() | ||
| paths_da = xr.DataArray( | ||
| paths, coords={"file_nr": np.arange(len(paths))}, dims=("file_nr",) | ||
| ) | ||
| self.dataset["paths"] = paths_da | ||
|
|
||
| @classmethod | ||
| def from_imod5_data(cls, imod5_data: Imod5DataDict): | ||
| paths = cast(list[list[str]], imod5_data["extra"]["paths"]) | ||
| paths_unpacked = {Path(p[0]) for p in paths} | ||
| files_to_filter = ( | ||
| "mete_grid.inp", | ||
| "para_sim.inp", | ||
| "svat2precgrid.inp", | ||
| "svat2etrefgrid.inp", | ||
| ) | ||
| paths_included = [ | ||
| str(p) for p in paths_unpacked if p.name.lower() not in files_to_filter | ||
| ] | ||
| paths_excluded = {str(p) for p in paths_unpacked} - set(paths_included) | ||
| if paths_excluded: | ||
| log_message = _LOG_MESSAGE_TEMPLATE.format(filtered=paths_excluded) | ||
| logger.log( | ||
| loglevel=LogLevel.INFO, | ||
| message=log_message, | ||
| ) | ||
| return cls(paths_included) | ||
|
|
||
| def write(self, directory: str | Path, *_): | ||
| directory = Path(directory) | ||
|
|
||
| src_paths = [Path(p) for p in self.dataset["paths"].to_numpy()] | ||
| dst_paths = [directory / p.name for p in src_paths] | ||
|
|
||
| for src_path, dst_path in zip(src_paths, dst_paths): | ||
| copy2(src_path, dst_path) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| from pytest_cases import parametrize_with_cases | ||
|
|
||
| from imod.msw.copy_files import FileCopier | ||
|
|
||
|
|
||
| def write_test_files(directory, filenames): | ||
| paths = [directory / filename for filename in filenames] | ||
| for p in paths: | ||
| with open(p, mode="w") as f: | ||
| f.write("test") | ||
| return paths | ||
|
|
||
|
|
||
| def case_simple_files(tmp_path_factory): | ||
| directory = tmp_path_factory.mktemp("simple_files") | ||
| filenames = [ | ||
| "a.inp", | ||
| "b.inp", | ||
| "c.inp", | ||
| ] | ||
| return write_test_files(directory, filenames) | ||
|
|
||
|
|
||
| def case_imod5_extra_files(tmp_path_factory): | ||
| directory = tmp_path_factory.mktemp("imod5_extra_files") | ||
| filenames = [ | ||
| "a.inp", | ||
| "b.inp", | ||
| "c.inp", | ||
| "mete_grid.inp", | ||
| "para_sim.inp", | ||
| "svat2precgrid.inp", | ||
| "svat2etrefgrid.inp", | ||
| ] | ||
| return write_test_files(directory, filenames) | ||
|
|
||
|
|
||
| @parametrize_with_cases("src_files", cases=".") | ||
| def test_copyfile_init(src_files): | ||
| # Act | ||
| copyfiles = FileCopier(src_files) | ||
| # Arrange | ||
| assert "paths" in copyfiles.dataset.keys() | ||
| assert len(copyfiles.dataset["paths"]) == len(src_files) | ||
|
|
||
|
|
||
| @parametrize_with_cases("src_files", cases=".") | ||
| def test_copyfile_write(src_files, tmp_path): | ||
| # Arrange | ||
| expected_filenames = {f.name for f in src_files} | ||
| # Act | ||
| copyfiles = FileCopier(src_files) | ||
| copyfiles.write(tmp_path) | ||
| # Assert | ||
| actual_filepaths = tmp_path.glob("*.inp") | ||
| actual_filenames = {f.name for f in actual_filepaths} | ||
| diff = expected_filenames ^ actual_filenames | ||
| assert len(diff) == 0 | ||
|
|
||
|
|
||
| @parametrize_with_cases("src_files", cases=".") | ||
| def test_from_imod5_data(src_files): | ||
| # Arrange | ||
| imod5_ls = [[p] for p in src_files] | ||
| imod5_data = {"extra": {"paths": imod5_ls}} | ||
| # Act | ||
| copyfiles = FileCopier.from_imod5_data(imod5_data) | ||
| # Assert | ||
| len(copyfiles.dataset["paths"]) == 3 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you using curly brackets and not block brackets? I thought curly brackets are only for dictionaries, but this seems to be a list.
The same thing occurs on line 39
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a Python builtin type
set. Sets are very useful to check values in iterables for uniqueness. And if one set contains values not present, or present, in another set. For docs, see: https://docs.python.org/3/library/stdtypes.html#setI wanted to make this more explicit by doing
set([Path(p[0]) for p in paths]), but Ruff disagreed with me. I guess the Ruff devs think the set is common knowledge as it is a Python builtin (though a bit more niche).