Skip to content

Commit

Permalink
feat: allow to specify shape at the level of peak
Browse files Browse the repository at this point in the history
  • Loading branch information
aiday-mar authored Mar 8, 2022
1 parent ffba763 commit 90704b8
Show file tree
Hide file tree
Showing 12 changed files with 673 additions and 319 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"prettier": "prettier --check src",
"prettier-write": "prettier --write src",
"test-coverage": "jest --coverage",
"test-only": "jest --coverage",
"test-only": "jest --coverage --silent=false",
"tsc": "npm run clean && npm run tsc-cjs && npm run tsc-esm",
"tsc-cjs": "tsc --project tsconfig.cjs.json",
"tsc-esm": "tsc --project tsconfig.esm.json",
Expand Down Expand Up @@ -50,7 +50,7 @@
"@babel/plugin-transform-modules-commonjs": "^7.16.8",
"@types/jest": "^27.4.1",
"cheminfo-build": "^1.1.11",
"eslint": "^8.9.0",
"eslint": "^8.10.0",
"eslint-config-cheminfo": "^7.2.2",
"eslint-config-cheminfo-typescript": "^10.3.0",
"esm": "^3.2.25",
Expand All @@ -59,12 +59,12 @@
"prettier": "^2.5.1",
"spectrum-generator": "^7.0.1",
"ts-jest": "^27.1.3",
"typescript": "^4.5.5"
"typescript": "^4.6.2"
},
"dependencies": {
"cheminfo-types": "^1.0.0",
"cheminfo-types": "^1.1.0",
"ml-array-max": "^1.2.4",
"ml-levenberg-marquardt": "^4.0.0",
"ml-levenberg-marquardt": "^4.1.0",
"ml-peak-shape-generator": "^4.1.1"
}
}
122 changes: 122 additions & 0 deletions src/__tests__/mixShapes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import type { DataXY } from 'cheminfo-types';
import { toBeDeepCloseTo, toMatchCloseTo } from 'jest-matcher-deep-close-to';
import { Shape1D } from 'ml-peak-shape-generator';
import { generateSpectrum } from 'spectrum-generator';

import { optimize } from '../index';

expect.extend({ toBeDeepCloseTo, toMatchCloseTo });

let nbPoints = 31;
let xFactor = 0.1;
let x = new Float64Array(nbPoints);
for (let i = 0; i < nbPoints; i++) {
x[i] = (i - nbPoints / 2) * xFactor;
}

describe('Sum of a mix of distributions', () => {
it('group of two GL', () => {
let peaks = [
{
x: 0,
y: 0.001,
fwhm: 0.31,
shape: {
kind: 'pseudoVoigt',
options: { mu: 0.5 },
} as Shape1D,
},
{
x: 0,
y: 0.001,
fwhm: 0.31,
shape: {
kind: 'pseudoVoigt',
options: { mu: 0.5 },
} as Shape1D,
},
{ x: -0.5, y: 0.001, fwhm: 0.31, shape: { kind: 'gaussian' } },
{ x: 0.5, y: 0.001, fwhm: 0.31, shape: { kind: 'gaussian' } },
{ x: -0.5, y: 0.001, fwhm: 0.31, shape: { kind: 'lorentzian' } },
{ x: 0.5, y: 0.001, fwhm: 0.31, shape: { kind: 'lorentzian' } },
];

const peaksGenerator = [
{ x: 0, y: 0.001, fwhm: 0.31, mu: 0.5 },
{ x: 0, y: 0.001, fwhm: 0.31, mu: 0.5 },
{ x: -0.5, y: 0.001, fwhm: 0.31 },
{ x: 0.5, y: 0.001, fwhm: 0.31 },
{ x: -0.5, y: 0.001, fwhm: 0.31 },
{ x: 0.5, y: 0.001, fwhm: 0.31 },
];

const data: DataXY = generateSpectrum(peaksGenerator, {
generator: {
from: -1,
to: 1,
nbPoints: 101,
},
});

let result = optimize(
data,
[
{
x: 0.001,
y: 0.0009,
fwhm: (xFactor * nbPoints) / 8,
shape: {
kind: 'pseudoVoigt',
options: { mu: 0.52 },
} as Shape1D,
},
{
x: 0.001,
y: 0.0009,
fwhm: (xFactor * nbPoints) / 8,
shape: {
kind: 'pseudoVoigt',
options: { mu: 0.52 },
} as Shape1D,
},
{
x: -0.52,
y: 0.0009,
fwhm: (xFactor * nbPoints) / 8,
shape: { kind: 'gaussian' } as Shape1D,
},
{
x: 0.52,
y: 0.0009,
fwhm: (xFactor * nbPoints) / 8,
shape: { kind: 'gaussian' } as Shape1D,
},
{
x: -0.52,
y: 0.0009,
fwhm: (xFactor * nbPoints) / 8,
shape: { kind: 'lorentzian' } as Shape1D,
},
{
x: 0.52,
y: 0.0009,
fwhm: (xFactor * nbPoints) / 8,
shape: { kind: 'lorentzian' } as Shape1D,
},
],
{
optimization: {
kind: 'lm',
options: { maxIterations: 1000, errorTolerance: 1e-8 },
},
},
);

for (let i = 0; i < 6; i++) {
let pFit = result.peaks[i];
expect(pFit.x).toBeCloseTo(peaks[i].x, 0);
expect(pFit.y).toBeCloseTo(peaks[i].y, 0);
expect(pFit.fwhm).toBeCloseTo(peaks[i].fwhm, 0);
}
});
});
149 changes: 149 additions & 0 deletions src/__tests__/oneShape.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import type { DataXY } from 'cheminfo-types';
import { toBeDeepCloseTo, toMatchCloseTo } from 'jest-matcher-deep-close-to';
import { Shape1D } from 'ml-peak-shape-generator';
import { generateSpectrum } from 'spectrum-generator';

