Opened 5 years ago

Last modified 5 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 5 years ago by butler

  • Milestone changed from SasView 4.3.0 to SasView 4.2.1

should be trivial enough to fix so would be nice to do.

comment:2 Changed 5 years ago by butler

  • Owner set to piotr
  • Status changed from new to assigned

comment:3 follow-up: Changed 5 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 5 years ago by piotr

Added the newline characters.

Version 0, edited 5 years ago by piotr (next)

comment:5 Changed 5 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 5 years ago by GitHub <noreply@…>

In 33d3a74a667474b22585012089a8913dabfa7454/sasview:

Merge pull request #200 from SasView?/ticket-1218

Added newlines to import statements in the plugin model template.

Addresses #1218
need to edit 1218 now

comment:7 in reply to: ↑ 3 Changed 5 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 5 years ago by butler

  • Milestone changed from SasView 4.2.1 to SasView 4.3.0
Note: See TracTickets for help on using tickets.