Skip to content

Conversation

@xinaesthete
Copy link
Collaborator

@xinaesthete xinaesthete commented Apr 9, 2025

@coderabbitai I'm doing something very wrong with my state management the way ChannelHistogram is used for colour channel limit control - I thought it was working reasonably ok at some point recently (before I went to bed), but it just goes into an infinite loop now. The original use seems ok.

Summary by CodeRabbit

  • New Features

    • Introduced an interactive histogram for dynamic range filtering and visualization.
    • Enhanced scatter plot interactivity with responsive hover behavior.
    • Added improved error display components for channel controls.
  • Refactor

    • Streamlined channel and selection dialogs for a more intuitive and robust user experience.

xinaesthete and others added 25 commits April 7, 2025 19:06
interactive rebase in progress; onto 9836fa8
Last commands done (2 commands done):
   pick fc6fb44 revisit of deck scatterplot
   pick 994d9b8 add visx dependency
You are currently rebasing branch 'deck_2025b' on '9836fa8e'.

Changes to be committed:
	modified:   package-lock.json
	modified:   package.json
try/catch in unproject rather than using onAfterRender that caused infinite loop.
* use container div for deckgl, fix axis update and mouse coordinates
* move useScatterplotLayer so that we are always using the same instance
* some doc comments on spatial layers
* useScatterRadius hook
* somewhat reasonable default pending better relation to units etc
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Apr 9, 2025

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The changes update multiple React components and utilities. A new histogram component and range filtering hooks have been added, with state management improvements using hooks and debouncing. The channel components now include raster data in their state and statistics calls, along with a new error display function. Other modifications include revised control flow with explicit block statements, updated hover handling in scatter components, and enhanced type definitions for raster data. Additionally, formatting, comments for future reviews, and minor refactorings have been introduced across several components.

Changes

File(s) Change Summary
src/react/components/ColorChannelComponents.tsx Added new imports; updated ChannelChooser to include a raster property; introduced ChannelHistogram for managing histogram data with hooks, debouncing, and error handling; added ChannelError function; modified ChannelController to remove direct limits/domains handling.
src/react/components/ColorChannelDialogReactWrapper.tsx Revised the close method to use a block statement and added a clarifying comment for cases when the React root is undefined.
src/react/components/HistogramComponent.tsx Introduced a new React component for rendering an interactive histogram using D3.js brush functionality; added the useRangeFilter hook and several type definitions for range filtering; integrated with MobX observer for responsive updates.
src/react/components/SelectionDialogComponent.tsx Removed the useRangeFilter function and associated types; added a new import for Histogram; refactored event handlers and reformatted ErrorBoundary fallback rendering.
src/react/components/VivScatterComponent.tsx Made formatting adjustments in useJsonLayer and updated type declarations; added a new hoverHooks property with a handleValue function to update the viewer store on hover events.
src/react/components/.../avivatorish/{MDVivViewer.tsx, state.tsx, utils.ts} In MDVivViewer.tsx: added review comments for "ome_thumbnails" branch changes; in state.tsx: introduced a new Raster type and integrated it into ChannelsState and default states; in utils.ts: improved parameter formatting and updated return values of selection stats functions to include raster data.

Sequence Diagram(s)

sequenceDiagram
    participant U as User
    participant CH as ChannelHistogram
    participant WW as WebWorker
    participant EB as ErrorBoundary

    U->>CH: Adjust histogram control
    CH->>CH: Debounce value update
    CH->>WW: Request histogram data
    WW-->>CH: Return histogram data
    CH->>CH: Update contrast limits and state
    EB-->>CH: Handle errors if any
Loading
sequenceDiagram
    participant U as User
    participant HC as HistogramComponent
    participant D3 as D3 Brush
    participant WW as WebWorker

    U->>HC: Interact with histogram (brush gesture)
    HC->>D3: Initialize brush events
    D3-->>HC: Emit range selection event
    HC->>HC: Debounce and update range filter state
    HC->>WW: Query filtered data
    WW-->>HC: Return filtered results
    HC->>HC: Render updated histogram
Loading

Poem

