Skip to content

Commit

Permalink
Merge 0d3df5f into e76fa0f
Browse files Browse the repository at this point in the history
  • Loading branch information
mdickinson committed Jul 17, 2014
2 parents e76fa0f + 0d3df5f commit 84ca45c
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 15 deletions.
55 changes: 40 additions & 15 deletions mayavi/filters/threshold.py
Expand Up @@ -178,22 +178,47 @@ def _update_ranges(self):
"""Updates the ranges of the input.
"""
data_range = self._get_data_range()
if len(data_range) > 0:
dr = data_range
if self._first:
self._data_min, self._data_max = dr
self.set(lower_threshold = dr[0], trait_change_notify=False)
if len(data_range) == 0:
return

dr = data_range
if self._first:
self._data_min, self._data_max = dr
self.set(lower_threshold = dr[0], trait_change_notify=False)
self.upper_threshold = dr[1]
self._first = False
return

# Decide whether to change 'lower' or 'upper' first, to avoid
# ending up with inconsistent bounds (max < min) in the lower_threshold
# and upper_threshold Range traits.
if dr[0] <= self._data_min:
# Safe to change lower bound first: intermediate range is [dr[0],
# self._data_max], and dr[0] <= self._data_min <= self._data_max.
change_lower_first = True
else:
# Safe to change upper bound first: intermediate range is [self._data_min, dr[1]],
# and self._data_min < dr[0] <= dr[1].
change_lower_first = False

if change_lower_first:
if self.auto_reset_lower:
self._data_min = dr[0]
notify = not self.auto_reset_upper
self.set(lower_threshold = dr[0],
trait_change_notify=notify)
if self.auto_reset_upper:
self._data_max = dr[1]
self.upper_threshold = dr[1]
self._first = False
else:
if self.auto_reset_lower:
self._data_min = dr[0]
notify = not self.auto_reset_upper
self.set(lower_threshold = dr[0],
trait_change_notify=notify)
if self.auto_reset_upper:
self._data_max = dr[1]
self.upper_threshold = dr[1]
else:
if self.auto_reset_upper:
self._data_max = dr[1]
notify = not self.auto_reset_lower
self.set(upper_threshold = dr[1],
trait_change_notify=notify)
if self.auto_reset_lower:
self._data_min = dr[0]
self.lower_threshold = dr[0]

def _get_data_range(self):
"""Returns the range of the input scalar data."""
Expand Down
34 changes: 34 additions & 0 deletions mayavi/tests/test_threshold_filter.py
Expand Up @@ -72,6 +72,40 @@ def test_threshold_filter_threhsold(self):
))
return

def test_threshold_filter_data_range_changes(self):
# Regression test for GitHub issue #136.
src = self.make_src()
self.e.add_source(src)
threshold = Threshold()
self.e.add_filter(threshold)

# Move from one data range to another non-overlapping range,
# first downwards, then back up.
src.scalar_data = np.linspace(3.0, 5.0, 27).reshape((3, 3, 3))
self.assertAlmostEqual(threshold.lower_threshold, 3.0)
self.assertAlmostEqual(threshold.upper_threshold, 5.0)
src.scalar_data = np.linspace(-5.0, -3.0, 27).reshape((3, 3, 3))
self.assertAlmostEqual(threshold.lower_threshold, -5.0)
self.assertAlmostEqual(threshold.upper_threshold, -3.0)
src.scalar_data = np.linspace(3.0, 5.0, 27).reshape((3, 3, 3))
self.assertAlmostEqual(threshold.lower_threshold, 3.0)
self.assertAlmostEqual(threshold.upper_threshold, 5.0)

# Narrow and widen.
src.scalar_data = np.linspace(4.2, 4.6, 27).reshape((3, 3, 3))
self.assertAlmostEqual(threshold.lower_threshold, 4.2)
self.assertAlmostEqual(threshold.upper_threshold, 4.6)
src.scalar_data = np.linspace(-20.0, 20.0, 27).reshape((3, 3, 3))
self.assertAlmostEqual(threshold.lower_threshold, -20.0)
self.assertAlmostEqual(threshold.upper_threshold, 20.0)

# Shift to a range overlapping the previous one.
src.scalar_data = np.linspace(-10.0, -30.0, 27).reshape((3, 3, 3))
self.assertAlmostEqual(threshold.lower_threshold, -30.0)
self.assertAlmostEqual(threshold.upper_threshold, -10.0)
src.scalar_data = np.linspace(-20.0, 20.0, 27).reshape((3, 3, 3))
self.assertAlmostEqual(threshold.lower_threshold, -20.0)
self.assertAlmostEqual(threshold.upper_threshold, 20.0)


if __name__ == '__main__':
Expand Down

0 comments on commit 84ca45c

Please sign in to comment.