Opened 6 years ago
Last modified 6 years ago
#1218 assigned defect
numpy/scipy cause syntax errors in python model builder
Reported by: | pkienzle | Owned by: | piotr |
---|---|---|---|
Priority: | major | Milestone: | SasView 4.3.0 |
Component: | SasView | Keywords: | |
Cc: | Work Package: | SasView Bug Fixing |
Description
If the code in your simple python model contains numpy, np or scipy then it will cause a syntax error when you try to run the model. This is because the model builder does not include \n when it inserts the numpy import line.
Better would be to place the imports at the top of the template for all models instead of including it dynamically in the function definition. The model reload problems should be fixed, so this should now work. This will make the code simpler, and we are relying on this fix already for the trig and exponential functions.
Instead of from math import *, we should use from sasmodels.special import * at the top of the template. This will define the same set of functions that are available to C models, including functions such as sas_erf, etc, constants such as M_PI and the gaussian quadrature weights for 20, 76 and 150 point quadrature. It also defines the np symbol in case any thing else is needed.
Change History (8)
comment:1 Changed 6 years ago by butler
- Milestone changed from SasView 4.3.0 to SasView 4.2.1
comment:2 Changed 6 years ago by butler
- Owner set to piotr
- Status changed from new to assigned
comment:3 follow-up: ↓ 7 Changed 6 years ago by piotr
It appears I already had "\n" added to relevant lines in the Qt version.
The sasmodels import may be superior but it is hiding information from the user - what are we actually importing? At least with the math module the user knows exactly what the namespace is.
comment:4 Changed 6 years ago by piotr
Added the newline characters. Pull request #200
comment:5 Changed 6 years ago by pkienzle
The namespace is documented in the sasmodels manual.
I suggest renaming the ticket: "Make the opencl special function library available to python models".
Maybe add that the template builder should be moved to sascalc rather than living in the GUI tree, or maybe make that a new ticket.
comment:6 Changed 6 years ago by GitHub <noreply@…>
comment:7 in reply to: ↑ 3 Changed 6 years ago by butler
Could this not be fixed by just adding a comment line before the import statement explaining what is being imported? The user writing a plugin doesn't see any of this anyway so should not matter?
Replying to piotr:
It appears I already had "\n" added to relevant lines in the Qt version.
The sasmodels import may be superior but it is hiding information from the user - what are we actually importing? At least with the math module the user knows exactly what the namespace is.
comment:8 Changed 6 years ago by butler
- Milestone changed from SasView 4.2.1 to SasView 4.3.0
should be trivial enough to fix so would be nice to do.