I'm a hopping rabbit coding through the night,
New histograms bloom with data shining bright.
Channels now dance with raster in their song,
And error boundaries catch what might go wrong.
With brush strokes of D3 and debounced delight,
My code hops forward to a future ever so light!
🐇✨


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@xinaesthete
Copy link
Collaborator Author

@coderabbitai review, and suggest saner state management in Histogram / ChannelHistogram...

@xinaesthete xinaesthete marked this pull request as ready for review April 9, 2025 14:09
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Apr 9, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Apr 9, 2025

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Find the Histogram and ChannelHistogram files
fd -t f -e js -e jsx -e ts -e tsx "Histogram|ChannelHistogram" --exec echo "File: {}"

Length of output: 220


🏁 Script executed:

#!/bin/bash
# Get the PR diff to see changed files
git diff --name-only origin/main...HEAD | grep -E "Histogram|histogram"

# Let's look at the HistogramComponent.tsx file content
cat ./src/react/components/HistogramComponent.tsx

# Let's also check the other histogram-related files
cat ./src/datastore/rawHistogramWorker.ts
cat ./src/charts/HistogramChart.js

# Look for any recent changes involving "ChannelHistogram" or state management
git log -n 5 --pretty=format:"%h %s" --grep="histogram\|Histogram\|Channel\|state"

Length of output: 20541


🏁 Script executed:

#!/bin/bash
# Search for ChannelHistogram in all files
rg "ChannelHistogram" -l

# Get the diff for this PR to see what changes have been made
git diff HEAD~1

Length of output: 277


🏁 Script executed:

#!/bin/bash
# Look at the ColorChannelComponents.tsx file
cat src/react/components/ColorChannelComponents.tsx

# Try to get PR details using a different approach
git branch --show-current
git log -n 1 --pretty=format:"%H"

Length of output: 17292


@xinaesthete I'll help you review and fix the state management in the Histogram and ChannelHistogram components. Let me first examine the files to understand what's causing the infinite loop.


🏁 Script executed:

#!/bin/bash
# Look at the PR diff to see what's changed
git checkout coderabbit_169
git diff origin/main -- src/react/components/ColorChannelComponents.tsx src/react/components/HistogramComponent.tsx

Length of output: 24447


@xinaesthete I've identified the cause of the infinite loop in your state management between ChannelHistogram and Histogram. The issue is a circular update pattern where:

  1. Brush interaction → updates component state → updates store
  2. Store update → updates props → moves brush → triggers brush event → back to step 1

Here's a fix for the ChannelHistogram component:

