Skip to content

Commit

Permalink
refactor(interactive.imagetool): preparation for saving and loading s…
Browse files Browse the repository at this point in the history
…tate
  • Loading branch information
kmnhan committed Jun 16, 2024
1 parent eb0cd2f commit eca8262
Show file tree
Hide file tree
Showing 8 changed files with 304 additions and 124 deletions.
24 changes: 15 additions & 9 deletions src/erlab/interactive/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,16 +259,16 @@ def set_colormap(
cmap: pg.ColorMap | str,
gamma: float,
reverse: bool = False,
highContrast: bool = False,
zeroCentered: bool = False,
high_contrast: bool = False,
zero_centered: bool = False,
update: bool = True,
):
cmap = pg_colormap_powernorm(
cmap,
gamma,
reverse,
highContrast=highContrast,
zeroCentered=zeroCentered,
high_contrast=high_contrast,
zero_centered=zero_centered,
)
self.set_pg_colormap(cmap, update=update)

Expand Down Expand Up @@ -377,13 +377,19 @@ def level_change(self):
if not self.isVisible():
return
for img_ref in self.images:
img_ref().setLevels(self._span.getRegion())
img_ref().setLevels(self.spanRegion())

Check warning on line 380 in src/erlab/interactive/colors.py

View check run for this annotation

Codecov / codecov/patch

src/erlab/interactive/colors.py#L380

Added line #L380 was not covered by tests
self.limit_changed()

@QtCore.Slot()
def level_change_fin(self):
pass

def spanRegion(self) -> tuple[float, float]:
return self._span.getRegion()

def setSpanRegion(self, levels: tuple[float, float]):
self._span.setRegion(levels)

Check warning on line 391 in src/erlab/interactive/colors.py

View check run for this annotation

Codecov / codecov/patch

src/erlab/interactive/colors.py#L391

Added line #L391 was not covered by tests

def setLimits(self, limits: tuple[float, float] | None):
self._fixedlimits = limits
if self._primary_image is not None:
Expand Down Expand Up @@ -625,8 +631,8 @@ def pg_colormap_powernorm(
cmap: str | pg.ColorMap,
gamma: float,
reverse: bool = False,
highContrast: bool = False,
zeroCentered: bool = False,
high_contrast: bool = False,
zero_centered: bool = False,
N: int = 65536,
) -> pg.ColorMap:
if isinstance(cmap, str):
Expand All @@ -637,7 +643,7 @@ def pg_colormap_powernorm(
def mapping_fn(x):
return x

elif highContrast:
elif high_contrast:

def mapping_fn(x):
return 1 - np.power(np.flip(x), 1.0 / gamma)
Expand All @@ -648,7 +654,7 @@ def mapping_fn(x):
return np.power(x, gamma)

x = np.linspace(0, 1, N)
if zeroCentered:
if zero_centered:
mapping = np.piecewise(
x,
[x < 0.5, x >= 0.5],
Expand Down
14 changes: 8 additions & 6 deletions src/erlab/interactive/imagetool/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,18 +421,20 @@ def refreshMenus(self):

cmap_props = self.slicer_area.colormap_properties
for ca, k in zip(
self.colorAct, ["reversed", "highContrast", "zeroCentered"], strict=True
self.colorAct, ["reversed", "high_contrast", "zero_centered"], strict=True
):
k = cast(Literal["reversed", "highContrast", "zeroCentered"], k) # for mypy
k = cast(
Literal["reversed", "high_contrast", "zero_centered"], k
) # for mypy
ca.blockSignals(True)
ca.setChecked(cmap_props[k])
ca.blockSignals(False)

def _set_colormap_options(self):
self.slicer_area.set_colormap(
reversed=self.colorAct[0].isChecked(),
highContrast=self.colorAct[1].isChecked(),
zeroCentered=self.colorAct[2].isChecked(),
high_contrast=self.colorAct[1].isChecked(),
zero_centered=self.colorAct[2].isChecked(),
)

def _copy_cursor_val(self):
Expand Down Expand Up @@ -472,12 +474,12 @@ def _export_file(self):
dialog.setAcceptMode(QtWidgets.QFileDialog.AcceptMode.AcceptSave)
dialog.setFileMode(QtWidgets.QFileDialog.FileMode.AnyFile)

valid_savers: dict[str, tuple[Callable, dict]] = {
valid_savers: dict[str, tuple[Callable, dict[str, Any]]] = {

Check warning on line 477 in src/erlab/interactive/imagetool/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/erlab/interactive/imagetool/__init__.py#L477

Added line #L477 was not covered by tests
"xarray HDF5 Files (*.h5)": (erlab.io.save_as_hdf5, {}),
"NetCDF Files (*.nc *.nc4 *.cdf)": (erlab.io.save_as_netcdf, {}),
}
dialog.setNameFilters(valid_savers.keys())
dialog.setDirectory(f"{self.slicer_area._data.name}.h5")
dialog.setDirectory(f"{self.slicer_area.data.name}.h5")

Check warning on line 482 in src/erlab/interactive/imagetool/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/erlab/interactive/imagetool/__init__.py#L482

Added line #L482 was not covered by tests
# dialog.setOption(QtWidgets.QFileDialog.Option.DontUseNativeDialog)
if dialog.exec():
files = dialog.selectedFiles()
Expand Down
18 changes: 9 additions & 9 deletions src/erlab/interactive/imagetool/_deprecated/imagetool_old.py
Original file line number Diff line number Diff line change
Expand Up @@ -1605,25 +1605,25 @@ def set_cmap(
cmap=None,
gamma=None,
reverse=None,
highContrast=False,
zeroCentered=None,
high_contrast=False,
zero_centered=None,
):
if cmap is not None:
self.cmap = cmap
if gamma is not None:
self.gamma = gamma
if reverse is not None:
self.reverse = reverse
if zeroCentered is None:
zeroCentered = self.zero_centered
if zero_centered is None:
zero_centered = self.zero_centered
else:
self.zero_centered = zeroCentered
self.zero_centered = zero_centered
self.norm_cmap = pg_colormap_powernorm(
self.cmap,
self.gamma,
reverse=self.reverse,
highContrast=highContrast,
zeroCentered=zeroCentered,
high_contrast=high_contrast,
zero_centered=zero_centered,
)
for im in self.maps:
im._colorMap = self.norm_cmap
Expand Down Expand Up @@ -2920,7 +2920,7 @@ def initialize_widgets(self):
toolTip="Center colormap at zero",
)
self._zero_center_button.toggled.connect(
lambda z: self.itool.set_cmap(zeroCentered=z)
lambda z: self.itool.set_cmap(zero_centered=z)
)

axes_names = [
Expand Down Expand Up @@ -2995,7 +2995,7 @@ def set_cmap(self, name=None):
else:
cmap = self._cmap_combo.currentText()
mode = self._cmap_mode_button.isChecked()
self.itool.set_cmap(cmap, gamma=gamma, reverse=reverse, highContrast=mode)
self.itool.set_cmap(cmap, gamma=gamma, reverse=reverse, high_contrast=mode)

def _color_button_clicked(self, s):
# print("click", s)
Expand Down
15 changes: 7 additions & 8 deletions src/erlab/interactive/imagetool/controls.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,12 +578,11 @@ def update(self):
self.btn_zero.blockSignals(True)
self.btn_lock.blockSignals(True)

self.btn_reverse.setChecked(self.slicer_area.colormap_properties["reversed"])
self.btn_contrast.setChecked(
self.slicer_area.colormap_properties["highContrast"]
)
self.btn_zero.setChecked(self.slicer_area.colormap_properties["zeroCentered"])
self.btn_lock.setChecked(self.slicer_area.color_locked)
props = self.slicer_area.colormap_properties
self.btn_reverse.setChecked(props["reversed"])
self.btn_contrast.setChecked(props["high_contrast"])
self.btn_zero.setChecked(props["zero_centered"])
self.btn_lock.setChecked(props["levels_locked"])

self.btn_reverse.blockSignals(False)
self.btn_contrast.blockSignals(False)
Expand All @@ -593,8 +592,8 @@ def update(self):
def update_colormap(self):
self.slicer_area.set_colormap(
reversed=self.btn_reverse.isChecked(),
highContrast=self.btn_contrast.isChecked(),
zeroCentered=self.btn_zero.isChecked(),
high_contrast=self.btn_contrast.isChecked(),
zero_centered=self.btn_zero.isChecked(),
)

def connect_signals(self):
Expand Down
Loading

0 comments on commit eca8262

Please sign in to comment.