Skip to content

fix nd2 reading on drop event#1406

Open
tlambert03 wants to merge 2 commits intoMouseLand:mainfrom
tlambert03:fix-nd2
Open

fix nd2 reading on drop event#1406
tlambert03 wants to merge 2 commits intoMouseLand:mainfrom
tlambert03:fix-nd2

Conversation

@tlambert03
Copy link

@tlambert03 tlambert03 commented Feb 14, 2026

In using the gui, I tried to drag an nd2 file and got this error:

ERROR: images not compatible
ERROR: 
Traceback (most recent call last):
  File "/Users/talley/dev/self/cellpose/cellpose/gui/gui.py", line 896, in dropEvent
    io._load_image(self, filename=files[0], load_seg=True, load_3D=self.load_3D)
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/talley/dev/self/cellpose/cellpose/gui/io.py", line 154, in _load_image
    if len(np.unique(image[..., 1:])) == 1:
                     ^^^^^
UnboundLocalError: cannot access local variable 'image' where it is not associated with a value

that error is primarily due to exceptions simply being printed, but not stopping the function here:

cellpose/cellpose/gui/io.py

Lines 132 to 141 in 4fbdc7c

try:
print(f"GUI_INFO: loading image: {filename}")
if not load_3D:
image = imread_2D(filename)
else:
image = imread_3D(filename)
parent.loaded = True
except Exception as e:
print("ERROR: images not compatible")
print(f"ERROR: {e}")

but the underlying cause was that nd2 files aren't hooked up in imread

cellpose/cellpose/io.py

Lines 188 to 191 in 4fbdc7c

elif ext == ".nd2":
if not ND2:
io_logger.critical("ERROR: need to 'pip install nd2' to load in .nd2 file")
return None

so, even if nd2 is available, the function returns None. This fixes both.

Note: the intended semantics for the return value of imread wasn't entirely clear to me (i.e. what expectations are made about the dimensionality and shape of the value returned by imread?) ... so I took a best guess from the other code. Let me know if i should be returning something different

@tlambert03 tlambert03 changed the title fix nd2 fix nd2 reading on drop event Feb 14, 2026
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.

1 participant