SasView / sasmodels

Package for calculation of small angle scattering models using OpenCL.
BSD 3-Clause "New" or "Revised" License
16 stars 28 forks source link

Reparameterizing an inline C model fails #605

Open tsole0 opened 4 months ago

tsole0 commented 4 months ago

Partner issue of SasView/sasview#2958 To see how to replicate issue in the SasView GUI, see above issue

Describe the bug Reparameterizing a python model with inline C code will fail with an error in the format:

11:29:37 - root - ERROR - C:\Users\userDir\AppData\Local\miniconda3\envs\sasview_dev\Lib\site-packages\tinycc\tcc.exe -shared -rdynamic -Wall C:\Users\userDir\AppData\Local\Temp\1\sas64_reparameterizedModel_EF781090_2i9v5xc5.c -o C:\Users\userDir\.sasview\compiled_models\sas64_reparameterizedModel_EF781090.so
C:/Users/userDir/.sasview/plugin_models/oldModel.py:35: error: 'reparameterizedVariable' undeclared

on the developer version of SasView, where reparameterizedModel is the reparameterized file, oldModel is the model being reparameterized, and reparameterizedVariable is the name of a reparameterized variable accessed in inline C code within oldModel.

This bug occurs if any inline C code is included in the model, even if a separate C file is defined using source = [] in the main python file. However, models that have defined C code entirely in a separate file are unaffected.

Note: currently, this bug only affects lamellar.py in the 1.0.7 sasmodels version, but will also affect any user-created models with inline C unless resolved

To Reproduce Steps to reproduce the behavior:

  1. Create a new user model.
  2. Copy in the following code:
    
    from numpy import inf

from sasmodels.core import reparameterize from sasmodels.special import *

parameters = [

name, units, default, [min, max], type, description

['new', '', 0, [-inf, inf], '', ''],

]

translation = """ sld = new """

model_info = reparameterize('lamellar', parameters, translation, file)


3. The new model should return an error if a model check is run on it

**Expected behavior**
The model should have no issues being loaded or used with the new redefinitions.

**sasmodels version:**
 - Version: 1.0.7

**Suggested Solution:**
Either deprecate using inline C code (only one model currently uses it and it is easily replaceable), or fix the reparameterization code in sasmodels.core to support inline C code.
pkienzle commented 2 months ago

generate._gen_fn needs to be called on the original parameter table, not the new parameter table defined by core.reparameterize. Replacing call_table with base_table in the following should work.

https://github.com/SasView/sasmodels/blob/ab4e49a1b24b4c80d8cac5b757cb39c2ed75d102/sasmodels/generate.py#L979-L996