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

Add a model viewer option to specify the model by variable name in __main__ #3271

Merged
merged 4 commits into from
Jun 14, 2024

Conversation

eslickj
Copy link
Contributor

@eslickj eslickj commented May 23, 2024

Summary/Motivation:

When you are working with the model viewer in Jupyter (Lab, qtconsole...) usually your Pyomo model is stored in main. This isn't necessarily the case, but it is more than likely, and if we assume that is the case we can do a few things to make the viewer easier to work with. This adds an option to specify the model by its variable name in main rather than directly specifying the model by pointer. This is just a new option and doesn't change the default behavior. In jupyter there is a callback that causes the model view to refresh every time you run a command. When you use the new option, the refresh will also check the model id to see if it has changed and load the new model if it has.

This also changes what happens if you open the UI without specifying a model. It looks in the main namespace and pulls out the first pyomo model it finds based on the order of dir(main), which I think is alphabetical. In the qtconsole-based model-viewer, it no longer creates an empty model if it can't find one, it just shows the viewer empty, and you'll have to point it at a model after you create one.

The main motivation for this is that it saves me (and hopefully anyone else that uses the viewer) a lot of time selecting the model in the UI. A lot of times I create a model by running a script. This lets me run the script and open the UI without explicitly selecting the model. Then I often find something need changed in the model which is easier done by updating the script. This lets me rerun the script overwriting the model and having the UI update without having to go back and select the model again. This also avoids confusion where the user may not realize they are looking at an older version of the model.

Changes proposed in this PR:

  • Add option to automatically update the model in the viewer if it is replaced by a new model.
  • Search the main namespace for a model, if one is not specified
  • Don't create a new model when opening the UI in the qtconsole app if one is not specified.

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.

@eslickj eslickj changed the title Add a model viewer option to specify this model by variable name in __main__ Add a model viewer option to specify the model by variable name in __main__ May 28, 2024
Copy link

codecov bot commented May 29, 2024

Codecov Report

Attention: Patch coverage is 41.66667% with 21 lines in your changes missing coverage. Please review.

Project coverage is 88.55%. Comparing base (6fb0c5d) to head (19df8be).
Report is 1033 commits behind head on main.

Files with missing lines Patch % Lines
pyomo/contrib/viewer/ui.py 42.30% 15 Missing ⚠️
pyomo/contrib/viewer/model_select.py 25.00% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3271      +/-   ##
==========================================
+ Coverage   88.51%   88.55%   +0.03%     
==========================================
  Files         865      865              
  Lines       97585    97610      +25     
==========================================
+ Hits        86381    86440      +59     
+ Misses      11204    11170      -34     
Flag Coverage Δ
linux 86.48% <41.66%> (-0.02%) ⬇️
osx ?
other 86.68% <41.66%> (-0.01%) ⬇️
win 83.96% <41.66%> (-0.01%) ⬇️

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

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

@mrmundt mrmundt merged commit 7bbe3a7 into Pyomo:main Jun 14, 2024
30 of 32 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.

4 participants