StatsTransformer#

class StatsTransformer[source]#

Bases: RasterTransformer

Transforms non-uint8 to uint8 values using channel statistics.

This works as follows:

  • Convert pixel values to z-scores using channel means and standard deviations.

  • Clip z-scores to the specified number of standard deviations (default 3) on each side.

  • Scale values to 0-255 and cast to uint8.

This transformation is not applied to NODATA pixels (assumed to be pixels with all values equal to zero).

Attributes

stats

Current statistics as a RasterStats instance.

__init__(means: Sequence[float], stds: Sequence[float], max_stds: float = 3.0)[source]#

Construct a new StatsTransformer.

Parameters:
  • means (Sequence[float]) – Channel means.

  • means – Channel standard deviations.

  • max_stds (float) – Number of standard deviations to clip the distribution to on both sides. Defaults to 3.

  • stds (Sequence[float]) –

Methods

__init__(means, stds[, max_stds])

Construct a new StatsTransformer.

from_raster_sources(raster_sources[, ...])

Build with stats from the given raster sources.

from_raster_stats(stats[, channel_order])

Build with stats from a RasterStats instance.

from_stats_json(uri[, channel_order])

Build with stats from a JSON file.

get_out_channels(in_channels)

Number of channels in output of transform().

get_out_dtype(in_dtype)

dtype of the output of transform().

transform(chip)

Clip values to +-max_stds std devs and convert to uint8 (0-255).

__init__(means: Sequence[float], stds: Sequence[float], max_stds: float = 3.0)[source]#

Construct a new StatsTransformer.

Parameters:
  • means (Sequence[float]) – Channel means.

  • means – Channel standard deviations.

  • max_stds (float) – Number of standard deviations to clip the distribution to on both sides. Defaults to 3.

  • stds (Sequence[float]) –

classmethod from_raster_sources(raster_sources: list['RasterSource'], sample_prob: float | None = 0.1, max_stds: float = 3.0, chip_sz: int = 300) Self[source]#

Build with stats from the given raster sources.

Parameters:
  • raster_sources (list[RasterSource]) – List of raster sources to compute stats from.

  • sample_prob (float) – Fraction of each raster to sample for computing stats. For details see docs for RasterStats.compute(). Defaults to 0.1.

  • max_stds (float) – Number of standard deviations to clip the distribution to on both sides. Defaults to 3.

  • chip_sz (int) –

Returns:

A StatsTransformer.

Return type:

StatsTransformer

classmethod from_raster_stats(stats: RasterStats, channel_order: list[int] | None = None, **kwargs) Self[source]#

Build with stats from a RasterStats instance.

The file is expected to be in the same format as written by RasterStats.save().

Parameters:
Returns:

A StatsTransformer.

Return type:

Self

classmethod from_stats_json(uri: str, channel_order: list[int] | None = None, **kwargs) Self[source]#

Build with stats from a JSON file.

The file is expected to be in the same format as written by RasterStats.save().

Parameters:
  • uri (str) – URI of the JSON file.

  • channel_order (list[int] | None) – Channel order to apply to the means and stds in the file.

  • **kwargs – Extra args for __init__().

Returns:

A StatsTransformer.

Return type:

Self

get_out_channels(in_channels: int) int#

Number of channels in output of transform().

Parameters:

in_channels (int) –

Return type:

int

get_out_dtype(in_dtype: dtype) dtype[source]#

dtype of the output of transform().

Parameters:

in_dtype (dtype) –

Return type:

dtype

transform(chip: ndarray) ndarray[source]#

Clip values to +-max_stds std devs and convert to uint8 (0-255).

Parameters:

chip (ndarray) – Array of shape (…, H, W, C).

Returns:

Array of shape (…, H, W, C)

Return type:

ndarray

property stats: RasterStats#

Current statistics as a RasterStats instance.