Repository navigation
Expand file tree
/
Copy pathtest_plotting.py
More file actions
179 lines (146 loc) · 5.96 KB
/
Copy pathtest_plotting.py
File metadata and controls
179 lines (146 loc) · 5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import os
import re
import csv
import pytest
import pickle
from unittest.mock import patch
from unittest.mock import MagicMock
import numpy as np
import matplotlib.pyplot as plt
from RAT.rat_core import PlotEventData
from RAT.utils.plotting import Figure, plot_ref_sld
TEST_DIR_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)),
'test_data')
def data() -> PlotEventData:
"""
Creates the fixture for the tests.
"""
data_path = os.path.join(TEST_DIR_PATH, 'plotting_data.pickle')
with open(data_path, 'rb') as f:
loaded_data = pickle.load(f)
data = PlotEventData()
data.modelType = loaded_data['modelType']
data.dataPresent = loaded_data['dataPresent']
data.subRoughs = loaded_data['subRoughs']
data.resample = loaded_data['resample']
data.resampledLayers = loaded_data['resampledLayers']
data.reflectivity = loaded_data['reflectivity']
data.shiftedData = loaded_data['shiftedData']
data.sldProfiles = loaded_data['sldProfiles']
return data
@pytest.fixture
def fig() -> Figure:
"""
Creates the fixture for the tests.
"""
plt.close('all')
figure = Figure(1, 3)
fig = plot_ref_sld(fig=figure, data=data())
return fig
def test_figure_axis_formating(fig: Figure) -> None:
"""
Tests the axis formating of the figure.
"""
ref_plot = fig._ax[0]
sld_plot = fig._ax[1]
assert fig._fig.axes[0].get_subplotspec().get_gridspec().get_geometry() == (1, 2)
assert fig._ax.shape == (2,)
assert ref_plot.get_xlabel() == "Qz"
assert ref_plot.get_xscale() == "log"
assert ref_plot.get_ylabel() == "Ref"
assert ref_plot.get_yscale() == "log"
assert [label._text for label in ref_plot.get_legend().texts] == ['ref 1', 'ref 2', 'ref 3']
assert sld_plot.get_xlabel() == "Z"
assert sld_plot.get_xscale() == "linear"
assert sld_plot.get_ylabel() == "SLD"
assert sld_plot.get_yscale() == "linear"
assert [label._text for label in sld_plot.get_legend().texts] == ['sld 1', 'sld 2', 'sld 3']
def test_figure_color_formating(fig: Figure) -> None:
"""
Tests the color formating of the figure.
"""
ref_plot = fig._ax[0]
sld_plot = fig._ax[1]
assert len(ref_plot.get_lines()) == 3
assert len(sld_plot.get_lines()) == 6
for axis_ix in range(len(ref_plot.get_lines())):
ax1 = axis_ix*2
ax2 = ax1 + 1
# Tests whether the color of the line and the errorbars match on the ref_plot
assert (ref_plot.containers[ax1][2][0]._original_edgecolor ==
ref_plot.containers[ax2][2][0]._original_edgecolor ==
ref_plot.get_lines()[axis_ix].get_color())
# Tests whether the color of the sld and resampled_sld match on the sld_plot
assert (sld_plot.get_lines()[ax1].get_color() ==
sld_plot.get_lines()[ax2].get_color())
def test_eventhandlers_linked_to_figure(fig: Figure) -> None:
"""
Tests whether the eventhandlers for close_event
and key_press_event in the figure are linked to the
class methods.
"""
pattern = r'\(([^\]]+)\)'
for ix, val in fig._fig.canvas.callbacks.callbacks['close_event'].items():
if str(type(val)) == "<class 'weakref.WeakMethod'>":
break
canvas_close_event_callback = fig._fig.canvas.callbacks.callbacks['close_event'][ix]._func_ref.__repr__()
close_event_callback = re.findall(pattern,
canvas_close_event_callback)[0]
assert close_event_callback == "_close"
assert hasattr(Figure, "_close")
for ix, val in fig._fig.canvas.callbacks.callbacks['key_press_event'].items():
if str(type(val)) == "<class 'weakref.WeakMethod'>":
break
canvas_key_press_event_callback = fig._fig.canvas.callbacks.callbacks['key_press_event'][ix]._func_ref.__repr__()
key_press_event_callback = re.findall(pattern,
canvas_key_press_event_callback)[0]
assert key_press_event_callback == "_process_button_press"
assert hasattr(Figure, "_process_button_press")
def test_eventhandler_variable_update(fig: Figure) -> None:
"""
Tests whether the eventhandlers for close_event
and key_press_event update variables that stop
while loop in wait_for_close.
"""
assert not fig._esc_pressed
on_key_mock_event = type('MockEvent', (object,), {'key': 'escape'})
fig._process_button_press(on_key_mock_event)
assert fig._esc_pressed
assert not fig._close_clicked
fig._close('test')
assert fig._close_clicked
@patch("RAT.utils.plotting.plt.waitforbuttonpress")
def test_wait_for_close(mock: MagicMock, fig: Figure) -> None:
"""
Tests the _wait_for_close method stops the
while loop when _esc_pressed is True.
"""
def mock_wait_for_button_press(timeout):
fig._esc_pressed = True
mock.side_effect = mock_wait_for_button_press
assert not fig._esc_pressed
fig.wait_for_close()
assert fig._esc_pressed
@patch("RAT.utils.plotting.makeSLDProfileXY")
def test_sld_profile_function_call(mock: MagicMock) -> None:
"""
Tests the makeSLDProfileXY function called with
correct args.
"""
plot_ref_sld(data())
assert mock.call_count == 3
assert mock.call_args_list[0].args[0] == 2.07e-06
assert mock.call_args_list[0].args[1] == 6.28e-06
assert mock.call_args_list[0].args[2] == 0.0
assert mock.call_args_list[0].args[4] == 82
assert mock.call_args_list[0].args[5] == 1.0
assert mock.call_args_list[1].args[0] == 2.07e-06
assert mock.call_args_list[1].args[1] == 1.83e-06
assert mock.call_args_list[1].args[2] == 0.0
assert mock.call_args_list[1].args[4] == 128
assert mock.call_args_list[1].args[5] == 1.0
assert mock.call_args_list[2].args[0] == 2.07e-06
assert mock.call_args_list[2].args[1] == -5.87e-07
assert mock.call_args_list[2].args[2] == 0.0
assert mock.call_args_list[2].args[4] == 153
assert mock.call_args_list[2].args[5] == 1.0