Skip to content

Commit 11370e8

Browse files
authored
feat: add peakDetectionAlgorithm option
* feat: select the center of the peaks by scoring peak candidates * chore: keep simple * feat: split peakpicking into differents algorithms * chore: fix second derivative algorithm * chore: add selection of algorithm in main function * chore: add web page to visualize * chore: add test case for first derivative and no perfect shape * chore: fix prettier * feat: add auto algorithm and avoid code duplication * chore: remove non used argument * chore: fix types * chore: add documentation to peakDetectionAlgorithm option * chore: new rule to exports
1 parent 6e3584d commit 11370e8

14 files changed

+1076
-99
lines changed

examples/xy-peaks-plotly.html

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>XY Data Visualization with Peaks</title>
7+
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
8+
<style>
9+
body { font-family: Arial, sans-serif; margin: 40px; }
10+
#plot { width: 100%; max-width: 900px; height: 600px; }
11+
</style>
12+
</head>
13+
<body>
14+
<h2>XY Data Visualization with Peaks</h2>
15+
<input type="file" id="fileInput" accept="application/json">
16+
<div id="plot"></div>
17+
<script>
18+
document.getElementById('fileInput').addEventListener('change', function(e) {
19+
const file = e.target.files[0];
20+
if (!file) return;
21+
const reader = new FileReader();
22+
reader.onload = function(event) {
23+
const data = JSON.parse(event.target.result);
24+
plotData(data);
25+
};
26+
reader.readAsText(file);
27+
});
28+
29+
function rescaleArray(arr) {
30+
if (!arr || arr.length === 0) return arr;
31+
const max = Math.max(...arr);
32+
if (max === 0) return arr;
33+
return arr.map(v => v * 100 / max);
34+
}
35+
36+
function plotData(data) {
37+
// Rescale first and second arrays
38+
const yRescaled = rescaleArray(data.y)
39+
const firstRescaled = rescaleArray(data.first);
40+
const secondRescaled = rescaleArray(data.second);
41+
42+
// Rescale peaks.y values
43+
const peaksRescaled = (data.peaks || []).map(p => ({
44+
...p,
45+
y: p.y * 100 / Math.max(...(data.peaks || []).map(pk => pk.y || 0) || [1])
46+
}));
47+
48+
const xyTrace = {
49+
x: data.x,
50+
y: yRescaled,
51+
mode: 'lines',
52+
name: 'original',
53+
line: { color: 'blue' }
54+
};
55+
// Plot 'first' in red
56+
const firstTrace = {
57+
x: data.x,
58+
y: firstRescaled,
59+
mode: 'lines',
60+
name: 'First',
61+
line: { color: 'red' }
62+
};
63+
64+
// Plot 'second' in blue
65+
const secondTrace = {
66+
x: data.x,
67+
y: secondRescaled,
68+
mode: 'lines',
69+
name: 'Second',
70+
line: { color: 'green' }
71+
};
72+
73+
// Vertical lines for peaks
74+
const peakTraces = peaksRescaled.map(peak => ({
75+
x: [peak.x, peak.x],
76+
y: [0, peak.y],
77+
mode: 'lines',
78+
name: 'Peak',
79+
line: { color: 'black', width: 2 },
80+
showlegend: false
81+
}));
82+
83+
// Mark the peak points
84+
const peakPoints = {
85+
x: peaksRescaled.map(p => p.x),
86+
y: peaksRescaled.map(p => p.y),
87+
mode: 'markers',
88+
marker: { color: 'red', size: 8 },
89+
name: 'Peak Center'
90+
};
91+
92+
Plotly.newPlot('plot', [xyTrace, firstTrace, secondTrace, ...peakTraces, peakPoints], {
93+
margin: { t: 40 },
94+
xaxis: { title: 'X', autorange: true },
95+
yaxis: { title: 'Y', autorange: true },
96+
title: 'XY Data with Peaks',
97+
dragmode: 'zoom',
98+
}, {responsive: true});
99+
}
100+
</script>
101+
</body>
102+
</html>

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
"version": "13.0.1",
44
"description": "Global Spectral Deconvolution",
55
"type": "module",
6-
"exports": "./lib/index.js",
6+
"exports": {
7+
".": "./lib/index.js"
8+
},
79
"files": [
810
"lib",
911
"src"

src/XIndex.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface XIndex {
2+
x: number;
3+
index: number;
4+
}

0 commit comments

Comments
 (0)