Skip to content
This repository was archived by the owner on Oct 25, 2024. It is now read-only.

Commit 788bd07

Browse files
authored
Merge pull request #21 from UCL-RITS/enforce_style
Adds style guide enforcement in CI builds
2 parents 8e930f6 + 548b008 commit 788bd07

13 files changed

+88
-77
lines changed

.github/workflows/main.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,8 @@ jobs:
3434
- name: Test with pytest
3535
run: |
3636
pytest data_preprocessing/tests/
37-
37+
- name: Style check
38+
run: black --check data_preprocessing
39+
40+
3841

data_preprocessing/scripts/convert_xml_to_yolotxt.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,20 @@ def convert_xml_annotation_to_yolo(label_path, output_path, is_masati, classes):
5858

5959
in_file.close()
6060
out_file.close()
61+
62+
6163
def main():
6264
from read_params import read_parameter_file
65+
6366
params = read_parameter_file("params.yaml")
64-
labels_dir = os.path.join(os.getcwd(), str(params["get_yolo_labels"]["annotations_dir"]))
67+
labels_dir = os.path.join(
68+
os.getcwd(), str(params["get_yolo_labels"]["annotations_dir"])
69+
)
6570
classes = ["boat"]
6671

6772
output_path = os.path.join(os.getcwd(), "yolo_annotations")
6873
print("Will output to: {}".format(output_path))
69-
74+
7075
is_masati = params["get_yolo_labels"]["is_masati"]
7176
if not os.path.exists(output_path):
7277
os.makedirs(output_path)

