An inspection station can export one integer for each rejected tray pocket. When the original report uses MATLAB linear indices and the receiving dashboard uses NumPy, two conventions travel with that integer. MATLAB starts its linear IDs at one and advances down rows before moving to the next column. NumPy starts at zero and uses row-major order by default in unravel_index. A wrong conversion can highlight a different pocket while still returning an in-bounds coordinate.
Make the fixture rectangular
Take a tray with three rows and five columns. The pocket at MATLAB row 2, column 4 has linear ID 11. There are three complete columns before it, accounting for nine entries, and it is the second entry of the fourth column. The equivalent zero-based coordinate is row 1, column 3. Subtract one from the linear ID and decode it with column-major order to recover that coordinate.
import numpy as np
shape = (3, 5)
matlab_id = 11
if not 1 <= matlab_id <= np.prod(shape):
raise ValueError("Inspection cell ID is outside the tray")
flat_zero = matlab_id - 1
cell = np.unravel_index(flat_zero, shape, order="F")
np.testing.assert_equal(cell, (1, 3))
np.testing.assert_equal(
np.unravel_index(flat_zero, shape, order="C"), (2, 0)
)
Running the fixture produced (1, 3) with order="F". Using the default row-major convention on the same zero-based ID gave (2, 0), a valid but incorrect cell for this file. Confirm the expected coordinate from a marked physical pocket or the producer's export. A round trip through a matching encoder and decoder can pass when both sides use the wrong convention.
Carry the tray layout with the file
Write the row count, column count, axis labels, linear index base, and traversal order into the export contract. Validate the producer's grid dimensions before drawing the overlay. A transposed grid has the same number of cells but changes the stride used to map linear IDs into coordinates. A rectangular tray and an interior pocket away from the diagonal make that mistake easier to detect.
If the camera image is rotated relative to the tray, apply the calibrated tray-to-image transform after decoding the cell. The linear ID conversion recovers a row and column in the producer's grid. The calibrated transform gives the position where that pocket should appear in the image.
Sources
https://www.mathworks.com/help/matlab/ref/sub2ind.html
https://numpy.org/doc/stable/reference/generated/numpy.unravel_index.html