SasView / sasmodels

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

When directly fitting a sasmodels model from bumps the best fit theory function is not stored #601

Open pkienzle opened 1 week ago

pkienzle commented 1 week ago

It looks like I didn't implement problem.save() for all sasdata types in the sassmodels bumps wrapper:

https://github.com/SasView/sasmodels/blob/59f5bae160cf928aa80a65b7636432749f413a93/sasmodels/bumps_model.py#L277-L285

That's because the data object for sasmodels is messy:

https://github.com/SasView/sasmodels/blob/59f5bae160cf928aa80a65b7636432749f413a93/sasmodels/direct_model.py#L203-L292

To add simple 1D sans data I think we could rewrite the save as follows (untested):

def save(self, basename):
    # type: (str) -> None
    """
    Save the model parameters and data into a file.

    Not Implemented except for sesans fits.
    """
    if self.data_type == "sesans":
        np.savetxt(basename+".dat", np.array([self._data.x, self.theory()]).T)
    elif self.data_type == "Iq":
        data, index = self._data.data, self._data.index
        x, y, dy, dx = data.x[index], self.theory(), data.dy[index], data.dx[index]
        np.savetxt(basename+".dat", np.array([x, y, dy, dx]).T)

For now you can monkey-patch this into sasmodels at the start of your script:

from sasmodels.bumps_model import Experiment
Experiment.save = save
pkienzle commented 1 week ago

… and extend this for the other data types return by sasdata.

Particularly messy is the adhoc masking I do within the _interpret_data method, which means the Iq returned by the theory calculation doesn't align with the measured q or (qx,qy) values.

Related: the complexity required for save already lives in the sasmodels plotting routines.

We could have a method to set set up the data structures needed for save/plot in one function so that they only need to render the data to a file in save or to a graph in plot.