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