mortie.batch
Bulk (plural) operators over morton sets, MOCs and geometry columns. Every
function here is the batch twin of a scalar that lives elsewhere in the
package: one call carries a whole ragged column across the Python/Rust
boundary, and element i of the result is bit-identical to the scalar applied
to element i alone. Consolidated by arity (issue #170) — a different
axis from the domain split the rest of the package is organised on — with a
See Also on each side of every scalar/plural pair. The pyarrow skins
(mortie.arrow.from_wkbs, mortie.arrow.polygons_to_morton_mocs) stay in
mortie.arrow; the names stay flat on the package
(mortie.from_wkbs, mortie.children_of).
Bulk (plural) operators over morton sets, MOCs and geometry columns.
Every function here is the batch twin of a scalar that lives elsewhere in
the package: one call carries a whole ragged column across the Python/Rust
boundary, releases the GIL, and lets Rust parallelize across the elements, so
the per-call fixed cost that dominates a Python loop over half a million
footprints is paid once. Element i of the result is bit-identical to the
scalar applied to element i alone -- the batch is a throughput surface, not
a second semantics.
Consolidated here by arity (issue #170), which is a different axis from the
domain split the rest of the package is organised on (issues #156 / #159): the
plural twins used to sit beside their scalars in :mod:mortie.coverage,
:mod:mortie.moc, :mod:mortie.orders and :mod:mortie.geometry, so "what is
batched?" had four answers and every new twin landed in whichever of those
modules was furthest from the size aim. The scalar/plural pair is kept
navigable by a See Also on each side: every plural below names its scalar
by module path, and every scalar's docstring points back at its plural here
(issue #170).
The Rust kernels stay split by domain (coverage/batch.rs, moc/batch.rs,
decimal_morton/batch.rs, wkb/batch.rs), so this module deliberately does
not mirror the Rust tree the way :mod:mortie.orders and
:mod:mortie.convert do: the Python surface is organised for callers and the
Rust for kernels, and the two need not be 1:1.
The pyarrow skin is a third axis: :func:mortie.arrow.from_wkbs and
:func:mortie.arrow.polygons_to_morton_mocs take pyarrow columns and stay in
:mod:mortie.arrow (issue #154). The names here stay flat on the package
(mortie.from_wkbs, mortie.children_of): this module is where they live,
not how they are spelled.
polygons_to_morton_mocs(lats, lons, offsets, order=18, tolerance=None, max_cells=None, normalize=True)
Compute MOC coverage of many independent polygons in one call.
The batch sibling of :func:morton_coverage_moc (issue #153): the ragged
polygon set crosses the Python/Rust boundary once, the GIL is released
for the whole batch, and Rust parallelizes across polygons — so the
per-call fixed cost that dominates a Python loop over half a million
footprints is paid once. Identity-preserving: result i is exactly the
cover of input polygon i (unlike the multipart form of
:func:morton_coverage_moc, which unions its rings into one cover). The
plural MOCs in the name marks that many→many contract — one MOC per
input polygon — against the many→one union of the multipart form.
Polygons are covered in chunks and each chunk is copied into the ragged
output as it lands, so peak memory is about the returned values array
plus one chunk of in-flight covers — not the ~2.5x of holding every
polygon's cover to concatenate at the end.
Input and output are ragged arrays in arrow list layout: polygon i is
lats[offsets[i]:offsets[i+1]] / lons[offsets[i]:offsets[i+1]], and
its MOC is values[out_offsets[i]:out_offsets[i+1]] in the result —
byte-identical to morton_coverage_moc on that ring alone.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lats
|
array_like
|
Flat |
required |
lons
|
array_like
|
Flat |
required |
offsets
|
array_like
|
|
required |
order
|
int
|
Finest HEALPix order (1-29), shared by every polygon. Default 18. |
18
|
tolerance
|
float
|
Stop refining a boundary cell once its angular radius (in degrees)
drops to this value — exactly :func: |
None
|
max_cells
|
int
|
Best-first cell budget per polygon — exactly
:func: |
None
|
normalize
|
bool
|
Ring-orientation handling, identical in meaning to
:func: |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
values |
ndarray
|
All polygons' morton MOC words concatenated ( |
out_offsets |
ndarray
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Fail-fast, naming the lowest-index offending polygon (e.g.
|
Warns:
| Type | Description |
|---|---|
UserWarning
|
If |
See Also
mortie.coverage.morton_coverage_moc : the scalar (one polygon / one ring-set) form.
Examples:
>>> import mortie, numpy as np
>>> lats = np.array([40.0, 50.0, 45.0, 10.0, 20.0, 15.0])
>>> lons = np.array([-120.0, -120.0, -110.0, -80.0, -80.0, -70.0])
>>> values, off = mortie.polygons_to_morton_mocs(lats, lons, [0, 3, 6], order=6)
>>> first = values[off[0]:off[1]] # MOC of the first triangle
Source code in mortie/batch.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | |
from_wkbs(blobs, order=18, tolerance=None, max_cells=None, normalize=True)
Cover many WKB blobs with one call -- ragged MOCs out, no backend.
The batch sibling of :func:from_wkb (issue #157) and the plural twin its
name marks: one MOC per input blob (many→many), against the many→one
union :func:from_wkb performs over the rings inside one blob. The
whole column crosses the Python/Rust boundary once, and Rust parses and
covers the blobs in parallel with the GIL released — so the per-call fixed
cost that dominates a Python loop over half a million footprints is paid
once. Result i is byte-identical to
from_wkb(blobs[i], order=order, moc=True, ...).
Memory: a chunk ends at 2048 blobs or 64 MiB, whichever comes first,
and peak is the returned values array plus one chunk of copied input
bytes (the copy is mandatory — a Python bytes buffer is GIL-bound and
cannot cross into the parallel region) plus one chunk of in-flight covers.
Neither the whole column's bytes nor every blob's cover is ever resident at
once, and that holds for every input spelling and every blob size:
non-bytes entries are coerced inside the chunk, so their copy dies with
it, and the byte budget stops 2048 fat geometries from making "one chunk"
mean gigabytes. Measured on the 555,867-blob ATL03 v007 corpus (276.7 MiB
of WKB, 167.3 MiB of result, order 6), peak growth over the resident
column:
========================== ========== =========
input spelling peak × result
========================== ========== =========
list[bytes] 178.7 MiB 1.07
numpy object array 179.4 MiB 1.07
hex str 178.9 MiB 1.07
bytearray 178.9 MiB 1.07
memoryview 179.4 MiB 1.07
uint8 array 221.2 MiB 1.32
arrow buffer slices 179.3 MiB 1.07
========================== ========== =========
On the fat end, 3,000 Antarctic-basin blobs (1.25 MiB each, a 3.7 GiB column) peak at 610-634 MiB, against 3,120 MiB when the chunk was bounded by blob count alone — and most of what is left is the in-flight cover work, not the copy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
blobs
|
sequence
|
One WKB/EWKB geometry per entry. Each entry takes exactly what
:func: |
required |
order
|
int
|
Finest HEALPix order (1-29), shared by every blob. Default 18. |
18
|
tolerance
|
float
|
Stop refining a boundary cell at this angular radius in degrees —
:func: |
None
|
max_cells
|
int
|
Per-blob cell budget, shared by every blob. A budget below some blob's representable floor is raised for that blob (soft target, as in the scalar path) and one summary warning is emitted. |
None
|
normalize
|
bool
|
Ring-orientation handling, identical in meaning to
:func: |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
values |
ndarray
|
Every blob's morton MOC words concatenated ( |
out_offsets |
ndarray
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Fail-fast, naming the lowest-index offending blob (e.g.
|
TypeError
|
Naming the offending index, for an entry that is neither a string nor a buffer of bytes. |
Notes
Two ordered gates, as in :func:mortie.polygons_to_morton_mocs: the input
contract is screened by a serial pre-pass over the whole sequence, then
the blobs are parsed and covered. Each gate reports its own lowest-index
offender, so a TypeError at a high index does surface ahead of a
malformed blob at a lower one — the pre-pass is an earlier gate, not a
competing one. The pre-pass validates without retaining
(_wkb_bytes(..., materialize=False)): it applies the identical accept
list — an invalid hex string is still caught here, ahead of any parse
error — but keeps the entries as they came, so a column in a non-bytes
spelling is not duplicated for the duration of the call.
Feeding a pyarrow column has a typed entry point of its own —
:func:mortie.arrow.from_wkbs (issue #163) — and that is what to call:
marrow.from_wkbs(column, order=...) returns exactly this pair. Do
not improvise the extraction. Four traps sit between a column and its
blobs. Three are silent — they yield different, valid-looking data
rather than an error: a parquet column reads back as a ChunkedArray,
which has no .buffers() at all; slice and take are zero-copy
metadata, so a chunk's buffers belong to the original array and must be
indexed from chunk.offset; and a large_binary column's offsets are
int64, not int32. The fourth is a wrong diagnosis: a null
entry spans zero bytes, so it arrives as an empty blob and this function
reports it as a truncated geometry rather than as a missing one. The
skin handles all four, and hands this function zero-copy memoryview
slices.
from_wkbs(column.to_pylist(), ...) is also right on every one of
those cases and needs no pyarrow-typed call, but its cost is real: on the
555,867-row ATL03 v007 WKB column (290.1 MB of payload) to_pylist()
peaks at ~322 MB of Python bytes objects — the per-object
overhead on top of the payload, plus the list — against ~112 MB for
the skin's views of the same column.
Warns:
| Type | Description |
|---|---|
UserWarning
|
If |
See Also
mortie.geometry.from_wkb : the scalar (one blob) form, and the input contract in full.
Examples:
>>> import mortie
>>> values, off = mortie.from_wkbs(wkb_column, order=8)
>>> first = values[off[0]:off[1]] # the first blob's MOC
Source code in mortie/batch.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 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 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 | |
mocs_to_orders(values, offsets, order, max_cells=_FLAT_COVER_WARN_THRESHOLD)
Densify many independent MOCs to a flat order in one call.
The batch sibling of :func:moc_to_order (issue #156): the ragged MOC set
crosses the Python/Rust boundary once, the GIL is released for the whole
batch, and Rust parallelizes across MOCs — so the per-call fixed cost that
dominates a Python loop over half a million covers is paid once. Result
i is byte-identical to :func:moc_to_order on MOC i alone.
Input and output are ragged arrays in arrow list layout, the same pair
:func:polygons_to_morton_mocs returns — so the two chain with no
marshalling::
cells, off = mortie.polygons_to_morton_mocs(lats, lons, off_in, order=8)
flat, flat_off = mortie.mocs_to_orders(cells, off, 8)
MOCs are densified in chunks and each chunk is copied into the ragged output
as it lands, so the per-MOC flat lists never all coexist — not the ~2.5x of
holding every one of them to concatenate at the end. Peak is then the
input copy + the result + one chunk: the binding copies values and
offsets before releasing the GIL (a borrowed numpy slice cannot cross
allow_threads), so the input is a full second resident array for the
duration. Densifying, that copy is noise — measured 1.16x of the returned
array for 100k MOCs at order 8 → 10, 1.18x for 250k at 11 → 11. Coarsening
it is the whole of the peak: 250k order-11 MOCs down to order 4 is a 5.3 MiB
result behind a 317.5 MiB peak (60x), essentially the 304.3 MiB input copy.
Size a worker off input + result, not the result alone.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
array_like
|
Flat |
required |
offsets
|
array_like
|
|
required |
order
|
int
|
Target HEALPix order (0-29) to densify to, shared by every MOC — the
same domain :func: |
required |
max_cells
|
int or None
|
Pre-emptive budget on the densified flat cell count, applied per
MOC exactly as :func: |
_FLAT_COVER_WARN_THRESHOLD
|
Returns:
| Name | Type | Description |
|---|---|---|
values |
ndarray
|
All MOCs' flat cells at |
out_offsets |
ndarray
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Fail-fast, naming the lowest-index offending MOC (e.g. |
See Also
mortie.moc.moc_to_order : the scalar (one MOC) form. polygons_to_morton_mocs : the batch coverer whose output feeds this verbatim.
Notes
Each slice comes back sorted and unique — the same guarantee
:func:moc_to_order gives — so a downstream np.unique over a slice is
redundant work, and np.searchsorted applies directly.
Examples:
>>> import mortie, numpy as np
>>> lats = np.array([40.0, 50.0, 45.0, 10.0, 20.0, 15.0])
>>> lons = np.array([-120.0, -120.0, -110.0, -80.0, -80.0, -70.0])
>>> mocs, off = mortie.polygons_to_morton_mocs(lats, lons, [0, 3, 6], order=6)
>>> flat, flat_off = mortie.mocs_to_orders(mocs, off, 6)
>>> first = flat[flat_off[0]:flat_off[1]] # flat cover of the first triangle
Source code in mortie/batch.py
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 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 | |
mocs_and(a, values, offsets)
Intersect one shared morton cover with many independent MOCs in one call.
The 1 x N broadcast of :func:mortie.moc.moc_and (issue #173): one shared
operand a against len(offsets) - 1 ragged MOCs, crossing the
Python/Rust boundary once with the GIL released while Rust parallelizes
across MOCs. Result i is byte-identical to
moc_and(a, values[offsets[i]:offsets[i+1]]). Beyond the boundary
amortization every batch twin shares, the broadcast has a structural win of
its own: the scalar normalizes and re-encodes both operands on every
call, so a Python loop rebuilds the shared operand's BMOC N times — here it
is built once and borrowed by every item. moc_and is commutative, so
it does not matter which side of your loop was "the AOI": pass either
operand as a.
An empty intersection keeps its slot (out_offsets[i] ==
out_offsets[i+1]), as does every slot when a is empty — so the ragged
output always agrees with :func:mocs_intersect on which items overlap.
There is deliberately no max_cells: the densify budget on
:func:mocs_to_orders guards an exponential blow-up term, while an
intersection is bounded by its inputs (the scalar set ops carry no budget
either).
Memory: MOCs are intersected in chunks and each chunk is copied into the
ragged output as it lands, so peak is the input copy + the result + one
chunk — the binding copies a, values and offsets before
releasing the GIL (a borrowed numpy slice cannot cross allow_threads),
so the input is a full second resident array for the duration, and because
an intersection result is never larger than its inputs, that copy is the
dominant term. Measured over 100k ~4-cell granule MOCs against an order-8
AOI cover (benchmarks/measure_mocs_and.py --mem): 3.8 MiB of ragged
input, a 1.1 MiB result, 5-9 MiB of peak-RSS growth across repeated runs
over the resident inputs for one mocs_and plus one mocs_intersect
call — the input copy plus the result plus a chunk. (ru_maxrss is a
high-water mark, so the growth is a noisy lower bound, not an exact
peak.) Size a worker off input + result, not the result alone.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
array_like
|
The shared morton cover ( |
required |
values
|
array_like
|
Flat |
required |
offsets
|
array_like
|
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
values |
ndarray
|
All intersections concatenated ( |
out_offsets |
ndarray
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Fail-fast, naming the lowest-index offending MOC: non-monotone or
out-of-bounds offsets, or offsets that do not exactly cover |
See Also
mortie.moc.moc_and : the scalar (one pair) form. mocs_intersect : the allocation-free predicate over the same broadcast. mocs_to_orders : densifies the surviving intersections, chaining on this output verbatim.
Examples:
>>> import mortie, numpy as np
>>> aoi = np.asarray(mortie.norm2mort([0], [0], 2), dtype=np.uint64)
>>> items = np.asarray(mortie.norm2mort([0, 200], [0, 0], 4), dtype=np.uint64)
>>> hit, off = mortie.mocs_and(aoi, items, [0, 1, 2])
>>> [int(off[i + 1] - off[i]) for i in range(2)] # item 0 overlaps, 1 not
[1, 0]
Source code in mortie/batch.py
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 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 | |
mocs_intersect(a, values, offsets)
Test which of many MOCs intersect one shared cover, materializing nothing.
The predicate twin of :func:mocs_and and the batch form of
:func:mortie.moc.moc_intersects (issue #173): out[i] is exactly
moc_intersects(a, values[offsets[i]:offsets[i+1]]), i.e. whether
:func:mocs_and's slot i would be non-empty — without building it.
Per item this is a range-overlap walk over the normalized covers, never a
BMOC build or result encode — no intersection is materialized, and the
only per-item allocation is that item's normalize scratch — and it
short-circuits on the first overlap, something the materializing form
cannot do. The shared operand is normalized and range-decoded once for
the whole batch. Proving a miss still takes the full walk, so the win
on non-overlapping items over moc_and(...).size is the skipped
build/encode/allocation, not the short-circuit.
Compaction-safe per item, by construction: each item is tested for
geometric overlap against a, so this cannot be (and is not) implemented
by intersecting once and testing membership — which would silently drop
dense regions that compact to a parent cell.
Memory: no results are materialized; peak is the input copy the binding
makes before releasing the GIL, one bool per MOC out, plus the
in-flight items' normalize scratch (one chunk at most).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
array_like
|
The shared morton cover ( |
required |
values
|
array_like
|
Flat |
required |
offsets
|
array_like
|
|
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Fail-fast, naming the lowest-index offending MOC: non-monotone or
out-of-bounds offsets, or offsets that do not exactly cover |
See Also
mortie.moc.moc_intersects : the scalar (one pair) form. mocs_and : materializes the intersections this only tests.
Examples:
>>> import mortie, numpy as np
>>> aoi = np.asarray(mortie.norm2mort([0], [0], 2), dtype=np.uint64)
>>> items = np.asarray(mortie.norm2mort([0, 200], [0, 0], 4), dtype=np.uint64)
>>> mortie.mocs_intersect(aoi, items, [0, 1, 2]).tolist()
[True, False]
Source code in mortie/batch.py
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 588 589 590 591 592 593 594 595 596 | |
common_ancestors(values, offsets)
Reduce many groups of morton words to their common ancestors in one call.
The batch sibling of :func:common_ancestor (issue #156): the whole ragged
group set crosses the Python/Rust boundary once, the GIL is released for
the batch, and Rust parallelizes across groups. Result i is
bit-identical to :func:common_ancestor on group i alone, the
single-word case included (it comes back verbatim, kind preserved).
Input is ragged in the arrow list layout :func:polygons_to_morton_mocs
and :func:mocs_to_orders use; the output is dense — one uint64 per
group — because the reduction is many→one per group, so there are no output
offsets to carry.
The consumer this exists for is a per-worker inner loop, not a one-off:
zagg's t-digest reduction runs for j in np.flatnonzero(~single): over
every multi-member centroid (stats/tdigest.py:198) — once per centroid,
per cell, per build and per fold, on every Lambda worker, at 65,536
cells per shard in the shipped ATL03 configuration. That module's own
docstring already calls it "the same O(n) Python-loop shape issue #279
removed".
Memory: the binding copies values and offsets before releasing the
GIL (a borrowed numpy slice cannot cross allow_threads), so peak is
the input copy + the result + one 64 KiB chunk of per-group outcomes
+ the reduction's own scratch. That last term is
:func:common_ancestor's internal buffer, 16 bytes per non-first word in
the group being reduced, held for that whole reduction and one per group in
flight — so it is min(threads, n_groups) * 16 * max_group_size bytes.
It scales with the largest single group, not with the total word count.
For small groups it is invisible and the input copy is the peak: over 5M
groups of 3 order-9 words, 152.6 MiB of input, a 38.1 MiB result, a 191.9
MiB peak — 1.01x the input + result model, 5.0x the result alone,
and under a kilobyte of scratch. For large groups it dominates: 40 groups
of 1M words peaks at 458.6 MiB against a 305.2 MiB model (1.50x), and a
single 20M-word group at 460.0 MiB against 152.7 MiB (3.01x) — in both
cases the excess is the scratch term to within 2 MiB. Size a worker off
input + result for many small groups, and off the largest group when
groups are large.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
array_like
|
Flat |
required |
offsets
|
array_like
|
|
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Fail-fast, naming the offending group (e.g. |
See Also
mortie.moc.common_ancestor : the scalar (one group) form. mortie.moc.split_base_cells : partitions a mixed-base-cell set into groups this accepts.
Examples:
Two groups of order-5 siblings reduce to their two order-4 parents:
>>> import mortie, numpy as np
>>> kids = np.concatenate([
... np.asarray(mortie.norm2mort([11 * 4 + s for s in range(4)], [0] * 4, 5)),
... np.asarray(mortie.norm2mort([7 * 4 + s for s in range(4)], [3] * 4, 5)),
... ])
>>> got = mortie.common_ancestors(kids, [0, 4, 8])
>>> [int(got[0]), int(got[1])] == [
... int(mortie.norm2mort(11, 0, 4)), int(mortie.norm2mort(7, 3, 4))
... ]
True
Source code in mortie/batch.py
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 | |
children_of(words, order, max_cells=None)
Refine many parent words to their children at order, in one call.
The batch sibling of :func:generate_morton_children (issue #156), whose
wrapper coerces its input to a single parent: the whole parent array
crosses the Python/Rust boundary once, the GIL is released for the
batch, and Rust parallelizes across parents. Row i is bit-identical to
:func:generate_morton_children on words[i] alone.
Every parent must sit at one shared order p <= order, so each yields
exactly 4**d children for d = order - p and the result is dense
— an (n, 4**d) matrix, not a ragged pair. That is not a new
restriction: consumers already assume it, because the loop this replaces
ends in np.stack, which raises on rows of unequal width (moczarr
dggs.py:310, whose comment at dggs.py:302-305 records the gap this
closes — "there is still no vectorized many-parent children kernel").
zagg calls the scalar the same way per sub-chunk on every worker
(grids/healpix.py:199) and per shard in the shardmap reprojection
(catalog/shardmap.py:708).
Memory: the result is the whole of it, and it is n * 4**d * 8 bytes —
refining 100k parents by 5 orders is 100k x 1024 x 8 B = 819 MB. The
binding copies words before releasing the GIL (a borrowed numpy slice
cannot cross allow_threads), which adds n * 8 bytes, and the block
is allocated once at its exact final size, so there is no growth-realloc
transient and no ragged assembly copy — peak is input copy + result
plus a fixed 64 KiB chunk of per-word outcomes. Measured over 1M order-6
parents refined to order 9: 7.6 MiB of input, a 488.3 MiB result, a 497.1
MiB peak — 1.00x that model.
The result block is allocated fallibly, so a size the allocator refuses
is a catchable ValueError naming the byte count rather than a process
abort — matching the MemoryError the np.stack loop this replaces
raises at those sizes. That is not a budget, though: an allocation an
overcommitting OS accepts and then cannot back still ends in a kill, exactly
as it does for the scalar loop (numpy accepts the same over-RAM request).
Only a policy ceiling refuses those, which is what max_cells is for.
max_cells is opt-in — None by default, the opposite of
:func:moc_to_order's always-on budget. The difference is deliberate and
is about predictability, not about one op being safer. moc_to_order
defaults its budget on because a densify explodes from a tiny input
(Σ 4**(order - depth), issue #80) and the caller cannot cheaply predict
the output. Here the output is exactly n * 4**d cells, computable from
the arguments before the call, so a default guard would refuse calls the
caller already knows are fine. Same parameter name, opposite default, for a
stated reason. The None polarity inverts with it: for
:func:moc_to_order None disables a default budget, here it means
there is no budget to begin with.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
words
|
array_like
|
Parent packed morton words ( |
required |
order
|
int
|
Target HEALPix order (0-29) for the children, shared by every parent.
|
required |
max_cells
|
int or None
|
Opt-in budget on the result's cell count, |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Fail-fast, naming the lowest-index offending word (e.g.
|
See Also
mortie.orders.generate_morton_children : the scalar (one parent) form. mortie.orders.clip2order : the coarsening direction (elementwise, already vectorized).
Examples:
>>> import mortie, numpy as np
>>> parents = np.asarray(mortie.norm2mort([11, 7], [0, 3], 4), dtype=np.uint64)
>>> kids = mortie.children_of(parents, 6)
>>> kids.shape
(2, 16)
>>> np.array_equal(kids[0], mortie.generate_morton_children(int(parents[0]), 6))
True
An opt-in budget refuses an oversized refinement before it is allocated:
>>> mortie.children_of(parents, 14, max_cells=1 << 20)
...
Traceback (most recent call last):
...
ValueError: children_of would generate 2097152 cells ... max_cells=1048576...
Source code in mortie/batch.py
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 | |