source: sasview/sansmodels/src/sans/models/PeakGaussModel.py @ 0b12abb5

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 0b12abb5 was 27fea3f, checked in by Gervaise Alina <gervyh@…>, 15 years ago

add a few models 1D

  • Property mode set to 100644
File size: 3.2 KB
Line 
1#!/usr/bin/env python
2"""
3    Model describes a Gaussian shaped peak including a flat background
4    Provide F(q) = scale*exp( -1/2 *[(q-q0)/B]^2 )+ background
5    PeakGaussModel function as a BaseComponent model
6"""
7
8from sans.models.BaseComponent import BaseComponent
9import math
10
11class PeakGaussModel(BaseComponent):
12   
13    """
14        Class that evaluates a gaussian  shaped peak.
15       
16        F(q) = scale*exp( -1/2 *[(q-qo)/B]^2 )+ background
17       
18        The model has three parameters:
19            scale     =  scale
20            q0        =  peak position
21            B         =  standard deviation
22            background=  incoherent background
23    """
24       
25    def __init__(self):
26        """ Initialization """
27       
28        # Initialize BaseComponent first, then sphere
29        BaseComponent.__init__(self)
30       
31        ## Name of the model
32        self.name = "Peak Gauss Model"
33        self.description=""" F(q) = scale*exp( -1/2 *[(q-q0)/B]^2 )+ background
34       
35        The model has three parameters:
36        scale     =  scale
37        q0        =  peak position
38        B         =  standard deviation
39        background=  incoherent background"""
40        ## Define parameters
41        self.params = {}
42        self.params['scale']              = 100.0
43        self.params['q0']                 = 0.05
44        self.params['B']              = 0.005
45        self.params['background']         = 1.0
46
47        ## Parameter details [units, min, max]
48        self.details = {}
49        self.details['q0']            = ['[1/A]', None, None]
50        self.details['scale']             = ['', 0, None]
51        self.details['B']            = ['[1/A]', None, None]
52        self.details['background']        = ['[1/cm]', None, None]
53        #list of parameter that cannot be fitted
54        self.fixed= [] 
55           
56    def _PeakGauss(self, x):
57        """
58            Evaluate  F(x) = scale*exp( -1/2 *[(x-q0)/B]^2 )+ background
59           
60        """
61        return self.params['scale']*math.exp(-1/2 *\
62                         math.pow((x - self.params['q0'])/self.params['B'],2)) \
63                            + self.params['background']
64       
65   
66    def run(self, x = 0.0):
67        """ Evaluate the model
68            @param x: input q-value (float or [float, float] as [r, theta])
69            @return: (Peak Gaussian value)
70        """
71        if x.__class__.__name__ == 'list':
72            return self._PeakGauss(x[0])
73        elif x.__class__.__name__ == 'tuple':
74            raise ValueError, "Tuples are not allowed as input to BaseComponent models"
75        else:
76            return self._PeakGauss(x)
77   
78    def runXY(self, x = 0.0):
79        """ Evaluate the model
80            @param x: input q-value (float or [float, float] as [qx, qy])
81            @return: Peak Gaussian value
82        """
83        if x.__class__.__name__ == 'list':
84            q = math.sqrt(x[0]**2 + x[1]**2)
85            return self._PeakGauss(q)
86        elif x.__class__.__name__ == 'tuple':
87            raise ValueError, "Tuples are not allowed as input to BaseComponent models"
88        else:
89            return self._PeakGauss(x)
Note: See TracBrowser for help on using the repository browser.