Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
fix: pytests (#294)
Browse files Browse the repository at this point in the history
* fix: pytests

* Update tests.yml
  • Loading branch information
makseq authored Jun 4, 2024
1 parent 9a3db49 commit 4e76ff5
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 18 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ jobs:
run: |
pytest --junitxml report.xml --cov=. -m "not integration_tests"
- name: "Upload coverage to Codecov"
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: true
# - name: Upload coverage reports to Codecov
# uses: codecov/codecov-action@v4.0.1
# with:
# token: ${{ secrets.CODECOV_TOKEN }}
# slug: makseq/label-studio-converter
25 changes: 16 additions & 9 deletions tests/test_export_yolo.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from label_studio_converter import Converter
import os
import pytest
import tempfile
import shutil

from label_studio_converter.utils import convert_annotation_to_yolo, convert_annotation_to_yolo_obb
from label_studio_converter import Converter
from .utils import almost_equal_1d, almost_equal_2d


BASE_DIR = os.path.dirname(__file__)
TEST_DATA_PATH = os.path.join(BASE_DIR, "data", "test_export_yolo")
Expand Down Expand Up @@ -136,18 +139,18 @@ def test_convert_to_yolo_obb(create_temp_folder):
), f"Generated file: \n {generated_paths} \n does not match expected ones: \n {expected_paths}"

# Check all the annotations have been converted to yolo
axpected_annotations = [23, 1, 1]
expected_annotations = [23, 1, 1]
for fidx, file in enumerate(expected_paths):
with open(file) as f:
lines = f.readlines()
assert len(lines) == axpected_annotations[fidx], f"Expect different number of annotations in file {file}."
assert len(lines) == expected_annotations[fidx], f"Expect different number of annotations in file {file}."
for idx, line in enumerate(lines):
parameters = line.split(' ')
total_parameters = len(parameters)
assert total_parameters == 9, f'Expected 9 parameters but got {total_parameters} in line {idx}'

def test_convert_polygons_to_yolo(create_temp_folder):
"""Check converstion label_studio json exported file to yolo with polygons"""
"""Check conversion label_studio json exported file to yolo with polygons"""

# Generates a temporary folder and return the absolute path
# The temporary folder contains all the data generate by the following function
Expand Down Expand Up @@ -184,7 +187,6 @@ def test_convert_polygons_to_yolo(create_temp_folder):
len(lines) == 1
), f"Expect different number of annotations in file {file}."


def test_convert_annotation_to_yolo_format():
"""
Verify conversion from LS annotation to normalized Yolo format.
Expand Down Expand Up @@ -228,7 +230,9 @@ def test_convert_annotation_to_yolo_format():

for idx, annotation in enumerate(annotations):
result = convert_annotation_to_yolo(annotation)
assert result == expectations[idx], f'Converted LS annotation to normalized Yolo format does not match expected result at index {idx}'
assert almost_equal_1d(
result, expectations[idx]
), f'Converted LS annotation to normalized Yolo format does not match expected result at index {idx}'

def test_convert_invalid_annotation_to_yolo_format():
"""
Expand Down Expand Up @@ -271,7 +275,7 @@ def test_convert_invalid_annotation_to_yolo_format():

for idx, annotation in enumerate(annotations):
result = convert_annotation_to_yolo(annotation)
assert result == None, f'Expected annotation at index {idx} to be invalid'
assert result is None, f'Expected annotation at index {idx} to be invalid'


def test_convert_annotation_to_yolo_obb_format():
Expand Down Expand Up @@ -340,7 +344,10 @@ def test_convert_annotation_to_yolo_obb_format():

for idx, annotation in enumerate(annotations):
result = convert_annotation_to_yolo_obb(annotation)
assert result == expectations[idx], f'Converted LS annotation to normalized Yolo OBB-format does not match expected result at index {idx}'
print('result = ', result)
assert almost_equal_2d(
result, expectations[idx]
), f'Converted LS annotation to normalized Yolo OBB-format does not match expected result at index {idx}'

def test_convert_invalid_annotation_to_yolo_obb_format():
"""
Expand Down Expand Up @@ -421,4 +428,4 @@ def test_convert_invalid_annotation_to_yolo_obb_format():

for idx, annotation in enumerate(annotations):
result = convert_annotation_to_yolo_obb(annotation)
assert result == None, f'Expected annotation for OBB at index {idx} to be invalid'
assert result is None, f'Expected annotation for OBB at index {idx} to be invalid'
8 changes: 4 additions & 4 deletions tests/test_import_yolo.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def test_base_import_yolo():

#generated labels from config xml
label_element = ET.parse(out_config_file).getroot()[2]
lables_generated = [x.attrib['value'] for x in label_element.getchildren()]
assert set(labels) == set(lables_generated), "> generated class labels do not match original labels"
labels_generated = [x.attrib['value'] for x in label_element.getchildren()]
assert set(labels) == set(labels_generated), "> generated class labels do not match original labels"

#total image files in the input folder
img_files = glob.glob(os.path.join(input_data_dir,'images','*'))
Expand Down Expand Up @@ -72,9 +72,9 @@ def test_base_import_yolo_with_img_dims():

# generated labels from config xml
label_element = ET.parse(out_config_file).getroot()[2]
lables_generated = [x.attrib['value'] for x in label_element.getchildren()]
labels_generated = [x.attrib['value'] for x in label_element.getchildren()]
assert set(labels) == set(
lables_generated
labels_generated
), "> generated class labels do not match original labels"

#total image files in the input folder
Expand Down
10 changes: 10 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import math


def almost_equal_1d(a, b, tol=1e-9):
return all(math.isclose(x, y, abs_tol=tol) for x, y in zip(a, b))

def almost_equal_2d(a, b, tol=1e-9):
a = [z for x in a for z in x]
b = [z for x in b for z in x]
return all(math.isclose(x, y, abs_tol=tol) for x, y in zip(a, b))

0 comments on commit 4e76ff5

Please sign in to comment.