Skip to content

Commit

Permalink
chore: refactor to use both Filters1DManager n Filters2DManager
Browse files Browse the repository at this point in the history
  • Loading branch information
jobo322 committed Nov 4, 2024
1 parent 0372969 commit 5aad9a6
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 86 deletions.
4 changes: 2 additions & 2 deletions src/component/header/SimpleZeroFillingOptionsPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Checkbox } from '@blueprintjs/core';
import { Filters } from 'nmr-processing';
import { Filters1D } from 'nmr-processing';

import ActionButtons from '../elements/ActionButtons.js';
import Label from '../elements/Label.js';
Expand All @@ -14,7 +14,7 @@ import { headerLabelStyle } from './Header.js';
import { HeaderWrapper } from './HeaderWrapper.js';

export function SimpleZeroFillingOptionsPanel() {
const filter = useFilter(Filters.zeroFilling.id);
const filter = useFilter(Filters1D.zeroFilling.id);
const { control, submitHandler, register, handleCancelFilter } =
useZeroFilling(filter, { applyFilterOnload: true });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import styled from '@emotion/styled';
import { yupResolver } from '@hookform/resolvers/yup';
import { SvgNmrMultipleAnalysis } from 'cheminfo-font';
import { Filters } from 'nmr-processing';
import { Filters1D } from 'nmr-processing';
import { useCallback, useEffect, useRef } from 'react';
import { FormProvider, useForm } from 'react-hook-form';
import { Button, Toolbar } from 'react-science/ui';
Expand Down Expand Up @@ -31,7 +31,7 @@ import { ExclusionsZonesTable } from './ExclusionsZonesTable.js';
import { FiltersOptions } from './FiltersOptions.js';
import { MatrixGenerationPanelHeader } from './MatrixGenerationPanelHeader.js';

const { signalProcessing } = Filters;
const { signalProcessing } = Filters1D;

const StickyFooter = styled.div({
position: 'sticky',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @jsxImportSource @emotion/react */
import { Select } from '@blueprintjs/select';
import { Filters } from 'nmr-processing';
import { Filters2D } from 'nmr-processing';
import type { CSSProperties } from 'react';
import { FaRulerHorizontal, FaRulerVertical } from 'react-icons/fa';
import { MdLooksTwo } from 'react-icons/md';
Expand Down Expand Up @@ -46,7 +46,7 @@ export default function PhaseCorrectionTwoDimensionsOptionsPanel(
) {
const { enableEdit = true, onCancel, onConfirm } = props;

const filter = useFilter(Filters.phaseCorrectionTwoDimensions.id);
const filter = useFilter(Filters2D.phaseCorrectionTwoDimensions.id);
const {
ph0Ref,
ph1Ref,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Filters } from 'nmr-processing';
import { Filters1D, Filters2D } from 'nmr-processing';
import { useForm } from 'react-hook-form';

import { useDispatch } from '../../../context/DispatchContext.js';
Expand All @@ -13,7 +13,8 @@ import { HeaderContainer, StickyHeader } from './InnerFilterHeader.js';
import type { BaseFilterOptionsPanelProps } from './index.js';
import { formLabelStyle } from './index.js';

const { shiftX, shift2DX, shift2DY } = Filters;
const { shiftX } = Filters1D;
const { shift2DX, shift2DY } = Filters2D;

export default function ShiftOptionsPanel(props: BaseFilterOptionsPanelProps) {
const { filter, enableEdit = true, onCancel, onConfirm } = props;
Expand Down
4 changes: 2 additions & 2 deletions src/component/panels/filtersPanel/Filters/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ButtonProps } from '@blueprintjs/core';

Check failure on line 1 in src/component/panels/filtersPanel/Filters/index.tsx

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

All imports in the declaration are only used as types. Use `import type`

import type { Filter1D } from 'nmr-processing';
import type { Filter1DOptions, Filter2DOptions } from 'nmr-processing';
import { Filters1D, Filters2D } from 'nmr-processing';

import type { LabelStyle } from '../../../elements/Label.js';
Expand Down Expand Up @@ -37,7 +37,7 @@ export const filterOptionPanels = {
};

export interface BaseFilterOptionsPanelProps {
filter: Extract<Filter1D, { name: 'baselineCorrection' }>;
filter: Filter1DOptions | Filter2DOptions;
enableEdit: boolean;
onConfirm: ButtonProps['onClick'];
onCancel: ButtonProps['onClick'];
Expand Down
103 changes: 68 additions & 35 deletions src/component/reducer/actions/FiltersActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import type {
} from 'nmr-load-save';
import type { BaselineCorrectionOptions } from 'nmr-processing';

Check failure on line 12 in src/component/reducer/actions/FiltersActions.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

'/home/runner/work/nmrium/nmrium/node_modules/nmr-processing/nmr-processing.mjs' imported multiple times
import {
BaselineCorrectionOptions,
getBaselineZonesByDietrich,
Filters1DManager,
Filters2DManager,
Expand All @@ -20,7 +19,7 @@ import {
} from 'nmr-processing';

import { defaultApodizationOptions } from '../../../data/constants/DefaultApodizationOptions.js';
import type { Apodization1DOptions } from '../../../data/constants/DefaultApodizationOptions.js';
import type { Apodization1DOptions } from 'nmr-processing';

Check failure on line 22 in src/component/reducer/actions/FiltersActions.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

'/home/runner/work/nmrium/nmrium/node_modules/nmr-processing/nmr-processing.mjs' imported multiple times
import { isSpectrum1D } from '../../../data/data1d/Spectrum1D/index.js';
import { getProjection } from '../../../data/data2d/Spectrum2D/getMissingProjection.js';
import { isSpectrum2D } from '../../../data/data2d/Spectrum2D/index.js';
Expand Down Expand Up @@ -216,7 +215,7 @@ function getFilterUpdateDomainRules(
defaultRule?: Filters1DManager.FilterDomainUpdateRules,
) {
return (
Filters[filterName]?.DOMAIN_UPDATE_RULES ||
Filters1D[filterName]?.DOMAIN_UPDATE_RULES ||
defaultRule || {
updateXDomain: false,
updateYDomain: false,
Expand Down Expand Up @@ -273,7 +272,7 @@ function rollbackSpectrumByFilter(
} = options || {};

const currentActiveSpectrum = activeSpectrum || getActiveSpectrum(draft);
let updateDomainOptions: Partial<FiltersManager.FilterDomainUpdateRules> = {
let updateDomainOptions: Partial<Filters1DManager.FilterDomainUpdateRules> = {
updateXDomain: false,
updateYDomain: false,
};
Expand Down Expand Up @@ -312,7 +311,11 @@ function rollbackSpectrumByFilter(
// apply the current Filters
if (applyFilter) {
const { name, value } = datum.filters[filterIndex];
Filters[name].apply(datum, value);
if (datum.info.dimension === 1) {
Filters1D[name].apply(datum, value);
} else {
Filters2D[name].apply(datum, value);
}
}

currentIsFid = datum.info.isFid;
Expand Down Expand Up @@ -392,7 +395,7 @@ function rollbackSpectrum(
signalProcessing.id,
digitalFilter.id,
digitalFilter2D.id,
].includes(filterKey);
].includes(filterKey as any);

beforeRollback(draft, filterKey);

Expand Down Expand Up @@ -569,7 +572,7 @@ function getActiveFilterIndex(draft: Draft<State>) {

function updateView(
draft: Draft<State>,
filterUpdateDomainRules: Readonly<FiltersManager.FilterDomainUpdateRules>,
filterUpdateDomainRules: Readonly<Filters1DManager.FilterDomainUpdateRules>,
) {
draft.tempData = null;
const { updateXDomain, updateYDomain } = filterUpdateDomainRules;
Expand Down Expand Up @@ -629,7 +632,7 @@ function handleShiftSpectrumAlongXAxis(
if (isOneDimensionShift(options)) {
const { shift } = options;

FiltersManager.applyFilters(draft.data[index], [
Filters1DManager.applyFilters(draft.data[index] as Spectrum1D, [
{ name: shiftX.id, value: { shift } },
]);

Expand All @@ -638,14 +641,14 @@ function handleShiftSpectrumAlongXAxis(
const { shiftX, shiftY } = options;

if (shiftX) {
FiltersManager.applyFilters(draft.data[index], [
Filters2DManager.applyFilters(draft.data[index] as Spectrum2D, [
{ name: shift2DX.id, value: { shift: shiftX } },
]);
updateView(draft, shift2DX.DOMAIN_UPDATE_RULES);
}

if (shiftY) {
FiltersManager.applyFilters(draft.data[index], [
Filters2DManager.applyFilters(draft.data[index] as Spectrum2D, [
{ name: shift2DY.id, value: { shift: shiftY } },
]);

Expand All @@ -672,7 +675,7 @@ function handleApplyZeroFillingFilter(
value: action.payload.options,
},
];
FiltersManager.applyFilters(draft.tempData[index], filters);
Filters1DManager.applyFilters(draft.tempData[index], filters);
draft.data[index] = draft.tempData[index];

updateView(draft, zeroFilling.DOMAIN_UPDATE_RULES);
Expand Down Expand Up @@ -780,7 +783,7 @@ function handleApplyFFTFilter(draft: Draft<State>) {
//apply filter into the spectrum
Filters1DManager.applyFilters(
activeFilterIndex !== -1 ? draft.tempData[index] : draft.data[index],
[{ name: fft.id }],
[{ name: fft.id, value: {} }],
);

if (activeFilterIndex !== -1) {
Expand Down Expand Up @@ -836,7 +839,7 @@ function handleApplyFFtDimension2Filter(draft: Draft<State>) {
const activeFilterIndex = getActiveFilterIndex(draft);

//apply filter into the spectrum
FiltersManager.applyFilters(
Filters2DManager.applyFilters(
activeFilterIndex !== -1 ? draft.tempData[index] : draft.data[index],
[{ name: fftDimension2.id, value: {} }],
);
Expand Down Expand Up @@ -869,7 +872,7 @@ function handleApplyManualPhaseCorrectionFilter(
const { ph0, ph1 } = action.payload;
draft.data = draft.tempData;

FiltersManager.applyFilters(draft.tempData[index], [
Filters1DManager.applyFilters(draft.tempData[index], [
{
name: phaseCorrection.id,
value: { ph0, ph1 },
Expand Down Expand Up @@ -1008,7 +1011,7 @@ function handleApplyAbsoluteFilter(draft: Draft<State>) {

const { index } = activeSpectrum;

FiltersManager.applyFilters(draft.tempData[index], [
Filters1DManager.applyFilters(draft.tempData[index], [
{
name: phaseCorrection.id,
value: { absolute: true },
Expand All @@ -1029,7 +1032,7 @@ function handleApplyAutoPhaseCorrectionFilter(draft: Draft<State>) {

const { index } = activeSpectrum;

FiltersManager.applyFilters(draft.tempData[index], [
Filters1DManager.applyFilters(draft.tempData[index], [
{
name: phaseCorrection.id,
value: {},
Expand All @@ -1055,7 +1058,7 @@ function handleBaseLineCorrectionFilter(
const { index } = activeSpectrum;
const { zones } = draft.toolOptions.data.baselineCorrection;
const { options } = action.payload;
FiltersManager.applyFilters(draft.tempData[index], [
Filters1DManager.applyFilters(draft.tempData[index], [
{
name: baselineCorrection.id,
value: {
Expand Down Expand Up @@ -1124,11 +1127,19 @@ function handleEnableFilter(draft: Draft<State>, action: EnableFilterAction) {
const { id: filterID, enabled } = action.payload;

//apply filter into the spectrum
FiltersManager.enableFilter(
draft.data[activeSpectrum.index],
filterID,
enabled,
);
if (draft.data[activeSpectrum.index].info.dimension === 1) {
Filters1DManager.enableFilter(
draft.data[activeSpectrum.index] as Spectrum1D,
filterID,
enabled,
);
} else {
Filters2DManager.enableFilter(
draft.data[activeSpectrum.index] as Spectrum2D,
filterID,
enabled,
);
}

resetSelectedTool(draft);
setDomain(draft);
Expand Down Expand Up @@ -1157,7 +1168,17 @@ function handleDeleteFilter(draft: Draft<State>, action: DeleteFilterAction) {
const filterID = action?.payload?.id;

//apply filter into the spectrum
FiltersManager.deleteFilter(draft.data[activeSpectrum.index], filterID);
if (draft.data[activeSpectrum.index].info.dimension === 1) {
Filters1DManager.deleteFilter(
draft.data[activeSpectrum.index] as Spectrum1D,
filterID,
);
} else {
Filters2DManager.deleteFilter(
draft.data[activeSpectrum.index] as Spectrum2D,
filterID,
);
}
draft.toolOptions.data.activeFilterID = null;
resetSelectedTool(draft);
setDomain(draft);
Expand All @@ -1180,7 +1201,11 @@ function handleDeleteSpectraFilter(
datum.filters?.filter((filter) => filter.name === filterName) || [];

for (const filter of filtersResult) {
FiltersManager.deleteFilter(datum, filter.id);
if (datum.info.dimension === 1) {
Filters1DManager.deleteFilter(datum as Spectrum1D, filter.id);
} else {
Filters2DManager.deleteFilter(datum as Spectrum2D, filter.id);
}
}
}
}
Expand Down Expand Up @@ -1220,7 +1245,7 @@ function handleSignalProcessingFilter(

const spectra = getSpectraByNucleus(nucleus, data) as Spectrum1D[];
for (const spectrum of spectra) {
FiltersManager.applyFilters(
Filters1DManager.applyFilters(
spectrum,
[
{
Expand Down Expand Up @@ -1249,12 +1274,15 @@ function handleApplyExclusionZone(
return;
}

FiltersManager.applyFilters(draft.data[activeSpectrum.index], [
{
name: exclusionZones.id,
value: zones,
},
]);
Filters1DManager.applyFilters(
draft.data[activeSpectrum.index] as Spectrum1D,
[
{
name: exclusionZones.id,
value: zones,
},
],
);

const { updateXDomain, updateYDomain } = exclusionZones.DOMAIN_UPDATE_RULES;

Expand Down Expand Up @@ -1282,7 +1310,7 @@ function handleAddExclusionZone(
}

for (const spectrum of spectra) {
FiltersManager.applyFilters(spectrum, [
Filters1DManager.applyFilters(spectrum, [
{
name: exclusionZones.id,
value: [
Expand Down Expand Up @@ -1318,10 +1346,15 @@ function handleDeleteExclusionZone(
);
if (filter) {
if (filter.value.length === 1) {
FiltersManager.deleteFilter(draft.data[spectrumIndex], filter.id);
Filters1DManager.deleteFilter(
draft.data[spectrumIndex] as Spectrum1D,
filter.id,
);
} else {
filter.value = filter.value.filter((_zone) => _zone.id !== zone?.id);
FiltersManager.reapplyFilters(draft.data[spectrumIndex]);
Filters1DManager.reapplyFilters(
draft.data[spectrumIndex] as Spectrum1D,
);
}
}
} else {
Expand All @@ -1333,7 +1366,7 @@ function handleDeleteExclusionZone(
filter.value = filter.value.filter(
(_zone) => zone.from !== _zone.from && zone.to !== _zone.to,
);
FiltersManager.reapplyFilters(datum);
Filters1DManager.reapplyFilters(datum as Spectrum1D);
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/component/reducer/actions/SpectraActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {
Spectrum2D,
} from 'nmr-load-save';
import type { NMRRange } from 'nmr-processing';
import { Filters, FiltersManager } from 'nmr-processing';
import { Filters1D, Filters1DManager } from 'nmr-processing';

import {
get1DColor,
Expand Down Expand Up @@ -169,7 +169,6 @@ export type SpectrumActions =
| SimulateSpectrumAction
| UpdateSpectrumMetaAction;

const { applyFilters } = FiltersManager;
function checkIsVisible2D(datum: Spectrum2D): boolean {
if (!datum.display.isPositiveVisible && !datum.display.isNegativeVisible) {
return false;
Expand Down Expand Up @@ -548,9 +547,9 @@ function handleAlignSpectraHandler(
!datum.info?.isFid
) {
const shift = getReferenceShift(datum, { ...action.payload });
applyFilters(datum, [
Filters1DManager.applyFilters(datum as Spectrum1D, [
{
name: Filters.shiftX.id,
name: Filters1D.shiftX.id,
value: { shift },
},
]);
Expand Down
Loading

0 comments on commit 5aad9a6

Please sign in to comment.