Skip to content

Commit

Permalink
fix: peaks, signals, ranges, zones with id (#1732)
Browse files Browse the repository at this point in the history
* chore: black filling in peak shapes

* fix(fft): ensure length is a power of two

ensure that data has the same length
pad data to the next power of two
close: #1688

* chore: update ml-gsd n prerelease nmr-processing

* chore: update pre-release nmr-processing

* chore: less code

* chore: return caret

* chore: remove unexpected data-test-id

* chore: fix fft

* chore: update nmr-processing
  • Loading branch information
jobo322 committed Sep 1, 2022
1 parent 179e4a0 commit bdb4daa
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 21 deletions.
32 changes: 17 additions & 15 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -70,13 +70,13 @@
"ml-array-xy-equally-spaced": "^1.2.1",
"ml-baseline-correction-regression": "^1.0.0",
"ml-conrec": "^3.2.1",
"ml-gsd": "^12.0.0",
"ml-gsd": "^12.1.0",
"ml-spectra-processing": "^11.12.0",
"ml-stat": "^1.3.3",
"multiplet-analysis": "^2.0.0",
"nmr-correlation": "^2.3.3",
"nmr-parser": "^1.9.1",
"nmr-processing": "^9.0.3",
"nmr-processing": "^9.1.0",
"nmredata": "^0.7.0",
"numeral": "^2.0.6",
"openchemlib": "^8.0.1",
Expand Down
2 changes: 1 addition & 1 deletion src/component/1d/peaks/PeaksShapes.tsx
Expand Up @@ -45,10 +45,10 @@ function PeaksShapesItems(props: { vAlign: number }) {
{' '}
{spectrum.peaks.values.map((peak) => {
const { fill, path } = getPath({ target: 'peakShape', peak });

return (
<path
key={peak.id}
stroke={'red'}
fill={fill}
fillOpacity={0.3}
d={path}
Expand Down
3 changes: 1 addition & 2 deletions src/component/1d/peaks/usePeakShapesPath.ts
Expand Up @@ -19,7 +19,6 @@ type PeaksShapesOptions =
export function usePeakShapesPath(spectrum: Datum1D) {
const { scaleX, scaleY } = useScaleChecked();
const { width, xDomain } = useChartData();

return function getPath(options: PeaksShapesOptions): {
path: string;
fill: string;
Expand Down Expand Up @@ -59,7 +58,7 @@ export function usePeakShapesPath(spectrum: Datum1D) {

if (target === 'peakShape') {
pathBuilder.closePath();
fill = 'red';
fill = 'black';
}
}

Expand Down
1 change: 1 addition & 0 deletions src/data/data1d/Spectrum1D/peaks/autoPeakPicking.ts
Expand Up @@ -37,6 +37,7 @@ export function autoPeakPicking(datum1D: Datum1D, options) {
sgOptions: { windowSize: 15, polynomial: 3 },
},
);

peaks.sort((a, b) => b.y - a.y);
if (maxNumberOfPeaks < peaks.length) peaks = peaks.slice(0, maxNumberOfPeaks);
return mapPeaks(peaks as Peak[], datum1D);
Expand Down
15 changes: 14 additions & 1 deletion src/data/data1d/filter1d/fft.ts
Expand Up @@ -2,6 +2,8 @@ import { reimFFT, reimPhaseCorrection } from 'ml-spectra-processing';

import { Datum1D } from '../../types/data1d/Datum1D';

import { padDataToNextPowerOfTwo } from './utils/padDataToNextPowerOfTwo';

export const id = 'fft';
export const name = 'FFT';

Expand All @@ -19,7 +21,14 @@ export function apply(datum1D: Datum1D) {
(e) => e.name === 'digitalFilter' && e.flag,
);

Object.assign(datum1D.data, reimFFT(datum1D.data, { applyZeroShift: true }));
const { data } = datum1D;
if (data.x.length !== data.re.length || data.x.length !== data.im.length) {
throw new Error('The length of data should be equal');
} else if (!isPowerOfTwo(datum1D.data.x.length)) {
padDataToNextPowerOfTwo(datum1D, digitalFilterApplied);
}

Object.assign(datum1D.data, reimFFT(data, { applyZeroShift: true }));

if (digitalFilterApplied) {
let { digitalFilter = 0 } = datum1D.info;
Expand Down Expand Up @@ -60,3 +69,7 @@ function generateXAxis(datum1D) {
}
return xAxis;
}

function isPowerOfTwo(n) {
return n !== 0 && (n & (n - 1)) === 0;
}
48 changes: 48 additions & 0 deletions src/data/data1d/filter1d/utils/padDataToNextPowerOfTwo.ts
@@ -0,0 +1,48 @@
import { xSequentialFill } from 'ml-spectra-processing';

import { Datum1D } from '../../../types/data1d';

export function padDataToNextPowerOfTwo(
datum1D: Datum1D,
digitalFilterApplied: boolean,
) {
const { x, re, im } = datum1D.data;
const size = nextPowerOfTwo(x.length);

let newRE = new Float64Array(size);
let newIM = new Float64Array(size);

const pointsToShift = getPointsToShift(datum1D);

newRE.set(re.slice(0, length - pointsToShift));
newIM.set(im.slice(0, length - pointsToShift));

if (pointsToShift > 0 && digitalFilterApplied) {
newRE.set(re.slice(re.length - pointsToShift), size - pointsToShift);
newIM.set(im.slice(re.length - pointsToShift), size - pointsToShift);
}

const newX = xSequentialFill({
from: x[0],
size,
step: x[1] - x[0],
}) as Float64Array;

datum1D.data = { ...datum1D.data, ...{ re: newRE, im: newIM, x: newX } };
}

function getPointsToShift(datum1D: Datum1D) {
let grpdly = datum1D.info?.digitalFilter || 0;
return grpdly > 0 ? Math.floor(grpdly) : 0;
}

function nextPowerOfTwo(n: number) {
if (n === 0) return 1;
n--;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
return n + 1;
}

0 comments on commit bdb4daa

Please sign in to comment.