Skip to content
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

Updated Pyomo.DoE tutorial notebook. #2889

Merged
merged 14 commits into from
Aug 23, 2023
Merged

Conversation

hglynch
Copy link
Contributor

@hglynch hglynch commented Jun 27, 2023

Fixes # .

Summary/Motivation:

  • Pyomo.DoE tutorial notebooks would benefit from better explanations for users new to model-based design of experiments

Changes proposed in this PR:

  • Added optional code to run on Google Colab (and install solvers via IDAES)
  • Improved explanations
  • Added recommended reading

Legal Acknowledgement

By contributing to this software project, I have read the contribution guide and agree to the following terms and conditions for my contribution:

  1. I agree my contributions are submitted under the BSD license.
  2. I represent I am authorized to make the contributions and grant the license. If my employer has rights to intellectual property that includes these contributions, I represent that I have received permission to make contributions and grant the required license on behalf of that employer.

"import sys\n",
"# If running on Google Colab, install Ipopt via IDAES\n",
"if \"google.colab\" in sys.modules:\n",
" !wget \"https://raw.githubusercontent.com/ndcbe/CBE60499/main/notebooks/helper.py\"\n",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the best place to distribute helper.py? Maybe it should be incorporated into the idaes-pse repo? @adowling2?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm considering updating the notebook to just give the helper code here.

Another option would be to have a separate package released by ideas, say colab-pyomo-helper. This would then make it !pip install colab-pyomo-helper.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just looked at the helper code, and it is sufficiently complex (and not directly tied to DoE) that I would not embed it here. I also think it is way too much overhead to have (yet another) package that has to be released. Plus, there is little / no value to being able to install previous versions of it.

It seems pretty simple to put it in idaes/scripts and download it here. That has the side benefit that everyone can reference the same script, so as things evolve, we can keep just one script up-to-date.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hglynch Change code to as follows:

if "google.colab" in sys.modules:
    !wget "https://raw.githubusercontent.com/adowling2/idaes-pse/tree/colab-install-script/scripts/colab_helper.py"
    import colab_helper
    colab_helper.install_idaes()
    colab_helper.install_ipopt()

Does the IDAES and Ipopt install still work on Colab?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hglynch you should change the intro code to:

if "google.colab" in sys.modules:
    !wget "https://raw.githubusercontent.com/idaes-pse/main/scripts"
    import colab_helper
    colab_helper.install_idaes()
    colab_helper.install_ipopt()

@adowling2
Copy link
Member

I'll add it there. Thank you in advance for reviewing ;)

@hglynch
Copy link
Contributor Author

hglynch commented Aug 6, 2023

@adowling2 completed updates for Pyomo.DoE tutorial, ready for review

@hglynch hglynch requested a review from adowling2 August 6, 2023 05:13
@blnicho
Copy link
Member

blnicho commented Aug 8, 2023

@hglynch @adowling2 the August Pyomo release is currently scheduled for August 23rd. Is this PR something you want included in the release?

Copy link
Member

@adowling2 adowling2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hglynch Here are some minor edits. Once these are done, retag myself and John.

pyomo/contrib/doe/examples/fim_doe_tutorial.ipynb Outdated Show resolved Hide resolved
pyomo/contrib/doe/examples/fim_doe_tutorial.ipynb Outdated Show resolved Hide resolved
pyomo/contrib/doe/examples/fim_doe_tutorial.ipynb Outdated Show resolved Hide resolved
pyomo/contrib/doe/examples/fim_doe_tutorial.ipynb Outdated Show resolved Hide resolved
pyomo/contrib/doe/examples/fim_doe_tutorial.ipynb Outdated Show resolved Hide resolved
pyomo/contrib/doe/examples/fim_doe_tutorial.ipynb Outdated Show resolved Hide resolved
pyomo/contrib/doe/examples/fim_doe_tutorial.ipynb Outdated Show resolved Hide resolved
pyomo/contrib/doe/examples/fim_doe_tutorial.ipynb Outdated Show resolved Hide resolved
pyomo/contrib/doe/examples/fim_doe_tutorial.ipynb Outdated Show resolved Hide resolved
@adowling2
Copy link
Member

@blnicho Thank you for the nudge. Yes, I'd like to include this in the release. These changes make the Pyomo.DoE tutorial more accessible. @hglynch, Can you remove the [WIP] from the name of this PR after making the edits I requested? That tells the Pyomo team this is ready to review.

@mrmundt
Copy link
Contributor

mrmundt commented Aug 15, 2023

@hglynch , @adowling2 - Reminder that the target release for Pyomo is next week. If you'd like to get this in before the release, please make edits before this weekend, if possible.

@adowling2
Copy link
Member

Another barrier is getting @jsiirola to review and approve this PR: IDAES/idaes-pse#1237

@adowling2
Copy link
Member

adowling2 commented Aug 18, 2023

@hglynch here are the action items, which should be quick:

  • Address my comments and retest the notebook on Google Colab. We want to check the new installation script that is now available via the IDAES git repo. (See my comment above for specific instructions.)
  • Remove [WIP] from this PR name
  • Request reviews from @jsiirola @blnicho @mrmundt and myself. You can also post a comment here and tag us to make sure we get notified.

@hglynch hglynch changed the title [WIP] Updated Pyomo.DoE tutorial notebook. Updated Pyomo.DoE tutorial notebook. Aug 19, 2023
@hglynch
Copy link
Contributor Author

hglynch commented Aug 19, 2023

@adowling2 @jsiirola @blnicho @mrmundt This PR is ready for final review.

Copy link
Member

@adowling2 adowling2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great.

@adowling2
Copy link
Member

@hglynch One minor change:

!wget "https://raw.githubusercontent.com/idaes-pse/main/scripts"

Should become

!wget "https://raw.githubusercontent.com/idaes-pse/main/scripts/colab_helper.py"

Please also verify this notebook runs on Colab.

@hglynch
Copy link
Contributor Author

hglynch commented Aug 21, 2023

@adowling2 I am receiving an error when I try running it in colab: "no module named colab_helper"

@adowling2
Copy link
Member

@hglynch Did you successfully run this line first?

!wget "https://raw.githubusercontent.com/idaes-pse/main/scripts/colab_helper.py"

That downloads colab_helper.py.

@hglynch
Copy link
Contributor Author

hglynch commented Aug 21, 2023

@adowling2 Yes I ran that first, here is the error:
image

@adowling2
Copy link
Member

Ah, you can see the 404 error in the output. That means the script was not being downloaded correctly. Instead, try:

!wget "https://raw.githubusercontent.com/IDAES/idaes-pse/main/scripts/colab_helper.py"

@hglynch
Copy link
Contributor Author

hglynch commented Aug 21, 2023

@adowling2 It works in colab now, I adjusted it in the jupyter notebook as well.

