Einx¶
geoops.einx wraps einx — universal
einstein-notation tensor ops — as carrier-aware Operators. einx is a core
dependency: the same notation also powers the internal linear algebra of
the Tier-A primitives (covariance products, matched-filter scoring, PCA
projections, channel-order flips).
The spatial-survival rule decides what a pattern does to geospatial
metadata: if the output expression ends in the bare spatial axes
(... y x) and neither axis is composed anywhere in the pattern, a
GeoArray input returns a GeoArray (transform/CRS/fill preserved).
Any pattern that consumes, moves, or recomposes a spatial axis returns a
plain np.ndarray. SpatialPool is the deliberate exception — it
rescales the transform to the pooled grid.
Nodata. SpatialPool and PerBandReduce exclude invalid pixels
(non-finite, or equal to the carrier's fill_value in any band)
with NaN-aware reductions; SpatialPool writes its output fill into
blocks with no valid pixel (NaN for float results of an integer input,
otherwise the input's fill). Einx runs einx on the raw values, and a
value-changing, spatially-surviving output declares an explicit fill
(NaN for floats, False for booleans) written into the carrier's
invalid pixels. As everywhere in geotoolz, fill_value=0 means 0 is
nodata — set it to None or NaN when 0 is real data.
All presets broadcast over leading axes, so (T, C, H, W) time stacks
work too.
import geoops as gz
mean_map = gz.Einx(op="mean", pattern="c y x -> y x") # GeoArray -> GeoArray
band_stats = gz.PerBandReduce(reduce="std") # GeoArray -> (C,) ndarray
scores = gz.Einx(op="dot", pattern="band y x, sig band -> sig y x")
coarse = gz.SpatialPool(reduce="mean", factor=4) # transform rescaled
pooled = gz.Einx(op="mean", pattern="c (y py) (x px) -> c y x", op_kwargs={"py": 2, "px": 2})
geoops.einx.Einx
¶
Bases: Operator
Apply any einx operation to the carrier, geo-aware.
The pattern is analyzed once at construction with
geoops.einx._src.array.spatial_survives: when the trailing two
axes of both the output and the carrier's (first) input expression
are the bare spatial axes (default ("y", "x")) and no spatial
axis is composed anywhere, a GeoArray input comes
back as a GeoArray (transform / CRS propagated via
wrap_like, with fresh attrs whose per-band keys are dropped
when the band count changes); otherwise the result is a plain
np.ndarray. Plain-array input always returns a plain array.
Nodata: einx runs on the raw values. For a spatially-surviving output the pixel grid is unchanged, so:
"id"/"rearrange"only move values and inherit the carrier'sfill_value;- every other op changes the values, so the output gets an explicit
fill —
NaNfor float outputs,Falsefor boolean outputs, the input's fill for integer outputs when representable (else none) — written into every pixel that was invalid in the carrier (non-finite or equal to its fill in any band; seegeoops._src.valid). Withfill_value=0on the input, 0 is nodata (georeader's convention); passNoneorNaNon the carrier when 0 is real data.
Non-surviving outputs (e.g. "c y x -> c") are raw einx results:
a fill or NaN pixel enters the statistic. Use PerBandReduce /
SpatialPool for nodata-aware reductions.
Multi-input patterns work by passing extra arrays at call time — the first argument is the carrier, the rest are unwrapped to plain arrays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
op
|
str
|
einx entry-point name ( |
required |
pattern
|
str
|
The einx pattern string. |
required |
spatial_axes
|
tuple[str, str] | list[str]
|
Names of the (row, column) axes in the pattern.
Default |
('y', 'x')
|
op_kwargs
|
Mapping[str, Any] | Iterable[Any] | None
|
Extra keyword arguments forwarded to the einx call:
axis sizes named in |
None
|
**axis_kwargs
|
Any
|
Shorthand for |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Unsupported |
Examples:
>>> import geoops as gz
>>> # Per-pixel mean over bands; spatial survives -> GeoArray out.
>>> mean_map = gz.Einx(op="mean", pattern="c y x -> y x")
>>> # Matched-filter scoring against a signature matrix.
>>> score = gz.Einx(op="dot", pattern="band y x, sig band -> sig y x")
>>> scores = score(reflectance_gt, signatures)
>>> # Axis sizes for composed axes.
>>> pooled = gz.Einx(
... op="mean",
... pattern="c (y py) (x px) -> c y x",
... op_kwargs={"py": 2, "px": 2},
... )
Source code in packages/geotoolz-ops/src/geoops/einx/_src/operators.py
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 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 | |
geoops.einx.CHWtoHWC
¶
Bases: Einx
Reorder a (..., C, H, W) cube to channels-last (..., H, W, C).
Display / ML-interop helper (matplotlib imshow, most vision
frameworks expect channels-last). Leading axes (e.g. time) are kept.
The output is always a plain np.ndarray: with channels trailing,
the spatial axes no longer sit in the trailing-two positions a
GeoArray requires.
Examples:
Source code in packages/geotoolz-ops/src/geoops/einx/_src/operators.py
geoops.einx.HWCtoCHW
¶
Bases: Einx
Reorder a channels-last (..., H, W, C) array to (..., C, H, W).
Inverse of CHWtoHWC, for bringing external channels-last arrays
into geotoolz's channel-first convention. Input is typically a
plain array (channels-last carriers can't be GeoArrays), and the
output is a plain array — rewrapping needs a reference carrier's
metadata, e.g. wrap_like(reference_gt, out) downstream.
Examples:
Source code in packages/geotoolz-ops/src/geoops/einx/_src/operators.py
geoops.einx.PerBandReduce
¶
Bases: Operator
Reduce each band over its spatial extent to one scalar, ignoring nodata.
(C, H, W) in, (C,) out; (T, C, H, W) in, (T, C) out
(einx pattern "... c y x -> ... c") — band-wise statistics for QA
summaries, normalization fitting, or feature vectors. Spatial
structure is consumed, so the result is always a plain float
np.ndarray regardless of carrier.
NaN-aware, like geoops.normalize.PerBandStats: invalid pixels —
non-finite or equal to the carrier's fill_value in any
band (per frame for 4-D input; see geoops._src.valid) — are
excluded from every band's statistic. A band (frame) with no valid
pixel reduces to NaN. With fill_value=0 on the input,
0 is nodata (georeader's convention); pass None or NaN on the
carrier when 0 is real data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reduce
|
str
|
einx reduction name: |
'mean'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Unsupported |
Examples:
Source code in packages/geotoolz-ops/src/geoops/einx/_src/operators.py
geoops.einx.SpatialPool
¶
Bases: Operator
Downsample the spatial grid by integer factors, updating the transform.
Non-overlapping block pooling via einx
("... (y py) (x px) -> ... y x"), so (H, W), (C, H, W) and
(T, C, H, W) inputs all work. This is the one einx preset that
changes the geotransform rather than preserving or dropping it: a
GeoArray input returns a GeoArray whose pixel size is
scaled by the pool factors (same origin, coarser grid, fresh
attrs). Plain-array input returns a plain array.
Nodata: invalid pixels — non-finite, or equal to the carrier's
fill_value in any band (per frame for 4-D input; see
geoops._src.valid) — are excluded with a NaN-aware reduction, so
a block pools only its valid pixels. A block with no valid pixel is
written as the output fill, which the output also declares: NaN
for float results of an integer input or of an input without a fill,
otherwise the input's fill_value. With
fill_value=0 on the input, 0 is nodata (georeader's
convention); pass None or NaN on the carrier when 0 is real
data.
Output dtype: "max" / "min" keep an integer input's dtype;
every other reduction returns floats (float64 for integer
input, the input's float dtype otherwise).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reduce
|
str
|
einx reduction applied per block: |
'mean'
|
factor
|
int | tuple[int, int] | list[int]
|
Pool factor — one int for square blocks or a
|
2
|
Raises:
| Type | Description |
|---|---|
TypeError
|
|
ValueError
|
|
Examples:
Source code in packages/geotoolz-ops/src/geoops/einx/_src/operators.py
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 | |
Pattern analysis¶
These helpers are pure string processing over pattern text.
geoops.einx.spatial_survives(pattern, spatial_axes=('y', 'x'))
¶
Return whether an einx pattern preserves the carrier's spatial grid.
True iff the output expression's trailing two top-level tokens are
exactly the bare spatial_axes names in order, AND the carrier's
input expression (the first comma-separated part of the input side)
ends in those same bare names in the same order, AND neither
spatial axis appears inside a composed / bracketed group anywhere
in the pattern (composition splits or merges the axis, changing its
size and invalidating the geotransform).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
An einx pattern string. |
required |
spatial_axes
|
tuple[str, str]
|
The (row, column) axis names, default |
('y', 'x')
|
Returns:
| Type | Description |
|---|---|
bool
|
Whether a |
bool
|
for the output. |
Examples:
>>> spatial_survives("c y x -> y x")
True
>>> spatial_survives("c y x -> x y") # transposed
False
>>> spatial_survives("c x y -> c y x") # input-side transpose
False
>>> spatial_survives("y x c -> c y x") # channels-last input
False
>>> spatial_survives("c y x -> y x c") # channels-last
False
>>> spatial_survives("c (y py) (x px) -> c y x") # pooled: resized
False
>>> spatial_survives("band y x, sig band -> sig y x")
True
Source code in packages/geotoolz-ops/src/geoops/einx/_src/array.py
geoops.einx.output_axes(pattern)
¶
Return the top-level axis tokens of a pattern's output expression.
The output expression is everything after the last ->.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
An einx pattern string, e.g. |
required |
Returns:
| Type | Description |
|---|---|
list[str] | None
|
The list of top-level output tokens, or |
list[str] | None
|
pattern has no explicit |
list[str] | None
|
cannot be analyzed statically here). |
Raises:
| Type | Description |
|---|---|
ValueError
|
On unbalanced brackets in the pattern. |