Completed
Pull Request — master (#552)
by Ryan
58s
created

test_stationplot_clipping()   A

Complexity

Conditions 1

Size

Total Lines 21

Duplication

Lines 42
Ratio 200 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 42
loc 21
rs 9.3142
1
# Copyright (c) 2008-2015 MetPy Developers.
2
# Distributed under the terms of the BSD 3-Clause License.
3
# SPDX-License-Identifier: BSD-3-Clause
4
"""Tests for the `station_plot` module."""
5
6
import matplotlib
7
import matplotlib.pyplot as plt
8
import numpy as np
9
import pytest
10
11
from metpy.plots import nws_layout, simple_layout, StationPlot, StationPlotLayout
12
from metpy.plots.wx_symbols import current_weather, high_clouds, sky_cover
13
# Fixtures to make sure we have the right backend and consistent round
14
from metpy.testing import patch_round, set_agg_backend  # noqa: F401
15
from metpy.units import units
16
17
18
MPL_VERSION = matplotlib.__version__[:3]
19
20
21 View Code Duplication
@pytest.mark.mpl_image_compare(tolerance={'1.5': 0.04625, '1.4': 4.1}.get(MPL_VERSION, 0.0033),
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
22
                               savefig_kwargs={'dpi': 300}, remove_text=True)
23
def test_stationplot_api():
24
    """Test the StationPlot API."""
25
    fig = plt.figure(figsize=(9, 9))
26
27
    # testing data
28
    x = np.array([1, 5])
29
    y = np.array([2, 4])
30
31
    # Make the plot
32
    sp = StationPlot(fig.add_subplot(1, 1, 1), x, y, fontsize=16)
33
    sp.plot_barb([20, 0], [0, -50])
34
    sp.plot_text('E', ['KOKC', 'ICT'], color='blue')
35
    sp.plot_parameter('NW', [10.5, 15] * units.degC, color='red')
36
    sp.plot_symbol('S', [5, 7], high_clouds, color='green')
37
38
    sp.ax.set_xlim(0, 6)
39
    sp.ax.set_ylim(0, 6)
40
41
    return fig
42
43
44 View Code Duplication
@pytest.mark.mpl_image_compare(tolerance={'1.4': 2.81}.get(MPL_VERSION, 0.003),
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
45
                               savefig_kwargs={'dpi': 300}, remove_text=True)
46
def test_stationplot_clipping():
47
    """Test the that clipping can be enabled as a default parameter."""
48
    fig = plt.figure(figsize=(9, 9))
49
50
    # testing data
51
    x = np.array([1, 5])
52
    y = np.array([2, 4])
53
54
    # Make the plot
55
    sp = StationPlot(fig.add_subplot(1, 1, 1), x, y, fontsize=16, clip_on=True)
56
    sp.plot_barb([20, 0], [0, -50])
57
    sp.plot_text('E', ['KOKC', 'ICT'], color='blue')
58
    sp.plot_parameter('NW', [10.5, 15] * units.degC, color='red')
59
    sp.plot_symbol('S', [5, 7], high_clouds, color='green')
60
61
    sp.ax.set_xlim(1, 5)
62
    sp.ax.set_ylim(1.75, 4.25)
63
64
    return fig
65
66
67
@pytest.mark.mpl_image_compare(tolerance={'1.5': 0.05974, '1.4': 3.7}.get(MPL_VERSION, 0.0033),
68
                               savefig_kwargs={'dpi': 300}, remove_text=True)
69
def test_station_plot_replace():
70
    """Test that locations are properly replaced."""
71
    fig = plt.figure(figsize=(3, 3))
72
73
    # testing data
74
    x = np.array([1])
75
    y = np.array([1])
76
77
    # Make the plot
78
    sp = StationPlot(fig.add_subplot(1, 1, 1), x, y, fontsize=16)
79
    sp.plot_barb([20], [0])
80
    sp.plot_barb([5], [0])
81
    sp.plot_parameter('NW', [10.5], color='red')
82
    sp.plot_parameter('NW', [20], color='blue')
83
84
    sp.ax.set_xlim(-3, 3)
85
    sp.ax.set_ylim(-3, 3)
86
87
    return fig
88
89
90
@pytest.mark.mpl_image_compare(tolerance={'1.5': 0.036, '1.4': 2.02}.get(MPL_VERSION, 0.00321),
91
                               savefig_kwargs={'dpi': 300}, remove_text=True)
92
def test_stationlayout_api():
93
    """Test the StationPlot API."""
94
    fig = plt.figure(figsize=(9, 9))
95
96
    # testing data
97
    x = np.array([1, 5])
98
    y = np.array([2, 4])
99
    data = {'temp': np.array([32., 212.]) * units.degF, 'u': np.array([2, 0]) * units.knots,
100
            'v': np.array([0, 5]) * units.knots, 'stid': ['KDEN', 'KSHV'], 'cover': [3, 8]}
101
102
    # Set up the layout
103
    layout = StationPlotLayout()
104
    layout.add_barb('u', 'v', units='knots')
105
    layout.add_value('NW', 'temp', fmt='0.1f', units=units.degC, color='darkred')
106
    layout.add_symbol('C', 'cover', sky_cover, color='magenta')
107
    layout.add_text((0, 2), 'stid', color='darkgrey')
108
    layout.add_value('NE', 'dewpt', color='green')  # This should be ignored
109
110
    # Make the plot
111
    sp = StationPlot(fig.add_subplot(1, 1, 1), x, y, fontsize=12)
112
    layout.plot(sp, data)
113
114
    sp.ax.set_xlim(0, 6)
115
    sp.ax.set_ylim(0, 6)
116
117
    return fig
118
119
120
def test_station_layout_odd_data():
121
    """Test more corner cases with data passed in."""
122
    fig = plt.figure(figsize=(9, 9))
123
124
    # Set up test layout
125
    layout = StationPlotLayout()
126
    layout.add_barb('u', 'v')
127
    layout.add_value('W', 'temperature', units='degF')
128
129
    # Now only use data without wind and no units
130
    data = {'temperature': [25.]}
131
132
    # Make the plot
133
    sp = StationPlot(fig.add_subplot(1, 1, 1), [1], [2], fontsize=12)
134
    layout.plot(sp, data)
135
    assert True
136
137
138
def test_station_layout_replace():
139
    """Test that layout locations are replaced."""
140
    layout = StationPlotLayout()
141
    layout.add_text('E', 'temperature')
142
    layout.add_value('E', 'dewpoint')
143
    assert 'E' in layout
144
    assert layout['E'][0] is StationPlotLayout.PlotTypes.value
145
    assert layout['E'][1] == 'dewpoint'
146
147
148
def test_station_layout_names():
149
    """Test getting station layout names."""
150
    layout = StationPlotLayout()
151
    layout.add_barb('u', 'v')
152
    layout.add_text('E', 'stid')
153
    layout.add_value('W', 'temp')
154
    layout.add_symbol('C', 'cover', lambda x: x)
155
    assert sorted(layout.names()) == ['cover', 'stid', 'temp', 'u', 'v']
156
157
158
@pytest.mark.mpl_image_compare(tolerance={'1.5': 0.05447, '1.4': 3.0}.get(MPL_VERSION, 0.0039),
159
                               savefig_kwargs={'dpi': 300}, remove_text=True)
160
def test_simple_layout():
161
    """Test metpy's simple layout for station plots."""
162
    fig = plt.figure(figsize=(9, 9))
163
164
    # testing data
165
    x = np.array([1, 5])
166
    y = np.array([2, 4])
167
    data = {'air_temperature': np.array([32., 212.]) * units.degF,
168
            'dew_point_temperature': np.array([28., 80.]) * units.degF,
169
            'air_pressure_at_sea_level': np.array([29.92, 28.00]) * units.inHg,
170
            'eastward_wind': np.array([2, 0]) * units.knots,
171
            'northward_wind': np.array([0, 5]) * units.knots, 'cloud_coverage': [3, 8],
172
            'present_weather': [65, 75], 'unused': [1, 2]}
173
174
    # Make the plot
175
    sp = StationPlot(fig.add_subplot(1, 1, 1), x, y, fontsize=12)
176
    simple_layout.plot(sp, data)
177
178
    sp.ax.set_xlim(0, 6)
179
    sp.ax.set_ylim(0, 6)
180
181
    return fig
182
183
184
@pytest.mark.mpl_image_compare(tolerance={'1.4': 7.02}.get(MPL_VERSION, 0.1848),
185
                               savefig_kwargs={'dpi': 300}, remove_text=True)
186
def test_nws_layout():
187
    """Test metpy's NWS layout for station plots."""
188
    fig = plt.figure(figsize=(3, 3))
189
190
    # testing data
191
    x = np.array([1])
192
    y = np.array([2])
193
    data = {'air_temperature': np.array([77]) * units.degF,
194
            'dew_point_temperature': np.array([71]) * units.degF,
195
            'air_pressure_at_sea_level': np.array([999.8]) * units('mbar'),
196
            'eastward_wind': np.array([15.]) * units.knots,
197
            'northward_wind': np.array([15.]) * units.knots, 'cloud_coverage': [7],
198
            'present_weather': [80], 'high_cloud_type': [1], 'medium_cloud_type': [3],
199
            'low_cloud_type': [2], 'visibility_in_air': np.array([5.]) * units.mile,
200
            'tendency_of_air_pressure': np.array([-0.3]) * units('mbar'),
201
            'tendency_of_air_pressure_symbol': [8]}
202
203
    # Make the plot
204
    sp = StationPlot(fig.add_subplot(1, 1, 1), x, y, fontsize=12, spacing=16)
205
    nws_layout.plot(sp, data)
206
207
    sp.ax.set_xlim(0, 3)
208
    sp.ax.set_ylim(0, 3)
209
210
    return fig
211
212
213
@pytest.mark.mpl_image_compare(tolerance={'1.4': 6.68}.get(MPL_VERSION, 1.05),
214
                               remove_text=True)
215
def test_plot_text_fontsize():
216
    """Test changing fontsize in plot_text."""
217
    fig = plt.figure(figsize=(3, 3))
218
    ax = plt.subplot(1, 1, 1)
219
220
    # testing data
221
    x = np.array([1])
222
    y = np.array([2])
223
224
    # Make the plot
225
    sp = StationPlot(ax, x, y, fontsize=36)
226
    sp.plot_text('NW', ['72'], fontsize=24)
227
    sp.plot_text('SW', ['60'], fontsize=4)
228
229
    sp.ax.set_xlim(0, 3)
230
    sp.ax.set_ylim(0, 3)
231
232
    return fig
233
234
235
@pytest.mark.mpl_image_compare(tolerance={'1.4': 26.8}.get(MPL_VERSION, 1.05),
236
                               remove_text=True)
237
def test_plot_symbol_fontsize():
238
    """Test changing fontsize in plotting of symbols."""
239
    fig = plt.figure(figsize=(3, 3))
240
    ax = plt.subplot(1, 1, 1)
241
242
    sp = StationPlot(ax, [0], [0], fontsize=8, spacing=32)
243
    sp.plot_symbol('E', [92], current_weather)
244
    sp.plot_symbol('W', [96], current_weather, fontsize=100)
245
246
    return fig
247
248
249
def test_layout_str():
250
    """Test layout string representation."""
251
    layout = StationPlotLayout()
252
    layout.add_barb('u', 'v')
253
    layout.add_text('E', 'stid')
254
    layout.add_value('W', 'temp')
255
    layout.add_symbol('C', 'cover', lambda x: x)
256
    assert str(layout) == ('{C: (symbol, cover, ...), E: (text, stid, ...), '
257
                           'W: (value, temp, ...), barb: (barb, (\'u\', \'v\'), ...)}')
258