Deltares / Deltares/imod-python
zonal_aggregate_raster and zonal_aggregate_polygon may give unexpected/unusable results
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 41
- Forks
- 12
- Avg merge
- 21h 8m
- Merged PRs (30d)
- 1
Description
In GitLab by @Huite on Nov 18, 2023, 17:38
I got this report from Jacco.
If the resolution is small enough in these methods, it will result in duplicate entries in the output:
imod.prepare.zonal_aggregate_raster(peilvlakken_shp, 'Id', da_1, 1.0, 'mean')
Id area aggregated
0 1 1066795.0 1.0
1 2 279899.0 1.0
2 3 1162011.0 1.0
3 4 1491911.0 1.0
4 5 258044.0 1.0
5 6 419897.0 1.0
6 7 89131.0 1.0
7 11 593048.0 1.0
imod.prepare.zonal_aggregate_raster(peilvlakken_shp, 'Id', da_1, 0.5, 'mean')
Id area aggregated
0 2 94655.00 1.0
1 11 294333.25 1.0
0 1 1066068.50 1.0
1 2 183242.75 1.0
2 3 1162725.75 1.0
3 4 1489183.75 1.0
4 5 257467.75 1.0
5 6 420808.75 1.0
6 7 89104.25 1.0
7 11 214502.00 1.0
0 11 59612.75 1.0
0 11 23599.50 1.0
The reason is relatively simple, internally the data is chunked and processed out-of-core using dask.
raster_chunks, _, _ = _create_chunks(raster, resolution, chunksize)
collection = [
dask.delayed(_zonal_aggregate_raster)(path, column, resolution, chunk, method)
for chunk in raster_chunks
]
result = dask.compute(collection)[0]
return pd.concat(result)
Obviously, if a polygon is present in more than one chunk, the ID will be present more than once in the result, which is then just concatenated.
This isn't too big a deal for methods such as sum or mean, because all the information is available to do another reduction afterwards.
But for something like mode, it won't work, since that requires ALL values.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with zonal_aggregate_raster and zonal_aggregate_polygon, then read the shown _create_chunks, _zonal_aggregate_raster, and dask.compute flow. Reproduce the duplicate IDs with the supplied resolution examples and determine how chunk results should be reduced, including mode. Done means polygons spanning chunks produce one usable result with correct aggregation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- data-engineering
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100