import { optimize } from '../index';

expect.extend({ toBeDeepCloseTo, toMatchCloseTo });

let nbPoints = 31;
let xFactor = 0.1;
let x = new Float64Array(nbPoints);
for (let i = 0; i < nbPoints; i++) {
x[i] = (i - nbPoints / 2) * xFactor;
}

describe('One Shape tested', () => {
it('Gaussian', () => {
let peaks = [
{
x: -0.5,
y: 0.001,
fwhm: 0.31,
shape: { kind: 'gaussian' } as Shape1D,
},
];

const peaksGenerator = [{ x: -0.5, y: 0.001, fwhm: 0.31 }];

const data: DataXY = generateSpectrum(peaksGenerator, {
generator: {
from: -1,
to: 1,
nbPoints: 101,
},
});

let result = optimize(
data,
[
{
x: -0.52,
y: 0.0009,
fwhm: (xFactor * nbPoints) / 8,
shape: { kind: 'gaussian' } as Shape1D,
},
],
{
optimization: {
kind: 'lm',
options: { maxIterations: 1000, errorTolerance: 1e-8 },
},
},
);
let pFit = result.peaks[0];
expect(pFit.x).toBeCloseTo(peaks[0].x, 0);
expect(pFit.y).toBeCloseTo(peaks[0].y, 0);
expect(pFit.fwhm).toBeCloseTo(peaks[0].fwhm, 0);
});

it('Lorentzian', () => {
let peaks = [
{
x: -0.5,
y: 0.001,
fwhm: 0.31,
shape: { kind: 'lorentzian' } as Shape1D,
},
];

const peaksGenerator = [{ x: -0.5, y: 0.001, fwhm: 0.31 }];

const data: DataXY = generateSpectrum(peaksGenerator, {
generator: {
from: -1,
to: 1,
nbPoints: 101,
},
});

let result = optimize(
data,
[
{
x: -0.52,
y: 0.0009,
fwhm: (xFactor * nbPoints) / 8,
shape: { kind: 'lorentzian' } as Shape1D,
},
],
{
optimization: {
kind: 'lm',
options: { maxIterations: 1000, errorTolerance: 1e-8 },
},
},
);
let pFit = result.peaks[0];
expect(pFit.x).toBeCloseTo(peaks[0].x, 0);
expect(pFit.y).toBeCloseTo(peaks[0].y, 0);
expect(pFit.fwhm).toBeCloseTo(peaks[0].fwhm, 0);
});

it('Pseudo Voigt', () => {
let peaks = [
{
x: 0,
y: 0.001,
fwhm: 0.31,
shape: { kind: 'pseudoVoigt', options: { mu: 0.5 } } as Shape1D,
},
];

const peaksGenerator = [{ x: 0, y: 0.001, fwhm: 0.31, mu: 0.5 }];

const data: DataXY = generateSpectrum(peaksGenerator, {
generator: {
from: -1,
to: 1,
nbPoints: 101,
},
});

let result = optimize(
data,
[
{
x: 0.001,
y: 0.0009,
fwhm: (xFactor * nbPoints) / 8,
shape: {
kind: 'pseudoVoigt',
options: { mu: 0.52 },
} as Shape1D,
},
],
{
optimization: {
kind: 'lm',
options: { maxIterations: 1000, errorTolerance: 1e-8 },
},
},
);
let pFit = result.peaks[0];
expect(pFit.x).toBeCloseTo(peaks[0].x, 0);
expect(pFit.y).toBeCloseTo(peaks[0].y, 0);
expect(pFit.fwhm).toBeCloseTo(peaks[0].fwhm, 0);
});
});
Loading

0 comments on commit 90704b8

Please sign in to comment.