Skip to content

Commit

Permalink
add tests and fixes for raster.misc
Browse files Browse the repository at this point in the history
- adds tests for gradient, normed_potential_vectors (sort of), and
  divergence
- fixes divergence

reference issue #20
  • Loading branch information
njwilson23 committed Jan 7, 2017
1 parent fe8455b commit ed7f215
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
17 changes: 8 additions & 9 deletions karta/raster/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ def gradient(grid, band=0):
Parameters
----------
grid: RegularGrid
band: int, optional
band to compute hillshade for (default 0)
band: int, optional (default 0)
Returns
-------
Expand All @@ -108,23 +107,24 @@ def _div(U, V, res=(1.0, 1.0)):
+ np.pad(dVdy, ((1, 1), (0, 0)), "constant", constant_values=(np.nan,))
return divergence

def divergence(grid, band=0):
def divergence(grid, bands=(0, 1)):
""" Compute divergence from a grid.
Parameters
----------
grid: RegularGrid
band: int, optional
band to compute hillshade for (default 0)
band: tuple of ints, optional (default (0, 1))
Returns
-------
RegularGrid
"""
if grid.skew != (0, 0):
raise NotImplementedError("divergence calculations not implemented on skewed grids")
D = np.where(grid.data_mask, grid[:,:,band].astype(np.float32), np.nan)
return RegularGrid(grid.transform, _div(D, grid.resolution),
return RegularGrid(grid.transform,
_div(grid[:,:,bands[0]],
grid[:,:,bands[1]],
grid.resolution),
crs=grid.crs, nodata_value=np.nan)

def _normed_potential_vectors(D, res=(1.0, 1.0)):
Expand All @@ -149,8 +149,7 @@ def normed_potential_vectors(grid, band=0):
Parameters
----------
grid: RegularGrid
band: int, optional
band to compute hillshade for (default 0)
band: int, optional (default 0)
Returns
-------
Expand Down
32 changes: 31 additions & 1 deletion tests/raster_misc_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,37 @@ def test_aspect(self):
grid = RegularGrid((0, 0, 10, 10, 0, 0), values=np.ones_like(x)*y)
aspect = misc.aspect(grid)
self.assertTrue(np.allclose(0.5*np.pi, aspect[1:-1,1:-1]))
pass
return

def test_gradient(self):
x = np.linspace(-np.pi, np.pi, 256)
y = np.linspace(-np.pi, np.pi, 256).reshape(-1, 1)
g = RegularGrid((0, 0, 2*np.pi/255, 2*np.pi/255, 0, 0), values=np.sin(x)*np.cos(y))
gx, gy = misc.gradient(g)
self.assertTrue(np.nansum(np.abs(gx[:,:] - np.cos(x)*np.cos(y))) < 7.0)
self.assertTrue(np.nansum(np.abs(gy[:,:] + np.sin(x)*np.sin(y))) < 7.0)
return

def test_normed_potential_vectors(self):
x = np.linspace(-np.pi, np.pi, 256)
y = np.linspace(-np.pi, np.pi, 256).reshape(-1, 1)
g = RegularGrid((0, 0, 2*np.pi/255, 2*np.pi/255, 0, 0), values=np.sin(x)+np.sin(y))
u, v = misc.normed_potential_vectors(g)
# TODO: add test for correctness
return

def test_divergence(self):
x = np.linspace(-np.pi, np.pi, 256)
y = np.linspace(-np.pi, np.pi, 256).reshape(-1, 1)
g = RegularGrid((0, 0, 2*np.pi/255, 2*np.pi/255, 0, 0), values=np.sin(x)+np.sin(y))
u, v = misc.normed_potential_vectors(g)
twoband = RegularGrid(g.transform, np.dstack([u[:,:], v[:,:]]))
div = misc.divergence(twoband)

self.assertTrue(
np.nansum(np.abs((g[:,:]/g.max() + div[:,:]/div.max()))) < 1.32
)
return

if __name__ == "__main__":
unittest.main()
Expand Down

0 comments on commit ed7f215

Please sign in to comment.