Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gselzer committed Nov 20, 2024
1 parent f9909c1 commit 403cf47
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/ndv/histogram/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def data(self) -> np.ndarray:
"""Returns the data backing this StatsModel."""
if self._data is not None:
return self._data
raise Exception("Data has not yet been set!")
raise RuntimeError("Data has not yet been set!")

@data.setter
def data(self, data: np.ndarray) -> None:
Expand Down
14 changes: 7 additions & 7 deletions src/ndv/histogram/views/_vispy.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ def zoom(
super().zoom(factor, center=center)
return

if isinstance(factor, float):
if isinstance(factor, (float, int)):
factor = (factor, factor)
_factor = list(factor)
_factor[self.axis_index] = 1
Expand Down Expand Up @@ -462,7 +462,7 @@ def view(self) -> Any:

def set_visibility(self, visible: bool) -> None:
if self._hist_mesh is None:
return
return # pragma: no cover
self._hist_mesh.visible = visible
self._lut_line.visible = visible
self._gamma_handle.visible = visible
Expand Down Expand Up @@ -536,7 +536,7 @@ def _update_histogram(self) -> None:
https://github.com/vispy/vispy/blob/af847424425d4ce51f144a4d1c75ab4033fe39be/vispy/visuals/histogram.py#L28
"""
if self._values is None or self._bin_edges is None:
return
return # pragma: no cover
values = self._values
if self._log_y:
# Replace zero values with 1 (which will be log10(1) = 0)
Expand All @@ -552,7 +552,7 @@ def _update_histogram(self) -> None:

def _update_lut_lines(self, npoints: int = 256) -> None:
if self._clims is None or self._gamma is None:
return
return # pragma: no cover

# 2 additional points for each of the two vertical clims lines
X = np.empty(npoints + 4)
Expand Down Expand Up @@ -596,7 +596,7 @@ def _update_lut_lines(self, npoints: int = 256) -> None:

def on_mouse_press(self, event: SceneMouseEvent) -> None:
if event.pos is None:

Check warning on line 598 in src/ndv/histogram/views/_vispy.py

View check run for this annotation

Codecov / codecov/patch

src/ndv/histogram/views/_vispy.py#L598

Added line #L598 was not covered by tests
return
return # pragma: no cover
# check whether the user grabbed a node
self._grabbed = self._find_nearby_node(event)
if self._grabbed != Grabbable.NONE:

Check warning on line 602 in src/ndv/histogram/views/_vispy.py

View check run for this annotation

Codecov / codecov/patch

src/ndv/histogram/views/_vispy.py#L601-L602

Added lines #L601 - L602 were not covered by tests
Expand All @@ -610,9 +610,9 @@ def on_mouse_release(self, event: SceneMouseEvent) -> None:
def on_mouse_move(self, event: SceneMouseEvent) -> None:
"""Called whenever mouse moves over canvas."""
if event.pos is None:

Check warning on line 612 in src/ndv/histogram/views/_vispy.py

View check run for this annotation

Codecov / codecov/patch

src/ndv/histogram/views/_vispy.py#L612

Added line #L612 was not covered by tests
return
return # pragma: no cover
if self._clims is None:

Check warning on line 614 in src/ndv/histogram/views/_vispy.py

View check run for this annotation

Codecov / codecov/patch

src/ndv/histogram/views/_vispy.py#L614

Added line #L614 was not covered by tests
return
return # pragma: no cover

if self._grabbed in [Grabbable.LEFT_CLIM, Grabbable.RIGHT_CLIM]:
newlims = list(self._clims)
Expand Down
42 changes: 42 additions & 0 deletions tests/test_stats_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import numpy as np
import pytest

from ndv.histogram.model import StatsModel

EPSILON = 1e-6


@pytest.fixture
def data() -> np.ndarray:
gen = np.random.default_rng(0xDEADBEEF)
# Average - 1.000104
# Std. Dev. - 10.003385
data = gen.normal(1, 10, (1000, 1000))
return data


def test_empty_stats_model() -> None:
model = StatsModel()
with pytest.raises(RuntimeError):
_ = model.data
assert model.average is None
assert model.standard_deviation is None
assert model.histogram is None
assert model.bins == 256


def test_stats_model(data: np.ndarray) -> None:
model = StatsModel()
model.data = data
assert np.all(model.data == data)
# Basic regression tests
assert abs(model.average - 1.000104) < 1e-6
assert abs(model.standard_deviation - 10.003385) < 1e-6
assert 256 == model.bins
values, edges = model.histogram
assert len(values) == 256
assert np.all(values >= 0)
assert np.all(values <= data.size)
assert len(edges) == 257
assert edges[0] == np.min(data)
assert edges[256] == np.max(data)
33 changes: 33 additions & 0 deletions tests/test_vispy_histogram_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,39 @@ def view(qtbot: QtBot, data: np.ndarray) -> VispyHistogramView:
return view


def test_plot(view: VispyHistogramView) -> None:
plot = view.plot

assert plot.title == ""
plot.title = "foo"
assert plot._title.text == "foo"

assert plot.xlabel == ""
plot.xlabel = "bar"
assert plot._xlabel.text == "bar"

assert plot.ylabel == ""
plot.ylabel = "baz"
assert plot._ylabel.text == "baz"

# Test axis lock - pan
_domain = plot.xaxis.axis.domain
_range = plot.yaxis.axis.domain
plot.camera.pan([20, 20])
assert np.all(np.isclose(_domain, [x - 20 for x in plot.xaxis.axis.domain]))
assert np.all(np.isclose(_range, plot.yaxis.axis.domain))

# Test axis lock - zoom
_domain = plot.xaxis.axis.domain
_range = plot.yaxis.axis.domain
plot.camera.zoom(0.5)
dx = (_domain[1] - _domain[0]) / 4
assert np.all(
np.isclose([_domain[0] + dx, _domain[1] - dx], plot.xaxis.axis.domain)
)
assert np.all(np.isclose(_range, plot.yaxis.axis.domain))


def test_clims(data: np.ndarray, view: VispyHistogramView) -> None:
# on startup, clims should be at the extent of the data
clims = np.min(data), np.max(data)
Expand Down

0 comments on commit 403cf47

Please sign in to comment.