azavea / raster-vision

An open source library and framework for deep learning on satellite and aerial imagery.
https://docs.rastervision.io
Other
2.03k stars 379 forks source link

Compute RasterStats from transformed RasterSource #2177

Open bdevoghel opened 2 weeks ago

bdevoghel commented 2 weeks ago

🚀 Feature

When computing RasterStats from a RasterSource (eg. in my case, to create a StatsTransformer), the chips are retrieved as raw chips. It would be great to allow to get transformed chips too.

Motivation

I'd like to obtain a StatsTransformer from a RasterSource which has raster_transforms, in order to normalize (with this StatsTransformer) the RasterSource. Not allowing for this implies mathematical limitations, as normalization and transformation are not commutative.

Pitch

raster_stats.get_chip() should have an additional (optional) parameter to define if the chip to get is raw or not.

def get_chip(raster_source: 'RasterSource',
             window: 'Box',
             nodata_value: Optional[float] = 0
             get_raw: Optional[bool] = True) -> Optional[np.ndarray]:
    """Return chip or None if all values are NODATA."""
    if get_raw:
        chip = raster_source.get_raw_chip(window).astype(float)
    else:
        chip = raster_source.get_chip(window).astype(float)
    ...

This option should be propagated in methods using get_chip() to allow for its use, this should be straightforward.

Alternatives

No alternatives are available at the moment, as far as I know.

Additional context

I would be available to implement a fix in a PR if this is validated.

AdeelH commented 1 week ago

Thanks for the comprehensive issue! This makes perfect sense to me. The only thing I'm debating is whether we should get rid of the raw chip sampling entirely and only have RasterStats sample transformed chips since that seems more intuitive to me. The tricky thing there would be ensuring channel_order is handled correctly in StatsTransformer and ensuring that we don't break backward compatibility.

Let me think about it some more and get back to you about a PR.