diff --git a/src/machinevisiontoolbox/ImagePointFeatures.py b/src/machinevisiontoolbox/ImagePointFeatures.py index 913ae802..cecbaf04 100644 --- a/src/machinevisiontoolbox/ImagePointFeatures.py +++ b/src/machinevisiontoolbox/ImagePointFeatures.py @@ -1395,7 +1395,10 @@ def __init__( self._matches = m self._kp1 = fv1 self._kp2 = fv2 - self._inliers = inliers + # always an ndarray of bool, or None if the status has not been set + self._inliers: np.ndarray | None = ( + None if inliers is None else np.asarray(inliers, dtype=bool) + ) self._inverse_dict1: dict[int, int] | None = None self._inverse_dict2: dict[int, int] | None = None @@ -1426,33 +1429,34 @@ def __getitem__( :seealso: :meth:`.__len__` """ - inliers = None + if not isinstance(i, (int, slice, np.ndarray)): + if isinstance(i, (str, bytes)) or not isinstance(i, Sequence): + raise ValueError( + f"index must be an int, slice, sequence or array, got {type(i).__name__}" + ) + # a list or tuple of ints or bools + i = np.asarray(i) + if i.size == 0: + i = i.astype(int) + if isinstance(i, int): matches = [self._matches[i]] - if self._inliers is not None: - inliers = self._inliers[i] + select: Any = [i] elif isinstance(i, slice): matches = self._matches[i] - if self._inliers is not None: - inliers = self._inliers[i] - elif isinstance(i, np.ndarray): - if np.issubdtype(i.dtype, bool): - matches = [m for m, g in zip(self._matches, i) if g] - if self._inliers is not None: - inliers = [m for m, g in zip(self._inliers, i) if g] - elif np.issubdtype(i.dtype, np.integer): - matches = [self._matches[k] for k in i] - if self._inliers is not None: - inliers = [self._inliers[k] for k in i] - else: - raise ValueError(f"index array must be integer or bool, got {i.dtype}") + select = i + elif np.issubdtype(i.dtype, bool): + matches = [m for m, g in zip(self._matches, i) if g] + select = i + elif np.issubdtype(i.dtype, np.integer): + matches = [self._matches[k] for k in i] + select = i else: - raise ValueError( - f"index must be an int, slice, sequence or array, got {type(i).__name__}" - ) - # an int index leaves a bare bool in inliers, and a bool or int array index - # leaves a list, which later fail in outliers: to be fixed separately - return FeatureMatch(matches, self._kp1, self._kp2, inliers) # type: ignore[arg-type] + raise ValueError(f"index array must be integer or bool, got {i.dtype}") + + # indexing the array keeps the inlier status an ndarray, whatever the index + inliers = None if self._inliers is None else self._inliers[select] + return FeatureMatch(matches, self._kp1, self._kp2, inliers) def __len__(self) -> int: """ @@ -1635,17 +1639,23 @@ def __repr__(self) -> str: @property @scalar_result - def status(self) -> str: + def status(self) -> "builtins.list[str]": """ Inlier status of matches - :return: inlier status of matches - :rtype: bool + :return: ``"+"`` for an inlier and ``"-"`` for an outlier, or ``""`` if the + status has not been set + :rtype: str or ndarray(N) of str + + A single string for one match, otherwise an array with one string per match. + + .. versionchanged:: 2.5.0 + Works for more than one match. It used to raise ``ValueError``. """ if self._inliers is not None: - return "+" if self._inliers else "-" + return ["+" if inlier else "-" for inlier in self._inliers] else: - return "" + return [""] * len(self) def list(self) -> None: """ @@ -1738,7 +1748,7 @@ def outliers(self) -> "FeatureMatch": """ if self._inliers is None: raise ValueError("inlier status has not been set") - return self[~self._inliers] # type: ignore[operator] # a list after fancy indexing + return self[~self._inliers] def subset(self, N: int = 100) -> "FeatureMatch": """ diff --git a/tests/test_image_point_features_extra.py b/tests/test_image_point_features_extra.py index 069aa5e6..1e4d390c 100644 --- a/tests/test_image_point_features_extra.py +++ b/tests/test_image_point_features_extra.py @@ -523,6 +523,60 @@ def test_inliers_follow_indexing(self, fmatch): assert len(fmatch[np.array([True, False, True, False])]) == 2 assert len(fmatch[np.array([0, 1])]) == 2 + @pytest.mark.parametrize( + "index", + [ + 1, + slice(1, 3), + np.array([True, False, True, False]), + np.array([0, 3]), + [0, 3], + (1, 2), + [True, False, True, False], + ], + ) + def test_inlier_status_stays_an_array_whatever_the_index(self, fmatch, index): + # indexing by an int used to leave a bare bool, and by an index array a + # list, so inliers and outliers then failed + fmatch._inliers = np.array([True, False, True, True]) + sub = fmatch[index] + assert isinstance(sub._inliers, np.ndarray) + assert sub._inliers.dtype == bool + assert len(sub._inliers) == len(sub) + assert len(sub.inliers) + len(sub.outliers) == len(sub) + + def test_inliers_and_outliers_of_a_selection(self, fmatch): + fmatch._inliers = np.array([True, False, True, True]) + sub = fmatch[np.array([1, 2, 3])] + nt.assert_array_equal(sub.inliers.id1, [2, 3]) + nt.assert_array_equal(sub.outliers.id1, [1]) + + def test_outliers_of_one_match(self, fmatch): + fmatch._inliers = np.array([True, False, True, True]) + assert len(fmatch[1].outliers) == 1 + assert len(fmatch[1].inliers) == 0 + assert len(fmatch[0].outliers) == 0 + + def test_status_of_many_matches(self, fmatch): + # status used to raise ValueError for more than one match + fmatch._inliers = np.array([True, False, True, True]) + nt.assert_array_equal(fmatch.status, ["+", "-", "+", "+"]) + + def test_status_of_many_matches_when_unknown(self, fmatch): + nt.assert_array_equal(fmatch.status, ["", "", "", ""]) + + def test_inliers_given_as_a_list(self, fmatch): + match = FeatureMatch( + [(0, 3, 0.5), (1, 2, 1.0)], fmatch._kp1, fmatch._kp2, [True, False] + ) + assert isinstance(match._inliers, np.ndarray) + assert len(match.inliers) == 1 + + def test_index_by_a_list_or_tuple(self, fmatch): + nt.assert_array_equal(fmatch[[3, 0]].id1, [3, 0]) + nt.assert_array_equal(fmatch[(1, 2)].id1, [1, 2]) + assert len(fmatch[[]]) == 0 + def test_status_is_blank_when_unknown(self, fmatch): assert fmatch[0].status == ""