diff --git a/src/__tests__/optimize.test.js b/src/__tests__/optimize.test.js index a900349..825efc2 100644 --- a/src/__tests__/optimize.test.js +++ b/src/__tests__/optimize.test.js @@ -36,7 +36,6 @@ describe('Optimize sum of Gaussians', function () { it('group of two GL', function () { let pTrue = [-0.5, 0.5, 0.001, 0.001, 0.31, 0.31]; let yData = sumOfGaussians(pTrue); - let result = optimize( { x, y: x.map((i) => yData(i)) }, [ @@ -67,9 +66,9 @@ describe('Optimize 4 parameters of a linear combination of gaussian and lorentzi (xFactor * nbPoints) / 10, (xFactor * nbPoints) / 10, ]; - let yData = sumOfGaussianLorentzians(pTrue); + let func = sumOfGaussianLorentzians(pTrue); let result = optimize( - { x, y: x.map(yData) }, + { x, y: x.map(func) }, [ { x: 0.1, y: 0.0009, width: (xFactor * nbPoints) / 6 }, { x: 0.1, y: 0.0009, width: (xFactor * nbPoints) / 6 }, diff --git a/src/index.js b/src/index.js index fbe3e0a..6663fa3 100644 --- a/src/index.js +++ b/src/index.js @@ -52,6 +52,21 @@ export function optimize(data, peaks, options = {}) { }, } = options; + let { + minFactorWidth = 0.25, + maxFactorWidth = 4, + minFactorX = 2, + maxFactorX = 2, + minFactorY = 0, + maxFactorY = 1.5, + minMuValue = 0, + maxMuValue = 1, + xGradientDifference, + yGradientDifference = 1e-3, + widthGradientDifference, + muGradientDifference = 0.01, + } = optimization; + peaks = JSON.parse(JSON.stringify(peaks)); if (typeof shape.kind !== 'string') { @@ -101,7 +116,6 @@ export function optimize(data, peaks, options = {}) { widthGradientDifference, muGradientDifference, }; - let nbShapes = peaks.length; let pMin = new Float64Array(nbShapes * nbParams); let pMax = new Float64Array(nbShapes * nbParams); @@ -140,6 +154,7 @@ export function optimize(data, peaks, options = {}) { peaks[i][keys[s]] = pFit.parameterValues[i + s * peaks.length]; } } + return result; } @@ -162,13 +177,25 @@ function getValue(parameterIndex, peak, state, options) { case STATE_GRADIENT_DIFFERENCE: switch (parameterIndex) { case X: - return options.xGradientDifference || peak.width / 2000; + return peak.xGradientDifference !== undefined + ? peak.xGradientDifference + : options.xGradientDifference !== undefined + ? options.xGradientDifference + : peak.width / 2e3; case Y: - return options.yGradientDifference || 1e-3; + return peak.yGradientDifference !== undefined + ? peak.yGradientDifference + : options.yGradientDifference; case WIDTH: - return options.widthGradientDifference || peak.width / 2000; + return peak.widthGradientDifference !== undefined + ? peak.widthGradientDifference + : options.widthGradientDifference !== undefined + ? options.widthGradientDifference + : peak.width / 2e3; case MU: - return options.muGradientDifference; + return peak.muGradientDifference !== undefined + ? peak.muGradientDifference + : options.muGradientDifference; default: throw new Error('The parameter is not supported'); }