xarray-contrib / xvec

Vector data cubes for Xarray
https://xvec.readthedocs.io
MIT License
93 stars 9 forks source link

set output data type in zonal_stats #76

Closed benmcdermott closed 3 weeks ago

benmcdermott commented 3 weeks ago

Hi there, I've been testing zonal_stats on a large raster containing decimal values. zonal_stats wants to allocate a large array with type float64 - which is unnecessary as my input raster is float32. Would it be possible to set this dtype throughout the calculation as to avoid overuse of memory?

martinfleis commented 3 weeks ago

Which method for zonal stats are you using? The default?

benmcdermott commented 3 weeks ago

Yes the default: raster.xvec.zonal_stats(...)

martinfleis commented 3 weeks ago

It has actually nothing to do with the input dtype, that is preserved to the aggregated array. We need to create a rasterised version of geometries and that array is, due to masking of empty areas, being generated as float64. In #77 I am switching to float32, which should help a bit. If the memory is a concern, method="iterate" is significantly less demanding but slower.

benmcdermott commented 2 weeks ago

I guess the reason I am testing your package rather than using rasterio or rasterstats is the vectorized method hoping for performance improvement. So would switching to the slower iterate version would be similar to packages I already use?

martinfleis commented 2 weeks ago

So would switching to the slower iterate version would be similar to packages I already use?

Performance-wise, the method="iterate" it is likely to be similar to rasterstats, tough I haven't compared that directly.

Try using the dev version of xvec installed from main, that may solve the issues you're having. But it is to be noted that the performance here comes certainly at a memory cost.