Skip to content

Commit

Permalink
Update viz_tools tests to include zorder parameter (#114)
Browse files Browse the repository at this point in the history
Added assertions for the `zorder` parameter in `plot_coastline` and
`plot_land_mask` tests to improve feature coverage and correctness. Adjusted
calls to use `numpy.array` where applicable for consistency in data handling.
  • Loading branch information
douglatornell authored Dec 22, 2024
1 parent 975a0b0 commit 0bfe396
Showing 1 changed file with 101 additions and 16 deletions.
117 changes: 101 additions & 16 deletions SalishSeaTools/tests/test_viz_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
patch,
)
import netCDF4 as nc
import numpy
import numpy as np
import pytest

Expand Down Expand Up @@ -57,24 +58,35 @@ class TestPlotCoastline(object):
@patch('salishsea_tools.viz_tools.nc.Dataset')
def test_plot_coastline_defaults_bathy_file(self, m_dataset):
axes = Mock()

viz_tools.plot_coastline(axes, 'bathyfile')

m_dataset.assert_called_once_with('bathyfile')
m_dataset().close.assert_called_once_with()

@patch('salishsea_tools.viz_tools.nc.Dataset')
def test_plot_coastline_defaults_bathy_netCDF_obj(self, m_dataset):
axes, bathy = Mock(), Mock()
bathy.variables = {'Bathymetry': Mock()}

viz_tools.plot_coastline(axes, bathy)

assert not m_dataset.called
assert not m_dataset.close.called

def test_plot_coastline_defaults(self):
axes, bathy = Mock(), Mock()
bathy.variables = {'Bathymetry': Mock()}

contour_lines = viz_tools.plot_coastline(axes, bathy)

axes.contour.assert_called_once_with(
bathy.variables['Bathymetry'], [0], colors='black')
bathy.variables['Bathymetry'],
[0],
colors='black',
zorder=2,
)
assert contour_lines == axes.contour()
assert contour_lines == axes.contour()

def test_plot_coastline_map_coords(self):
Expand All @@ -84,31 +96,46 @@ def test_plot_coastline_map_coords(self):
'nav_lat': Mock(),
'nav_lon': Mock(),
}

contour_lines = viz_tools.plot_coastline(axes, bathy, coords='map')

axes.contour.assert_called_once_with(
bathy.variables['nav_lon'], bathy.variables['nav_lat'],
bathy.variables['Bathymetry'], [0], colors='black')
bathy.variables['nav_lon'],
bathy.variables['nav_lat'],
bathy.variables['Bathymetry'],
[0],
colors='black',
zorder=2,
)
assert contour_lines == axes.contour()

def test_plot_coastline_isobath(self):
axes, bathy = Mock(), Mock()
bathy.variables = {'Bathymetry': Mock()}

contour_lines = viz_tools.plot_coastline(
axes, bathy, isobath=42.42)

axes.contour.assert_called_once_with(
bathy.variables['Bathymetry'], [42.42], colors='black')
bathy.variables['Bathymetry'],
[42.42],
colors='black',
zorder=2,
)
assert contour_lines == axes.contour()

def test_plot_coastline_no_xslice(self):
axes, bathy = Mock(), Mock()
bathy.variables = {'Bathymetry': Mock()}

with pytest.raises(ValueError):
viz_tools.plot_coastline(
axes, bathy, yslice=np.arange(200, 320))

def test_plot_coastline_no_yslice(self):
axes, bathy = Mock(), Mock()
bathy.variables = {'Bathymetry': Mock()}

with pytest.raises(ValueError):
viz_tools.plot_coastline(
axes, bathy, xslice=np.arange(250, 370))
Expand All @@ -118,11 +145,18 @@ def test_plot_coastline_grid_coords_slice(self):
bathy.variables = {'Bathymetry': MagicMock(spec=nc.Variable)}
xslice = np.arange(250, 370)
yslice = np.arange(200, 320)

contour_lines = viz_tools.plot_coastline(
axes, bathy, xslice=xslice, yslice=yslice)

axes.contour.assert_called_once_with(
xslice, yslice, bathy.variables['Bathymetry'][yslice, xslice].data,
[0], colors='black')
xslice,
yslice,
bathy.variables['Bathymetry'][yslice, xslice].data,
[0],
colors='black',
zorder=2,
)
assert contour_lines == axes.contour()

def test_plot_coastline_map_coords_slice(self):
Expand All @@ -134,47 +168,68 @@ def test_plot_coastline_map_coords_slice(self):
}
xslice = np.arange(250, 370)
yslice = np.arange(200, 320)

contour_lines = viz_tools.plot_coastline(
axes, bathy, coords='map', xslice=xslice, yslice=yslice)

axes.contour.assert_called_once_with(
bathy.variables['nav_lon'][yslice, xslice],
bathy.variables['nav_lat'][yslice, xslice],
bathy.variables['Bathymetry'][yslice, xslice].data,
[0], colors='black')
[0],
colors='black',
zorder=2,
)
assert contour_lines == axes.contour()

def test_plot_coastline_color_arg(self):
axes, bathy = Mock(), Mock()
bathy.variables = {'Bathymetry': Mock()}

contour_lines = viz_tools.plot_coastline(
axes, bathy, color='red')

axes.contour.assert_called_once_with(
bathy.variables['Bathymetry'], [0], colors='red')
bathy.variables['Bathymetry'],
[0],
colors='red',
zorder=2,
)
assert contour_lines == axes.contour()


class TestPlotLandMask(object):
@patch('salishsea_tools.viz_tools.nc.Dataset')
def test_plot_land_mask_defaults_bathy_file(self, m_dataset):
axes = Mock()

viz_tools.plot_land_mask(axes, 'bathyfile')

m_dataset.assert_called_once_with('bathyfile')
m_dataset().close.assert_called_once_with()

@patch('salishsea_tools.viz_tools.nc.Dataset')
def test_plot_land_mask_defaults_bathy_netCDF_obj(self, m_dataset):
axes, bathy = Mock(), Mock()
bathy.variables = {'Bathymetry': Mock()}

viz_tools.plot_land_mask(axes, bathy)

assert not m_dataset.called
assert not m_dataset.close.called

def test_plot_land_mask_defaults(self):
axes, bathy = Mock(), Mock()
bathy.variables = {'Bathymetry': Mock()}

contour_fills = viz_tools.plot_land_mask(axes, bathy)

axes.contourf.assert_called_once_with(
bathy.variables['Bathymetry'], [-0.01, 0.01], colors='black')
numpy.array(bathy.variables['Bathymetry'], dtype=object),
[-0.01, 0.01],
colors='black',
zorder=1,
)
assert contour_fills == axes.contourf()

def test_plot_land_mask_map_coords(self):
Expand All @@ -184,32 +239,44 @@ def test_plot_land_mask_map_coords(self):
'nav_lat': Mock(),
'nav_lon': Mock(),
}

contour_fills = viz_tools.plot_land_mask(axes, bathy, coords='map')

axes.contourf.assert_called_once_with(
bathy.variables['nav_lon'], bathy.variables['nav_lat'],
bathy.variables['Bathymetry'], [-0.01, 0.01], colors='black')
numpy.array(bathy.variables['nav_lon'], dtype=object),
numpy.array(bathy.variables['nav_lat'], dtype=object),
numpy.array(bathy.variables['Bathymetry'], dtype=object),
[-0.01, 0.01],
colors='black',
zorder=1,
)
assert contour_fills == axes.contourf()

def test_plot_land_mask_isobath(self):
axes, bathy = Mock(), Mock()
bathy.variables = {'Bathymetry': Mock()}

contour_fills = viz_tools.plot_land_mask(
axes, bathy, isobath=42.42)

args, kwargs = axes.contourf.call_args

assert args[0] == bathy.variables['Bathymetry']
np.testing.assert_almost_equal(args[1], [-0.01, 42.43])
assert kwargs == {'colors': 'black'}
assert kwargs == {'colors': 'black', 'zorder': 1}
assert contour_fills == axes.contourf()

def test_plot_land_mask_no_xslice(self):
axes, bathy = Mock(), Mock()
bathy.variables = {'Bathymetry': Mock()}

with pytest.raises(ValueError):
viz_tools.plot_land_mask(axes, bathy, yslice=np.arange(200, 320))

def test_plot_land_mask_no_yslice(self):
axes, bathy = Mock(), Mock()
bathy.variables = {'Bathymetry': Mock()}

with pytest.raises(ValueError):
viz_tools.plot_land_mask(axes, bathy, xslice=np.arange(250, 370))

Expand All @@ -218,11 +285,18 @@ def test_plot_land_mask_grid_coords_slice(self):
bathy.variables = {'Bathymetry': MagicMock(spec=nc.Variable)}
xslice = np.arange(250, 370)
yslice = np.arange(200, 320)

contour_fills = viz_tools.plot_land_mask(
axes, bathy, xslice=xslice, yslice=yslice)

axes.contourf.assert_called_once_with(
xslice, yslice, bathy.variables['Bathymetry'][yslice, xslice].data,
[-0.01, 0.01], colors='black')
xslice,
yslice,
bathy.variables['Bathymetry'][yslice, xslice].data,
[-0.01, 0.01],
colors='black',
zorder=1,
)
assert contour_fills == axes.contourf()

def test_plot_land_mask_map_coords_slice(self):
Expand All @@ -234,22 +308,33 @@ def test_plot_land_mask_map_coords_slice(self):
}
xslice = np.arange(250, 370)
yslice = np.arange(200, 320)

contour_fills = viz_tools.plot_land_mask(
axes, bathy, coords='map', xslice=xslice, yslice=yslice)

axes.contourf.assert_called_once_with(
bathy.variables['nav_lon'][yslice, xslice],
bathy.variables['nav_lat'][yslice, xslice],
bathy.variables['Bathymetry'][yslice, xslice].data,
[-0.01, 0.01], colors='black')
[-0.01, 0.01],
colors='black',
zorder=1,
)
assert contour_fills == axes.contourf()

def test_plot_land_mask_color_arg(self):
axes, bathy = Mock(), Mock()
bathy.variables = {'Bathymetry': Mock()}

contour_fills = viz_tools.plot_land_mask(
axes, bathy, color='red')

axes.contourf.assert_called_once_with(
bathy.variables['Bathymetry'], [-0.01, 0.01], colors='red')
bathy.variables['Bathymetry'],
[-0.01, 0.01],
colors='red',
zorder=1,
)
assert contour_fills == axes.contourf()


Expand Down

0 comments on commit 0bfe396

Please sign in to comment.