Skip to content

Commit

Permalink
Fix landing pad tracking return (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xierumeng authored Oct 12, 2024
1 parent a16bcdb commit f01f598
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 31 deletions.
28 changes: 16 additions & 12 deletions modules/decision/landing_pad_tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,14 @@ def mark_confirmed_positive(self, detection: object_in_world.ObjectInWorld) -> N

def run(
self, detections: "list[object_in_world.ObjectInWorld]"
) -> "tuple[bool, object_in_world.ObjectInWorld | None]":
) -> "tuple[bool, list[object_in_world.ObjectInWorld] | None]":
"""
Updates the list of unconfirmed positives and returns the a first confirmed positive if
one exists, else the unconfirmed positive with the lowest variance.
Updates the list of unconfirmed positives and returns the confirmed positives if
they exist, otherwise returns the unconfirmed positives.
detections: New detections.
Return: List of confirmed/unconfirmed positives.
"""
for detection in detections:
match_found = False
Expand All @@ -82,16 +86,16 @@ def run(
# If new landing pad, add to list of unconfirmed positives
self.__unconfirmed_positives.append(detection)

# If there are confirmed positives, return the first one
# If there are confirmed positives, return them
if len(self.__confirmed_positives) > 0:
return True, self.__confirmed_positives[0]
return True, self.__confirmed_positives

# If the list is empty, all landing pads have been visited, none are viable
if len(self.__unconfirmed_positives) == 0:
return False, None
# If there are unconfirmed positives, return them
if len(self.__unconfirmed_positives) > 0:
# Sort list by variance in ascending order
self.__unconfirmed_positives.sort(key=lambda x: x.spherical_variance)

# Sort list by variance in ascending order
self.__unconfirmed_positives.sort(key=lambda x: x.spherical_variance)
return True, self.__unconfirmed_positives

# Return detection with lowest variance
return True, self.__unconfirmed_positives[0]
# All landing pads have been visited, none are viable
return False, None
55 changes: 36 additions & 19 deletions tests/unit/test_landing_pad_tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,10 @@ def test_run_with_empty_detections_list(
"""
Test run method with empty detections list.
"""
# Run
result, actual = tracker.run([])

# Check
assert not result
assert actual is None

Expand All @@ -396,20 +399,21 @@ def test_run_one_input(
"""
Test run with only 1 input.
"""
expected_output = detections_1[2]
expected_unconfirmed_positives = [
# Setup
expected = [
detections_1[2],
detections_1[1],
detections_1[4],
detections_1[0],
detections_1[3],
]

# Run
result, actual = tracker.run(detections_1)

# Check
assert result
assert actual == expected_output
assert tracker._LandingPadTracking__unconfirmed_positives == expected_unconfirmed_positives # type: ignore
assert actual == expected

def test_run_one_input_similar_detections(
self,
Expand All @@ -419,19 +423,20 @@ def test_run_one_input_similar_detections(
"""
Test run with only 1 input where 2 landing pads are similar.
"""
expected_output = detections_3[2]
expected_unconfirmed_positives = [
# Setup
expected = [
detections_3[2],
detections_3[1],
detections_3[4],
detections_3[3],
]

# Run
result, actual = tracker.run(detections_3)

# Check
assert result
assert actual == expected_output
assert tracker._LandingPadTracking__unconfirmed_positives == expected_unconfirmed_positives # type: ignore
assert actual == expected

def test_run_multiple_inputs(
self,
Expand All @@ -442,8 +447,8 @@ def test_run_multiple_inputs(
"""
Test run with 2 inputs where some landing pads are similar.
"""
expected_output = detections_2[0]
expected_unconfirmed_positives = [
# Setup
expected = [
detections_2[0],
detections_1[2],
detections_2[1],
Expand All @@ -454,12 +459,15 @@ def test_run_multiple_inputs(
detections_1[3],
]

tracker.run(detections_1)
# Run
result, _ = tracker.run(detections_1)
assert result

result, actual = tracker.run(detections_2)

# Check
assert result
assert actual == expected_output
assert tracker._LandingPadTracking__unconfirmed_positives == expected_unconfirmed_positives # type: ignore
assert actual == expected

def test_run_with_confirmed_positive(
self,
Expand All @@ -469,14 +477,18 @@ def test_run_with_confirmed_positive(
"""
Test run when there is a confirmed positive.
"""
_, confirmed_positive = object_in_world.ObjectInWorld.create(1, 1, 1)
# Setup
result, confirmed_positive = object_in_world.ObjectInWorld.create(1, 1, 1)
assert result
assert confirmed_positive is not None

tracker._LandingPadTracking__confirmed_positives.append(confirmed_positive) # type: ignore
expected = confirmed_positive
expected = [confirmed_positive]

# Run
result, actual = tracker.run(detections_1)

# Check
assert result
assert actual == expected

Expand All @@ -488,12 +500,17 @@ def test_run_with_false_positive(
"""
Test to see if run function doesn't add landing pads that are similar to false positives.
"""
_, false_positive = object_in_world.ObjectInWorld.create(1, 1, 1)
# Setup
result, false_positive = object_in_world.ObjectInWorld.create(1, 1, 1)
assert result
assert false_positive is not None

tracker._LandingPadTracking__false_positives.append(false_positive) # type: ignore
expected_unconfirmed_positives = [detections_2[3], detections_2[2], detections_2[4]]
expected = [detections_2[3], detections_2[2], detections_2[4]]

tracker.run(detections_2)
# Run
result, actual = tracker.run(detections_2)

assert tracker._LandingPadTracking__unconfirmed_positives == expected_unconfirmed_positives # type: ignore
# Check
assert result
assert actual == expected

0 comments on commit f01f598

Please sign in to comment.