Skip to content

Commit

Permalink
Merge pull request #1937 from dopplershift/cleanup
Browse files Browse the repository at this point in the history
Cleaning up analysis warnings
  • Loading branch information
dcamron authored Jun 30, 2021
2 parents cf21f02 + e5c47a5 commit bb98358
Show file tree
Hide file tree
Showing 18 changed files with 66 additions and 80 deletions.
11 changes: 11 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,14 @@ def test_da_xy():
}

return ds.metpy.parse_cf('temperature')


@pytest.fixture()
def set_agg_backend():
"""Fixture to ensure the Agg backend is active."""
prev_backend = matplotlib.pyplot.get_backend()
try:
matplotlib.pyplot.switch_backend('agg')
yield
finally:
matplotlib.pyplot.switch_backend(prev_backend)
1 change: 0 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
# serve to show the default.

from datetime import datetime
import os
from pathlib import Path
import re
import sys
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ where = src
[options.extras_require]
doc = sphinx; sphinx-gallery>=0.4; myst-parser; netCDF4
examples = cartopy>=0.15.0; matplotlib>=2.2.0
test = pytest>=2.4; pytest-mpl; cartopy>=0.16.0; netCDF4
test = pytest>=2.4; pytest-mpl; cartopy>=0.17.0; netCDF4

[build_sphinx]
source-dir = docs/source
Expand Down
9 changes: 4 additions & 5 deletions src/metpy/io/metar.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""Parse METAR-formatted data."""
# Import the necessary libraries
from collections import namedtuple
import contextlib
from datetime import datetime
import warnings

Expand Down Expand Up @@ -521,11 +522,9 @@ def merge(x, key=' '):
if len(tmp):
yield ' '.join(tmp)

# Open the file
myfile = open_as_needed(filename, 'rt')

# Clean up the file and take out the next line (\n)
value = myfile.read().rstrip()
# Open the file and clean up and take out the next line (\n)
with contextlib.closing(open_as_needed(filename, 'rt')) as myfile:
value = myfile.read().rstrip()
list_values = value.split('\n')
list_values = list(filter(None, list_values))

Expand Down
2 changes: 1 addition & 1 deletion src/metpy/io/nexrad.py
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ def __init__(self, prod):
offset = prod.thresholds[1] / 1000.
leading_flags = prod.thresholds[3]
for i in range(leading_flags, data_levels):
self.lut = scale * i + offset
self.lut[i] = scale * i + offset


class LegacyMapper(DataMapper):
Expand Down
2 changes: 1 addition & 1 deletion src/metpy/plots/cartopy_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def geometries(self):
# Ensure that the associated files are in the cache
fname = f'{self.name}_{self.scaler.scale}'
for extension in ['.dbf', '.shx']:
get_test_data(fname + extension)
get_test_data(fname + extension, as_file_obj=False)
path = get_test_data(fname + '.shp', as_file_obj=False)
return iter(tuple(shapereader.Reader(path).geometries()))

Expand Down
41 changes: 14 additions & 27 deletions src/metpy/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* unit-aware test functions
* code for testing matplotlib figures
"""
import contextlib
import functools

import numpy as np
Expand Down Expand Up @@ -57,30 +58,28 @@ def get_upper_air_data(date, station):
'2002-11-11T00Z_BNA': 'nov11_sounding.txt',
'2010-12-09T12Z_BOI': 'dec9_sounding.txt'}

fname = sounding_files[sounding_key]
fobj = get_test_data(fname)
# Initiate lists for variables
arr_data = []

def to_float(s):
# Remove all whitespace and replace empty values with NaN
if not s.strip():
s = 'nan'
return float(s)

# Skip dashes, column names, units, and more dashes
for _ in range(4):
fobj.readline()

# Initiate lists for variables
arr_data = []
with contextlib.closing(get_test_data(sounding_files[sounding_key])) as fobj:
# Skip dashes, column names, units, and more dashes
for _ in range(4):
fobj.readline()

# Read all lines of data and append to lists only if there is some data
for row in fobj:
level = to_float(row[0:7])
values = (to_float(row[7:14]), to_float(row[14:21]), to_float(row[21:28]),
to_float(row[42:49]), to_float(row[49:56]))
# Read all lines of data and append to lists only if there is some data
for row in fobj:
level = to_float(row[0:7])
values = (to_float(row[7:14]), to_float(row[14:21]), to_float(row[21:28]),
to_float(row[42:49]), to_float(row[49:56]))

