Skip to content

Commit

Permalink
fix: deselecting curves of a qmap resulted in ValueError if QMap tab …
Browse files Browse the repository at this point in the history
…was selected
  • Loading branch information
paulmueller committed Jan 27, 2020
1 parent bf91ff8 commit 477e7f2
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 27 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
- fix: info tab did not display QMap metadata
- fix: do not fit the entire dataset when applying a rating threshold
but take the values from previous ratings
- fix: deselecting curves of a qmap resulted in ValueError if QMap tab
was selected
- fix: set correct display range for residuals
- docs: UI section for basics FD analysis
- build: Windows build broken since 0.5.6
Expand Down
2 changes: 2 additions & 0 deletions docs/scrots/make_scrots_ui_fd.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ def cleanup_autosave(jpkfile):
war.tabs.grab().save("_ui_fd_tab_info.png")

war.tabs.setCurrentIndex(5)
war.sp_rating_thresh.setValue(0)
war.btn_rating_filter.clicked.emit()
QApplication.processEvents()
war.tabs.grab().save("_ui_fd_tab_qmap.png")

Expand Down
17 changes: 10 additions & 7 deletions pyjibe/fd/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def selected_curves(self):
for ar in self.data_set:
idx = self.data_set.index(ar)
item = self.list_curves.topLevelItem(idx)
if item.checkState(3) == 2:
if item.checkState(3) == QtCore.Qt.Checked:
curves.append(ar)
return curves

Expand Down Expand Up @@ -163,7 +163,7 @@ def callback(partial):

def autosave(self, fdist):
"""Performs autosaving for all files"""
if (self.cb_autosave.checkState() == 2 and
if (self.cb_autosave.checkState() == QtCore.Qt.Checked and
fdist.fit_properties and
fdist.fit_properties["success"]):
# Determine the directory of the current curve
Expand All @@ -183,7 +183,7 @@ def autosave(self, fdist):
# fdist was fitted with same model
ar.fit_properties["model_key"] == model_key and
# user selected curve for export ("use")
it.checkState(3) == 2
it.checkState(3) == QtCore.Qt.Checked
):
exp_curv.append(ar)
# The file to export
Expand Down Expand Up @@ -449,14 +449,17 @@ def on_params_init(self):
def on_rating_threshold(self):
"""(De)select curves according to threshold rating"""
thresh = self.sp_rating_thresh.value()
self.list_curves.blockSignals(True)
for ii, fdist in enumerate(self.data_set):
rtd = fdist.get_rating_parameters()
rating = rtd["Rating"]
it = self.list_curves.topLevelItem(ii)
if not np.isnan(rating) and rating >= thresh:
it.setCheckState(3, 2)
else:
it.setCheckState(3, 0)
if not np.isnan(rating):
if rating >= thresh:
it.setCheckState(3, QtCore.Qt.Checked)
else:
it.setCheckState(3, QtCore.Qt.Unhecked)
self.list_curves.blockSignals(False)
# TODO:
# - make this more efficient. There is a lot written to disk here.
for fdist in self.data_set:
Expand Down
3 changes: 3 additions & 0 deletions pyjibe/fd/main.ui
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,9 @@
<property name="decimals">
<number>1</number>
</property>
<property name="minimum">
<double>0.000000000000000</double>
</property>
<property name="maximum">
<double>10.000000000000000</double>
</property>
Expand Down
28 changes: 20 additions & 8 deletions pyjibe/fd/mpl_qmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,15 @@ def set_selection_by_index(self, index):
cx = self.qmap_coords[:, 0]
cy = self.qmap_coords[:, 1]
newxy = (cx[index]-self.dx/2, cy[index]-self.dy/2)
self.select_rect.set_visible(True)
self.select_rect.set_xy(newxy)
self.show_selection(True)

def show_selection(self, b):
self.select_rect.set_visible(b)
if b:
self.select_rect.set_width(self.dx)
self.select_rect.set_height(self.dy)
self.canvas.draw()
self.canvas.draw()

def update(self, qmap, feature, cmap="viridis", vmin=None, vmax=None):
"""Update the map tab plot data
Expand Down Expand Up @@ -221,6 +225,10 @@ def update(self, qmap, feature, cmap="viridis", vmin=None, vmax=None):

self.plot.set_cmap(cmap)

# explicitly set x/y limits
self.axis_main.set_xlim(extent[0], extent[1])
self.axis_main.set_ylim(extent[2], extent[3])

if (prev_data is None
or not np.allclose(qmap_data, prev_data, equal_nan=True)):
# visibility of plot elements
Expand All @@ -238,15 +246,19 @@ def update(self, qmap, feature, cmap="viridis", vmin=None, vmax=None):
self.reset_lines()
xm, ym = np.meshgrid(range(shape[0]),
range(shape[1]))

for xi, yi in zip(xm.flat, ym.flat):
if np.isnan(qmap_data[yi, xi]):
xv = qmap_coords[0][0] + xi * dx
yv = qmap_coords[0][1] + yi * dy
if (np.sum(np.all(np.array([xi, yi]) == p)
for p in qmap_coords_px)):
color = "#14571A"
xv = extent[0] + (xi+.5) * dx
yv = extent[2] + (yi+.5) * dy
for p in qmap_coords_px:
if np.allclose([xi, yi], p):
# data available, but not computed
color = "#14571A" # green
break
else:
color = "#571714"
# curve not available
color = "#571714" # red
self.lines.append(
self.axis_main.plot([xv-dx*.4, xv+dx*.4],
[yv-dy*.4, yv+dy*.4],
Expand Down
45 changes: 33 additions & 12 deletions pyjibe/fd/tab_qmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(self, *args, **kwargs):
# set colormaps
cmaps = ["viridis", "plasma", "afmhot", "seismic"]
for cm in cmaps:
self.qmpa_cmap_cb.addItem(cm)
self.qmpa_cmap_cb.addItem(cm, cm)
self.qmpa_cmap_cb.setCurrentIndex(0)
self.qmap_data_cb.currentIndexChanged.connect(
self.on_qmap_data_changed)
Expand All @@ -44,8 +44,9 @@ def mpl_qmap_update(self):
if self.fd.tabs.currentWidget() == self:
fdist = self.current_curve
# Get all selected curves with the same path
fdist_map = self.fd.selected_curves.subgroup_with_path(fdist.path)
self.update_qmap(fdist_map, fdist_map.index(fdist))
fdist_group = self.fd.selected_curves.subgroup_with_path(
fdist.path)
self.update_qmap(fdist_group, fdist)

def on_qmap_cmap_changed(self):
"""colormap selection changed"""
Expand All @@ -57,7 +58,7 @@ def on_qmap_data_changed(self):
self.qmap_sp_range1.blockSignals(True)
self.qmap_sp_range2.blockSignals(True)
if hasattr(self, "_cache_qmap_spin_ctl"):
data = self.qmap_data_cb.currentIndex()
data = self.qmap_data_cb.currentData()
if data in self._cache_qmap_spin_ctl:
vmin, vmax = self._cache_qmap_spin_ctl[data]
else:
Expand All @@ -73,7 +74,7 @@ def on_qmap_min_max_changed(self):
# store spin control values for data column
vmin = self.qmap_sp_range1.value()
vmax = self.qmap_sp_range2.value()
data = self.qmap_data_cb.currentIndex()
data = self.qmap_data_cb.currentData()
if not hasattr(self, "_cache_qmap_spin_ctl"):
self._cache_qmap_spin_ctl = {}
self._cache_qmap_spin_ctl[data] = (vmin, vmax)
Expand All @@ -92,12 +93,29 @@ def on_qmap_selection(self, idx):
item = fd.list_curves.topLevelItem(idcurve)
fd.list_curves.setCurrentItem(item)

def update_qmap(self, fdist_map, index):
def update_qmap(self, fdist_group, fdist):
"""Update the QMap plotting data
Parameters
----------
fdist_group: nanite.IndentationGroup
Indentation group containing all curves that will be
part of the map
fdist: nanite.Indentation
Indentation curve that is currently selected. If `fdist`
is not in `fdist_group`, then the red selection square
is hidden.
"""
if fdist in fdist_group:
index = fdist_group.index(fdist)
else:
index = None

# Build list of possible selections
selist = nanite.qmap.available_features

# Get plotting parameter and check if it makes sense
feature = self.qmap_data_cb.currentText()
feature = self.qmap_data_cb.currentData()
if not feature or feature not in selist:
# Use a default plotting map
feature = "data min height"
Expand All @@ -113,19 +131,22 @@ def update_qmap(self, fdist_map, index):
self.qmap_data_cb.removeItem(0)
# add new items
for item in selist:
self.qmap_data_cb.addItem(item)
self.qmap_data_cb.addItem(item, item)
self.qmap_data_cb.setCurrentIndex(selist.index(feature))
self.qmap_data_cb.blockSignals(False)

if len(fdist_map) > 1:
if len(fdist_group) > 1:
# Get map data
qmap = nanite.QMap(fdist_map)
qmap = nanite.QMap(fdist_group)
# update plot
self.mpl_qmap.update(qmap=qmap,
feature=feature,
cmap=self.qmpa_cmap_cb.currentText(),
cmap=self.qmpa_cmap_cb.currentData(),
vmin=self.qmap_sp_range1.value(),
vmax=self.qmap_sp_range2.value())
self.mpl_qmap.set_selection_by_index(index)
if index is not None:
self.mpl_qmap.set_selection_by_index(index)
else:
self.mpl_qmap.show_selection(False)
else:
self.mpl_qmap.reset()
Binary file added tests/data/map2x2_extracted.jpk-force-map
Binary file not shown.
42 changes: 42 additions & 0 deletions tests/test_fd_qmap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""Test of data set functionalities"""
import pathlib

from PyQt5 import QtWidgets, QtCore
import pytest


import pyjibe.head


here = pathlib.Path(__file__).parent
jpkfile = here / "data" / "map2x2_extracted.jpk-force-map"


def cleanup_autosave(jpkfile):
"""Remove autosave files"""
path = jpkfile.parent
files = path.glob("*.tsv")
files = [f for f in files if f.name.startswith("pyjibe_")]
[f.unlink() for f in files]


@pytest.fixture(autouse=True)
def run_around_tests():
# Code that will run before your test, for example:
cleanup_autosave(jpkfile)
# A test function will be run at this point
yield
# Code that will run after your test, for example:
cleanup_autosave(jpkfile)


def test_qmap_with_unused_curves(qtbot):
"""Uncheck "use" in the curve list and switch to the qmap tab"""
main_window = pyjibe.head.PyJibe()
main_window.load_data(files=[jpkfile])
war = main_window.subwindows[0].widget()
# uncheck first curve
cl1 = war.list_curves.currentItem()
cl1.setCheckState(3, QtCore.Qt.Unchecked)
war.tabs.setCurrentIndex(5)
QtWidgets.QApplication.processEvents()

0 comments on commit 477e7f2

Please sign in to comment.