Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ml-spectra-fitting

Package Overview
Dependencies
Maintainers
4
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ml-spectra-fitting

Fit spectra using guassian or lorentzian

  • 0.3.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.4K
increased by21.8%
Maintainers
4
Weekly downloads
 
Created
Source

NPM version build status npm download

ml-spectra-fitting

Curve fitting method in javascript.

This is spectra fitting package optimize the position (x), max intensity (y), full width at half maximum (width) and the percent of gaussian (mu). It supports three kind of shapes:

NameEquation
Gaussian
Lorentzian
Pseudo Voigt

where

It is a wrapper of ml-levenberg-marquardt

API Documentation

Installation

$ npm install ml-spectra-fitting

Example

// import library
import { optimizeSum } from 'ml-spectra-fitting';
import { generateSpectrum } from 'spectrum-generator';

const peaks = [
  { x: 0.5, y: 0.2, width: 0.2 },
  { x: -0.5, y: 0.2, width: 0.3 },
];
const data = generateSpectrum(peaks, {from: -1, to: 1, nbPoints: 41});


//the approximate values to be optimized, It could come from a peak picking with ml-gsd
let peakList = [
  {
    x: -0.5,
    y: 0.18,
    width: 0.18,
  },
  {
    x: 0.52,
    y: 0.17,
    width: 0.37,
  },
];

// the function recive a peaklist with {x, y, width} as a guess
// and return a list of objects

let fittedParams = optimize(data, peakList);
console.log(fittedParams);
/**
 {
    error: 0.010502794375558983,
    iterations: 15,
    peaks: [
      {
        x: -0.49999760133593774,
        y: 0.1999880261075537,
        width: 0.3000369491704072
      },
      {
        x: 0.5000084944744884,
        y: 0.20004144804853427,
        width: 0.1999731186595336
      }
    ]
  }
 */

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 }, { width: 0.2 });
generator.addPeak(
  { x: -0.5, y: 0.2 },
  {
    width: 0.1,
    shape: {
      kind: 'lorentzian',
      options: {
        fwhm: 1000,
        length: 50001,
        factor: 5
      },
    },
  },
);

//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 peakList = [
  {
    x: -0.5,
    y: 0.22,
    width: 0.25,
  },
  {
    x: 0.52,
    y: 0.18,
    width: 0.18,
  },
];

// the function recive a peaklist with {x, y, width} as a guess
// and return a list of objects
let fittedParams = optimize(data, peakList, { kind: 'pseudovoigt' });

console.log(fittedParams);
/**
{
  error: 0.12361588652854476,
  iterations: 100,
  peaks: [
    {
      x: -0.5000014532421942,
      y: 0.19995307937326137,
      width: 0.10007670374735196,
      mu: 0.004731136777288483
    },
    {
      x: 0.5001051783652894,
      y: 0.19960010175400406,
      width: 0.19935932346969124,
      mu: 1
    }
  ]
}
*/

License

MIT

Keywords

FAQs

Package last updated on 03 Nov 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc