Commit 74d1b345 by Valentin Valls

### Rework fit_gaussian

parent 9a6699fc
 ... ... @@ -7,8 +7,8 @@ from __future__ import annotations from typing import Tuple from typing import NamedTuple import collections import numpy import scipy.optimize ... ... @@ -47,13 +47,22 @@ def _gaussian(x, p): return p[3] + p[2] * numpy.exp(-(x - p[0]) ** 2 / (2 * p[1] ** 2)) GaussianType = collections.namedtuple( "FwhmType", ["fwhm", "std", "pos_x", "height", "background"] ) """Description of the gaussian modelization""" class GaussianFitResult(NamedTuple): """Description of the gaussian modelization""" fwhm: float std: float pos_x: float height: float background: float def fit_gaussian(xx: numpy.ndarray, yy: numpy.ndarray) -> GaussianType: def transform(self, xx: numpy.ndarray) -> numpy.ndarray: """Transform a X array using the fitted function""" p = [self.pos_x, self.std, self.height, self.background] return _gaussian(xx, p) def fit_gaussian(xx: numpy.ndarray, yy: numpy.ndarray) -> GaussianFitResult: """ Fit `xx` and `yy` curve with a gaussian and returns its characteristics. """ ... ... @@ -74,4 +83,4 @@ def fit_gaussian(xx: numpy.ndarray, yy: numpy.ndarray) -> GaussianType: # Compute characteristics fit_mean, fit_std, fit_height, fit_background = p1 fwhm = 2 * numpy.sqrt(2 * numpy.log(2)) * fit_std return GaussianType(fwhm, fit_std, fit_mean, fit_height, fit_background) return GaussianFitResult(fwhm, fit_std, fit_mean, fit_height, fit_background)
 """Testing mathutils module.""" import numpy from bliss.flint.utils import mathutils def test_gaussian_fit(): xx = numpy.arange(5) yy = numpy.array([10, 10, 110, 10, 10]) r = mathutils.fit_gaussian(xx, yy) assert r is not None numpy.testing.assert_almost_equal(r.height, 100) numpy.testing.assert_almost_equal(r.pos_x, 2) numpy.testing.assert_almost_equal(r.background, 10) yy2 = r.transform(xx) numpy.testing.assert_almost_equal(yy2, yy)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!