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

ml-peak-shape-generator

Package Overview
Dependencies
Maintainers
5
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ml-peak-shape-generator - npm Package Compare versions

Comparing version 0.11.0 to 0.12.0

src/util/erfinv.js

13

CHANGELOG.md
# Changelog
## [0.12.0](https://www.github.com/mljs/peak-shape-generator/compare/v0.11.0...v0.12.0) (2020-12-11)
### Features
* add fwhmToWidth, widthToFWHM and getFactor static function for each kind of shape ([0bc6807](https://www.github.com/mljs/peak-shape-generator/commit/0bc6807da60d3777d80a751a0220f2ad3fd18ae3))
* implement internal erfinv code ([13b1e7a](https://www.github.com/mljs/peak-shape-generator/commit/13b1e7a328f3b1d713da4d0625463b89feb9135b))
### Bug Fixes
* avoid a big factor for mu=1 in pseudoVoigt shape ([20a79a1](https://www.github.com/mljs/peak-shape-generator/commit/20a79a136341f0ee59c8daff5bd1cbcfde10cbc5)), closes [#35](https://www.github.com/mljs/peak-shape-generator/issues/35)
## [0.11.0](https://www.github.com/mljs/peak-shape-generator/compare/v0.10.2...v0.11.0) (2020-12-10)

@@ -4,0 +17,0 @@

157

lib/index.js

@@ -5,8 +5,2 @@ 'use strict';

var erfinv = require('compute-erfinv');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var erfinv__default = /*#__PURE__*/_interopDefaultLegacy(erfinv);
const GAUSSIAN_EXP_FACTOR = -4 * Math.LN2;

@@ -18,2 +12,17 @@ const ROOT_PI_OVER_LN2 = Math.sqrt(Math.PI / Math.LN2);

// https://en.wikipedia.org/wiki/Error_function#Inverse_functions
// This code yields to a good approximation
// If needed a better implementation using polynomial can be found on https://en.wikipedia.org/wiki/Error_function#Inverse_functions
function erfinv(x) {
let a = 0.147;
if (x === 0) return 0;
let ln1MinusXSqrd = Math.log(1 - x * x);
let lnEtcBy2Plus2 = ln1MinusXSqrd / 2 + 2 / (Math.PI * a);
let firstSqrt = Math.sqrt(lnEtcBy2Plus2 ** 2 - ln1MinusXSqrd / a);
let secondSqrt = Math.sqrt(firstSqrt - lnEtcBy2Plus2);
return secondSqrt * (x > 0 ? 1 : -1);
}
class Gaussian {

@@ -78,3 +87,3 @@ /**

getFactor(area = 0.9999) {
return Math.sqrt(2) * erfinv__default['default'](area);
return Gaussian.getFactor(area);
}

@@ -92,2 +101,23 @@

/**
* Compute the value of Full Width at Half Maximum (FWHM) from the width between the inflection points.
* //https://mathworld.wolfram.com/GaussianFunction.html
* @param {number} width - Width between the inflection points
* @returns {number} fwhm
*/
widthToFWHM(width) {
//https://mathworld.wolfram.com/GaussianFunction.html
return Gaussian.widthToFWHM(width);
}
/**
* Compute the value of width between the inflection points from Full Width at Half Maximum (FWHM).
* //https://mathworld.wolfram.com/GaussianFunction.html
* @param {number} fwhm - Full Width at Half Maximum.
* @returns {number} width
*/
fwhmToWidth(fwhm = this.fwhm) {
return Gaussian.fwhmToWidth(fwhm);
}
/**
* set a new full width at half maximum

@@ -120,9 +150,8 @@ * @param {number} fwhm - full width at half maximum

/**
* Compute the value of Full Width at Half Maximum (FWHM) of a specific shape from the width between the inflection points.
* Compute the value of Full Width at Half Maximum (FWHM) from the width between the inflection points.
* //https://mathworld.wolfram.com/GaussianFunction.html
* @param {number} width - Width between the inflection points
* @returns {number} fwhm
*/
Gaussian.widthToFWHM = function widthToFWHM(width) {
//https://mathworld.wolfram.com/GaussianFunction.html
return width * ROOT_2LN2;

@@ -132,3 +161,4 @@ };

/**
* Compute the value of width between the inflection points of a specific shape from Full Width at Half Maximum (FWHM).
* Compute the value of width between the inflection points from Full Width at Half Maximum (FWHM).
* //https://mathworld.wolfram.com/GaussianFunction.html
* @param {number} fwhm - Full Width at Half Maximum.

@@ -149,3 +179,3 @@ * @returns {number} width

Gaussian.getArea = function (fwhm, options = {}) {
Gaussian.getArea = function getArea(fwhm, options = {}) {
let { height = 1 } = options;

@@ -155,2 +185,11 @@ return (height * ROOT_PI_OVER_LN2 * fwhm) / 2;

/**
* Calculate the number of times FWHM allows to reach a specific area coverage.
* @param {number} [area=0.9999]
* @returns {number}
*/
Gaussian.getFactor = function getFactor(area = 0.9999) {
return Math.sqrt(2) * erfinv(area);
};
class Lorentzian {

@@ -207,3 +246,3 @@ /**

getFactor(area = 0.9999) {
return 2 * Math.tan(Math.PI * (area - 0.5));
return Lorentzian.getFactor(area);
}

@@ -221,2 +260,21 @@

/**
* Compute the value of width between the inflection points of a specific shape from Full Width at Half Maximum (FWHM).
* //https://mathworld.wolfram.com/LorentzianFunction.html
* @param {number} [fwhm] - Full Width at Half Maximum.
* @returns {number} width between the inflection points
*/
fwhmToWidth(fwhm = this.fwhm) {
return Lorentzian.fwhmToWidth(fwhm);
}
/**
* Compute the value of Full Width at Half Maximum (FWHM) of a specific shape from the width between the inflection points.
* //https://mathworld.wolfram.com/LorentzianFunction.html
* @param {number} [width] Width between the inflection points
* @returns {number} fwhm
*/
widthToFWHM(width) {
return Lorentzian.widthToFWHM(width);
}
/**
* set a new full width at half maximum

@@ -251,2 +309,3 @@ * @param {number} fwhm - full width at half maximum

* Compute the value of width between the inflection points of a specific shape from Full Width at Half Maximum (FWHM).
* //https://mathworld.wolfram.com/LorentzianFunction.html
* @param {number} [fwhm] - Full Width at Half Maximum.

@@ -261,2 +320,3 @@ * @returns {number} width between the inflection points

* Compute the value of Full Width at Half Maximum (FWHM) of a specific shape from the width between the inflection points.
* //https://mathworld.wolfram.com/LorentzianFunction.html
* @param {number} [width] Width between the inflection points

@@ -266,3 +326,2 @@ * @returns {number} fwhm

Lorentzian.widthToFWHM = function widthToFWHM(width) {
//https://mathworld.wolfram.com/LorentzianFunction.html
return width * ROOT_THREE;

@@ -278,3 +337,3 @@ };

*/
Lorentzian.getArea = function (fwhm, options = {}) {
Lorentzian.getArea = function getArea(fwhm, options = {}) {
let { height = 1 } = options;

@@ -285,2 +344,11 @@

/**
* Calculate the number of times FWHM allows to reach a specific area coverage
* @param {number} [area=0.9999]
* @returns {number}
*/
Lorentzian.getFactor = function getFactor(area = 0.9999) {
return 2 * Math.tan(Math.PI * (area - 0.5));
};
class PseudoVoigt {

@@ -315,3 +383,2 @@ /**

let { length, factor = this.getFactor() } = options;
if (!length) {

@@ -345,7 +412,8 @@ length = Math.ceil(this.fwhm * factor);

* Calculate the number of times FWHM allows to reach a specific area coverage
* @param {number} [area=0.9999]
* @param {number} [area=0.9999] - required area to be coverage
* @param {number} [mu=this.mu] - ratio of gaussian contribution.
* @returns {number}
*/
getFactor(area = 0.9999) {
return 2 * Math.tan(Math.PI * (area - 0.5));
getFactor(area = 0.9999, mu = this.mu) {
return PseudoVoigt.getFactor(area, mu);
}

@@ -357,3 +425,2 @@

*/
getArea() {

@@ -364,2 +431,21 @@ return PseudoVoigt.getArea(this.fwhm, { height: this.height, mu: this.mu });

/**
* Compute the value of Full Width at Half Maximum (FMHM) from width between the inflection points.
* @param {number} width - width between the inflection points
* @param {number} [mu = 0.5] - ratio of gaussian contribution.
* @returns {number} Full Width at Half Maximum (FMHM).
*/
widthToFWHM(width, mu) {
return PseudoVoigt.widthToFWHM(width, mu);
}
/**
* Compute the value of width between the inflection points from Full Width at Half Maximum (FWHM).
* @param {number} fwhm - Full Width at Half Maximum.
* @param {number} [mu] - ratio of gaussian contribution.
* @returns {number} width between the inflection points.
*/
fwhmToWidth(fwhm = this.fwhm, mu = this.mu) {
return PseudoVoigt.fwhmToWidth(fwhm, mu);
}
/**
* set a new full width at half maximum

@@ -401,8 +487,18 @@ * @param {number} fwhm - full width at half maximum

/**
* Compute the value of width between the inflection points of a specific shape from Full Width at Half Maximum (FWHM).
* Compute the value of Full Width at Half Maximum (FMHM) from width between the inflection points.
* @param {number} width - width between the inflection points
* @param {number} [mu = 0.5] - ratio of gaussian contribution.
* @returns {number} Full Width at Half Maximum (FMHM).
*/
PseudoVoigt.widthToFWHM = function widthToFWHM(width, mu = 0.5) {
return width * (mu * ROOT_2LN2_MINUS_ONE + 1);
};
/**
* Compute the value of width between the inflection points from Full Width at Half Maximum (FWHM).
* @param {number} fwhm - Full Width at Half Maximum.
* @returns {number} width between the inflection points
* @param {number} [mu = 0.5] - ratio of gaussian contribution.
* @returns {number} width between the inflection points.
*/
PseudoVoigt.fwhmToWidth = function fwhmToWidth(fwhm) {
return fwhm / (this.mu * ROOT_2LN2_MINUS_ONE + 1);
PseudoVoigt.fwhmToWidth = function fwhmToWidth(fwhm, mu = 0.5) {
return fwhm / (mu * ROOT_2LN2_MINUS_ONE + 1);
};

@@ -418,8 +514,17 @@

*/
PseudoVoigt.getArea = function (fwhm, options = {}) {
PseudoVoigt.getArea = function getArea(fwhm, options = {}) {
let { height = 1, mu = 0.5 } = options;
return (fwhm * height * (mu * ROOT_PI_OVER_LN2 + (1 - mu) * Math.PI)) / 2;
};
/**
* Calculate the number of times FWHM allows to reach a specific area coverage
* @param {number} [area=0.9999] - required area to be coverage
* @param {number} [mu=this.mu] - ratio of gaussian contribution.
* @returns {number}
*/
PseudoVoigt.getFactor = function getFactor(area = 0.9999, mu = 0.5) {
return mu < 1 ? Lorentzian.getFactor(area) : Gaussian.getFactor(area);
};
function getShapeGenerator(options) {

@@ -426,0 +531,0 @@ let { kind = 'Gaussian', options: shapeOptions } = options;

6

package.json
{
"name": "ml-peak-shape-generator",
"version": "0.11.0",
"version": "0.12.0",
"description": "",

@@ -43,2 +43,3 @@ "main": "lib/index.js",

"@babel/plugin-transform-modules-commonjs": "^7.12.1",
"compute-erfinv": "^3.0.1",
"eslint": "^7.15.0",

@@ -54,6 +55,3 @@ "eslint-config-cheminfo": "^5.2.2",

"rollup": "^2.34.2"
},
"dependencies": {
"compute-erfinv": "^3.0.1"
}
}

@@ -27,21 +27,21 @@ # ml-peak-shape-generator

```js
import { gaussian, lorentzian, pseudoVoigt} from 'ml-peak-shape-generator';
import { Gaussian, Lorentzian, PseudoVoigt} from 'ml-peak-shape-generator';
// It's possible to specify the windows size with factor option
let {data, fwhm} = gaussian({factor: 3.5, sd: 500});
let data = new Gaussian({factor: 3.5, sd: 500}).getData();
// or fix the number of points as Full Width at Half Maximum
let {data, fwhm} = gaussian({factor: 3.5, fwhm: 500});
let data = new Gaussian({factor: 3.5, fwhm: 500}).getData();
// It's possible to specify the windows size with factor option
let {data, fwhm} = loretzian({factor: 5, fwhm: 500});
let data = new Loretzian({factor: 5, fwhm: 500}).getData();
// It's possible to specify the windows size with factor option
let {data, fwhm} = pseudoVoigt({{factor: 5, fwhm: 500}});
let data = new PseudoVoigt({{factor: 5, fwhm: 500}}).getData();
```
```js
import { getShape, GAUSSIAN, LORENTZIAN, PSEUDO_VOIGT} from 'ml-peak-shape-generator';
import { getShapeGenerator } from 'ml-peak-shape-generator';
// If you want to dynamically select a shape you can use the `getShape` method.
let {data, fwhm} = getShape(LORENTZIAN, {factor: 3.5, sd: 500});
// If you want to dynamically select a shape you can use the `getShapeGenerator` method. It returns a instance of required kind of shape.
let shapeGenerator = getShapeGenerator('lorentzian', {factor: 3.5, sd: 500});

@@ -53,24 +53,10 @@ ```

```js
import { gaussianFct} from 'ml-peak-shape-generator';
const func = gaussianFct({ x: 0, y: 2, width: 0.2 });
import { Gaussian } from 'ml-peak-shape-generator';
const func = Gaussian.fct(x - mean, fwhm);
```
You can create a class as well and add many peaks
```js
const peakShapeGenerator = new PeakShapeGenerator({cacheSize:20});
let shape = peakShapeGenerator.getShape(GAUSSIAN, { fwhm: 500 });
let shape2 = peakShapeGenerator.getShape(GAUSSIAN, { fwhm: 500 });
// shape===shape2 true
let shape3 = peakShapeGenerator.getShape(GAUSSIAN, { fwhm: 100 });
// shape===shape3 false
let shape4 = peakShapeGenerator.getShape(GAUSSIAN, { fwhm: 100 });
// shape3===shape4 true
```
## [API Documentation](https://mljs.github.io/peak-shape-generator/)

@@ -77,0 +63,0 @@

@@ -1,3 +0,1 @@

import erfinv from 'compute-erfinv';
import {

@@ -8,2 +6,3 @@ ROOT_2LN2,

} from '../util/constants';
import erfinv from '../util/erfinv';

@@ -69,3 +68,3 @@ export class Gaussian {

getFactor(area = 0.9999) {
return Math.sqrt(2) * erfinv(area);
return Gaussian.getFactor(area);
}

@@ -83,2 +82,23 @@

/**
* Compute the value of Full Width at Half Maximum (FWHM) from the width between the inflection points.
* //https://mathworld.wolfram.com/GaussianFunction.html
* @param {number} width - Width between the inflection points
* @returns {number} fwhm
*/
widthToFWHM(width) {
//https://mathworld.wolfram.com/GaussianFunction.html
return Gaussian.widthToFWHM(width);
}
/**
* Compute the value of width between the inflection points from Full Width at Half Maximum (FWHM).
* //https://mathworld.wolfram.com/GaussianFunction.html
* @param {number} fwhm - Full Width at Half Maximum.
* @returns {number} width
*/
fwhmToWidth(fwhm = this.fwhm) {
return Gaussian.fwhmToWidth(fwhm);
}
/**
* set a new full width at half maximum

@@ -111,9 +131,8 @@ * @param {number} fwhm - full width at half maximum

/**
* Compute the value of Full Width at Half Maximum (FWHM) of a specific shape from the width between the inflection points.
* Compute the value of Full Width at Half Maximum (FWHM) from the width between the inflection points.
* //https://mathworld.wolfram.com/GaussianFunction.html
* @param {number} width - Width between the inflection points
* @returns {number} fwhm
*/
Gaussian.widthToFWHM = function widthToFWHM(width) {
//https://mathworld.wolfram.com/GaussianFunction.html
return width * ROOT_2LN2;

@@ -123,3 +142,4 @@ };

/**
* Compute the value of width between the inflection points of a specific shape from Full Width at Half Maximum (FWHM).
* Compute the value of width between the inflection points from Full Width at Half Maximum (FWHM).
* //https://mathworld.wolfram.com/GaussianFunction.html
* @param {number} fwhm - Full Width at Half Maximum.

@@ -140,5 +160,14 @@ * @returns {number} width

Gaussian.getArea = function (fwhm, options = {}) {
Gaussian.getArea = function getArea(fwhm, options = {}) {
let { height = 1 } = options;
return (height * ROOT_PI_OVER_LN2 * fwhm) / 2;
};
/**
* Calculate the number of times FWHM allows to reach a specific area coverage.
* @param {number} [area=0.9999]
* @returns {number}
*/
Gaussian.getFactor = function getFactor(area = 0.9999) {
return Math.sqrt(2) * erfinv(area);
};

@@ -54,3 +54,3 @@ import { ROOT_THREE } from '../util/constants';

getFactor(area = 0.9999) {
return 2 * Math.tan(Math.PI * (area - 0.5));
return Lorentzian.getFactor(area);
}

@@ -68,2 +68,21 @@

/**
* Compute the value of width between the inflection points of a specific shape from Full Width at Half Maximum (FWHM).
* //https://mathworld.wolfram.com/LorentzianFunction.html
* @param {number} [fwhm] - Full Width at Half Maximum.
* @returns {number} width between the inflection points
*/
fwhmToWidth(fwhm = this.fwhm) {
return Lorentzian.fwhmToWidth(fwhm);
}
/**
* Compute the value of Full Width at Half Maximum (FWHM) of a specific shape from the width between the inflection points.
* //https://mathworld.wolfram.com/LorentzianFunction.html
* @param {number} [width] Width between the inflection points
* @returns {number} fwhm
*/
widthToFWHM(width) {
return Lorentzian.widthToFWHM(width);
}
/**
* set a new full width at half maximum

@@ -98,2 +117,3 @@ * @param {number} fwhm - full width at half maximum

* Compute the value of width between the inflection points of a specific shape from Full Width at Half Maximum (FWHM).
* //https://mathworld.wolfram.com/LorentzianFunction.html
* @param {number} [fwhm] - Full Width at Half Maximum.

@@ -108,2 +128,3 @@ * @returns {number} width between the inflection points

* Compute the value of Full Width at Half Maximum (FWHM) of a specific shape from the width between the inflection points.
* //https://mathworld.wolfram.com/LorentzianFunction.html
* @param {number} [width] Width between the inflection points

@@ -113,3 +134,2 @@ * @returns {number} fwhm

Lorentzian.widthToFWHM = function widthToFWHM(width) {
//https://mathworld.wolfram.com/LorentzianFunction.html
return width * ROOT_THREE;

@@ -125,3 +145,3 @@ };

*/
Lorentzian.getArea = function (fwhm, options = {}) {
Lorentzian.getArea = function getArea(fwhm, options = {}) {
let { height = 1 } = options;

@@ -131,1 +151,10 @@

};
/**
* Calculate the number of times FWHM allows to reach a specific area coverage
* @param {number} [area=0.9999]
* @returns {number}
*/
Lorentzian.getFactor = function getFactor(area = 0.9999) {
return 2 * Math.tan(Math.PI * (area - 0.5));
};

@@ -39,3 +39,2 @@ import {

let { length, factor = this.getFactor() } = options;
if (!length) {

@@ -69,7 +68,8 @@ length = Math.ceil(this.fwhm * factor);

* Calculate the number of times FWHM allows to reach a specific area coverage
* @param {number} [area=0.9999]
* @param {number} [area=0.9999] - required area to be coverage
* @param {number} [mu=this.mu] - ratio of gaussian contribution.
* @returns {number}
*/
getFactor(area = 0.9999) {
return 2 * Math.tan(Math.PI * (area - 0.5));
getFactor(area = 0.9999, mu = this.mu) {
return PseudoVoigt.getFactor(area, mu);
}

@@ -81,3 +81,2 @@

*/
getArea() {

@@ -88,2 +87,21 @@ return PseudoVoigt.getArea(this.fwhm, { height: this.height, mu: this.mu });

/**
* Compute the value of Full Width at Half Maximum (FMHM) from width between the inflection points.
* @param {number} width - width between the inflection points
* @param {number} [mu = 0.5] - ratio of gaussian contribution.
* @returns {number} Full Width at Half Maximum (FMHM).
*/
widthToFWHM(width, mu) {
return PseudoVoigt.widthToFWHM(width, mu);
}
/**
* Compute the value of width between the inflection points from Full Width at Half Maximum (FWHM).
* @param {number} fwhm - Full Width at Half Maximum.
* @param {number} [mu] - ratio of gaussian contribution.
* @returns {number} width between the inflection points.
*/
fwhmToWidth(fwhm = this.fwhm, mu = this.mu) {
return PseudoVoigt.fwhmToWidth(fwhm, mu);
}
/**
* set a new full width at half maximum

@@ -125,8 +143,18 @@ * @param {number} fwhm - full width at half maximum

/**
* Compute the value of width between the inflection points of a specific shape from Full Width at Half Maximum (FWHM).
* Compute the value of Full Width at Half Maximum (FMHM) from width between the inflection points.
* @param {number} width - width between the inflection points
* @param {number} [mu = 0.5] - ratio of gaussian contribution.
* @returns {number} Full Width at Half Maximum (FMHM).
*/
PseudoVoigt.widthToFWHM = function widthToFWHM(width, mu = 0.5) {
return width * (mu * ROOT_2LN2_MINUS_ONE + 1);
};
/**
* Compute the value of width between the inflection points from Full Width at Half Maximum (FWHM).
* @param {number} fwhm - Full Width at Half Maximum.
* @returns {number} width between the inflection points
* @param {number} [mu = 0.5] - ratio of gaussian contribution.
* @returns {number} width between the inflection points.
*/
PseudoVoigt.fwhmToWidth = function fwhmToWidth(fwhm) {
return fwhm / (this.mu * ROOT_2LN2_MINUS_ONE + 1);
PseudoVoigt.fwhmToWidth = function fwhmToWidth(fwhm, mu = 0.5) {
return fwhm / (mu * ROOT_2LN2_MINUS_ONE + 1);
};

@@ -142,6 +170,15 @@

*/
PseudoVoigt.getArea = function (fwhm, options = {}) {
PseudoVoigt.getArea = function getArea(fwhm, options = {}) {
let { height = 1, mu = 0.5 } = options;
return (fwhm * height * (mu * ROOT_PI_OVER_LN2 + (1 - mu) * Math.PI)) / 2;
};
/**
* Calculate the number of times FWHM allows to reach a specific area coverage
* @param {number} [area=0.9999] - required area to be coverage
* @param {number} [mu=this.mu] - ratio of gaussian contribution.
* @returns {number}
*/
PseudoVoigt.getFactor = function getFactor(area = 0.9999, mu = 0.5) {
return mu < 1 ? Lorentzian.getFactor(area) : Gaussian.getFactor(area);
};
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