Skip to content

Commit

Permalink
fix: apply phase correction to the region n reduce critFoundJ to 0.75
Browse files Browse the repository at this point in the history
  • Loading branch information
jobo322 committed Dec 3, 2024
1 parent b8d5bd7 commit 75b19dc
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 22 deletions.
28 changes: 10 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"ml-spectra-processing": "^14.6.2",
"ml-stat": "^1.3.3",
"ml-tree-similarity": "^2.2.0",
"multiplet-analysis": "^2.1.2",
"multiplet-analysis": "^2.1.3",
"nmr-correlation": "^2.3.3",
"nmr-load-save": "^2.1.0",
"nmr-processing": "^14.0.3",
Expand Down
64 changes: 61 additions & 3 deletions src/component/modal/MultipletAnalysisModal.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/** @jsxImportSource @emotion/react */
import { Dialog, DialogBody } from '@blueprintjs/core';
import { css } from '@emotion/react';
import { xGetFromToIndex, xyToXYObject } from 'ml-spectra-processing';
import type { DoubleArray } from 'cheminfo-types';
import {
reimPhaseCorrection,
xGetFromToIndex,
xyToXYObject,
} from 'ml-spectra-processing';
import { analyseMultiplet } from 'multiplet-analysis';
import type { ActiveSpectrum, Spectrum } from 'nmr-load-save';
import { useEffect, useState } from 'react';
Expand Down Expand Up @@ -142,7 +147,7 @@ function InnerMultipleAnalysis(props: InnerMultipleAnalysisProps) {
}

const {
data: { x, re },
data: { x, re, im },
info,
} = spectrum;

Expand All @@ -153,18 +158,34 @@ function InnerMultipleAnalysis(props: InnerMultipleAnalysisProps) {
from,
to,
});

let currentRe = re.slice(fromIndex, toIndex);
if (im) {
const currentIm = im.slice(fromIndex, toIndex);
const ph0 = autoPhaseRegion(currentRe, currentIm);
const phased = reimPhaseCorrection(
{ re: currentRe, im: currentIm },
toRadians(ph0),
0,
);
currentRe = phased.re;
}

const analysesProps = {
x: x.slice(fromIndex, toIndex),
y: re.slice(fromIndex, toIndex),
};

try {
const result = analyseMultiplet(analysesProps, {
frequency: info.originFrequency,
minimalResolution: 0.1,
critFoundJ: 0.75,
maxTestedJ: 17,
minTestedJ: 1,
takeBestPartMultiplet: true,
correctVerticalOffset: true,
symmetrizeEachStep: true,
symmetrizeEachStep: false,
decreasingJvalues: true,
makeShortCutForSpeed: true,
debug: true,
Expand Down Expand Up @@ -261,3 +282,40 @@ function InnerMultipleAnalysis(props: InnerMultipleAnalysisProps) {
</div>
);
}

function autoPhaseRegion(re: DoubleArray, im: DoubleArray): any {
let start = -180;
let stop = 180;
const nSteps = 6;
let maxSteps = 10;

let bestAng = 0;
let minArea = Number.MAX_SAFE_INTEGER;
while (maxSteps > 0) {
const dAng = (stop - start) / (nSteps + 1);
for (let i = start; i <= stop; i += dAng) {
const tmpPhased = reimPhaseCorrection({ re, im }, toRadians(i), 0);
const negArea = getNegArea(tmpPhased.re);
if (negArea < minArea) {
[minArea, bestAng] = [negArea, i];
}
}
start = bestAng - dAng;
stop = bestAng + dAng;
maxSteps--;
}

return bestAng;
}

function toRadians(degree: number): number {
return (degree * Math.PI) / 180;
}

function getNegArea(data: DoubleArray): number {
let area = 0;
for (const element of data) {
if (element < 0) area -= element;
}
return area;
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export function detectSignalsByMultipletAnalysis(
checkSymmetryFirst: true,
takeBestPartMultiplet: true,
correctVerticalOffset: true,
critFoundJ: 0.75,
symmetrizeEachStep: false,
decreasingJvalues: true,
makeShortCutForSpeed: true,
Expand Down

0 comments on commit 75b19dc

Please sign in to comment.