Repository navigation
Commit d8bc3ba
perf(over-sample): cache the non-uniform binning divisor on the OverSampler (#507)
`OverSampler.binned_array_2d_from` is called once per numpy light-profile
evaluation -- 120 times per likelihood evaluation for a 60-component MGE (the
data grid and the blurring grid), and once more for every ordinary light
profile. On the non-uniform branch it recomputed, every call, two quantities
that depend only on the `sub_size` fixed in the constructor:
- `np.bincount(self.segment_ids, ...)`, the number of sub-pixels binned into
each pixel, plus the zero-guard `counts[counts == 0] = 1`;
- `sub_is_uniform`, an `np.isclose` over the whole `sub_size` array, which
selects the branch in the first place.
Both are now `cached_property`. Measured on the HST breakdown cell's over
sampler (17980 sub-pixels into 15361 pixels): the divisor cost 38.8 us per
call and the uniformity check 61.3 us, against 81.5 us for the whole cached
call -- so a binned evaluation was more than twice the work it needed to be.
The cached divisor is marked read-only (`flags.writeable = False`) and built
with `np.maximum(counts, 1)` rather than the old in-place `counts[counts == 0]
= 1`, because it is now handed to every caller instead of being a fresh array
per call. Values are unchanged: `np.maximum` on the `bincount` output is the
same int64 array the in-place guard produced, so the binned arrays are
bit-identical.
`cached_property` stores on the instance `__dict__`, so the cache pickles to
Nautilus worker processes with the rest of the object -- asserted by a
round-trip test, since `OverSampler` is pickled once per fit.
Tests (`test_autoarray/operators/over_sample/test_over_sampler.py`):
`..._non_uniform_counts_are_cached_and_not_mutated` (repeated calls
bit-identical, cached array unchanged and not writeable),
`..._zero_count_segments_do_not_divide_by_zero` (the guard the in-place
mutation used to provide), and `..._pickles_with_cached_properties`.
`pytest test_autoarray`: 1326 passed (1323 before, +3).
Refs #507
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Fr6iJ5T1RDARWfxttCuGkK1 parent 88e14bc commit d8bc3ba
2 files changed
Lines changed: 122 additions & 8 deletions
File tree
- autoarray/operators/over_sampling
- test_autoarray/operators/over_sample
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
167 | 167 | | |
168 | 168 | | |
169 | 169 | | |
170 | | - | |
| 170 | + | |
171 | 171 | | |
172 | 172 | | |
173 | 173 | | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
174 | 178 | | |
175 | 179 | | |
176 | 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 | + | |
177 | 205 | | |
178 | 206 | | |
179 | 207 | | |
| |||
263 | 291 | | |
264 | 292 | | |
265 | 293 | | |
266 | | - | |
267 | | - | |
268 | | - | |
269 | | - | |
270 | | - | |
271 | | - | |
272 | | - | |
| 294 | + | |
| 295 | + | |
273 | 296 | | |
274 | 297 | | |
275 | 298 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
107 | 107 | | |
108 | 108 | | |
109 | 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 | + | |
0 commit comments