From acadb61f4cc51a8095ece5be472db4bf5f839131 Mon Sep 17 00:00:00 2001 From: Francois Colleoni <110899888+inoelloc@users.noreply.github.com> Date: Fri, 12 Jul 2024 12:11:01 +0200 Subject: [PATCH] Rebase of MJA pull request on main (#241) (#245) --- smash/_constant.py | 5 +++++ smash/core/model/_standardize.py | 18 ++++++++++++------ smash/core/simulation/_standardize.py | 15 ++++++++------- venv_install.sh | 20 +++++--------------- 4 files changed, 30 insertions(+), 28 deletions(-) diff --git a/smash/_constant.py b/smash/_constant.py index a7304ab1..928b067c 100644 --- a/smash/_constant.py +++ b/smash/_constant.py @@ -29,6 +29,11 @@ def get_rr_states_from_structure(structure: str) -> list[str]: return rr_states +### FLOAT PRECISION FOR FLOAT COMPARISON ### +F_PRECISION = 1.0e-5 +############################################ + + ### MODULE ### ############## diff --git a/smash/core/model/_standardize.py b/smash/core/model/_standardize.py index e69ecf17..ef68e75f 100644 --- a/smash/core/model/_standardize.py +++ b/smash/core/model/_standardize.py @@ -10,6 +10,7 @@ from smash._constant import ( DEFAULT_MODEL_SETUP, + F_PRECISION, FEASIBLE_RR_INITIAL_STATES, FEASIBLE_RR_PARAMETERS, FEASIBLE_SERR_MU_PARAMETERS, @@ -32,11 +33,16 @@ from smash.util._typing import AnyTuple, ListLike, Numeric -def _standardize_model_setup_bool(key: str, value: bool) -> bool: - if not isinstance(value, bool): - raise TypeError(f"{key} model setup must be a boolean") +def _standardize_model_setup_bool(key: str, value: bool | int) -> bool: + if isinstance(value, bool): + pass + elif isinstance(value, int): + if value not in (0, 1): + raise ValueError(f"{key} model setup must be equal to 0 or 1") + else: + raise TypeError(f"{key} model setup must be a boolean or integer (0, 1)") - return value + return bool(value) def _standardize_model_setup_directory(read: bool, key: str, value: str | None) -> str: @@ -531,7 +537,7 @@ def _standardize_rr_parameters_value( f"{value.shape} into shape {model.mesh.flwdir.shape}" ) - if low_arr <= low or upp_arr >= upp: + if (low_arr + F_PRECISION) <= low or (upp_arr - F_PRECISION) >= upp: raise ValueError( f"Invalid value for model rr_parameter '{key}'. rr_parameter domain [{low_arr}, {upp_arr}] is " f"not included in the feasible domain ]{low}, {upp}[" @@ -557,7 +563,7 @@ def _standardize_rr_states_value( f"{value.shape} into shape {model.mesh.flwdir.shape}" ) - if low_arr <= low or upp_arr >= upp: + if (low_arr + F_PRECISION) <= low or (upp_arr - F_PRECISION) >= upp: raise ValueError( f"Invalid value for model {state_kind} '{key}'. {state_kind} domain [{low_arr}, {upp_arr}] is " f"not included in the feasible domain ]{low}, {upp}[" diff --git a/smash/core/simulation/_standardize.py b/smash/core/simulation/_standardize.py index 83c57bf1..779efc4b 100644 --- a/smash/core/simulation/_standardize.py +++ b/smash/core/simulation/_standardize.py @@ -15,6 +15,7 @@ DEFAULT_TERMINATION_CRIT, EVENT_SEG_KEYS, F90_OPTIMIZER_CONTROL_TFM, + F_PRECISION, FEASIBLE_RR_INITIAL_STATES, FEASIBLE_RR_PARAMETERS, FEASIBLE_SERR_MU_PARAMETERS, @@ -107,7 +108,7 @@ def _standardize_simulation_samples(model: Model, samples: Samples) -> Samples: arr = getattr(samples, key) low_arr = np.min(arr) upp_arr = np.max(arr) - if low_arr <= low or upp_arr >= upp: + if (low_arr + F_PRECISION) <= low or (upp_arr - F_PRECISION) >= upp: raise ValueError( f"Invalid sample values for parameter '{key}'. Sample domain [{low_arr}, {upp_arr}] is " f"not included in the feasible domain ]{low}, {upp}[" @@ -239,13 +240,13 @@ def _standardize_simulation_optimize_options_bounds( low_arr = np.min(arr) upp_arr = np.max(arr) - if (low_arr + 1e-3) < value[0] or (upp_arr - 1e-3) > value[1]: + if (low_arr + F_PRECISION) < value[0] or (upp_arr - F_PRECISION) > value[1]: raise ValueError( f"Invalid bounds values for parameter '{key}'. Bounds domain [{value[0]}, {value[1]}] does " f"not include parameter domain [{low_arr}, {upp_arr}] in bounds optimize_options" ) - if value[0] <= low or value[1] >= upp: + if (value[0] + F_PRECISION) <= low or (value[1] - F_PRECISION) >= upp: raise ValueError( f"Invalid bounds values for parameter '{key}'. Bounds domain [{value[0]}, {value[1]}] is not " f"included in the feasible domain ]{low}, {upp}[ in bounds optimize_options" @@ -1099,7 +1100,7 @@ def _standardize_simulation_parameters_feasibility(model: Model): low_arr = np.min(arr) upp_arr = np.max(arr) - if low_arr <= low or upp_arr >= upp: + if (low_arr + F_PRECISION) <= low or (upp_arr - F_PRECISION) >= upp: raise ValueError( f"Invalid value for model rr_parameter '{key}'. rr_parameter domain [{low_arr}, {upp_arr}] " f"is not included in the feasible domain ]{low}, {upp}[" @@ -1111,7 +1112,7 @@ def _standardize_simulation_parameters_feasibility(model: Model): low_arr = np.min(arr) upp_arr = np.max(arr) - if low_arr <= low or upp_arr >= upp: + if (low_arr + F_PRECISION) <= low or (upp_arr - F_PRECISION) >= upp: raise ValueError( f"Invalid value for model rr_initial_state '{key}'. rr_initial_state domain " f"[{low_arr}, {upp_arr}] is not included in the feasible domain ]{low}, {upp}[" @@ -1126,7 +1127,7 @@ def _standardize_simulation_parameters_feasibility(model: Model): low_arr = np.min(arr) upp_arr = np.max(arr) - if low_arr <= low or upp_arr >= upp: + if (low_arr + F_PRECISION) <= low or (upp_arr - F_PRECISION) >= upp: raise ValueError( f"Invalid value for model serr_mu_parameter '{key}'. serr_mu_parameter domain " f"[{low_arr}, {upp_arr}] is not included in the feasible domain ]{low}, {upp}[" @@ -1140,7 +1141,7 @@ def _standardize_simulation_parameters_feasibility(model: Model): low_arr = np.min(arr) upp_arr = np.max(arr) - if low_arr <= low or upp_arr >= upp: + if (low_arr + F_PRECISION) <= low or (upp_arr - F_PRECISION) >= upp: raise ValueError( f"Invalid value for model serr_sigma_parameter '{key}'. serr_sigma_parameter domain " f"[{low_arr}, {upp_arr}] is not included in the feasible domain ]{low}, {upp}[" diff --git a/venv_install.sh b/venv_install.sh index df6dda15..a5bf1887 100755 --- a/venv_install.sh +++ b/venv_install.sh @@ -4,7 +4,7 @@ #When running this script, you will be prompted for your administrator password to install the required dependencies. venv_path=~/python_venv -env_name=smash_1.0 +env_name=smash_dev_1.0 #Parse input arguments @@ -51,14 +51,14 @@ fi #Warning to the user if [ $(cat /etc/*release | grep ^NAME | grep -o Ubuntu) == "Ubuntu" ]; then - DEB_PACKAGE_NAME="gfortran build-essential gdal-bin libgdal-dev libshp-dev python3-gdal python3-venv python3-dev" + DEB_PACKAGE_NAME="gfortran build-essential python3-venv" echo "===============================================" echo "Make sure you first installed the following package (ubuntu): $DEB_PACKAGE_NAME" echo "===============================================" else - PACKAGE_NAME="gfortran build-essential gdal-bin libgdal-dev libshp-dev python3-gdal python3-venv python3-dev" + PACKAGE_NAME="gfortran build-essential python3-venv python3-dev" echo "===============================================" echo "Builging smash require some packages. Make sure you install them first. Install the corresponding package depending your distribution. On Debian/Ubuntu these package are: $PACKAGE_NAME" echo "===============================================" @@ -83,10 +83,7 @@ if [ ! -d "${venv_path}/.venv-${env_name}" ] ; then #install python dependencies pip install --upgrade pip - pip install 'numpy>=1.13,<=1.23' - pip install f90wrap - pip install wheel - pip install rasterio pandas h5py tqdm scipy pyyaml terminaltables matplotlib + pip install -r requirements-dev.txt echo '' echo 'Building Smash...' @@ -94,12 +91,6 @@ if [ ! -d "${venv_path}/.venv-${env_name}" ] ; then make - echo '' - echo 'Installing extra-package for building the documentation...' - echo '' - #install extra-package for building the documentation: - pip install sphinx numpydoc pydata-sphinx-theme ipython sphinxcontrib-bibtex sphinx-design sphinx-autosummary-accessors pytest pytest-cov black ruff fprettify - else echo '' echo "The python environnemnt ${venv_path}/${env_name} already exist..." @@ -121,5 +112,4 @@ echo '>Then launch python:' echo '(smash)> python' echo 'Import the SMASH package in your python shell:' echo '>>> import smash' -echo '************************************************' - +echo '************************************************' \ No newline at end of file