Skip to content

Commit

Permalink
chore: fix prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
hamed-musallam committed Feb 27, 2024
1 parent 915b43c commit 36ed9f2
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 63 deletions.
55 changes: 36 additions & 19 deletions src/component/header/PhaseCorrectionTwoDimensionsPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { Spectrum1D } from 'nmr-load-save';
import {
CSSProperties,
useCallback,
useDeferredValue,
useEffect,
useRef,
useState,
} from 'react';
import { Spectrum2D } from 'nmr-load-save';
import { Filters } from 'nmr-processing';
import { CSSProperties, useCallback, useEffect, useRef, useState } from 'react';

import { stringCapitalize } from '../../utils/stringCapitalize';
import { useActivePhaseTraces } from '../2d/1d-tracer/phase-correction-traces/useActivePhaseTraces';
Expand All @@ -16,11 +10,13 @@ import Input, { InputStyle } from '../elements/Input';
import InputRange from '../elements/InputRange';
import Label from '../elements/Label';
import Select from '../elements/Select';
import { useFilter } from '../hooks/useFilter';
import useSpectrum from '../hooks/useSpectrum';
import { TraceDirection } from '../reducer/Reducer';
import { PhaseCorrectionTraceData, TraceDirection } from '../reducer/Reducer';

import { headerLabelStyle } from './Header';
import { HeaderContainer } from './HeaderContainer';

Check warning on line 18 in src/component/header/PhaseCorrectionTwoDimensionsPanel.tsx

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

There should be at least one empty line between import groups
import { NmrData2DFt } from 'cheminfo-types';

Check warning on line 19 in src/component/header/PhaseCorrectionTwoDimensionsPanel.tsx

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

`cheminfo-types` import should occur before import of `nmr-load-save`

