A robot vision review may compare several candidate image regions and report the one with the lowest residual. Some candidates may never produce a usable score because an image region is missing, a calculation yields a nonfinite value, or an upstream check excludes the region. A row can lose all its candidates. The report needs a clear outcome for that row, because a numeric index can accidentally point to an unrelated region even when none was evaluated successfully.
NumPy's nanargmin ignores NaN values, but its documentation raises a separate concern for slices that contain only NaNs and infinities. In a review that defines an eligible score as finite, test eligibility explicitly. Then calculate a minimum only for rows that have at least one candidate to compare.
Run the offline fixture
Each row represents a frame, and the columns refer to the same four candidate positions. A lower finite score ranks higher in this example. Positive infinity represents a failed calculation, and negative infinity is likewise outside the example's valid score domain. The row in the middle contains no finite entries.
import numpy as np
scores = np.array([
[np.nan, 0.7, 0.3, np.inf],
[np.nan, np.inf, np.nan, np.inf],
[-np.inf, 0.4, 0.9, np.nan],
])
finite = np.isfinite(scores)
eligible_count = finite.sum(axis=1)
usable = eligible_count > 0
safe_scores = np.where(finite, scores, np.inf)
best_column = np.full(scores.shape[0], -1, dtype=np.intp)
best_column[usable] = np.argmin(safe_scores[usable], axis=1)
best_score = np.full(scores.shape[0], np.nan)
row = np.flatnonzero(usable)
best_score[row] = scores[row, best_column[row]]
assert eligible_count.tolist() == [2, 0, 2]
assert best_column.tolist() == [2, -1, 1]
assert np.allclose(best_score, [0.3, np.nan, 0.4], equal_nan=True)
The first frame returns column two, with a score of 0.3. The middle frame carries a missing result. The third returns column one, with a score of 0.4. Negative infinity is excluded by the finite mask even though it would appear smaller than any ordinary score if included in a raw minimum. The positive infinity used in the temporary array can never beat a finite candidate. The code also skips the reduction on the all-invalid row by filtering the rows before calling argmin.
Preserve column identity
It can be tempting to compress each row into a shorter list of finite values and report the index of its minimum. That index then refers to the shorter list. If the first region was invalid and removed, every later list position has shifted. The fixture keeps the candidate columns in place. If a pipeline does choose to compact each row, keep a map back to the original positions or index the candidate IDs alongside the values.
The example uses -1 as an internal marker. In Python and NumPy, that value is also a valid index for the last element, so the code filters out missing rows before looking up their scores. A report or export can use a null match ID and a status like no valid score. Record the eligible count separately. A low residual with one eligible position reflects a different comparison than the same residual chosen from four positions.
Check whether scores are eligible in the first place
A finite number alone may be insufficient in a real inspection. The example deliberately uses a narrow eligibility definition. If the image crop is incomplete, the sensor metadata is missing or a coverage check fails, add those rules to the validity mask while keeping their reasons available for review. Do that before computing ranked results. A frame with no evaluated candidate needs different follow-up from a frame where all candidates were evaluated and none satisfied a separate inspection threshold.
Write down how the report handles equal finite minima. NumPy returns the first occurrence of a minimum for argmin. If a particular candidate has priority in ties, keep the order stable or implement that priority with the candidate identity. Test a missing row, a mixed finite and nonfinite row, and a row with equal scores. Verify the selected ID and the eligible count together. These checks catch both invalid matches and changes introduced when the candidate order is rearranged.
Sources
NumPy API reference, nanargmin
https://numpy.org/doc/stable/reference/generated/numpy.nanargmin.html
NumPy API reference, isfinite
https://numpy.org/doc/stable/reference/generated/numpy.isfinite.html
NumPy API reference, argmin
https://numpy.org/doc/stable/reference/generated/numpy.argmin.html