Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add shape to gsd result #102

Merged
merged 3 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/nodejs-ts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ jobs:
# Documentation: https://github.com/zakodium/workflows#nodejs-ci
uses: zakodium/workflows/.github/workflows/nodejs.yml@nodejs-v1
with:
node-version-matrix: '[12, 14, 16, 18]'
node-version-matrix: '[14, 16, 18]'
lint-check-types: true
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
"cheminfo-types": "^1.1.0",
"ml-peak-shape-generator": "^4.1.1",
"ml-savitzky-golay-generalized": "4.0.1",
"ml-spectra-fitting": "4.0.2",
"ml-spectra-fitting": "4.1.0",
"ml-spectra-processing": "^11.6.0"
}
}
5 changes: 5 additions & 0 deletions src/GSDPeak.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Shape1D } from 'ml-peak-shape-generator';

export interface GSDPeak {
x: number;
y: number;
Expand All @@ -14,6 +16,9 @@ export interface GSDPeak {
* This allows to determine if a peak is soft or not
*/
ddY: number;

shape: Shape1D;

inflectionPoints: {
from: { x: number; index: number };
to: { x: number; index: number };
Expand Down
8 changes: 8 additions & 0 deletions src/__tests__/gaussian-smooth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ describe('gaussian simulated peaks', () => {
ddY: -259.83290100626783,
width: 0.08,
index: 25,
shape: {
fwhm: 0.09419280180123805,
kind: 'gaussian',
},
inflectionPoints: {
from: { x: -0.54, index: 23 },
to: { x: -0.46, index: 27 },
Expand All @@ -49,6 +53,10 @@ describe('gaussian simulated peaks', () => {
ddY: -259.83290100626783,
width: 0.08,
index: 75,
shape: {
fwhm: 0.09419280180123805,
kind: 'gaussian',
},
inflectionPoints: {
from: { x: 0.46, index: 73 },
to: { x: 0.54, index: 77 },
Expand Down
32 changes: 32 additions & 0 deletions src/__tests__/gaussian.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ describe('smooth:false option', () => {
ddY: -259.83290100626783,
width: 0.08,
index: 25,
shape: {
fwhm: 0.09419280180123805,
kind: 'gaussian',
},
inflectionPoints: {
from: { index: 23, x: -0.54 },
to: { index: 27, x: -0.46 },
Expand All @@ -49,6 +53,10 @@ describe('smooth:false option', () => {
ddY: -259.83290100626783,
width: 0.08,
index: 75,
shape: {
fwhm: 0.09419280180123805,
kind: 'gaussian',
},
inflectionPoints: {
from: { index: 73, x: 0.46 },
to: { index: 77, x: 0.54 },
Expand All @@ -68,6 +76,10 @@ describe('smooth:false option', () => {
ddY: -259.83290100626783,
width: 0.08,
index: 25,
shape: {
fwhm: 0.09419280180123805,
kind: 'gaussian',
},
inflectionPoints: {
from: { index: 23, x: -0.54 },
to: { index: 27, x: -0.46 },
Expand All @@ -79,6 +91,10 @@ describe('smooth:false option', () => {
ddY: -259.83290100626783,
width: 0.08,
index: 75,
shape: {
fwhm: 0.09419280180123805,
kind: 'gaussian',
},
inflectionPoints: {
from: { index: 73, x: 0.46 },
to: { index: 77, x: 0.54 },
Expand All @@ -101,6 +117,10 @@ describe('smooth:false option', () => {
ddY: 259.83290100626783,
width: 0.08,
index: 25,
shape: {
fwhm: 0.09419280180123805,
kind: 'gaussian',
},
inflectionPoints: {
from: { index: 23, x: -0.54 },
to: { index: 27, x: -0.46 },
Expand All @@ -112,6 +132,10 @@ describe('smooth:false option', () => {
ddY: 259.83290100626783,
width: 0.08,
index: 75,
shape: {
fwhm: 0.09419280180123805,
kind: 'gaussian',
},
inflectionPoints: {
from: { index: 73, x: 0.46 },
to: { index: 77, x: 0.54 },
Expand All @@ -135,6 +159,10 @@ describe('smooth:false option', () => {
ddY: 259.83290100626783,
width: 0.08,
index: 25,
shape: {
fwhm: 0.09419280180123805,
kind: 'gaussian',
},
inflectionPoints: {
from: { index: 23, x: -0.54 },
to: { index: 27, x: -0.46 },
Expand All @@ -146,6 +174,10 @@ describe('smooth:false option', () => {
ddY: 259.83290100626783,
width: 0.08,
index: 75,
shape: {
fwhm: 0.09419280180123805,
kind: 'gaussian',
},
inflectionPoints: {
from: { index: 73, x: 0.46 },
to: { index: 77, x: 0.54 },
Expand Down
16 changes: 16 additions & 0 deletions src/__tests__/power.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ describe('power', () => {
width: 0.3,
index: 100,
ddY: -33832.03463203485,
shape: {
fwhm: 0.35322300675464324,
kind: 'gaussian',
},
inflectionPoints: {
from: { x: 4.85, index: 97 },
to: { x: 5.15, index: 103 },
Expand All @@ -78,6 +82,10 @@ describe('power', () => {
width: 0.05,
index: 89,
ddY: 800,
shape: {
fwhm: 0.05887050112577352,
kind: 'gaussian',
},
inflectionPoints: {
from: { x: 4.4, index: 88 },
to: { x: 4.45, index: 89 },
Expand All @@ -89,6 +97,10 @@ describe('power', () => {
width: 0.2,
index: 100,
ddY: -44914.28571428623,
shape: {
fwhm: 0.2354820045030941,
kind: 'gaussian',
},
inflectionPoints: {
from: { x: 4.9, index: 98 },
to: { x: 5.1, index: 102 },
Expand All @@ -100,6 +112,10 @@ describe('power', () => {
width: 0.05,
index: 111,
ddY: 800,
shape: {
fwhm: 0.05887050112577352,
kind: 'gaussian',
},
inflectionPoints: {
from: { x: 5.55, index: 111 },
to: { x: 5.6, index: 112 },
Expand Down
4 changes: 4 additions & 0 deletions src/__tests__/ubiquitin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ describe('Global spectra deconvolution ubiquitin', () => {
ddY: -15468134.039875854,
width: 0.002420000000000755,
index: 11,
shape: {
fwhm: 0.0028493322544883375,
kind: 'gaussian',
},
inflectionPoints: {
from: { x: 200.054133, index: 9 },
to: { x: 200.056553, index: 13 },
Expand Down
14 changes: 11 additions & 3 deletions src/gsd.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { DataXY } from 'cheminfo-types';
import { Shape1D } from 'ml-peak-shape-generator';
import { sgg, SGGOptions } from 'ml-savitzky-golay-generalized';
import {
xIsEquallySpaced,
Expand All @@ -9,6 +10,7 @@ import {
} from 'ml-spectra-processing';

import { GSDPeak } from './GSDPeak';
import { appendShapeAndFWHM } from './utils/appendShapeAndFWHM';
import { optimizeTop } from './utils/optimizeTop';

export interface GSDOptions {
Expand Down Expand Up @@ -44,6 +46,11 @@ export interface GSDOptions {
* @default false
*/
realTopDetection?: boolean;
/**
* Shape used to add FWHM property in the peaks
* @default {kind:'gaussian'}
*/
shape?: Shape1D;
}

/**
Expand All @@ -60,6 +67,7 @@ export function gsd(data: DataXY, options: GSDOptions = {}): GSDPeak[] {
windowSize: 9,
polynomial: 3,
},
shape,
noiseLevel,
smoothY = false,
maxCriteria = true,
Expand Down Expand Up @@ -239,14 +247,14 @@ export function gsd(data: DataXY, options: GSDOptions = {}): GSDPeak[] {
peaks.push({
x: deltaX,
y: yData[minddYIndex],
width: width,
width,
index: minddYIndex,
ddY: ddY[minddYIndex],
inflectionPoints: {
from: intervalL[possible],
to: intervalR[possible],
},
});
} as GSDPeak);
}
}
}
Expand All @@ -266,5 +274,5 @@ export function gsd(data: DataXY, options: GSDOptions = {}): GSDPeak[] {
return a.x - b.x;
});

return peaks;
return appendShapeAndFWHM(peaks, { shape });
}
2 changes: 1 addition & 1 deletion src/post/__tests__/optimizePeaksWithLogs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('optimizePeaksWithLogs', () => {
expect(result.logs).toMatchObject([
{
iterations: 100,
error: 0.000017852930772995625,
error: 0.000017852931247867768,
parameters: { kind: 'lm', options: { timeout: 10 } },
message: 'optimization successful',
groupSize: 1,
Expand Down
2 changes: 1 addition & 1 deletion src/post/broadenPeaks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { GSDPeak } from '../GSDPeak';
*/

export function broadenPeaks(
peakList: GSDPeak[],
peakList: Omit<GSDPeak, 'shape'>[],
options: {
/**
* @default 2
Expand Down
9 changes: 9 additions & 0 deletions src/post/optimizePeaks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { DataXY, PeakXYWidth } from 'cheminfo-types';
import { FromTo } from 'cheminfo-types';
import { Shape1D } from 'ml-peak-shape-generator';
import type { OptimizationOptions } from 'ml-spectra-fitting';

Expand All @@ -7,6 +8,14 @@ import { GSDPeakOptimized } from '../GSDPeakOptimized';
import { optimizePeaksWithLogs } from './optimizePeaksWithLogs';

export interface OptimizePeaksOptions {
/**
* baseline
*/
baseline?: number;
/**
* range to apply the optimization
*/
fromTo?: Partial<FromTo>;
/**
* Shape to use for optimization
* @default {kind:'gaussian'}
Expand Down
11 changes: 8 additions & 3 deletions src/post/optimizePeaksWithLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export function optimizePeaksWithLogs(
options: OptimizePeaksOptions = {},
): { logs: any[]; optimizedPeaks: GSDPeakOptimized[] } {
const {
fromTo = {},
baseline,
shape = { kind: 'gaussian' },
groupingFactor = 1,
factorLimits = 2,
Expand All @@ -40,7 +42,6 @@ export function optimizePeaksWithLogs(
let groups = groupPeaks(peakList, { factor: groupingFactor });
let logs: any[] = [];
let results: GSDPeakOptimized[] = [];

groups.forEach((peakGroup) => {
const start = Date.now();
// In order to make optimization we will add fwhm and shape on all the peaks
Expand All @@ -49,8 +50,11 @@ export function optimizePeaksWithLogs(
const firstPeak = peaks[0];
const lastPeak = peaks[peaks.length - 1];

const from = firstPeak.x - firstPeak.width * factorLimits;
const to = lastPeak.x + lastPeak.width * factorLimits;
const {
from = firstPeak.x - firstPeak.width * factorLimits,
to = lastPeak.x + lastPeak.width * factorLimits,
} = fromTo;

const { fromIndex, toIndex } = xGetFromToIndex(data.x, { from, to });

const x =
Expand All @@ -76,6 +80,7 @@ export function optimizePeaksWithLogs(
peaks: optimizedPeaks,
} = optimize({ x, y }, peaks, {
shape,
baseline,
optimization,
});

Expand Down