source: sasview/sansmodels/src/sans/models/dispersion_models.py @ b3644f3

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since b3644f3 was c9aa125, checked in by Gervaise Alina <gervyh@…>, 15 years ago

dispersion class rename

  • Property mode set to 100644
File size: 4.8 KB
Line 
1"""
2    This software was developed by the University of Tennessee as part of the
3    Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
4    project funded by the US National Science Foundation.
5
6    If you use DANSE applications to do scientific research that leads to
7    publication, we ask that you acknowledge the use of the software with the
8    following sentence:
9
10    "This work benefited from DANSE software developed under NSF award DMR-0520547."
11
12    copyright 2008, University of Tennessee
13"""
14
15"""
16    Class definitions for python dispersion model for
17    model parameters. These classes are bridges to the C++
18    dispersion object.
19   
20    The ArrayDispersion class takes in numpy arrays only.
21   
22    Usage:
23    These classes can be used to set the dispersion model of a SANS model
24    parameter:
25   
26        cyl = CylinderModel()
27        cyl.set_dispersion('radius', GaussianDispersion())
28   
29   
30    After the dispersion model is set, you can access it's
31    parameter through the dispersion dictionary:
32   
33        cyl.dispersion['radius']['width'] = 5.0
34   
35    TODO: For backward compatibility, the model parameters are still kept in
36    a dictionary. The next iteration of refactoring work should involve moving
37    away from value-based parameters to object-based parameter. We want to
38    store parameters as objects so that we can unify the 'params' and 'dispersion'
39    dictionaries into a single dictionary of parameter objects that hold the
40    complete information about the parameter (units, limits, dispersion model, etc...). 
41   
42   
43"""
44import sans_extension.c_models as c_models 
45
46
47
48class DispersionModel:
49    """
50        Python bridge class for a basic dispersion model
51        class with a constant parameter value distribution
52    """
53    def __init__(self):
54        self.cdisp = c_models.new_dispersion_model()
55       
56    def set_weights(self, values, weights):
57        """
58            Set the weights of an array dispersion
59        """
60        message = "set_weights is not available for DispersionModel.\n"
61        message += "  Solution: Use an ArrayDispersion object"
62        raise "RuntimeError", message
63       
64class GaussianDispersion(DispersionModel):
65    """
66        Python bridge class for a dispersion model based
67        on a Gaussian distribution.
68    """
69    def __init__(self):
70        self.cdisp = c_models.new_gaussian_model()
71       
72    def set_weights(self, values, weights):
73        """
74            Set the weights of an array dispersion
75        """
76        message = "set_weights is not available for GaussianDispersion.\n"
77        message += "  Solution: Use an ArrayDispersion object"
78        raise "RuntimeError", message
79   
80class SchulzDispersion(DispersionModel):
81    """
82        Python bridge class for a dispersion model based
83        on a Schulz distribution.
84    """
85    def __init__(self):
86        self.cdisp = c_models.new_schulz_model()
87       
88    def set_weights(self, values, weights):
89        """
90            Set the weights of an array dispersion
91        """
92        message = "set_weights is not available for SchulzDispersion.\n"
93        message += "  Solution: Use an ArrayDispersion object"
94        raise "RuntimeError", message
95   
96class LogNormalDispersion(DispersionModel):
97    """
98        Python bridge class for a dispersion model based
99        on a Log Normal distribution.
100    """
101    def __init__(self):
102        self.cdisp = c_models.new_lognormal_model()
103       
104    def set_weights(self, values, weights):
105        """
106            Set the weights of an array dispersion
107        """
108        message = "set_weights is not available for LogNormalDispersion.\n"
109        message += "  Solution: Use an ArrayDispersion object"
110        raise "RuntimeError", message
111       
112class ArrayDispersion(DispersionModel):
113    """
114        Python bridge class for a dispersion model based on arrays.
115        The user has to set a weight distribution that
116        will be used in the averaging the model parameter
117        it is applied to.
118    """
119    def __init__(self):
120        self.cdisp = c_models.new_array_model()
121       
122    def set_weights(self, values, weights):
123        """
124            Set the weights of an array dispersion
125            Only accept numpy arrays.
126            @param values: numpy array of values
127            @param weights: numpy array of weights for each value entry
128        """
129        if len(values) != len(weights):
130            raise ValueError, "ArrayDispersion.set_weights: given arrays are of different lengths"
131       
132        c_models.set_dispersion_weights(self.cdisp, values, weights)
133 
134models = {"gaussian":GaussianDispersion, "array":ArrayDispersion,
135          "schulz":SchulzDispersion, "lognormal":LogNormalDispersion}       
136       
Note: See TracBrowser for help on using the repository browser.