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

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

Construct a new StatsTransformer.

Parameters
  • means (np.ndarray) – 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[, ...])

Create a StatsTransformer with stats from the given raster sources.

transform(chip[, channel_order])

Transform a chip.

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

Construct a new StatsTransformer.

Parameters
  • means (np.ndarray) – 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 = 0.1, max_stds: float = 3.0) StatsTransformer[source]#

Create a StatsTransformer with stats from the given raster sources.

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

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

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

Returns

A StatsTransformer.

Return type

StatsTransformer

transform(chip: ndarray, channel_order: Optional[Sequence[int]] = None) ndarray[source]#

Transform a chip.

Transforms non-uint8 to uint8 values using raster_stats.

Parameters
  • chip (ndarray) – ndarray of shape [height, width, channels] This is assumed to already have the channel_order applied to it if channel_order is set. In other words, channels should be equal to len(channel_order).

  • channel_order (Optional[Sequence[int]]) – list of indices of channels that were extracted from the raw imagery.

Returns

[height, width, channels] uint8 numpy array

Return type

ndarray