Matched multi-source patching — geopatcher.matched¶
Co-located patching across N sources: a MatchedField wraps one
primary Field plus named secondaries and per-secondary coregistration
callables, and is split and merged per source by the matched patchers. See
ADR-003 in Design decisions and the
query → matchup → patch design.
Field¶
geopatcher.matched.MatchedField
dataclass
¶
N co-registered Fields presented as one Field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
primary
|
Field
|
The |
required |
secondaries
|
Mapping[str, Field]
|
|
dict()
|
coreg
|
Mapping[str, CoregFn]
|
|
dict()
|
valid_mask
|
bool
|
When True, the matched patchers
( |
True
|
Notes
The set of keys in secondaries and coreg must match
exactly; mismatched keys raise on construction.
Source code in packages/geotoolz-patcher/src/geopatcher/_src/matched/field.py
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 148 149 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 | |
domain
property
¶
Forward the primary's domain so existing samplers work.
get_config()
¶
Best-effort, JSON-able description of this composite field.
Field instances and coregistration callables are not
config-serializable (they carry live data handles / closures),
so each member is represented as a {"class": name} envelope
without a config payload — enough to identify the pipeline
shape, not enough to reconstruct it (hence
forbid_in_yaml = True).
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict with the primary's class-name envelope, per-secondary |
dict[str, Any]
|
class-name envelopes keyed by member name, and the |
dict[str, Any]
|
|
Source code in packages/geotoolz-patcher/src/geopatcher/_src/matched/field.py
select(indexer)
¶
Read primary + all secondaries at indexer and align them.
Returns a dict[str, data] keyed by source name — the primary
under PRIMARY_KEY ("primary"), each secondary under
the name supplied to MatchedField.secondaries. The values
are whatever the underlying Fields' select returns: a
geoarray.GeoArray for raster, a sub-xarray.DataArray for grid,
etc.
Each secondary is read over the primary chip's geographic footprint (see the module docstring), so the coreg callable receives a raw chip covering the whole primary chip even when the secondary's grid differs in resolution, origin or CRS.
The per-source aligned data flows through Patch.data when
a plain SpatialPatcher consumes a MatchedField. Consumers
that want the matched-patch carrier shape go through
MatchedSpatialPatcher.split, which unpacks the dict.
Source code in packages/geotoolz-patcher/src/geopatcher/_src/matched/field.py
with_data(array)
¶
Forward to the primary — every source is merged on its grid.
The single-array with_data signature is the primary's
reconstruction path. MatchedSpatialPatcher.merge returns each
source's raw aggregation output on the primary's grid;
MatchedSpatialPatcher.merge_to_field wraps every one of them
through this (the primary's) with_data, since the
coregistration already mapped each secondary onto that grid.
Source code in packages/geotoolz-patcher/src/geopatcher/_src/matched/field.py
Carriers¶
geopatcher.matched.MatchedPatch
dataclass
¶
Bases: _MatchedMembersBase['Patch']
A co-located patch read from N sources at a single anchor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
anchor
|
Any
|
Where the patch lives in the primary's coordinate
system. Same shape the primary's |
required |
members
|
dict[str, Patch]
|
|
required |
valid_mask
|
dict[str, ndarray] | None
|
Optional |
None
|
weights
|
dict[str, ndarray] | None
|
Optional |
None
|
Source code in packages/geotoolz-patcher/src/geopatcher/_src/matched/patch.py
geopatcher.matched.MatchedTemporalPatch
dataclass
¶
Bases: _MatchedMembersBase['TemporalPatch']
A co-located temporal patch read from N sources at a single anchor.
Sibling of MatchedPatch for the temporal axis. members holds
TemporalPatch values rather than Patch so the temporal
anchor (an int time index) and indices (a slice
along the time axis) flow through to downstream
temporal.aggregation.Aggregation consumers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
anchor
|
Any
|
The time-index anchor in the primary's time axis. |
required |
members
|
dict[str, TemporalPatch]
|
|
required |
valid_mask
|
dict[str, ndarray] | None
|
Optional |
None
|
weights
|
dict[str, ndarray] | None
|
Optional |
None
|
Source code in packages/geotoolz-patcher/src/geopatcher/_src/matched/patch.py
geopatcher.matched.MatchedSpatioTemporalPatch
dataclass
¶
Bases: _MatchedMembersBase['SpatioTemporalPatch']
A co-located spatio-temporal patch read from N sources.
Sibling of MatchedPatch for the spatio-temporal axis. members
holds SpatioTemporalPatch values so both spatial and temporal
anchors flow through to downstream aggregations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
space
|
Any
|
Spatial anchor in the primary's coordinate system. |
required |
time
|
Any
|
Time anchor in the primary's time axis. |
required |
members
|
dict[str, SpatioTemporalPatch]
|
|
required |
valid_mask
|
dict[str, ndarray] | None
|
Optional |
None
|
weights
|
dict[str, ndarray] | None
|
Optional |
None
|
Source code in packages/geotoolz-patcher/src/geopatcher/_src/matched/patch.py
Patchers¶
geopatcher.matched.MatchedSpatialPatcher
dataclass
¶
Bases: _MatchedConfigMixin
Spatial patcher that yields MatchedPatches and merges per-source.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
primary
|
SpatialPatcher
|
A regular |
required |
secondary_aggregators
|
Mapping[str, Aggregation]
|
|
dict()
|
Source code in packages/geotoolz-patcher/src/geopatcher/_src/matched/patcher.py
591 592 593 594 595 596 597 598 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 700 701 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 836 837 838 839 840 841 842 843 844 845 846 847 | |
anchors(mfield)
¶
Materialise the sampler's anchor sequence for mfield (no reads).
merge(patches, mfield, hooks=None)
¶
Per-source merge: dict of name -> aggregation result.
Returns the primary under MatchedPatch.PRIMARY_KEY;
secondaries appear under the names supplied to
MatchedField.secondaries. Names whose
secondary_aggregators entry is missing are skipped (you
can choose to only reconstruct a subset). Names that are
in secondary_aggregators but not in
mfield.secondaries raise — typo guard.
Each value is the aggregation's raw output, exactly as
SpatialPatcher.merge returns it: a bare np.ndarray on the
primary's grid for the dense aggregations (no transform, CRS,
nodata or attrs), a dict for spatial.aggregation.MeanStd /
spatial.aggregation.InvVarWeightedMean / spatial.aggregation.ByIndex. Use
merge_to_field to get georeferenced carriers back.
Every source is aggregated against the primary's domain because the coregistration callable mapped each secondary onto the primary's grid at split time. Reconstructing a secondary back into its own original grid would require re-inverting the coregistration, which is the user's problem if they need it.
Streaming: patches is consumed once and each source's
aggregation runs on its own thread over a bounded queue, so only
a few patches per source are resident at a time — a streaming
aggregation (e.g. zarr-backed spatial.aggregation.OverlapAdd) keeps its
bound on every source. Every aggregation (primary and secondary)
gets the strict-mode streaming-safety check, which warns (or
raises under set_strict) for a streaming_safe = False one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
patches
|
Iterable[MatchedPatch]
|
Iterable of |
required |
mfield
|
MatchedField
|
Original |
required |
hooks
|
Iterable[PatcherHook] | None
|
Optional observability hooks, dispatched once at the
matched level (one |
None
|
Source code in packages/geotoolz-patcher/src/geopatcher/_src/matched/patcher.py
merge_to_field(patches, mfield, hooks=None)
¶
merge + rebuild each source on the primary's grid via with_data.
Every source was aggregated on the primary's domain, so each
value is wrapped with mfield.primary.with_data — it carries
the primary's transform and CRS, and also the primary's nodata
and attrs (a secondary's own nodata / attrs are not carried; its
values keep that secondary's dtype when they fit, by the
SpatialPatcher.merge_to_xarray rule).
Raises:
| Type | Description |
|---|---|
TypeError
|
As |
Source code in packages/geotoolz-patcher/src/geopatcher/_src/matched/patcher.py
n_anchors(mfield)
¶
split(mfield, hooks=None, *, prefetch=0, journal=None, cache=None, max_in_flight=None, max_in_flight_bytes=None)
¶
Yield MatchedPatches by walking mfield with the primary's sampler.
Internally drives self.primary.split(mfield) — since
MatchedField already satisfies the Field Protocol, the
existing sampler / geometry / window machinery works
unchanged. Each outer Patch carries the per-source
dict returned by MatchedField.select in its data
field; this method unpacks that dict into a MatchedPatch
whose members is {name: Patch}, whose anchor /
indices / weights mirror the outer patch, and whose
weights maps each member to its window weights.
Per-source valid_mask arrays are computed when
mfield.valid_mask is True (the default): for numeric
array-coercible data a cell is invalid where it equals the
carrier's declared nodata (fill_value /
rio.nodata) or, for float data, is NaN / ±inf. Non-array
members are simply omitted from the mask dict (and the dict
drops to None if no member produced a mask).
The primary's on_error policy covers every source: a failed
read or coregistration raises ("raise"), drops the anchor
("skip" / exhausted "retry"), or under "mask" yields a
MatchedPatch whose every member is the all-NaN placeholder on the
primary's grid with an all-False valid_mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mfield
|
MatchedField
|
A |
required |
hooks
|
Iterable[PatcherHook] | None
|
Optional observability hooks, dispatched by the primary
|
None
|
prefetch
|
int
|
Forwarded to |
0
|
journal
|
Any | None
|
Forwarded to |
None
|
cache
|
Any | None
|
A |
None
|
max_in_flight
|
int | None
|
Forwarded to |
None
|
max_in_flight_bytes
|
int | None
|
Forwarded to |
None
|
Source code in packages/geotoolz-patcher/src/geopatcher/_src/matched/patcher.py
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 | |
geopatcher.matched.MatchedTemporalPatcher
dataclass
¶
Bases: _MatchedConfigMixin
Temporal patcher that yields MatchedTemporalPatches and merges per-source.
Mirror of MatchedSpatialPatcher over the time axis. The
TemporalPatcher slices a numpy array directly (it does not call
Field.select per anchor the way SpatialPatcher does), so the
matched-temporal split first reads every source's full series once
— mfield.select(indexer), with an indexer covering the primary's
whole extent (see split) — then drives the primary patcher on the
primary's series and slices each secondary's array in lockstep.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
primary
|
TemporalPatcher
|
A regular |
required |
secondary_aggregators
|
Mapping[str, Aggregation]
|
|
dict()
|
Source code in packages/geotoolz-patcher/src/geopatcher/_src/matched/patcher.py
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 | |
anchors(mfield, time_axis=0, *, indexer=None, coord=None)
¶
Materialise the sampler's anchor sequence for mfield.
Reads the primary only (no secondary read, no coregistration).
Source code in packages/geotoolz-patcher/src/geopatcher/_src/matched/patcher.py
merge(patches, mfield, hooks=None)
¶
Per-source merge: dict of name -> aggregation result.
Returns the primary under MatchedTemporalPatch.PRIMARY_KEY;
secondaries appear under the names supplied to
MatchedField.secondaries. Names whose
secondary_aggregators entry is missing are skipped (the
user opted out for that source). Names that are in
secondary_aggregators but not in mfield.secondaries
raise — typo guard.
Unlike the spatial path, temporal.aggregation.Aggregation.merge takes
only the patches (no domain argument), so mfield is used
solely for the typo-guard check. patches is consumed once,
streamed to every source's aggregation as in
MatchedSpatialPatcher.merge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
patches
|
Iterable[MatchedTemporalPatch]
|
Iterable of |
required |
mfield
|
MatchedField
|
Original |
required |
hooks
|
Iterable[PatcherHook] | None
|
Optional observability hooks, dispatched once at the
matched level (one |
None
|
Source code in packages/geotoolz-patcher/src/geopatcher/_src/matched/patcher.py
n_anchors(mfield, time_axis=0, *, indexer=None, coord=None)
¶
Number of MatchedTemporalPatches split will yield.
Reads the primary only (no secondary read, no coregistration).
Source code in packages/geotoolz-patcher/src/geopatcher/_src/matched/patcher.py
split(mfield, time_axis=0, hooks=None, *, indexer=None, coord=None, prefetch=0)
¶
Yield MatchedTemporalPatches by driving the primary on each anchor.
Reads every source's series once via mfield.select(indexer),
then drives the primary TemporalPatcher on the primary's array.
For each yielded TemporalPatch, each secondary's array is sliced
with the same indices and packaged into the matched carrier.
Per-source valid_mask arrays are computed when
mfield.valid_mask is True (the default): a cell is invalid
where it equals the source carrier's declared nodata or, for
float data, is NaN / ±inf.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mfield
|
MatchedField
|
A |
required |
time_axis
|
int
|
Which axis of each source's array is the time axis. Default 0. Must be the same across sources. |
0
|
hooks
|
Iterable[PatcherHook] | None
|
Optional observability hooks, dispatched once per
matched patch ( |
None
|
indexer
|
Any | None
|
The indexer that reads the whole series. |
None
|
coord
|
ndarray | None
|
1-D coordinate vector along |
None
|
prefetch
|
int
|
If positive, read ahead up to |
0
|
Raises:
| Type | Description |
|---|---|
ValueError
|
A secondary's length along |
Source code in packages/geotoolz-patcher/src/geopatcher/_src/matched/patcher.py
geopatcher.matched.MatchedSpatioTemporalPatcher
dataclass
¶
Bases: _MatchedConfigMixin
Spatio-temporal matched patcher — yields MatchedSpatioTemporalPatches.
Mirror of MatchedSpatialPatcher over the spatio-temporal axis: it
drives the primary SpatioTemporalPatcher over the MatchedField
and turns each yielded patch into a matched carrier, so the coupling
mode ("product" or "coupled", from primary.coupling), the
chip reads, the on_error policy, hooks, journal and backpressure
are the primary's own.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
primary
|
SpatioTemporalPatcher
|
A regular |
required |
secondary_aggregators
|
Mapping[str, Aggregation]
|
|
dict()
|
Source code in packages/geotoolz-patcher/src/geopatcher/_src/matched/patcher.py
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 | |
merge(patches, mfield, hooks=None)
¶
Per-source merge: dict of name -> [(spatial_anchor, temporal_merge), …].
Each source's value is what SpatioTemporalPatcher.merge returns
for that source's members: (spatial_anchor,
temporal_aggregation_result) pairs grouped by spatial anchor
(first-seen order). Secondaries use their temporal.aggregation.Aggregation;
the primary uses self.primary.temporal.aggregation.
patches is consumed once and fanned out to every source as in
MatchedSpatialPatcher.merge; each source's grouping still holds
its groups until the pass ends.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
patches
|
Iterable[MatchedSpatioTemporalPatch]
|
Iterable of |
required |
mfield
|
MatchedField
|
Original |
required |
hooks
|
Iterable[PatcherHook] | None
|
Optional observability hooks. Dispatched at the
matched layer (one |
None
|
Source code in packages/geotoolz-patcher/src/geopatcher/_src/matched/patcher.py
split(mfield, hooks=None, *, coord=None, prefetch=0, journal=None, cache=None, max_in_flight=None, max_in_flight_bytes=None)
¶
Yield MatchedSpatioTemporalPatches lazily.
Runs self.primary.split over mfield: each spatial chip is
one MatchedField read (every source over the primary chip's
footprint, coregistered onto it) checked to hold the primary and
to share its time length, and every source is sliced to the same
time window. Each yielded patch is unpacked into a matched carrier
whose members are per-source SpatioTemporalPatches.
The spatial patcher's on_error policy covers every source's
read and coregistration; under "mask" a failed chip yields
all-NaN members with an all-False valid_mask. Per-source
valid_mask arrays are otherwise computed when
mfield.valid_mask is True (the default).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mfield
|
MatchedField
|
A |
required |
hooks
|
Iterable[PatcherHook] | None
|
Optional observability hooks, dispatched by the
primary's split once per matched patch — |
None
|
coord
|
ndarray | None
|
1-D coordinate vector along the time axis, as
|
None
|
prefetch
|
int
|
If positive, read ahead up to |
0
|
journal
|
Any | None
|
Forwarded to |
None
|
cache
|
Any | None
|
A |
None
|
max_in_flight
|
int | None
|
Forwarded to |
None
|
max_in_flight_bytes
|
int | None
|
Forwarded; a matched patch is sized as the sum of its members' bytes. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
A secondary's length along the time axis differs from the primary's. |
Source code in packages/geotoolz-patcher/src/geopatcher/_src/matched/patcher.py
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 | |