Skip to content

Commit

Permalink
Testing: Tests for horizontal and vertical plotting
Browse files Browse the repository at this point in the history
  • Loading branch information
CSSFrancis committed May 5, 2023
1 parent 656f417 commit 904ddf3
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions hyperspy/drawing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ def create_figure(window_title=None,
if "autoscale" in kwargs:
raise ValueError

Check warning on line 165 in hyperspy/drawing/utils.py

View check run for this annotation

Codecov / codecov/patch

hyperspy/drawing/utils.py#L165

Added line #L165 was not covered by tests
if widget:
from IPython.display import display

Check warning on line 167 in hyperspy/drawing/utils.py

View check run for this annotation

Codecov / codecov/patch

hyperspy/drawing/utils.py#L167

Added line #L167 was not covered by tests
with widget:
fig = plt.figure(**kwargs)
display(fig.canvas)

Check warning on line 170 in hyperspy/drawing/utils.py

View check run for this annotation

Codecov / codecov/patch

hyperspy/drawing/utils.py#L169-L170

Added lines #L169 - L170 were not covered by tests
Expand Down
75 changes: 75 additions & 0 deletions hyperspy/tests/drawing/test_horizontal_plotting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Copyright 2007-2022 The HyperSpy developers
#
# This file is part of HyperSpy.
#
# HyperSpy is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# HyperSpy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with HyperSpy. If not, see <https://www.gnu.org/licenses/#GPL>.
import pytest

import hyperspy.api as hs
import numpy as np
import matplotlib
from contextlib import redirect_stdout
import io
ipympl = pytest.importorskip("ipympl")
ipywidgets = pytest.importorskip("ipywidgets")
from ipywidgets import Output

class TestIPYMPL:
def test_horizontal(self):
matplotlib.use("module://ipympl.backend_nbagg")
f = io.StringIO()
with redirect_stdout(f):
s = hs.signals.Signal2D(np.random.random((4, 4, 2, 2)))
s.plot(plot_style="horizontal")

assert("HBox(children=(Output(layout=Layout(margin='auto 0px auto 0px')),"
" Output(layout=Layout(margin='auto 0px auto 0px')" in f.getvalue())

def test_vertical(self):
matplotlib.use("module://ipympl.backend_nbagg")
f = io.StringIO()
with redirect_stdout(f):
s = hs.signals.Signal2D(np.random.random((4, 4, 2, 2)))
s.plot(plot_style="vertical")

assert("VBox(children=(Output(layout=Layout(margin='0px auto 0px auto')),"
" Output(layout=Layout(margin='0px auto 0px auto')" in f.getvalue())

def test_one_output(self):
matplotlib.use("module://ipympl.backend_nbagg")
f = io.StringIO()
with redirect_stdout(f):
o = Output()
s = hs.signals.Signal2D(np.random.random((4, 4, 2, 2)))
s.plot(widget=o)
assert("Output()" in f.getvalue())

with redirect_stdout(f):
o = Output()
s = hs.signals.Signal2D(np.random.random((4, 4, 2, 2)))
s.plot(navigator_kwds={"widget":o})
assert("Output()" in f.getvalue())


def test_two_outputs(self):
matplotlib.use("module://ipympl.backend_nbagg")
f = io.StringIO()
with redirect_stdout(f):
o = Output()
o2 = Output()
s = hs.signals.Signal2D(np.random.random((4, 4, 2, 2)))
s.plot(widget=o, navigator_kwds={"widget":o2})
assert("Output" not in f.getvalue())


0 comments on commit 904ddf3

Please sign in to comment.