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

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 e390933 was 0f5bc9f, checked in by Mathieu Doucet <doucetm@…>, 16 years ago

Update of all C models to the new style of C++ models

  • Property mode set to 100644
File size: 3.6 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
46class DispersionModel:
47    """
48        Python bridge class for a basic dispersion model
49        class with a constant parameter value distribution
50    """
51    def __init__(self):
52        self.cdisp = c_models.new_dispersion_model()
53       
54    def set_weights(self, values, weights):
55        """
56            Set the weights of an array dispersion
57        """
58        message = "set_weights is not available for DispersionModel.\n"
59        message += "  Solution: Use an ArrayDispersion object"
60        raise "RuntimeError", message
61       
62class GaussianDispersion(DispersionModel):
63    """
64        Python bridge class for a dispersion model based
65        on a Gaussian distribution.
66    """
67    def __init__(self):
68        self.cdisp = c_models.new_gaussian_model()
69       
70    def set_weights(self, values, weights):
71        """
72            Set the weights of an array dispersion
73        """
74        message = "set_weights is not available for GaussiantDispersion.\n"
75        message += "  Solution: Use an ArrayDispersion object"
76        raise "RuntimeError", message
77       
78class ArrayDispersion(DispersionModel):
79    """
80        Python bridge class for a dispersion model based on arrays.
81        The user has to set a weight distribution that
82        will be used in the averaging the model parameter
83        it is applied to.
84    """
85    def __init__(self):
86        self.cdisp = c_models.new_array_model()
87       
88    def set_weights(self, values, weights):
89        """
90            Set the weights of an array dispersion
91            Only accept numpy arrays.
92            @param values: numpy array of values
93            @param weights: numpy array of weights for each value entry
94        """
95        if len(values) != len(weights):
96            raise ValueError, "ArrayDispersion.set_weights: given arrays are of different lengths"
97       
98        c_models.set_dispersion_weights(self.cdisp, values, weights)
99       
100       
Note: See TracBrowser for help on using the repository browser.