Skip to content

Commit

Permalink
chore: approach of context to keep the calculated contours
Browse files Browse the repository at this point in the history
  • Loading branch information
jobo322 committed Sep 3, 2024
1 parent 2e8c154 commit 4cc2778
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 22 deletions.
14 changes: 10 additions & 4 deletions src/component/2d/ft/Contours.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import debounce from 'lodash/debounce';
import get from 'lodash/get';
import { Spectrum2D } from 'nmr-load-save';
import { memo, useMemo, useRef } from 'react';
import { memo, useEffect, useMemo, useRef } from 'react';

Check failure on line 4 in src/component/2d/ft/Contours.tsx

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

'useEffect' is defined but never used

import {
drawContours,
getDefaultContoursLevel,
LevelSign,
} from '../../../data/data2d/Spectrum2D/contours';
import { useChartData } from '../../context/ChartContext';
import { useContourCache } from '../../context/ContourCacheContext';
import { usePreferences } from '../../context/PreferencesContext';
import { useToaster } from '../../context/ToasterContext';
import { useActiveSpectrum } from '../../hooks/useActiveSpectrum';
Expand Down Expand Up @@ -76,20 +77,25 @@ function ContoursPaths({
const activeSpectrum = useActiveSpectrum();
const preferences = usePreferences();
const level = useContoursLevel(spectrum, sign);
const [cache, setCache] = useContourCache();

const contours = useMemo(() => {
const { contours, timeout } = drawContours(
level,
spectrum,
cache,
sign === 'negative',
);
if (timeout) {
onTimeout();
}
return contours;
}, [spectrum, level, onTimeout, sign]);
setCache(cache);
return { contours, cache };
}, [spectrum, level, onTimeout, sign, cache, setCache]);

const path = usePath(spectrum, contours);
// useEffect(() => {}, [setCache, contours]);

const path = usePath(spectrum, contours.contours);

const opacity =
activeSpectrum === null || spectrumID === activeSpectrum.id
Expand Down
9 changes: 9 additions & 0 deletions src/component/context/ContourCacheContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React, { createContext, useState } from 'react';

const initialContourCache: Record<string, any> = {};
const ContourCacheContext = createContext(null);
export const ContourCacheProvider = ContourCacheContext.Provider;
export function useContourCache() {
React.useContext(ContourCacheContext);
return useState(initialContourCache);
}
58 changes: 40 additions & 18 deletions src/data/data2d/Spectrum2D/contours.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,45 +177,67 @@ function range(from: number, to: number, step: number) {
function drawContours(
level: ContourItem,
spectrum: Spectrum2D,
contourCache: Record<string, any>,
negative = false,
quadrant = 'rr',
) {
const { contourLevels, numberOfLayers } = level;
const key = negative ? 'negative' : 'positive';

return getContours({
const nbLevels = Math.min(
numberOfLayers,
contourLevels[1] - contourLevels[0],
);

const { id, data } = spectrum;
if (!contourCache[id]) {

Check failure on line 193 in src/data/data2d/Spectrum2D/contours.ts

View workflow job for this annotation

GitHub Actions / nodejs / test (18)

src/data/data2d/__tests__/Datum2D.test.ts > Datum2D

TypeError: Cannot read properties of undefined (reading '2e86dbed-ae58-429a-875e-b93b7e8c8a4e') ❯ Module.drawContours src/data/data2d/Spectrum2D/contours.ts:193:20 ❯ src/data/data2d/__tests__/Datum2D.test.ts:28:20

Check failure on line 193 in src/data/data2d/Spectrum2D/contours.ts

View workflow job for this annotation

GitHub Actions / nodejs / test (20)

src/data/data2d/__tests__/Datum2D.test.ts > Datum2D

TypeError: Cannot read properties of undefined (reading 'd703e77e-e0a2-43c8-a0f5-d81ace81f878') ❯ Module.drawContours src/data/data2d/Spectrum2D/contours.ts:193:20 ❯ src/data/data2d/__tests__/Datum2D.test.ts:28:20

Check failure on line 193 in src/data/data2d/Spectrum2D/contours.ts

View workflow job for this annotation

GitHub Actions / nodejs / test (22)

src/data/data2d/__tests__/Datum2D.test.ts > Datum2D

TypeError: Cannot read properties of undefined (reading '6793563d-9ed4-4923-adce-ad296281e9c1') ❯ Module.drawContours src/data/data2d/Spectrum2D/contours.ts:193:20 ❯ src/data/data2d/__tests__/Datum2D.test.ts:28:20
contourCache[id] = {};
}

if (!(key in contourCache[id])) {
contourCache[id][key] = { contours: [], timeout: false };
}

const oneSenseContours = contourCache[id][key].contours;
const selectedLevels = getRange(
Math.max(0, contourLevels[0]),
contourLevels[1],
nbLevels,
).map((e) => Math.round(e));

const levels = selectedLevels.filter((level) => !oneSenseContours[level]);
const { contours, timeout } = getContours({
levels,
negative,
boundary: contourLevels,
nbLevels: numberOfLayers,
data: spectrum.data[quadrant],
data: data[quadrant],
});

for (const [i, level] of contours.entries()) {
oneSenseContours[levels[i]] = level;
}
contourCache[id][key] = { contours: oneSenseContours, timeout };

return {
contours: selectedLevels.map((level) => oneSenseContours[level]),
timeout,
};
}

interface ContoursCalcOptions {
boundary: [number, number];
levels: number[];
negative?: boolean;
timeout?: number;
nbLevels: number;
data: NmrData2DFt['rr'];
}

function getContours(options: ContoursCalcOptions) {
const {
boundary,
negative = false,
timeout = 2000,
nbLevels,
data,
} = options;
const { levels, negative = false, timeout = 2000, data } = options;
const xs = getRange(data.minX, data.maxX, data.z[0].length);
const ys = getRange(data.minY, data.maxY, data.z.length);
const conrec = new Conrec(data.z, { xs, ys, swapAxes: false });
const max = Math.max(Math.abs(data.minZ), Math.abs(data.maxZ));
const minLevel = calculateValueOfLevel(boundary[0], max);
const maxLevel = calculateValueOfLevel(boundary[1], max);

const diffRange = boundary[1] - boundary[0];

let _range = getRange(minLevel, maxLevel, Math.min(nbLevels, diffRange), 2);
let _range = levels.map((level) => calculateValueOfLevel(level, max));
if (negative) {
_range = _range.map((value) => -value);
}
Expand Down

0 comments on commit 4cc2778

Please sign in to comment.