Skip to content

Commit

Permalink
Merge pull request #43 from jmborr/42_tabulated_resolution
Browse files Browse the repository at this point in the history
Tabulated resolution function
  • Loading branch information
jmborr authored Mar 20, 2018
2 parents d277d6b + 2d9accf commit f38c9b8
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 7 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ History
0.2.0 ()
--------
* Load Mantid Nexus data (PR #38)
* Tabulated resolution model (PR #43)

0.1.0 (2017-12-13)
------------------
Expand Down
1 change: 1 addition & 0 deletions docs/qef/models/models.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Models
:maxdepth: 1

deltadirac
resolution
strexpft
tabulatedmodel
teixeira
Expand Down
7 changes: 7 additions & 0 deletions docs/qef/models/resolution.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Resolution models
=================

.. automodule:: qef.models.resolution
:members:
:undoc-members:
:show-inheritance:
21 changes: 21 additions & 0 deletions qef/models/resolution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from __future__ import (absolute_import, division, print_function)

from qef.models.tabulatedmodel import TabulatedModel


class TabulatedResolutionModel(TabulatedModel):
r"""Interpolator of resolution data with no fit parameters
Parameters
----------
xs: :class:`~numpy:numpy.ndarray`
given domain of the function, energy
ys: :class:`~numpy:numpy.ndarray`
given domain of the function, intensity
"""

def __init__(self, xs, ys, *args, **kwargs):
super(TabulatedResolutionModel, self).__init__(xs, ys, *args, **kwargs)
self.set_param_hint('amplitude', value=1.0, vary=False)
self.set_param_hint('center', value=0.0, vary=False)
13 changes: 6 additions & 7 deletions qef/models/tabulatedmodel.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from __future__ import (absolute_import, division, print_function)

from scipy.interpolate import interp1d
from lmfit import Model, models


class TabulatedModel (Model):
class TabulatedModel(Model):
"""fitting the tabulated Model to some arbitrary points
Parameters
Expand All @@ -13,12 +15,9 @@ class TabulatedModel (Model):
ys: :class:`~numpy:numpy.ndarray`
given domain of the function, intensity
amplitude : float
peak intensity of the curve
center : float
position of the peak
Fitting parameters:
- rescaling factor ``amplitude``
- shift along the X-axis ``center``
"""

def __init__(self, xs, ys, *args, **kwargs):
Expand Down
29 changes: 29 additions & 0 deletions tests/models/test_resolution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from __future__ import (absolute_import, division, print_function)

import os
import pytest
import numpy as np
from lmfit.lineshapes import lorentzian

from qef.models.resolution import TabulatedResolutionModel


def test_tabulated_resolution():
x_sim = np.arange(-1.0, 1.0, 0.0003) # energy domain, in meV
y_sim = lorentzian(x_sim, amplitude=1, center=0, sigma=0.042)
intensity = 42.0
peak_center = 0.0002
x_exp = np.arange(-0.1, 0.5, 0.0004)
y_exp = lorentzian(x_exp, amplitude=intensity, center=peak_center,
sigma=0.042)

model = TabulatedResolutionModel(x_sim, y_sim)
params = model.make_params()
fit = model.fit(y_exp, params, x=x_exp)

assert abs(fit.best_values['amplitude'] - 1.0) < 0.0001
assert abs(fit.best_values['center'] - 0.0) < 0.0001


if __name__ == '__main__':
pytest.main([os.path.abspath(__file__)])

0 comments on commit f38c9b8

Please sign in to comment.