Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make fillsinks memory-order agnostic #125

Merged
merged 4 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading