Détail du package

ml-spectra-fitting

mljs6kMIT5.0.1

Fit spectra using gaussian or lorentzian

Optimization, Lorentzian, Gaussian, pseudovoigt

readme

ml-spectra-fitting

NPM version npm download test coverage license

This is a spectra fitting package to optimize the position (x), max intensity (y), full width at half-maximum (FWHM = width) and the ratio of gaussian contribution (mu) if it's required. It supports three kinds of shapes:

Name Equation
Gaussian
Lorentzian
Pseudo Voigt

where

It is a wrapper of ml-levenberg-marquardt

API Documentation

Installation

npm i ml-spectra-fitting

Example

import { optimize } from 'ml-spectra-fitting';
import { SpectrumGenerator } from 'spectrum-generator';

const generator = new SpectrumGenerator({
  nbPoints: 101,
  from: -1,
  to: 1,
});

// by default the kind of shape is gaussian;
generator.addPeak({ x: 0.5, y: 0.2 }, { fwhm: 0.2 });
generator.addPeak(
  { x: -0.5, y: 0.2 },
  {
    shape: {
      kind: 'lorentzian',
      fwhm: 0.1,
    },
  },
);

//points to fit {x, y};
let data = generator.getSpectrum();
console.log(JSON.stringify({ x: Array.from(data.x), y: Array.from(data.y) }));
//the approximate values to be optimized, It could coming from a peak picking with ml-gsd
let peaks = [
  {
    x: -0.5,
    y: 0.22,
    shape: {
      kind: 'gaussian',
      fwhm: 0.25,
    },
  },
  {
    x: 0.52,
    y: 0.18,
    shape: {
      kind: 'gaussian',
      fwhm: 0.18,
    },
  },
];

// the function receive an array of peak with {x, y, fwhm} as a guess
// and return a list of objects
let fittedParams = optimize(data, peaks, { shape: { kind: 'pseudoVoigt' } });

console.log(fittedParams);
const result = {
  error: 0.12361588652854476,
  iterations: 100,
  peaks: [
    {
      x: -0.5000014532421942,
      y: 0.19995307937326137,
      shape: {
        kind: 'pseudoVoigt',
        fwhm: 0.10007670374735196,
        mu: 0.004731136777288483,
      },
    },
    {
      x: 0.5001051783652894,
      y: 0.19960010175400406,
      shape: {
        kind: 'pseudoVoigt',
        fwhm: 0.19935932346969124,
        mu: 1,
      },
    },
  ],
};

For data with and combination of signals with shapes between gaussian and lorentzians, we could use the kind pseudovoigt to fit the data.

import { optimize } from 'ml-spectra-fitting';
import { SpectrumGenerator } from 'spectrum-generator';

const generator = new SpectrumGenerator({
  nbPoints: 101,
  from: -1,
  to: 1,
});

// by default the kind of shape is gaussian;
generator.addPeak({ x: 0.5, y: 0.2 }, { fwhm: 0.2 });
generator.addPeak(
  { x: -0.5, y: 0.2 },
  {
    shape: {
      kind: 'lorentzian',
      fwhm: 0.1,
    },
  },
);

//points to fit {x, y};
let data = generator.getSpectrum();
console.log(JSON.stringify({ x: Array.from(data.x), y: Array.from(data.y) }));
//the approximate values to be optimized, It could coming from a peak picking with ml-gsd
let peaks = [
  {
    x: -0.5,
    y: 0.22,
    shape: {
      kind: 'gaussian',
      fwhm: 0.25,
    },
  },
  {
    x: 0.52,
    y: 0.18,
    shape: {
      kind: 'gaussian',
      fwhm: 0.18,
    },
  },
];

// the function receive an array of peak with {x, y, fwhm} as a guess
// and return a list of objects
let fittedParams = optimize(data, peaks, { shape: { kind: 'pseudoVoigt' } });

console.log(fittedParams);
const result = {
  error: 0.12361588652854476,
  iterations: 100,
  peaks: [
    {
      x: -0.5000014532421942,
      y: 0.19995307937326137,
      shape: {
        kind: 'pseudoVoigt',
        fwhm: 0.10007670374735196,
        mu: 0.004731136777288483,
      },
    },
    {
      x: 0.5001051783652894,
      y: 0.19960010175400406,
      shape: {
        kind: 'pseudoVoigt',
        fwhm: 0.19935932346969124,
        mu: 1,
      },
    },
  ],
};

License

MIT

changelog

Changelog

5.0.1 (2025-06-13)

Bug Fixes

5.0.0 (2025-06-13)

⚠ BREAKING CHANGES

  • migrate to esm-only and update ml-levenberg-marquardt (#112)

Code Refactoring

  • migrate to esm-only and update ml-levenberg-marquardt (#112) (24873a6)

4.2.4 (2024-09-26)

Bug Fixes

4.2.3 (2024-03-07)

Bug Fixes

  • ml-spectra-processing and dependencies (1c1e181)

4.2.2 (2024-03-06)

Bug Fixes

4.2.1 (2023-03-24)

Bug Fixes

4.2.0 (2022-09-23)

Features

  • global optimization by direct algorithm (#103) (fb27ade), closes #98

4.1.1 (2022-08-26)

Bug Fixes

4.1.0 (2022-08-04)

Features

  • add optional baseline option (7e9d718)

Bug Fixes

4.0.2 (2022-05-04)

Bug Fixes

  • put back compatibility with node 12 (a4bd8c6)

4.0.1 (2022-05-02)

Bug Fixes

  • correctly define type of OptimizedPeak (6776ea6)

4.0.0 (2022-05-01)

⚠ BREAKING CHANGES

  • many breaking changes

Features

  • allow to specify shape at the level of peak (90704b8)
  • many breaking changes (813ad04)

3.1.0 (2022-02-25)

Features

Bug Fixes

3.0.4 (2022-01-23)

Bug Fixes

3.0.3 (2021-12-01)

Bug Fixes

  • move type definition to src to publish in npm (baa1fb2)

3.0.2 (2021-11-30)

Bug Fixes

  • remove I from OptimizeOptions and OptimizationOptions (b007255)
  • update README (f4f34cd)

3.0.1 (2021-11-23)

Bug Fixes

3.0.0 (2021-11-18)

⚠ BREAKING CHANGES

  • change width to fwhm (#60)

Features

2.0.0 (2021-10-11)

⚠ BREAKING CHANGES

  • homogenize and generalize output (#58)

Features

  • fixing wrong order in object assignment. close #52 (#53) (ce3d47f)
  • homogenize and generalize output (#58) (1b3970c)
  • update ml-peak-shape-generator to 2.0.1 (#54) (73782cc)

1.0.0 (2021-03-24)

Bug Fixes

  • update readme and dependencies (c512ea3)

0.13.0 (2021-01-21)

Features

  • generate new version with checkInput function (3782d1f)
  • increment min max of width parameter (7d7c324)

0.12.0 (2021-01-13)

Features

  • remove twice declaration (bb8d013)
  • use gradientDifference from peak too (#48) (fb81ac2)
  • x search space depending on peak width (#45) (63375d3)

0.11.0 (2020-12-20)

Features

  • add timeout option (7743e81)
  • update ml-peak-shape-generator 0.12.0 (de1313e)

0.10.0 (2020-12-10)

Features

  • update ml-peak-shape-generator (9fb7f67)

0.9.0 (2020-12-04)

Features

  • update ml-levenberg-marquardt and adapt to the new version (#41) (9773585)

0.8.0 (2020-11-18)

Features

  • first make a copy of the peaks to keep all attributes (466278e)

0.7.1 (2020-11-14)

Bug Fixes

  • avoid inplace modification of shape option (5d54fe3)

0.7.0 (2020-11-13)

Features

0.6.0 (2020-11-12)

Features

  • update ml-peak-shape-generator (c16fbaa)

0.5.0 (2020-11-06)

Features

  • optimize gaussian, lorentzian or pseudovoight (a756ff7)
  • OptimizeLorentzianTrain and OptimizeGaussianTrain (1650873)
  • update peak-shape-generator (a76be80)

Bug Fixes

0.3.2 (2020-11-04)

Bug Fixes

  • update dependencies to fix web compatibility (2113eef)

0.3.1 (2020-11-04)

Bug Fixes

  • add missing build script (42e4ce3)

0.3.0 (2020-11-03)

Features

  • optimize gaussian, lorentzian or pseudovoight (a756ff7)

Bug Fixes

0.2.1 (2020-10-15)

0.2.0 (2020-05-19)

0.1.5 (2020-05-06)

Bug Fixes

0.1.4 (2016-08-16)