Skip to content

Commit

Permalink
feat: show/hide the value of integrals
Browse files Browse the repository at this point in the history
close #2797
  • Loading branch information
hamed-musallam committed Jan 11, 2024
1 parent 1223b29 commit f2a0ce3
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 31 deletions.
11 changes: 7 additions & 4 deletions src/component/1d/integral/Integration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export function Integration({ integral, nucleus, max }: IntegralProps) {
const { x, y } = integral;
const { scaleRatio } = useActiveSpectrumIntegralsViewState();
const path = useIntegralPath({ x, y, max, scaleRatio });
const { showIntegralsValues } = useActiveSpectrumIntegralsViewState();
const integralPreferences = usePanelPreferences('integrals', nucleus);

return (
Expand All @@ -27,10 +28,12 @@ export function Integration({ integral, nucleus, max }: IntegralProps) {
d={path}
/>

<IntegralResizable
integralData={integral}
integralFormat={integralPreferences.relative.format}
/>
{showIntegralsValues && (
<IntegralResizable
integralData={integral}
integralFormat={integralPreferences.relative.format}
/>
)}
</g>
);
}
16 changes: 10 additions & 6 deletions src/component/1d/ranges/Range.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { filterForIDsWithAssignment } from '../../assignment/utilities/filterFor
import { useDispatch } from '../../context/DispatchContext';
import { ResizerWithScale } from '../../elements/ResizerWithScale';
import { HighlightEventSource, useHighlight } from '../../highlight';
import { useActiveSpectrumRangesViewState } from '../../hooks/useActiveSpectrumRangesViewState';
import { useResizerStatus } from '../../hooks/useResizerStatus';
import { options } from '../../toolbar/ToolTypes';
import { IntegralIndicator } from '../integral/IntegralIndicator';
Expand Down Expand Up @@ -60,6 +61,7 @@ function Range({

const scaleX = useScaleX();
const dispatch = useDispatch();
const { showIntegralsValues } = useActiveSpectrumRangesViewState();

const isBlockedByEditing =
selectedTool && selectedTool === options.editRange.id;
Expand Down Expand Up @@ -145,12 +147,14 @@ function Range({
data-no-export="true"
/>

<IntegralIndicator
value={integration}
format={relativeFormat}
width={width}
opacity={isNotSignal ? 0.5 : 1}
/>
{showIntegralsValues && (
<IntegralIndicator
value={integration}
format={relativeFormat}
width={width}
opacity={isNotSignal ? 0.5 : 1}
/>
)}
</g>
);
}}
Expand Down
1 change: 1 addition & 0 deletions src/component/hooks/useActiveSpectrumIntegralsViewState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useActiveSpectrum } from './useActiveSpectrum';

export const defaultIntegralsViewState: IntegralsViewState = {
scaleRatio: 1,
showIntegralsValues: true,
};

export function useActiveSpectrumIntegralsViewState() {
Expand Down
1 change: 1 addition & 0 deletions src/component/hooks/useActiveSpectrumRangesViewState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const defaultRangesViewState: RangesViewState = {
showPeaks: false,
showMultiplicityTrees: false,
showIntegrals: false,
showIntegralsValues: true,
showJGraph: false,
displayingMode: 'spread',
integralsScaleRatio: 1,
Expand Down
30 changes: 29 additions & 1 deletion src/component/panels/IntegralsPanel/IntegralPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @jsxImportSource @emotion/react */
import { css } from '@emotion/react';
import { SvgNmrSum } from 'cheminfo-font';
import { SvgNmrIntegrate, SvgNmrSum } from 'cheminfo-font';
import lodashGet from 'lodash/get';
import { Spectrum1D } from 'nmr-load-save';
import { Info1D, Integrals } from 'nmr-processing';
Expand All @@ -22,6 +22,7 @@ import PreferencesHeader from '../header/PreferencesHeader';

import IntegralTable from './IntegralTable';
import IntegralsPreferences from './IntegralsPreferences';

Check warning on line 24 in src/component/panels/IntegralsPanel/IntegralPanel.tsx

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

There should be at least one empty line between import groups
import { useActiveSpectrumIntegralsViewState } from '../../hooks/useActiveSpectrumIntegralsViewState';

const style = css`
.sum-button {
Expand All @@ -31,6 +32,10 @@ const style = css`
align-items: center;
justify-content: center;
}
button {
margin-right: 2px;
}
`;

export interface IntegralPanelInnerProps {
Expand All @@ -49,10 +54,19 @@ function IntegralPanelInner({
const [filterIsActive, setFilterIsActive] = useState(false);

const dispatch = useDispatch();
const { showIntegralsValues } = useActiveSpectrumIntegralsViewState();

const modal = useModal();
const [isFlipped, setFlipStatus] = useState(false);
const settingRef = useRef<any>();

function handleShowIntegralsValues() {
dispatch({
type: 'TOGGLE_INTEGRALS_VIEW_PROPERTY',
payload: { key: 'showIntegralsValues' },
});
}

const yesHandler = useCallback(() => {
dispatch({ type: 'DELETE_INTEGRAL', payload: {} });
}, [dispatch]);
Expand Down Expand Up @@ -180,6 +194,20 @@ function IntegralPanelInner({
>
<ImLink />
</ActiveButton>
<ActiveButton
popupTitle={
showIntegralsValues
? 'Hide integrals values'
: 'Show integrals values'
}
popupPlacement="right"
onClick={handleShowIntegralsValues}
value={showIntegralsValues}
>
<SvgNmrIntegrate
style={{ pointerEvents: 'none', fontSize: '12px' }}
/>
</ActiveButton>
</DefaultPanelHeader>
)}
{isFlipped && (
Expand Down
22 changes: 22 additions & 0 deletions src/component/panels/RangesPanel/RangesHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ function RangesHeader({
showMultiplicityTrees,
showJGraph,
showIntegrals,
showIntegralsValues,
showPeaks,
displayingMode,
} = useActiveSpectrumRangesViewState();
Expand Down Expand Up @@ -111,6 +112,12 @@ function RangesHeader({
});
}

function handleShowIntegralsValues() {
dispatch({
type: 'TOGGLE_RANGES_VIEW_PROPERTY',
payload: { key: 'showIntegralsValues' },
});
}
function handleShowIntegrals() {
dispatch({
type: 'TOGGLE_RANGES_VIEW_PROPERTY',
Expand Down Expand Up @@ -258,6 +265,21 @@ function RangesHeader({
style={{ pointerEvents: 'none', fontSize: '12px' }}
/>
</ActiveButton>
<ActiveButton
popupTitle={
showIntegralsValues
? 'Hide integrals values'
: 'Show integrals values'
}
popupPlacement="right"
onClick={handleShowIntegralsValues}
value={showIntegralsValues}
disabled={!hasRanges}
>
<SvgNmrIntegrate
style={{ pointerEvents: 'none', fontSize: '12px' }}
/>
</ActiveButton>

<ActiveButton
className="icon"
Expand Down
5 changes: 5 additions & 0 deletions src/component/reducer/Reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,11 @@ function innerSpectrumReducer(draft: Draft<State>, action: Action) {
);
case 'CUT_INTEGRAL':
return IntegralsActions.handleCutIntegral(draft, action);
case 'TOGGLE_INTEGRALS_VIEW_PROPERTY':
return IntegralsActions.handleToggleIntegralsViewProperty(
draft,
action,
);

case 'SET_X_DOMAIN':
return DomainActions.handleSetXDomain(draft, action);
Expand Down
29 changes: 28 additions & 1 deletion src/component/reducer/actions/IntegralsActions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { v4 } from '@lukeed/uuid';
import { Draft, original } from 'immer';
import { xyIntegration } from 'ml-spectra-processing';
import { Spectrum1D } from 'nmr-load-save';
import { IntegralsViewState, Spectrum1D } from 'nmr-load-save';
import { Integral } from 'nmr-processing';

import {
Expand All @@ -20,6 +20,8 @@ import { getActiveSpectrum } from '../helper/getActiveSpectrum';
import getRange from '../helper/getRange';
import { getSpectrum } from '../helper/getSpectrum';
import { ActionType } from '../types/ActionType';
import { setIntegralsViewProperty } from '../helper/setIntegralsViewProperty';
import { FilterType } from '../../utility/filterType';

type ChangeIntegralSumAction = ActionType<
'CHANGE_INTEGRAL_SUM',
Expand All @@ -40,13 +42,21 @@ type ChangeIntegralRelativeValueAction = ActionType<
>;
type CutIntegralAction = ActionType<'CUT_INTEGRAL', { cutValue: number }>;

type ToggleIntegralsViewAction = ActionType<
'TOGGLE_INTEGRALS_VIEW_PROPERTY',
{
key: keyof FilterType<IntegralsViewState, boolean>;
}
>;

export type IntegralsActions =
| ChangeIntegralSumAction
| AddIntegralAction
| DeleteIntegralAction
| ChangeIntegralAction
| ChangeIntegralRelativeValueAction
| CutIntegralAction
| ToggleIntegralsViewAction
| ActionType<'CHANGE_INTEGRALS_SUM_FLAG'>;

function handleChangeIntegralSum(
Expand Down Expand Up @@ -219,6 +229,22 @@ function handleCutIntegral(draft: Draft<State>, action: CutIntegralAction) {
updateIntegralsRelativeValues(spectrum);
}

function toggleIntegralsViewProperty(
draft: Draft<State>,
key: keyof FilterType<IntegralsViewState, boolean>,
) {
setIntegralsViewProperty(draft, key, (flag) => !flag);
}

//action
function handleToggleIntegralsViewProperty(
draft: Draft<State>,
action: ToggleIntegralsViewAction,
) {
const { key } = action.payload;
toggleIntegralsViewProperty(draft, key);
}

export {
handleCutIntegral,
handleChangeIntegralSum,
Expand All @@ -227,4 +253,5 @@ export {
handleChangeIntegral,
handleChangeIntegralsRelativeValue,
handleChangeIntegralsSumFlag,
handleToggleIntegralsViewProperty,
};
35 changes: 16 additions & 19 deletions src/data/PredictionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,26 +340,23 @@ function generated2DSpectrum(params: {
experiment,
});
const spectralWidth = getSpectralWidth(experiment, options);
const datum = initiateDatum2D(
{
data: { rr: { ...minMaxContent, noise: 0.01 } },
display: {
positiveColor: color,
negativeColor: adjustAlpha(color, 40),
},
info: {
name: SpectrumName,
title: SpectrumName,
nucleus: nuclei,
originFrequency: frequency,
baseFrequency: frequency,
pulseSequence: 'prediction',
spectralWidth,
experiment,
},
const datum = initiateDatum2D({
data: { rr: { ...minMaxContent, noise: 0.01 } },
display: {
positiveColor: color,
negativeColor: adjustAlpha(color, 40),
},
[],
);
info: {
name: SpectrumName,
title: SpectrumName,
nucleus: nuclei,
originFrequency: frequency,
baseFrequency: frequency,
pulseSequence: 'prediction',
spectralWidth,
experiment,
},
});
datum.zones.values = mapZones(zones);
return datum;
}
Expand Down

0 comments on commit f2a0ce3

Please sign in to comment.