RasterStats#

class RasterStats[source]#

Bases: object

Band-wise means and standard deviations.

Attributes

vars

Channel variances, if self.stds is set.

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

Constructor.

Parameters
  • means (Optional[np.ndarray]) – Band means. Defaults to None.

  • stds (Optional[np.ndarray]) – Band standard deviations. Defaults to None.

  • counts (Optional[np.ndarray]) – Band pixel counts (used to compute the specified means and stds). Defaults to None.

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[ndarray] = None, stds: Optional[ndarray] = None, counts: Optional[ndarray] = None)[source]#

Constructor.

Parameters
  • means (Optional[np.ndarray]) – Band means. Defaults to None.

  • stds (Optional[np.ndarray]) – Band standard deviations. Defaults to None.

  • counts (Optional[np.ndarray]) – Band pixel counts (used to compute the specified means and stds). Defaults to None.

compute(raster_sources: Sequence[RasterSource], sample_prob: Optional[float] = None, chip_sz: int = 300, stride: Optional[int] = None, nodata_value: Optional[float] = 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
  • Sequence['RasterSource'] (raster_sources) – List of RasterSources.

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

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

  • raster_sources (Sequence[RasterSource]) –

  • chip_sz (int) –

  • stride (Optional[int]) –

Return type

None

compute_from_chips(chips: Iterable[ndarray], running_mean: Optional[ndarray] = None, running_var: Optional[ndarray] = None, running_count: Optional[ndarray] = None) Union[Tuple[None, None, None], Tuple[ndarray, ndarray, ndarray]][source]#

Compute running mean and var from chips in stream.

Parameters
Return type

Union[Tuple[None, None, None], Tuple[ndarray, ndarray, ndarray]]

compute_from_pixels(pixels: ndarray, running_mean: Optional[ndarray] = None, running_var: Optional[ndarray] = None, running_count: Optional[ndarray] = None) Tuple[ndarray, ndarray, ndarray][source]#

Update running mean and var from pixel values.

Parameters
Return type

Tuple[ndarray, ndarray, ndarray]

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

Load stats from file.

Parameters

stats_uri (str) –

Return type

RasterStats

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: Optional[ndarray]#

Channel variances, if self.stds is set.