Skip to content

Commit b847438

Browse files
jobo322lpatiny
andauthored
feat: added timeout option to avoid long executions. close #37 (#47)
* feat: add default value of timeout to avoid long executions * feat: update opmitization package * chore: improve jsdoc * feat: add default value of timeout to avoid long executions * feat: update opmitization package Co-authored-by: Luc Patiny <[email protected]>
1 parent 57e30cf commit b847438

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
"dependencies": {
7878
"ml-peak-shape-generator": "^0.12.0",
7979
"ml-savitzky-golay-generalized": "2.0.3",
80-
"ml-spectra-fitting": "0.10.0",
80+
"ml-spectra-fitting": "^0.11.0",
8181
"ml-spectra-processing": "^4.10.0"
8282
}
8383
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { generateSpectrum } from 'spectrum-generator';
2+
3+
import { optimizePeaks } from '../optimizePeaks';
4+
5+
describe('optimizePeaks', () => {
6+
it('Should throw because execution time is over timeout', () => {
7+
const peaks = [{ x: 0, y: 1, width: 0.12 }];
8+
9+
const data = generateSpectrum(peaks, {
10+
from: -0.5,
11+
to: 0.5,
12+
nbPoints: 101,
13+
shape: {
14+
kind: 'gaussian',
15+
},
16+
});
17+
18+
const options = {
19+
optimization: {
20+
kind: 'lm',
21+
options: {
22+
timeout: 0,
23+
},
24+
},
25+
};
26+
expect(() =>
27+
optimizePeaks(data, [{ x: 0.1, y: 0.9, width: 0.11 }], options),
28+
).toThrow('The execution time is over to 0 seconds');
29+
});
30+
});

src/post/joinBroadPeaks.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ import { optimize } from 'ml-spectra-fitting';
99
* @param {string} [options.shape.kind = 'gaussian'] - kind of shape; lorentzian, gaussian and pseudovoigt are supported.
1010
* @param {object} [options.optimization = {}] - it's specify the kind and options of the algorithm use to optimize parameters.
1111
* @param {string} [options.optimization.kind = 'lm'] - kind of algorithm. By default it's levenberg-marquardt.
12+
* @param {number} [options.optimization.options.timeout = 10] - maximum time running before break in seconds.
1213
* @param {object} [options.optimization.options = {}] - options for the specific kind of algorithm.
1314
*/
1415
export function joinBroadPeaks(peakList, options = {}) {
1516
let {
1617
width = 0.25,
1718
shape = { kind: 'gaussian' },
18-
optimization = { kind: 'lm' },
19+
optimization = { kind: 'lm', timeout: 10 },
1920
} = options;
2021
let broadLines = [];
2122
// Optimize the possible broad lines

src/post/optimizePeaks.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import { groupPeaks } from './groupPeaks';
1313
* @param {number} [options.factorLimits = 2] - times of width to use to optimize peaks
1414
* @param {object} [options.shape={}] - it's specify the kind of shape used to fitting.
1515
* @param {string} [options.shape.kind='gaussian'] - kind of shape; lorentzian, gaussian and pseudovoigt are supported.
16-
* @param {string} [options.shape.options] - kind of shape; lorentzian, gaussian and pseudovoigt are supported.
1716
* @param {object} [options.optimization={}] - it's specify the kind and options of the algorithm use to optimize parameters.
1817
* @param {string} [options.optimization.kind='lm'] - kind of algorithm. By default it's levenberg-marquardt.
18+
* @param {number} [options.optimization.options.timeout=10] - maximum time running before break in seconds.
1919
* @param {object} [options.optimization.options={}] - options for the specific kind of algorithm.
2020
*/
2121

@@ -28,6 +28,9 @@ export function optimizePeaks(data, peakList, options = {}) {
2828
},
2929
optimization = {
3030
kind: 'lm',
31+
options: {
32+
timeout: 10,
33+
},
3134
},
3235
} = options;
3336

0 commit comments

Comments
 (0)