Skip to content

Commit

Permalink
feat!: homogenize and generalize output (#58)
Browse files Browse the repository at this point in the history
* feat:! homogenize and generalize output

* chore: remove nodejs 15.x n add 16.x

* fix: update dependencies

* chore: add prettier scripts

Co-authored-by: Luc Patiny <[email protected]>
  • Loading branch information
jobo322 and lpatiny committed Oct 11, 2021
1 parent ce3d47f commit 1b3970c
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x, 14.x, 15.x]
node-version: [12.x, 14.x, 16.x]
fail-fast: false
steps:
- uses: actions/checkout@v2
Expand Down
21 changes: 10 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"compile": "rollup -c",
"prepublishOnly": "npm run compile",
"test": "npm run test-coverage && npm run eslint",
"prettier": "prettier --check src",
"prettier-write": "prettier --write src",
"test-coverage": "jest --coverage",
"test-only": "jest",
"debug": "npm run prepublishOnly && node src/debug.js"
Expand Down Expand Up @@ -49,19 +51,16 @@
"trailingComma": "all"
},
"devDependencies": {
"@babel/plugin-transform-modules-commonjs": "^7.12.1",
"@babel/plugin-transform-modules-commonjs": "^7.15.4",
"@types/jest": "^27.0.2",
"cheminfo-build": "^1.1.8",
"eslint": "^7.12.1",
"eslint-config-cheminfo": "^5.2.2",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jest": "^24.1.0",
"eslint-plugin-prettier": "^3.1.4",
"cheminfo-build": "^1.1.11",
"eslint": "^7.32.0",
"eslint-config-cheminfo": "^5.6.0",
"esm": "^3.2.25",
"jest": "^27.2.4",
"prettier": "^2.1.2",
"rollup": "^2.33.1",
"spectrum-generator": "^4.4.1"
"jest": "^27.2.5",
"prettier": "^2.4.1",
"rollup": "^2.58.0",
"spectrum-generator": "^5.4.0"
},
"dependencies": {
"ml-array-max": "^1.2.3",
Expand Down
20 changes: 10 additions & 10 deletions src/__tests__/optimize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ describe('Optimize sum of Lorentzian', () => {
let result = optimize(
{ x, y: x.map((i) => yData(i)) },
[
{ 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 } },
],
{ shape: { kind: 'lorentzian' } },
);
Expand All @@ -27,7 +27,7 @@ describe('Optimize sum of Lorentzian', () => {
let pFit = result.peaks[i];
expect(pFit.x).toBeCloseTo(pTrue[i], 2);
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);
}
});
});
Expand All @@ -39,8 +39,8 @@ describe('Optimize sum of Gaussians', () => {
let result = optimize(
{ x, y: x.map((i) => yData(i)) },
[
{ 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 } },
],
{ shape: { kind: 'gaussian' } },
);
Expand All @@ -49,7 +49,7 @@ describe('Optimize sum of Gaussians', () => {
let pFit = result.peaks[i];
expect(pFit.x).toBeCloseTo(pTrue[i], 2);
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);
}
});
});
Expand All @@ -70,8 +70,8 @@ describe('Optimize 4 parameters of a linear combination of gaussian and lorentzi
let result = optimize(
{ 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 },
{ x: 0.1, y: 0.0009, shape: { width: (xFactor * nbPoints) / 6 } },
{ x: 0.1, y: 0.0009, shape: { width: (xFactor * nbPoints) / 6 } },
],
{
shape: { kind: 'pseudoVoigt' },
Expand All @@ -87,8 +87,8 @@ describe('Optimize 4 parameters of a linear combination of gaussian and lorentzi
let pFit = result.peaks[i];
expect(pFit.x).toBeCloseTo(pTrue[i], 3);
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);
}
});
});
Expand Down
9 changes: 7 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,15 @@ export function optimize(data, peakList, options = {}) {
let { parameterError: error, iterations } = pFit;
let result = { error, iterations, peaks };
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;
}
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/util/__tests__/checkInput.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import { checkInput } from '../checkInput';
describe('change default parameters', () => {
it('change the max value of x parameter', () => {
let data = { x: [-1, 0, 1], y: [1, 2, 1] };
let peaks = [{ x: 0, y: 1, width: 2 }];
let peaks = [{ x: 0, y: 1, shape: { width: 2 } }];
let options = {
optimization: {
parameters: {
x: {
hola: 'hola2',
max: (peak) => peak.x + peak.width * 0.1,
max: (peak) => peak.x + peak.shape.width * 0.1,
},
},
},
Expand Down
44 changes: 22 additions & 22 deletions src/util/checkInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ export function checkInput(data, peaks, options) {
defaultParameters = {
x: {
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,
},
y: {
init: (peak) => peak.y,
Expand All @@ -39,10 +39,10 @@ export function checkInput(data, peaks, options) {
gradientDifference: () => 1e-3,
},
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,
},
};
break;
Expand All @@ -51,9 +51,9 @@ export function checkInput(data, peaks, options) {
defaultParameters = {
x: {
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,
},
y: {
init: (peak) => peak.y,
Expand All @@ -62,10 +62,10 @@ export function checkInput(data, peaks, options) {
gradientDifference: () => 1e-3,
},
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,
},
};
break;
Expand All @@ -74,9 +74,9 @@ export function checkInput(data, peaks, options) {
defaultParameters = {
x: {
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,
},
y: {
init: (peak) => peak.y,
Expand All @@ -85,13 +85,13 @@ export function checkInput(data, peaks, options) {
gradientDifference: () => 1e-3,
},
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,
max: () => 1,
gradientDifference: () => 0.01,
Expand Down

0 comments on commit 1b3970c

Please sign in to comment.