Skip to content

Commit

Permalink
Port fix on layer.sum to layer.save when called with and_sum arg.
Browse files Browse the repository at this point in the history
  • Loading branch information
mdales committed Nov 20, 2024
1 parent e4d4786 commit ff8b75a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "yirgacheffe"
version = "0.8.6"
version = "0.8.7"
description = "Abstraction of gdal datasets for doing basic math operations"
readme = "README.md"
authors = [{ name = "Michael Dales", email = "[email protected]" }]
Expand Down
51 changes: 50 additions & 1 deletion tests/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,8 +501,57 @@ def test_write_mulitband_raster() -> None:

assert (expected == actual).all()

def test_sum_float32(monkeypatch) -> None:
def test_save_and_sum_float32(monkeypatch) -> None:
random.seed(42)
data = []
for _ in range(10):
row = []
for _ in range(10):
row.append(random.random())
data.append(row)

data1 = np.array(data, dtype=np.float32)
layer1 = RasterLayer(gdal_dataset_with_data((0.0, 0.0), 0.02, data1))
assert layer1.datatype == gdal.GDT_Float32

# Sum forces things to float64
expected = np.sum(data1.astype(np.float64))

with monkeypatch.context() as m:
for blocksize in range(1,11):
m.setattr(yirgacheffe.constants, "YSTEP", blocksize)
with RasterLayer.empty_raster_layer_like(layer1) as store:
actual = layer1.save(store, and_sum=True)
assert expected == actual

def test_parallel_save_and_sum_float32(monkeypatch) -> None:
random.seed(42)
data = []
for _ in range(10):
row = []
for _ in range(10):
row.append(random.random())
data.append(row)

with tempfile.TemporaryDirectory() as tempdir:
path1 = os.path.join(tempdir, "test1.tif")
data1 = np.array(data, dtype=np.float32)
dataset1 = gdal_dataset_with_data((0.0, 0.0), 0.02, data1, filename=path1)
dataset1.Close()
layer1 = RasterLayer.layer_from_file(path1)
assert layer1.datatype == gdal.GDT_Float32

# Sum forces things to float64
expected = np.sum(data1.astype(np.float64))

with monkeypatch.context() as m:
for blocksize in range(1,11):
m.setattr(yirgacheffe.constants, "YSTEP", blocksize)
with RasterLayer.empty_raster_layer_like(layer1) as store:
actual = layer1.parallel_save(store, and_sum=True)
assert expected == actual

def test_sum_float32(monkeypatch) -> None:
random.seed(42)
data = []
for _ in range(10):
Expand Down
5 changes: 2 additions & 3 deletions yirgacheffe/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def save(self, destination_layer, and_sum=False, callback=None, band=1):
yoffset + destination_window.yoff,
)
if and_sum:
total += np.sum(chunk)
total += np.sum(chunk.astype(np.float64))
if callback:
callback(1.0)

Expand Down Expand Up @@ -316,7 +316,6 @@ def parallel_save(self, destination_layer, and_sum=False, callback=None, paralle
gdal.GDT_UInt64: np.dtype('uint64'),
}[band.DataType]


with multiprocessing.Manager() as manager:
with SharedMemoryManager() as smm:

Expand Down Expand Up @@ -375,7 +374,7 @@ def parallel_save(self, destination_layer, and_sum=False, callback=None, paralle
yoffset + destination_window.yoff,
)
if and_sum:
total += np.sum(arr[0:step])
total += np.sum(np.array(arr[0:step]).astype(np.float64))
sem.release()
retired_blocks += 1
if callback:
Expand Down

0 comments on commit ff8b75a

Please sign in to comment.