Composite contrast stretch#
A composite turns three raster bands into one image by stretching each channel into [0, 1]. Every backend
that draws one needs the same three pieces — the per-channel bounds, the stretch itself, and the argument
checks around them — so they live in digitalearth.base.stretch and both the matplotlib and HoloViz tiers
consume them. Keeping one copy is what stops the tiers drifting; they had already grown two stretches that
disagreed about an all-nodata channel.
The bounds are ordinary numbers, which is what makes a stable time-lapse possible. Derived per call, each
frame gets its own black and white point and the clip pumps as the scene brightens and darkens; derive them
once over the whole stack and hand the same pair to every frame, and the sequence holds one stretch.
Map.animate does exactly that for kind="rgb_composite" / "hsv_composite", and InteractiveMap.rgb
accepts the same limits so a HoloViz sequence can be held the same way.
A channel with no finite cell reports (nan, nan) rather than raising: that means "no bound for this
channel", and stretch_to_unit answers it from the frame in hand instead of inventing a span.
digitalearth.base.stretch.channel_limits(stack)
#
Return the per-channel 2-98 percentile (lo, hi) of an (rows, cols, n) stack.
These are the bounds :func:stretch_to_unit derives on every call. Computing them once over a
whole animation stack and passing them back in freezes the stretch, so a composite time-lapse does not
pulse as each frame re-derives its own black and white point.
A channel with no finite cell at all (fully nodata, or all inf) has no percentile to take and
yields (nan, nan) rather than a warning; callers treat that as "this channel contributes no
bound" — see :meth:~digitalearth.static.builders.animation.AnimationMixin._stack_channel_limits, which
drops it, and :func:stretch_to_unit, which falls back rather than dividing by it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stack
|
ndarray
|
An |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[float, float]]
|
One |
Raises:
| Type | Description |
|---|---|
ValueError
|
when |
Examples:
- Bound each channel of a stack whose channels are scaled copies of one another:
- A channel with no finite cell reports
(nan, nan)rather than raising or warning, and leaves its neighbours' bounds intact:
See Also
stretch_to_unit: Applies these bounds (or derives its own when none are given). digitalearth.static.map.Map.animate: Combines these across a whole stack so every frame of a composite animation shares one stretch.
Source code in src/digitalearth/base/stretch.py
digitalearth.base.stretch.stretch_to_unit(stack, limits=None)
#
Per-channel contrast stretch of an (rows, cols, n) stack into [0, 1].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stack
|
ndarray
|
The |
required |
limits
|
ChannelLimits | None
|
Optional precomputed |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
The stretched stack: same shape, |
Raises:
| Type | Description |
|---|---|
ValueError
|
when |
Examples:
- Each channel spans the full range on its own percentiles:
>>> import numpy as np >>> from digitalearth.base.stretch import stretch_to_unit >>> stack = np.dstack([np.arange(100.0).reshape(10, 10) * scale for scale in (1, 2, 3)]) >>> out = stretch_to_unit(stack) >>> float(out.min()), float(out.max()) (0.0, 1.0) >>> [round(float(out[..., i].mean()), 3) for i in range(3)] [0.5, 0.5, 0.5] - One shared set of limits keeps a real brightness difference visible, which is what holds an
animation steady; each channel's own percentiles would erase it:
>>> import numpy as np >>> from digitalearth.base.stretch import channel_limits, stretch_to_unit >>> bright = np.dstack([np.arange(100.0).reshape(10, 10) for _ in range(3)]) >>> dim = bright * 0.4 >>> shared = channel_limits(bright) >>> round(float(stretch_to_unit(bright, shared).mean()), 3) 0.5 >>> round(float(stretch_to_unit(dim, shared).mean()), 3) 0.188 >>> round(float(stretch_to_unit(dim).mean()), 3) 0.5 - A channel with no bound to freeze falls back to that frame's own, rather than an invented span:
Source code in src/digitalearth/base/stretch.py
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
digitalearth.base.stretch.require_three_bands(caller, bands)
#
Reject a composite band list that is not exactly three long.
A composite maps its bands to three channels (R/G/B or H/S/V), so any other count is a caller mistake.
Checked up front because the failure otherwise surfaces deep inside the renderer: from an animation it
lands after the whole stack has been scanned, as a bare IndexError raised while matplotlib's writer
is already unwinding, with the real complaint buried in a chained traceback.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
caller
|
str
|
The method name to name in the message (e.g. |
required |
bands
|
Sequence[int]
|
The band indices to check. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
when |
Examples:
- Three bands pass silently:
- Any other count names the caller, the count and the value it got:
- A generator is consumed once, so it is materialised before it is counted:
Source code in src/digitalearth/base/stretch.py
ChannelLimits#
The type the bounds travel as: a sequence of one (lo, hi) pair per channel, in channel order. A
non-finite pair means no bound for this channel — stretch_to_unit answers that from the frame in
hand rather than inventing a span.
DEFAULT_COMPOSITE_BANDS#
The (1, 2, 3) a composite maps to its channels when the caller names none. Defined once here so the
renderers and the animation scan cannot drift apart about what a bare composite means.