pyomo/contrib/doe/examples/fim_doe_tutorial.ipynb Outdated Show resolved Hide resolved
pyomo/contrib/doe/examples/fim_doe_tutorial.ipynb Outdated Show resolved Hide resolved
pyomo/contrib/doe/examples/fim_doe_tutorial.ipynb Outdated Show resolved Hide resolved
pyomo/contrib/doe/examples/fim_doe_tutorial.ipynb Outdated Show resolved Hide resolved
pyomo/contrib/doe/examples/fim_doe_tutorial.ipynb Outdated Show resolved Hide resolved
pyomo/contrib/doe/examples/fim_doe_tutorial.ipynb Outdated Show resolved Hide resolved
pyomo/contrib/doe/examples/fim_doe_tutorial.ipynb Outdated Show resolved Hide resolved
pyomo/contrib/doe/examples/fim_doe_tutorial.ipynb Outdated Show resolved Hide resolved
pyomo/contrib/doe/examples/fim_doe_tutorial.ipynb Outdated Show resolved Hide resolved
"cell_type": "markdown",
"metadata": {},
"source": [
"## Key Takeaways\n",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the key takeaways. Can you maybe put them at the top and the bottom? That way I know what I am going to learn upfront and am reminded at the end what you taught me.

@mrmundt
Copy link
Contributor

mrmundt commented Aug 21, 2023

@hglynch - Please check out the Contributing guide for what tools we use for the automatic linting.

@hglynch
Copy link
Contributor Author

hglynch commented Aug 22, 2023

@hglynch - Please check out the Contributing guide for what tools we use for the automatic linting.

@mrmundt I looked through this page and searched the documentation for "lint" and did not find anything. Can you provide a link to an example or give the commands to run for the automatic linting?

@mrmundt
Copy link
Contributor

mrmundt commented Aug 22, 2023

@hglynch - Please check out the Contributing guide for what tools we use for the automatic linting.

@mrmundt I looked through this page and searched the documentation for "lint" and did not find anything. Can you provide a link to an example or give the commands to run for the automatic linting?

See here: https://pyomo.readthedocs.io/en/stable/contribution_guide.html#coding-standards - black and spell-checker

@hglynch
Copy link
Contributor Author

hglynch commented Aug 22, 2023

@hglynch - There are still a few typos that need fixed, according to our automatic checker. Sorry for the struggle!

@mrmundt Thank you! I fixed the typos that I could find, but I am unable to install the spell checker so if you are able to let me know if there are anymore I can fix them!

Copy link
Contributor

@mrmundt mrmundt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for addressing it all! Looks great.

@codecov
Copy link

codecov bot commented Aug 23, 2023

Codecov Report

Patch coverage has no change and project coverage change: -0.01% ⚠️

Comparison is base (c569b78) 87.84% compared to head (40c32d2) 87.83%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2889      +/-   ##
==========================================
- Coverage   87.84%   87.83%   -0.01%     
==========================================
  Files         770      770              
  Lines       89665    89665              
==========================================
- Hits        78764    78756       -8     
- Misses      10901    10909       +8     
Flag Coverage Δ
linux 84.87% <ø> (ø)
osx 74.58% <ø> (ø)
other 85.04% <ø> (ø)
win 82.11% <ø> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

see 2 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines 67 to 143
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Successfully loaded idaes.\n"
]
}
],
"source": [
"# Ipopt installer\n",
"import sys\n",
"\n",
"# If running on Google Colab, install Ipopt via IDAES\n",
"if \"google.colab\" in sys.modules:\n",
" !wget \"https://raw.githubusercontent.com/IDAES/idaes-pse/main/scripts/colab_helper.py\"\n",
" import colab_helper\n",
"\n",
" colab_helper.install_idaes()\n",
" colab_helper.install_ipopt()\n",
"\n",
"# Otherwise, attempt to load IDAES which should include Ipopt and k_aug\n",
"# See https://idaes-pse.readthedocs.io/en/stable/tutorials/getting_started/index.html\n",
"# for instructions on running IDAES get-extensions\n",
"else:\n",
" try:\n",
" import idaes\n",
"\n",
" # Provided IDAES extensions are installed, importing IDAES provides access to\n",
" # Ipopt with HSL and k_aug which are needed for this example\n",
" print(\"Successfully loaded IDAES.\")\n",
" except:\n",
" print(\n",
" \"IDAES is not installed. Make sure you have independently installed Ipopt with HSL and k_aug.\"\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"## check if ipopt available \n",
"ipopt_available = pyo.SolverFactory('ipopt').available()\n",
"# Check if Ipopt is available\n",
"ipopt_available = pyo.SolverFactory(\"ipopt\").available()\n",
"if not (ipopt_available):\n",
" raise RuntimeError('This Pyomo.DoE example requires Ipopt.')"
" raise RuntimeError(\"This Pyomo.DoE example requires Ipopt.\")"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# Imports\n",
"import numpy as np\n",
"import pyomo.environ as pyo\n",
"from pyomo.dae import ContinuousSet, DerivativeVar\n",
"from pyomo.contrib.doe import (\n",
" ModelOptionLib,\n",
" DesignOfExperiments,\n",
" MeasurementVariables,\n",
" DesignVariables,\n",
")"
]
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These first three setup cells are out of order. The output captured in the notebook notes that they were executed in a different order than they appear in order to run successfully. In particular, the second cell checking solver availability uses the pyo namespace which isn't imported until the third cell. Could you fix the ordering and rerun the notebook such that the cells are run in order?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

