Skip to content

Commit

Permalink
Python: Add progress argument to exact_extract
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaston committed Jul 25, 2024
1 parent d2b6802 commit 6b4dde4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
34 changes: 32 additions & 2 deletions python/src/exactextract/exact_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ def exact_extract(
max_cells_in_memory: int = 30000000,
output: str = "geojson",
output_options: Optional[Mapping] = None,
progress=False,
):
"""Calculate zonal statistics
Expand Down Expand Up @@ -297,7 +298,9 @@ def exact_extract(
which may be significant for operations with large result sizes
such as ``cell_id``, ``values``, etc.
output_options: an optional dictionary of options passed to the :py:class:`writer.JSONWriter`, :py:class:`writer.PandasWriter`, or :py:class:`writer.GDALWriter`.
progress: if `True`, a progress bar will be displayed. Alternatively, a
function may be provided that will be called with the completion fraction
and a status message.
"""
rast = prep_raster(rast)
weights = prep_raster(weights, name_root="weight")
Expand All @@ -317,8 +320,35 @@ def exact_extract(
if include_geom:
processor.add_geom()
processor.set_max_cells_in_memory(max_cells_in_memory)
processor.process()

if progress:
processor.show_progress(True)

if callable(progress):
processor.set_progress_fn(progress)

Check warning on line 328 in python/src/exactextract/exact_extract.py

View check run for this annotation

Codecov / codecov/patch

python/src/exactextract/exact_extract.py#L328

Added line #L328 was not covered by tests
elif progress is True:
try:
import tqdm

bar = tqdm.tqdm(total=100)

Check warning on line 333 in python/src/exactextract/exact_extract.py

View check run for this annotation

Codecov / codecov/patch

python/src/exactextract/exact_extract.py#L333

Added line #L333 was not covered by tests

def status(frac, message):
pct = frac * 100
bar.update(pct - bar.n)
bar.set_description(message)
if pct == 100:
bar.close()

Check warning on line 340 in python/src/exactextract/exact_extract.py

View check run for this annotation

Codecov / codecov/patch

python/src/exactextract/exact_extract.py#L335-L340

Added lines #L335 - L340 were not covered by tests

except ImportError:

def status(frac, message):
print(f"[{frac*100:0.1f}%] {message}")

processor.set_progress_fn(status)
else:
raise ValueError("progress should be True or a function")

Check warning on line 349 in python/src/exactextract/exact_extract.py

View check run for this annotation

Codecov / codecov/patch

python/src/exactextract/exact_extract.py#L349

Added line #L349 was not covered by tests

processor.process()
writer.finish()

return writer.features()
10 changes: 10 additions & 0 deletions python/tests/test_exact_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -1170,3 +1170,13 @@ def test_explicit_operation():
results = exact_extract(None, square, op)

assert results[0]["properties"]["my_op"] == 4.0


def test_progress():

rast = NumPyRasterSource(np.arange(9).reshape(3, 3))
square = make_rect(0.5, 0.5, 2.5, 2.5)

squares = [square] * 10

exact_extract(rast, squares, "count", progress=True)

0 comments on commit 6b4dde4

Please sign in to comment.