const selectStyle: CSSProperties = {
marginLeft: '5px',
Expand Down Expand Up @@ -49,10 +45,10 @@ const TRACE_DIRECTIONS: Array<{ label: string; value: TraceDirection }> = (
const emptyData = { datum: {}, filter: null };

export default function PhaseCorrectionTwoDimensionsPanel() {
const { ph0, ph1, pivot, activeTraceDirection } = useActivePhaseTraces();
const { activeTraceDirection, pivot } = useActivePhaseTraces();

const { data } = useSpectrum(emptyData) as Spectrum1D;
const activeDirection = useDeferredValue(activeTraceDirection);
const { data } = useSpectrum(emptyData) as Spectrum2D;
const filter = useFilter(Filters.phaseCorrectionTwoDimensions.id);

const dispatch = useDispatch();
const [value, setValue] = useState({ ph0: 0, ph1: 0 });
Expand All @@ -62,26 +58,47 @@ export default function PhaseCorrectionTwoDimensionsPanel() {
const ph1Ref = useRef<any>();

useEffect(() => {
if (activeDirection !== activeTraceDirection) {
if (filter) {
const { value } = filter;
const { ph0, ph1 } = value[
activeTraceDirection
] as PhaseCorrectionTraceData;
setValue({ ph0, ph1 });
valueRef.current = { ph0, ph1 };
}
}, [activeDirection, activeTraceDirection, ph0, ph1]);
if (ph0Ref.current && ph1Ref.current) {
if (filter) {
const { value } = filter;
const { ph0, ph1 } = value[
activeTraceDirection
] as PhaseCorrectionTraceData;
ph0Ref.current.setValue(ph0);
ph1Ref.current.setValue(ph1);
} else {
ph0Ref.current.setValue(valueRef.current.ph0);
ph1Ref.current.setValue(valueRef.current.ph1);
}
}
}, [activeTraceDirection, filter]);

const calcPhaseCorrectionHandler = useCallback(
(newValues, filedName) => {
if (filedName === 'ph1' && data.re && pivot) {
if (filedName === 'ph1' && data && pivot) {
const datum = (data as NmrData2DFt).rr;
const nbPoints =
activeTraceDirection === 'horizontal'
? datum.z[0].length
: datum.z.length;
const diff0 = newValues.ph0 - valueRef.current.ph0;
const diff1 = newValues.ph1 - valueRef.current.ph1;
newValues.ph0 +=
diff0 - (diff1 * (data.re.length - pivot?.index)) / data.re.length;
newValues.ph0 += diff0 - (diff1 * (nbPoints - pivot?.index)) / nbPoints;
}
dispatch({
type: 'CALCULATE_TOW_DIMENSIONS_MANUAL_PHASE_CORRECTION_FILTER',
payload: newValues,
});
},
[data.re, dispatch, pivot],
[activeTraceDirection, data, dispatch, pivot],
);

const updateInputRangeInitialValue = useCallback((value) => {
Expand Down
36 changes: 20 additions & 16 deletions src/component/reducer/Reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,25 @@ export interface TwoDimensionPhaseCorrection {
activeTraceDirection: TraceDirection;
}

export const getDefaultTwoDimensionsPhaseCorrectionTraceOptions =
(): TwoDimensionPhaseCorrection['traces'] => {
const directions: [TraceDirection, TraceDirection] = [
'horizontal',
'vertical',
];
const result = {};
for (const direction of directions) {
result[direction] = {
ph0: 0,
ph1: 0,
pivot: null,
spectra: [],
scaleRatio: 1,
};
}
return result as TwoDimensionPhaseCorrection['traces'];
};

export function getDefaultViewState(): ViewState {
return {
molecules: {},
Expand Down Expand Up @@ -143,22 +162,7 @@ export const getInitialState = (): State => ({
apodizationOptions: {} as ApodizationOptions,
twoDimensionPhaseCorrection: {
activeTraceDirection: 'horizontal',
traces: {
horizontal: {
ph0: 0,
ph1: 0,
pivot: null,
spectra: [],
scaleRatio: 1,
},
vertical: {
ph0: 0,
ph1: 0,
pivot: null,
spectra: [],
scaleRatio: 1,
},
},
traces: getDefaultTwoDimensionsPhaseCorrectionTraceOptions(),
},
pivot: { value: 0, index: 0 },
zonesNoiseFactor: 1,
Expand Down
54 changes: 30 additions & 24 deletions src/component/reducer/actions/FiltersActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { Tool, options as Tools } from '../../toolbar/ToolTypes';
import { getSpectraByNucleus } from '../../utility/getSpectraByNucleus';
import nucleusToString from '../../utility/nucleusToString';
import {
getDefaultTwoDimensionsPhaseCorrectionTraceOptions,
getInitialState,
PhaseCorrectionTraceData,
State,
Expand Down Expand Up @@ -170,13 +171,13 @@ export type FiltersActions =
| SetOneDimensionPhaseCorrectionPivotPoint
| SetTwoDimensionPhaseCorrectionPivotPoint
| ActionType<
| 'APPLY_FFT_FILTER'
| 'APPLY_FFT_DIMENSION_1_FILTER'
| 'APPLY_FFT_DIMENSION_2_FILTER'
| 'APPLY_AUTO_PHASE_CORRECTION_FILTER'
| 'APPLY_ABSOLUTE_FILTER'
| 'APPLY_MANUAL_PHASE_CORRECTION_TOW_DIMENSION_FILTER'
>;
| 'APPLY_FFT_FILTER'
| 'APPLY_FFT_DIMENSION_1_FILTER'
| 'APPLY_FFT_DIMENSION_2_FILTER'
| 'APPLY_AUTO_PHASE_CORRECTION_FILTER'
| 'APPLY_ABSOLUTE_FILTER'
| 'APPLY_MANUAL_PHASE_CORRECTION_TOW_DIMENSION_FILTER'
>;

function getFilterUpdateDomainRules(
filterName: string,
Expand Down Expand Up @@ -340,14 +341,14 @@ function rollbackSpectrum(
const applyFilter = !filterKey
? true
: [
phaseCorrection.id,
phaseCorrectionTwoDimensions.id,
fft.id,
shiftX.id,
shift2DX.id,
shift2DY.id,
signalProcessing.id,
].includes(filterKey);
phaseCorrection.id,
phaseCorrectionTwoDimensions.id,
fft.id,
shiftX.id,
shift2DX.id,
shift2DY.id,
signalProcessing.id,
].includes(filterKey);

beforeRollback(draft, filterKey);

Expand All @@ -373,21 +374,19 @@ function beforeRollback(draft: Draft<State>, filterKey) {
(filter) => filter.name === Tools.phaseCorrectionTwoDimensions.id,
);

if (isSpectrum1D(spectrum) && phaseCorrectionFilter) {
const {
value: { pivot },
} = phaseCorrectionFilter;
if (isSpectrum1D(spectrum)) {
const { value: filterOptions } = phaseCorrectionFilter || {};
let pivotObj = {
value: 0,
index: 0,
};
if (typeof pivot === 'number') {
if (typeof filterOptions?.pivot === 'number') {
const { pivot } = filterOptions;
const index = xFindClosestIndex(spectrum.data.re, pivot);
pivotObj = { value: pivot, index };
} else {
// look for the strongest peak to set it as a pivot
const peak = findStrongestPeak(spectrum.data);

if (peak) {
const { xValue, index } = peak;
pivotObj = { value: xValue, index };
Expand All @@ -408,8 +407,13 @@ function beforeRollback(draft: Draft<State>, filterKey) {
(filter) => filter.name === Tools.phaseCorrectionTwoDimensions.id,
);

if (isSpectrum2D(spectrum) && phaseCorrectionFilter) {
const { value: filterOptions } = phaseCorrectionFilter;
if (isSpectrum2D(spectrum)) {
const { value } = phaseCorrectionFilter || {};
let filterOptions =
getDefaultTwoDimensionsPhaseCorrectionTraceOptions();
if (value) {
filterOptions = value;
}

for (const direction in filterOptions) {
const phaseOptions = draft.toolOptions.data
Expand All @@ -428,7 +432,9 @@ function beforeRollback(draft: Draft<State>, filterKey) {
);

if (typeof pivot === 'number') {
const index = xFindClosestIndex(datum.x, pivot);
const index = xFindClosestIndex(datum.x, pivot, {
sorted: false,
});
phaseOptions.pivot = { index, value: pivot };
} else {
const peak = findStrongestPeak(datum);
Expand Down
9 changes: 5 additions & 4 deletions src/component/reducer/helper/findStrongestPeak.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { NmrData1D } from 'cheminfo-types';
import { max } from 'd3';
import { xFindClosestIndex } from 'ml-spectra-processing';

import { maxAbsoluteValue } from '../../utility/maxAbsoluteValue';

export function findStrongestPeak(data: NmrData1D) {
if (!data) return null;

const strongestPeakValue = max(data.re) as number;
const index = xFindClosestIndex(data.re, strongestPeakValue);
const maxY = maxAbsoluteValue(data.re);
const index = xFindClosestIndex(data.re, maxY, { sorted: false });
return {
xValue: data.x[index],
yValue: strongestPeakValue,
yValue: maxY,
index,
};
}

0 comments on commit 36ed9f2

Please sign in to comment.