pyomo/contrib/doe/examples/fim_doe_tutorial.ipynb Outdated Show resolved Hide resolved
pyomo/contrib/doe/examples/fim_doe_tutorial.ipynb Outdated Show resolved Hide resolved
" 22 -1.4309111e+01 8.54e-12 2.66e-08 -8.6 2.66e-04 -4.0 1.00e+00 1.00e+00h 1\n",
"Reallocating memory for MA57: lfact (450838)\n",
"Reallocating memory for MA57: lfact (477335)\n",
" 23 -1.4309111e+01 3.31e-12 5.52e-09 -8.6 1.66e-04 -4.5 1.00e+00 1.00e+00h 1\n",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know why you're now getting regularization at the solution when the previous run of the notebook didn't?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this to the todo list below. The initialization is also not as clear as I would prefer.

"text": [
"INFO: =======Iteration Number: 1 =====\n",
"INFO: elapsed time: 5.7\n",
"INFO: This is the 2 run out of 9 run.\n",
Copy link
Member

@blnicho blnicho Aug 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure where this info logging message is being generated but it looks like there is an off-by-one error in determining the current run number which leads to a confusing message at the end, "10 run out of 9". The grammar of this message could also be improved to "This is run 1 out of 9".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look like your change to the log message was included when you reran the notebook. Are you running this with an installed version of Pyomo instead of the main branch?

pyomo/contrib/doe/examples/fim_doe_tutorial.ipynb Outdated Show resolved Hide resolved
" )\n",
"\n",
" # Gas constant\n",
" mod.R = 8.31446261815324 # J / K / mole\n",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should get into the habit of actually using pyo.units.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree

pyomo/contrib/doe/examples/fim_doe_tutorial.ipynb Outdated Show resolved Hide resolved
Comment on lines +461 to +466
" # Calculating C, Jacobian, FIM\n",
" mod.k1_pert_rule = pyo.Constraint(mod.t, rule=cal_kp1)\n",
" mod.k2_pert_rule = pyo.Constraint(mod.t, rule=cal_kp2)\n",
" mod.dCdt_rule = pyo.Constraint(mod.C_set, mod.t, rule=dCdt_control)\n",
"\n",
" mod.alge_rule = pyo.Constraint(mod.t, rule=alge)\n",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Long term, I think this example would be a lot more powerful if we could separate the kinetic model from the DoE case study. One interesting (future) direction would be to use the examples/dae/ReactionKinetics.py module to define the generic kinetic model.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have conflicting feelings about this. In at least one case, this notebook has helped attract a new Pyomo user, so having the model in the notebook is convenient. But it also duplicates code.

@hglynch
Copy link
Contributor Author

hglynch commented Aug 23, 2023

@blnicho @jsiirola I resolved the comments from above, please let me know if there is anything else that needs to be addressed.

pyomo/contrib/doe/doe.py Outdated Show resolved Hide resolved
Copy link
Member

@blnicho blnicho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hglynch a couple more minor comments and then I think we can merge this

pyomo/contrib/doe/doe.py Outdated Show resolved Hide resolved
"text": [
"INFO: =======Iteration Number: 1 =====\n",
"INFO: elapsed time: 5.7\n",
"INFO: This is the 2 run out of 9 run.\n",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look like your change to the log message was included when you reran the notebook. Are you running this with an installed version of Pyomo instead of the main branch?

pyomo/contrib/doe/examples/fim_doe_tutorial.ipynb Outdated Show resolved Hide resolved
@adowling2
Copy link
Member

Enhancements for the next PR:

  • Why restoration at the end of this simple problem?
  • Use pyomo.units for this example
  • Discuss keeping the model here versus importing
  • Why is the initial point not well initialized?

@hglynch
Copy link
Contributor Author

hglynch commented Aug 23, 2023

@blnicho @jsiirola Comments should all be addressed now, let me know if there are any more.

@jsiirola
Copy link
Member

@hglynch: it looks like you may have forgotten to commit/push the changes to pyomo/contrib/doe/doe.py?

@hglynch
Copy link
Contributor Author

hglynch commented Aug 23, 2023

@hglynch: it looks like you may have forgotten to commit/push the changes to pyomo/contrib/doe/doe.py?

Should be working now, let me know if not!

@blnicho blnicho merged commit 5a56343 into Pyomo:main Aug 23, 2023
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants