Skip to content
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
18 changes: 10 additions & 8 deletions src/component/header/AutoPeakPickingOptionPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import Label from '../elements/Label.js';
import { NumberInput2Controller } from '../elements/NumberInput2Controller.js';
import { Select2Controller } from '../elements/Select2Controller.js';
import { useActiveNucleusTab } from '../hooks/useActiveNucleusTab.ts';
import { useActiveSpectra } from '../hooks/useActiveSpectra.ts';
import {
MIN_AREA_POINTS,
useCheckPointsNumberInWindowArea,
useCheckPointsNumberInSelectedSpectra,
} from '../hooks/useCheckPointsNumberInWindowArea.js';
import { usePanelPreferences } from '../hooks/usePanelPreferences.ts';

Expand Down Expand Up @@ -43,7 +44,7 @@ interface AutoPeakPickingOptions {
}

const validationSchema = Yup.object().shape({
maxNumberOfPeaks: Yup.number().min(0).required(),
maxNumberOfPeaks: Yup.number().min(1).required(),
minMaxRatio: Yup.number().min(0).required(),
noiseFactor: Yup.number().min(0).required(),
direction: Yup.mixed<Direction>()
Expand All @@ -55,14 +56,16 @@ const INIT_VALUES: AutoPeakPickingOptions = {
maxNumberOfPeaks: 50,
minMaxRatio: 0.05,
noiseFactor: 3,
direction: 'positive',
direction: 'both',
};

export function AutoPeakPickingOptionPanel() {
const dispatch = useDispatch();
const pointsNumber = useCheckPointsNumberInWindowArea();
const hasEnoughPoints = useCheckPointsNumberInSelectedSpectra();
const toaster = useToaster();
const nucleus = useActiveNucleusTab();
const activeSpectra = useActiveSpectra();

const { defaultPeakShape } = usePanelPreferences('peaks', nucleus);
const {
handleSubmit,
Expand All @@ -75,7 +78,7 @@ export function AutoPeakPickingOptionPanel() {
});

function handlePeakPicking(values: any) {
if (pointsNumber > MIN_AREA_POINTS) {
if (hasEnoughPoints) {
dispatch({
type: 'AUTO_PEAK_PICKING',
payload: {
Expand All @@ -90,7 +93,6 @@ export function AutoPeakPickingOptionPanel() {
});
}
}

return (
<HeaderWrapper>
<Label title="Direction:" shortTitle="" style={headerLabelStyle}>
Expand All @@ -104,7 +106,7 @@ export function AutoPeakPickingOptionPanel() {
<NumberInput2Controller
control={control}
name="maxNumberOfPeaks"
min={0}
min={1}
stepSize={1}
style={{ width: '60px' }}
/>
Expand Down Expand Up @@ -140,7 +142,7 @@ export function AutoPeakPickingOptionPanel() {
style={{ margin: '0 10px' }}
disabled={!isValid}
>
Apply
Apply to {activeSpectra?.length || 'all'} spectra
</Button>
</HeaderWrapper>
);
Expand Down
4 changes: 2 additions & 2 deletions src/component/header/RangesPickingOptionPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ function RangesPickingOptionPanel() {
resolver: yupResolver(validationSchema),
mode: 'onChange',
});
const pointsNumber = useCheckPointsNumberInWindowArea();
const hasEnoughPoints = useCheckPointsNumberInWindowArea();
const toaster = useToaster();

function handleRangesPicking(values: any) {
if (pointsNumber > MIN_AREA_POINTS) {
if (hasEnoughPoints) {
dispatch({
type: 'AUTO_RANGES_DETECTION',
payload: values,
Expand Down
54 changes: 41 additions & 13 deletions src/component/hooks/useCheckPointsNumberInWindowArea.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,57 @@
import type { Spectrum1D } from '@zakodium/nmrium-core';
import { xGetFromToIndex } from 'ml-spectra-processing';
import type { Spectrum } from 'nmr-correlation';

import { isSpectrum1D } from '../../data/data1d/Spectrum1D/isSpectrum1D.ts';
import { useChartData } from '../context/ChartContext.js';

import useSpectrum from './useSpectrum.js';
import { useSelectedSpectra } from './useSelectedSpectra.ts';
import useSpectraByActiveNucleus from './useSpectraPerNucleus.ts';
import useSpectrum from './useSpectrum.ts';

export const MIN_AREA_POINTS = 5;

function checkPointNumberInWindowArea(
spectra: Spectrum | Spectrum[],
from: number,
to: number,
) {
const spectraArray = Array.isArray(spectra) ? spectra : [spectra];
const filteredSpectra = spectraArray.filter(isSpectrum1D);

if (filteredSpectra.length === 0) {
return false;
}

return filteredSpectra.every((spectrum) => {
const { fromIndex, toIndex } = xGetFromToIndex(spectrum.data.x, {
from,
to,
});
return toIndex - fromIndex > MIN_AREA_POINTS;
});
}

export function useCheckPointsNumberInWindowArea() {
const state = useChartData();
const spectrum = useSpectrum(null);
const {
xDomain: [from, to],
} = state;

if (spectrum) {
const { fromIndex, toIndex } = xGetFromToIndex(
(spectrum as Spectrum1D).data.x,
{
from,
to,
},
);
return toIndex - fromIndex;
}
return checkPointNumberInWindowArea(spectrum, from, to);
}

export function useCheckPointsNumberInSelectedSpectra() {
const state = useChartData();
const spectra = useSelectedSpectra();
const spectraByActiveNucleus = useSpectraByActiveNucleus();
const {
xDomain: [from, to],
} = state;

return 0;
return checkPointNumberInWindowArea(
spectra ?? spectraByActiveNucleus,
from,
to,
);
}
26 changes: 26 additions & 0 deletions src/component/hooks/useSelectedSpectra.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { Spectrum } from '@zakodium/nmrium-core';
import { useMemo } from 'react';

import { useChartData } from '../context/ChartContext.tsx';

import { useActiveSpectra } from './useActiveSpectra.ts';

export function useSelectedSpectra() {
const activeSpectrum = useActiveSpectra();
const { data } = useChartData();

return useMemo<Spectrum[] | null>(() => {
const spectra = [];

if (!activeSpectrum || activeSpectrum?.length === 0) return null;

for (const active of activeSpectrum) {
const spectrum = data?.[active.index];
if (spectrum) {
spectra.push(spectrum);
}
}

return spectra;
}, [activeSpectrum, data]);
}
48 changes: 34 additions & 14 deletions src/component/reducer/actions/PeaksActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Peak1D } from '@zakodium/nmr-types';
import type {
PeaksViewState,
RangesViewState,
Spectrum,
ViewState,
} from '@zakodium/nmrium-core';
import type { Draft } from 'immer';
Expand All @@ -21,7 +22,9 @@ import { defaultPeaksViewState } from '../../hooks/useActiveSpectrumPeaksViewSta
import { getDefaultRangesViewState } from '../../hooks/useActiveSpectrumRangesViewState.js';
import type { FilterType } from '../../utility/filterType.js';
import { getClosePeak } from '../../utility/getClosePeak.js';
import { getSpectraByNucleus } from '../../utility/getSpectraByNucleus.ts';
import type { State } from '../Reducer.js';
import { getActiveSpectra } from '../helper/getActiveSpectra.ts';
import { getActiveSpectrum } from '../helper/getActiveSpectrum.js';
import getRange from '../helper/getRange.js';
import { getSpectrum } from '../helper/getSpectrum.js';
Expand Down Expand Up @@ -179,23 +182,40 @@ function handleAutoPeakPicking(
) {
const { options, defaultPeakShape } = action.payload;

const spectrum = getSpectrum(draft);
if (!isSpectrum1D(spectrum)) return;
const activeSpectra = getActiveSpectra(draft);

draft.toolOptions.selectedTool = 'zoom';
draft.toolOptions.selectedOptionPanel = null;
let spectra: Spectrum[] = [];

if (!activeSpectra || activeSpectra.length === 0) {
spectra = getSpectraByNucleus(draft.view.spectra.activeTab, draft.data);
} else {
for (const activeSpectrum of activeSpectra) {
const spectrum = getSpectrum(draft, activeSpectrum.index);
if (spectrum) {
spectra.push(spectrum);
}
}
}

const [from, to] = draft.xDomain;
const windowFromIndex = xFindClosestIndex(spectrum.data.x, from);
const windowToIndex = xFindClosestIndex(spectrum.data.x, to);

const peaks = autoPeakPicking(spectrum, {
...options,
windowFromIndex,
windowToIndex,
defaultPeakShape,
});
spectrum.peaks.values = spectrum.peaks.values.concat(peaks);

for (const spectrum of spectra) {
if (!isSpectrum1D(spectrum)) continue;

const windowFromIndex = xFindClosestIndex(spectrum.data.x, from);
const windowToIndex = xFindClosestIndex(spectrum.data.x, to);

const peaks = autoPeakPicking(spectrum, {
...options,
windowFromIndex,
windowToIndex,
defaultPeakShape,
});
spectrum.peaks.values = spectrum.peaks.values.concat(peaks);
}

draft.toolOptions.selectedTool = 'zoom';
draft.toolOptions.selectedOptionPanel = null;
}

//action
Expand Down
3 changes: 3 additions & 0 deletions src/component/toolbar/ToolTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ export const options: RecordOptions = {
info: [{ key: 'isFt', value: true }],
active: true,
},
{
active: false,
},
],
isToggle: true,
},
Expand Down
2 changes: 1 addition & 1 deletion test-e2e/panels/filters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ test('process 13c spectrum with shortcuts', async ({ page }) => {
await addPeaks(nmrium, { keyboard: true });
});
await test.step('Check peaks table', async () => {
await checkPeakNumber(nmrium, 16);
await checkPeakNumber(nmrium, 50);
});
await test.step('Check filters panel', async () => {
await expect(
Expand Down
Loading