data_preprocessing/scripts/prepare_yolov5.py

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,13 @@
88
"""
99

1010

11-
def create_yolo_dirs(train_label_dir,
12-
val_label_dir,
13-
train_img_dir,
14-
val_img_dir,
15-
target_directory
16-
):
11+
def create_yolo_dirs(
12+
train_label_dir, val_label_dir, train_img_dir, val_img_dir, target_directory
13+
):
1714

1815
output_images_dir = os.path.join(target_directory, "images")
1916
output_labels_dir = output_images_dir.replace("images", "labels")
2017

21-
2218
# Make these directories if they don't exist
2319
if not os.path.exists(target_directory):
2420
os.mkdir(target_directory)
@@ -31,8 +27,8 @@ def create_yolo_dirs(train_label_dir,
3127

3228
output_train_label_dir = os.path.join(output_labels_dir, "train")
3329
output_validation_label_dir = os.path.join(output_labels_dir, "val")
34-
output_train_img_dir = os.path.join(output_images_dir,"train")
35-
output_validation_image_dir = os.path.join(output_images_dir,"val")
30+
output_train_img_dir = os.path.join(output_images_dir, "train")
31+
output_validation_image_dir = os.path.join(output_images_dir, "val")
3632
os.rename(train_label_dir, output_train_label_dir)
3733
os.rename(val_label_dir, output_validation_label_dir)
3834
os.rename(train_img_dir, output_train_img_dir)
@@ -42,28 +38,26 @@ def create_yolo_dirs(train_label_dir,
4238
output_train_label_dir,
4339
output_validation_label_dir,
4440
output_train_img_dir,
45-
output_validation_image_dir
41+
output_validation_image_dir,
4642
]
4743

44+
4845
def main():
4946
from read_params import read_parameter_file
47+
5048
params = read_parameter_file("params.yaml")
5149
workdir = os.getcwd()
5250
train_label_dir = os.path.join(
53-
workdir,
54-
params["create_yolov5_filestructure"]["train_labels"]
51+
workdir, params["create_yolov5_filestructure"]["train_labels"]
5552
)
5653
val_label_dir = os.path.join(
57-
workdir,
58-
params["create_yolov5_filestructure"]["val_labels"]
54+
workdir, params["create_yolov5_filestructure"]["val_labels"]
5955
)
6056
train_img_dir = os.path.join(
61-
workdir,
62-
params["create_yolov5_filestructure"]["train_imgs"]
57+
workdir, params["create_yolov5_filestructure"]["train_imgs"]
6358
)
6459
val_img_dir = os.path.join(
65-
workdir,
66-
params["create_yolov5_filestructure"]["val_imgs"]
60+
workdir, params["create_yolov5_filestructure"]["val_imgs"]
6761
)
6862
dataset = params["create_yolov5_filestructure"]["dataset"]
6963

@@ -75,22 +69,23 @@ def main():
7569
assert os.path.isdir(val_img_dir)
7670
except AssertionError:
7771
"Check that the yolo train/val label and image directories exist in this directory!"
78-
79-
assert os.path.isdir("../../data_preprocessing"), "Run this script from a subdirectory of data_preprocessing"
80-
72+
73+
assert os.path.isdir(
74+
"../../data_preprocessing"
75+
), "Run this script from a subdirectory of data_preprocessing"
76+
8177
vetii_directory = str(Path(workdir).parent.parent)
82-
data_directory = os.path.join(vetii_directory,"data")
78+
data_directory = os.path.join(vetii_directory, "data")
79+
80+
assert os.path.isdir(
81+
data_directory
82+
), "data/ directory does not exist at the top level of this project. Create this directory before running!"
8383

84-
assert os.path.isdir(data_directory), "data/ directory does not exist at the top level of this project. Create this directory before running!"
85-
8684
target_directory = os.path.join(data_directory, dataset)
8785
print(target_directory)
88-
output_dirs = create_yolo_dirs(train_label_dir,
89-
val_label_dir,
90-
train_img_dir,
91-
val_img_dir,
92-
target_directory
93-
)
86+
output_dirs = create_yolo_dirs(
87+
train_label_dir, val_label_dir, train_img_dir, val_img_dir, target_directory
88+
)
9489

9590
for outdir in output_dirs:
9691
assert os.path.isdir(outdir), "{} was not found".format(outdir)

data_preprocessing/scripts/sort_yolo_images.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def create_yolo_dirs(workdir, imgdir, labeldir):
2222
if not os.path.exists(output_images_dir):
2323
os.mkdir(output_images_dir)
2424

25-
label_list = os.listdir(os.path.join(workdir,labeldir))
25+
label_list = os.listdir(os.path.join(workdir, labeldir))
2626
for imgfile in progress_bar(os.listdir(os.path.join(workdir, imgdir))):
2727
basefile = imgfile.split(".", 1)[0]
2828
if basefile + ".txt" in label_list:
@@ -44,7 +44,7 @@ def rename_yolo_files(workdir, label_dir, imgformat):
4444

4545
old_label_path = os.path.join(labels_path, thisfile)
4646
old_image_path = old_label_path.replace("labels", "images").replace(
47-
".txt", "."+imgformat
47+
".txt", "." + imgformat
4848
)
4949
new_label_name = str(counter).zfill(5)
5050
new_label_path = os.path.join(labels_path, new_label_name + ".txt")
@@ -57,6 +57,7 @@ def rename_yolo_files(workdir, label_dir, imgformat):
5757

5858
def main():
5959
from read_params import read_parameter_file
60+
6061
yolo_annotations_dir = "yolo_annotations"
6162
workdir = os.getcwd()
6263
params = read_parameter_file("params.yaml")

data_preprocessing/scripts/train_test_split.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ def move_image_files(workdir, train, test, annotations_dir_name, params):
8686

8787
def main():
8888
from read_params import read_parameter_file
89+
8990
params = read_parameter_file("params.yaml")
9091
float_fraction(params["split_dataset"]["trainpct"])
9192
xml_annotations_dir = params["split_dataset"]["annotations_dir"]

data_preprocessing/tests/fixtures/setup_convert_xml_yolo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ def create_temp_file_io(tmpdir_factory):
3838
r" </annotation>"
3939
)
4040
test_xml.write(test_xml_string)
41-
return test_xml, test_outdir
41+
return test_xml, test_outdir

data_preprocessing/tests/fixtures/setup_prepare_yolov5.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22
from pathlib import Path
33

4+
45
@pytest.fixture(scope="session")
56
def setup_target_dir(tmpdir_factory):
67
test_target_dir = tmpdir_factory.mktemp("data_testset")
@@ -15,6 +16,6 @@ def setup_target_dir(tmpdir_factory):
1516
"val_label_dir": str(val_label_dir),
1617
"train_img_dir": str(train_img_dir),
1718
"val_img_dir": str(val_img_dir),
18-
"target_dir": str(test_target_dir)
19+
"target_dir": str(test_target_dir),
1920
}
20-
return setup_dict
21+
return setup_dict

data_preprocessing/tests/fixtures/setup_sort_yolo_images.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22
from pathlib import Path
33

4+
45
@pytest.fixture(scope="session")
56
def setup_yolo_dirs(tmpdir_factory):
67

@@ -10,7 +11,6 @@ def setup_yolo_dirs(tmpdir_factory):
1011
test_label_two = test_label_dir.join("test_2.txt")
1112
test_label_two.write("test_label_2")
1213

13-
1414
test_img_dir = tmpdir_factory.mktemp("train")
1515
test_img_one = test_img_dir.join("test_1.png")
1616
test_img_one.write("test_img_1")
@@ -22,9 +22,9 @@ def setup_yolo_dirs(tmpdir_factory):
2222
assert test_img_one.read() == "test_img_1"
2323
assert test_img_two.read() == "test_img_2"
2424

25-
2625
return test_label_dir, test_img_dir
2726

27+
2828
@pytest.fixture(scope="session")
2929
def setup_yolo_rename_dirs(tmpdir_factory):
3030

@@ -40,4 +40,3 @@ def setup_yolo_rename_dirs(tmpdir_factory):
4040
assert test_img_one.read() == "test_img_1"
4141

4242
return test_old_label_dir, test_old_img_dir
43-

data_preprocessing/tests/fixtures/setup_train_test_split.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,23 @@ def setup_file_list(tmpdir_factory):
1616
str(test_xml_two),
1717
str(test_xml_three),
1818
str(test_xml_four),
19-
]
19+
]
2020
return test_file_list
2121

22+
2223
@pytest.fixture(scope="session")
2324
def setup_out_dir(tmpdir_factory):
2425
temp_dir = tmpdir_factory.mktemp("test_outdir")
2526
return temp_dir
2627

28+
2729
@pytest.fixture(scope="session")
2830
def setup_move_img_files(tmpdir_factory):
2931

3032
test_labels_dir = tmpdir_factory.mktemp("label")
3133
test_labels_dir.join("test_1.xml")
3234
test_labels_dir.join("test_2.xml")
33-
35+
3436
test_train_dir = tmpdir_factory.mktemp("train")
3537
test_valid_dir = tmpdir_factory.mktemp("valid")
3638

@@ -42,30 +44,24 @@ def setup_move_img_files(tmpdir_factory):
4244
assert test_img_one.read() == "test1"
4345
assert test_img_two.read() == "test2"
4446

45-
test_val_dict = {
46-
"Filename": [str(test_img_dir)+"/test_1.png"]
47-
}
48-
49-
test_train_dict = {
50-
"Filename": [str(test_img_dir)+"/test_2.png"]
51-
}
47+
test_val_dict = {"Filename": [str(test_img_dir) + "/test_1.png"]}
48+
49+
test_train_dict = {"Filename": [str(test_img_dir) + "/test_2.png"]}
5250
test_train_set = pd.DataFrame.from_dict(test_train_dict)
5351
test_val_set = pd.DataFrame.from_dict(test_val_dict)
5452

55-
5653
params = {
5754
"split_dataset": {
5855
"train_dir": str(test_train_dir),
5956
"valid_dir": str(test_valid_dir),
6057
"img_dir": str(test_img_dir),
61-
"file_type": "png"
58+
"file_type": "png",
6259
}
6360
}
6461
setup_dict = {
6562
"train": test_train_set,
6663
"test": test_val_set,
6764
"labels": str(test_labels_dir),
68-
"params": params
65+
"params": params,
6966
}
7067
return setup_dict
71-

data_preprocessing/tests/test_convert_xml_to_yolotxt.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@ def test_yolo_conversion():
1313

1414
boundaries = (469, 472, 28, 37)
1515

16-
1716
width = 512.0
1817
height = 512.0
1918
bounding_box = funcs.yolo_conversion((width, height), boundaries)
2019
expected_bounding_box = [0.9189453125, 0.0634765625, 0.005859375, 0.017578125]
2120
assert list(bounding_box) == expected_bounding_box
2221

22+
2323
@pytest.mark.usefixtures("create_temp_file_io")
2424
def test_convert_xml_annotation_to_yolo(create_temp_file_io):
2525
test_xml, test_outdir = create_temp_file_io
2626
print(test_xml, test_outdir)
27-
funcs.convert_xml_annotation_to_yolo(test_xml, test_outdir, True, ['boat'])
27+
funcs.convert_xml_annotation_to_yolo(test_xml, test_outdir, True, ["boat"])
2828
assert len(os.listdir(test_outdir)) == 1
2929
assert os.listdir(test_outdir)[0] == "test.txt"
30-
output_file = os.path.join(test_outdir,"test.txt")
30+
output_file = os.path.join(test_outdir, "test.txt")
3131
output_text = Path(output_file).read_text().replace("\n", "")
32-
assert output_text == "0 0.9189453125 0.0634765625 0.005859375 0.017578125"
32+
assert output_text == "0 0.9189453125 0.0634765625 0.005859375 0.017578125"

0 commit comments

Comments
 (0)