const ChannelHistogram = ({ index }: { index: number }) => {
    const limits = useChannelsStore(({ contrastLimits }) => contrastLimits);
    const { domains, raster } = useChannelsStore(({ domains, raster }) => ({ domains, raster }));
    const domain = domains[index];
    const rasterData = raster[index]?.data || [0];
    
    const [histogramData, setHistogramData] = useState<number[]>([]);
    // Add a ref to track if changes are coming from user interaction
    const isUserInteraction = useRef(false);

    const [min, max] = domain;
    const scaleValue = useCallback((v: number) => (v - min) / (max - min), [min, max]);
    const { limit, normalisedLow, normalisedHigh } = useMemo(() => {
        const limit = limits[index];
        const normalisedLow = scaleValue(limit[0]);
        const normalisedHigh = scaleValue(limit[1]);
        return { limit, normalisedLow, normalisedHigh };
    }, [limits, index, scaleValue]);
    
    const channelsStore = useChannelsStoreApi();

    // Histogram data query logic
    const queryHistogram = useCallback(async () => {
        const worker = new Worker(
            new URL("../../datastore/rawHistogramWorker.ts", import.meta.url),
        );
        worker.onmessage = (event) => {
            setHistogramData(event.data);
            worker.terminate();
        };
        const isInt32 = false;
        const originalData = rasterData;
        const data = new SharedArrayBuffer(originalData.length * 4);
        new Float32Array(data).set(originalData);
        worker.postMessage({
            data,
            min,
            max,
            bins: 100,
            isFloat: isInt32,
        });
    }, [min, max, rasterData]);
    
    // Use a more substantial debounce time
    const [liveValue, setLiveValue] = useState<Range | null>(null);
    const debounceTime = 50; // Increased from 10
    const [debouncedValue] = useDebounce(liveValue, debounceTime);
    
    useEffect(() => {
        // Only update store if this came from user interaction
        if (!isUserInteraction.current) return;
        if (!debouncedValue) return;
        if (debouncedValue.some(Number.isNaN)) return;
        
        // Deep equality check
        const currentLimits = channelsStore.getState().contrastLimits;
        const currentLimit = currentLimits[index];
        if (
            currentLimit && 
            Math.abs(debouncedValue[0] - currentLimit[0]) < 0.0001 && 
            Math.abs(debouncedValue[1] - currentLimit[1]) < 0.0001
        ) {
            return;
        }
        
        // Create a new array instead of mutating
        const newLimits = [...currentLimits];
        newLimits[index] = [...debouncedValue];
        
        // After updating store, reset the flag
        isUserInteraction.current = false;
        channelsStore.setState({ contrastLimits: newLimits });
    }, [debouncedValue, index, channelsStore]);
    
    const isChannelLoading = useViewerStore((state) => state.isChannelLoading)[index];
    
    return (
        <div className="p-4">
            {!isChannelLoading ? <Histogram 
                value={limit}
                step={0.001}
                histogram={histogramData}
                lowFraction={normalisedLow}
                highFraction={normalisedHigh}
                queryHistogram={queryHistogram}
                setValue={v => {
                    // Set the user interaction flag when value comes from the brush
                    isUserInteraction.current = true;
                    if (v) setLiveValue(v);
                }}
                minMax={domain}
                histoWidth={100}
                histoHeight={50}
            /> : "Loading..."}
        </div>
    );
};

And also modify the useBrushX hook in the Histogram component:

const useBrushX = (
    ref: React.RefObject<SVGSVGElement>,
    { value, setValue, minMax, histoWidth, histoHeight }: RangeProps,
) => {
    const brushRef = useRef<ReturnType<typeof d3.brushX> | null>(null);
    // Track whether we're currently handling a programmatic update
    const isUpdatingProgrammatically = useRef(false);
    const [initialValue] = useState(value);

    useEffect(() => {
        if (!ref.current) return;

        const svg = d3.select(ref.current);
        // Set up brush
        const brush = d3
            .brushX()
            .handleSize(1)
            .extent([
                [0, -2],
                [histoWidth, histoHeight + 2],
            ])
            .on("brush end", (event) => {
                // Skip if this event was triggered programmatically
                if (isUpdatingProgrammatically.current) return;
                
                if (event.selection) {
                    const [start, end] = event.selection.map((x: number) => {
                        if (!ref.current) {
                            console.error("No ref.current in brush event handler");
                            return 0;
                        }
                        const { width } = ref.current.getBoundingClientRect();
                        const r = width / histoWidth;
                        const normalizedX = (r * x) / width;
                        return minMax[0] + normalizedX * (minMax[1] - minMax[0]);
                    });
                    setValue([start, end]);
                } else {
                    setValue(null);
                }
            });

        brushRef.current = brush;

        // Apply the brush to the SVG
        const brushGroup = svg.append("g").attr("class", "brush").call(brush);
        
        // Initialize brush selection based on the initial value
        if (initialValue) {
            isUpdatingProgrammatically.current = true;
            const [start, end] = initialValue.map(
                (v) => ((v - minMax[0]) / (minMax[1] - minMax[0])) * histoWidth,
            );
            brushGroup.call(brush.move, [start, end]);
            isUpdatingProgrammatically.current = false;
        }

        // Apply vector effect styling
        brushGroup
            .selectAll(".selection")
            .attr("vector-effect", "non-scaling-stroke");
        brushGroup
            .selectAll(".handle")
            .attr("vector-effect", "non-scaling-stroke");

        // Cleanup on unmount
        return () => {
            svg.select(".brush").remove();
        };
    }, [ref, setValue, minMax, histoWidth, histoHeight, initialValue]);

    // Use a longer debounce time for prop changes
    const [debouncedValue] = useDebounce(value, 150, {
        equalityFn: (a, b) => {
            if (!a && !b) return true;
            if (!a || !b) return false;
            return a[0] === b[0] && a[1] === b[1];
        },
    });
    
    const setBrushValue = useCallback<set2d>(
        (v) => {
            if (!brushRef.current || !ref.current) return;
            const svg = d3.select(ref.current);

            // Set flag before updating
            isUpdatingProgrammatically.current = true;
            
            if (!v) {
                svg.select(".brush").call(brushRef.current.move, null);
                isUpdatingProgrammatically.current = false;
                return;
            }
            
            const [start, end] = v;
            const x0 = ((start - minMax[0]) / (minMax[1] - minMax[0])) * histoWidth;
            const x1 = ((end - minMax[0]) / (minMax[1] - minMax[0])) * histoWidth;
            
            svg.select(".brush").call(brushRef.current.move, [x0, x1]);
            
            // Reset flag after updating
            isUpdatingProgrammatically.current = false;
        },
        [minMax, histoWidth, ref],
    );
    
    useEffect(() => {
        if (debouncedValue) {
            setBrushValue(debouncedValue);
        }
    }, [debouncedValue, setBrushValue]);
};

