Skip to content

Commit

Permalink
fix: fix new width in broad singlet peak (#117)
Browse files Browse the repository at this point in the history
* fix: fix new width in broad singlet peak

* chore: reduce code in gsd
  • Loading branch information
jobo322 authored Mar 6, 2024
1 parent 883b2fc commit 79eb832
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 46 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@
"ml-peak-shape-generator": "^4.1.2",
"ml-savitzky-golay-generalized": "^4.0.1",
"ml-spectra-fitting": "^4.2.1",
"ml-spectra-processing": "^12.0.0"
"ml-spectra-processing": "^14.0.0"
}
}
62 changes: 18 additions & 44 deletions src/gsd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import { sgg, SGGOptions } from 'ml-savitzky-golay-generalized';
import {
xIsEquallySpaced,
xIsMonotonic,
xMinValue,
xMaxValue,
xNoiseStandardDeviation,
xMinMaxValues,
} from 'ml-spectra-processing';

import { GSDPeak } from './GSDPeak';
Expand Down Expand Up @@ -79,7 +78,7 @@ export function gsd(data: DataXY, options: GSDOptions = {}): GSDPeakID[] {

// If the max difference between delta x is less than 5%, then,
// we can assume it to be equally spaced variable
let equallySpaced = xIsEquallySpaced(x);
const equallySpaced = xIsEquallySpaced(x);

if (noiseLevel === undefined) {
if (equallySpaced) {
Expand Down Expand Up @@ -109,50 +108,25 @@ export function gsd(data: DataXY, options: GSDOptions = {}): GSDPeakID[] {
}
}

let yData = y;
let dY, ddY;
const { windowSize, polynomial } = sgOptions;
const xValue = equallySpaced ? x[1] - x[0] : x;

if (equallySpaced) {
if (smoothY) {
yData = sgg(y, x[1] - x[0], {
windowSize,
polynomial,
const yData = smoothY
? sgg(y, xValue, {
...sgOptions,
derivative: 0,
});
}
dY = sgg(y, x[1] - x[0], {
windowSize,
polynomial,
derivative: 1,
});
ddY = sgg(y, x[1] - x[0], {
windowSize,
polynomial,
derivative: 2,
});
} else {
if (smoothY) {
yData = sgg(y, x, {
windowSize,
polynomial,
derivative: 0,
});
}
dY = sgg(y, x, {
windowSize,
polynomial,
derivative: 1,
});
ddY = sgg(y, x, {
windowSize,
polynomial,
derivative: 2,
});
}
})
: y;

const dY = sgg(y, xValue, {
...sgOptions,
derivative: 1,
});
const ddY = sgg(y, xValue, {
...sgOptions,
derivative: 2,
});

const minY = xMinValue(yData);
const maxY = xMaxValue(yData);
const { min: minY, max: maxY } = xMinMaxValues(yData);

if (minY > maxY || minY === maxY) return [];

Expand Down
4 changes: 3 additions & 1 deletion src/post/joinBroadPeaks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ export function joinBroadPeaks<T extends GSDPeakOptionalShape>(
id: generateID(),
x: broadLines[maxI].x,
y: max,
width: candidates.x[0] - candidates.x[candidates.x.length - 1],
width: Math.abs(
candidates.x[candidates.x.length - 1] - candidates.x[0],
),
},
],
{ shape, optimization },
Expand Down

0 comments on commit 79eb832

Please sign in to comment.