Skip to content

Commit

Permalink
feat: auto assignments (#1456)
Browse files Browse the repository at this point in the history
* feat: auto assignments

* chore: reduce minScore in auto-assignment

* chore: prepare set automatic assignments action

* chore: update nmr-processing to 7.4.2

* feat: auto assignments

* chore: update nmr-processing

* refactor: assignments tool (#1470)

* refactor: assignments tool

* fix: import assignments types

* refactor: assignments context

* fix: y axis assignments

* fix: 2d assignments

* fix: leaving a zone is not always detected

Co-authored-by: jobo322 <jose.bolanos@correounivalle.edu.co>
  • Loading branch information
hamed-musallam and jobo322 committed Apr 14, 2022
1 parent cd6d86e commit 1024c2e
Show file tree
Hide file tree
Showing 41 changed files with 894 additions and 656 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -70,7 +70,7 @@
"multiplet-analysis": "^2.0.0",
"nmr-correlation": "^2.2.5",
"nmr-parser": "^1.7.3",
"nmr-processing": "^7.4.2",
"nmr-processing": "^7.4.3",
"nmredata": "^0.6.1",
"numeral": "^2.0.6",
"openchemlib": "^7.4.3",
Expand Down
10 changes: 5 additions & 5 deletions src/component/1d/multiplicityTree/MultiplicityTree.tsx
Expand Up @@ -3,7 +3,7 @@ import lodashGet from 'lodash/get';
import { useMemo, useState, useEffect, CSSProperties } from 'react';

import { Signal1D } from '../../../data/types/data1d';
import { useAssignment } from '../../assignment';
import { useAssignment } from '../../assignment/AssignmentsContext';
import { useChartData } from '../../context/ChartContext';
import { useScaleChecked } from '../../context/ScaleContext';
import { HighlightedSource, useHighlight } from '../../highlight';
Expand Down Expand Up @@ -41,7 +41,7 @@ interface MultiplicityTreeProps {
}

function extractID(assignment) {
return [assignment.id].concat(assignment.assigned.x || []);
return [assignment.id].concat(assignment.assigned?.x || []);
}

function MultiplicityTree({
Expand Down Expand Up @@ -273,11 +273,11 @@ function MultiplicityTree({
}
{...{
onMouseEnter: () => {
assignment.onMouseEnter('x');
assignment.show('x');
highlight.show();
},
onMouseLeave: () => {
assignment.onMouseLeave('x');
assignment.hide();
highlight.hide();
},
}}
Expand All @@ -290,7 +290,7 @@ function MultiplicityTree({
: (e) => {
if (e.shiftKey) {
e.stopPropagation();
assignment.onClick('x');
assignment.setActive('x');
}
},
}}
Expand Down
12 changes: 6 additions & 6 deletions src/component/1d/ranges/Range.tsx
Expand Up @@ -5,10 +5,10 @@ import { useCallback, useState, useEffect } from 'react';
import { Signal1D } from '../../../data/types/data1d';
import { checkRangeKind } from '../../../data/utilities/RangeUtilities';
import {
filterForIDsWithAssignment,
useAssignment,
useAssignmentData,
} from '../../assignment';
} from '../../assignment/AssignmentsContext';
import { filterForIDsWithAssignment } from '../../assignment/utilities/filterForIDsWithAssignment';
import { useDispatch } from '../../context/DispatchContext';
import { useScaleChecked } from '../../context/ScaleContext';
import Resizer from '../../elements/resizer/Resizer';
Expand Down Expand Up @@ -73,7 +73,7 @@ function Range({
const assignmentData = useAssignmentData();
const assignmentRange = useAssignment(id);
const highlightRange = useHighlight(
[assignmentRange.id].concat(assignmentRange.assigned.x || []).concat(
[assignmentRange.id].concat(assignmentRange.assigned?.x || []).concat(
filterForIDsWithAssignment(
assignmentData,
signals.map((_signal) => _signal.id),
Expand Down Expand Up @@ -115,12 +115,12 @@ function Range({
);

const mouseEnterHandler = useCallback(() => {
assignmentRange.onMouseEnter('x');
assignmentRange.show('x');
highlightRange.show();
}, [assignmentRange, highlightRange]);

const mouseLeaveHandler = useCallback(() => {
assignmentRange.onMouseLeave('x');
assignmentRange.hide();
highlightRange.hide();
}, [assignmentRange, highlightRange]);

Expand All @@ -131,7 +131,7 @@ function Range({
e.shiftKey &&
!isBlockedByEditing
) {
assignmentRange.onClick('x');
assignmentRange.setActive('x');
}
},
[assignmentRange, isBlockedByEditing, selectedTool],
Expand Down
7 changes: 3 additions & 4 deletions src/component/2d/zones/Signal.tsx
Expand Up @@ -4,7 +4,7 @@ import { memo, useState, useEffect, useCallback } from 'react';

import { Signal2D } from '../../../data/types/data2d';
import { buildID } from '../../../data/utilities/Concatenation';
import { useAssignment } from '../../assignment';
import { useAssignment } from '../../assignment/AssignmentsContext';
import { useChartData } from '../../context/ChartContext';
import { useHighlightData, useHighlight } from '../../highlight';
import { get2DXScale, get2DYScale } from '../utilities/scale';
Expand Down Expand Up @@ -60,14 +60,13 @@ const Signal = memo(({ signal, isVisible }: SignalProps) => {
{isVisible.signals && (
<g>
<SignalCrosshair signal={signal} />

<circle
onMouseEnter={() => {
assignment.onMouseEnter(undefined);
assignment.show();
highlight.show();
}}
onMouseLeave={() => {
assignment.onMouseLeave(undefined);
assignment.hide();
highlight.hide();
}}
key={signal.id}
Expand Down
6 changes: 3 additions & 3 deletions src/component/2d/zones/Zone.tsx
Expand Up @@ -4,7 +4,7 @@ import { useState, useEffect } from 'react';

import { Zone as ZoneDataProps } from '../../../data/types/data2d';
import { checkZoneKind } from '../../../data/utilities/ZoneUtilities';
import { useAssignment } from '../../assignment';
import { useAssignment } from '../../assignment/AssignmentsContext';
import { useChartData } from '../../context/ChartContext';
import { HighlightedSource, useHighlight } from '../../highlight';
import { get2DXScale, get2DYScale } from '../utilities/scale';
Expand Down Expand Up @@ -76,11 +76,11 @@ const Zone = ({ zoneData, isVisible }: ZoneProps) => {
}
key={id}
onMouseEnter={() => {
assignmentZone.onMouseEnter(undefined);
assignmentZone.show();
highlightZone.show();
}}
onMouseLeave={() => {
assignmentZone.onMouseLeave(undefined);
assignmentZone.hide();
highlightZone.hide();
}}
>
Expand Down
2 changes: 1 addition & 1 deletion src/component/EventsTrackers/KeysListenerTracker.tsx
@@ -1,7 +1,7 @@
import { useCallback, useEffect, useMemo } from 'react';

import checkModifierKeyActivated from '../../data/utilities/checkModifierKeyActivated';
import { useAssignmentData } from '../assignment';
import { useAssignmentData } from '../assignment/AssignmentsContext';
import { useChartData } from '../context/ChartContext';
import { useDispatch } from '../context/DispatchContext';
import { useLoader } from '../context/LoaderContext';
Expand Down
147 changes: 147 additions & 0 deletions src/component/assignment/AssignmentsContext.ts
@@ -0,0 +1,147 @@
import { createContext, useCallback, useContext, useMemo } from 'react';

import { AssignmentsActions } from './AssignmentsReducer';

export type Axis = 'x' | 'y';
export type Assignment = Record<Axis, string[]>;
export type Assignments = Record<string, Assignment>;

export type AssignmentDimension = '1D' | '2D';
export interface AssignmentAction {
id: string;
axis: Axis | null;
}
export interface AssignmentState {
assignments: Assignments;
activated: AssignmentAction | null;
highlighted: AssignmentAction | null;
}
export interface AssignmentContext {
data: AssignmentState;
dispatch: (action: AssignmentsActions) => void;
}

export interface AssignmentsData
extends Pick<AssignmentState, 'activated' | 'highlighted'> {
id: string;
isActive: boolean;
isOver: boolean;
assigned: Record<Axis, string[]>;
removeAll: (axis: Axis) => void;
toggle: (atomIDs: string[], dimension: AssignmentDimension) => void;
setActive: (axis: Axis) => void;
show: (axis?: Axis) => void;
hide: () => void;
}

export const assignmentState: AssignmentState = {
assignments: {},
activated: null,
highlighted: null,
};

export const assignmentContext = createContext<AssignmentContext>({
data: assignmentState,
dispatch: () => null,
});

export const AssignmentProvider = assignmentContext.Provider;

export function useAssignmentData() {
const context = useContext(assignmentContext);

if (!context) {
throw new Error('Assignment context was not found');
}

return context;
}

// key can be signal id,range id or zone id
export function useAssignment(key): AssignmentsData {
const {
data: { activated, highlighted, assignments },
dispatch,
} = useAssignmentData();

if ((typeof key !== 'string' && typeof key !== 'number') || key === '') {
throw new Error(`assignment key must be a non-empty string or number`);
}
const id = String(key);

const isActive = useMemo(() => {
return activated?.id === id;
}, [activated?.id, id]);
const isOver = useMemo(() => {
return highlighted?.id === id;
}, [highlighted?.id, id]);

const assigned = useMemo(() => {
return assignments[id] || null;
}, [assignments, id]);

const removeAll = useCallback(
(axis: Axis) => {
dispatch({
type: 'REMOVE',
payload: { ids: [id], axis },
});
},
[dispatch, id],
);

const toggle = useCallback(
(atomIDs: string[], dimension: AssignmentDimension) => {
dispatch({
type: 'TOGGLE',
payload: { atomIDs, id, dimension },
});
},
[dispatch, id],
);

const setActive = useCallback(
(axis: Axis) => {
dispatch({
type: 'SET_ACTIVE',
payload: {
id,
axis,
},
});
},
[dispatch, id],
);

const show = useCallback(
(axis?: Axis) => {
dispatch({
type: 'SHOW',
payload: {
id,
axis,
},
});
},
[dispatch, id],
);
const hide = useCallback(() => {
dispatch({
type: 'HIDE',
});
}, [dispatch]);

return {
id,
activated,
isActive,
isOver,
highlighted,
assigned,
removeAll,
toggle,
setActive,
show,
hide,
};
}

0 comments on commit 1024c2e

Please sign in to comment.