From 1764046c8a12ddfc3ca3b78adb2f0449c0381175 Mon Sep 17 00:00:00 2001 From: hamed musallam Date: Tue, 5 Sep 2023 12:31:01 +0200 Subject: [PATCH] feat: auto processing 1d proton and carbon --- package-lock.json | 1 + package.json | 3 +- src/NMRiumWrapper.tsx | 2 +- src/hooks/useLoadSpectra.ts | 7 ++ src/modal/AboutUsModal.tsx | 1 + src/utilities/appendFilters.ts | 121 +++++++++++++++++++++++++++++++++ 6 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 src/utilities/appendFilters.ts diff --git a/package-lock.json b/package-lock.json index 8fda184..fb7c5d1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "@emotion/react": "^11.11.1", "filelist-utils": "^1.10.2", "nmr-load-save": "^0.18.0", + "nmr-processing": "^10.0.7", "nmrium": "^0.43.1-pre.1692874523", "react-science": "^0.26.2", "vite-plugin-pwa": "^0.16.4" diff --git a/package.json b/package.json index 5611163..a299531 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "@emotion/react": "^11.11.1", "filelist-utils": "^1.10.2", "nmr-load-save": "^0.18.0", + "nmr-processing": "^10.0.7", "nmrium": "^0.43.1-pre.1692874523", "react-science": "^0.26.2", "vite-plugin-pwa": "^0.16.4" @@ -60,4 +61,4 @@ "typescript": "^5.2.2", "vite": "^4.4.9" } -} \ No newline at end of file +} diff --git a/src/NMRiumWrapper.tsx b/src/NMRiumWrapper.tsx index 1a3706c..f7f9a69 100644 --- a/src/NMRiumWrapper.tsx +++ b/src/NMRiumWrapper.tsx @@ -1,11 +1,11 @@ import { NMRium, NMRiumData, NMRiumRef, NMRiumChangeCb } from 'nmrium'; import { useEffect, useState, useCallback, CSSProperties, useRef } from 'react'; +import { RootLayout } from 'react-science/ui'; import events from './events'; import { useLoadSpectra } from './hooks/useLoadSpectra'; import { usePreferences } from './hooks/usePreferences'; import { useWhiteList } from './hooks/useWhiteList'; -import { RootLayout } from 'react-science/ui'; import AboutUsModal from './modal/AboutUsModal'; const styles: Record<'container' | 'loadingContainer', CSSProperties> = { diff --git a/src/hooks/useLoadSpectra.ts b/src/hooks/useLoadSpectra.ts index 8ff37ec..def625a 100644 --- a/src/hooks/useLoadSpectra.ts +++ b/src/hooks/useLoadSpectra.ts @@ -8,6 +8,7 @@ import { import { useCallback, useMemo, useState } from 'react'; import events from '../events'; +import { appendFilters } from '../utilities/appendFilters'; import { getFileNameFromURL } from '../utilities/getFileNameFromURL'; import { isArrayOfString } from '../utilities/isArrayOfString'; @@ -49,12 +50,18 @@ export function useLoadSpectra() { if ('urls' in options) { if (isArrayOfString(options.urls)) { const result = await loadSpectraFromURLs(options.urls); + if (result?.spectra) { + appendFilters(result?.spectra); + } setData(result as NMRiumData); } else { throw new Error('The input must be a valid urls array of string[]'); } } else if ('files' in options) { const result = await loadSpectraFromFiles(options.files); + if (result?.spectra) { + appendFilters(result?.spectra); + } setData(result as NMRiumData); } } catch (error: unknown) { diff --git a/src/modal/AboutUsModal.tsx b/src/modal/AboutUsModal.tsx index 9515f1a..e902940 100644 --- a/src/modal/AboutUsModal.tsx +++ b/src/modal/AboutUsModal.tsx @@ -1,6 +1,7 @@ /** @jsxImportSource @emotion/react */ import { css } from '@emotion/react'; import { Modal, useOnOff } from 'react-science/ui'; + import versionInfo from '../versionInfo'; const styles = css` diff --git a/src/utilities/appendFilters.ts b/src/utilities/appendFilters.ts new file mode 100644 index 0000000..66b2dfc --- /dev/null +++ b/src/utilities/appendFilters.ts @@ -0,0 +1,121 @@ +import { Spectrum } from 'nmr-load-save'; +import { BaseFilter, Filter, Filters, Nucleus } from 'nmr-processing'; + +const ConfigOnLoadProcessing: Partial> = { + '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.phaseCorrection.id, + label: Filters.phaseCorrection.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.fft.id, + label: Filters.fft.name, + + value: {}, + flag: true, + }, + { + name: Filters.phaseCorrection.id, + label: Filters.phaseCorrection.name, + + value: {}, + flag: true, + }, + ], +}; + +function nucleusToString(nucleus: string | string[]) { + return typeof nucleus === 'string' ? nucleus : nucleus.join(','); +} + +interface filterFiltersOptions { + digitalFilter?: number; +} + +function mapFilters( + onLoadfilters: BaseFilter[], + options: filterFiltersOptions, +) { + const { digitalFilter } = options; + const filters: BaseFilter[] = []; + + for (let filter of onLoadfilters) { + if ( + (!digitalFilter && filter.name === Filters.digitalFilter.id) || + !filter.flag + ) { + continue; + } + const isDeleteAllow = !(filter.name === Filters.digitalFilter.id); + filter = { ...filter, isDeleteAllow }; + filters.push(filter); + } + return filters; +} + +export function appendFilters(spectra: Spectrum[]) { + for (const spectrum of spectra) { + const { + info: { nucleus, isFid, digitalFilter }, + filters, + } = spectrum; + const hasFilters = filters.length > 0; + if (isFid && !hasFilters) { + const n = nucleusToString(nucleus); + const onLoadfilters: BaseFilter[] = ConfigOnLoadProcessing?.[n] || []; + if (onLoadfilters.length > 0) { + const autofilters = mapFilters(onLoadfilters, { digitalFilter }); + spectrum.filters = autofilters as Filter[]; + } + } + } +}