The key changes to fix the infinite loop:

  1. Added isUserInteraction flag to track when changes come from user vs. store
  2. Added isUpdatingProgrammatically flag to prevent brush events during programmatic updates
  3. Increased debounce times to avoid race conditions
  4. Added better equality checking to prevent unnecessary updates
  5. Fixed store updates to avoid direct mutation of the array
  6. Only update the store when changes come from user interaction

These changes break the circular dependency by preventing the update cycles, while maintaining the functionality where brush changes update the store and store changes update the brush UI.

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@xinaesthete xinaesthete force-pushed the MD-476-viv-histogram-widgets branch from b5f89f4 to 4eda2f2 Compare April 15, 2025 09:37
@xinaesthete
Copy link
Collaborator Author

@coderabbitai I think we should use tanstack query for queryHistogram, can you sketch something based on that?

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Apr 15, 2025

Tip

For best results, initiate chat on the files or code changes.

@xinaesthete Using TanStack Query for your histogram data fetching is an excellent idea! Here's a sketch of how you could implement it:

// 1. First, create a worker utility that returns promises
// src/utils/workerUtils.ts
export function createHistogramWorker(data: Float32Array, min: number, max: number, bins: number) {
  return new Promise

<number[]>((resolve, reject) => {
    try {
      const worker = new Worker(
        new URL("../../datastore/rawHistogramWorker.ts", import.meta.url)
      );
      
      worker.onmessage = (event) => {
        resolve(event.data);
        worker.terminate();
      };
      
      worker.onerror = (error) => {
        reject(error);
        worker.terminate();
      };
      
      const sharedBuffer = new SharedArrayBuffer(data.length * 4);
      new Float32Array(sharedBuffer).set(data);
      
      worker.postMessage({
        data: sharedBuffer,
        min,
        max,
        bins,
        isFloat: true,
      });
    } catch (err) {
      reject(err);
    }
  });
}

// 2. Create a React Query hook
// src/hooks/useHistogramQuery.ts
import { useQuery } from '@tanstack/react-query';
import { createHistogramWorker } from '../utils/workerUtils';

export function useHistogramQuery(
  channelIndex: number,
  rasterData: Float32Array,
  domain: [number, number],
  bins = 100,
  enabled = true
) {
  return useQuery({
    queryKey: ['histogram', channelIndex, domain[0], domain[1], bins, rasterData.length],
    queryFn: () => createHistogramWorker(rasterData, domain[0], domain[1], bins),
    enabled: enabled && rasterData.length > 0,
    staleTime: Infinity, // Data won't get stale unless explicitly invalidated
    // Only refetch when rasterData or domain actually changes content
    structuralSharing: false,
  });
}

// 3. Update the ChannelHistogram component
// ChannelHistogram.tsx
import { useHistogramQuery } from '../hooks/useHistogramQuery';
import { Histogram } from './Histogram'; // Your existing or visx-based histogram component
import { useChannelsStore, useChannelsStoreApi } from '../store/channelsStore';

