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
Current statistics as a
RasterStats
instance.- __init__(means: Sequence[float], stds: Sequence[float], max_stds: float = 3.0)[source]#
Construct a new StatsTransformer.
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, **kwargs)Build with stats from a
RasterStats
instance.from_stats_json
(uri, **kwargs)Build with stats from a JSON file.
transform
(chip[, channel_order])Transform a chip.
- __init__(means: Sequence[float], stds: Sequence[float], max_stds: float = 3.0)[source]#
Construct a new StatsTransformer.
- classmethod from_raster_sources(raster_sources: List[RasterSource], sample_prob: Optional[float] = 0.1, max_stds: float = 3.0) StatsTransformer [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, 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
- classmethod from_raster_stats(stats: RasterStats, **kwargs) StatsTransformer [source]#
Build with stats from a
RasterStats
instance.The file is expected to be in the same format as written by
RasterStats.save()
.- Parameters
stats (RasterStats) – A
RasterStats
instance with non-None stats.**kwargs – Extra args for
__init__()
.
- Returns
A StatsTransformer.
- Return type
- classmethod from_stats_json(uri: str, **kwargs) StatsTransformer [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.
**kwargs – Extra args for
__init__()
.
- Returns
A StatsTransformer.
- Return type
- 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
- property stats: RasterStats#
Current statistics as a
RasterStats
instance.