Skip to content

Commit

Permalink
feat: apodization window preview (#1647)
Browse files Browse the repository at this point in the history
* feat: apodization window preview

* refactor: change the apodization window  y scale

* refactor: ApdoizationLine component

* refactor: set default value for lineBroadeningCenter is 0

* style: make the input field width smaller

* chore: update dependencies

* feat: unchecking live preview should reset the preview

* feat: unchecking live preview should reset the preview
  • Loading branch information
hamed-musallam committed Jul 29, 2022
1 parent 7be801f commit 16b97b3
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 127 deletions.
96 changes: 14 additions & 82 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 77 additions & 0 deletions src/component/1d/ApodizationLine.tsx
@@ -0,0 +1,77 @@
import * as Filters from '../../data/Filters';
import {
apodizationFilter,
defaultApodizationOptions,
} from '../../data/data1d/filter1d/apodization';
import { Datum1D } from '../../data/types/data1d';
import { useChartData } from '../context/ChartContext';
import { useScaleChecked } from '../context/ScaleContext';
import useSpectrum from '../hooks/useSpectrum';
import useXYReduce, { XYReducerDomainAxis } from '../hooks/useXYReduce';
import { PathBuilder } from '../utility/PathBuilder';

import { getYScale } from './utilities/scale';

const emptyData = { data: {}, info: {} };

function useWindowYScale() {
const { height, margin, verticalAlign } = useChartData();
return getYScale({
height,
margin,
verticalAlign,
yDomain: [0, 1],
});
}

function ApdoizationLine() {
const {
activeSpectrum,
toolOptions: {
selectedTool,
data: { apodizationOptions },
},
} = useChartData();
const { scaleX } = useScaleChecked();
const spectrum = useSpectrum({ emptyData }) as Datum1D;
const xyReduce = useXYReduce(XYReducerDomainAxis.XAxis);
const scaleY = useWindowYScale();

if (!activeSpectrum?.id || selectedTool !== Filters.apodization.id) {
return null;
}

const paths = () => {
const pathBuilder = new PathBuilder();
const { windowData: y } = apodizationFilter(
spectrum,
apodizationOptions || defaultApodizationOptions,
);
const x = spectrum.data?.x;

if (x && y) {
const pathPoints = xyReduce({ x, y });

pathBuilder.moveTo(scaleX()(pathPoints.x[0]), scaleY(pathPoints.y[0]));
for (let i = 1; i < pathPoints.x.length; i++) {
pathBuilder.lineTo(scaleX()(pathPoints.x[i]), scaleY(pathPoints.y[i]));
}

return pathBuilder.toString();
} else {
return '';
}
};

return (
<path
data-test-id="apodization-line"
stroke="green"
fill="none"
strokeWidth="2"
d={paths()}
/>
);
}

export default ApdoizationLine;
3 changes: 2 additions & 1 deletion src/component/1d/Chart1D.tsx
@@ -1,5 +1,6 @@
import FloatMoleculeStructures from '../tool/FloatMoleculeStructures';

import ApdoizationLine from './ApodizationLine';
import ExclusionZonesAnnotations from './ExclusionZonesAnnotations';
import IntegralsSeries from './IntegralsSeries';
import LinesSeries from './LinesSeries';
Expand Down Expand Up @@ -29,8 +30,8 @@ function Chart1D({ mode, width, height, margin, displayerKey }) {
/>
</clipPath>
</defs>

<LinesSeries />
<ApdoizationLine />
<IntegralsSeries />
<PeakAnnotations />
<Ranges />
Expand Down
46 changes: 20 additions & 26 deletions src/component/header/ApodizationOptionsPanel.tsx
Expand Up @@ -3,6 +3,7 @@ import * as Yup from 'yup';

import * as Filters from '../../data/Filters';
import { Filter } from '../../data/FiltersManager';
import { defaultApodizationOptions } from '../../data/data1d/filter1d/apodization';
import { useDispatch } from '../context/DispatchContext';
import ActionButtons from '../elements/ActionButtons';
import Label from '../elements/Label';
Expand All @@ -15,28 +16,14 @@ import {
APPLY_APODIZATION_FILTER,
CALCULATE_APODIZATION_FILTER,
RESET_SELECTED_TOOL,
SET_SELECTED_TOOL,
} from '../reducer/types/Types';
import { options } from '../toolbar/ToolTypes';

const styles: Record<'container' | 'input' | 'label', CSSProperties> = {
container: {
padding: '5px',
height: '100%',
display: 'flex',
},

input: {
height: '100%',
width: '80px',
borderRadius: '5px',
border: '0.55px solid #c7c7c7',
margin: '0px 5px 0px 5px',
textAlign: 'center',
},

label: {
lineHeight: 2,
userSelect: 'none',
},
const containerStyle: CSSProperties = {
padding: '5px',
height: '100%',
display: 'flex',
};

const labelStyle = {
Expand All @@ -50,7 +37,7 @@ const labelStyle = {
};

const inputStyle = {
input: { height: '24px' },
input: { height: '24px', width: '60px' },
};

const validationSchema = Yup.object().shape({
Expand All @@ -60,9 +47,7 @@ const validationSchema = Yup.object().shape({
});

const initialValues = {
lineBroadening: 1,
gaussBroadening: 0,
lineBroadeningCenter: 0,
...defaultApodizationOptions,
livePreview: true,
};

Expand Down Expand Up @@ -105,8 +90,17 @@ function ApodizationOptionsInnerPanel(
}
}, [props?.filter]);

const resetTool = (event: React.ChangeEvent<HTMLInputElement>) => {
if (!event.target.checked) {
dispatch({
type: SET_SELECTED_TOOL,
payload: { selectedTool: options.apodization.id },
});
}
};

return (
<div style={styles.container}>
<div style={containerStyle}>
<FormikForm
ref={formRef}
onSubmit={(values) => handleApplyFilter(values)}
Expand Down Expand Up @@ -144,7 +138,7 @@ function ApodizationOptionsInnerPanel(
/>
</Label>
<Label title="live preview " style={{ label: { padding: '0 5px' } }}>
<FormikCheckBox name="livePreview" />
<FormikCheckBox name="livePreview" onChange={resetTool} />
</Label>

<FormikOnChange
Expand Down
3 changes: 3 additions & 0 deletions src/component/reducer/Reducer.ts
Expand Up @@ -4,6 +4,7 @@ import { buildCorrelationData, CorrelationData } from 'nmr-correlation';
import { predictSpectra } from '../../data/PredictionManager';
import * as SpectraManager from '../../data/SpectraManager';
import { SpectraAnalysis } from '../../data/data1d/MultipleAnalysis';
import { ApodizationOptions } from '../../data/data1d/filter1d/apodization';
import { migrate } from '../../data/migration/MigrationManager';
import { Molecule } from '../../data/molecules/Molecule';
import { Contours } from '../../data/types/data2d/Contours';
Expand Down Expand Up @@ -114,6 +115,7 @@ export const getInitialState = (): State => ({
options: {},
zones: [],
},
apodizationOptions: {} as ApodizationOptions,
pivot: { value: 0, index: 0 },
zonesNoiseFactor: 1,
activeFilterID: null,
Expand Down Expand Up @@ -326,6 +328,7 @@ export interface State {
zones: any[];
options: any;
};
apodizationOptions: ApodizationOptions;
/**
* pivot point for manual phase correction
* @default {value:0,index:0}
Expand Down
1 change: 1 addition & 0 deletions src/component/reducer/actions/FiltersActions.ts
Expand Up @@ -78,6 +78,7 @@ function calculateApodizationFilter(draft: Draft<State>, action) {
} = draft.data[index] as Datum1D;

let _data = { data: { x, re, im }, info };
draft.toolOptions.data.apodizationOptions = options;
apodization(_data as Datum1D, options);
const { im: newIm, re: newRe } = _data.data;
draft.tempData[index].data.im = newIm;
Expand Down
5 changes: 5 additions & 0 deletions src/component/reducer/actions/ToolsActions.ts
Expand Up @@ -2,6 +2,7 @@ import { max } from 'd3';
import { original, Draft, current } from 'immer';
import { xFindClosestIndex } from 'ml-spectra-processing';

import { defaultApodizationOptions } from '../../../data/data1d/filter1d/apodization';
import { Data1D, Datum1D } from '../../../data/types/data1d';
import { Datum2D } from '../../../data/types/data2d';
import generateID from '../../../data/utilities/generateID';
Expand Down Expand Up @@ -96,6 +97,10 @@ function setFilterChanges(draft: Draft<State>, selectedFilterID) {
}
break;
}
case options.apodization.id: {
draft.toolOptions.data.apodizationOptions = defaultApodizationOptions;
break;
}

default:
break;
Expand Down

0 comments on commit 16b97b3

Please sign in to comment.