Changeset d32de68 in sasmodels


Ignore:
Timestamp:
Aug 21, 2018 2:43:34 AM (6 years ago)
Author:
Torin Cooper-Bennun <torin.cooper-bennun@…>
Branches:
master, core_shell_microgels, magnetic_model, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
Children:
c11d09f
Parents:
d3ffeb7
Message:

put intermediate results into helper functions; tidy up; retain sasview 4.3 support in returning P(Q), S(Q)

Location:
sasmodels
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/product.py

    rd3ffeb7 rd32de68  
    1313from __future__ import print_function, division 
    1414 
     15from collections import OrderedDict 
     16 
    1517from copy import copy 
    1618import numpy as np  # type: ignore 
     
    2224# pylint: disable=unused-import 
    2325try: 
    24     from typing import Tuple 
     26    from typing import Tuple, Callable 
    2527except ImportError: 
    2628    pass 
     
    145147    return par 
    146148 
     149def _intermediates(P, S): 
     150    # type: (np.ndarray, np.ndarray) -> OrderedDict[str, np.ndarray] 
     151    """ 
     152    Returns intermediate results for standard product (P(Q)*S(Q)) 
     153    """ 
     154    return OrderedDict(( 
     155        ("P(Q)", P), 
     156        ("S(Q)", S), 
     157    )) 
     158 
     159def _intermediates_beta(F1, F2, S, scale, bg): 
     160    # type: (np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray) -> OrderedDict[str, np.ndarray] 
     161    """ 
     162    Returns intermediate results for beta approximation-enabled product 
     163    """ 
     164    # TODO: 1. include calculated Q vector 
     165    # TODO: 2. consider implications if there are intermediate results in P(Q) 
     166    return OrderedDict(( 
     167        ("P(Q)", scale*F2), 
     168        ("S(Q)", S), 
     169        ("beta(Q)", F1**2 / F2), 
     170        ("S_eff(Q)", 1 + (F1**2 / F2)*(S-1)), 
     171        # ("I(Q)", scale*(F2 + (F1**2)*(S-1)) + bg), 
     172    )) 
     173 
    147174class ProductModel(KernelModel): 
    148175    def __init__(self, model_info, P, S): 
     
    270297            F1, F2, volume_avg = self.p_kernel.beta(p_details, p_values, cutoff, magnetic) 
    271298            combined_scale = scale*volfrac/volume_avg 
    272             # Define lazy results based on intermediate values. 
    273             # The return value for the calculation should be an ordered 
    274             # dictionary containing any result the user might want to see 
    275             # at the end of the calculation, including scalars, strings, 
    276             # and plottable data.  Don't want to build this structure during 
    277             # fits, only when displaying the final result (or a one-off 
    278             # computation which asks for it). 
    279             # Do not use the current hack of storing the intermediate values 
    280             # in self.results since that leads to awkward threading issues. 
    281             # Instead return the function along with the bundle of inputs. 
    282             # P and Q may themselves have intermediate results they want to 
    283             # include, such as A and B if P = A + B.  Might use this mechanism 
    284             # to return the computed effective radius as well. 
    285             #def lazy_results(Q, S, F1, F2, scale): 
    286             #    """ 
    287             #    beta = F1**2 / F2  # what about divide by zero errors? 
    288             #    return { 
    289             #        'P' : Data1D(Q, scale*F2), 
    290             #        'beta': Data1D(Q, beta), 
    291             #        'S' : Data1D(Q, S), 
    292             #        'Seff': Data1D(Q, 1 + beta*(S-1)), 
    293             #        'I' : Data1D(Q, scale*(F2 + (F1**2)*(S-1)) + background), 
    294             #    } 
    295             #lazy_pars = s_result, F1, F2, combined_scale 
    296  
    297             # self.results = [F2*volfrac/volume_avg, s_result] 
     299 
     300            self.results = lambda: _intermediates_beta(F1, F2, s_result, volfrac/volume_avg, background) 
    298301            final_result = combined_scale*(F2 + (F1**2)*(s_result - 1)) + background 
    299  
    300             # TODO: 1. include calculated Q vector 
    301             # TODO: 2. consider implications if there are intermediate results in P(Q) 
    302             # def lazy(F1, F2, S, scale, bg): 
    303             def lazy(F1, F2, S, scale): 
    304                 return { 
    305                     "P(Q)": lambda: scale*F2, 
    306                     "S(Q)": lambda: S, 
    307                     "beta(Q)": lambda: F1**2 / F2, 
    308                     "S_eff(Q)": lambda: 1 + (F1**2 / F2)*(S-1), 
    309                     # "I(Q)":  lambda: scale*(F2 + (F1**2)*(S-1)) + bg, 
    310                 } 
    311  
    312             self.results = lazy(F1, F2, s_result, volfrac/volume_avg) 
    313302 
    314303        else: 
    315304            p_result = self.p_kernel.Iq(p_details, p_values, cutoff, magnetic) 
    316             # remember the parts for plotting later 
    317             # self.results = [p_result, s_result] 
     305 
     306            self.results = lambda: _intermediates(p_result, s_result) 
    318307            final_result = scale*(p_result*s_result) + background 
    319  
    320             def lazy(P, S): 
    321                 return { 
    322                     "P(Q)": lambda: P, 
    323                     "S(Q)": lambda: S, 
    324                 } 
    325  
    326             self.results = lazy(p_result, s_result) 
    327308 
    328309        #call_details.show(values) 
  • sasmodels/sasview_model.py

    rd3ffeb7 rd32de68  
    616616        # so that it returns a results object containing all the bits: 
    617617        #     the A, B, C, ... of the composition model (and any subcomponents?) 
    618         #     the P and S of the product model, 
     618        #     the P and S of the product model 
    619619        #     the combined model before resolution smearing, 
    620620        #     the sasmodel before sesans conversion, 
     
    638638            with calculation_lock: 
    639639                self._calculate_Iq(qx) 
    640                 return self._intermediate_results 
     640                # for compatibility with sasview 4.3 
     641                results = self._intermediate_results() 
     642                return results["P(Q)"], results["S(Q)"] 
    641643        else: 
    642644            return None 
Note: See TracChangeset for help on using the changeset viewer.