raster-vision icon indicating copy to clipboard operation
raster-vision copied to clipboard

Compute RasterStats from transformed RasterSource

Open bdevoghel opened this issue 1 year ago • 1 comments

🚀 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.

bdevoghel avatar Jun 25 '24 10:06 bdevoghel

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.

AdeelH avatar Jun 26 '24 14:06 AdeelH

Hey! I ended up making get_chip() the default. The backward-compatibility considerations made this a little tricky so I decided to handle it myself. But thanks again for the issue and I hope you will continue to contribute more in the future!

AdeelH avatar Aug 07 '24 13:08 AdeelH