Skip to content

Commit

Permalink
refactor: move auto processing to nmr-load-save (#2737)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
hamed-musallam and jobo322 authored Nov 7, 2023
1 parent 6deb455 commit 2aeb9e1
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 150 deletions.
12 changes: 6 additions & 6 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
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -148,4 +148,4 @@
"vite": "^4.5.0",
"vitest": "^0.34.6"
}
}
}
6 changes: 4 additions & 2 deletions src/component/loader/DropZone.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -59,6 +60,7 @@ function DropZone(props) {
const openImportMetaInformationModal = useMetaInformationImportationModal();
const alert = useAlert();
const { logger } = useLogger();
const experimentalFeatures = useCheckExperimentalFeature();

async function loadFilesHandler(files) {
try {
Expand All @@ -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,
},
);

Expand All @@ -97,7 +100,6 @@ function DropZone(props) {
payload: {
nmriumState,
containsNmrium,
onLoadProcessing: current.onLoadProcessing,
parseMetaFileResult,
},
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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<WorkspacePreferences>();
const [activeTab, setActiveTab] = useState<Nucleus>('1H');
const isExperimentalFeatures =
values.display?.general?.experimentalFeatures?.display || false;

const tabItems: TabItem[] = Object.keys(values?.onLoadProcessing || {}).map(
return (
<div>
<Label
title="Enable auto processing"
htmlFor="onLoadProcessing.autoProcessing"
style={{ wrapper: { padding: '10px 0' } }}
>
<FormikCheckBox name="onLoadProcessing.autoProcessing" />
</Label>
{isExperimentalFeatures && <AutoProcessingFilters />}
</div>
);
}

function AutoProcessingFilters() {
const { values } = useFormikContext<WorkspacePreferences>();
const [activeTab, setActiveTab] = useState<Nucleus>('1H');
const autoProcessingFilters = values?.onLoadProcessing?.filters || {};
const tabItems: TabItem[] = Object.keys(autoProcessingFilters).map(
(nucleus) => ({
id: nucleus,
title: <IsotopesViewer value={nucleus} />,
content: (
<FiltersTable
data={values?.onLoadProcessing?.[nucleus]}
nucleus={nucleus}
/>
<FiltersTable data={autoProcessingFilters[nucleus]} nucleus={nucleus} />
),
}),
);
Expand All @@ -37,7 +56,7 @@ function OnLoadProcessingTabContent() {
}

function FiltersTable({ data, nucleus }) {
const COLUMNS: Array<CustomColumn<OnLoadProcessing>> = [
const COLUMNS: Array<CustomColumn<BaseFilter>> = [
{
index: 1,
Header: '#',
Expand All @@ -54,7 +73,7 @@ function FiltersTable({ data, nucleus }) {
Header: 'Enable',
Cell: ({ row }) => (
<CheckBoxCell
name={`onLoadProcessing.${nucleus}.${row.index}.flag`}
name={`onLoadProcessing.filters.${nucleus}.${row.index}.flag`}
defaultValue={false}
/>
),
Expand Down
36 changes: 7 additions & 29 deletions src/component/reducer/actions/LoadActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';

Expand All @@ -32,7 +26,6 @@ interface InitiateProps {
interface InputProps extends InitiateProps {
containsNmrium?: boolean;
usedColors?: UsedColors;
onLoadProcessing?: OnLoadProcessing;
parseMetaFileResult?: ParseResult<any> | null;
resetSourceObject?: boolean;
}
Expand Down Expand Up @@ -87,23 +80,15 @@ function setCorrelation(draft: Draft<State>, correlations: CorrelationData) {
}
}

function setData(
draft: Draft<State>,
input: InputProps,
options: {
autoOnLoadProcessing?: boolean;
} = {},
) {
function setData(draft: Draft<State>, input: InputProps) {
const {
nmriumState: { data, view },
onLoadProcessing = {},
parseMetaFileResult = null,
} = input || {
nmriumState: { data: { spectra: [], molecules: [], correlations: {} } },
multipleAnalysis: {},
};

const { autoOnLoadProcessing = true } = options;
const {
source,
spectra = [],
Expand Down Expand Up @@ -133,7 +118,6 @@ function setData(
draft.data = draft.data.concat(
initSpectra(spectra, {
usedColors: draft.usedColors,
onLoadProcessing: autoOnLoadProcessing ? onLoadProcessing : {},
molecules: draft.molecules,
}),
);
Expand All @@ -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 }));
}
Expand Down Expand Up @@ -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 },
Expand All @@ -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;
Expand All @@ -234,7 +213,6 @@ function handleSetIsLoading(draft: Draft<State>, action: SetIsLoadingAction) {
function handleInitiate(draft: Draft<State>, action: InitiateAction) {
return initData(draft, action, {
forceInitialize: true,
autoOnLoadProcessing: false,
});
}

Expand All @@ -249,7 +227,7 @@ function handleLoadDropFiles(draft: Draft<State>, action: LoadDropFilesAction) {
} = payload;

if (containsNmrium) {
return initData(draft, action, { autoOnLoadProcessing: false });
return initData(draft, action);
} else {
setData(draft, payload);
setActiveTab(draft);
Expand Down
1 change: 1 addition & 0 deletions src/component/workspaces/exercise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const exercise: InnerWorkspace = {
verticalSplitterCloseThreshold: 600,
spectraRendering: 'auto',
loggingLevel: 'info',
invert: false,
},
formatting: {
nuclei: {},
Expand Down
1 change: 1 addition & 0 deletions src/component/workspaces/prediction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const prediction: InnerWorkspace = {
verticalSplitterCloseThreshold: 600,
spectraRendering: 'auto',
loggingLevel: 'info',
invert: false,
},
display: {
panels: {
Expand Down
1 change: 1 addition & 0 deletions src/component/workspaces/simulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const simulation: InnerWorkspace = {
verticalSplitterCloseThreshold: 600,
spectraRendering: 'auto',
loggingLevel: 'info',
invert: false,
},
display: {
panels: {
Expand Down
Loading

0 comments on commit 2aeb9e1

Please sign in to comment.