Make parameters importable#1475
Conversation
Codecov Report
@@ Coverage Diff @@
## develop #1475 +/- ##
===========================================
- Coverage 98.37% 98.30% -0.07%
===========================================
Files 282 295 +13
Lines 16602 16742 +140
===========================================
+ Hits 16332 16459 +127
- Misses 270 283 +13
Continue to review full report at Codecov.
|
111c546 to
f0581c0
Compare
valentinsulzer
left a comment
There was a problem hiding this comment.
Thanks @priyanshuone6 , changes look good. Is there a way to test that saving and loading a sim now works in the case it didn't before?
|
|
||
| # Else, search in the whole PyBaMM directory for matches | ||
| if "PyBaMM" in filename: | ||
| root_path = filename[filename.rfind("PyBaMM") :][7:] |
There was a problem hiding this comment.
Maybe worth adding a comment explaining what this does and why?
Is it because you want to strip the full path to the module from ../PyBaMM/pybamm/input/example.py to pybamm/input/example.py ?
There was a problem hiding this comment.
Yes, I'll add a comment
There was a problem hiding this comment.
So that works if pybamm is imported from a directory named PyBaMM (which is indeed default when cloning from github). But what happens if pybamm is imported from its installed location (e.g. ../site-packages/pybamm)? This would be the case when installing PyBaMM with pip instead of importing it directly from the sources
| # Test load_sim for parameters imports | ||
| pkl_obj = pybamm.load_sim( | ||
| os.path.join( | ||
| "tests", "unit", "test_parameters", "data", "test_load_param.p" |
There was a problem hiding this comment.
You committed the pickle file test_load_param.p but we don't really know what it corresponds to. How about, instead, this tests saves a simulation before trying to load it?
There was a problem hiding this comment.
@tlestang The bug is reproducible only when the pickle file is loaded afresh in a new terminal. If we'll save and then load it then the parameter will already be imported and the error won't occur.
There was a problem hiding this comment.
Ah yes. Alternatively we could write the pickle from another process, something like
save_sim = "import pybamm; model = pybamm.lithium_ion.SPM(); pybamm.Simulation(model).save("test_load_param.p")"
subprocess.run("python -c {save_sim}")
Relying on the binary pickle only may make debugging hard in the future, when nobody remembers what is contains :)
Which makes me think another option is adding a comment that describes how you generated the pickle file.
|
@tlestang Thank you for all the suggestions, I have added the subprocess.run in the test and also tested it with the develop branch and it worked. |
59a2fdf to
06f4ec9
Compare
|
Looks good @priyanshuone6 , nice one :) |
Description
sys.modulesto avoid namespace clashes.__init__.pyin parameters to convert them to modules and make them importable through pybammFixes #960
Type of change
Please add a line in the relevant section of CHANGELOG.md to document the change (include PR #) - note reverse order of PR #s. If necessary, also add to the list of breaking changes.
Key checklist:
$ flake8$ python run-tests.py --unit$ cd docsand then$ make clean; make htmlYou can run all three at once, using
$ python run-tests.py --quick.Further checks: