Skip to content

Commit

Permalink
Tp/add integration test (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanpepinartefact authored Nov 24, 2023
1 parent 612759e commit cf95dde
Show file tree
Hide file tree
Showing 23 changed files with 3,314 additions and 32 deletions.
File renamed without changes.
1,595 changes: 1,595 additions & 0 deletions tests/assets/integration_tests/data/tracker_output.txt

Large diffs are not rendered by default.

File renamed without changes.
1,570 changes: 1,570 additions & 0 deletions tests/assets/integration_tests/expected_outputs/corrected_tracks.txt

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"object_id": 1.0, "state": 0, "re_id_chain": [1.0, 33.0], "metadata": {"first_frame_id": 1, "last_frame_id": 363, "class_counts": {"0": 313}, "bbox": [729, 88, 888, 251], "confidence": 0.818931, "confidence_sum": 245.7679709999999, "observations": 313}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"object_id": 2.0, "state": 0, "re_id_chain": [2.0, 6.0, 10.0, 16.0], "metadata": {"first_frame_id": 1, "last_frame_id": 363, "class_counts": {"0": 320}, "bbox": [596, 100, 714, 218], "confidence": 0.631255, "confidence_sum": 224.94876499999998, "observations": 320}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"object_id": 3.0, "state": 0, "re_id_chain": [3.0, 38.0, 41.0, 45.0], "metadata": {"first_frame_id": 1, "last_frame_id": 363, "class_counts": {"0": 314}, "bbox": [529, 471, 715, 605], "confidence": 0.756379, "confidence_sum": 229.919842, "observations": 314}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"object_id": 4.0, "state": 0, "re_id_chain": [4.0, 43.0], "metadata": {"first_frame_id": 1, "last_frame_id": 363, "class_counts": {"0": 349}, "bbox": [464, 187, 886, 562], "confidence": 0.575016, "confidence_sum": 260.3729819999998, "observations": 349}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"object_id": 5.0, "state": 1, "re_id_chain": [5.0, 17.0, 21.0, 23.0, 27.0], "metadata": {"first_frame_id": 1, "last_frame_id": 323, "class_counts": {"0": 422}, "bbox": [584, 260, 710, 477], "confidence": 0.728422, "confidence_sum": 322.0736030000005, "observations": 422}}
File renamed without changes.
Empty file.
Empty file.
76 changes: 76 additions & 0 deletions tests/bin/integration_tests/test_full_correction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import json
from pathlib import Path

import numpy as np

from tests.utils.file_utils import compare_files, reset_output_folder
from trackreid.reid_processor import ReidProcessor
from trackreid.tracked_object import TrackedObject

INPUT_FOLDER = Path("tests/assets/integration_tests/data/")
INPUT_FILE = "tracker_output.txt"

OUTPUT_FOLDER = Path("tests/results/integration_tests/")
OUTPUT_FILE = "corrected_tracks.txt"

EXPECTED_OUTPUT_FOLDER = Path("tests/assets/integration_tests/expected_outputs")
EXPECTED_OUTPUT_FILES = [
OUTPUT_FILE,
"tracked_object_1.json",
"tracked_object_2.json",
"tracked_object_3.json",
"tracked_object_4.json",
"tracked_object_5.json",
]


def save_tracked_objects(reid_processor: ReidProcessor, file_path_folder: Path):
for tracked_object in reid_processor.all_tracked_objects:
file_path = file_path_folder / f"tracked_object_{int(tracked_object.object_id)}.json"
with file_path.open("w") as file:
json.dump(tracked_object.to_dict(), file)


def test_full_correction():
# 1. Define dummy cost and selection functions
def dummy_cost_function(candidate: TrackedObject, switcher: TrackedObject): # noqa: ARG001
return 0

def dummy_selection_function(candidate: TrackedObject, switcher: TrackedObject): # noqa: ARG001
return 1

# 2. Initialize ReidProcessor
reid_processor = ReidProcessor(
filter_confidence_threshold=0.1,
filter_time_threshold=1,
max_frames_to_rematch=100,
max_attempt_to_match=5,
cost_function=dummy_cost_function,
selection_function=dummy_selection_function,
save_to_txt=True,
file_path=OUTPUT_FOLDER / OUTPUT_FILE,
)

# 3. Reset output folder, load data and group by frame_id

reset_output_folder(output_folder=OUTPUT_FOLDER, create_folder=True)

tracker_output = np.loadtxt(INPUT_FOLDER / INPUT_FILE)
frame_ids, indexes = np.unique(tracker_output[:, 0], return_index=True)
frame_tracker_outputs = np.split(tracker_output[:, 1:], indexes)[1:]

# 4. perform reid process and save corrected objects

for frame_id, frame_tracker_output in zip(frame_ids, frame_tracker_outputs):
reid_processor.update(frame_id=frame_id, tracker_output=frame_tracker_output)

save_tracked_objects(reid_processor=reid_processor, file_path_folder=OUTPUT_FOLDER)

# 5. Perform evaluation and delete files if test succeed

for expected_output_file in EXPECTED_OUTPUT_FILES:
compare_files(
EXPECTED_OUTPUT_FOLDER / expected_output_file, OUTPUT_FOLDER / expected_output_file
)

