Skip to content

Commit

Permalink
parameter edits
Browse files Browse the repository at this point in the history
  • Loading branch information
jmccreight committed Aug 11, 2023
1 parent 5dcbc04 commit 384ccda
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 11 deletions.
33 changes: 33 additions & 0 deletions autotest/test_parameters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from pprint import pprint

from numpy.testing import assert_equal

from pywatershed.parameters import Parameters, PrmsParameters
from pywatershed.base.parameters import _set_dict_read_write

from utils import assert_dicts_equal


def test_param_dd_param(domain):
# round trip from read-only to read-write to read-only
# use a PRMS Parameter file for now
domain_dir = domain["param_file"].parent
params = Parameters.from_netcdf(
domain_dir / "parameters_PRMSGroundwater.nc"
)
# These both copy by default
param_dd = params.to_dd()
params2 = Parameters(**param_dd.data)

assert_dicts_equal(params.data, params2.data)

param_dd.data_vars["gwflow_coef"] *= 4
params3 = Parameters(**param_dd.data)

try:
assert_dicts_equal(params.data, params3.data)
assert False
except AssertionError:
pass

return
20 changes: 20 additions & 0 deletions autotest/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from types import MappingProxyType
import numpy as np

print_ans = False
Expand Down Expand Up @@ -41,3 +42,22 @@ def assert_or_print(
# Always fail if printing answers
assert not print_ans
return


def assert_dicts_equal(dic1, dic2):
assert set(dic1.keys()) == set(dic2.keys())

# add cases as needed
for kk, vv in dic1.items():
if isinstance(vv, (dict, MappingProxyType)):
assert_dicts_equal(dic1[kk], dic2[kk])
elif isinstance(vv, np.ndarray):
np.testing.assert_equal(dic1[kk], dic2[kk])
else:
if (
isinstance(vv, float)
and np.isnan(dic1[kk])
and np.isnan(dic2[kk])
):
continue
assert dic1[kk] == dic2[kk]
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ dependencies:
- flake8
- git+https://github.com/modflowpy/flopy.git
- jupyter_black
- modflow-devtools
- modflow-devtools < 1.0.0
- pylint
27 changes: 19 additions & 8 deletions pywatershed/base/parameters.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from copy import deepcopy
from types import MappingProxyType
from typing import Union

Expand Down Expand Up @@ -51,7 +52,16 @@ def __init__(
metadata: dict = None,
encoding: dict = None,
validate: bool = True,
copy: bool = True,
) -> None:
if copy:
dims = deepcopy(dims)
coords = deepcopy(coords)
data_vars = deepcopy(data_vars)
metadata = deepcopy(metadata)
encoding = deepcopy(encoding)
validate = deepcopy(validate)

super().__init__(
dims=dims,
coords=coords,
Expand Down Expand Up @@ -95,17 +105,16 @@ def get_dim_values(
return {kk: self.dims[kk] for kk in keys}

def to_xr_ds(self) -> xr.Dataset:
# must pass as dictionary not a mapping proxy: dict = mp | {}
return dd_to_xr_ds(_set_dict_read_write(self.data))

def to_nc4_ds(self, filename) -> None:
# must pass as dictionary not a mapping proxy: dict = mp | {}
dd_to_nc4_ds(_set_dict_read_write(self.data), filename)
return

def to_dd(self) -> DatasetDict:
# must pass as dictionary not a mapping proxy: dict = mp | {}
return DatasetDict(_set_dict_read_write(self.data))
def to_dd(self, copy=True) -> DatasetDict:
return DatasetDict.from_dict(
_set_dict_read_write(self.data), copy=copy
)

@classmethod
def merge(cls, *param_list, copy=True, del_global_src=True):
Expand All @@ -121,10 +130,12 @@ def merge(cls, *param_list, copy=True, del_global_src=True):
def _set_dict_read_write(mp: MappingProxyType):
dd = mp | {}
for kk, vv in dd.items():
if isinstance(vv, MappingProxyType):
dd[kk] = _set_dict_read_write(dd[kk] | {})
if isinstance(vv, (dict, MappingProxyType)):
dd[kk] = _set_dict_read_write(vv)
elif isinstance(vv, np.ndarray):
vv.flags.writeable = True
# copy is sufficient to make writeable
# https://numpy.org/doc/stable/reference/generated/numpy.copy.html
dd[kk] = dd[kk].copy()

return dd

Expand Down
4 changes: 2 additions & 2 deletions pywatershed/static/metadata/parameters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2079,7 +2079,7 @@ ppt_zero_thresh:
help: The minimum/zero threhold for precipitation
maximum: 0.0
minimum: 0.0
modules:
modules: None
type: F
units: inches
precip_units:
Expand Down Expand Up @@ -3057,7 +3057,7 @@ stream_tave_init:
help: Cold-start stream average stream temperature
maximum: unbounded
minimum: unbounded
modules:
modules: None
type: F
units: unknown
subbasin_down:
Expand Down

0 comments on commit 384ccda

Please sign in to comment.