Refactor import paths and improve text file reading

Refactored import paths for test utility methods across multiple test files to align with a recent package restructure. Enriched 'read_txt_file' functionality in 'file.py' allowing to optionally skip empty lines, enhancing flexibility for various use-cases. Added tests for the new 'read_txt_file' behavior in a new 'test_file.py'. Renamed 'utils.py' in test package to 'test_utils.py' for better differentiation from the util package in the source code.
This commit is contained in:
SkalskiP 2023-11-10 10:15:08 -08:00
parent 1dce43747f
commit e416ef4c0e
11 changed files with 110 additions and 12 deletions

View File

@ -79,8 +79,6 @@ def yolo_annotations_to_detections(
w, h = resolution_wh
for line in lines:
values = line.split()
if len(values) == 0:
continue
class_id.append(int(values[0]))
if len(values) == 5:
box = _parse_box(values=values[1:])
@ -151,7 +149,7 @@ def load_yolo_annotations(
annotations[image_path] = Detections.empty()
continue
lines = read_txt_file(str(annotation_path))
lines = read_txt_file(file_path=annotation_path, skip_empty=True)
h, w, _ = image.shape
resolution_wh = (w, h)

View File

@ -57,19 +57,24 @@ def list_files_with_extensions(
return files_with_extensions
def read_txt_file(file_path: str) -> List[str]:
def read_txt_file(file_path: str, skip_empty: bool = False) -> List[str]:
"""
Read a text file and return a list of strings without newline characters.
Optionally skip empty lines.
Args:
file_path (str): The path to the text file.
skip_empty (bool): If True, skip lines that are empty or contain only
whitespace. Default is False.
Returns:
List[str]: A list of strings representing the lines in the text file.
"""
with open(file_path, "r") as file:
lines = file.readlines()
lines = [line.rstrip("\n") for line in lines]
if skip_empty:
lines = [line.rstrip("\n") for line in file if line.strip()]
else:
lines = [line.rstrip("\n") for line in file]
return lines

View File

@ -1,5 +1,5 @@
from contextlib import ExitStack as DoesNotRaise
from test.utils import mock_detections
from test.test_utils import mock_detections
from typing import Optional
import numpy as np

View File

@ -1,6 +1,6 @@
import xml.etree.ElementTree as ET
from contextlib import ExitStack as DoesNotRaise
from test.utils import mock_detections
from test.test_utils import mock_detections
from typing import List, Optional
import numpy as np

View File

@ -1,5 +1,5 @@
from contextlib import ExitStack as DoesNotRaise
from test.utils import mock_detections
from test.test_utils import mock_detections
from typing import List, Optional
import numpy as np

View File

@ -1,5 +1,5 @@
from contextlib import ExitStack as DoesNotRaise
from test.utils import mock_detections
from test.test_utils import mock_detections
from typing import Dict, List, Optional, Tuple, TypeVar
import pytest

View File

@ -1,5 +1,5 @@
from contextlib import ExitStack as DoesNotRaise
from test.utils import mock_detections
from test.test_utils import mock_detections
from typing import List, Optional, Union
import numpy as np

View File

@ -1,5 +1,5 @@
from contextlib import ExitStack as DoesNotRaise
from test.utils import assert_almost_equal, mock_detections
from test.test_utils import assert_almost_equal, mock_detections
from typing import Optional, Union
import numpy as np

0
test/utils/__init__.py Normal file
View File

95
test/utils/test_file.py Normal file
View File

@ -0,0 +1,95 @@
from contextlib import ExitStack as DoesNotRaise
from typing import List, Optional
import pytest
import os
from supervision.utils.file import read_txt_file
FILE_1_CONTENT = """Line 1
Line 2
Line 3
"""
FILE_2_CONTENT = """
Line 2
Line 4
"""
FILE_3_CONTENT = """
Line 2
Line 4
"""
@pytest.fixture(scope="module", autouse=True)
def setup_and_teardown_files():
with open("file_1.txt", "w") as file:
file.write(FILE_1_CONTENT)
with open("file_2.txt", "w") as file:
file.write(FILE_2_CONTENT)
with open("file_3.txt", "w") as file:
file.write(FILE_3_CONTENT)
yield
os.remove("file_1.txt")
os.remove("file_2.txt")
os.remove("file_3.txt")
@pytest.mark.parametrize(
"file_name, skip_empty, expected_result, exception",
[
(
"file_1.txt",
False,
["Line 1", "Line 2", "Line 3"],
DoesNotRaise()
),
(
"file_2.txt",
True,
["Line 2", "Line 4"],
DoesNotRaise()
),
(
"file_2.txt",
False,
[" ", "Line 2", "", "Line 4", ""],
DoesNotRaise()
),
(
"file_3.txt",
True,
["Line 2", "Line 4"],
DoesNotRaise()
),
(
"file_3.txt",
False,
["", "Line 2", "", "Line 4", ""],
DoesNotRaise()
),
(
"file_4.txt",
True,
None,
pytest.raises(FileNotFoundError)
)
]
)
def test_read_txt_file(
file_name: str,
skip_empty: bool,
expected_result: Optional[List[str]],
exception: Exception
):
with exception:
result = read_txt_file(file_name, skip_empty)
assert result == expected_result