reset_output_folder(output_folder=OUTPUT_FOLDER, create_folder=False)
Empty file added tests/bin/unit_tests/.gitkeep
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from trackreid.matcher import Matcher
from trackreid.tracked_object import TrackedObject

INPUT_FOLDER = Path("tests/data/unit_tests/tracked_objects")
LIST_TRACKED_OBJECTS = ["object_1.json", "object_4.json", "object_24.json"]
INPUT_FOLDER = Path("tests/assets/unit_tests/data/tracked_objects")
LIST_TRACKED_OBJECTS = ["tracked_object_1.json", "tracked_object_4.json", "tracked_object_24.json"]

ALL_TRACKED_OBJECTS = []
for tracked_object in LIST_TRACKED_OBJECTS:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

from trackreid.tracked_object_metadata import TrackedObjectMetaData

INPUT_FOLDER = Path("tests/data/unit_tests/tracked_objects")
LIST_TRACKED_OBJECTS = ["object_1.json", "object_4.json", "object_24.json"]
INPUT_FOLDER = Path("tests/assets/unit_tests/data/tracked_objects")
LIST_TRACKED_OBJECTS = ["tracked_object_1.json", "tracked_object_4.json", "tracked_object_24.json"]

ALL_TRACKED_METADATA = []
for tracked_object in LIST_TRACKED_OBJECTS:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

from trackreid.tracked_object import TrackedObject

INPUT_FOLDER = Path("tests/data/unit_tests/tracked_objects")
LIST_TRACKED_OBJECTS = ["object_1.json", "object_4.json", "object_24.json"]
INPUT_FOLDER = Path("tests/assets/unit_tests/data/tracked_objects")
LIST_TRACKED_OBJECTS = ["tracked_object_1.json", "tracked_object_4.json", "tracked_object_24.json"]

ALL_TRACKED_OBJECTS = []
for tracked_object in LIST_TRACKED_OBJECTS:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from trackreid.tracked_object import TrackedObject

# Load tracked object data
INPUT_FOLDER = Path("tests/data/unit_tests/tracked_objects")
LIST_TRACKED_OBJECTS = ["object_1.json", "object_4.json", "object_24.json"]
INPUT_FOLDER = Path("tests/assets/unit_tests/data/tracked_objects")
LIST_TRACKED_OBJECTS = ["tracked_object_1.json", "tracked_object_4.json", "tracked_object_24.json"]

ALL_TRACKED_OBJECTS = []
for tracked_object in LIST_TRACKED_OBJECTS:
Expand Down
60 changes: 60 additions & 0 deletions tests/utils/file_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import filecmp
import json
import shutil
from pathlib import Path


def reset_output_folder(output_folder: Path, create_folder=True):
"""
Each test should start and end by deleting the output folder
"""
if output_folder.exists():
shutil.rmtree(output_folder, ignore_errors=True)

if create_folder:
output_folder.mkdir(parents=True, exist_ok=True)


def _compare_txt_files(file1: Path, file2: Path):
"""Compare two txt files line by line"""
with file1.open("r") as f1:
with file2.open("r") as f2:
for line1, line2 in zip(f1, f2):
assert line1.strip() == line2.strip(), (line1, line2)


def _compare_json_files(file1: Path, file2: Path):
with file1.open("r") as f1:
json1 = json.load(f1)

with file2.open("r") as f2:
json2 = json.load(f2)

# converting to string with same key order
json1, json2 = json.dumps(json1, sort_keys=True), json.dumps(json2, sort_keys=True)
assert json1 == json2, (json1, json2)


def compare_files(file1: Path, file2: Path):
"""
Compare two files, regardless of their extension
"""
assert file1.exists(), f"file1 {file1} does not exist"
assert file2.exists(), f"File {file2} does not exist"

extension1 = file1.suffix
extension2 = file2.suffix

if extension1 != extension2:
raise ValueError(
f"Cannot compare {file1} and {file2} because they have different extensions"
)

if extension1 == ".json":
return _compare_json_files(file1, file2)

elif extension1 == ".txt": # we don't use filecmp to compare line by line
return _compare_txt_files(file1, file2)

else:
assert filecmp.cmp(file1, file2)
24 changes: 0 additions & 24 deletions trackreid/tracked_object.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import json
from typing import Optional, Union

import numpy as np
Expand Down Expand Up @@ -248,15 +247,6 @@ def to_dict(self):
}
return data

def to_json(self):
"""
Converts the TrackedObject instance to a JSON string.
Returns:
str: A JSON string representation of the TrackedObject instance.
"""
return json.dumps(self.to_dict(), indent=4)

@classmethod
def from_dict(cls, data: dict):
"""
Expand All @@ -273,17 +263,3 @@ def from_dict(cls, data: dict):
obj.re_id_chain = sllist(data["re_id_chain"])
obj.metadata = TrackedObjectMetaData.from_dict(data["metadata"])
return obj

@classmethod
def from_json(cls, json_str: str):
"""
Creates a new TrackedObject instance from a JSON string.
Args:
json_str (str): A JSON string containing the data for the TrackedObject instance.
Returns:
TrackedObject: A new TrackedObject instance created from the JSON string.
"""
data = json.loads(json_str)
return cls.from_dict(data)

0 comments on commit cf95dde

Please sign in to comment.