Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: calculate only missing contour levels #3224

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/component/2d/ft/Contours.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
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 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 '8f1d664f-08ca-41e1-8dca-ba0b10f60050') ❯ 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 'b1a23576-1c7c-4b27-86c8-c30dc5f60164') ❯ 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 '3d3bb788-faa0-415b-b479-34210807f8d9') ❯ 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
Loading