[27fea3f] | 1 | #!/usr/bin/env python |
---|
| 2 | """ |
---|
| 3 | Model describes a Lorentzian shaped peak including a flat background |
---|
| 4 | Provide F(q) = scale/(1+[(q-q0)/B]^2 ) + background |
---|
| 5 | PeakLorentzModel function as a BaseComponent model |
---|
| 6 | """ |
---|
| 7 | |
---|
[79492222] | 8 | from sas.models.BaseComponent import BaseComponent |
---|
[27fea3f] | 9 | import math |
---|
| 10 | |
---|
| 11 | class PeakLorentzModel(BaseComponent): |
---|
| 12 | |
---|
| 13 | """ |
---|
| 14 | Class that evaluates a gaussian shaped peak. |
---|
| 15 | |
---|
| 16 | F(q) = scale/(1+[(q-q0)/B]^2 ) + background |
---|
| 17 | |
---|
| 18 | The model has three parameters: |
---|
| 19 | scale = scale |
---|
| 20 | q0 = peak position |
---|
| 21 | B = ( hwhm) half-width-halfmaximum |
---|
| 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 Lorentz Model" |
---|
[411d8bf] | 33 | self.description = """ F(q) = scale/(1+[(q-q0)/B]^2 ) + background |
---|
[27fea3f] | 34 | |
---|
| 35 | The model has three parameters: |
---|
| 36 | scale = scale |
---|
| 37 | q0 = peak position |
---|
| 38 | B = ( hwhm) half-width-halfmaximum |
---|
| 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 |
---|
[411d8bf] | 54 | self.fixed = [] |
---|
[27fea3f] | 55 | |
---|
| 56 | def _PeakLorentz(self, x): |
---|
| 57 | """ |
---|
| 58 | Evaluate F(x) = scale/(1+[(x-q0)/B]^2 ) + background |
---|
| 59 | |
---|
| 60 | """ |
---|
| 61 | return self.params['scale']/(1+\ |
---|
| 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 Lorentzian value |
---|
| 70 | """ |
---|
| 71 | if x.__class__.__name__ == 'list': |
---|
| 72 | return self._PeakLorentz(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._PeakLorentz(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 Lorentzian value |
---|
| 82 | """ |
---|
| 83 | if x.__class__.__name__ == 'list': |
---|
| 84 | q = math.sqrt(x[0]**2 + x[1]**2) |
---|
| 85 | return self._PeakLorentz(q) |
---|
| 86 | elif x.__class__.__name__ == 'tuple': |
---|
| 87 | raise ValueError, "Tuples are not allowed as input to BaseComponent models" |
---|
| 88 | else: |
---|
| 89 | return self._PeakLorentz(x) |
---|