ml-spectra-fitting
Advanced tools
Comparing version 1.0.0 to 2.0.0
# Changelog | ||
## [2.0.0](https://www.github.com/mljs/spectra-fitting/compare/v1.0.0...v2.0.0) (2021-10-11) | ||
### ⚠ BREAKING CHANGES | ||
* homogenize and generalize output (#58) | ||
### Features | ||
* fixing wrong order in object assignment. close [#52](https://www.github.com/mljs/spectra-fitting/issues/52) ([#53](https://www.github.com/mljs/spectra-fitting/issues/53)) ([ce3d47f](https://www.github.com/mljs/spectra-fitting/commit/ce3d47f4a9f8da60a58c030a98ac5acf5fda3fa0)) | ||
* homogenize and generalize output ([#58](https://www.github.com/mljs/spectra-fitting/issues/58)) ([1b3970c](https://www.github.com/mljs/spectra-fitting/commit/1b3970c992f45e5c497507b8b0fa500464d73387)) | ||
* update ml-peak-shape-generator to 2.0.1 ([#54](https://www.github.com/mljs/spectra-fitting/issues/54)) ([73782cc](https://www.github.com/mljs/spectra-fitting/commit/73782cc0a5291f0dd7b618861164f3881e89014a)) | ||
## [1.0.0](https://www.github.com/mljs/spectra-fitting/compare/v0.13.0...v1.0.0) (2021-03-24) | ||
@@ -4,0 +17,0 @@ |
108
lib/index.js
@@ -5,3 +5,2 @@ 'use strict'; | ||
var assignDeep = require('assign-deep'); | ||
var getMaxValue = require('ml-array-max'); | ||
@@ -13,3 +12,2 @@ var mlPeakShapeGenerator = require('ml-peak-shape-generator'); | ||
var assignDeep__default = /*#__PURE__*/_interopDefaultLegacy(assignDeep); | ||
var getMaxValue__default = /*#__PURE__*/_interopDefaultLegacy(getMaxValue); | ||
@@ -26,4 +24,6 @@ var LM__default = /*#__PURE__*/_interopDefaultLegacy(LM); | ||
const { fct: pseudoVoigtFct } = mlPeakShapeGenerator.pseudoVoigt; | ||
function sumOfGaussianLorentzians(p) { | ||
return function (t) { | ||
return (t) => { | ||
let nL = p.length / 4; | ||
@@ -33,3 +33,3 @@ let result = 0; | ||
result += | ||
p[i + nL] * mlPeakShapeGenerator.PseudoVoigt.fct(t - p[i], p[i + nL * 2], p[i + nL * 3]); | ||
p[i + nL] * pseudoVoigtFct(t - p[i], p[i + nL * 2], p[i + nL * 3]); | ||
} | ||
@@ -48,8 +48,10 @@ return result; | ||
const { fct: gaussianFct } = mlPeakShapeGenerator.gaussian; | ||
function sumOfGaussians(p) { | ||
return function (t) { | ||
return (t) => { | ||
let nL = p.length / 3; | ||
let result = 0; | ||
for (let i = 0; i < nL; i++) { | ||
result += p[i + nL] * mlPeakShapeGenerator.Gaussian.fct(t - p[i], p[i + nL * 2]); | ||
result += p[i + nL] * gaussianFct(t - p[i], p[i + nL * 2]); | ||
} | ||
@@ -68,8 +70,10 @@ return result; | ||
const { fct: lorentzianFct } = mlPeakShapeGenerator.lorentzian; | ||
function sumOfLorentzians(p) { | ||
return function (t) { | ||
return (t) => { | ||
let nL = p.length / 3; | ||
let result = 0; | ||
for (let i = 0; i < nL; i++) { | ||
result += p[i + nL] * mlPeakShapeGenerator.Lorentzian.fct(t - p[i], p[i + nL * 2]); | ||
result += p[i + nL] * lorentzianFct(t - p[i], p[i + nL * 2]); | ||
} | ||
@@ -80,2 +84,31 @@ return result; | ||
const isValidKey = (key) => { | ||
return key !== '__proto__' && key !== 'constructor' && key !== 'prototype'; | ||
}; | ||
const isObject = (val) => { | ||
return typeof val === 'object'; | ||
}; | ||
const isPrimitive = (val) => { | ||
return typeof val === 'object' ? val === null : typeof val !== 'function'; | ||
}; | ||
function assignDeep(target, ...args) { | ||
let index = 0; | ||
if (isPrimitive(target)) target = args[index++]; | ||
if (!target) target = {}; | ||
for (; index < args.length; index++) { | ||
if (!isObject(args[index])) continue; | ||
for (const key in args[index]) { | ||
if (!isValidKey(key)) continue; | ||
if (isObject(target[key]) && isObject(args[index][key])) { | ||
assignDeep(target[key], args[index][key]); | ||
} else { | ||
target[key] = args[index][key]; | ||
} | ||
} | ||
} | ||
return target; | ||
} | ||
function checkInput(data, peaks, options) { | ||
@@ -103,5 +136,5 @@ let { | ||
init: (peak) => peak.x, | ||
max: (peak) => peak.x + peak.width * 2, | ||
min: (peak) => peak.x - peak.width * 2, | ||
gradientDifference: (peak) => peak.width * 2e-3, | ||
max: (peak) => peak.x + peak.shape.width * 2, | ||
min: (peak) => peak.x - peak.shape.width * 2, | ||
gradientDifference: (peak) => peak.shape.width * 2e-3, | ||
}, | ||
@@ -115,6 +148,6 @@ y: { | ||
width: { | ||
init: (peak) => peak.width, | ||
max: (peak) => peak.width * 4, | ||
min: (peak) => peak.width * 0.25, | ||
gradientDifference: (peak) => peak.width * 2e-3, | ||
init: (peak) => peak.shape.width, | ||
max: (peak) => peak.shape.width * 4, | ||
min: (peak) => peak.shape.width * 0.25, | ||
gradientDifference: (peak) => peak.shape.width * 2e-3, | ||
}, | ||
@@ -128,5 +161,5 @@ }; | ||
init: (peak) => peak.x, | ||
max: (peak) => peak.x + peak.width * 2, | ||
min: (peak) => peak.x - peak.width * 2, | ||
gradientDifference: (peak) => peak.width * 2e-3, | ||
max: (peak) => peak.x + peak.shape.width * 2, | ||
min: (peak) => peak.x - peak.shape.width * 2, | ||
gradientDifference: (peak) => peak.shape.width * 2e-3, | ||
}, | ||
@@ -140,6 +173,6 @@ y: { | ||
width: { | ||
init: (peak) => peak.width, | ||
max: (peak) => peak.width * 4, | ||
min: (peak) => peak.width * 0.25, | ||
gradientDifference: (peak) => peak.width * 2e-3, | ||
init: (peak) => peak.shape.width, | ||
max: (peak) => peak.shape.width * 4, | ||
min: (peak) => peak.shape.width * 0.25, | ||
gradientDifference: (peak) => peak.shape.width * 2e-3, | ||
}, | ||
@@ -153,5 +186,5 @@ }; | ||
init: (peak) => peak.x, | ||
max: (peak) => peak.x + peak.width * 2, | ||
min: (peak) => peak.x - peak.width * 2, | ||
gradientDifference: (peak) => peak.width * 2e-3, | ||
max: (peak) => peak.x + peak.shape.width * 2, | ||
min: (peak) => peak.x - peak.shape.width * 2, | ||
gradientDifference: (peak) => peak.shape.width * 2e-3, | ||
}, | ||
@@ -165,9 +198,9 @@ y: { | ||
width: { | ||
init: (peak) => peak.width, | ||
max: (peak) => peak.width * 4, | ||
min: (peak) => peak.width * 0.25, | ||
gradientDifference: (peak) => peak.width * 2e-3, | ||
init: (peak) => peak.shape.width, | ||
max: (peak) => peak.shape.width * 4, | ||
min: (peak) => peak.shape.width * 0.25, | ||
gradientDifference: (peak) => peak.shape.width * 2e-3, | ||
}, | ||
mu: { | ||
init: (peak) => (peak.mu !== undefined ? peak.mu : 0.5), | ||
init: (peak) => (peak.shape.mu !== undefined ? peak.shape.mu : 0.5), | ||
min: () => 0, | ||
@@ -184,3 +217,3 @@ max: () => 1, | ||
let x = data.x; | ||
let maxY = getMaxValue__default['default'](data.y); | ||
let maxY = getMaxValue__default["default"](data.y); | ||
let y = new Array(x.length); | ||
@@ -195,3 +228,3 @@ for (let i = 0; i < x.length; i++) { | ||
let parameters = assignDeep__default['default']({}, optimization.parameters, defaultParameters); | ||
let parameters = assignDeep({}, defaultParameters, optimization.parameters); | ||
@@ -238,3 +271,3 @@ for (let key in parameters) { | ||
return { | ||
algorithm: LM__default['default'], | ||
algorithm: LM__default["default"], | ||
optimizationOptions: checkOptions(kind, options), | ||
@@ -358,6 +391,11 @@ }; | ||
for (let i = 0; i < nbShapes; i++) { | ||
pFit.parameterValues[i + nbShapes] *= maxY; | ||
for (let k = 0; k < parameterKey.length; k++) { | ||
const key = parameterKey[k]; | ||
const value = pFit.parameterValues[i + k * nbShapes]; | ||
// we modify the optimized parameters | ||
peaks[i][parameterKey[k]] = pFit.parameterValues[i + k * nbShapes]; | ||
if (key === 'x' || key === 'y') { | ||
peaks[i][parameterKey[k]] = key === 'y' ? value * maxY : value; | ||
} else { | ||
peaks[i].shape[parameterKey[k]] = value; | ||
} | ||
} | ||
@@ -364,0 +402,0 @@ } |
{ | ||
"name": "ml-spectra-fitting", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "Fit spectra using gaussian or lorentzian", | ||
@@ -18,2 +18,4 @@ "main": "lib/index.js", | ||
"test": "npm run test-coverage && npm run eslint", | ||
"prettier": "prettier --check src", | ||
"prettier-write": "prettier --write src", | ||
"test-coverage": "jest --coverage", | ||
@@ -34,2 +36,5 @@ "test-only": "jest", | ||
"author": "Andres Castillo", | ||
"contributors": [ | ||
"J. Alejandro Bolaños A. <jobo322>" | ||
], | ||
"license": "MIT", | ||
@@ -51,19 +56,18 @@ "bugs": { | ||
"devDependencies": { | ||
"@babel/plugin-transform-modules-commonjs": "^7.13.8", | ||
"@types/jest": "^26.0.21", | ||
"cheminfo-build": "^1.1.10", | ||
"eslint": "^7.22.0", | ||
"eslint-config-cheminfo": "^5.2.3", | ||
"@babel/plugin-transform-modules-commonjs": "^7.15.4", | ||
"@types/jest": "^27.0.2", | ||
"cheminfo-build": "^1.1.11", | ||
"eslint": "^7.32.0", | ||
"eslint-config-cheminfo": "^5.6.0", | ||
"esm": "^3.2.25", | ||
"jest": "^26.6.3", | ||
"prettier": "^2.2.1", | ||
"rollup": "^2.42.3", | ||
"spectrum-generator": "^4.7.0" | ||
"jest": "^27.2.5", | ||
"prettier": "^2.4.1", | ||
"rollup": "^2.58.0", | ||
"spectrum-generator": "^5.4.0" | ||
}, | ||
"dependencies": { | ||
"assign-deep": "^1.0.1", | ||
"ml-array-max": "^1.2.2", | ||
"ml-levenberg-marquardt": "^3.1.0", | ||
"ml-peak-shape-generator": "^1.0.0" | ||
"ml-array-max": "^1.2.3", | ||
"ml-levenberg-marquardt": "^3.1.1", | ||
"ml-peak-shape-generator": "^2.0.2" | ||
} | ||
} |
@@ -13,4 +13,4 @@ import { optimize } from '../index'; | ||
describe('Optimize sum of Lorentzian', function () { | ||
it('group of two GL', function () { | ||
describe('Optimize sum of Lorentzian', () => { | ||
it('group of two GL', () => { | ||
let pTrue = [-0.5, 0.5, 0.001, 0.001, 0.31, 0.31]; | ||
@@ -21,4 +21,4 @@ let yData = sumOfLorentzians(pTrue); | ||
[ | ||
{ x: -0.5, y: 0.0009, width: (xFactor * nbPoints) / 8 }, | ||
{ x: 0.52, y: 0.0009, width: (xFactor * nbPoints) / 8 }, | ||
{ x: -0.5, y: 0.0009, shape: { width: (xFactor * nbPoints) / 8 } }, | ||
{ x: 0.52, y: 0.0009, shape: { width: (xFactor * nbPoints) / 8 } }, | ||
], | ||
@@ -32,3 +32,3 @@ { shape: { kind: 'lorentzian' } }, | ||
expect(pFit.y).toBeCloseTo(pTrue[i + nL], 2); | ||
expect(pFit.width).toBeCloseTo(pTrue[i + nL * 2], 2); | ||
expect(pFit.shape.width).toBeCloseTo(pTrue[i + nL * 2], 2); | ||
} | ||
@@ -38,4 +38,4 @@ }); | ||
describe('Optimize sum of Gaussians', function () { | ||
it('group of two GL', function () { | ||
describe('Optimize sum of Gaussians', () => { | ||
it('group of two GL', () => { | ||
let pTrue = [-0.5, 0.5, 0.001, 0.001, 0.31, 0.31]; | ||
@@ -46,4 +46,4 @@ let yData = sumOfGaussians(pTrue); | ||
[ | ||
{ x: -0.5, y: 0.0009, width: (xFactor * nbPoints) / 8 }, | ||
{ x: 0.52, y: 0.0009, width: (xFactor * nbPoints) / 8 }, | ||
{ x: -0.5, y: 0.0009, shape: { width: (xFactor * nbPoints) / 8 } }, | ||
{ x: 0.52, y: 0.0009, shape: { width: (xFactor * nbPoints) / 8 } }, | ||
], | ||
@@ -57,3 +57,3 @@ { shape: { kind: 'gaussian' } }, | ||
expect(pFit.y).toBeCloseTo(pTrue[i + nL], 2); | ||
expect(pFit.width).toBeCloseTo(pTrue[i + nL * 2], 2); | ||
expect(pFit.shape.width).toBeCloseTo(pTrue[i + nL * 2], 2); | ||
} | ||
@@ -63,4 +63,4 @@ }); | ||
describe('Optimize 4 parameters of a linear combination of gaussian and lorentzians', function () { | ||
it('group of two GL', function () { | ||
describe('Optimize 4 parameters of a linear combination of gaussian and lorentzians', () => { | ||
it('group of two GL', () => { | ||
let pTrue = [ | ||
@@ -80,4 +80,4 @@ 0, | ||
[ | ||
{ x: 0.1, y: 0.0009, width: (xFactor * nbPoints) / 6 }, | ||
{ x: 0.1, y: 0.0009, width: (xFactor * nbPoints) / 6 }, | ||
{ x: 0.1, y: 0.0009, shape: { width: (xFactor * nbPoints) / 6 } }, | ||
{ x: 0.1, y: 0.0009, shape: { width: (xFactor * nbPoints) / 6 } }, | ||
], | ||
@@ -98,4 +98,4 @@ { | ||
expect(pFit.y).toBeCloseTo(pTrue[i + nL], 3); | ||
expect(pFit.width).toBeCloseTo(pTrue[i + nL * 2], 3); | ||
expect(pFit.mu).toBeCloseTo(pTrue[i + nL * 3], 3); | ||
expect(pFit.shape.width).toBeCloseTo(pTrue[i + nL * 2], 3); | ||
expect(pFit.shape.mu).toBeCloseTo(pTrue[i + nL * 3], 3); | ||
} | ||
@@ -102,0 +102,0 @@ }); |
@@ -1,3 +0,3 @@ | ||
import { checkInput } from './checkInput'; | ||
import { selectMethod } from './selectMethod'; | ||
import { checkInput } from './util/checkInput'; | ||
import { selectMethod } from './util/selectMethod'; | ||
@@ -90,6 +90,11 @@ // const STATE_INIT = 0; | ||
for (let i = 0; i < nbShapes; i++) { | ||
pFit.parameterValues[i + nbShapes] *= maxY; | ||
for (let k = 0; k < parameterKey.length; k++) { | ||
const key = parameterKey[k]; | ||
const value = pFit.parameterValues[i + k * nbShapes]; | ||
// we modify the optimized parameters | ||
peaks[i][parameterKey[k]] = pFit.parameterValues[i + k * nbShapes]; | ||
if (key === 'x' || key === 'y') { | ||
peaks[i][parameterKey[k]] = key === 'y' ? value * maxY : value; | ||
} else { | ||
peaks[i].shape[parameterKey[k]] = value; | ||
} | ||
} | ||
@@ -96,0 +101,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import { PseudoVoigt } from 'ml-peak-shape-generator'; | ||
import { pseudoVoigt } from 'ml-peak-shape-generator'; | ||
@@ -11,4 +11,6 @@ /** | ||
const { fct: pseudoVoigtFct } = pseudoVoigt; | ||
export function sumOfGaussianLorentzians(p) { | ||
return function (t) { | ||
return (t) => { | ||
let nL = p.length / 4; | ||
@@ -18,3 +20,3 @@ let result = 0; | ||
result += | ||
p[i + nL] * PseudoVoigt.fct(t - p[i], p[i + nL * 2], p[i + nL * 3]); | ||
p[i + nL] * pseudoVoigtFct(t - p[i], p[i + nL * 2], p[i + nL * 3]); | ||
} | ||
@@ -21,0 +23,0 @@ return result; |
@@ -1,2 +0,3 @@ | ||
import { Gaussian } from 'ml-peak-shape-generator'; | ||
import { gaussian } from 'ml-peak-shape-generator'; | ||
/** | ||
@@ -10,8 +11,10 @@ * This function calculates the spectrum as a sum of gaussian functions. The Gaussian | ||
const { fct: gaussianFct } = gaussian; | ||
export function sumOfGaussians(p) { | ||
return function (t) { | ||
return (t) => { | ||
let nL = p.length / 3; | ||
let result = 0; | ||
for (let i = 0; i < nL; i++) { | ||
result += p[i + nL] * Gaussian.fct(t - p[i], p[i + nL * 2]); | ||
result += p[i + nL] * gaussianFct(t - p[i], p[i + nL * 2]); | ||
} | ||
@@ -18,0 +21,0 @@ return result; |
@@ -1,2 +0,2 @@ | ||
import { Lorentzian } from 'ml-peak-shape-generator'; | ||
import { lorentzian } from 'ml-peak-shape-generator'; | ||
@@ -11,8 +11,10 @@ /** | ||
const { fct: lorentzianFct } = lorentzian; | ||
export function sumOfLorentzians(p) { | ||
return function (t) { | ||
return (t) => { | ||
let nL = p.length / 3; | ||
let result = 0; | ||
for (let i = 0; i < nL; i++) { | ||
result += p[i + nL] * Lorentzian.fct(t - p[i], p[i + nL * 2]); | ||
result += p[i + nL] * lorentzianFct(t - p[i], p[i + nL * 2]); | ||
} | ||
@@ -19,0 +21,0 @@ return result; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
46287
3
15
824
+ Addedml-peak-shape-generator@2.0.2(transitive)
- Removedassign-deep@^1.0.1
- Removedassign-deep@1.0.1(transitive)
- Removedassign-symbols@2.0.2(transitive)
- Removedml-peak-shape-generator@1.0.0(transitive)
Updatedml-array-max@^1.2.3