Skip to content

Commit

Permalink
fix(interactive.imagetool): fix code generation behaviour for non-uni…
Browse files Browse the repository at this point in the history
…form coordinates
  • Loading branch information
kmnhan committed Jun 17, 2024
1 parent e7e8213 commit 3652a21
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
12 changes: 9 additions & 3 deletions src/erlab/interactive/imagetool/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,8 +527,6 @@ def __init__(
if self.bench:
print("\n")

self.flush_history()

@property
def colormap_properties(self) -> ColorMapState:
prop = copy.deepcopy(self._colormap_properties)
Expand Down Expand Up @@ -799,21 +797,25 @@ def refresh_current(self, axes: tuple[int, ...] | None = None):
def refresh(self, cursor: int, axes: tuple[int, ...] | None = None):
self.sigIndexChanged.emit(cursor, axes)

@record_history
def view_all(self):
for ax in self.axes:
ax.vb.enableAutoRange()
ax.vb.updateAutoRange()

@link_slicer
@record_history
def center_all_cursors(self):
for i in range(self.n_cursors):
self.array_slicer.center_cursor(i)

@link_slicer
@record_history
def center_cursor(self):
self.array_slicer.center_cursor(self.current_cursor)

@link_slicer
@record_history
def set_current_cursor(self, cursor: int, update: bool = True):
cursor = cursor % self.n_cursors
if cursor > self.n_cursors - 1:
Expand Down Expand Up @@ -893,6 +895,7 @@ def set_data(
self.set_colormap(update=True)
self._colorbar.cb.setImageItem()
self.lock_levels(False)
self.flush_history()

def update_values(self, values: npt.NDArray | xr.DataArray, update: bool = True):
"""Update only the values of the data.
Expand Down Expand Up @@ -1283,6 +1286,8 @@ def changeEvent(self, evt: QtCore.QEvent | None):


class ItoolCursorLine(pg.InfiniteLine):
sigDragStarted = QtCore.Signal(object)

def __init__(self, *args, **kargs):
super().__init__(*args, **kargs)

Expand Down Expand Up @@ -1314,7 +1319,7 @@ def mouseDragEvent(self, ev: mouseEvents.MouseDragEvent):
ev.buttonDownPos()
)
self.startPosition = self.pos()
self.slicer_area.sigWriteHistory.emit()
self.sigDragStarted.emit(self)
ev.accept()

if not self.moving:
Expand Down Expand Up @@ -1720,6 +1725,7 @@ def add_cursor(self, update: bool = True):
lambda v, *, line=c, axis=ax: self.line_drag(line, v.temp_value, axis)
)
c.sigClicked.connect(lambda *, line=c: self.line_click(line))
c.sigDragStarted.connect(lambda: self.slicer_area.sigWriteHistory.emit())

if update:
self.refresh_cursor(new_cursor)
Expand Down
10 changes: 7 additions & 3 deletions src/erlab/interactive/imagetool/slicer.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,9 +609,11 @@ def index_of_value(
def isel_args(
self, cursor: int, disp: Sequence[int], int_if_one: bool = False
) -> dict[str, slice | int]:
axis = sorted(set(range(self._obj.ndim)) - set(disp))
axis: list[int] = sorted(set(range(self._obj.ndim)) - set(disp))
return {
str(self._obj.dims[ax]): self._bin_slice(cursor, ax, int_if_one)
str(self._obj.dims[ax]).rstrip("_idx")
if ax in self._nonuniform_axes
else str(self._obj.dims[ax]): self._bin_slice(cursor, ax, int_if_one)
for ax in axis
}

Expand Down Expand Up @@ -647,7 +649,9 @@ def qsel_args(self, cursor: int, disp: Sequence[int]) -> dict:
return out

def qsel_code(self, cursor: int, disp: Sequence[int]) -> str:
if self._nonuniform_axes:
if any(
a in self._nonuniform_axes for a in set(range(self._obj.ndim)) - set(disp)
):
# Has non-uniform axes, fallback to isel
return self.isel_code(cursor, disp)

Expand Down
3 changes: 3 additions & 0 deletions tests/interactive/test_imagetool.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ def test_itool(qtbot):
win.array_slicer.set_bin(0, 1, 2, update=True)
move_and_compare_values(qtbot, win, [9.0, 8.0, 3.0, 4.0])

# Test code generation
assert win.array_slicer.qsel_code(0, (0,)) == ".qsel(x=1.5, x_width=2.0)"

# Set colormap and gamma
win.slicer_area.set_colormap(
"ColdWarm", gamma=1.5, reversed=True, high_contrast=True, zero_centered=True
Expand Down

0 comments on commit 3652a21

Please sign in to comment.