Skip to content

Commit

Permalink
feat: use gradientDifference from peak too (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
jobo322 authored Jan 13, 2021
1 parent 99d0d3a commit fb81ac2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
5 changes: 2 additions & 3 deletions src/__tests__/optimize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) },
[
Expand Down Expand Up @@ -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 },
Expand Down
37 changes: 32 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -140,6 +154,7 @@ export function optimize(data, peaks, options = {}) {
peaks[i][keys[s]] = pFit.parameterValues[i + s * peaks.length];
}
}

return result;
}

Expand All @@ -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');
}
Expand Down

0 comments on commit fb81ac2

Please sign in to comment.