const ChannelHistogram = ({ index }: { index: number }) => {
  const { contrastLimits, domains, raster } = useChannelsStore(state => ({
    contrastLimits: state.contrastLimits,
    domains: state.domains,
    raster: state.raster
  }));
  const channelsStore = useChannelsStoreApi();
  
  const domain = domains[index] || [0, 1];
  const limit = contrastLimits[index] || domain;
  const rasterData = raster[index]?.data || new Float32Array();
  
  // Use TanStack Query for histogram data
  const { 
    data: histogramData = [], 
    isLoading, 
    isError, 
    error 
  } = useHistogramQuery(index, rasterData, domain, 100, true);
  
  const handleRangeChange = useCallback((newLimit: [number, number]) => {
    const newLimits = [...contrastLimits];
    newLimits[index] = newLimit;
    channelsStore.setState({ contrastLimits: newLimits });
  }, [channelsStore, contrastLimits, index]);
  
  if (isLoading) return <div className="p-4">Loading histogram...</div>;
  if (isError) return <div className="p-4">Error: {error?.message}</div>;
  
  return (
    <div className="p-4">
      <Histogram
        value={limit}
        setValue={handleRangeChange}
        minMax={domain}
        histogram={histogramData}
        histoWidth={100}
        histoHeight={50}
      />
    </div>
  );
};

Setup Requirements

npm install @tanstack/react-query

And then add the query client to your app:

// In your app entry point
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';

const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      refetchOnWindowFocus: false,
    },
  },
});

function App() {
  return (
    <QueryClientProvider client={queryClient}>
      {/* Your app components */}
      <ReactQueryDevtools /> {/* Optional but helpful for debugging */}
    </QueryClientProvider>
  );
}

Benefits of this approach:

  1. Automatic caching - Histogram data is cached by channel index, domain, and other parameters
  2. Loading/error states - Built-in loading and error handling
  3. Separation of concerns - Data fetching logic is separate from UI components
  4. Automatic refetching - Data refetches when query params change
  5. Devtools integration - Can see all queries and their states in React Query DevTools

You could extend this further by integrating with TanStack Query's query invalidation to force refreshes when needed or implement prefetching for expected user interactions.

Would you like me to elaborate on any specific aspect of this implementation?

✏️ Learnings added
Learnt from: xinaesthete
URL: https://github.com/Taylor-CCB-Group/MDV/pull/169

Timestamp: 2025-04-15T09:41:58.353Z
Learning: When implementing data fetching for visualization components in React, TanStack Query (React Query) provides significant advantages over direct worker communication by offering automatic caching, loading states, and separation of concerns. This is particularly valuable for histogram data that needs to be generated from large array buffers, as it allows for efficient caching based on input parameters and better handling of the asynchronous processing.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

---
🧠 Learnings used
Learnt from: xinaesthete
PR: Taylor-CCB-Group/MDV#0
File: :0-0
Timestamp: 2025-04-09T14:27:39.036Z
Learning: When implementing interactive D3 visualization components like histograms with brushes, use a unidirectional data flow with both real-time updates and minimal guards (like flags and short debounce times) to prevent infinite update loops. Real-time updates during interaction provide better UX while maintaining application state consistency.

Learnt from: xinaesthete
PR: Taylor-CCB-Group/MDV#0
File: :0-0
Timestamp: 2025-04-09T14:25:03.682Z
Learning: In visualization components with interactive elements like histograms with D3 brushes, it's better to follow a unidirectional data flow pattern where the store is the single source of truth, rather than adding complexity with flags and guards to prevent infinite update loops.

@xinaesthete xinaesthete force-pushed the deck_2025b branch 4 times, most recently from de20661 to b11dd38 Compare April 23, 2025 11:10
@xinaesthete xinaesthete force-pushed the deck_2025b branch 2 times, most recently from 31a8002 to b7c8cc3 Compare April 23, 2025 15:10
Base automatically changed from deck_2025b to dev April 23, 2025 15:50
@xinaesthete xinaesthete marked this pull request as draft May 20, 2025 09:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants