Skip to content

Optimization: Replace Iterative Random Crop Validation in _crop_random with 3D Max Filter Pre-Check #3

@nezba00

Description

@nezba00

Priority: Medium (Performance Improvement)

The private method _crop_random in img_proc/crop.py is currently inefficient. It uses a guessing loop (while (crop_count < num_random_tracks)) that randomly selects coordinates and performs slow, frame-by-frame validation for three criteria: spatial boundaries, APO-blocking status, and correct size.

This iterative approach is computationally wasteful for checks based on static volume data.

Proposed Solution

Dramatically improve performance by replacing the blocking checks with a single, fast 3D Maximum Filter pre-calculation.

Implementation Plan:

  1. Pre-Calculation (New Block in _crop_random):

    • Use scipy.ndimage.maximum_filter of size $(\text{L}, \text{W}, \text{W})$ (Length, Window, Window) on the apo_check_array.
    • Find all coordinates $(\text{t}, \text{y}, \text{x})$ where the filtered volume is $\mathbf{0}$. These are the guaranteed valid centers concerning spatial boundaries and static APO-blocking.
    • Store the result as a list of candidate center tuples.
  2. Sampling:

    • Replace the inefficient while guessing loop with a direct random.sample() from the pre-calculated list of valid candidate centers.

File Location: img_proc/crop.py, method _crop_random(self, ...)


Metadata

Metadata

Assignees

No one assigned

    Labels

    OptimizationLow Priority, only for optimization of already working code

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions