From 3085f3314af7e339c5d39115d20bb4986f2b1954 Mon Sep 17 00:00:00 2001 From: AntSimi <36040805+AntSimi@users.noreply.github.com> Date: Sat, 25 Sep 2021 16:14:49 +0200 Subject: [PATCH] Add dummy test on convolution => which detect an index error in original code(corrected) --- requirements.txt | 2 +- src/py_eddy_tracker/dataset/grid.py | 4 +- src/py_eddy_tracker/observations/network.py | 26 +++--------- tests/test_grid.py | 45 ++++++++++++++++----- 4 files changed, 44 insertions(+), 33 deletions(-) diff --git a/requirements.txt b/requirements.txt index 477cf32d..c4ff9c41 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -matplotlib +matplotlib<3.5 netCDF4 numba>=0.53 numpy<1.21 diff --git a/src/py_eddy_tracker/dataset/grid.py b/src/py_eddy_tracker/dataset/grid.py index 6c8e332f..59cda040 100644 --- a/src/py_eddy_tracker/dataset/grid.py +++ b/src/py_eddy_tracker/dataset/grid.py @@ -680,6 +680,7 @@ def eddy_identification( ) z_min, z_max = z_min_p, z_max_p + logger.debug("Levels from %f to %f", z_min, z_max) levels = arange(z_min - z_min % step, z_max - z_max % step + 2 * step, step) # Get x and y values @@ -1404,7 +1405,8 @@ def convolve_filter_with_dynamic_kernel( tmp_matrix = ma.zeros((2 * d_lon + data.shape[0], k_shape[1])) tmp_matrix.mask = ones(tmp_matrix.shape, dtype=bool) # Slice to apply on input data - sl_lat_data = slice(max(0, i - d_lat), min(i + d_lat, data.shape[1])) + # +1 for upper bound, to take in acount this column + sl_lat_data = slice(max(0, i - d_lat), min(i + d_lat + 1, data.shape[1])) # slice to apply on temporary matrix to store input data sl_lat_in = slice( d_lat - (i - sl_lat_data.start), d_lat + (sl_lat_data.stop - i) diff --git a/src/py_eddy_tracker/observations/network.py b/src/py_eddy_tracker/observations/network.py index 0ae80634..90bf6b70 100644 --- a/src/py_eddy_tracker/observations/network.py +++ b/src/py_eddy_tracker/observations/network.py @@ -6,6 +6,8 @@ import time from glob import glob +import netCDF4 +import zarr from numba import njit from numpy import ( arange, @@ -23,9 +25,6 @@ zeros, ) -import netCDF4 -import zarr - from ..dataset.grid import GridCollection from ..generic import build_index, wrap_longitude from ..poly import bbox_intersection, vertice_overlap @@ -680,13 +679,7 @@ def display_timeline( """ self.only_one_network() j = 0 - line_kw = dict( - ls="-", - marker="+", - markersize=6, - zorder=1, - lw=3, - ) + line_kw = dict(ls="-", marker="+", markersize=6, zorder=1, lw=3,) line_kw.update(kwargs) mappables = dict(lines=list()) @@ -919,10 +912,7 @@ def event_map(self, ax, **kwargs): """Add the merging and splitting events to a map""" j = 0 mappables = dict() - symbol_kw = dict( - markersize=10, - color="k", - ) + symbol_kw = dict(markersize=10, color="k",) symbol_kw.update(kwargs) symbol_kw_split = symbol_kw.copy() symbol_kw_split["markersize"] += 4 @@ -951,13 +941,7 @@ def event_map(self, ax, **kwargs): return mappables def scatter( - self, - ax, - name="time", - factor=1, - ref=None, - edgecolor_cycle=None, - **kwargs, + self, ax, name="time", factor=1, ref=None, edgecolor_cycle=None, **kwargs, ): """ This function scatters the path of each network, with the merging and splitting events diff --git a/tests/test_grid.py b/tests/test_grid.py index 2c89550a..759a40e1 100644 --- a/tests/test_grid.py +++ b/tests/test_grid.py @@ -1,5 +1,5 @@ from matplotlib.path import Path -from numpy import array, isnan, ma +from numpy import arange, array, isnan, ma, nan, ones, zeros from pytest import approx from py_eddy_tracker.data import get_demo_path @@ -7,15 +7,7 @@ G = RegularGridDataset(get_demo_path("mask_1_60.nc"), "lon", "lat") X = 0.025 -contour = Path( - ( - (-X, 0), - (X, 0), - (X, X), - (-X, X), - (-X, 0), - ) -) +contour = Path(((-X, 0), (X, 0), (X, X), (-X, X), (-X, 0),)) # contour @@ -85,3 +77,36 @@ def test_interp(): assert g.interp("z", x0, y0) == 1.5 assert g.interp("z", x1, y1) == 2 assert isnan(g.interp("z", x2, y2)) + + +def test_convolution(): + """ + Add some dummy check on convolution filter + """ + # Fake grid + z = ma.array( + arange(12).reshape((-1, 1)) * arange(10).reshape((1, -1)), + mask=zeros((12, 10), dtype="bool"), + dtype="f4", + ) + g = RegularGridDataset.with_array( + coordinates=("x", "y"), + datas=dict(z=z, x=arange(0, 6, 0.5), y=arange(0, 5, 0.5),), + centered=True, + ) + + def kernel_func(lat): + return ones((3, 3)) + + # After transpose we must get same result + d = g.convolve_filter_with_dynamic_kernel("z", kernel_func) + assert (d.T[:9, :9] == d[:9, :9]).all() + # We mask one value and check convolution result + z.mask[2, 2] = True + d = g.convolve_filter_with_dynamic_kernel("z", kernel_func) + assert d[1, 1] == z[:3, :3].sum() / 8 + # Add nan and check only nearest value is contaminate + z[2, 2] = nan + d = g.convolve_filter_with_dynamic_kernel("z", kernel_func) + assert not isnan(d[0, 0]) + assert isnan(d[1:4, 1:4]).all()