Skip to content

Commit

Permalink
Make fillsinks memory-order agnostic (TopoToolbox#125)
Browse files Browse the repository at this point in the history
This change represents one way that we might make our interface with libtopotoolbox agnostic to the memory order of our underlying numpy arrays. I have added a property dims to GridObject, which reverses the order of the dimensions if the array is in row-major layout. GridObject.shape still returns (rows, columns). If we pass self.dims instead of self.shape to the fillsinks wrapper, we will now get the same, correct, answer if the GridObject.z array is column- or row-major. There is also a test that confirms this behavior.
  • Loading branch information
wkearn authored Jan 16, 2025
1 parent 0fb25b6 commit 84140e0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/topotoolbox/grid_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ def columns(self):
"""
return self.z.shape[1]

@property
def dims(self):
"""The dimensions of the grid in the correct order for libtopotoolbox
"""
if self.z.flags.c_contiguous:
return (self.columns, self.rows)

if self.z.flags.f_contiguous:
return (self.rows, self.columns)

raise TypeError("Grid is not stored as a contiguous row- or column-major array")

def reproject(self,
crs: 'CRS',
resolution: 'float | None' = None,
Expand Down Expand Up @@ -137,7 +149,7 @@ def fillsinks(self,
"""

dem = self.z.astype(np.float32, order='F')
dem = self.z.astype(np.float32)
output = np.zeros_like(dem)

restore_nans = False
Expand All @@ -161,9 +173,9 @@ def fillsinks(self,

if hybrid:
queue = np.zeros_like(dem, dtype=np.int64)
_grid.fillsinks_hybrid(output, queue, dem, bc, self.shape)
_grid.fillsinks_hybrid(output, queue, dem, bc, self.dims)
else:
_grid.fillsinks(output, dem, bc, self.shape)
_grid.fillsinks(output, dem, bc, self.dims)

if restore_nans:
dem[nans] = np.nan
Expand Down
36 changes: 36 additions & 0 deletions tests/test_grid_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import topotoolbox as topo

import opensimplex


@pytest.fixture
def square_dem():
Expand Down Expand Up @@ -67,6 +69,40 @@ def test_fillsinks(square_dem, wide_dem, tall_dem):
assert sink < 8


def test_fillsinks_order():
opensimplex.seed(12)

x = np.arange(0,128)
y = np.arange(0,256)

dem_C = topo.GridObject()
dem_C.z = 64 * (opensimplex.noise2array(x,y) + 1)

assert dem_C.shape[0] == 256
assert dem_C.shape[1] == 128

assert dem_C.z.flags.c_contiguous
assert dem_C.dims[0] == 128
assert dem_C.dims[1] == 256

dem_F = topo.GridObject()
dem_F.z = np.asfortranarray(dem_C.z)

assert dem_F.shape[0] == 256
assert dem_F.shape[1] == 128

assert dem_F.z.flags.f_contiguous
assert dem_F.dims[0] == 256
assert dem_F.dims[1] == 128

filled_C = dem_C.fillsinks()
assert filled_C.z.flags.c_contiguous

filled_F = dem_F.fillsinks()
assert filled_F.z.flags.f_contiguous

assert np.array_equal(filled_F.z, filled_C.z)

def test_identifyflats(square_dem, wide_dem, tall_dem):
# TODO: add more tests
for dem in [square_dem, wide_dem, tall_dem]:
Expand Down

0 comments on commit 84140e0

Please sign in to comment.