RasterStats#

class RasterStats[source]#

Bases: object

Band-wise means and standard deviations.

Attributes

vars

Channel variances, if self.stds is set.

__init__(means: Optional[Sequence[float]] = None, stds: Optional[Sequence[float]] = None, counts: Optional[Sequence[float]] = None)[source]#

Constructor.

Parameters

Methods

__init__([means, stds, counts])

Constructor.

compute(raster_sources[, sample_prob, ...])

Compute the mean and stds over all the raster_sources.

compute_from_chips(chips[, running_mean, ...])

Compute running mean and var from chips in stream.

compute_from_pixels(pixels[, running_mean, ...])

Update running mean and var from pixel values.

load(stats_uri)

Load stats from file.

save(stats_uri)

Save stats to file.

to_dict()

__init__(means: Optional[Sequence[float]] = None, stds: Optional[Sequence[float]] = None, counts: Optional[Sequence[float]] = None)[source]#

Constructor.

Parameters
compute(raster_sources: Sequence[RasterSource], sample_prob: float | None = None, chip_sz: int = 300, stride: int | None = None, nodata_value: float | None = 0) None[source]#

Compute the mean and stds over all the raster_sources.

This ignores NODATA values if nodata_value is not None.

If sample_prob is set, then a subset of each scene is used to compute stats which speeds up the computation. Roughly speaking, if sample_prob=0.5, then half the pixels in the scene will be used. More precisely, the number of chips is equal to sample_prob * (width * height / 300^2), or 1, whichever is greater. Each chip is uniformly sampled from the scene with replacement. Otherwise, it uses a sliding window over the entire scene to compute stats.

Parameters
  • raster_sources (Sequence[RasterSource]) – List of RasterSources.

  • sample_prob (float | None) – Pixel sampling probability. See notes above. Defaults to None.

  • nodata_value (float | None) – NODATA value. If set, these pixels will be ignored when computing stats.

  • chip_sz (int) –

  • stride (int | None) –

Return type

None

compute_from_chips(chips: Iterable[ndarray], running_mean: numpy.ndarray | None = None, running_var: numpy.ndarray | None = None, running_count: numpy.ndarray | None = None) tuple[None, None, None] | tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray][source]#

Compute running mean and var from chips in stream.

Parameters
Return type

tuple[None, None, None] | tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]

compute_from_pixels(pixels: ndarray, running_mean: numpy.ndarray | None = None, running_var: numpy.ndarray | None = None, running_count: numpy.ndarray | None = None) tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray][source]#

Update running mean and var from pixel values.

Parameters
Return type

tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]

classmethod load(stats_uri: str) Self[source]#

Load stats from file.

Parameters

stats_uri (str) –

Return type

Self

save(stats_uri: str) None[source]#

Save stats to file.

Parameters

stats_uri (str) –

Return type

None

to_dict() dict[source]#
Return type

dict

property vars: numpy.ndarray | None#

Channel variances, if self.stds is set.