arrayprobe 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- arrayprobe/__init__.py +219 -0
- arrayprobe/__main__.py +19 -0
- arrayprobe/_v314_lazy.py +48 -0
- arrayprobe/ai_ready.py +212 -0
- arrayprobe/attributed_export.py +1333 -0
- arrayprobe/authordefaults.py +267 -0
- arrayprobe/cli.py +3510 -0
- arrayprobe/combine.py +12 -0
- arrayprobe/compare.py +950 -0
- arrayprobe/config.py +1239 -0
- arrayprobe/constants.py +12 -0
- arrayprobe/count_closure.py +204 -0
- arrayprobe/diagnostic_views.py +769 -0
- arrayprobe/diode_report.py +265 -0
- arrayprobe/driver/__init__.py +5 -0
- arrayprobe/driver/_load_record.py +331 -0
- arrayprobe/driver/preflight_inputs.py +250 -0
- arrayprobe/driver/reprocess.py +553 -0
- arrayprobe/driver/run_flight.py +8166 -0
- arrayprobe/driver/tas_from_probe_housekeeping.py +124 -0
- arrayprobe/dropsize.py +519 -0
- arrayprobe/friendly.py +206 -0
- arrayprobe/geometry/__init__.py +77 -0
- arrayprobe/geometry/min_rectangle.py +92 -0
- arrayprobe/intarr.py +12 -0
- arrayprobe/intarr_analysis.py +16 -0
- arrayprobe/intarr_plots.py +12 -0
- arrayprobe/inventory.py +129 -0
- arrayprobe/jit_kernels.py +344 -0
- arrayprobe/lwc.py +44 -0
- arrayprobe/masks.py +349 -0
- arrayprobe/matio.py +410 -0
- arrayprobe/matlab_compat.py +13 -0
- arrayprobe/ncio.py +26 -0
- arrayprobe/particle.py +55 -0
- arrayprobe/particle_perimeter.py +12 -0
- arrayprobe/products.py +78 -0
- arrayprobe/profiles.py +179 -0
- arrayprobe/provenance.py +21 -0
- arrayprobe/py.typed +11 -0
- arrayprobe/readers/__init__.py +9 -0
- arrayprobe/readers/dmt.py +603 -0
- arrayprobe/readers/mask_csv.py +12 -0
- arrayprobe/readers/pms.py +12 -0
- arrayprobe/readers/sea.py +433 -0
- arrayprobe/readers/tasinfo.py +12 -0
- arrayprobe/regimes.py +507 -0
- arrayprobe/render.py +728 -0
- arrayprobe/reproduce_report.py +149 -0
- arrayprobe/review_agreement.py +135 -0
- arrayprobe/review_sampling.py +327 -0
- arrayprobe/run_page.py +624 -0
- arrayprobe/sample_area.py +12 -0
- arrayprobe/selector.py +206 -0
- arrayprobe/sizing.py +151 -0
- arrayprobe/time_convert.py +12 -0
- arrayprobe/timing_closure.py +141 -0
- arrayprobe/tools/__init__.py +27 -0
- arrayprobe/tools/make_demo_flight.py +1089 -0
- arrayprobe/tools/make_tas_file.py +760 -0
- arrayprobe/tools/probe_comparison.py +569 -0
- arrayprobe/tools/synthetic_truth.py +698 -0
- arrayprobe/tools/tas_sources.py +705 -0
- arrayprobe/triage_context.py +488 -0
- arrayprobe/triage_server.py +5215 -0
- arrayprobe/tune/__init__.py +15 -0
- arrayprobe/tune/diodes.py +400 -0
- arrayprobe/tune/distributions.py +300 -0
- arrayprobe/tune/sample.py +243 -0
- arrayprobe/tune/score.py +230 -0
- arrayprobe/tune/settings.py +354 -0
- arrayprobe/validate_by_condition.py +208 -0
- arrayprobe/validation_report.py +166 -0
- arrayprobe-0.1.0.dist-info/METADATA +260 -0
- arrayprobe-0.1.0.dist-info/RECORD +189 -0
- arrayprobe-0.1.0.dist-info/WHEEL +5 -0
- arrayprobe-0.1.0.dist-info/entry_points.txt +2 -0
- arrayprobe-0.1.0.dist-info/licenses/LICENSE +31 -0
- arrayprobe-0.1.0.dist-info/licenses/LICENSES/BSD-3-Clause.txt +47 -0
- arrayprobe-0.1.0.dist-info/licenses/NOTICE.md +369 -0
- arrayprobe-0.1.0.dist-info/top_level.txt +4 -0
- arrayprobe_soda2/__init__.py +131 -0
- arrayprobe_soda2/angles.py +143 -0
- arrayprobe_soda2/aspect_ratio.py +541 -0
- arrayprobe_soda2/backfill.py +100 -0
- arrayprobe_soda2/binary_dof.py +181 -0
- arrayprobe_soda2/buildindex.py +113 -0
- arrayprobe_soda2/decompress_dmt.py +285 -0
- arrayprobe_soda2/enhancement_hooks.py +275 -0
- arrayprobe_soda2/export_ascii.py +732 -0
- arrayprobe_soda2/export_ncdf.py +683 -0
- arrayprobe_soda2/export_ncdf_raf.py +396 -0
- arrayprobe_soda2/fillholes.py +161 -0
- arrayprobe_soda2/find_radius.py +105 -0
- arrayprobe_soda2/findsize.py +407 -0
- arrayprobe_soda2/fit_poisson.py +170 -0
- arrayprobe_soda2/fix2dimage.py +63 -0
- arrayprobe_soda2/fixraking.py +162 -0
- arrayprobe_soda2/fixstuckbits.py +146 -0
- arrayprobe_soda2/hvps4_read_frame.py +121 -0
- arrayprobe_soda2/jit_kernels.py +152 -0
- arrayprobe_soda2/label_blobs.py +177 -0
- arrayprobe_soda2/min_circle_fast.py +320 -0
- arrayprobe_soda2/particlesort.py +703 -0
- arrayprobe_soda2/pbp_writer.py +558 -0
- arrayprobe_soda2/poisson_spot_correct.py +183 -0
- arrayprobe_soda2/princomp.py +107 -0
- arrayprobe_soda2/probespecs.py +361 -0
- arrayprobe_soda2/process_2d.py +1364 -0
- arrayprobe_soda2/process_2d_parallel.py +626 -0
- arrayprobe_soda2/process_hk.py +512 -0
- arrayprobe_soda2/processbuffer_2dc.py +155 -0
- arrayprobe_soda2/processbuffer_cip.py +213 -0
- arrayprobe_soda2/processbuffer_f2dc.py +139 -0
- arrayprobe_soda2/processbuffer_spec.py +441 -0
- arrayprobe_soda2/processbuffer_tail.py +350 -0
- arrayprobe_soda2/pth_readers.py +245 -0
- arrayprobe_soda2/py.typed +11 -0
- arrayprobe_soda2/read2dbuffer.py +371 -0
- arrayprobe_soda2/reject.py +246 -0
- arrayprobe_soda2/reprocess_pbp.py +156 -0
- arrayprobe_soda2/sample_area.py +264 -0
- arrayprobe_soda2/save_writer.py +480 -0
- arrayprobe_soda2/sea.py +324 -0
- arrayprobe_soda2/spec_absolutepointers.py +109 -0
- arrayprobe_soda2/spec_decompress.py +203 -0
- arrayprobe_soda2/spec_index.py +476 -0
- arrayprobe_soda2/spec_read_frame.py +286 -0
- arrayprobe_soda2/spec_read_hk.py +301 -0
- arrayprobe_soda2/spec_readint.py +127 -0
- arrayprobe_soda2/timeconv.py +77 -0
- arrayprobe_soda2/update_op.py +101 -0
- arrayprobe_uiops/__init__.py +45 -0
- arrayprobe_uiops/combine.py +192 -0
- arrayprobe_uiops/combine_fork.py +359 -0
- arrayprobe_uiops/constants.py +830 -0
- arrayprobe_uiops/decode_naming.py +113 -0
- arrayprobe_uiops/diode_mask.py +250 -0
- arrayprobe_uiops/engine_absence.py +41 -0
- arrayprobe_uiops/export_oap.py +325 -0
- arrayprobe_uiops/geometry_core.py +366 -0
- arrayprobe_uiops/habit.py +749 -0
- arrayprobe_uiops/habit_core.py +248 -0
- arrayprobe_uiops/hollow_size_rule.py +376 -0
- arrayprobe_uiops/imgproc.py +2081 -0
- arrayprobe_uiops/imgproc_core.py +275 -0
- arrayprobe_uiops/intarr.py +167 -0
- arrayprobe_uiops/intarr_analysis.py +569 -0
- arrayprobe_uiops/intarr_plots.py +119 -0
- arrayprobe_uiops/jit_kernels.py +430 -0
- arrayprobe_uiops/lwc.py +70 -0
- arrayprobe_uiops/matlab_compat.py +860 -0
- arrayprobe_uiops/measure.py +346 -0
- arrayprobe_uiops/min_circle.py +204 -0
- arrayprobe_uiops/min_ellipse.py +161 -0
- arrayprobe_uiops/min_rectangle.py +200 -0
- arrayprobe_uiops/ncar_2dc_timing.py +100 -0
- arrayprobe_uiops/ncio.py +810 -0
- arrayprobe_uiops/oap_read.py +228 -0
- arrayprobe_uiops/particle.py +51 -0
- arrayprobe_uiops/particle_perimeter.py +94 -0
- arrayprobe_uiops/particle_physics.py +199 -0
- arrayprobe_uiops/pecan_params.py +134 -0
- arrayprobe_uiops/probe_cases.py +298 -0
- arrayprobe_uiops/product_sanity.py +337 -0
- arrayprobe_uiops/profile.py +71 -0
- arrayprobe_uiops/profiles.py +756 -0
- arrayprobe_uiops/provenance.py +804 -0
- arrayprobe_uiops/py.typed +11 -0
- arrayprobe_uiops/qc.py +169 -0
- arrayprobe_uiops/readers/__init__.py +8 -0
- arrayprobe_uiops/readers/mask_csv.py +74 -0
- arrayprobe_uiops/readers/pms.py +239 -0
- arrayprobe_uiops/readers/tasinfo.py +57 -0
- arrayprobe_uiops/rect_ellipse.py +129 -0
- arrayprobe_uiops/register_image_assisted.py +25 -0
- arrayprobe_uiops/reject.py +501 -0
- arrayprobe_uiops/reject_core.py +220 -0
- arrayprobe_uiops/sample_area.py +93 -0
- arrayprobe_uiops/sizedist.py +1923 -0
- arrayprobe_uiops/sizedist_core.py +814 -0
- arrayprobe_uiops/sizedist_tables.py +642 -0
- arrayprobe_uiops/spec.py +1094 -0
- arrayprobe_uiops/spec_core.py +703 -0
- arrayprobe_uiops/stray_pixel.py +103 -0
- arrayprobe_uiops/tas.py +191 -0
- arrayprobe_uiops/time_convert.py +41 -0
- uiops/__init__.py +402 -0
- uiops/py.typed +11 -0
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
2
|
+
# Reproduces SODA-2, Copyright (c) 2016 UCAR; see LICENSES/BSD-3-Clause.txt and NOTICE.md.
|
|
3
|
+
"""``label_blobs.pro`` -- label connected blobs in a binary image, working
|
|
4
|
+
around ``label_region``'s own border-zeroing.
|
|
5
|
+
|
|
6
|
+
30 lines of IDL. Signature ``label_blobs(img, _EXTRA=e, dilatelength=,
|
|
7
|
+
integer=)``. Callers: ``soda2_processbuffer.pro:687-689``
|
|
8
|
+
(``keeplargest`` 1/2/3, default 0 at ``soda2_update_op.pro:28``, called
|
|
9
|
+
with ``dilate=2``, IDL keyword abbreviation of ``dilatelength``), the
|
|
10
|
+
``:574`` HAIL branch (``dilate=1, /int``), and ``fillholes.pro:9``
|
|
11
|
+
(``dilatelength=0``, no ``/all``, byte return).
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
**Line-by-line**:
|
|
15
|
+
|
|
16
|
+
- ``:12`` -- ``img2 = 1b < img`` (``MIN(1, img)``): any nonzero pixel
|
|
17
|
+
becomes 1.
|
|
18
|
+
- ``:13`` -- ``s = [size(img2,/dim), 1]``: a 1-D input gets ``s[1] = 1``
|
|
19
|
+
and the routine's whole body still runs on the 2-D ``temp`` it builds;
|
|
20
|
+
this port accepts a 1-D ``img`` and returns 1-D.
|
|
21
|
+
- ``:15-17`` -- ``pad = 1 > dilatelength`` (``MAX(1, dilatelength)``);
|
|
22
|
+
``temp`` is a zero-filled array ``pad`` pixels larger on every side,
|
|
23
|
+
with ``img2`` written into the middle.
|
|
24
|
+
- **``label_region``'s own border rule**, worked around by the padding:
|
|
25
|
+
IDL's ``label_region`` zeroes the outermost pixel ring of *its own
|
|
26
|
+
input* (measured under GDL: a set pixel on ``temp``'s border gets label
|
|
27
|
+
0). Because ``temp`` is always padded by at least 1, a real foreground
|
|
28
|
+
pixel at ``img``'s own edge is never on ``temp``'s border, so
|
|
29
|
+
``label_blobs`` itself never loses an edge pixel -- that is the whole
|
|
30
|
+
point of the padding (``:2``'s comment). Reimplemented here as
|
|
31
|
+
:func:`_label_region`: a flood fill over ``temp``'s interior only,
|
|
32
|
+
4-connected by default or 8-connected with ``all_neighbors=True``,
|
|
33
|
+
labels assigned in **raster order (x fastest, then y)** over the first
|
|
34
|
+
pixel of each blob encountered -- measured against GDL on a 3-blob case.
|
|
35
|
+
Returns ``uint32`` labels (IDL: ``label_region`` "automatically converts
|
|
36
|
+
to integer").
|
|
37
|
+
- ``:19-23`` -- when ``dilatelength != 0``: ``kerneldim = (dilatelength*2+1)
|
|
38
|
+
< (s[1]+2)`` -- **clamped against the slice count plus 2, not against
|
|
39
|
+
``temp``'s own dimension** ``s[1]+2*pad`` -- so for a 1- or 2-slice roi
|
|
40
|
+
with ``dilatelength=2`` the kernel is 3x3 in *both* directions, not 5x5
|
|
41
|
+
(S4-z). A square all-ones kernel of that size, origin centred, dilates
|
|
42
|
+
``temp``; ``label_region`` runs on the dilated image; the *original*
|
|
43
|
+
pixels then take the label of the dilated blob they fall in
|
|
44
|
+
(``labels = temp * dilatedimage``). Because the foreground sits ``pad``
|
|
45
|
+
pixels inside ``temp`` and the kernel radius never exceeds ``pad``
|
|
46
|
+
(``kerneldim <= 2*dilatelength+1`` and ``pad >= dilatelength``),
|
|
47
|
+
dilation never reaches ``temp``'s own border -- so no shim assumption
|
|
48
|
+
about out-of-range reads is exercised here.
|
|
49
|
+
- ``:28-29`` -- crop back to the original footprint; ``integer=1`` returns
|
|
50
|
+
the ``uint32`` crop, otherwise **``byte()``** -- modulo 256, so a roi
|
|
51
|
+
with more than 255 blobs wraps (S4-y).
|
|
52
|
+
"""
|
|
53
|
+
from __future__ import annotations
|
|
54
|
+
|
|
55
|
+
import numpy as np
|
|
56
|
+
|
|
57
|
+
__all__ = ["label_blobs"]
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _label_region(temp: np.ndarray, all_neighbors: bool) -> np.ndarray:
|
|
61
|
+
"""4- (or 8-, ``all_neighbors``) connected labelling of ``temp``,
|
|
62
|
+
raster order (x fastest, y slowest), matching GDL's ``label_region``
|
|
63
|
+
on a padded (border-safe) input. ``temp`` is 2-D, ``uint8``-or-similar,
|
|
64
|
+
0/nonzero."""
|
|
65
|
+
ny, nx = temp.shape # temp is stored [y, x] here; caller transposes
|
|
66
|
+
labels = np.zeros((ny, nx), dtype=np.uint32)
|
|
67
|
+
next_label = 1
|
|
68
|
+
if all_neighbors:
|
|
69
|
+
neighbours = [(-1, -1), (-1, 0), (-1, 1), (0, -1),
|
|
70
|
+
(0, 1), (1, -1), (1, 0), (1, 1)]
|
|
71
|
+
else:
|
|
72
|
+
neighbours = [(-1, 0), (1, 0), (0, -1), (0, 1)]
|
|
73
|
+
|
|
74
|
+
for y in range(ny):
|
|
75
|
+
for x in range(nx):
|
|
76
|
+
if temp[y, x] == 0 or labels[y, x] != 0:
|
|
77
|
+
continue
|
|
78
|
+
# New blob found at raster position (x fastest): flood fill.
|
|
79
|
+
stack = [(y, x)]
|
|
80
|
+
labels[y, x] = next_label
|
|
81
|
+
while stack:
|
|
82
|
+
cy, cx = stack.pop()
|
|
83
|
+
for dy, dx in neighbours:
|
|
84
|
+
ny_, nx_ = cy + dy, cx + dx
|
|
85
|
+
if (0 <= ny_ < ny and 0 <= nx_ < nx
|
|
86
|
+
and temp[ny_, nx_] != 0 and labels[ny_, nx_] == 0):
|
|
87
|
+
labels[ny_, nx_] = next_label
|
|
88
|
+
stack.append((ny_, nx_))
|
|
89
|
+
next_label += 1
|
|
90
|
+
return labels
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def _dilate_square(img: np.ndarray, kerneldim: int) -> np.ndarray:
|
|
94
|
+
"""Binary dilation with a square all-ones kernel, origin centred,
|
|
95
|
+
out-of-range reads as 0. Not exercised beyond ``temp``'s own padding
|
|
96
|
+
(see module docstring), so no shim assumption is at stake here."""
|
|
97
|
+
ny, nx = img.shape
|
|
98
|
+
r = kerneldim // 2
|
|
99
|
+
out = np.zeros_like(img)
|
|
100
|
+
ys, xs = np.nonzero(img)
|
|
101
|
+
for y, x in zip(ys.tolist(), xs.tolist(), strict=True):
|
|
102
|
+
y0, y1 = max(0, y - r), min(ny, y + r + 1)
|
|
103
|
+
x0, x1 = max(0, x - r), min(nx, x + r + 1)
|
|
104
|
+
out[y0:y1, x0:x1] = 1
|
|
105
|
+
return out
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def label_blobs(
|
|
109
|
+
img: np.ndarray,
|
|
110
|
+
dilatelength: int = 0,
|
|
111
|
+
all_neighbors: bool = False,
|
|
112
|
+
integer: bool = False,
|
|
113
|
+
jit: bool = False,
|
|
114
|
+
) -> np.ndarray:
|
|
115
|
+
"""``label_blobs(img, dilatelength=dilatelength,
|
|
116
|
+
all_neighbors=all_neighbors, integer=integer)``
|
|
117
|
+
(``label_blobs.pro:9-29``).
|
|
118
|
+
|
|
119
|
+
``all_neighbors`` is this port's name for IDL's ``/all`` keyword
|
|
120
|
+
(passed through ``_EXTRA`` to ``label_region``'s ``ALL_NEIGHBORS``).
|
|
121
|
+
Accepts and returns 1-D input for a 1-D ``img`` (``:13``). Returns
|
|
122
|
+
``uint32`` labels when ``integer=True``, else ``uint8`` (**wraps at
|
|
123
|
+
256 blobs, modulo; SODA-2 register entry S4-y**, ``:29``).
|
|
124
|
+
|
|
125
|
+
``jit``
|
|
126
|
+
Speed only, **default off** (space: the port --
|
|
127
|
+
a profile found
|
|
128
|
+
:func:`_label_region` the single hottest leaf in a real SODA-2 run,
|
|
129
|
+
~24-28 %). ``True`` routes :func:`_label_region` through
|
|
130
|
+
:mod:`arrayprobe_soda2.jit_kernels`'s numba flood fill, exact by
|
|
131
|
+
construction, so the array returned is the same bytes
|
|
132
|
+
(``tests/test_soda2_label_region_jit_identity.py``). Needs numba,
|
|
133
|
+
imported only here, never by ``import arrayprobe_soda2``. Not IDL's.
|
|
134
|
+
"""
|
|
135
|
+
img = np.asarray(img)
|
|
136
|
+
one_d = img.ndim == 1
|
|
137
|
+
if one_d:
|
|
138
|
+
img2d = img.reshape(1, -1) # s[1] = 1, :13
|
|
139
|
+
else:
|
|
140
|
+
img2d = img
|
|
141
|
+
|
|
142
|
+
img2 = np.minimum(1, img2d).astype(np.uint8) # :12, [x, y] convention
|
|
143
|
+
s0, s1 = img2.shape[0], img2.shape[1] # s0 = diode axis, s1 = slice axis
|
|
144
|
+
|
|
145
|
+
pad = max(1, dilatelength)
|
|
146
|
+
temp = np.zeros((s0 + 2 * pad, s1 + 2 * pad), dtype=np.uint8)
|
|
147
|
+
temp[pad:pad + s0, pad:pad + s1] = img2
|
|
148
|
+
|
|
149
|
+
# _label_region above takes [y, x] = [slice, diode]; temp here is
|
|
150
|
+
# [diode, slice], so transpose in and back out for the flood fill.
|
|
151
|
+
temp_yx = temp.T
|
|
152
|
+
|
|
153
|
+
if jit:
|
|
154
|
+
# Imported here, never at module level: numba costs seconds to
|
|
155
|
+
# import and the default path must not pay for it.
|
|
156
|
+
from . import jit_kernels
|
|
157
|
+
region_fn = jit_kernels.load().label_region
|
|
158
|
+
else:
|
|
159
|
+
region_fn = _label_region
|
|
160
|
+
|
|
161
|
+
if dilatelength != 0:
|
|
162
|
+
kerneldim = min(dilatelength * 2 + 1, s1 + 2) # :20, S4-z
|
|
163
|
+
dilated_yx = _dilate_square(temp_yx, kerneldim)
|
|
164
|
+
region_labels_yx = region_fn(dilated_yx, all_neighbors)
|
|
165
|
+
labels_yx = temp_yx.astype(np.uint32) * region_labels_yx
|
|
166
|
+
else:
|
|
167
|
+
labels_yx = region_fn(temp_yx, all_neighbors)
|
|
168
|
+
|
|
169
|
+
labels = labels_yx.T # back to [diode, slice]
|
|
170
|
+
|
|
171
|
+
cropped = labels[pad:pad + s0, pad:pad + s1]
|
|
172
|
+
if one_d:
|
|
173
|
+
cropped = cropped.reshape(-1)
|
|
174
|
+
|
|
175
|
+
if integer:
|
|
176
|
+
return cropped.astype(np.uint32)
|
|
177
|
+
return (cropped % 256).astype(np.uint8) # byte(), :29, S4-y
|
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
2
|
+
# Reproduces SODA-2, Copyright (c) 2016 UCAR; see LICENSES/BSD-3-Clause.txt and NOTICE.md.
|
|
3
|
+
r"""``min_circle_fast.pro`` -- SODA-2's minimum-enclosing-circle routine.
|
|
4
|
+
|
|
5
|
+
**Rokne, not Welzl.** The source's own header cites *"algorithm in Graphics
|
|
6
|
+
Gems II, article by Jon Rokne"* and *"A faster version that eliminates the
|
|
7
|
+
'for' loops 6/2012"*. It is an iterative angle-based method -- take the
|
|
8
|
+
lowest point ``P``, find ``Q`` minimising the angle to the x-axis, then
|
|
9
|
+
repeatedly find ``R`` minimising angle ``PRQ`` and swap ``P`` or ``Q``
|
|
10
|
+
whenever their vertex angle is obtuse -- not the exact incremental algorithm
|
|
11
|
+
(`Miniball`, via CGAL) that this repository's own v3.1.4/v3.5 ports already
|
|
12
|
+
use for the same *problem*. The two can legitimately disagree on the same
|
|
13
|
+
points; see :mod:`tools.soda2_min_circle_vs_exact` for a search that has not
|
|
14
|
+
found one yet.
|
|
15
|
+
|
|
16
|
+
Four properties, each read from
|
|
17
|
+
the source rather than inferred:
|
|
18
|
+
|
|
19
|
+
1. **It can terminate without converging.** ``c=c+1`` / ``IF c gt 10000 THEN
|
|
20
|
+
doagain=0`` caps the loop at 10 001 iterations and returns whatever
|
|
21
|
+
``P``/``Q``/``R`` it happens to hold, with no flag and no way for a caller
|
|
22
|
+
to tell -- see :func:`min_circle_fast_traced` and :class:`Trace`, which
|
|
23
|
+
expose this additively, and :data:`ITERATION_CAP`.
|
|
24
|
+
2. **The angle at ``:19`` narrows only its numerator to single precision, not
|
|
25
|
+
the whole computation.** ``angle=atan(float(b)/a)``: ``b`` is cast to
|
|
26
|
+
FLOAT, but ``a`` is already DOUBLE (from ``:11-12``), so IDL promotes the
|
|
27
|
+
division back to DOUBLE and ``atan`` runs in DOUBLE. Measured under GDL,
|
|
28
|
+
``typename(float(b)/a)`` is **DOUBLE**, not FLOAT -- a corrected reading:
|
|
29
|
+
an earlier draft of this project's own design record described the whole
|
|
30
|
+
expression as "computed in single precision", which would be a different
|
|
31
|
+
(and wrong) port. What actually happens: ``float(b)`` rounds ``b`` to the
|
|
32
|
+
nearest float32 value, and that rounding -- not a float32 ``atan`` -- is
|
|
33
|
+
what changes which point wins ``Q`` on a close call. Measured:
|
|
34
|
+
``atan(float(0.1)/3.0) = 3.3320996374401286e-02`` against pure double
|
|
35
|
+
``atan(0.1/3.0) = 3.3320995878247196e-02``; a full-float32 evaluation would
|
|
36
|
+
give a third value, ``3.3320997e-02``, and silently port the wrong thing.
|
|
37
|
+
3. **A NaN/inf fallback for near-collinear point sets.** :func:`find_radius`
|
|
38
|
+
returns non-finite for collinear input; this routine's ``IF finite(circ.r)
|
|
39
|
+
eq 0`` branch (``:64``) catches it and recomputes from the maximally
|
|
40
|
+
separated x or y extent instead -- two different circle definitions
|
|
41
|
+
depending on conditioning, both reproduced.
|
|
42
|
+
4. **An acknowledged, unexplained defect sits in the source.** A commented-out
|
|
43
|
+
speedup carries the author's own note: ``;Q=max(where(angle eq
|
|
44
|
+
min(angle))) ;Speedup, use point furthest away to start, ** CAUSES ERROR...
|
|
45
|
+
WHY? **``. Not reproduced (it is commented out in the original too); worth
|
|
46
|
+
raising with NCAR as their own open item.
|
|
47
|
+
|
|
48
|
+
**`!pi` is IDL's FLOAT pi, and that is the headline finding of this file.**
|
|
49
|
+
``!pi`` is ``3.1415927410125732``, not double-precision
|
|
50
|
+
``3.141592653589793`` (``!dpi``, which appears nowhere in the whole SODA-2
|
|
51
|
+
tree). This routine uses ``!pi`` four times (``:34, :37, :41, :51``). Its
|
|
52
|
+
consequence is not cosmetic: ``angP = !pi - angR - angQ`` (``:34``) carries a
|
|
53
|
+
``+8.74e-8`` rad bias, and the obtuse tests at ``:37, :41, :51`` compare
|
|
54
|
+
DOUBLE angles against FLOAT ``!pi/2`` (``1.5707963705062866``), which is
|
|
55
|
+
strictly *greater* than the true DOUBLE right angle
|
|
56
|
+
(``acos(0) = 1.5707963267948966``). So an **exact right angle at R never
|
|
57
|
+
takes the two-point branch at :51** -- every rectangle, every axis-aligned
|
|
58
|
+
pixel block, and a digitised disc outline all take the three-point branch
|
|
59
|
+
instead, with an identical circle but three ``corners`` where the exact
|
|
60
|
+
geometry has two. GDL-measured on ``rect_4``, ``block_5x3``,
|
|
61
|
+
``outline_20x8``, ``disc_outline_r12`` and ``rect_tas_1p037`` -- see
|
|
62
|
+
``tests/data/gdl_soda2_min_circle_fast_golden.json``.
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
**`corners` and `area` are both part of the public contract, not incidental.**
|
|
66
|
+
``aspect_ratio.pro:27-49`` branches on ``n_elements(circle.corners)``, indexes
|
|
67
|
+
the input arrays by them, and uses their order for an orientation sign;
|
|
68
|
+
``soda2_findsize.pro:34`` stores ``circle.area`` as ``out.perimeterarea``.
|
|
69
|
+
``area`` is a misnomer worth keeping and documenting: it is simply
|
|
70
|
+
``n_elements(xin)``, the count of points passed in (named "area" because the
|
|
71
|
+
caller passes perimeter pixels), never an actual area computation.
|
|
72
|
+
|
|
73
|
+
Provenance: ``SODA2/abansemer-soda2-afcea15/min_circle_fast.pro`` (82 lines,
|
|
74
|
+
whole file). Calls :func:`arrayprobe_soda2.angles.find_angle`,
|
|
75
|
+
:func:`arrayprobe_soda2.angles.find_angle_array` and
|
|
76
|
+
:func:`arrayprobe_soda2.find_radius.find_radius`, all three already
|
|
77
|
+
GDL-validated leaves -- composed here, not re-translated. Validated against
|
|
78
|
+
GDL by ``tests/test_soda2_min_circle_fast.py``.
|
|
79
|
+
"""
|
|
80
|
+
from __future__ import annotations
|
|
81
|
+
|
|
82
|
+
import warnings
|
|
83
|
+
from collections.abc import Callable
|
|
84
|
+
from dataclasses import dataclass
|
|
85
|
+
|
|
86
|
+
import numpy as np
|
|
87
|
+
from numpy.typing import ArrayLike
|
|
88
|
+
|
|
89
|
+
from .angles import find_angle, find_angle_array
|
|
90
|
+
from .find_radius import find_radius
|
|
91
|
+
|
|
92
|
+
__all__ = [
|
|
93
|
+
"PI_SINGLE",
|
|
94
|
+
"HALF_PI_SINGLE",
|
|
95
|
+
"ITERATION_CAP",
|
|
96
|
+
"MinCircle",
|
|
97
|
+
"Trace",
|
|
98
|
+
"MinCircleCapWarning",
|
|
99
|
+
"EmptyInput",
|
|
100
|
+
"min_circle_fast",
|
|
101
|
+
"min_circle_fast_traced",
|
|
102
|
+
]
|
|
103
|
+
|
|
104
|
+
#: IDL's ``!pi``, measured under GDL: FLOAT, not DOUBLE. ``!dpi``
|
|
105
|
+
#: (``3.1415926535897931``) appears nowhere in the vendored SODA-2 tree.
|
|
106
|
+
PI_SINGLE = float(np.float32(np.pi)) # 3.1415927410125732
|
|
107
|
+
|
|
108
|
+
#: ``!pi/2`` -- FLOAT divided by an INT literal stays FLOAT in IDL.
|
|
109
|
+
HALF_PI_SINGLE = float(np.float32(np.pi) / np.float32(2)) # 1.5707963705062866
|
|
110
|
+
|
|
111
|
+
#: ``min_circle_fast.pro:48`` -- the loop is stopped once ``c`` passes 10000. The loop
|
|
112
|
+
#: body always runs at least once before the test, so the maximum iteration
|
|
113
|
+
#: count actually reached is ``ITERATION_CAP + 1`` (10 001).
|
|
114
|
+
ITERATION_CAP = 10000
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
class EmptyInput(ValueError):
|
|
118
|
+
"""``xin``/``yin`` had zero elements.
|
|
119
|
+
|
|
120
|
+
The IDL has no explicit guard for this: ``min(y, P)`` on an empty array is
|
|
121
|
+
itself an IDL error, so there is no return value from the original to
|
|
122
|
+
reproduce here -- raising is the port's own choice, not a translated one.
|
|
123
|
+
"""
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
class MinCircleCapWarning(UserWarning):
|
|
127
|
+
"""Opt-in only (see ``warn_on_cap``): the 10001-iteration cap was hit.
|
|
128
|
+
|
|
129
|
+
The IDL emits nothing here -- no flag, no message -- so a warning is
|
|
130
|
+
itself a behaviour change relative to the source, and this project's own
|
|
131
|
+
tests run degenerate inputs under ``warnings.simplefilter("error")``,
|
|
132
|
+
where a warning becomes an exception the IDL never raises. Silent by
|
|
133
|
+
default; see :func:`min_circle_fast_traced` for the always-on channel.
|
|
134
|
+
"""
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
@dataclass(frozen=True)
|
|
138
|
+
class MinCircle:
|
|
139
|
+
"""The IDL anonymous structure ``{diam, center, corners, area}``.
|
|
140
|
+
|
|
141
|
+
``diam`` is a **diameter** (the source's own comment: ``;RETURNS
|
|
142
|
+
*DIAMETER*``). ``corners`` is 0-based indices into ``xin``/``yin`` in the
|
|
143
|
+
order the IDL builds them -- ``[P, Q]``, ``[P, Q, R]``, or the fallback's
|
|
144
|
+
``[i_max, i_min]`` -- consumed by ``aspect_ratio.pro:27-49`` for both its
|
|
145
|
+
branch and its orientation sign. ``area`` is ``len(xin)``, not a
|
|
146
|
+
geometric area; kept under this name because ``soda2_findsize.pro:34``
|
|
147
|
+
reads it as ``circle.area``.
|
|
148
|
+
"""
|
|
149
|
+
|
|
150
|
+
diam: float
|
|
151
|
+
center: np.ndarray
|
|
152
|
+
corners: np.ndarray
|
|
153
|
+
area: int
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
@dataclass(frozen=True)
|
|
157
|
+
class Trace:
|
|
158
|
+
"""What the IDL computes but never returns -- exposed additively.
|
|
159
|
+
|
|
160
|
+
``niter`` is the number of times the ``REPEAT...UNTIL`` body ran (so
|
|
161
|
+
``ITERATION_CAP + 1`` marks a capped run); ``hit_cap`` and ``fell_back``
|
|
162
|
+
name the two mechanisms SODA-2 register entry S2-h found to be one (the fallback's
|
|
163
|
+
``finite()`` test at ``:64`` is, for finite duplicate-free input, only
|
|
164
|
+
ever reached after the cap); ``q0`` is the initial ``Q`` chosen at
|
|
165
|
+
``:21``, before any swap.
|
|
166
|
+
"""
|
|
167
|
+
|
|
168
|
+
niter: int
|
|
169
|
+
hit_cap: bool
|
|
170
|
+
fell_back: bool
|
|
171
|
+
q0: int | None
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def _two_point_circle(
|
|
175
|
+
x: np.ndarray, y: np.ndarray, p: int, q: int,
|
|
176
|
+
) -> tuple[np.floating, np.ndarray]:
|
|
177
|
+
r = np.sqrt((x[p] - x[q]) ** 2 + (y[p] - y[q]) ** 2) / 2.0
|
|
178
|
+
center = np.array([(x[p] + x[q]) / 2.0, (y[p] + y[q]) / 2.0])
|
|
179
|
+
return r, center
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def min_circle_fast_traced(
|
|
183
|
+
xin: ArrayLike, yin: ArrayLike, *, _angle_at_q: Callable[..., float] | None = None,
|
|
184
|
+
) -> tuple[MinCircle, Trace]:
|
|
185
|
+
"""``min_circle_fast(xin, yin)`` plus everything the IDL computes but
|
|
186
|
+
discards on return -- see :class:`Trace`. :func:`min_circle_fast` is a
|
|
187
|
+
thin wrapper that keeps only the first element.
|
|
188
|
+
|
|
189
|
+
Source: ``min_circle_fast.pro`` (SODA-2).
|
|
190
|
+
|
|
191
|
+
Degenerate arithmetic (``0.0/0.0``, ``x/0.0``) is kept in numpy scalars
|
|
192
|
+
throughout, never a bare Python ``float`` division -- ``x / 0.0`` on a
|
|
193
|
+
Python ``float`` raises ``ZeroDivisionError`` where IDL, and numpy, yield
|
|
194
|
+
``inf``/``nan``. Those values are the behaviour being ported: the fallback
|
|
195
|
+
at ``:64`` branches on exactly this.
|
|
196
|
+
|
|
197
|
+
``_angle_at_q`` is a **private, test-only** hook (default
|
|
198
|
+
:func:`~arrayprobe_soda2.angles.find_angle`, matching ``:33`` exactly).
|
|
199
|
+
It exists only so a formula-pairing
|
|
200
|
+
search can pass :func:`~arrayprobe_soda2.angles.find_angle_array` instead,
|
|
201
|
+
wrapped to the same ``(p1, p2, p3)`` calling convention, to look for a
|
|
202
|
+
case where the formula pairing of SODA-2 register entry S2-a changes
|
|
203
|
+
which ``corners`` come back.
|
|
204
|
+
:func:`min_circle_fast` never sets it.
|
|
205
|
+
"""
|
|
206
|
+
xin_a = np.asarray(xin).ravel() # :1 ORIGINAL type/values -- the fallback uses these, not x/y
|
|
207
|
+
yin_a = np.asarray(yin).ravel()
|
|
208
|
+
area = xin_a.size
|
|
209
|
+
if area == 0:
|
|
210
|
+
raise EmptyInput(
|
|
211
|
+
"min_circle_fast.pro has no value for an empty array -- min(y, P) "
|
|
212
|
+
"on it is itself an IDL error")
|
|
213
|
+
if area == 1: # :10
|
|
214
|
+
center = np.array([float(xin_a[0]), float(yin_a[0])], dtype=np.float64)
|
|
215
|
+
return (MinCircle(diam=0.0, center=center,
|
|
216
|
+
corners=np.array([0, 0], dtype=np.intp), area=1),
|
|
217
|
+
Trace(niter=0, hit_cap=False, fell_back=False, q0=None))
|
|
218
|
+
|
|
219
|
+
x = xin_a.astype(np.float64) # :11
|
|
220
|
+
y = yin_a.astype(np.float64) # :12
|
|
221
|
+
|
|
222
|
+
p = int(np.argmin(y)) # :14 dummy=min(y,P) -- first index on ties (GDL-measured)
|
|
223
|
+
|
|
224
|
+
b = y - y[p] # :17
|
|
225
|
+
a = np.abs(x - x[p]) # :18
|
|
226
|
+
with np.errstate(divide="ignore", invalid="ignore"):
|
|
227
|
+
# :19 angle=atan(float(b)/a) -- ONLY `b` narrows to float32; `a` is
|
|
228
|
+
# already double, so the division promotes back to double and atan
|
|
229
|
+
# runs in double (measured: typename(float(b)/a) is DOUBLE). The
|
|
230
|
+
# narrowing of `b` alone -- not a float32 atan -- is what can flip Q.
|
|
231
|
+
b_narrowed = b.astype(np.float32).astype(np.float64)
|
|
232
|
+
angle = np.arctan(b_narrowed / a)
|
|
233
|
+
angle[p] = 100.0 # :20 sentinel -- ignore P itself
|
|
234
|
+
q = int(np.nanargmin(angle)) # :21 min(angle, Q) -- GDL skips NaN (measured)
|
|
235
|
+
q0 = q
|
|
236
|
+
|
|
237
|
+
angle_at_q = _angle_at_q or find_angle
|
|
238
|
+
|
|
239
|
+
c = 0
|
|
240
|
+
r_idx = p # keeps mypy/pyflakes happy across the loop; always reassigned below
|
|
241
|
+
ang_r = 0.0
|
|
242
|
+
while True: # :25-49 REPEAT ... UNTIL not doagain (a do-while)
|
|
243
|
+
doagain = False
|
|
244
|
+
with np.errstate(divide="ignore", invalid="ignore"):
|
|
245
|
+
anglearray = np.asarray(
|
|
246
|
+
find_angle_array(x, y, x[p], y[p], x[q], y[q]), dtype=np.float64) # :29
|
|
247
|
+
anglearray = anglearray.copy()
|
|
248
|
+
anglearray[[p, q]] = 100.0 # :30
|
|
249
|
+
r_idx = int(np.nanargmin(anglearray)) # :31 GDL's min() skips NaN
|
|
250
|
+
ang_r = float(anglearray[r_idx])
|
|
251
|
+
|
|
252
|
+
# :33 dot product, NOT find_angle_array -- see the module docstring / S2-a
|
|
253
|
+
ang_q = float(angle_at_q([x[q], y[q]], [x[r_idx], y[r_idx]], [x[p], y[p]]))
|
|
254
|
+
ang_p = PI_SINGLE - ang_r - ang_q # :34 FLOAT !pi widened into double arithmetic
|
|
255
|
+
|
|
256
|
+
if ang_p >= HALF_PI_SINGLE: # :37-40 NaN >= x is False, as in IDL
|
|
257
|
+
doagain = True
|
|
258
|
+
p = r_idx
|
|
259
|
+
if ang_q >= HALF_PI_SINGLE: # :41-44
|
|
260
|
+
doagain = True
|
|
261
|
+
q = r_idx
|
|
262
|
+
|
|
263
|
+
c += 1
|
|
264
|
+
if c > ITERATION_CAP: # :47-48
|
|
265
|
+
doagain = False
|
|
266
|
+
if not doagain:
|
|
267
|
+
break
|
|
268
|
+
|
|
269
|
+
if ang_r >= HALF_PI_SINGLE: # :51 FLOAT !pi/2 -- an exact right angle fails this test
|
|
270
|
+
r, center = _two_point_circle(x, y, p, q) # :53-54
|
|
271
|
+
corners = [p, q]
|
|
272
|
+
else:
|
|
273
|
+
circ = find_radius([x[p], y[p]], [x[q], y[q]], [x[r_idx], y[r_idx]]) # :59
|
|
274
|
+
r = np.float64(circ.r)
|
|
275
|
+
center = circ.center
|
|
276
|
+
corners = [p, q, r_idx] # :60
|
|
277
|
+
|
|
278
|
+
fell_back = not np.isfinite(r) # :64 finite() is False for both NaN and +inf
|
|
279
|
+
if fell_back:
|
|
280
|
+
ixmax, ixmin = int(np.argmax(xin_a)), int(np.argmin(xin_a)) # :65
|
|
281
|
+
iymax, iymin = int(np.argmax(yin_a)), int(np.argmin(yin_a)) # :66
|
|
282
|
+
xwid = float(xin_a.max()) - float(xin_a.min())
|
|
283
|
+
ywid = float(yin_a.max()) - float(yin_a.min())
|
|
284
|
+
if xwid > ywid: # :68 strict gt -- a tie goes to y
|
|
285
|
+
p, q = ixmax, ixmin
|
|
286
|
+
else: # :71-74
|
|
287
|
+
p, q = iymax, iymin
|
|
288
|
+
r, center = _two_point_circle(x, y, p, q) # :75-76
|
|
289
|
+
corners = [p, q] # :78
|
|
290
|
+
|
|
291
|
+
circle = MinCircle(
|
|
292
|
+
diam=float(2.0 * r),
|
|
293
|
+
center=np.asarray(center, dtype=np.float64),
|
|
294
|
+
corners=np.array(corners, dtype=np.intp),
|
|
295
|
+
area=int(area),
|
|
296
|
+
)
|
|
297
|
+
trace = Trace(niter=c, hit_cap=c > ITERATION_CAP, fell_back=fell_back, q0=q0)
|
|
298
|
+
return circle, trace
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
def min_circle_fast(xin: ArrayLike, yin: ArrayLike, *, warn_on_cap: bool = False) -> MinCircle:
|
|
302
|
+
"""``min_circle_fast(xin, yin)`` -- SODA-2's minimum bounding circle.
|
|
303
|
+
|
|
304
|
+
Source: ``min_circle_fast.pro`` (SODA-2).
|
|
305
|
+
|
|
306
|
+
``warn_on_cap`` is an opt-in flag, **defaulting OFF** per this project's
|
|
307
|
+
fidelity doctrine: the IDL returns whatever it holds at the 10001st
|
|
308
|
+
iteration with no signal at all, so silence is the faithful default. When
|
|
309
|
+
``True`` and the cap was hit, emits :class:`MinCircleCapWarning`; it
|
|
310
|
+
changes no returned field (pinned by a silent-by-default test).
|
|
311
|
+
"""
|
|
312
|
+
circle, trace = min_circle_fast_traced(xin, yin)
|
|
313
|
+
if warn_on_cap and trace.hit_cap:
|
|
314
|
+
warnings.warn(
|
|
315
|
+
MinCircleCapWarning(
|
|
316
|
+
f"iteration cap {ITERATION_CAP} reached after {trace.niter} "
|
|
317
|
+
"iterations; result is whatever P/Q/R were held, as in "
|
|
318
|
+
"min_circle_fast.pro:48"),
|
|
319
|
+
stacklevel=2)
|
|
320
|
+
return circle
|