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

Setup typescript #66

Merged
merged 55 commits into from
Oct 26, 2021
Merged
Changes from 1 commit
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
c1775b8
refactor: setup project for TypeScript
wadjih-bencheikh18 Oct 18, 2021
9747d70
add config
wadjih-bencheikh18 Oct 19, 2021
fa00b10
done gsd.ts
wadjih-bencheikh18 Oct 19, 2021
048eed5
fix broadenPeaks
wadjih-bencheikh18 Oct 19, 2021
a748e3c
done groupPeaks
wadjih-bencheikh18 Oct 19, 2021
5ef1afe
migration done
wadjih-bencheikh18 Oct 19, 2021
b370db5
fix test files
wadjih-bencheikh18 Oct 19, 2021
8d0895e
fix packages
wadjih-bencheikh18 Oct 19, 2021
ea0fa45
update package fixed spectrum-generato
wadjih-bencheikh18 Oct 20, 2021
e5e3a3e
update node
wadjih-bencheikh18 Oct 20, 2021
acfa5d5
fix joinBroadPeaks
wadjih-bencheikh18 Oct 20, 2021
547b6ab
add checkjs and fix problems
wadjih-bencheikh18 Oct 20, 2021
e01df3d
fix import
wadjih-bencheikh18 Oct 21, 2021
be26d54
update types
wadjih-bencheikh18 Oct 21, 2021
e8a3d97
fix types in gsd.ts
wadjih-bencheikh18 Oct 21, 2021
d9c5ab3
fix for PR tests
wadjih-bencheikh18 Oct 21, 2021
226a4a8
update code
wadjih-bencheikh18 Oct 21, 2021
a9a9f1a
update tests types
wadjih-bencheikh18 Oct 21, 2021
effb87f
fix joinBroadPeaks
wadjih-bencheikh18 Oct 21, 2021
b54496a
test JoinBroad
wadjih-bencheikh18 Oct 21, 2021
8a6a431
test JoinBoardPeaks and fix optimizePeaks
wadjih-bencheikh18 Oct 21, 2021
89cb97f
fix joinBroad test-converge
wadjih-bencheikh18 Oct 21, 2021
f0e69c5
fix montioned problems (caste problem no fixed)
wadjih-bencheikh18 Oct 22, 2021
f0b0b4f
fix jest.config.js
wadjih-bencheikh18 Oct 22, 2021
df378eb
fix small details
wadjih-bencheikh18 Oct 22, 2021
3c40ee1
update joinBroadPeaks
wadjih-bencheikh18 Oct 22, 2021
eb7cd19
hits upgrade
wadjih-bencheikh18 Oct 22, 2021
409d200
test patch
wadjih-bencheikh18 Oct 22, 2021
93523a9
fix nodejs.yml
wadjih-bencheikh18 Oct 22, 2021
fdd2798
reverse commit
wadjih-bencheikh18 Oct 22, 2021
bd2c5cb
fix patch
wadjih-bencheikh18 Oct 22, 2021
bcfabb1
fix : reset changes before patch's error
wadjih-bencheikh18 Oct 22, 2021
7f27140
fix dataType
wadjih-bencheikh18 Oct 22, 2021
7830763
test patch
wadjih-bencheikh18 Oct 22, 2021
d71d55c
fix package test
wadjih-bencheikh18 Oct 22, 2021
2628354
fix patch joinbroad
wadjih-bencheikh18 Oct 22, 2021
5de7931
fix patch OptimizePeaks
wadjih-bencheikh18 Oct 22, 2021
b090901
fix package joinboardPeaks
wadjih-bencheikh18 Oct 22, 2021
44e3a25
fix joinBoard problem
wadjih-bencheikh18 Oct 22, 2021
f9d1514
update files : export types
wadjih-bencheikh18 Oct 22, 2021
9fe5882
optimize types
wadjih-bencheikh18 Oct 22, 2021
2dfe9d4
fix patch
wadjih-bencheikh18 Oct 22, 2021
9d6ff82
test patch
wadjih-bencheikh18 Oct 22, 2021
38adee6
test patch 2
wadjih-bencheikh18 Oct 22, 2021
f43aa8a
test package
wadjih-bencheikh18 Oct 23, 2021
70d3f89
fix joinBoard package
wadjih-bencheikh18 Oct 23, 2021
eff67e0
change width in JoinBoards
wadjih-bencheikh18 Oct 23, 2021
5e02fbf
fix types format
wadjih-bencheikh18 Oct 25, 2021
0fb2b17
fix jest config + tsconfig
wadjih-bencheikh18 Oct 25, 2021
ac6cc8b
fix nodejs.yml
wadjih-bencheikh18 Oct 25, 2021
83c76aa
fix nodejs.yml 2
wadjih-bencheikh18 Oct 25, 2021
955d9fb
fix prettier
wadjih-bencheikh18 Oct 25, 2021
c7998dd
fix nodejs.yml
wadjih-bencheikh18 Oct 25, 2021
23354e1
update nodejs file
wadjih-bencheikh18 Oct 25, 2021
fc50c1b
add the resuable workflow
wadjih-bencheikh18 Oct 25, 2021
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
Prev Previous commit
Next Next commit
fix types in gsd.ts
  • Loading branch information
wadjih-bencheikh18 committed Oct 21, 2021
commit e8a3d97a3da74eef5a11ce1e6cc6277561626942
51 changes: 34 additions & 17 deletions src/gsd.ts
Original file line number Diff line number Diff line change
@@ -29,9 +29,9 @@ interface peakType {
index?: number;
x: number;
y: number;
shape: shapeType,
from?: number,
to?: number
shape: shapeType;
from?: number;
to?: number;
}
interface lastType {
wadjih-bencheikh18 marked this conversation as resolved.
Show resolved Hide resolved
x: number;
@@ -59,30 +59,30 @@ interface optionsType {
minMaxRatio?: number;
derivativeThreshold?: number;
realTopDetection?: boolean;
factor?:number
factor?: number;
}
interface dataType {
wadjih-bencheikh18 marked this conversation as resolved.
Show resolved Hide resolved
x: number[];
y: number[];
}
export function gsd(data: dataType, options:optionsType={}) {
export function gsd(data: dataType, options: optionsType = {}) {
let {
noiseLevel,
sgOptions = {
windowSize: 9,
polynomial: 3,
},
shape = { kind: 'gaussian', options: Gaussian },
shape = { kind: 'gaussian', options: new Gaussian(), width: 0 },
smoothY = true,
heightFactor = 0,
broadRatio = 0.0,
maxCriteria = true,
minMaxRatio = 0.00025,
derivativeThreshold = -1,
realTopDetection = false,
} = options;
}: optionsType = options;

let { y: yIn, x } = data;
let { y: yIn, x }: dataType = data;

const y = yIn.slice();
let equalSpaced = isEqualSpaced(x);
@@ -107,8 +107,14 @@ export function gsd(data: dataType, options:optionsType={}) {
// If the max difference between delta x is less than 5%, then,
// we can assume it to be equally spaced variable
let yData = y;
let dY:number[], ddY:number[];
const { windowSize, polynomial } = sgOptions;
let dY: number[], ddY: number[];
const {
windowSize,
polynomial,
}: {
windowSize: number;
polynomial: number;
} = sgOptions;

if (equalSpaced) {
if (smoothY) {
@@ -148,7 +154,7 @@ export function gsd(data: dataType, options:optionsType={}) {
});
}

const xData:number[] = x;
const xData: number[] = x;
const dX = x[1] - x[0];
let maxDdy = 0;
let maxY = 0;
@@ -209,11 +215,18 @@ export function gsd(data: dataType, options:optionsType={}) {
}
}

let widthProcessor = getShape1D(shape.kind as ShapeKind, shape.options).widthToFWHM;
let widthProcessor = getShape1D(
shape.kind as ShapeKind,
shape.options,
).widthToFWHM;

let signals: peakType[] = [];
let lastK = -1;
let possible, frequency, distanceJ, minDistance, gettingCloser;
let possible: number,
frequency: number,
distanceJ: number,
minDistance: number,
gettingCloser: boolean;
for (let j = 0; j < minddY.length; ++j) {
frequency = xData[minddY[j]];
possible = -1;
@@ -271,7 +284,7 @@ export function gsd(data: dataType, options:optionsType={}) {

// Correct the values to fit the original spectra data
signals.forEach((signal) => {
signal.shape.noiseLevel= noiseLevel ;
signal.shape.noiseLevel = noiseLevel;
});

signals.sort((a, b) => {
@@ -282,7 +295,7 @@ export function gsd(data: dataType, options:optionsType={}) {
}

const isEqualSpaced = (x: number[]) => {
let tmp;
let tmp: number;
let maxDx = 0;
let minDx = Number.MAX_SAFE_INTEGER;
for (let i = 0; i < x.length - 1; ++i) {
@@ -306,7 +319,7 @@ const getNoiseLevel = (y: number[]) => {
mean += y[i];
}
mean /= length;
let averageDeviations = new Array(length);
let averageDeviations: number[] = new Array(length);
for (let i = 0; i < length; ++i) {
averageDeviations[i] = Math.abs(y[i] - mean);
}
@@ -323,7 +336,11 @@ const getNoiseLevel = (y: number[]) => {
return stddev;
};
const determineRealTop = (peakList: peakType[], x: number[], y: number[]) => {
let alpha, beta, gamma, p, currentPoint;
let alpha: number,
beta: number,
gamma: number,
p: number,
currentPoint: number;
peakList.forEach((peak) => {
currentPoint = peak.index as number; // peakList[j][2];
wadjih-bencheikh18 marked this conversation as resolved.
Show resolved Hide resolved
// The detected peak could be moved 1 or 2 units to left or right.
25 changes: 13 additions & 12 deletions src/post/broadenPeaks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Shape1D, ShapeKind } from "ml-peak-shape-generator";
import { Shape1D, ShapeKind } from 'ml-peak-shape-generator';

/**
* This method will allow to enlarge peaks and prevent overlap between peaks
@@ -13,9 +13,9 @@ interface peakType {
index?: number;
x: number;
y: number;
shape: shapeType,
from?: number,
to?: number
shape: shapeType;
from?: number;
to?: number;
}
interface shapeType {
kind?: ShapeKind;
@@ -26,13 +26,13 @@ interface shapeType {
noiseLevel?: number;
}
interface optionsType {
factor?: number,
overlap?: boolean
factor?: number;
overlap?: boolean;
}
export function broadenPeaks(peakList: peakType[], options: optionsType={}) {
const { factor = 2, overlap = false } = options;
export function broadenPeaks(peakList: peakType[], options: optionsType = {}) {
const { factor = 2, overlap = false }: optionsType = options;

const peaks:peakType[] = JSON.parse(JSON.stringify(peakList));
const peaks: peakType[] = JSON.parse(JSON.stringify(peakList));

for (let peak of peaks) {
peak.from = peak.x - (peak.shape.width / 2) * factor;
@@ -43,14 +43,15 @@ export function broadenPeaks(peakList: peakType[], options: optionsType={}) {
for (let i = 0; i < peaks.length - 1; i++) {
let peak = peaks[i];
let nextPeak = peaks[i + 1];
if (peak.to as number > (nextPeak.from as number)) {
peak.to = nextPeak.from = (peak.to as number + (nextPeak.from as number)) / 2;
if ((peak.to as number) > (nextPeak.from as number)) {
peak.to = nextPeak.from =
((peak.to as number) + (nextPeak.from as number)) / 2;
}
}
}

for (let peak of peaks) {
peak.shape.width = peak.to as number - (peak.from as number);
peak.shape.width = (peak.to as number) - (peak.from as number);
}

return peaks.map((peak: peakType) => {
45 changes: 26 additions & 19 deletions src/post/joinBroadPeaks.ts
Original file line number Diff line number Diff line change
@@ -17,9 +17,9 @@ interface peakType {
index?: number;
x: number;
y: number;
shape: shapeType,
from?: number,
to?: number
shape: shapeType;
from?: number;
to?: number;
}
interface shapeType {
kind?: ShapeKind;
@@ -30,32 +30,38 @@ interface shapeType {
noiseLevel?: number;
}
interface optionsType {
width: number,
shape?: shapeType,
optimization?: { kind: string, timeout: number }
width?: number;
shape?: shapeType;
optimization?: { kind: string; timeout: number };
}
export function joinBroadPeaks(peakList: peakType[], options: optionsType = {width:0.25}) {
export function joinBroadPeaks(
peakList: peakType[],
options: optionsType = {},
) {
let {
width = 0.25,
shape = { kind: 'gaussian' },
shape = { kind: 'gaussian', width: 0 },
optimization = { kind: 'lm', timeout: 10 },
} = options;
}: optionsType = options;
let broadLines: peakType[] = [];
// Optimize the possible broad lines
let max= 0;
let max = 0;
let maxI = 0;
let count = 1;

const peaks: peakType[] = JSON.parse(JSON.stringify(peakList));
for (let i:number = peaks.length - 1; i >= 0; i--) {
for (let i: number = peaks.length - 1; i >= 0; i--) {
if (peaks[i].shape.soft) {
broadLines.push(peaks.splice(i, 1)[0]);
}
}
// Push a feke peak
broadLines.push({ x: Number.MAX_VALUE, shape: { width: 0 },y:0 });
broadLines.push({ x: Number.MAX_VALUE, shape: { width: 0 }, y: 0 });

let candidates:{x:number[],y:number[]} = { x: [broadLines[0].x], y: [broadLines[0].y] };
let candidates: { x: number[]; y: number[] } = {
x: [broadLines[0].x],
y: [broadLines[0].y],
};
let indexes: number[] = [0];
for (let i = 1; i < broadLines.length; i++) {
if (Math.abs(broadLines[i - 1].x - broadLines[i].x) < width) {
@@ -69,17 +75,18 @@ export function joinBroadPeaks(peakList: peakType[], options: optionsType = {wid
count++;
} else {
if (count && count > 2) {
let fitted:{peaks:peakType[]}= optimize(
let optimizeShape: shapeType = {
width: Math.abs(
candidates.x[0] - candidates.x[candidates.x.length - 1],
),
};
let fitted: { peaks: peakType[] } = optimize(
candidates,
[
{
x: broadLines[maxI].x,
y: max,
shape: {
width: Math.abs(
candidates.x[0] - candidates.x[candidates.x.length - 1],
),
},
shape: optimizeShape,
},
],
{ shape, optimization },
58 changes: 33 additions & 25 deletions src/post/optimizePeaks.ts
Original file line number Diff line number Diff line change
@@ -4,8 +4,6 @@ import { xGetFromToIndex } from 'ml-spectra-processing';

import { groupPeaks } from './groupPeaks';



/**
* Optimize the position (x), max intensity (y), full width at half maximum (width)
* and the ratio of gaussian contribution (mu) if it's required. It supports three kind of shapes: gaussian, lorentzian and pseudovoigt
@@ -30,9 +28,9 @@ interface peakType {
index?: number;
x: number;
y: number;
shape: shapeType,
from?: number,
to?: number
shape: shapeType;
from?: number;
to?: number;
}
interface shapeType {
kind?: ShapeKind;
@@ -43,56 +41,66 @@ interface shapeType {
noiseLevel?: number;
}
interface optionsType {
factorWidth?: number,
factorLimits?:number,
shape?: shapeType,
factorWidth?: number;
factorLimits?: number;
shape?: shapeType;
optimization?: {
kind: string,
kind: string;
options: {
timeout: number,
},
},
timeout: number;
};
};
}
export function optimizePeaks(data:dataType, peakList:peakType[], options:optionsType = {}) {
export function optimizePeaks(
data: dataType,
peakList: peakType[],
options: optionsType = {},
) {
const {
factorWidth = 1,
factorLimits = 2,
shape = {
kind: 'gaussian'
kind: 'gaussian',
width: 0,
},
optimization = {
kind: 'lm',
options: {
timeout: 10,
},
},
} = options;
}: optionsType = options;

if (data && data.x[0] > data.x[1]) {
data.x.reverse();
data.y.reverse();
}

let groups:peakType[][] = groupPeaks(peakList, factorWidth);
let groups: peakType[][] = groupPeaks(peakList, factorWidth);

let results:peakType[] = [];
let results: peakType[] = [];
for (const peaks of groups) {
const firstPeak:peakType = peaks[0];
const lastPeak:peakType = peaks[peaks.length - 1];
const firstPeak: peakType = peaks[0];
const lastPeak: peakType = peaks[peaks.length - 1];

const from = firstPeak.x - firstPeak.shape.width * factorLimits;
const to = lastPeak.x + lastPeak.shape.width * factorLimits;
const { fromIndex, toIndex }:{fromIndex:number,toIndex:number} = xGetFromToIndex(data.x, { from, to });
const { fromIndex, toIndex }: { fromIndex: number; toIndex: number } =
xGetFromToIndex(data.x, { from, to });
// Multiple peaks
const currentRange:dataType = {
const currentRange: dataType = {
x: data.x.slice(fromIndex, toIndex),
y: data.y.slice(fromIndex, toIndex),
};
if (currentRange && currentRange.x.length > 5) {
let { peaks: optimizedPeaks }:{peaks: peakType[]} = optimize(currentRange, peaks, {
shape,
optimization,
});
let { peaks: optimizedPeaks }: { peaks: peakType[] } = optimize(
currentRange,
peaks,
{
shape,
optimization,
},
);
results = results.concat(optimizedPeaks);
} else {
results = results.concat(peaks);