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,541 @@
|
|
|
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
|
+
"""``aspect_ratio.pro`` -- SODA-2's rotation-invariant aspect ratio, the last
|
|
4
|
+
file of the geometry core.
|
|
5
|
+
|
|
6
|
+
**The real dependency list, measured (a grep of every call in the 106
|
|
7
|
+
lines), and a correction to this doc's own earlier call graph.** V1.4's call
|
|
8
|
+
graph reads ``aspect_ratio <- calls min_circle_fast + princomp``. That is
|
|
9
|
+
wrong: ``grep -c princomp aspect_ratio.pro`` is 0. The routine calls
|
|
10
|
+
``min_circle_fast`` once, :func:`arrayprobe_soda2.angles.find_angle` three
|
|
11
|
+
times, IDL's ``erode`` (only when the image has more than two slices), and a
|
|
12
|
+
handful of intrinsics (``byte``, ``size``, ``where``, ``mod``, ``/``, ``min``,
|
|
13
|
+
``max``, ``atan``, ``float``, ``sqrt``). ``princomp`` and ``find_angle_array``
|
|
14
|
+
are not called here at all -- do not substitute either for ``find_angle``.
|
|
15
|
+
|
|
16
|
+
**What it computes.** An aspect ratio in ``[0, 1]``, rotation-invariant up to
|
|
17
|
+
pixelisation, from the particle's minimum enclosing circle (``min_circle_fast``,
|
|
18
|
+
ported separately):
|
|
19
|
+
|
|
20
|
+
1. Outline pixels (:func:`outline_indices`) or given coordinates.
|
|
21
|
+
2. ``y_ind *= tas_adjust`` -- the true-airspeed / pixel-aspect correction,
|
|
22
|
+
applied to the geometry only; the ``y_ind`` keyword hands back the
|
|
23
|
+
*unadjusted* indices, which this port mirrors via
|
|
24
|
+
:class:`AspectRatioResult`.
|
|
25
|
+
3. ``circle = min_circle_fast(x_ind, y_ind)``.
|
|
26
|
+
4. The major axis: for a 3-point circle, the line from the vertex of smallest
|
|
27
|
+
interior angle to the circle centre (a radius); for a 2-point circle, the
|
|
28
|
+
line through the two points (a diameter). See finding S2-c (the other
|
|
29
|
+
pair's): ``min_circle_fast`` compares against **FLOAT** ``!pi/2``
|
|
30
|
+
(:math:`1.5707963705062866`), which is *greater* than an exact right angle
|
|
31
|
+
(:math:`\\mathrm{acos}(0) = 1.5707963267948966`) -- so squares, axis-aligned
|
|
32
|
+
rectangles and digitised discs all take the **3-point** branch, not the
|
|
33
|
+
2-point one a hand-picked case list might expect. This module does not
|
|
34
|
+
choose that branch; it only reads whatever ``circle.corners`` it is given,
|
|
35
|
+
so the consequence lands entirely in the golden's recorded corner counts
|
|
36
|
+
(``:func:`axis_and_ratio``'s own docstring below).
|
|
37
|
+
5. ``orientation`` -- degrees, folded to (-90, 90] with respect to the array
|
|
38
|
+
axis.
|
|
39
|
+
6. ``width`` = (max signed perpendicular distance from the axis) - (min) + 1
|
|
40
|
+
px ("Korolev and Isaac 2003"); ``aspr = width / (circle.diam + 1)``,
|
|
41
|
+
returned as ``min(aspr, 1.0)``.
|
|
42
|
+
|
|
43
|
+
**``!pi`` is IDL's single-precision pi**, :math:`3.1415927410125732`, not
|
|
44
|
+
:math:`3.141592653589793` -- measured under GDL (the other geometry pair's finding,
|
|
45
|
+
folded in here because line :53 divides by it). ``!dpi`` never appears in the
|
|
46
|
+
SODA-2 tree. :func:`orientation_deg` uses ``np.float32(np.pi)``, not
|
|
47
|
+
``np.pi``, for exactly this reason; see
|
|
48
|
+
``tests/test_soda2_aspect_ratio.py``'s ``PiIsSinglePrecision`` class for a
|
|
49
|
+
case where the double-vs-float difference is measured directly rather than
|
|
50
|
+
left to a tolerance that happens to be tight enough to catch it.
|
|
51
|
+
|
|
52
|
+
**Every ``<``/``>`` in the file, classified** (design record V1.3's hazard,
|
|
53
|
+
confirmed line by line rather than assumed):
|
|
54
|
+
|
|
55
|
+
===== ======================================= ===============================
|
|
56
|
+
line text class
|
|
57
|
+
===== ======================================= ===============================
|
|
58
|
+
13 ``IF s[2] gt 2`` genuine comparison (``gt``)
|
|
59
|
+
24 ``n_elements(tas_adjust) gt 0`` genuine comparison
|
|
60
|
+
27,89 ``eq 3``, ``eq 2`` genuine comparison
|
|
61
|
+
38 ``min([angP,angQ,angR], vertex)`` the ``min`` **function**
|
|
62
|
+
**66** ``den=sqrt(a^2+b^2) > 1`` **MAX**: ``max(den, 1)``
|
|
63
|
+
**105** ``return, aspr < 1.0`` **MIN**: ``min(aspr, 1.0)``
|
|
64
|
+
===== ======================================= ===============================
|
|
65
|
+
|
|
66
|
+
Line 105's comment reads "Eliminate round-off errors". Measured, it does more
|
|
67
|
+
than that: **a non-finite ``aspr`` also becomes exactly 1.0**, because IDL's
|
|
68
|
+
``<`` returns the *other* operand when one side is NaN
|
|
69
|
+
(``!values.d_nan < 1.0d`` prints ``1.0000000`` under GDL) -- finding S2-d, in
|
|
70
|
+
the design doc section this module's docstring points to. The symmetric
|
|
71
|
+
behaviour for ``>`` at line 66 is assumed by the same convention, not
|
|
72
|
+
separately measured; the code implementing it (:func:`axis_and_ratio`) says so
|
|
73
|
+
where it matters.
|
|
74
|
+
|
|
75
|
+
**Three same-rank IDL hazards, each with its own test:**
|
|
76
|
+
|
|
77
|
+
- ``mod`` at line 57 takes the *dividend's* sign (``-450.0 mod 180 = -90``,
|
|
78
|
+
measured) -- :func:`orientation_deg` uses ``np.fmod``, never Python ``%``.
|
|
79
|
+
- ``where`` with no match returns the scalar ``-1``, count 0; IDL's ``-1/nx``
|
|
80
|
+
truncates to 0 and ``-1 mod nx`` stays ``-1`` (dividend's sign again) --
|
|
81
|
+
:func:`outline_indices` returns exactly ``([-1], [0], 0)`` for an empty
|
|
82
|
+
image rather than empty arrays.
|
|
83
|
+
- ``orientation`` is computed in **single precision throughout** (``float()``
|
|
84
|
+
at line 53, ``!pi`` itself is FLOAT); the 2-point branch's ``distmajor`` is
|
|
85
|
+
FLOAT whatever the input dtype (``sqrt(LONG)`` promotes to FLOAT, so does
|
|
86
|
+
``LONG/FLOAT``), but the 3-point branch's is DOUBLE, because
|
|
87
|
+
``circle.center`` is double. NumPy's own promotion rule for
|
|
88
|
+
``int32 * float32`` is **float64**, which is *not* IDL's ``LONG*FLOAT ->
|
|
89
|
+
FLOAT`` -- :func:`axis_and_ratio` routes every such mixed operation through
|
|
90
|
+
:func:`_idl_promote` instead of raw NumPy operators, specifically to avoid
|
|
91
|
+
that silent widening.
|
|
92
|
+
|
|
93
|
+
**The oracle gap: GDL has no ``ERODE``.** ``erode`` runs only when the image
|
|
94
|
+
has more than two slices along airflow (``s[2] gt 2``), so GDL runs
|
|
95
|
+
``aspect_ratio.pro`` **unmodified** for coordinate input and for images with
|
|
96
|
+
at most two slices -- doctrine layer 2, no shim needed. For images with more
|
|
97
|
+
than two slices, the GDL oracle bank supplies its own ``erode.pro`` (a plain
|
|
98
|
+
4-connected cross erosion), and that reading of IDL's edge behaviour at the
|
|
99
|
+
image border is **not confirmed** -- the IDL/NV5 ERODE reference pages did not
|
|
100
|
+
resolve when checked (see the design doc section). ``ERODE_EDGE_RULE`` is the
|
|
101
|
+
one switch this module exposes for the two candidate readings:
|
|
102
|
+
|
|
103
|
+
- ``"zero_outside"`` -- a pixel outside the image counts as 0, so every pixel
|
|
104
|
+
touching the image border is eroded away (a tight, border-touching ROI has
|
|
105
|
+
its whole boundary in the outline).
|
|
106
|
+
- ``"clip_element"`` -- a missing neighbour is simply not required to be 1
|
|
107
|
+
(as if the structuring element were clipped to the image), so a border
|
|
108
|
+
pixel can survive erosion.
|
|
109
|
+
|
|
110
|
+
Both are implemented and tested; the module default is ``"zero_outside"``,
|
|
111
|
+
labelled unconfirmed everywhere it is exercised. A pixel more than one row
|
|
112
|
+
away from every border sees the same answer under both rules, which is why
|
|
113
|
+
the padded family in the test suite is rule-*independent* evidence and the
|
|
114
|
+
unpadded one is not.
|
|
115
|
+
|
|
116
|
+
Provenance: ``SODA2/abansemer-soda2-afcea15/aspect_ratio.pro``, whole file
|
|
117
|
+
minus the ``makeplot`` block (lines 76-102), which is plotting only and has
|
|
118
|
+
no numeric effect. Validated against GDL by
|
|
119
|
+
``tests/test_soda2_aspect_ratio.py``; the composition test
|
|
120
|
+
(:func:`aspect_ratio` end to end) needs ``arrayprobe_soda2.min_circle_fast``
|
|
121
|
+
(the concurrent S2-c pair's module) and skips at method level via
|
|
122
|
+
:class:`MinCircleFastNotPorted` if it is ever absent.
|
|
123
|
+
"""
|
|
124
|
+
from __future__ import annotations
|
|
125
|
+
|
|
126
|
+
from collections.abc import Mapping
|
|
127
|
+
from dataclasses import dataclass
|
|
128
|
+
from typing import Any
|
|
129
|
+
|
|
130
|
+
import numpy as np
|
|
131
|
+
|
|
132
|
+
from arrayprobe_soda2.angles import find_angle, find_angle_f32
|
|
133
|
+
|
|
134
|
+
__all__ = [
|
|
135
|
+
"ERODE_EDGE_RULE",
|
|
136
|
+
"AspectRatioResult",
|
|
137
|
+
"MinCircleFastNotPorted",
|
|
138
|
+
"aspect_ratio",
|
|
139
|
+
"axis_and_ratio",
|
|
140
|
+
"orientation_deg",
|
|
141
|
+
"outline_indices",
|
|
142
|
+
]
|
|
143
|
+
|
|
144
|
+
#: Which reading of IDL's (GDL-absent) ERODE border behaviour this module
|
|
145
|
+
#: uses by default. See the module docstring's "oracle gap" section -- this
|
|
146
|
+
#: is a declared, tested switch, not a confirmed fact. Read at call time
|
|
147
|
+
#: (functions default their keyword to ``None`` and resolve it here), so a
|
|
148
|
+
#: test can monkeypatch this module attribute and have it take effect.
|
|
149
|
+
ERODE_EDGE_RULE = "zero_outside"
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
class MinCircleFastNotPorted(ImportError):
|
|
153
|
+
"""``arrayprobe_soda2.min_circle_fast`` has not landed yet (a concurrent
|
|
154
|
+
pair owns that port). Raised lazily, at call time, so importing this
|
|
155
|
+
module never requires it -- only calling :func:`aspect_ratio` does."""
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
@dataclass(frozen=True)
|
|
159
|
+
class AspectRatioResult:
|
|
160
|
+
"""Everything ``aspect_ratio.pro`` returns, keyword outputs included.
|
|
161
|
+
|
|
162
|
+
``circle`` is whatever :func:`arrayprobe_soda2.min_circle_fast.min_circle_fast`
|
|
163
|
+
returns -- an object or mapping with ``diam``, ``center`` (2-element) and
|
|
164
|
+
``corners`` (2 or 3 zero-based indices into ``x_ind``/``y_ind``).
|
|
165
|
+
``y_ind`` is the **unadjusted** indices, exactly as IDL's ``y_ind_orig``
|
|
166
|
+
keyword output is (:74 in the source): the TAS correction is applied to a
|
|
167
|
+
local copy only, and the keyword hands back the copy made before that.
|
|
168
|
+
"""
|
|
169
|
+
|
|
170
|
+
aspr: np.float64
|
|
171
|
+
orientation: np.float32
|
|
172
|
+
circle: Any
|
|
173
|
+
x_ind: np.ndarray
|
|
174
|
+
y_ind: np.ndarray
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def _field(circle: Any, name: str) -> Any:
|
|
178
|
+
"""Read ``name`` off ``circle``, which may be an attribute-bearing object
|
|
179
|
+
(a dataclass, a ``SimpleNamespace``, the eventual ``min_circle_fast``
|
|
180
|
+
return type) or a mapping (the golden's own records, read back as
|
|
181
|
+
dicts). Both must work so the differential tests can build a fake circle
|
|
182
|
+
straight from the GDL golden without needing the other pair's module."""
|
|
183
|
+
if isinstance(circle, Mapping):
|
|
184
|
+
return circle[name]
|
|
185
|
+
return getattr(circle, name)
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def _to_byte(img_in: np.ndarray) -> np.ndarray:
|
|
189
|
+
"""IDL ``byte(img_in)``: truncate (not round) toward zero for floating
|
|
190
|
+
input, then wrap into ``[0, 255]``. Every case this port is fed is already
|
|
191
|
+
strictly 0/1, so this only matters as documented behaviour, not as a
|
|
192
|
+
tested path."""
|
|
193
|
+
arr = np.asarray(img_in)
|
|
194
|
+
if arr.dtype.kind == "f":
|
|
195
|
+
arr = np.trunc(arr)
|
|
196
|
+
return np.mod(arr.astype(np.int64), 256).astype(np.uint8)
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def _erode_cross(img01: np.ndarray, edge_rule: str) -> np.ndarray:
|
|
200
|
+
"""4-connected (cross) binary erosion -- GDL 1.1.2 has no ``ERODE``
|
|
201
|
+
(measured: ``Attempt to call undefined function: ERODE``), so this is the
|
|
202
|
+
port's own implementation, not a translation of one. ``edge_rule`` picks
|
|
203
|
+
between the two candidate readings of IDL's real border behaviour, which
|
|
204
|
+
is unconfirmed (module docstring). A pixel is retained iff it and all
|
|
205
|
+
four of its cross neighbours are 1; a neighbour that falls outside the
|
|
206
|
+
image is treated as 0 under ``"zero_outside"`` and simply not required
|
|
207
|
+
under ``"clip_element"``.
|
|
208
|
+
"""
|
|
209
|
+
fg = img01 != 0
|
|
210
|
+
up = np.zeros_like(fg)
|
|
211
|
+
up[1:, :] = fg[:-1, :]
|
|
212
|
+
down = np.zeros_like(fg)
|
|
213
|
+
down[:-1, :] = fg[1:, :]
|
|
214
|
+
left = np.zeros_like(fg)
|
|
215
|
+
left[:, 1:] = fg[:, :-1]
|
|
216
|
+
right = np.zeros_like(fg)
|
|
217
|
+
right[:, :-1] = fg[:, 1:]
|
|
218
|
+
|
|
219
|
+
if edge_rule == "zero_outside":
|
|
220
|
+
eroded = fg & up & down & left & right
|
|
221
|
+
elif edge_rule == "clip_element":
|
|
222
|
+
border_up = np.zeros_like(fg)
|
|
223
|
+
border_up[0, :] = True
|
|
224
|
+
border_down = np.zeros_like(fg)
|
|
225
|
+
border_down[-1, :] = True
|
|
226
|
+
border_left = np.zeros_like(fg)
|
|
227
|
+
border_left[:, 0] = True
|
|
228
|
+
border_right = np.zeros_like(fg)
|
|
229
|
+
border_right[:, -1] = True
|
|
230
|
+
eroded = (
|
|
231
|
+
fg
|
|
232
|
+
& (up | border_up)
|
|
233
|
+
& (down | border_down)
|
|
234
|
+
& (left | border_left)
|
|
235
|
+
& (right | border_right)
|
|
236
|
+
)
|
|
237
|
+
else:
|
|
238
|
+
raise ValueError(f"unknown edge_rule {edge_rule!r}; expected "
|
|
239
|
+
"'zero_outside' or 'clip_element'")
|
|
240
|
+
return eroded.astype(np.uint8)
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
def outline_indices(
|
|
244
|
+
img: np.ndarray, *, edge_rule: str | None = None
|
|
245
|
+
) -> tuple[np.ndarray, np.ndarray, int]:
|
|
246
|
+
"""``aspect_ratio.pro`` lines 11-17: outline pixel indices of an image.
|
|
247
|
+
|
|
248
|
+
``img`` is ``(n_slices, n_diodes)`` -- y (along airflow) first, x (across
|
|
249
|
+
the array) second, matching how this port holds every other SODA-2/UIOOPS
|
|
250
|
+
image. IDL's array is column-major with x as dimension 1, so its flat
|
|
251
|
+
``where`` index order (x fastest) is exactly this array's C-order
|
|
252
|
+
``ravel()`` -- **this equivalence is load-bearing** for
|
|
253
|
+
``min_circle_fast``'s tie-breaking on the returned index order, so it is
|
|
254
|
+
asserted directly by ``OutlineAndIndexOrderMatchIdl`` rather than only
|
|
255
|
+
implied by matching final answers.
|
|
256
|
+
|
|
257
|
+
A 1-D ``img`` is treated as a single slice (no erosion), which is also
|
|
258
|
+
what IDL does here, but not for the reason it looks like: after
|
|
259
|
+
``img = byte(img_in)``, IDL's ``size(img)`` for a 1-D array is
|
|
260
|
+
``[1, n, type, n_elements]``, so ``s[2]`` is the **type code** (1, for
|
|
261
|
+
BYTE), never the y-dimension, and 1 is never greater than 2. The outline
|
|
262
|
+
step is skipped by coincidence, not by an explicit 1-D branch -- SODA-2
|
|
263
|
+
register entry S2-g. It is harmless in production because ``soda2_findsize.pro`` reforms
|
|
264
|
+
1-D images before calling this routine.
|
|
265
|
+
|
|
266
|
+
Returns ``(x_ind, y_ind, area)``, both ``int32``. An image with nothing
|
|
267
|
+
shadowed returns exactly ``([-1], [0], 0)`` -- IDL's ``where`` returns the
|
|
268
|
+
scalar ``-1`` with count 0 on no match, and ``-1 mod nx`` keeps the
|
|
269
|
+
dividend's sign (stays ``-1``) while IDL's ``/`` truncates toward zero
|
|
270
|
+
(``-1/nx`` is 0 for any ``nx > 1``), not the floor NumPy's ``//`` would
|
|
271
|
+
give.
|
|
272
|
+
"""
|
|
273
|
+
if edge_rule is None:
|
|
274
|
+
edge_rule = ERODE_EDGE_RULE
|
|
275
|
+
|
|
276
|
+
byte_img = _to_byte(img)
|
|
277
|
+
if byte_img.ndim <= 1:
|
|
278
|
+
# 1-D input: single slice, erosion never applies (see docstring).
|
|
279
|
+
row = byte_img.reshape(1, -1) if byte_img.ndim == 1 else byte_img.reshape(1, 1)
|
|
280
|
+
im = row
|
|
281
|
+
nx = im.shape[1]
|
|
282
|
+
else:
|
|
283
|
+
ny, nx = byte_img.shape
|
|
284
|
+
if ny > 2:
|
|
285
|
+
im = _erode_cross(byte_img, edge_rule) ^ byte_img
|
|
286
|
+
else:
|
|
287
|
+
im = byte_img
|
|
288
|
+
|
|
289
|
+
z = np.flatnonzero(im.ravel(order="C") == 1)
|
|
290
|
+
if z.size == 0:
|
|
291
|
+
return np.array([-1], dtype=np.int32), np.array([0], dtype=np.int32), 0
|
|
292
|
+
x_ind = (z % nx).astype(np.int32)
|
|
293
|
+
y_ind = (z // nx).astype(np.int32)
|
|
294
|
+
return x_ind, y_ind, int(z.size)
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
def orientation_deg(cx1: Any, cy1: Any, cx2: Any, cy2: Any) -> np.float32:
|
|
298
|
+
"""``aspect_ratio.pro`` lines 53 and 57: orientation in degrees.
|
|
299
|
+
|
|
300
|
+
``atan(float(cy2-cy1), float(cx2-cx1)) * 180/!pi``, then folded to
|
|
301
|
+
``(-90, 90]`` by ``((orientation - 360 - 90) mod 180) + 90``.
|
|
302
|
+
|
|
303
|
+
Each difference is computed at its *native* precision first (whatever
|
|
304
|
+
dtype the caller's ``cx1``/``cy1``/``cx2``/``cy2`` actually carry -- the
|
|
305
|
+
only mixing that ever occurs here is with ``float64``
|
|
306
|
+
(:func:`axis_and_ratio`'s 3-point branch, via ``circle.center``), which
|
|
307
|
+
NumPy promotes to unambiguously, exactly as IDL would) and *then*
|
|
308
|
+
narrowed to ``float32`` with an explicit cast -- mirroring IDL's
|
|
309
|
+
``float(cy2-cy1)``, which narrows after subtracting, not before. ``!pi``
|
|
310
|
+
is IDL's **single-precision** pi (measured under GDL:
|
|
311
|
+
``3.1415927410125732``, not ``3.141592653589793``); ``!dpi`` is never used
|
|
312
|
+
in this tree. ``np.fmod``, not Python's ``%``, for the ``mod`` at line 57
|
|
313
|
+
-- IDL's ``mod`` takes the dividend's sign, and ``np.fmod`` does too.
|
|
314
|
+
"""
|
|
315
|
+
dy = np.float32(np.asarray(cy2) - np.asarray(cy1))
|
|
316
|
+
dx = np.float32(np.asarray(cx2) - np.asarray(cx1))
|
|
317
|
+
pi32 = np.float32(np.pi) # IDL's `!pi` is FLOAT, not DOUBLE -- measured.
|
|
318
|
+
|
|
319
|
+
# ``atan`` on FLOAT: the double atan2 rounded to FLOAT (platform ``atan2f``
|
|
320
|
+
# differs by one ULP under glibc, which the fmod chain below amplifies).
|
|
321
|
+
orientation = np.float32(np.arctan2(np.float64(dy), np.float64(dx))) * np.float32(180)
|
|
322
|
+
orientation = orientation / pi32
|
|
323
|
+
orientation = (
|
|
324
|
+
np.fmod(orientation - np.float32(360) - np.float32(90), np.float32(180))
|
|
325
|
+
+ np.float32(90)
|
|
326
|
+
)
|
|
327
|
+
return np.float32(orientation)
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
def _idl_promote(dtype_a: Any, dtype_b: Any) -> type:
|
|
331
|
+
"""The result dtype of an IDL binary numeric op between LONG/FLOAT/DOUBLE
|
|
332
|
+
operands (the only three that occur in this routine): DOUBLE beats FLOAT
|
|
333
|
+
beats LONG. NumPy's own promotion of ``int32`` with ``float32`` is
|
|
334
|
+
``float64`` -- not IDL's ``LONG*FLOAT -> FLOAT`` -- so callers must route
|
|
335
|
+
through this and :func:`_op` rather than the bare NumPy operators.
|
|
336
|
+
"""
|
|
337
|
+
def rank(dt: Any) -> int:
|
|
338
|
+
dt = np.dtype(dt)
|
|
339
|
+
if dt.kind in "iu":
|
|
340
|
+
return 0
|
|
341
|
+
if dt == np.dtype(np.float32):
|
|
342
|
+
return 1
|
|
343
|
+
if dt == np.dtype(np.float64):
|
|
344
|
+
return 2
|
|
345
|
+
raise TypeError(f"unexpected dtype in aspect_ratio arithmetic: {dt}")
|
|
346
|
+
|
|
347
|
+
order = max(rank(dtype_a), rank(dtype_b))
|
|
348
|
+
return (np.int32, np.float32, np.float64)[order]
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
def _op(x: Any, y: Any, fn: Any) -> np.ndarray:
|
|
352
|
+
"""Apply a binary NumPy ufunc under IDL's promotion rule (see
|
|
353
|
+
:func:`_idl_promote`) instead of NumPy's own: both operands are cast to
|
|
354
|
+
the IDL result dtype *before* the ufunc runs, so the actual NumPy
|
|
355
|
+
operation that executes is same-dtype and unambiguous.
|
|
356
|
+
"""
|
|
357
|
+
xa = np.asarray(x)
|
|
358
|
+
ya = np.asarray(y)
|
|
359
|
+
dt = _idl_promote(xa.dtype, ya.dtype)
|
|
360
|
+
return fn(xa.astype(dt), ya.astype(dt))
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
def _sqrt_idl(x: Any) -> np.ndarray:
|
|
364
|
+
"""IDL ``sqrt``: LONG always promotes to FLOAT (never stays integer);
|
|
365
|
+
FLOAT stays FLOAT, DOUBLE stays DOUBLE."""
|
|
366
|
+
xa = np.asarray(x)
|
|
367
|
+
out_dtype = np.float32 if xa.dtype.kind in "iu" else xa.dtype
|
|
368
|
+
return np.sqrt(xa.astype(out_dtype)).astype(out_dtype)
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
def axis_and_ratio(
|
|
372
|
+
x_ind: np.ndarray, y_ind_adjusted: np.ndarray, circle: Any
|
|
373
|
+
) -> tuple[np.float64, np.float32]:
|
|
374
|
+
"""``aspect_ratio.pro`` lines 27-73 and 105, given an already-computed
|
|
375
|
+
``circle``. Returns ``(aspr, orientation)``.
|
|
376
|
+
|
|
377
|
+
``circle`` needs ``corners`` (2 or 3 zero-based indices into ``x_ind`` /
|
|
378
|
+
``y_ind_adjusted``), ``center`` (2-element, DOUBLE in the source) and
|
|
379
|
+
``diam`` (DOUBLE); see :func:`_field` for the attribute-or-mapping
|
|
380
|
+
accessor that lets this take either a real ``min_circle_fast`` result or
|
|
381
|
+
a ``SimpleNamespace``/``dict`` built straight from the GDL golden.
|
|
382
|
+
|
|
383
|
+
**Dtype.** ``y_ind_adjusted``'s own dtype is IDL's "Ty": ``int32`` if no
|
|
384
|
+
TAS correction was applied, ``float32`` in production
|
|
385
|
+
(``tas_adjust`` is a FLOAT keyword), ``float64`` only if a caller passes a
|
|
386
|
+
genuine DOUBLE. In the 2-point branch, ``a`` (``cy1-cy2``) and the running
|
|
387
|
+
sums stay in Ty; ``b`` (``cx2-cx1``) is always ``int32`` (``x_ind`` is
|
|
388
|
+
always LONG) but gets promoted to Ty the moment it multiplies against a
|
|
389
|
+
Ty operand, via :func:`_op`/:func:`_idl_promote` rather than NumPy's own
|
|
390
|
+
(wider) promotion. In the 3-point branch ``cx2``/``cy2`` come from
|
|
391
|
+
``circle.center``, which is DOUBLE, so **everything** downstream --
|
|
392
|
+
``a``, ``b``, ``c``, ``den``, ``distmajor`` -- ends up ``float64``
|
|
393
|
+
regardless of Ty; this falls out of :func:`_idl_promote` automatically
|
|
394
|
+
once ``cx2``/``cy2`` are cast to ``float64`` up front, with no separate
|
|
395
|
+
branch needed. ``aspr`` is always returned as ``float64`` (``circle.diam``
|
|
396
|
+
is DOUBLE, so ``width / (diam + 1)`` is DOUBLE regardless of ``width``'s
|
|
397
|
+
own dtype).
|
|
398
|
+
|
|
399
|
+
**The two clamps.** ``den = sqrt(a^2+b^2) > 1`` (MAX) and the final
|
|
400
|
+
``return, aspr < 1.0`` (MIN) are both written as a Python ternary
|
|
401
|
+
(``x if x > floor else floor`` / ``x if x < ceiling else ceiling``) rather
|
|
402
|
+
than ``max``/``min``, because Python's comparison operators already give
|
|
403
|
+
``False`` for a NaN operand, which is exactly IDL's own measured
|
|
404
|
+
behaviour for ``<`` (SODA-2 register entry S2-d: ``!values.d_nan < 1.0d`` is ``1.0`` under
|
|
405
|
+
GDL) -- so the *ternary form* reproduces the NaN case for free, where
|
|
406
|
+
calling ``min()``/``max()`` would not reliably. The ``>`` side is written
|
|
407
|
+
the same way for consistency; only the ``<`` case has actually been
|
|
408
|
+
measured against real IDL/GDL.
|
|
409
|
+
"""
|
|
410
|
+
x_ind = np.asarray(x_ind)
|
|
411
|
+
y_ind = np.asarray(y_ind_adjusted)
|
|
412
|
+
corners = np.asarray(_field(circle, "corners")).ravel()
|
|
413
|
+
|
|
414
|
+
if corners.size == 3:
|
|
415
|
+
p_idx, q_idx, r_idx = (int(corners[0]), int(corners[1]), int(corners[2]))
|
|
416
|
+
# Measured (the min_circle_fast pair, folded in here 2026-09-12):
|
|
417
|
+
# `[x_ind[P], y_ind[P]]` is an IDL array LITERAL combining a LONG
|
|
418
|
+
# (`x_ind`) with Ty (`y_ind`), so it takes IDL's own array-promotion
|
|
419
|
+
# type -- FLOAT whenever Ty is int32 or float32 (`total()`/`acos`
|
|
420
|
+
# inside find_angle.pro cannot stay LONG, the same "sqrt/trig always
|
|
421
|
+
# promotes" rule as :func:`_sqrt_idl`), DOUBLE only when Ty is
|
|
422
|
+
# float64. Measured: `find_angle([0L,0.0],[1L,0.0],[1L,1e-3])` is
|
|
423
|
+
# FLOAT `9.765625e-04`, against `9.99999667e-04` in double -- a 2.3%
|
|
424
|
+
# difference, and :38's `min(..., vertex)` can pick a **different
|
|
425
|
+
# corner** on a near-tie at that resolution. `find_angle` itself is
|
|
426
|
+
# owned by the concurrent min_circle_fast pair and is not edited
|
|
427
|
+
# here; this only controls what precision *this* call site feeds it
|
|
428
|
+
# with, so the effect is inert until their centralised dtype fix
|
|
429
|
+
# lands -- until then `find_angle` still widens everything to
|
|
430
|
+
# float64 internally, exactly as before.
|
|
431
|
+
ty_trig = np.float32 if y_ind.dtype != np.float64 else np.float64
|
|
432
|
+
|
|
433
|
+
def _trig_pt(idx: int) -> np.ndarray:
|
|
434
|
+
return np.array([x_ind[idx], y_ind[idx]], dtype=ty_trig)
|
|
435
|
+
|
|
436
|
+
p_pt, q_pt, r_pt = _trig_pt(p_idx), _trig_pt(q_idx), _trig_pt(r_idx)
|
|
437
|
+
# FLOAT find_angle on this path (measured under GDL).
|
|
438
|
+
fa = find_angle_f32 if ty_trig is np.float32 else find_angle
|
|
439
|
+
ang_p = fa(p_pt, r_pt, q_pt)
|
|
440
|
+
ang_q = fa(q_pt, r_pt, p_pt)
|
|
441
|
+
ang_r = fa(r_pt, q_pt, p_pt)
|
|
442
|
+
vertex = int(np.argmin([ang_p, ang_q, ang_r])) # first index on ties,
|
|
443
|
+
# matching IDL's `min(array, subscript)` convention.
|
|
444
|
+
corner_idx = int(corners[vertex])
|
|
445
|
+
cx1, cy1 = x_ind[corner_idx], y_ind[corner_idx]
|
|
446
|
+
center = np.asarray(_field(circle, "center"), dtype=np.float64).ravel()
|
|
447
|
+
cx2, cy2 = np.float64(center[0]), np.float64(center[1])
|
|
448
|
+
else:
|
|
449
|
+
i0, i1 = int(corners[0]), int(corners[1])
|
|
450
|
+
cx1, cy1 = x_ind[i0], y_ind[i0]
|
|
451
|
+
cx2, cy2 = x_ind[i1], y_ind[i1]
|
|
452
|
+
|
|
453
|
+
orientation = orientation_deg(cx1, cy1, cx2, cy2)
|
|
454
|
+
|
|
455
|
+
with np.errstate(invalid="ignore", divide="ignore"):
|
|
456
|
+
a = _op(cy1, cy2, np.subtract)
|
|
457
|
+
b = _op(cx2, cx1, np.subtract)
|
|
458
|
+
c = _op(_op(cx1, cy2, np.multiply), _op(cx2, cy1, np.multiply), np.subtract)
|
|
459
|
+
|
|
460
|
+
den_raw = _sqrt_idl(_op(_op(a, a, np.multiply), _op(b, b, np.multiply), np.add))
|
|
461
|
+
den_dtype = den_raw.dtype
|
|
462
|
+
den = den_raw if float(den_raw) > 1.0 else den_dtype.type(1.0)
|
|
463
|
+
|
|
464
|
+
term1 = _op(a, x_ind, np.multiply)
|
|
465
|
+
term2 = _op(b, y_ind, np.multiply)
|
|
466
|
+
numerator = _op(_op(term1, term2, np.add), c, np.add)
|
|
467
|
+
distmajor = _op(numerator, den, np.divide)
|
|
468
|
+
|
|
469
|
+
width = _op(_op(np.max(distmajor), np.min(distmajor), np.subtract), 1, np.add)
|
|
470
|
+
diam = np.float64(_field(circle, "diam"))
|
|
471
|
+
aspr_raw = _op(width, diam + 1.0, np.divide)
|
|
472
|
+
|
|
473
|
+
aspr64 = np.float64(aspr_raw)
|
|
474
|
+
aspr = aspr64 if float(aspr64) < 1.0 else np.float64(1.0)
|
|
475
|
+
return np.float64(aspr), np.float32(orientation)
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
def aspect_ratio(
|
|
479
|
+
img_or_coords: np.ndarray,
|
|
480
|
+
*,
|
|
481
|
+
tas_adjust: Any = None,
|
|
482
|
+
coordinates: bool = False,
|
|
483
|
+
edge_rule: str | None = None,
|
|
484
|
+
) -> AspectRatioResult:
|
|
485
|
+
"""``aspect_ratio.pro``, whole routine (minus the ``makeplot`` block).
|
|
486
|
+
|
|
487
|
+
``coordinates=True``: ``img_or_coords`` is an ``(n, 2)`` array of
|
|
488
|
+
``[x, y]`` integer coordinates (lines 19-21) rather than an image.
|
|
489
|
+
|
|
490
|
+
``tas_adjust`` selects IDL's own type behaviour by the *type of the
|
|
491
|
+
Python value passed*, mirroring what a caller in IDL would have to do
|
|
492
|
+
with a literal: ``None`` means the keyword was never supplied (line 24's
|
|
493
|
+
``n_elements(tas_adjust) gt 0`` is false), so ``y_ind`` stays whatever
|
|
494
|
+
integer/float type it already was; ``np.float32`` gives FLOAT, which is
|
|
495
|
+
what the real production caller passes
|
|
496
|
+
(``soda2_findsize.pro:30``, ``tas_adjust=yres/xres`` where both are FLOAT
|
|
497
|
+
fields); any other floating value -- a bare Python ``float`` or
|
|
498
|
+
``np.float64`` -- is treated as DOUBLE, exactly as IDL would treat a
|
|
499
|
+
``0.5d`` literal. **A bare Python float is therefore not "whatever is
|
|
500
|
+
convenient" here: it silently selects DOUBLE**, so a caller wanting to
|
|
501
|
+
reproduce the production path must pass ``np.float32(yres / xres)``
|
|
502
|
+
explicitly, not a plain float that happens to equal it.
|
|
503
|
+
|
|
504
|
+
The enclosing circle comes from ``arrayprobe_soda2.min_circle_fast``
|
|
505
|
+
(ported; checked against GDL by a test that is not part of this package), imported lazily. The
|
|
506
|
+
``except ImportError`` branch that raises :class:`MinCircleFastNotPorted`
|
|
507
|
+
is unreachable in a complete install and is kept so an old import of the
|
|
508
|
+
exception does not break.
|
|
509
|
+
"""
|
|
510
|
+
if not coordinates:
|
|
511
|
+
x_ind, y_ind_orig, _area = outline_indices(img_or_coords, edge_rule=edge_rule)
|
|
512
|
+
else:
|
|
513
|
+
coords = np.asarray(img_or_coords)
|
|
514
|
+
x_ind = coords[:, 0].astype(np.int32)
|
|
515
|
+
y_ind_orig = coords[:, 1].astype(np.int32)
|
|
516
|
+
|
|
517
|
+
if tas_adjust is None:
|
|
518
|
+
y_ind_adj = y_ind_orig
|
|
519
|
+
elif isinstance(tas_adjust, np.float32):
|
|
520
|
+
y_ind_adj = (y_ind_orig.astype(np.float32) * tas_adjust).astype(np.float32)
|
|
521
|
+
else:
|
|
522
|
+
y_ind_adj = y_ind_orig.astype(np.float64) * np.float64(tas_adjust)
|
|
523
|
+
|
|
524
|
+
# min_circle_fast is ported (min_circle_fast.py); the except branch is
|
|
525
|
+
# dead in a complete install and kept only for the exception's importers.
|
|
526
|
+
try:
|
|
527
|
+
from arrayprobe_soda2.min_circle_fast import min_circle_fast
|
|
528
|
+
except ImportError as exc:
|
|
529
|
+
raise MinCircleFastNotPorted(
|
|
530
|
+
"arrayprobe_soda2.min_circle_fast is not ported yet; "
|
|
531
|
+
"aspect_ratio() needs it to compute the enclosing circle. "
|
|
532
|
+
"axis_and_ratio() works without it, given a circle from "
|
|
533
|
+
"elsewhere (e.g. the GDL golden)."
|
|
534
|
+
) from exc
|
|
535
|
+
|
|
536
|
+
circle = min_circle_fast(x_ind, y_ind_adj)
|
|
537
|
+
aspr, orientation = axis_and_ratio(x_ind, y_ind_adj, circle)
|
|
538
|
+
return AspectRatioResult(
|
|
539
|
+
aspr=aspr, orientation=orientation, circle=circle,
|
|
540
|
+
x_ind=x_ind, y_ind=y_ind_orig,
|
|
541
|
+
)
|
|
@@ -0,0 +1,100 @@
|
|
|
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
|
+
"""Bug-for-bug port of ``SODA2/abansemer-soda2-afcea15/soda2_backfill.pro``.
|
|
4
|
+
|
|
5
|
+
Reconstructs a 2DC particle's unrecorded first slice from an eroded copy of
|
|
6
|
+
the first recorded one (``soda2_processbuffer.pro:678``, reached whenever
|
|
7
|
+
``restore_slice`` is set -- every particle of the 2DC branch,
|
|
8
|
+
``soda2_processbuffer.pro:114-115``).
|
|
9
|
+
|
|
10
|
+
Layout. IDL's ``image`` is ``[diode, slice]``; the port's ROI is
|
|
11
|
+
``(slice, diode)``, so IDL ``image[*,k]`` is ``image[k, :]`` here and
|
|
12
|
+
``s[1]`` (the diode count) is ``image.shape[-1]``.
|
|
13
|
+
|
|
14
|
+
Line map (``soda2_backfill.pro``):
|
|
15
|
+
|
|
16
|
+
- ``:14-15`` ``s[0] eq 1`` -- a 1-D (one-slice) image is returned unchanged.
|
|
17
|
+
- ``:16`` a zero-area image is returned unchanged.
|
|
18
|
+
- ``:18-23`` ``temp=image``; ``orig=temp[*,1]``; ``origtot=total(...)``
|
|
19
|
+
(FLOAT); fewer than two pixels on the first recorded slice returns
|
|
20
|
+
``image`` unchanged.
|
|
21
|
+
- ``:24-33`` the REPEAT loop: ``es`` grows by one element per pass (first
|
|
22
|
+
pass length 2); origin ``x0`` is ``count/2.0`` (FLOAT), 0 when the first
|
|
23
|
+
diode is shadowed, ``count`` when the last diode is (the last test wins);
|
|
24
|
+
``:31``'s erosion of ``orig`` written into ``temp`` overwrites slice 0 (IDL assignment keeps
|
|
25
|
+
``temp``'s type); the loop runs until ``total(temp[*,0]) lt origtot/2.0``.
|
|
26
|
+
The test is REPEAT...UNTIL, so the body runs at least once.
|
|
27
|
+
|
|
28
|
+
**The oracle gap: GDL has no ``ERODE``** (measured, GDL 1.1.2). The binary
|
|
29
|
+
1-D erosion here is this package's reading of IDL's documented ``ERODE``,
|
|
30
|
+
``result[i] = 1`` iff ``orig[i + j - long(x0)] ne 0`` for every ``j`` of the
|
|
31
|
+
all-ones structuring element, with a neighbour outside the array read as 0
|
|
32
|
+
(``ERODE_EDGE_RULE = "zero_outside"``, the same unconfirmed edge rule
|
|
33
|
+
``aspect_ratio.py`` uses) and a FLOAT origin truncated toward zero. The GDL
|
|
34
|
+
bank (``gdl_oracle/run_soda2_backfill_bank.py``) supplies an ``erode.pro``
|
|
35
|
+
shim implementing exactly this reading, so its golden confirms everything in
|
|
36
|
+
``soda2_backfill.pro`` *around* the erosion (layer 1 for the control flow,
|
|
37
|
+
types and stopping rule; layer 3 for ``ERODE`` itself). Under the
|
|
38
|
+
``zero_outside`` rule the loop always terminates: once ``es`` is longer than
|
|
39
|
+
the slice, the erosion is all zero and ``0 < origtot/2.0``.
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
from __future__ import annotations
|
|
43
|
+
|
|
44
|
+
import numpy as np
|
|
45
|
+
|
|
46
|
+
#: Edge rule for the 1-D ``ERODE`` stand-in; see the module docstring.
|
|
47
|
+
ERODE_EDGE_RULE = "zero_outside"
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _erode_1d(orig: np.ndarray, n: int, x0: float) -> np.ndarray:
|
|
51
|
+
"""Binary 1-D erosion by an all-ones element of length ``n``, origin
|
|
52
|
+
``long(x0)`` (truncation), outside read as 0. Returns BYTE."""
|
|
53
|
+
ox = int(x0) # IDL LONG() of a FLOAT truncates toward zero; x0 >= 0 here
|
|
54
|
+
fg = np.asarray(orig) != 0
|
|
55
|
+
nx = fg.size
|
|
56
|
+
out = np.zeros(nx, dtype=np.uint8)
|
|
57
|
+
for i in range(nx):
|
|
58
|
+
ok = True
|
|
59
|
+
for j in range(n):
|
|
60
|
+
k = i + j - ox
|
|
61
|
+
if k < 0 or k >= nx or not fg[k]:
|
|
62
|
+
ok = False
|
|
63
|
+
break
|
|
64
|
+
out[i] = 1 if ok else 0
|
|
65
|
+
return out
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def soda2_backfill(image: np.ndarray) -> np.ndarray:
|
|
69
|
+
"""``soda2_backfill.pro:1-36``. ``image`` is ``(slice, diode)``; a 1-D
|
|
70
|
+
array is a one-slice image. Returns ``image`` itself on the early
|
|
71
|
+
returns (``:15``, ``:16``, ``:23``), else a modified copy."""
|
|
72
|
+
image = np.asarray(image)
|
|
73
|
+
if image.ndim == 1: # :15
|
|
74
|
+
return image
|
|
75
|
+
if image.max() == 0: # :16
|
|
76
|
+
return image
|
|
77
|
+
|
|
78
|
+
temp = image.copy() # :18
|
|
79
|
+
count = 1 # :20
|
|
80
|
+
orig = temp[1, :].copy() # :21
|
|
81
|
+
origtot = np.float32(temp[1, :].sum(dtype=np.float64)) # :22 total -> FLOAT
|
|
82
|
+
if origtot < 2: # :23
|
|
83
|
+
return image
|
|
84
|
+
nes = 1 # :24 es=[1]
|
|
85
|
+
nx = image.shape[-1] # s[1]
|
|
86
|
+
|
|
87
|
+
while True: # :27 REPEAT
|
|
88
|
+
nes += 1 # :28
|
|
89
|
+
if orig[0] == 1: # :29
|
|
90
|
+
x0: float = 0
|
|
91
|
+
else:
|
|
92
|
+
x0 = float(np.float32(count) / np.float32(2.0))
|
|
93
|
+
if orig[nx - 1] == 1: # :30
|
|
94
|
+
x0 = count
|
|
95
|
+
temp[0, :] = _erode_1d(orig, nes, x0) # :31
|
|
96
|
+
count += 1 # :32
|
|
97
|
+
tot0 = np.float32(temp[0, :].sum(dtype=np.float64))
|
|
98
|
+
if tot0 < origtot / np.float32(2.0): # :33
|
|
99
|
+
break
|
|
100
|
+
return temp # :35
|