Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 33 additions & 10 deletions src/machinevisiontoolbox/ImagePointFeatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,14 +314,19 @@ def __add__(self, other: "BaseFeature2D | builtins.list | None") -> "BaseFeature

:param other: set of features
:type other: :class:`BaseFeature2D`
:raises TypeError: _description_
:raises TypeError: ``other`` is not a feature set, ``[]`` or ``None``, or
the two sets are of different feature types
:return: set of features
:rtype: :class:`BaseFeature2D` instance

Add two feature sets to form a new feature sets. If ``other`` is
equal to ``None`` or ``[]`` it is interpreted as an empty feature
set, this is useful in a loop for aggregating feature sets.

.. versionchanged:: 2.5.0
Adding something that is not a feature set, ``[]`` or ``None``
raises ``TypeError``. It used to raise ``AssertionError``.

Example:

.. runblock:: pycon
Expand All @@ -342,9 +347,11 @@ def __add__(self, other: "BaseFeature2D | builtins.list | None") -> "BaseFeature
isinstance(other, list) and len(other) == 0
): # Add feature sets
return self
assert isinstance(
other, BaseFeature2D
), f"cannot add {type(other)} to BaseFeature2D"
if not isinstance(other, BaseFeature2D):
raise TypeError(
f"cannot add {type(other).__name__} to a feature set, "
"only another feature set, [] or None"
)
if self._feature_type != other._feature_type:
raise TypeError(
"can't add different feature types:",
Expand All @@ -364,13 +371,20 @@ def __radd__(self, other) -> "BaseFeature2D":

:param other: set of features
:type other: :class:`BaseFeature2D`
:raises TypeError: _description_
:raises TypeError: ``other`` is not a feature set, ``[]``, ``None`` or ``0``
:return: set of features
:rtype: :class:`BaseFeature2D`

Add two feature sets to form a new feature sets. If ``other`` is
equal to ``None`` or ``[]`` it is interpreted as an empty feature
set, this is useful in a loop for aggregating feature sets.
equal to ``None``, ``[]`` or ``0`` it is interpreted as an empty feature
set, this is useful in a loop for aggregating feature sets. The ``0``
is the starting value of the builtin :func:`sum`, so a list of
feature sets can be combined by ``sum(features)``.

.. versionchanged:: 2.5.0
``None`` and ``0`` are accepted, and anything else raises
``TypeError`` with a message. It used to raise ``ValueError("bad")``,
and ``sum()`` of feature sets did not work.

Example:

Expand All @@ -385,13 +399,22 @@ def __radd__(self, other) -> "BaseFeature2D":
>>> orb += img1.ORB()
>>> orb += img2.ORB()
>>> orb
>>> orb = sum([img1.ORB(), img2.ORB()])
>>> orb

:seealso: :meth:`__add__`
"""
if isinstance(other, list) and len(other) == 0: # Add feature sets
# an empty set, or the 0 that sum() starts with
if (
other is None
or (isinstance(other, list) and len(other) == 0)
or (isinstance(other, int) and not isinstance(other, bool) and other == 0)
):
return self
else:
raise ValueError("bad")
raise TypeError(
f"cannot add a feature set to {type(other).__name__}, "
"only to [], None or 0 (as the start of sum)"
)

@property
@scalar_result
Expand Down
37 changes: 35 additions & 2 deletions tests/test_image_point_features_extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,43 @@
a + b

def test_adding_other_things_is_an_error(self):
with pytest.raises(AssertionError):
with pytest.raises(TypeError, match="cannot add int to a feature set"):
make_features(3) + 5
with pytest.raises(ValueError):
with pytest.raises(TypeError, match="cannot add a feature set to int"):
5 + make_features(3)
with pytest.raises(TypeError, match="cannot add a feature set to str"):
"a" + make_features(3)

Check warning on line 208 in tests/test_image_point_features_extra.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/test_image_point_features_extra.py#L208

Expression "'a' + make_features(3)" is assigned to nothing
with pytest.raises(TypeError):
[1] + make_features(3)

Check warning on line 210 in tests/test_image_point_features_extra.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/test_image_point_features_extra.py#L210

+ make_features(3)" is assigned to nothing
with pytest.raises(TypeError):
True + make_features(3)

Check warning on line 212 in tests/test_image_point_features_extra.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/test_image_point_features_extra.py#L212

Expression "True + make_features(3)" is assigned to nothing

def test_none_and_zero_on_the_left(self):
a = make_features(3)
# None cannot be on the left of + in Python, so call the method
assert a.__radd__(None) is a
assert (0 + a) is a

def test_sum_of_feature_sets(self):
a, b, c = make_features(3), make_features(2), make_features(4)
total = sum([a, b, c])
assert len(total) == 9
assert total.descriptor.shape == (9, 4)
# the inputs are not changed
assert len(a) == 3 and len(b) == 2 and len(c) == 4
nt.assert_array_equal(total.u[:3], a.u)
nt.assert_array_equal(total.u[3:5], b.u)

def test_sum_of_one_and_of_a_generator(self):
a = make_features(3)
assert sum([a]) is a
assert len(sum(make_features(n) for n in (1, 2, 3))) == 6

def test_sum_of_different_types_is_an_error(self):
a, b = make_features(3), make_features(3)
b._feature_type = "ORB"
with pytest.raises(TypeError, match="different feature types"):
sum([a, b])


class TestGridify:
Expand Down
Loading