Skip to content

Commit

Permalink
validate='nothing' to validate=None (#79)
Browse files Browse the repository at this point in the history
* validate='nothing' -> validate=None

* docstrings tweaks + update release notes
  • Loading branch information
benbovy committed Dec 16, 2019
1 parent 260ed14 commit e5db463
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
6 changes: 3 additions & 3 deletions doc/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ Enhancements
- Added static variables, i.e., variables that don't accept time-varying input
values (:issue:`73`).
- Added support for the validation of variable values (given as inputs and/or
set through foreign variables), reusing :func:`attr.validate` (:issue:`74`).
Validation is optional and is controlled by the parameter ``validate`` added
to :func:`xarray.Dataset.xsimlab.run`.
set through foreign variables), reusing :func:`attr.validate` (:issue:`74`,
:issue:`79`). Validation is optional and is controlled by the parameter
``validate`` added to :func:`xarray.Dataset.xsimlab.run`.
- Check or automatically transpose the dimensions of the variables given in
input xarray Datasets to match those defined in model variables (:issue:`76`).
This is optional and controlled by the parameter ``check_dims`` added
Expand Down
9 changes: 5 additions & 4 deletions xsimlab/drivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@


class ValidateOption(Enum):
NOTHING = 'nothing'
INPUTS = 'inputs'
ALL = 'all'

Expand Down Expand Up @@ -206,7 +205,9 @@ def __init__(self, dataset, model, store, output_store,

self._transposed_vars = {}

self._validate_option = ValidateOption(validate)
if validate is not None:
validate = ValidateOption(validate)
self._validate_option = validate

def _check_missing_model_inputs(self):
"""Check if all model inputs have their corresponding variables
Expand Down Expand Up @@ -394,7 +395,7 @@ def _get_runtime_datasets(self):
def _maybe_validate_inputs(self, input_vars):
p_names = set([v[0] for v in input_vars])

if self._validate_option != ValidateOption.NOTHING:
if self._validate_option is not None:
self.validate(p_names)

def run_model(self):
Expand All @@ -409,7 +410,7 @@ def run_model(self):
"""
ds_init, ds_gby_steps = self._get_runtime_datasets()

validate_all = self._validate_option == ValidateOption.ALL
validate_all = self._validate_option is ValidateOption.ALL

runtime_context = RuntimeContext(
sim_start=ds_init['_sim_start'].values,
Expand Down
6 changes: 3 additions & 3 deletions xsimlab/tests/test_xr_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,10 @@ class P:
def test_run_validate(self, model, in_dataset):
in_dataset['roll__shift'] = 2.5

# no validation -> raises within np.roll()
# no input validation -> raises within np.roll()
with pytest.raises(TypeError,
match=r"slice indices must be integers.*"):
in_dataset.xsimlab.run(model=model, validate='nothing')
in_dataset.xsimlab.run(model=model, validate=None)

# input validation at initialization -> raises within attr.validate()
with pytest.raises(TypeError, match=r".*'int'.*"):
Expand All @@ -361,7 +361,7 @@ def initialize(self):

m = model.update_processes({'set_shift': SetRollShift})

# no validation -> raises within np.roll()
# no internal validation -> raises within np.roll()
with pytest.raises(TypeError,
match=r"slice indices must be integers.*"):
in_dataset.xsimlab.run(model=m, validate='inputs')
Expand Down
8 changes: 4 additions & 4 deletions xsimlab/xr_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ def run(self, model=None, check_dims='strict', validate='inputs',
----------
model : :class:`xsimlab.Model` object, optional
Reference model. If None, tries to get model from context.
check_dims : str, optional
check_dims : {'strict', 'transpose'}, optional
Check the dimension(s) of each input variable given in Dataset.
It may be one of the following options:
Expand All @@ -512,18 +512,18 @@ def run(self, model=None, check_dims='strict', validate='inputs',
model variables
If None is given, no check is performed.
validate : {'nothing', 'inputs', 'all'}, optional
validate : {'inputs', 'all'}, optional
Define what will be validated using the variable's validators
defined in ``model``'s processes (if any). It should be one of the
defined in ``model``'s processes (if any). It may be one of the
following options:
- 'nothing': no validation is performed
- 'inputs': validate only values given as inputs (default)
- 'all': validate both input values and values set through foreign
variables in process classes
The latter may significantly impact performance, but it may be
useful for debugging.
If None is given, no validation is performed.
safe_mode : bool, optional
If True (default), it is safe to run multiple simulations
simultaneously. Generally safe mode shouldn't be disabled, except
Expand Down

0 comments on commit e5db463

Please sign in to comment.