-
Notifications
You must be signed in to change notification settings - Fork 6
Add tests for layer methods #450
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
Changes from all commits
68872e4
bb197df
b26c26d
ab64d5d
722aeff
6b689a5
569529c
a118ce6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,14 +9,13 @@ | |
| from nlmod.plot import DatasetCrossSection | ||
|
|
||
|
|
||
| def get_regis_horstermeer(): | ||
| def get_regis_horstermeer(cachedir=None, cachename="regis_horstermeer"): | ||
| extent = [131000, 136800, 471500, 475700] | ||
| cachedir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "data") | ||
| if cachedir is None: | ||
| cachedir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "data") | ||
| if not os.path.isdir(cachedir): | ||
| os.makedirs(cachedir) | ||
| regis = nlmod.read.get_regis( | ||
| extent, cachedir=cachedir, cachename="regis_horstermeer" | ||
| ) | ||
| regis = nlmod.read.get_regis(extent, cachedir=cachedir, cachename=cachename) | ||
| return regis | ||
|
|
||
|
|
||
|
|
@@ -159,6 +158,68 @@ def test_set_minimum_layer_thickness(plot=False): | |
| plot_test(regis, ds_new) | ||
|
|
||
|
|
||
| def test_calculate_transmissivity(): | ||
| regis = get_regis_horstermeer() | ||
| nlmod.layers.calculate_transmissivity(regis) | ||
|
|
||
|
|
||
| def test_calculate_resistance(): | ||
| regis = get_regis_horstermeer() | ||
| # with the default value of between_layers=True | ||
| nlmod.layers.calculate_resistance(regis) | ||
| # and also with between_layers=False | ||
| nlmod.layers.calculate_resistance(regis, between_layers=False) | ||
|
|
||
|
|
||
| def test_get_layer_of_z(): | ||
| regis = get_regis_horstermeer() | ||
| z = -100 | ||
|
|
||
| layer = nlmod.layers.get_layer_of_z(regis, z) | ||
|
|
||
| assert (regis["botm"].isel(layer=layer) < z).all() | ||
| top = regis["botm"] + nlmod.layers.calculate_thickness(regis) | ||
| assert (top.isel(layer=layer) > z).all() | ||
|
|
||
|
|
||
| def test_aggregate_by_weighted_mean_to_ds(): | ||
| regis = get_regis_horstermeer() | ||
| regis2 = regis.copy(deep=True) | ||
|
|
||
| # botm needs to have the name "bottom' | ||
| regis2["bottom"] = regis2["botm"] | ||
| # top needs to be 3d | ||
| regis2["top"] = regis2["botm"] + nlmod.layers.calculate_thickness(regis2) | ||
| kh_new = nlmod.layers.aggregate_by_weighted_mean_to_ds(regis, regis2, "kh") | ||
| assert np.abs(kh_new - regis["kh"]).max() < 1e-5 | ||
| # assert (kh_new.isnull() == regis["kh"].isnull()).all() # does not assert to True... | ||
|
|
||
|
|
||
| def test_check_elevations_consistency(caplog): | ||
| regis = get_regis_horstermeer() | ||
| # there are no inconsistencies in this dataset, let's check for that: | ||
| nlmod.layers.check_elevations_consistency(regis) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. perhaps we can add an inconsistent elevation to check method when the layer model isn't correct?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a check. The inconsistencies are reported by the logger, but it turns out you can use pytest to test logger output as well (at least locally on my computer). |
||
| assert len(caplog.text) == 0 | ||
|
|
||
| # add an inconsistency by lowering the top of the model in part of the model domain | ||
| regis["top"][10:20, 20:25] = -5 | ||
| nlmod.layers.check_elevations_consistency(regis) | ||
| assert "check_elevations_consistency" not in caplog.text | ||
| assert len(caplog.text) > 0 | ||
| assert "Thickness of layers is negative in 50 cells" in caplog.text | ||
|
|
||
|
|
||
| def test_get_first_and_last_active_layer(): | ||
| regis = get_regis_horstermeer() | ||
| thickness = nlmod.layers.calculate_thickness(regis) | ||
|
|
||
| fal = nlmod.layers.get_first_active_layer(regis) | ||
| assert (thickness[fal] > 0).all() | ||
|
|
||
| lal = nlmod.layers.get_last_active_layer(regis) | ||
| assert (thickness[lal] > 0).all() | ||
|
|
||
|
|
||
| def test_set_model_top(plot=False): | ||
| regis = get_regis_horstermeer() | ||
| ds_new = nlmod.layers.set_model_top(regis.copy(deep=True), 5.0) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -134,6 +134,48 @@ def test_fillnan_da(): | |
| assert not top[mask].isnull().any() | ||
|
|
||
|
|
||
| def test_interpolate_gdf_to_array(): | ||
| bgt = get_bgt() | ||
| bgt.geometry = bgt.centroid | ||
| bgt["values"] = range(len(bgt)) | ||
|
|
||
| regis = get_regis() | ||
| ds = nlmod.to_model_ds(regis, model_ws=model_ws) | ||
| sim = nlmod.sim.sim(ds) | ||
| gwf = nlmod.gwf.gwf(ds, sim) | ||
| nlmod.gwf.dis(ds, gwf) | ||
|
|
||
| nlmod.grid.interpolate_gdf_to_array(bgt, gwf, field="values", method="linear") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should try to bring some more structure to the interpolation methods? There are quite a few, and I've been working on a new one, but finding them isn't as easy as I'd hoped.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This notebook gives an overview of what is available: https://nlmod.readthedocs.io/en/stable/examples/07_resampling.html
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The interpolation methods are in https://nlmod.readthedocs.io/en/stable/examples/06_gridding_vector_data.html I split the original notebook in a gridding_vector_data-notebook and a resample-notebook a long time ago.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the reminders, I saw the resampling notebook but I wasn't really looking for raster ops, but somehow missed the gridding_vector data notebook. I think the new work should probably get it's own notebook, but it is essentially the opposite direction of the gridding vectors notebook (but only for points). I'll commit it now so you guys can take a look. |
||
|
|
||
|
|
||
| def test_gdf_to_da_methods(): | ||
| bgt = get_bgt() | ||
| regis = get_regis() | ||
| ds = nlmod.to_model_ds(regis) | ||
| bgt["values"] = range(len(bgt)) | ||
|
|
||
| bgt_line = bgt.copy() | ||
| bgt_line.geometry = bgt.boundary | ||
|
|
||
| for agg_method in [ | ||
| "nearest", | ||
| "area_weighted", | ||
| "max_area", | ||
| "length_weighted", | ||
| "max_length", | ||
| # "center_grid", | ||
| "max", | ||
| "min", | ||
| "mean", | ||
| "sum", | ||
| ]: | ||
| if agg_method in ["length_weighted", "max_length"]: | ||
| gdf = bgt_line | ||
| else: | ||
| gdf = bgt | ||
| nlmod.grid.gdf_to_da(gdf, ds, column="values", agg_method=agg_method) | ||
|
|
||
|
|
||
| def test_gdf_to_bool_da(): | ||
| bgt = get_bgt() | ||
|
|
||
|
|
@@ -158,6 +200,30 @@ def test_gdf_to_bool_da(): | |
| assert da.any() | ||
|
|
||
|
|
||
| def test_gdf_to_count_da(): | ||
| bgt = get_bgt() | ||
|
|
||
| # test for a structured grid | ||
| ds = get_structured_model_ds() | ||
| da = nlmod.grid.gdf_to_count_da(bgt, ds) | ||
| assert da.any() | ||
|
|
||
| # test for a vertex grid | ||
| ds = get_vertex_model_ds() | ||
| da = nlmod.grid.gdf_to_count_da(bgt, ds) | ||
| assert da.any() | ||
|
|
||
| # tets for a slightly rotated structured grid | ||
| ds = get_structured_model_ds_rotated() | ||
| da = nlmod.grid.gdf_to_count_da(bgt, ds) | ||
| assert da.any() | ||
|
|
||
| # test for a rotated vertex grid | ||
| ds = get_vertex_model_ds_rotated() | ||
| da = nlmod.grid.gdf_to_count_da(bgt, ds) | ||
| assert da.any() | ||
|
|
||
|
|
||
| def test_gdf_to_da(): | ||
| bgt = get_bgt() | ||
|
|
||
|
|
||
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.
Yeah, this should be fixed in the aggregate method. Let's make it a new issue.
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.
ok, I see you did this. Thanks!