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

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 30ccad1 was 8dc02d8b, checked in by Jae Cho <jhjcho@…>, 14 years ago

added rectangular function for poly-dispersion

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