Skip to content

Commit

Permalink
feat: manage auto processing by nmr-load-save
Browse files Browse the repository at this point in the history
  • Loading branch information
hamed-musallam committed Nov 6, 2023
1 parent 39669b4 commit 2711282
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 73 deletions.
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
Expand Up @@ -7,22 +7,40 @@ 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 Down Expand Up @@ -55,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
38 changes: 7 additions & 31 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 = { autoProcessing: true },
parseMetaFileResult = null,
} = input || {
nmriumState: { data: { spectra: [], molecules: [], correlations: {} } },
multipleAnalysis: {},
};

const { autoOnLoadProcessing = true } = options;
const {
source,
spectra = [],
Expand Down Expand Up @@ -133,9 +118,6 @@ function setData(
draft.data = draft.data.concat(
initSpectra(spectra, {
usedColors: draft.usedColors,
onLoadProcessing: autoOnLoadProcessing
? onLoadProcessing
: { autoProcessing: true },
molecules: draft.molecules,
}),
);
Expand All @@ -155,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 @@ -197,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 @@ -209,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 @@ -236,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 @@ -251,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
44 changes: 11 additions & 33 deletions src/data/data1d/Spectrum1D/initiateDatum1D.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { v4 } from '@lukeed/uuid';
import { Spectrum1D } from 'nmr-load-save';
import { BaseFilter, FiltersManager, Filters } from 'nmr-processing';
import { FiltersManager, Filters } from 'nmr-processing';

import { UsedColors } from '../../../types/UsedColors';
import { initiateFilters } from '../../initiateFilters';
Expand All @@ -15,15 +15,14 @@ import { initiateRanges } from './ranges/initiateRanges';

export interface InitiateDatum1DOptions {
usedColors?: UsedColors;
filters?: any[];
molecules?: StateMoleculeExtended[];
}

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 };
Expand Down Expand Up @@ -82,40 +81,19 @@ export function initiateDatum1D(
//reapply filters after load the original data
FiltersManager.reapplyFilters(spectrumObj);

preprocessing(spectrumObj, filters);
preprocessing(spectrumObj);

return spectrumObj;
}

function preprocessing(datum, onLoadFilters: BaseFilter[] = []) {
function preprocessing(datum) {
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);
}
FiltersManager.applyFilter(datum, [
{
name: Filters.digitalFilter.id,
value: {},
isDeleteAllow: false,
},
]);

Check warning on line 97 in src/data/data1d/Spectrum1D/initiateDatum1D.ts

View check run for this annotation

Codecov / codecov/patch

src/data/data1d/Spectrum1D/initiateDatum1D.ts#L91-L97

Added lines #L91 - L97 were not covered by tests
}
}

0 comments on commit 2711282

Please sign in to comment.