Skip to content

KeyError when extracting ROIs from images with blob detection results #276

@PierreRaybaut

Description

@PierreRaybaut

KeyError when extracting ROIs from images with blob detection results

Bug Description

When trying to extract ROIs ("ROI" menu > "Extract…") from an image that has ROIs generated by a blob detection function (e.g., OpenCV blob detection), a KeyError is raised in the plothandler.

Steps to Reproduce

  1. Open DataLab
  2. Add an image
  3. Run blob detection (e.g., "Analysis" > "Blob detection (OpenCV)") with "Create regions of interest" enabled
  4. Verify that ROIs are created around detected blobs
  5. Try to extract ROIs via "ROI" menu > "Extract…"
  6. Result: KeyError is raised instead of opening the ROI extraction dialog

Error Traceback

Traceback (most recent call last):
  File "C:\Dev\Projets\DataLab\datalab\utils\qthelpers.py", line 373, in method_wrapper
    output = func(*args, **kwargs)
  File "C:\Dev\Projets\DataLab\datalab\gui\processor\base.py", line 2300, in compute_roi_extraction
    roi = self.edit_roi_graphically(mode="extract")
  File "C:\Dev\Projets\DataLab\datalab\gui\processor\base.py", line 2346, in edit_roi_graphically
    results = self.panel.get_roi_editor_output(mode=mode)
  File "C:\Dev\Projets\DataLab\datalab\gui\panel\base.py", line 2925, in get_roi_editor_output
    update_from=self.plothandler[get_uuid(obj)]
  File "C:\Dev\Projets\DataLab\datalab\gui\plothandler.py", line 114, in __getitem__
    raise exc
  File "C:\Dev\Projets\DataLab\datalab\gui\plothandler.py", line 105, in __getitem__
    return self.__plotitems[oid]
KeyError: 'eca389d9-c8e2-4bd3-a4eb-e4dc80e96818'

Root Cause Analysis

The issue occurs in get_roi_editor_output() method in datalab/gui/panel/base.py. The code attempts to access a plot item from the plothandler using direct dictionary access (self.plothandler[get_uuid(obj)]), which raises a KeyError if the plot item doesn't exist.

This can happen in several scenarios:

  1. When "auto refresh" is disabled and the plot item hasn't been created yet
  2. When "show first only" mode is enabled and only the first object has a plot item
  3. After certain operations that clear or invalidate the plot item cache

Solution

The fix is to use the get() method instead of direct dictionary access, which returns None if the key doesn't exist. The make_item() method already handles update_from=None gracefully.

Changed code in datalab/gui/panel/base.py:

# Before (raises KeyError):
item = create_adapter_from_object(obj).make_item(
    update_from=self.plothandler[get_uuid(obj)]
)

# After (handles missing item gracefully):
existing_item = self.plothandler.get(get_uuid(obj))
item = create_adapter_from_object(obj).make_item(update_from=existing_item)

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions