Skip to content

Commit

Permalink
[fix #2] Avoid crash while ploting a variable whose min and max are e…
Browse files Browse the repository at this point in the history
…qual
  • Loading branch information
lucduron committed Oct 26, 2017
1 parent 65e3cb8 commit a9a6f01
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions PyTelTools/workflow/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
from slf import Serafin


EPS_VALUE = 0.001 # value to add to or substrate from min or max values for color maps


logger = logging.getLogger(__name__)
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(message)s'))
Expand Down Expand Up @@ -1083,6 +1086,9 @@ def change_color_range(self):
except ValueError:
QMessageBox.critical(self, 'Error', 'Invalid input.', QMessageBox.Ok)
return
if cmax <= cmin:
QMessageBox.critical(self, 'Error', 'Values are not increasing.', QMessageBox.Ok)
return

self.color_limits = (cmin, cmax)
self.replot(False)
Expand Down Expand Up @@ -1129,7 +1135,11 @@ def replot(self, compute=True):
self.canvas.axes.tricontourf(self.triang, self.values, cmap=self.current_style, levels=levels,
extend='both', vmin=self.color_limits[0], vmax=self.color_limits[1])
else:
levels = np.linspace(np.nanmin(self.values), np.nanmax(self.values), NB_COLOR_LEVELS)
min_value, max_value = np.nanmin(self.values), np.nanmax(self.values)
if min_value == max_value:

This comment has been minimized.

Copy link
@jamaa

jamaa Nov 2, 2017

this only works if min_value and max_value are equal to 0, but not with other float numbers. A better condition would be:
if abs(max_value - min_value) < EPS_VALUE:

min_value -= EPS_VALUE
max_value += EPS_VALUE
levels = np.linspace(min_value, max_value, NB_COLOR_LEVELS)
self.canvas.axes.tricontourf(self.triang, self.values, cmap=self.current_style, levels=levels,
extend='both')

Expand Down Expand Up @@ -1280,6 +1290,9 @@ def change_color_range(self):
except ValueError:
QMessageBox.critical(self, 'Error', 'Invalid input.', QMessageBox.Ok)
return
if cmax <= cmin:
QMessageBox.critical(self, 'Error', 'Values are not increasing.', QMessageBox.Ok)
return

self.color_limits = (cmin, cmax)
self.replot(False)
Expand Down Expand Up @@ -1319,7 +1332,11 @@ def replot(self, compute=True):
self.canvas.axes.tricontourf(triang, self.z, cmap=self.current_style, levels=levels, extend='both',
vmin=self.color_limits[0], vmax=self.color_limits[1])
else:
levels = np.linspace(np.nanmin(self.z), np.nanmax(self.z), NB_COLOR_LEVELS)
min_value, max_value = np.nanmin(self.z), np.nanmax(self.z)
if min_value == max_value:
min_value -= EPS_VALUE
max_value += EPS_VALUE
levels = np.linspace(min_value, max_value, NB_COLOR_LEVELS)
self.canvas.axes.tricontourf(triang, self.z, cmap=self.current_style, levels=levels, extend='both')

divider = make_axes_locatable(self.canvas.axes)
Expand Down Expand Up @@ -2202,7 +2219,11 @@ def replot(self, mesh, values, color_style, limits, variable_label):
self.axes.tricontourf(triang, values, cmap=color_style, levels=levels, extend='both',
vmin=limits[0], vmax=limits[1])
else:
levels = np.linspace(np.nanmin(values), np.nanmax(values), NB_COLOR_LEVELS)
min_value, max_value = np.nanmin(values), np.nanmax(values)
if min_value == max_value:
min_value -= EPS_VALUE
max_value += EPS_VALUE
levels = np.linspace(min_value, max_value, NB_COLOR_LEVELS)
self.axes.tricontourf(triang, values, cmap=color_style, levels=levels, extend='both')

# add colorbar
Expand Down Expand Up @@ -2314,6 +2335,9 @@ def change_color_range(self):
except ValueError:
QMessageBox.critical(self, 'Error', 'Invalid input.', QMessageBox.Ok)
return
if cmax <= cmin:
QMessageBox.critical(self, 'Error', 'Values are not increasing.', QMessageBox.Ok)
return

self.color_limits = (cmin, cmax)
self.replot(False)
Expand Down

0 comments on commit a9a6f01

Please sign in to comment.