if any(np.invert(np.isnan(values[1:]))):
arr_data.append((level,) + values)
if any(np.invert(np.isnan(values[1:]))):
arr_data.append((level,) + values)

p, z, t, td, direc, spd = np.array(arr_data).T

Expand Down Expand Up @@ -200,18 +199,6 @@ def assert_xarray_allclose(actual, desired):
assert desired.attrs == actual.attrs


@pytest.fixture(scope='module', autouse=True)
def set_agg_backend():
"""Fixture to ensure the Agg backend is active."""
import matplotlib.pyplot as plt
prev_backend = plt.get_backend()
try:
plt.switch_backend('agg')
yield
finally:
plt.switch_backend(prev_backend)


def check_and_silence_warning(warn_type):
"""Decorate a function to swallow some warning type, making sure they are present.
Expand Down
1 change: 1 addition & 0 deletions tests/interpolate/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ def test_inverse_distance_to_grid(method, test_data, test_grid):
xg, yg = test_grid

extra_kw = {}
test_file = ''
if method == 'cressman':
extra_kw['r'] = 20
extra_kw['min_neighbors'] = 1
Expand Down
35 changes: 18 additions & 17 deletions tests/interpolate/test_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,27 +119,28 @@ def test_inverse_distance_to_points(method, test_data, test_points):
obs_points = np.vstack([xp, yp]).transpose()

extra_kw = {}
if method == 'cressman':
extra_kw['r'] = 20
extra_kw['min_neighbors'] = 1
test_file = 'cressman_r20_mn1.npz'
elif method == 'barnes':
extra_kw['r'] = 40
extra_kw['kappa'] = 100
test_file = 'barnes_r40_k100.npz'
elif method == 'shouldraise':
if method == 'shouldraise':
extra_kw['r'] = 40
with pytest.raises(ValueError):
inverse_distance_to_points(
obs_points, z, test_points, kind=method, **extra_kw)
return

img = inverse_distance_to_points(obs_points, z, test_points, kind=method, **extra_kw)

with get_test_data(test_file) as fobj:
truth = np.load(fobj)['img'].reshape(-1)

assert_array_almost_equal(truth, img)
else:
test_file = ''
if method == 'cressman':
extra_kw['r'] = 20
extra_kw['min_neighbors'] = 1
test_file = 'cressman_r20_mn1.npz'
elif method == 'barnes':
extra_kw['r'] = 40
extra_kw['kappa'] = 100
test_file = 'barnes_r40_k100.npz'

img = inverse_distance_to_points(obs_points, z, test_points, kind=method, **extra_kw)

with get_test_data(test_file) as fobj:
truth = np.load(fobj)['img'].reshape(-1)

assert_array_almost_equal(truth, img)


@pytest.mark.parametrize('method', ['natural_neighbor', 'cressman', 'barnes', 'linear',
Expand Down
5 changes: 3 additions & 2 deletions tests/io/test_nexrad.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Distributed under the terms of the BSD 3-Clause License.
# SPDX-License-Identifier: BSD-3-Clause
"""Test the `nexrad` module."""

import contextlib
from datetime import datetime
from io import BytesIO
import logging
Expand Down Expand Up @@ -76,7 +76,8 @@ def read(self, n=None):

def test_doubled_file():
"""Test for #489 where doubled-up files didn't parse at all."""
data = get_test_data('Level2_KFTG_20150430_1419.ar2v').read()
with contextlib.closing(get_test_data('Level2_KFTG_20150430_1419.ar2v')) as infile:
data = infile.read()
fobj = BytesIO(data + data)
f = Level2File(fobj)
assert len(f.sweeps) == 12
Expand Down
7 changes: 2 additions & 5 deletions tests/plots/test_cartopy_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
import pytest

import metpy.plots as mpplots
from metpy.plots import cartopy_utils
# Fixture to make sure we have the right backend
from metpy.testing import set_agg_backend # noqa: F401, I202

MPL_VERSION = matplotlib.__version__[:3]

Expand Down Expand Up @@ -79,15 +76,15 @@ def test_cartopy_stub(monkeypatch):
# This makes sure that cartopy is not found
monkeypatch.setitem(sys.modules, 'cartopy.crs', None)

ccrs = cartopy_utils.import_cartopy()
ccrs = mpplots.cartopy_utils.import_cartopy()
with pytest.raises(AttributeError, match='without Cartopy'):
ccrs.PlateCarree()


def test_plots_getattr(monkeypatch):
"""Ensure the module-level getattr works."""
# Make sure the feature is missing
monkeypatch.delattr(cartopy_utils, 'USSTATES', raising=False)
monkeypatch.delattr(mpplots.cartopy_utils, 'USSTATES', raising=False)
with pytest.raises(AttributeError, match='Cannot use USSTATES without Cartopy'):
assert not mpplots.USSTATES # Should fail on attribute lookup before assert

Expand Down
5 changes: 2 additions & 3 deletions tests/plots/test_declarative.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
from metpy.io.metar import parse_metar_file
from metpy.plots import (BarbPlot, ContourPlot, FilledContourPlot, ImagePlot, MapPanel,
PanelContainer, PlotObs)
# Fixtures to make sure we have the right backend
from metpy.testing import needs_cartopy, set_agg_backend # noqa: F401, I202
from metpy.testing import needs_cartopy
from metpy.units import units


Expand Down Expand Up @@ -1357,7 +1356,7 @@ def test_save():
assert fobj.read()


def test_show():
def test_show(set_agg_backend):
"""Test that show works properly."""
pc = PanelContainer()

Expand Down
15 changes: 7 additions & 8 deletions tests/plots/test_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,13 @@ def test_mercator():
assert crs.proj4_params['lon_0'] == -100


# This won't work until at least CartoPy > 0.16.0
# def test_mercator_scale_factor():
# """Test handling a mercator projection with a scale factor."""
# attrs = {'grid_mapping_name': 'mercator', 'scale_factor_at_projection_origin': 0.9}
# crs = CFProjection(attrs).to_cartopy()
#
# assert isinstance(crs, ccrs.Mercator)
# assert crs.proj4_params['k_0'] == 0.9
def test_mercator_scale_factor():
"""Test handling a mercator projection with a scale factor."""
attrs = {'grid_mapping_name': 'mercator', 'scale_factor_at_projection_origin': 0.9}
crs = CFProjection(attrs).to_cartopy()

assert isinstance(crs, ccrs.Mercator)
assert crs.proj4_params['k_0'] == 0.9


def test_geostationary():
Expand Down
2 changes: 0 additions & 2 deletions tests/plots/test_mpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

# Needed to trigger scattertext monkey-patching
import metpy.plots # noqa: F401, I202
# Fixture to make sure we have the right backend
from metpy.testing import set_agg_backend # noqa: F401, I202


# Avoiding an image-based test here since that would involve text, which can be tricky
Expand Down
2 changes: 0 additions & 2 deletions tests/plots/test_skewt.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import pytest

from metpy.plots import Hodograph, SkewT
# Fixtures to make sure we have the right backend and consistent round
from metpy.testing import set_agg_backend # noqa: F401, I202
from metpy.units import units


Expand Down
2 changes: 0 additions & 2 deletions tests/plots/test_station_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

from metpy.plots import (current_weather, high_clouds, nws_layout, simple_layout,
sky_cover, StationPlot, StationPlotLayout)
# Fixtures to make sure we have the right backend and consistent round
from metpy.testing import set_agg_backend # noqa: F401, I202
from metpy.units import units


Expand Down
2 changes: 0 additions & 2 deletions tests/plots/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import pytest

from metpy.plots import add_metpy_logo, add_timestamp, add_unidata_logo, convert_gempak_color
# Fixture to make sure we have the right backend
from metpy.testing import set_agg_backend # noqa: F401, I202

MPL_VERSION = matplotlib.__version__[:3]

Expand Down
2 changes: 1 addition & 1 deletion tests/units/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import pytest

from metpy.testing import assert_array_almost_equal, assert_array_equal
from metpy.testing import assert_nan, set_agg_backend # noqa: F401
from metpy.testing import assert_nan
from metpy.units import check_units, concatenate, pandas_dataframe_to_unit_arrays, units


Expand Down

0 comments on commit bb98358

Please sign in to comment.