Skip to content

Commit d4f1564

Browse files
authored
feat: add types (#55)
* feat: add types * fix: unnamed functions
1 parent e86bfd8 commit d4f1564

File tree

5 files changed

+152
-5
lines changed

5 files changed

+152
-5
lines changed

ml-gsd.d.ts

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
export interface XYNumberArray {
2+
x: Array<number> | Float64Array;
3+
y: Array<number> | Float64Array;
4+
}
5+
6+
interface GsdOptions {
7+
/**
8+
* Noise threshold in spectrum y units. Default is three/thresholdFactor times the absolute median of data.y.
9+
* @default `median(data.y) * (options.thresholdFactor || 3)`
10+
*/
11+
noiseLevel?: number;
12+
/**
13+
* Threshold to determine if a given peak should be considered as a noise, bases on its relative height compared to the highest peak.
14+
* @default 0.01
15+
*/
16+
minMaxRatio?: number;
17+
/**
18+
* If broadRatio is higher than 0, then all the peaks which second derivative smaller than broadRatio * maxAbsSecondDerivative will be marked with the soft mask equal to true.
19+
* @default 0.00025
20+
*/
21+
broadRatio?: number;
22+
/**
23+
* Select the peak intensities from a smoothed version of the independent variables.
24+
* @default true
25+
*/
26+
smoothY?: boolean;
27+
/**
28+
* if it is true, it optimizes the x and intensity by extrapolation.
29+
* @default false
30+
*/
31+
realTopDetection?: boolean;
32+
/**
33+
* options to shape used to adapt the FWHM
34+
* @default {kind:'gaussian'}
35+
*/
36+
shape?: { kind: string };
37+
// /**
38+
// * Threshold to determine if some peak is candidate to clustering into range.
39+
// * @default 0.25
40+
// */
41+
// broadWidth: number;
42+
/**
43+
* Options for savitz Golay
44+
*/
45+
sgOptions?: { windowSize: number; polynomial: number };
46+
/**
47+
* filter based on intensity of the first derive.
48+
* @default -1;
49+
*/
50+
}
51+
52+
export interface Peak {
53+
x: number;
54+
y: number;
55+
width: number;
56+
left?: number;
57+
right?: number;
58+
base?: number;
59+
soft?: boolean;
60+
kind?: string;
61+
}
62+
63+
export interface OptimizePeaksOptions {
64+
/**
65+
* factor to determine the width at the moment to group the peaks in signals in 'GSD.optimizePeaks' function.
66+
* @default 1
67+
*/
68+
factorWidth?: number;
69+
/**
70+
* times of width to use to optimize peaks
71+
* @default 2
72+
*/
73+
factorLimits?: number;
74+
/**
75+
* options to shape used to adapt the FWHM
76+
* @default {kind:'gaussian'}
77+
*/
78+
shape?: { kind: string };
79+
/**
80+
* options for optimization step
81+
*/
82+
optimization?: OptimizationOptions;
83+
}
84+
85+
export interface OptimizationOptions {
86+
/**
87+
* represent the algorithm to optimize.
88+
* @default {kind:'lm'}
89+
*/
90+
kind?: string;
91+
/**
92+
* Time limit to stop the optimization in seconds.
93+
* @default 10
94+
*/
95+
timeout?: number
96+
}
97+
98+
export interface OptimizedPeak {
99+
x: number;
100+
y: number;
101+
width: number;
102+
mu?: number;
103+
}
104+
105+
export function gsd(data: XYNumberArray, options?: GsdOptions): Peak[];
106+
107+
export function optimizePeaks(
108+
data: XYNumberArray,
109+
peakList: Peak[],
110+
options?: OptimizePeaksOptions,
111+
): OptimizedPeak[];
112+
113+
export function joinBroadPeaks(
114+
peakList: Peak[],
115+
options?: JoinBroadPeaksOptions,
116+
): Peak[];
117+
118+
export interface JoinBroadPeaksOptions {
119+
/**
120+
* @default 0.25
121+
*/
122+
width: number;
123+
/**
124+
* @default { kind: 'gaussian' }
125+
*/
126+
shape: { kind: string };
127+
/**
128+
* @default { kind: 'lm', timeout: 10 }
129+
*/
130+
optimization: OptimizationOptions;
131+
}
132+
133+
export function groupPeaks(peakList: Peak[], factor?: number): Peak[][];
134+
135+
export function broadenPeaks(
136+
peakList: Peak[],
137+
options?: {
138+
/**
139+
* @default 2
140+
*/
141+
factor: number;
142+
/**
143+
* @default false
144+
*/
145+
overlap: boolean;
146+
},
147+
): Peak[];

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "Global Spectra Deconvolution",
55
"main": "lib/index.js",
66
"module": "src/index.js",
7+
"types": "ml-gsd.d.ts",
78
"files": [
89
"lib",
910
"src"

src/__tests__/infraRed.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { readFileSync } from 'fs';
22

33
import { gsd } from '..';
44

5-
describe('Global spectra deconvolution Infrared spectra', function () {
5+
describe('Global spectra deconvolution Infrared spectra', () => {
66
// Test case obtained from Pag 443, Chap 8.
7-
it('Should get the correct result', function () {
7+
it('Should get the correct result', () => {
88
let spectrum = JSON.parse(
99
readFileSync(`${__dirname}/data/infraRed.json`, 'utf-8'),
1010
);

src/gsd.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ export function gsd(data, options = {}) {
131131
// By the intermediate value theorem We cannot find 2 consecutive maximum or minimum
132132
for (let i = 1; i < yData.length - 1; ++i) {
133133
// filter based on derivativeThreshold
134-
// console.log('pasa', y[i], dY[i], ddY[i]);
135134
if (Math.abs(dY[i]) > derivativeThreshold) {
136135
// Minimum in first derivative
137136
if (
@@ -237,7 +236,7 @@ export function gsd(data, options = {}) {
237236
signals[j].base = noiseLevel;
238237
}
239238

240-
signals.sort(function (a, b) {
239+
signals.sort((a, b) => {
241240
return a.x - b.x;
242241
});
243242

src/post/joinBroadPeaks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export function joinBroadPeaks(peakList, options = {}) {
7979
count = 1;
8080
}
8181
}
82-
peakList.sort(function (a, b) {
82+
peakList.sort((a, b) => {
8383
return a.x - b.x;
8484
});
8585

0 commit comments

Comments
 (0)