From 2aeb9e11790bb3fa7f1354ffed986919ec749fa0 Mon Sep 17 00:00:00 2001 From: hamed-musallam <35760236+hamed-musallam@users.noreply.github.com> Date: Tue, 7 Nov 2023 15:32:06 +0100 Subject: [PATCH] refactor: move auto processing to nmr-load-save (#2737) * feat: update nmr-load-save * feat: manage auto processing by nmr-load-save * fix: digitalFilter applied by nmr-load-save * chore: fix sourceSelector option --------- Co-authored-by: jobo322 --- package-lock.json | 12 +- package.json | 4 +- src/component/loader/DropZone.tsx | 6 +- .../OnLoadProcessingTabContent.tsx | 37 +++-- src/component/reducer/actions/LoadActions.ts | 36 +---- src/component/workspaces/exercise.ts | 1 + src/component/workspaces/prediction.ts | 1 + src/component/workspaces/simulation.ts | 1 + .../workspaces/workspaceDefaultProperties.ts | 132 +++++++++--------- src/data/data1d/Spectrum1D/initiateDatum1D.ts | 40 +----- 10 files changed, 120 insertions(+), 150 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8a66b67d1..18d39a83d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -39,7 +39,7 @@ "ml-stat": "^1.3.3", "multiplet-analysis": "^2.1.2", "nmr-correlation": "^2.3.3", - "nmr-load-save": "^0.22.4", + "nmr-load-save": "^0.23.2", "nmr-processing": "^11.5.0", "nmredata": "^0.9.7", "numeral": "^2.0.6", @@ -9876,9 +9876,9 @@ } }, "node_modules/nmr-load-save": { - "version": "0.22.4", - "resolved": "https://registry.npmjs.org/nmr-load-save/-/nmr-load-save-0.22.4.tgz", - "integrity": "sha512-U3HAOz67ilLKLKoCKVQciGma8e/FPE9bM/J9k+1BJEU+JgmWuLPt4bqIoGz8E2A5huvKD1/JW38SrMXZvOWCVg==", + "version": "0.23.2", + "resolved": "https://registry.npmjs.org/nmr-load-save/-/nmr-load-save-0.23.2.tgz", + "integrity": "sha512-QSBYdjENOAhznJoZWYzn8aLQc3Df24xSWzwn/maK5dHd015XKy35p4+O5u2KmGW6lmyF8KysWemjmiKHUXdv7Q==", "dependencies": { "@lukeed/uuid": "^2.0.1", "@types/lodash.merge": "^4.6.7", @@ -9893,7 +9893,7 @@ "lodash.merge": "^4.6.2", "ml-spectra-processing": "^12.5.1", "nmr-correlation": "^2.3.3", - "nmr-processing": "^11.4.1", + "nmr-processing": "^11.5.0", "nmredata": "^0.9.7", "openchemlib": "^8.7.0", "openchemlib-utils": "^5.4.0", @@ -14080,4 +14080,4 @@ } } } -} +} \ No newline at end of file diff --git a/package.json b/package.json index 6ab76c38d..e437e76d2 100644 --- a/package.json +++ b/package.json @@ -86,7 +86,7 @@ "ml-stat": "^1.3.3", "multiplet-analysis": "^2.1.2", "nmr-correlation": "^2.3.3", - "nmr-load-save": "^0.22.4", + "nmr-load-save": "^0.23.2", "nmr-processing": "^11.5.0", "nmredata": "^0.9.7", "numeral": "^2.0.6", @@ -148,4 +148,4 @@ "vite": "^4.5.0", "vitest": "^0.34.6" } -} +} \ No newline at end of file diff --git a/src/component/loader/DropZone.tsx b/src/component/loader/DropZone.tsx index ee9f70eff..72bf126bd 100644 --- a/src/component/loader/DropZone.tsx +++ b/src/component/loader/DropZone.tsx @@ -14,6 +14,7 @@ import { LoaderProvider } from '../context/LoaderContext'; import { useLogger } from '../context/LoggerContext'; import { usePreferences } from '../context/PreferencesContext'; import { useAlert } from '../elements/popup/Alert'; +import useCheckExperimentalFeature from '../hooks/useCheckExperimentalFeature'; import { useCheckToolsVisibility } from '../hooks/useCheckToolsVisibility'; import { useMetaInformationImportationModal } from '../modal/metaImportation/index'; @@ -59,6 +60,7 @@ function DropZone(props) { const openImportMetaInformationModal = useMetaInformationImportationModal(); const alert = useAlert(); const { logger } = useLogger(); + const experimentalFeatures = useCheckExperimentalFeature(); async function loadFilesHandler(files) { try { @@ -73,13 +75,14 @@ function DropZone(props) { if (metaFile) { parseMetaFileResult = await parseMetaFile(metaFile); } - const { nmrLoaders: sourceSelector } = preferences.current; const { nmriumState, containsNmrium } = await readDropFiles( fileCollection, { sourceSelector, logger: logger.child({ context: 'nmr-processing' }), + onLoadProcessing: current.onLoadProcessing, + experimentalFeatures, }, ); @@ -97,7 +100,6 @@ function DropZone(props) { payload: { nmriumState, containsNmrium, - onLoadProcessing: current.onLoadProcessing, parseMetaFileResult, }, }); diff --git a/src/component/modal/setting/settings-tabs/OnLoadProcessingTabContent.tsx b/src/component/modal/setting/settings-tabs/OnLoadProcessingTabContent.tsx index cd0675c70..fd0d74180 100644 --- a/src/component/modal/setting/settings-tabs/OnLoadProcessingTabContent.tsx +++ b/src/component/modal/setting/settings-tabs/OnLoadProcessingTabContent.tsx @@ -1,27 +1,46 @@ import { useFormikContext } from 'formik'; -import { OnLoadProcessing, WorkspacePreferences } from 'nmr-load-save'; +import { WorkspacePreferences } from 'nmr-load-save'; +import { BaseFilter } from 'nmr-processing'; import { useState } from 'react'; import { TabItem, Tabs } from 'react-science/ui'; import { Nucleus } from '../../../../data/types/common/Nucleus'; import { CheckBoxCell } from '../../../elements/CheckBoxCell'; import IsotopesViewer from '../../../elements/IsotopesViewer'; +import Label from '../../../elements/Label'; import ReactTable from '../../../elements/ReactTable/ReactTable'; import { CustomColumn } from '../../../elements/ReactTable/utility/addCustomColumn'; +import FormikCheckBox from '../../../elements/formik/FormikCheckBox'; function OnLoadProcessingTabContent() { const { values } = useFormikContext(); - const [activeTab, setActiveTab] = useState('1H'); + const isExperimentalFeatures = + values.display?.general?.experimentalFeatures?.display || false; - const tabItems: TabItem[] = Object.keys(values?.onLoadProcessing || {}).map( + return ( +
+ + {isExperimentalFeatures && } +
+ ); +} + +function AutoProcessingFilters() { + const { values } = useFormikContext(); + const [activeTab, setActiveTab] = useState('1H'); + const autoProcessingFilters = values?.onLoadProcessing?.filters || {}; + const tabItems: TabItem[] = Object.keys(autoProcessingFilters).map( (nucleus) => ({ id: nucleus, title: , content: ( - + ), }), ); @@ -37,7 +56,7 @@ function OnLoadProcessingTabContent() { } function FiltersTable({ data, nucleus }) { - const COLUMNS: Array> = [ + const COLUMNS: Array> = [ { index: 1, Header: '#', @@ -54,7 +73,7 @@ function FiltersTable({ data, nucleus }) { Header: 'Enable', Cell: ({ row }) => ( ), diff --git a/src/component/reducer/actions/LoadActions.ts b/src/component/reducer/actions/LoadActions.ts index 7b1ab57d8..7eaa6c9ae 100644 --- a/src/component/reducer/actions/LoadActions.ts +++ b/src/component/reducer/actions/LoadActions.ts @@ -2,12 +2,7 @@ import { Draft } from 'immer'; import lodashMerge from 'lodash/merge'; import lodashMergeWith from 'lodash/mergeWith'; import { buildCorrelationData, CorrelationData } from 'nmr-correlation'; -import { - Spectrum, - OnLoadProcessing, - ViewState, - NmriumState, -} from 'nmr-load-save'; +import { Spectrum, ViewState, NmriumState } from 'nmr-load-save'; import { ParseResult } from 'papaparse'; import { initiateDatum1D } from '../../../data/data1d/Spectrum1D'; @@ -17,7 +12,6 @@ import * as MoleculeManager from '../../../data/molecules/MoleculeManager'; import { linkMetaWithSpectra } from '../../../data/parseMeta/linkMetaWithSpectra'; import { UsedColors } from '../../../types/UsedColors'; import { DefaultTolerance } from '../../panels/SummaryPanel/CorrelationTable/Constants'; -import nucleusToString from '../../utility/nucleusToString'; import { getDefaultViewState, getInitialState, State } from '../Reducer'; import { ActionType } from '../types/ActionType'; @@ -32,7 +26,6 @@ interface InitiateProps { interface InputProps extends InitiateProps { containsNmrium?: boolean; usedColors?: UsedColors; - onLoadProcessing?: OnLoadProcessing; parseMetaFileResult?: ParseResult | null; resetSourceObject?: boolean; } @@ -87,23 +80,15 @@ function setCorrelation(draft: Draft, correlations: CorrelationData) { } } -function setData( - draft: Draft, - input: InputProps, - options: { - autoOnLoadProcessing?: boolean; - } = {}, -) { +function setData(draft: Draft, input: InputProps) { const { nmriumState: { data, view }, - onLoadProcessing = {}, parseMetaFileResult = null, } = input || { nmriumState: { data: { spectra: [], molecules: [], correlations: {} } }, multipleAnalysis: {}, }; - const { autoOnLoadProcessing = true } = options; const { source, spectra = [], @@ -133,7 +118,6 @@ function setData( draft.data = draft.data.concat( initSpectra(spectra, { usedColors: draft.usedColors, - onLoadProcessing: autoOnLoadProcessing ? onLoadProcessing : {}, molecules: draft.molecules, }), ); @@ -153,19 +137,15 @@ function initSpectra( inputSpectra: Spectrum[], options: { usedColors: UsedColors; - onLoadProcessing: OnLoadProcessing; molecules: StateMoleculeExtended[]; }, ) { const spectra: any = []; - const { usedColors, onLoadProcessing, molecules } = options; + const { usedColors, molecules } = options; for (const spectrum of inputSpectra) { const { info } = spectrum; if (info.dimension === 1) { - const filters = onLoadProcessing?.[nucleusToString(info.nucleus)] || []; - spectra.push( - initiateDatum1D(spectrum, { usedColors, filters, molecules }), - ); + spectra.push(initiateDatum1D(spectrum, { usedColors, molecules })); } else if (info.dimension === 2) { spectra.push(initiateDatum2D({ ...spectrum }, { usedColors })); } @@ -195,10 +175,9 @@ function initData( action: LoadDropFilesAction | InitiateAction, options: { forceInitialize?: boolean; - autoOnLoadProcessing?: boolean; } = {}, ) { - const { forceInitialize = false, autoOnLoadProcessing = true } = options; + const { forceInitialize = false } = options; const { nmriumState: { data, view }, @@ -207,7 +186,7 @@ function initData( const viewState = view as ViewState; if (data?.spectra?.length || forceInitialize) { const state = getInitialState(); - setData(state, action.payload, { autoOnLoadProcessing }); + setData(state, action.payload); setActiveTab(state, { tab: viewState?.spectra?.activeTab || '' }); state.width = draft.width; state.height = draft.height; @@ -234,7 +213,6 @@ function handleSetIsLoading(draft: Draft, action: SetIsLoadingAction) { function handleInitiate(draft: Draft, action: InitiateAction) { return initData(draft, action, { forceInitialize: true, - autoOnLoadProcessing: false, }); } @@ -249,7 +227,7 @@ function handleLoadDropFiles(draft: Draft, action: LoadDropFilesAction) { } = payload; if (containsNmrium) { - return initData(draft, action, { autoOnLoadProcessing: false }); + return initData(draft, action); } else { setData(draft, payload); setActiveTab(draft); diff --git a/src/component/workspaces/exercise.ts b/src/component/workspaces/exercise.ts index bea7bff18..54efb746b 100644 --- a/src/component/workspaces/exercise.ts +++ b/src/component/workspaces/exercise.ts @@ -34,6 +34,7 @@ export const exercise: InnerWorkspace = { verticalSplitterCloseThreshold: 600, spectraRendering: 'auto', loggingLevel: 'info', + invert: false, }, formatting: { nuclei: {}, diff --git a/src/component/workspaces/prediction.ts b/src/component/workspaces/prediction.ts index 8a0d36f7d..b5e8ac723 100644 --- a/src/component/workspaces/prediction.ts +++ b/src/component/workspaces/prediction.ts @@ -9,6 +9,7 @@ export const prediction: InnerWorkspace = { verticalSplitterCloseThreshold: 600, spectraRendering: 'auto', loggingLevel: 'info', + invert: false, }, display: { panels: { diff --git a/src/component/workspaces/simulation.ts b/src/component/workspaces/simulation.ts index 60d2151b7..628da33c8 100644 --- a/src/component/workspaces/simulation.ts +++ b/src/component/workspaces/simulation.ts @@ -9,6 +9,7 @@ export const simulation: InnerWorkspace = { verticalSplitterCloseThreshold: 600, spectraRendering: 'auto', loggingLevel: 'info', + invert: false, }, display: { panels: { diff --git a/src/component/workspaces/workspaceDefaultProperties.ts b/src/component/workspaces/workspaceDefaultProperties.ts index e0694c355..168fae145 100644 --- a/src/component/workspaces/workspaceDefaultProperties.ts +++ b/src/component/workspaces/workspaceDefaultProperties.ts @@ -61,6 +61,7 @@ export const workspaceDefaultProperties: Required = { verticalSplitterCloseThreshold: 600, spectraRendering: 'auto', loggingLevel: 'info', + invert: false, }, formatting: { nuclei: { @@ -121,76 +122,79 @@ export const workspaceDefaultProperties: Required = { ], }, onLoadProcessing: { - '1H': [ - { - name: Filters.digitalFilter.id, - label: Filters.digitalFilter.name, - value: {}, - flag: true, - }, - { - name: Filters.apodization.id, - label: Filters.apodization.name, - value: {}, - flag: false, - }, - { - name: Filters.zeroFilling.id, - label: Filters.zeroFilling.name, + autoProcessing: true, + filters: { + '1H': [ + { + name: Filters.digitalFilter.id, + label: Filters.digitalFilter.name, + value: {}, + flag: true, + }, + { + name: Filters.apodization.id, + label: Filters.apodization.name, + value: {}, + flag: false, + }, + { + name: Filters.zeroFilling.id, + label: Filters.zeroFilling.name, - value: {}, - flag: true, - }, - { - name: Filters.fft.id, - label: Filters.fft.name, + value: {}, + flag: true, + }, + { + name: Filters.fft.id, + label: Filters.fft.name, - value: {}, - flag: true, - }, - { - name: Filters.phaseCorrection.id, - label: Filters.phaseCorrection.name, + value: {}, + flag: true, + }, + { + name: Filters.phaseCorrection.id, + label: Filters.phaseCorrection.name, - value: {}, - flag: true, - }, - ], - '13C': [ - { - name: Filters.digitalFilter.id, - label: Filters.digitalFilter.name, + value: {}, + flag: true, + }, + ], + '13C': [ + { + name: Filters.digitalFilter.id, + label: Filters.digitalFilter.name, - value: {}, - flag: true, - }, - { - name: Filters.apodization.id, - label: Filters.apodization.name, - value: {}, - flag: true, - }, - { - name: Filters.zeroFilling.id, - label: Filters.zeroFilling.name, + value: {}, + flag: true, + }, + { + name: Filters.apodization.id, + label: Filters.apodization.name, + value: {}, + flag: true, + }, + { + name: Filters.zeroFilling.id, + label: Filters.zeroFilling.name, - value: {}, - flag: true, - }, - { - name: Filters.fft.id, - label: Filters.fft.name, + value: {}, + flag: true, + }, + { + name: Filters.fft.id, + label: Filters.fft.name, - value: {}, - flag: true, - }, - { - name: Filters.phaseCorrection.id, - label: Filters.phaseCorrection.name, + value: {}, + flag: true, + }, + { + name: Filters.phaseCorrection.id, + label: Filters.phaseCorrection.name, - value: {}, - flag: true, - }, - ], + value: {}, + flag: true, + }, + ], + }, }, }; diff --git a/src/data/data1d/Spectrum1D/initiateDatum1D.ts b/src/data/data1d/Spectrum1D/initiateDatum1D.ts index d2a838bb4..ce7fb735d 100644 --- a/src/data/data1d/Spectrum1D/initiateDatum1D.ts +++ b/src/data/data1d/Spectrum1D/initiateDatum1D.ts @@ -1,6 +1,6 @@ import { v4 } from '@lukeed/uuid'; import { Spectrum1D } from 'nmr-load-save'; -import { BaseFilter, FiltersManager, Filters } from 'nmr-processing'; +import { FiltersManager } from 'nmr-processing'; import { UsedColors } from '../../../types/UsedColors'; import { initiateFilters } from '../../initiateFilters'; @@ -15,7 +15,6 @@ import { initiateRanges } from './ranges/initiateRanges'; export interface InitiateDatum1DOptions { usedColors?: UsedColors; - filters?: any[]; molecules?: StateMoleculeExtended[]; } @@ -23,7 +22,7 @@ export function initiateDatum1D( spectrum: any, options: InitiateDatum1DOptions = {}, ): Spectrum1D { - const { usedColors = {}, filters = [], molecules = [] } = options; + const { usedColors = {}, molecules = [] } = options; const { integrals, ranges, ...restSpectrum } = spectrum; const spectrumObj: Spectrum1D = { ...restSpectrum }; @@ -82,40 +81,5 @@ export function initiateDatum1D( //reapply filters after load the original data FiltersManager.reapplyFilters(spectrumObj); - preprocessing(spectrumObj, filters); - return spectrumObj; } - -function preprocessing(datum, onLoadFilters: BaseFilter[] = []) { - if (datum.info.isFid) { - if (onLoadFilters?.length === 0) { - FiltersManager.applyFilter(datum, [ - { - name: Filters.digitalFilter.id, - value: {}, - isDeleteAllow: false, - }, - ]); - } else { - const filters: BaseFilter[] = []; - - for (let filter of onLoadFilters) { - if ( - (!datum.info?.digitalFilter && - filter.name === Filters.digitalFilter.id) || - !filter.flag - ) { - continue; - } - if (filter.name === Filters.digitalFilter.id) { - filter = { ...filter, isDeleteAllow: false }; - } - - filters.push(filter); - } - - FiltersManager.applyFilter(datum, filters); - } - } -}