Skip to content

Commit

Permalink
add tutorial and ruff (SMTorg#639)
Browse files Browse the repository at this point in the history
* add tutorial and ruff

* change notebook title, enforce ruff on notebooks

* format SMT 2.0 Notebook
  • Loading branch information
Paul-Saves authored Aug 22, 2024
1 parent 0c42843 commit 5f7ad79
Show file tree
Hide file tree
Showing 16 changed files with 105,811 additions and 69,441 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ requires = ["setuptools", "wheel", "numpy", "Cython"]
[tool.ruff.lint]
select = ["E", "F", "W"]
ignore = []
exclude = ["*.ipynb"]
exclude = []

[tool.ruff.lint.pycodestyle]
max-line-length = 120
Expand Down
467 changes: 238 additions & 229 deletions tutorial/NotebookRunTestCases_Paper_SMT_v2.ipynb

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions tutorial/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ These tutorials introduce to use the opensource Surrogate Modeling Toolbox where

[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/SMTorg/smt/blob/master/tutorial/SMT_Tutorial.ipynb)

## Efficient Global Optimization: How to start?

[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/SMTorg/smt/blob/master/tutorial/SMT_SBO_EGO_Educational.ipynb)

## Noisy Gaussian process

[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/SMTorg/smt/blob/master/tutorial/SMT_Noise.ipynb)
Expand Down
123 changes: 74 additions & 49 deletions tutorial/SMT_DesignSpace_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,26 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: smt in d:\\bartoli\\anaconda3\\lib\\site-packages (2.5.0)\n",
"Requirement already satisfied: scikit-learn in d:\\bartoli\\anaconda3\\lib\\site-packages (from smt) (1.3.0)\n",
"Requirement already satisfied: pyDOE3 in d:\\bartoli\\anaconda3\\lib\\site-packages (from smt) (1.0.1)\n",
"Requirement already satisfied: scipy in d:\\bartoli\\anaconda3\\lib\\site-packages (from smt) (1.11.1)\n",
"Requirement already satisfied: jenn in d:\\bartoli\\anaconda3\\lib\\site-packages (from smt) (1.0.3)\n",
"Requirement already satisfied: orjson>=3.9 in d:\\bartoli\\anaconda3\\lib\\site-packages (from jenn->smt) (3.9.15)\n",
"Requirement already satisfied: numpy>=1.22 in d:\\bartoli\\anaconda3\\lib\\site-packages (from jenn->smt) (1.24.3)\n",
"Requirement already satisfied: joblib>=1.1.1 in d:\\bartoli\\anaconda3\\lib\\site-packages (from scikit-learn->smt) (1.2.0)\n",
"Requirement already satisfied: threadpoolctl>=2.0.0 in d:\\bartoli\\anaconda3\\lib\\site-packages (from scikit-learn->smt) (2.2.0)\n"
"Requirement already satisfied: smt in /stck/psaves/miniconda3/envs/newenv1/lib/python3.9/site-packages (2.6.2)\n",
"Requirement already satisfied: scikit-learn in /stck/psaves/miniconda3/envs/newenv1/lib/python3.9/site-packages (from smt) (1.4.0)\n",
"Requirement already satisfied: pyDOE3 in /stck/psaves/miniconda3/envs/newenv1/lib/python3.9/site-packages (from smt) (1.0.3)\n",
"Requirement already satisfied: scipy in /stck/psaves/miniconda3/envs/newenv1/lib/python3.9/site-packages (from smt) (1.11.3)\n",
"Requirement already satisfied: jenn in /stck/psaves/miniconda3/envs/newenv1/lib/python3.9/site-packages (from smt) (1.0.6)\n",
"Requirement already satisfied: jsonpointer>=2.4 in /stck/psaves/miniconda3/envs/newenv1/lib/python3.9/site-packages (from jenn->smt) (3.0.0)\n",
"Requirement already satisfied: jsonschema>=4.22 in /stck/psaves/miniconda3/envs/newenv1/lib/python3.9/site-packages (from jenn->smt) (4.22.0)\n",
"Requirement already satisfied: orjson>=3.9 in /stck/psaves/miniconda3/envs/newenv1/lib/python3.9/site-packages (from jenn->smt) (3.10.6)\n",
"Requirement already satisfied: numpy>=1.22 in /stck/psaves/miniconda3/envs/newenv1/lib/python3.9/site-packages (from jenn->smt) (1.23.5)\n",
"Requirement already satisfied: joblib>=1.2.0 in /stck/psaves/miniconda3/envs/newenv1/lib/python3.9/site-packages (from scikit-learn->smt) (1.4.2)\n",
"Requirement already satisfied: threadpoolctl>=2.0.0 in /stck/psaves/miniconda3/envs/newenv1/lib/python3.9/site-packages (from scikit-learn->smt) (3.5.0)\n",
"Requirement already satisfied: attrs>=22.2.0 in /stck/psaves/miniconda3/envs/newenv1/lib/python3.9/site-packages (from jsonschema>=4.22->jenn->smt) (23.1.0)\n",
"Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /stck/psaves/miniconda3/envs/newenv1/lib/python3.9/site-packages (from jsonschema>=4.22->jenn->smt) (2023.12.1)\n",
"Requirement already satisfied: referencing>=0.28.4 in /stck/psaves/miniconda3/envs/newenv1/lib/python3.9/site-packages (from jsonschema>=4.22->jenn->smt) (0.35.1)\n",
"Requirement already satisfied: rpds-py>=0.7.1 in /stck/psaves/miniconda3/envs/newenv1/lib/python3.9/site-packages (from jsonschema>=4.22->jenn->smt) (0.18.1)\n"
]
}
],
"source": [
"#to install smt\n",
"# to install smt\n",
"!pip install smt"
]
},
Expand Down Expand Up @@ -122,8 +128,13 @@
"metadata": {},
"outputs": [],
"source": [
"from smt.utils.design_space import DesignSpace, FloatVariable, IntegerVariable, OrdinalVariable, CategoricalVariable\n",
"import numpy as np"
"from smt.utils.design_space import (\n",
" DesignSpace,\n",
" FloatVariable,\n",
" IntegerVariable,\n",
" OrdinalVariable,\n",
" CategoricalVariable,\n",
")"
]
},
{
Expand Down Expand Up @@ -165,18 +176,24 @@
}
],
"source": [
"#Instantiate the design space with all its design variables:\n",
"# Instantiate the design space with all its design variables:\n",
"\n",
"ds = DesignSpace([\n",
" CategoricalVariable(['A', 'B']), # x0 categorical: A or B; order is not relevant\n",
" OrdinalVariable(['C', 'D', 'E']), # x1 ordinal: C, D or E; order is relevant\n",
" IntegerVariable(0, 2), # x2 integer between 0 and 2 (inclusive): 0, 1, 2\n",
" FloatVariable(0, 1), # c3 continuous between 0 and 1\n",
" ])\n",
"ds = DesignSpace(\n",
" [\n",
" CategoricalVariable(\n",
" [\"A\", \"B\"]\n",
" ), # x0 categorical: A or B; order is not relevant\n",
" OrdinalVariable([\"C\", \"D\", \"E\"]), # x1 ordinal: C, D or E; order is relevant\n",
" IntegerVariable(0, 2), # x2 integer between 0 and 2 (inclusive): 0, 1, 2\n",
" FloatVariable(0, 1), # c3 continuous between 0 and 1\n",
" ]\n",
")\n",
"\n",
"print(\"Number of design variables\",len(ds.design_variables))\n",
"#You can define decreed variables (conditional activation):\n",
"ds.declare_decreed_var(decreed_var=1, meta_var=0, meta_value='A') # Activate x1 if x0 == A"
"print(\"Number of design variables\", len(ds.design_variables))\n",
"# You can define decreed variables (conditional activation):\n",
"ds.declare_decreed_var(\n",
" decreed_var=1, meta_var=0, meta_value=\"A\"\n",
") # Activate x1 if x0 == A"
]
},
{
Expand All @@ -189,24 +206,24 @@
"output_type": "stream",
"text": [
"Data encoded: \n",
" [[1. 0. 0. 0.52853667]\n",
" [1. 0. 1. 0.09673616]\n",
" [0. 0. 1. 0.33682711]\n",
" [0. 1. 1. 0.91199549]\n",
" [1. 0. 2. 0.67001367]]\n",
" [[0. 2. 1. 0.50141714]\n",
" [1. 0. 1. 0.48517298]\n",
" [0. 1. 2. 0.83539973]\n",
" [0. 0. 1. 0.65900747]\n",
" [0. 2. 1. 0.5664969 ]]\n",
"Data in initial space: \n",
" [['B', 'C', 0.0, 0.5285366741197288], ['B', 'C', 1.0, 0.09673615686563666], ['A', 'C', 1.0, 0.33682711198922366], ['A', 'D', 1.0, 0.9119954884545415], ['B', 'C', 2.0, 0.6700136703569705]]\n"
" [['A', 'E', 1.0, 0.5014171417556681], ['B', 'C', 1.0, 0.48517297677504534], ['A', 'D', 2.0, 0.835399731580032], ['A', 'C', 1.0, 0.6590074713239295], ['A', 'E', 1.0, 0.5664968989411066]]\n"
]
}
],
"source": [
"## To give some examples\n",
"#It is also possible to randomly sample design vectors conforming to the constraints:\n",
"# It is also possible to randomly sample design vectors conforming to the constraints:\n",
"n = 5\n",
"x_sampled, is_acting_sampled = ds.sample_valid_x(5)\n",
"\n",
"print('Data encoded: \\n', x_sampled)\n",
"print('Data in initial space: \\n', ds.decode_values(x_sampled))"
"print(\"Data encoded: \\n\", x_sampled)\n",
"print(\"Data in initial space: \\n\", ds.decode_values(x_sampled))"
]
},
{
Expand All @@ -219,18 +236,18 @@
"output_type": "stream",
"text": [
"Which variables are active \n",
" [[ True False True True]\n",
" [[ True True True True]\n",
" [ True False True True]\n",
" [ True True True True]\n",
" [ True True True True]\n",
" [ True False True True]]\n"
" [ True True True True]]\n"
]
}
],
"source": [
"#After defining everything correctly, you can then use the design space object \n",
"#to correct design vectors and get information about which design variables are acting:\n",
"x_corr, is_acting = ds.correct_get_acting(x_sampled) \n",
"# After defining everything correctly, you can then use the design space object\n",
"# to correct design vectors and get information about which design variables are acting:\n",
"x_corr, is_acting = ds.correct_get_acting(x_sampled)\n",
"print(\"Which variables are active \\n\", is_acting)"
]
},
Expand Down Expand Up @@ -260,13 +277,16 @@
}
],
"source": [
"#If needed, it is possible to get the legacy design space definition format:\n",
"# If needed, it is possible to get the legacy design space definition format:\n",
"xlimits = ds.get_x_limits()\n",
"cont_bounds = ds.get_num_bounds()\n",
"unfolded_cont_bounds = ds.get_unfolded_num_bounds()\n",
"print(\"Limits of each variable \\n\", xlimits)\n",
"print(\"Continuous bounds with the encoding done (4 variables now) \\n\", cont_bounds)\n",
"print(\"Continuous bounds with the unfolded encoding done (5 variables now)\\n\", unfolded_cont_bounds)"
"print(\n",
" \"Continuous bounds with the unfolded encoding done (5 variables now)\\n\",\n",
" unfolded_cont_bounds,\n",
")"
]
},
{
Expand All @@ -290,9 +310,14 @@
}
],
"source": [
"#You can also instantiate a purely-continuous design space from bounds directly:\n",
"continuous_design_space = DesignSpace([(0, 1), (0, 2), (.5, 5.5)])\n",
"print(\"Number of design variables =\",continuous_design_space.n_dv, ' or ', len(continuous_design_space.design_variables))"
"# You can also instantiate a purely-continuous design space from bounds directly:\n",
"continuous_design_space = DesignSpace([(0, 1), (0, 2), (0.5, 5.5)])\n",
"print(\n",
" \"Number of design variables =\",\n",
" continuous_design_space.n_dv,\n",
" \" or \",\n",
" len(continuous_design_space.design_variables),\n",
")"
]
},
{
Expand All @@ -314,11 +339,11 @@
"output_type": "stream",
"text": [
"Data encoded: \n",
" [[0.76145156 1.55158873 0.70950859]\n",
" [0.15453887 0.5979029 5.24003766]\n",
" [0.98290394 0.82219187 3.92612692]\n",
" [0.40976592 0.09755228 2.0798789 ]\n",
" [0.31427305 1.74529453 3.30571286]]\n",
" [[0.31711655 0.1616406 2.58306429]\n",
" [0.49960756 0.62928794 2.96869517]\n",
" [0.11611917 0.63034393 2.74785997]\n",
" [0.44327097 0.3821734 3.78969088]\n",
" [0.32158259 1.88724976 1.06429242]]\n",
"Is_acting: \n",
" [[ True True True]\n",
" [ True True True]\n",
Expand All @@ -329,8 +354,8 @@
}
],
"source": [
"print('Data encoded: \\n', x_sampled_cont)\n",
"print('Is_acting: \\n', is_acting_sampled_cont)"
"print(\"Data encoded: \\n\", x_sampled_cont)\n",
"print(\"Is_acting: \\n\", is_acting_sampled_cont)"
]
},
{
Expand Down Expand Up @@ -362,7 +387,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.9.18"
}
},
"nbformat": 4,
Expand Down
46,676 changes: 22,682 additions & 23,994 deletions tutorial/SMT_EGO_application.ipynb

Large diffs are not rendered by default.

244 changes: 156 additions & 88 deletions tutorial/SMT_ExpandedLHS.ipynb

Large diffs are not rendered by default.

82,469 changes: 41,245 additions & 41,224 deletions tutorial/SMT_GP_Sampling.ipynb

Large diffs are not rendered by default.

Loading

0 comments on commit 5f7ad79

Please sign in to comment.