Changeset 37a7252 in sasmodels


Ignore:
Timestamp:
Jan 27, 2016 1:42:24 PM (8 years ago)
Author:
Paul Kienzle <pkienzle@…>
Branches:
master, core_shell_microgels, costrafo411, magnetic_model, release_v0.94, release_v0.95, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
Children:
d15a908
Parents:
87edabf
Message:

doc and delint

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • extra/pylint.rc

    r5ef0633 r37a7252  
    66# Python code to execute, usually for sys.path manipulation such as 
    77# pygtk.require(). 
    8 #init-hook= 
     8#init-hook='import os, sys; sys.path.extend([os.getcwd(),os.path.join(os.getcwd(),"extra")])' 
    99 
    1010# Profiled execution. 
  • sasmodels/__init__.py

    r9890053 r37a7252  
     1""" 
     2sasmodels 
     3========= 
     4 
     5**sasmodels** is a package containing models for small angle neutron and xray 
     6scattering.  Models supported are the one dimensional circular average and 
     7two dimensional oriented patterns.  As well as the form factor calculations 
     8for the individual shapes **sasmodels** also provides automatic shape 
     9polydispersity, angular dispersion and resolution convolution.  SESANS 
     10patterns can be computed for any model. 
     11 
     12Models can be written in python or in C.  C models can run on the GPU if 
     13OpenCL drivers are available.  See :mod:`generate` for details on 
     14defining new models. 
     15""" 
     16 
    117__version__ = "0.9" 
  • sasmodels/bumps_model.py

    rec7e360 r37a7252  
    66arguments to set the initial value for each parameter. 
    77 
    8 :class:`Experiment` combines the *Model* function with a data file loaded by the 
    9 sasview data loader.  *Experiment* takes a *cutoff* parameter controlling 
     8:class:`Experiment` combines the *Model* function with a data file loaded by 
     9the sasview data loader.  *Experiment* takes a *cutoff* parameter controlling 
    1010how far the polydispersity integral extends. 
    1111 
    1212""" 
     13 
     14__all__ = [ 
     15    "Model", "Experiment", 
     16    ] 
    1317 
    1418import warnings 
     
    2024 
    2125# CRUFT: old style bumps wrapper which doesn't separate data and model 
     26# pylint: disable=invalid-name 
    2227def BumpsModel(data, model, cutoff=1e-5, **kw): 
     28    r""" 
     29    Bind a model to data, along with a polydispersity cutoff. 
     30 
     31    *data* is a :class:`data.Data1D`, :class:`data.Data2D` or 
     32    :class:`data.Sesans` object.  Use :func:`data.empty_data1D` or 
     33    :func:`data.empty_data2D` to define $q, \Delta q$ calculation 
     34    points for displaying the SANS curve when there is no measured data. 
     35 
     36    *model* is a runnable module as returned from :func:`core.load_model`. 
     37 
     38    *cutoff* is the polydispersity weight cutoff. 
     39 
     40    Any additional *key=value* pairs are model dependent parameters. 
     41 
     42    Returns an :class:`Experiment` object. 
     43 
     44    Note that the usual Bumps semantics is not fully supported, since 
     45    assigning *M.name = parameter* on the returned experiment object 
     46    does not set that parameter in the model.  Range setting will still 
     47    work as expected though. 
     48 
     49    .. deprecated:: 0.1 
     50        Use :class:`Experiment` instead. 
     51    """ 
    2352    warnings.warn("Use of BumpsModel is deprecated.  Use bumps_model.Experiment instead.") 
     53 
     54    # Create the model and experiment 
    2455    model = Model(model, **kw) 
    2556    experiment = Experiment(data=data, model=model, cutoff=cutoff) 
    26     for k in model._parameter_names: 
    27         setattr(experiment, k, getattr(model, k)) 
     57 
     58    # Copy the model parameters up to the experiment object. 
     59    for k, v in model.parameters().items(): 
     60        setattr(experiment, k, v) 
    2861    return experiment 
    2962 
     63 
    3064def create_parameters(model_info, **kwargs): 
     65    """ 
     66    Generate Bumps parameters from the model info. 
     67 
     68    *model_info* is returned from :func:`generate.model_info` on the 
     69    model definition module. 
     70 
     71    Any additional *key=value* pairs are initial values for the parameters 
     72    to the models.  Uninitialized parameters will use the model default 
     73    value. 
     74 
     75    Returns a dictionary of *{name: Parameter}* containing the bumps 
     76    parameters for each model parameter, and a dictionary of 
     77    *{name: str}* containing the polydispersity distribution types. 
     78    """ 
    3179    # lazy import; this allows the doc builder and nosetests to run even 
    3280    # when bumps is not on the path. 
    3381    from bumps.names import Parameter 
    34  
    35     partype = model_info['partype'] 
    3682 
    3783    pars = {} 
     
    4086        value = kwargs.pop(name, default) 
    4187        pars[name] = Parameter.default(value, name=name, limits=limits) 
    42     for name in partype['pd-2d']: 
     88    for name in model_info['partype']['pd-2d']: 
    4389        for xpart, xdefault, xlimits in [ 
    44             ('_pd', 0., limits), 
    45             ('_pd_n', 35., (0, 1000)), 
    46             ('_pd_nsigma', 3., (0, 10)), 
    47         ]: 
     90                ('_pd', 0., limits), 
     91                ('_pd_n', 35., (0, 1000)), 
     92                ('_pd_nsigma', 3., (0, 10)), 
     93            ]: 
    4894            xname = name + xpart 
    4995            xvalue = kwargs.pop(xname, xdefault) 
     
    5197 
    5298    pd_types = {} 
    53     for name in partype['pd-2d']: 
     99    for name in model_info['partype']['pd-2d']: 
    54100        xname = name + '_pd_type' 
    55101        xvalue = kwargs.pop(xname, 'gaussian') 
     
    63109 
    64110class Model(object): 
     111    """ 
     112    Bumps wrapper for a SAS model. 
     113 
     114    *model* is a runnable module as returned from :func:`core.load_model`. 
     115 
     116    *cutoff* is the polydispersity weight cutoff. 
     117 
     118    Any additional *key=value* pairs are model dependent parameters. 
     119    """ 
    65120    def __init__(self, model, **kwargs): 
    66121        self._sasmodel = model 
    67122        pars, pd_types = create_parameters(model.info, **kwargs) 
    68         for k,v in pars.items(): 
     123        for k, v in pars.items(): 
    69124            setattr(self, k, v) 
    70         for k,v in pd_types.items(): 
     125        for k, v in pd_types.items(): 
    71126            setattr(self, k, v) 
    72127        self._parameter_names = list(pars.keys()) 
     
    75130    def parameters(self): 
    76131        """ 
    77         Return a dictionary of parameters 
     132        Return a dictionary of parameters objects for the parameters, 
     133        excluding polydispersity distribution type. 
    78134        """ 
    79135        return dict((k, getattr(self, k)) for k in self._parameter_names) 
    80136 
    81137    def state(self): 
     138        """ 
     139        Return a dictionary of current values for all the parameters, 
     140        including polydispersity distribution type. 
     141        """ 
    82142        pars = dict((k, getattr(self, k).value) for k in self._parameter_names) 
    83143        pars.update((k, getattr(self, k)) for k in self._pd_type_names) 
     
    85145 
    86146class Experiment(DataMixin): 
    87     """ 
    88     Return a bumps wrapper for a SAS model. 
    89  
    90     *data* is the data to be fitted. 
    91  
    92     *model* is the SAS model from :func:`core.load_model`. 
     147    r""" 
     148    Bumps wrapper for a SAS experiment. 
     149 
     150    *data* is a :class:`data.Data1D`, :class:`data.Data2D` or 
     151    :class:`data.Sesans` object.  Use :func:`data.empty_data1D` or 
     152    :func:`data.empty_data2D` to define $q, \Delta q$ calculation 
     153    points for displaying the SANS curve when there is no measured data. 
     154 
     155    *model* is a :class:`Model` object. 
    93156 
    94157    *cutoff* is the integration cutoff, which avoids computing the 
    95158    the SAS model where the polydispersity weight is low. 
    96159 
    97     Model parameters can be initialized with additional keyword 
    98     arguments, or by assigning to model.parameter_name.value. 
    99  
    100     The resulting bumps model can be used directly in a FitProblem call. 
     160    The resulting model can be used directly in a Bumps FitProblem call. 
    101161    """ 
    102162    def __init__(self, data, model, cutoff=1e-5): 
     
    109169 
    110170    def update(self): 
     171        """ 
     172        Call when model parameters have changed and theory needs to be 
     173        recalculated. 
     174        """ 
    111175        self._cache = {} 
    112176 
    113177    def numpoints(self): 
    114178        """ 
    115             Return the number of points 
     179        Return the number of data points 
    116180        """ 
    117181        return len(self.Iq) 
     
    124188 
    125189    def theory(self): 
     190        """ 
     191        Return the theory corresponding to the model parameters. 
     192 
     193        This method uses lazy evaluation, and requires model.update() to be 
     194        called when the parameters have changed. 
     195        """ 
    126196        if 'theory' not in self._cache: 
    127197            pars = self.model.state() 
     
    130200 
    131201    def residuals(self): 
     202        """ 
     203        Return theory minus data normalized by uncertainty. 
     204        """ 
    132205        #if np.any(self.err ==0): print("zeros in err") 
    133206        return (self.theory() - self.Iq) / self.dIq 
    134207 
    135208    def nllf(self): 
     209        """ 
     210        Return the negative log likelihood of seeing data given the model 
     211        parameters, up to a normalizing constant which depends on the data 
     212        uncertainty. 
     213        """ 
    136214        delta = self.residuals() 
    137215        #if np.any(np.isnan(R)): print("NaN in residuals") 
     
    149227 
    150228    def simulate_data(self, noise=None): 
     229        """ 
     230        Generate simulated data. 
     231        """ 
    151232        Iq = self.theory() 
    152233        self._set_data(Iq, noise) 
    153234 
    154235    def save(self, basename): 
     236        """ 
     237        Save the model parameters and data into a file. 
     238 
     239        Not Implemented. 
     240        """ 
    155241        pass 
    156242 
Note: See TracChangeset for help on using the changeset viewer.