Skip to content

Commit

Permalink
Merge pull request #387 from European-XFEL/fix/guiInspectComplex
Browse files Browse the repository at this point in the history
fix: gui crashes when inspecting complex arrays
  • Loading branch information
tmichela authored Feb 7, 2025
2 parents 88824dd + 8ed030f commit 245ff00
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
22 changes: 16 additions & 6 deletions damnit/gui/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,12 @@ def inspect_data(self, index):

if data.ndim == 1:
if isinstance(data, xr.DataArray):
canvas = Xarray1DPlotWindow(self, data, title=title)
try:
canvas = Xarray1DPlotWindow(self, data, title=title)
except Exception as exc:
QMessageBox.warning(
self, f"Can't inspect variable {quantity}", str(exc))
return
else:
canvas = ScatterPlotWindow(self,
x=[np.arange(len(data))],
Expand All @@ -701,11 +706,16 @@ def inspect_data(self, index):
title=title,
)
elif data.ndim == 2 or (data.ndim == 3 and data.shape[-1] in (3, 4)):
canvas = ImagePlotWindow(
self,
image=data,
title=f"{variable.title} (run {run})",
)
try:
canvas = ImagePlotWindow(
self,
image=data,
title=f"{variable.title} (run {run})",
)
except Exception as exc:
QMessageBox.warning(
self, f"Can't inspect variable {quantity}", str(exc))
return
elif data.ndim == 0:
# If this is a scalar value, then we can't plot it
QMessageBox.warning(self, "Can't inspect variable",
Expand Down
23 changes: 23 additions & 0 deletions tests/test_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,18 @@ def color_image(run):
@Variable(title='2D data with summary', summary='mean')
def mean_2d(run):
return np.random.rand(512, 512)
@Variable(title="2D Complex", summary='max')
def complex_2d(run):
return np.array([
[1+1j, 2+2j, 3+3j, 4+4j],
[1+1j, 2+2j, 3+3j, 4+4j],
[1+1j, 2+2j, 3+3j, 4+4j]])
@Variable(title="1D Xarray Complex", summary='max')
def complex_xr_1d(run):
import xarray as xr
return xr.DataArray(np.array([1+1j, 2+2j, 3+3j, 4+4j]))
"""
ctx_code = mock_ctx.code + "\n\n" + textwrap.dedent(const_array_code)
(db_dir / "context.py").write_text(ctx_code)
Expand Down Expand Up @@ -789,6 +801,17 @@ def get_index(title, row=0):
win.inspect_data(mean_2d_index)
warning.assert_not_called()

# xarray of complex data is not inspectable
complex_2d = get_index('2D Complex')
with patch.object(QMessageBox, "warning") as warning:
win.inspect_data(complex_2d)
warning.assert_called_once()

complex_xr_1d = get_index('1D Xarray Complex')
with patch.object(QMessageBox, "warning") as warning:
win.inspect_data(complex_xr_1d)
warning.assert_called_once()


def test_open_dialog(mock_db, qtbot):
db_dir, db = mock_db
Expand Down

0 comments on commit 245ff00

Please sign in to comment.