Changes in / [6e7ba14:aa44a6a] in sasmodels


Ignore:
Location:
sasmodels
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/product.py

    r6e7ba14 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, Union 
    2527except ImportError: 
    2628    pass 
     
    4042    pars = [] 
    4143    if p_info.have_Fq: 
    42         par = Parameter("structure_factor_mode", "", 0, [["P*S","P*(1+beta*(S-1))"]], "", 
     44        par = Parameter("structure_factor_mode", ["P*S","P*(1+beta*(S-1))"], 0, None, "", 
    4345                        "Structure factor calculation") 
    4446        pars.append(par) 
    4547    if p_info.effective_radius_type is not None: 
    46         par = Parameter("radius_effective_mode", "", 0, 
    47                         [["unconstrained"] + p_info.effective_radius_type], 
    48                         "", "Effective radius calculation") 
     48        par = Parameter("radius_effective_mode", ["unconstrained"] + p_info.effective_radius_type, 0, None, "", 
     49                        "Effective radius calculation") 
    4950        pars.append(par) 
    5051    return pars 
     
    157158    return par 
    158159 
     160def _intermediates(P, S, effective_radius): 
     161    # type: (np.ndarray, np.ndarray, float) -> OrderedDict[str, np.ndarray] 
     162    """ 
     163    Returns intermediate results for standard product (P(Q)*S(Q)) 
     164    """ 
     165    return OrderedDict(( 
     166        ("P(Q)", P), 
     167        ("S(Q)", S), 
     168        ("effective_radius", effective_radius), 
     169    )) 
     170 
     171def _intermediates_beta(F1, F2, S, scale, bg, effective_radius): 
     172    # type: (np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, float) -> OrderedDict[str, Union[np.ndarray, float]] 
     173    """ 
     174    Returns intermediate results for beta approximation-enabled product. The result may be an array or a float. 
     175    """ 
     176    # TODO: 1. include calculated Q vector 
     177    # TODO: 2. consider implications if there are intermediate results in P(Q) 
     178    return OrderedDict(( 
     179        ("P(Q)", scale*F2), 
     180        ("S(Q)", S), 
     181        ("beta(Q)", F1**2 / F2), 
     182        ("S_eff(Q)", 1 + (F1**2 / F2)*(S-1)), 
     183        ("effective_radius", effective_radius), 
     184        # ("I(Q)", scale*(F2 + (F1**2)*(S-1)) + bg), 
     185    )) 
     186 
    159187class ProductModel(KernelModel): 
    160188    def __init__(self, model_info, P, S): 
     
    276304            s_result = self.s_kernel.Iq(s_details, s_values, cutoff, False) 
    277305            combined_scale = scale*volfrac/volume_avg 
    278             # Define lazy results based on intermediate values. 
    279             # The return value for the calculation should be an ordered 
    280             # dictionary containing any result the user might want to see 
    281             # at the end of the calculation, including scalars, strings, 
    282             # and plottable data.  Don't want to build this structure during 
    283             # fits, only when displaying the final result (or a one-off 
    284             # computation which asks for it). 
    285             # Do not use the current hack of storing the intermediate values 
    286             # in self.results since that leads to awkward threading issues. 
    287             # Instead return the function along with the bundle of inputs. 
    288             # P and Q may themselves have intermediate results they want to 
    289             # include, such as A and B if P = A + B.  Might use this mechanism 
    290             # to return the computed effective radius as well. 
    291             #def lazy_results(Q, S, F1, F2, scale): 
    292             #    """ 
    293             #    beta = F1**2 / F2  # what about divide by zero errors? 
    294             #    return { 
    295             #        'P' : Data1D(Q, scale*F2), 
    296             #        'beta': Data1D(Q, beta), 
    297             #        'S' : Data1D(Q, S), 
    298             #        'Seff': Data1D(Q, 1 + beta*(S-1)), 
    299             #        'I' : Data1D(Q, scale*(F2 + (F1**2)*(S-1)) + background), 
    300             #    } 
    301             #lazy_pars = s_result, F1, F2, combined_scale 
    302             self.results = [F2*volfrac/volume_avg, s_result] 
     306 
     307            self.results = lambda: _intermediates_beta(F1, F2, s_result, volfrac/volume_avg, background, effective_radius) 
    303308            final_result = combined_scale*(F2 + (F1**2)*(s_result - 1)) + background 
     309 
    304310        else: 
    305311            p_result, effective_radius = self.p_kernel.Pq_Reff( 
     
    309315            s_result = self.s_kernel.Iq(s_details, s_values, cutoff, False) 
    310316            # remember the parts for plotting later 
    311             self.results = [p_result, s_result] 
     317            self.results = lambda: _intermediates(p_result, s_result, effective_radius) 
    312318            final_result = scale*(p_result*s_result) + background 
    313319 
  • sasmodels/sasview_model.py

    r6e7ba14 rd32de68  
    381381                continue 
    382382            self.params[p.name] = p.default 
    383             self.details[p.id] = [p.units, p.limits[0], p.limits[1]] 
     383            if p.limits and type(p.limits) is list and len(p.limits) > 1: 
     384                self.details[p.id] = [p.units if not p.choices else p.choices, p.limits[0], p.limits[1]] 
     385            else: 
     386                self.details[p.id] = [p.units if not p.choices else p.choices, None, None] 
    384387            if p.polydisperse: 
    385388                self.details[p.id+".width"] = [ 
     
    616619        # so that it returns a results object containing all the bits: 
    617620        #     the A, B, C, ... of the composition model (and any subcomponents?) 
    618         #     the P and S of the product model, 
     621        #     the P and S of the product model 
    619622        #     the combined model before resolution smearing, 
    620623        #     the sasmodel before sesans conversion, 
     
    638641            with calculation_lock: 
    639642                self._calculate_Iq(qx) 
    640                 return self._intermediate_results 
     643                # for compatibility with sasview 4.3 
     644                results = self._intermediate_results() 
     645                return results["P(Q)"], results["S(Q)"] 
    641646        else: 
    642647            return None 
Note: See TracChangeset for help on using the changeset viewer.