зеркало из
				https://github.com/ssciwr/AMMICO.git
				synced 2025-10-30 21:46:04 +02:00 
			
		
		
		
	Tests (#37)
* add py test * update dependencies and markers * Update ci.yml * fix linting * trying with editable install for cov * fix code smells * fix code smells Co-authored-by: xianghe ma <maxianghe@outlook.com>
Этот коммит содержится в:
		
							родитель
							
								
									9b3e3796fa
								
							
						
					
					
						Коммит
						27732c5fab
					
				
							
								
								
									
										6
									
								
								.github/workflows/ci.yml
									
									
									
									
										поставляемый
									
									
								
							
							
						
						
									
										6
									
								
								.github/workflows/ci.yml
									
									
									
									
										поставляемый
									
									
								
							| @ -27,13 +27,13 @@ jobs: | |||||||
|     - name: Install dependencies |     - name: Install dependencies | ||||||
|       run: | |       run: | | ||||||
|         python -m pip install --upgrade pip |         python -m pip install --upgrade pip | ||||||
|         python -m pip install . |         python -m pip install -e . | ||||||
|     - name: Run pytest |     - name: Run pytest | ||||||
|       run: | |       run: | | ||||||
|         cd misinformation |         cd misinformation | ||||||
|         python -m pytest -s --cov=. --cov-report=xml |         python -m pytest -s -m "not imageai" --cov=. --cov-report=xml | ||||||
|     - name: Upload coverage |     - name: Upload coverage | ||||||
|       uses: codecov/codecov-action@v1 |       uses: codecov/codecov-action@v3 | ||||||
|       with: |       with: | ||||||
|         fail_ci_if_error: true |         fail_ci_if_error: true | ||||||
|         files: misinformation/coverage.xml |         files: misinformation/coverage.xml | ||||||
|  | |||||||
							
								
								
									
										1
									
								
								misinformation/test/data/example_analysis_faces.json
									
									
									
									
									
										Обычный файл
									
								
							
							
						
						
									
										1
									
								
								misinformation/test/data/example_analysis_faces.json
									
									
									
									
									
										Обычный файл
									
								
							| @ -0,0 +1 @@ | |||||||
|  | {"IMG_2746": {"filename": "./test/data/IMG_2746.png", "face": "Yes", "multiple_faces": "Yes", "no_faces": 11, "wears_mask": ["No", "No", "Yes"], "age": [36, 35, 33], "gender": ["Man", "Man", "Man"], "race": ["white", "white", null], "emotion": [["sad", 73.24262697950762], ["fear", 84.20094847679138], null], "emotion (category)": ["Negative", "Negative", null]}} | ||||||
							
								
								
									
										1
									
								
								misinformation/test/data/example_analysis_objects.json
									
									
									
									
									
										Обычный файл
									
								
							
							
						
						
									
										1
									
								
								misinformation/test/data/example_analysis_objects.json
									
									
									
									
									
										Обычный файл
									
								
							| @ -0,0 +1 @@ | |||||||
|  | {"IMG_2746": {"filename": "./test/data/IMG_2809.png", "person": "yes", "bicycle": "no", "car": "yes", "motorcycle": "no", "airplane": "no", "bus": "yes", "train": "no", "truck": "no", "boat": "no", "traffic light": "no", "cell phone": "no"}} | ||||||
| @ -1,14 +1 @@ | |||||||
| { | {"filename": "./test/data/IMG_2809.png", "person": "yes", "bicycle": "no", "car": "yes", "motorcycle": "no", "airplane": "no", "bus": "yes", "train": "no", "truck": "no", "boat": "no", "traffic light": "no", "cell phone": "no"} | ||||||
|     "filename": "./test/data/IMG_2809.png",  |  | ||||||
|     "person": "yes",  |  | ||||||
|     "bicycle": "no",  |  | ||||||
|     "car": "yes",  |  | ||||||
|     "motorcycle": "no",  |  | ||||||
|     "airplane": "no",  |  | ||||||
|     "bus": "yes",  |  | ||||||
|     "train": "no",  |  | ||||||
|     "truck": "no",  |  | ||||||
|     "boat": "no",  |  | ||||||
|     "traffic light": "no",  |  | ||||||
|     "cell phone": "no" |  | ||||||
| } |  | ||||||
							
								
								
									
										14
									
								
								misinformation/test/data/example_objects_imageai.json
									
									
									
									
									
										Обычный файл
									
								
							
							
						
						
									
										14
									
								
								misinformation/test/data/example_objects_imageai.json
									
									
									
									
									
										Обычный файл
									
								
							| @ -0,0 +1,14 @@ | |||||||
|  | { | ||||||
|  |     "filename": "./test/data/IMG_2809.png", | ||||||
|  |     "person": "yes", | ||||||
|  |     "bicycle": "yes", | ||||||
|  |     "car": "yes", | ||||||
|  |     "motorcycle": "no", | ||||||
|  |     "airplane": "no", | ||||||
|  |     "bus": "yes", | ||||||
|  |     "train": "no", | ||||||
|  |     "truck": "no", | ||||||
|  |     "boat": "no", | ||||||
|  |     "traffic light": "no", | ||||||
|  |     "cell phone": "no" | ||||||
|  | } | ||||||
| @ -1 +0,0 @@ | |||||||
| {'image_objects': {'filename': './misinformation/test/data/IMG_2809.png', 'person': 'yes', 'bicycle': 'yes', 'car': 'yes', 'motorcycle': 'no', 'airplane': 'no', 'bus': 'yes', 'train': 'no', 'truck': 'no', 'boat': 'no', 'traffic light': 'no', 'cell phone': 'no'}} |  | ||||||
							
								
								
									
										
											Двоичные данные
										
									
								
								misinformation/test/data/pic1.png
									
									
									
									
									
										Обычный файл
									
								
							
							
						
						
									
										
											Двоичные данные
										
									
								
								misinformation/test/data/pic1.png
									
									
									
									
									
										Обычный файл
									
								
							
										
											Двоичный файл не отображается.
										
									
								
							| После Ширина: | Высота: | Размер: 671 KiB | 
							
								
								
									
										
											Двоичные данные
										
									
								
								misinformation/test/data/pic2.png
									
									
									
									
									
										Обычный файл
									
								
							
							
						
						
									
										
											Двоичные данные
										
									
								
								misinformation/test/data/pic2.png
									
									
									
									
									
										Обычный файл
									
								
							
										
											Двоичный файл не отображается.
										
									
								
							| После Ширина: | Высота: | Размер: 272 KiB | 
							
								
								
									
										3
									
								
								misinformation/test/pytest.ini
									
									
									
									
									
										Обычный файл
									
								
							
							
						
						
									
										3
									
								
								misinformation/test/pytest.ini
									
									
									
									
									
										Обычный файл
									
								
							| @ -0,0 +1,3 @@ | |||||||
|  | [pytest] | ||||||
|  | markers = | ||||||
|  |     imageai: mark a test related to imageai. | ||||||
							
								
								
									
										63
									
								
								misinformation/test/test_cropposts.py
									
									
									
									
									
										Обычный файл
									
								
							
							
						
						
									
										63
									
								
								misinformation/test/test_cropposts.py
									
									
									
									
									
										Обычный файл
									
								
							| @ -0,0 +1,63 @@ | |||||||
|  | import misinformation.cropposts as crpo | ||||||
|  | import numpy as np | ||||||
|  | from PIL import Image | ||||||
|  | 
 | ||||||
|  | TEST_IMAGE_1 = "./test/data/pic1.png" | ||||||
|  | TEST_IMAGE_2 = "./test/data/pic2.png" | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def test_matching_points(): | ||||||
|  |     ref_view = np.array(Image.open(TEST_IMAGE_2)) | ||||||
|  |     view = np.array(Image.open(TEST_IMAGE_1)) | ||||||
|  |     filtered_matches, kp1, kp2 = crpo.matching_points(ref_view, view) | ||||||
|  |     assert len(filtered_matches) > 0 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def test_kp_from_matches(): | ||||||
|  |     ref_view = np.array(Image.open(TEST_IMAGE_2)) | ||||||
|  |     view = np.array(Image.open(TEST_IMAGE_1)) | ||||||
|  |     filtered_matches, kp1, kp2 = crpo.matching_points(ref_view, view) | ||||||
|  |     kp1, kp2 = crpo.kp_from_matches(filtered_matches, kp1, kp2) | ||||||
|  | 
 | ||||||
|  |     assert kp1.shape[0] == len(filtered_matches) | ||||||
|  |     assert kp2.shape[0] == len(filtered_matches) | ||||||
|  |     assert kp1.shape[1] == 2 | ||||||
|  |     assert kp2.shape[1] == 2 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def test_compute_crop_corner(): | ||||||
|  |     ref_view = np.array(Image.open(TEST_IMAGE_2)) | ||||||
|  |     view = np.array(Image.open(TEST_IMAGE_1)) | ||||||
|  |     filtered_matches, kp1, kp2 = crpo.matching_points(ref_view, view) | ||||||
|  |     corner = crpo.compute_crop_corner(filtered_matches, kp1, kp2) | ||||||
|  |     print(view.shape) | ||||||
|  |     print(corner) | ||||||
|  |     assert corner is not None | ||||||
|  |     v, h = corner | ||||||
|  |     assert 0 <= v < view.shape[0] | ||||||
|  |     assert 0 <= h < view.shape[0] | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def test_crop_posts_image(): | ||||||
|  |     ref_view = np.array(Image.open(TEST_IMAGE_2)) | ||||||
|  |     view = np.array(Image.open(TEST_IMAGE_1)) | ||||||
|  |     rte = crpo.crop_posts_image(ref_view, view) | ||||||
|  |     assert rte is not None | ||||||
|  |     crop_view, match_num = rte | ||||||
|  |     assert match_num > 0 | ||||||
|  |     assert crop_view.shape[0] * crop_view.shape[1] <= view.shape[0] * view.shape[1] | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def test_crop_posts_from_refs(): | ||||||
|  |     ref_view = np.array(Image.open(TEST_IMAGE_2)) | ||||||
|  |     view = np.array(Image.open(TEST_IMAGE_1)) | ||||||
|  |     ref_views = [ref_view] | ||||||
|  |     crop_view = crpo.crop_posts_from_refs(ref_views, view) | ||||||
|  |     assert crop_view.shape[0] * crop_view.shape[1] <= view.shape[0] * view.shape[1] | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def test_get_file_list(): | ||||||
|  |     ref_list = [] | ||||||
|  |     ref_dir = "./test/data" | ||||||
|  |     ref_list = crpo.get_file_list(ref_dir, ref_list, ext="png") | ||||||
|  |     assert len(ref_list) > 0 | ||||||
							
								
								
									
										33
									
								
								misinformation/test/test_display.py
									
									
									
									
									
										Обычный файл
									
								
							
							
						
						
									
										33
									
								
								misinformation/test/test_display.py
									
									
									
									
									
										Обычный файл
									
								
							| @ -0,0 +1,33 @@ | |||||||
|  | import json | ||||||
|  | from misinformation.display import explore_analysis | ||||||
|  | from pytest import approx | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def test_explore_analysis_faces(): | ||||||
|  |     mydict = {"IMG_2746": {"filename": "./test/data/IMG_2746.png"}} | ||||||
|  |     explore_analysis(mydict, identify="faces") | ||||||
|  |     with open("./test/data/example_analysis_faces.json", "r") as file: | ||||||
|  |         outs = json.load(file) | ||||||
|  | 
 | ||||||
|  |     for im_key in mydict.keys(): | ||||||
|  |         sub_dict = mydict[im_key] | ||||||
|  |         for key in sub_dict.keys(): | ||||||
|  |             if key != "emotion": | ||||||
|  |                 assert sub_dict[key] == outs[im_key][key] | ||||||
|  |         # json can't handle tuples natively | ||||||
|  |         for i in range(0, len(sub_dict["emotion"])): | ||||||
|  |             temp = ( | ||||||
|  |                 list(sub_dict["emotion"][i]) | ||||||
|  |                 if type(sub_dict["emotion"][i]) == tuple | ||||||
|  |                 else sub_dict["emotion"][i] | ||||||
|  |             ) | ||||||
|  |             assert approx(temp) == outs[im_key]["emotion"][i] | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def test_explore_analysis_objects(): | ||||||
|  |     mydict = {"IMG_2746": {"filename": "./test/data/IMG_2809.png"}} | ||||||
|  |     explore_analysis(mydict, identify="objects") | ||||||
|  |     with open("./test/data/example_analysis_objects.json", "r") as file: | ||||||
|  |         outs = json.load(file) | ||||||
|  | 
 | ||||||
|  |     assert str(mydict) == str(outs) | ||||||
| @ -1,9 +1,17 @@ | |||||||
| import json | import json | ||||||
| import pytest | import pytest | ||||||
| import misinformation |  | ||||||
| import misinformation.objects as ob | import misinformation.objects as ob | ||||||
| import misinformation.objects_cvlib as ob_cvlib | import misinformation.objects_cvlib as ob_cvlib | ||||||
| 
 | 
 | ||||||
|  | # import misinformation.objects_imageai as ob_iai | ||||||
|  | 
 | ||||||
|  | OBJECT_1 = "cell phone" | ||||||
|  | OBJECT_2 = "motorcycle" | ||||||
|  | OBJECT_3 = "traffic light" | ||||||
|  | TEST_IMAGE_1 = "./test/data/IMG_2809.png" | ||||||
|  | JSON_1 = "./test/data/example_objects_cvlib.json" | ||||||
|  | JSON_2 = "./test/data/example_objects_imageai.json" | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| @pytest.fixture() | @pytest.fixture() | ||||||
| def default_objects(): | def default_objects(): | ||||||
| @ -11,7 +19,7 @@ def default_objects(): | |||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def test_objects_from_cvlib(default_objects): | def test_objects_from_cvlib(default_objects): | ||||||
|     objects_list = ["cell phone", "motorcycle", "traffic light"] |     objects_list = [OBJECT_1, OBJECT_2, OBJECT_3] | ||||||
|     objects = ob_cvlib.objects_from_cvlib(objects_list) |     objects = ob_cvlib.objects_from_cvlib(objects_list) | ||||||
|     out_objects = default_objects |     out_objects = default_objects | ||||||
|     for obj in objects_list: |     for obj in objects_list: | ||||||
| @ -21,11 +29,113 @@ def test_objects_from_cvlib(default_objects): | |||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def test_analyse_image_cvlib(): | def test_analyse_image_cvlib(): | ||||||
|     mydict = {"filename": "./test/data/IMG_2809.png"} |     mydict = {"filename": TEST_IMAGE_1} | ||||||
|     ob_cvlib.ObjectCVLib().analyse_image(mydict) |     ob_cvlib.ObjectCVLib().analyse_image(mydict) | ||||||
| 
 | 
 | ||||||
|     with open("./test/data/example_objects_cvlib.json", "r") as file: |     with open(JSON_1, "r") as file: | ||||||
|         out_dict = json.load(file) |         out_dict = json.load(file) | ||||||
|     for key in mydict.keys(): |     for key in mydict.keys(): | ||||||
|         print(key) |  | ||||||
|         assert mydict[key] == out_dict[key] |         assert mydict[key] == out_dict[key] | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def test_init_default_objects(): | ||||||
|  |     default_obj_list = [ | ||||||
|  |         "person", | ||||||
|  |         "bicycle", | ||||||
|  |         "car", | ||||||
|  |         OBJECT_2, | ||||||
|  |         "airplane", | ||||||
|  |         "bus", | ||||||
|  |         "train", | ||||||
|  |         "truck", | ||||||
|  |         "boat", | ||||||
|  |         OBJECT_3, | ||||||
|  |         OBJECT_1, | ||||||
|  |     ] | ||||||
|  |     init_objects = ob_cvlib.init_default_objects() | ||||||
|  |     for obj in default_obj_list: | ||||||
|  |         assert init_objects[obj] == "no" | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def test_analyse_image_from_file_cvlib(): | ||||||
|  |     file_path = TEST_IMAGE_1 | ||||||
|  |     objs = ob_cvlib.ObjectCVLib().analyse_image_from_file(file_path) | ||||||
|  | 
 | ||||||
|  |     with open(JSON_1, "r") as file: | ||||||
|  |         out_dict = json.load(file) | ||||||
|  |     for key in objs.keys(): | ||||||
|  |         assert objs[key] == out_dict[key] | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def test_detect_objects_cvlib(): | ||||||
|  |     file_path = TEST_IMAGE_1 | ||||||
|  |     objs = ob_cvlib.ObjectCVLib().detect_objects_cvlib(file_path) | ||||||
|  | 
 | ||||||
|  |     with open(JSON_1, "r") as file: | ||||||
|  |         out_dict = json.load(file) | ||||||
|  |     for key in objs.keys(): | ||||||
|  |         assert objs[key] == out_dict[key] | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | @pytest.mark.imageai | ||||||
|  | def test_objects_from_imageai(default_objects): | ||||||
|  |     objects_list = [OBJECT_1, OBJECT_2, OBJECT_3] | ||||||
|  |     objs_input = [ | ||||||
|  |         {"name": OBJECT_1}, | ||||||
|  |         {"name": OBJECT_2}, | ||||||
|  |         {"name": OBJECT_3}, | ||||||
|  |     ] | ||||||
|  |     objects = ob_iai.objects_from_imageai(objs_input)  # noqa: F821 | ||||||
|  |     out_objects = default_objects | ||||||
|  |     for obj in objects_list: | ||||||
|  |         out_objects[obj] = "yes" | ||||||
|  | 
 | ||||||
|  |     assert str(objects) == str(out_objects) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | @pytest.mark.imageai | ||||||
|  | def test_analyse_image_from_file_imageai(): | ||||||
|  |     file_path = TEST_IMAGE_1 | ||||||
|  |     objs = ob_iai.ObjectImageAI().analyse_image_from_file(file_path)  # noqa: F821 | ||||||
|  | 
 | ||||||
|  |     with open(JSON_2, "r") as file: | ||||||
|  |         out_dict = json.load(file) | ||||||
|  |     for key in objs.keys(): | ||||||
|  |         assert objs[key] == out_dict[key] | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | @pytest.mark.imageai | ||||||
|  | def test_detect_objects_imageai(): | ||||||
|  |     file_path = TEST_IMAGE_1 | ||||||
|  |     objs = ob_iai.ObjectImageAI().detect_objects_imageai(file_path)  # noqa: F821 | ||||||
|  | 
 | ||||||
|  |     with open(JSON_2, "r") as file: | ||||||
|  |         out_dict = json.load(file) | ||||||
|  |     for key in objs.keys(): | ||||||
|  |         assert objs[key] == out_dict[key] | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | @pytest.mark.imageai | ||||||
|  | def test_analyse_image_imageai(): | ||||||
|  |     mydict = {"filename": TEST_IMAGE_1} | ||||||
|  |     ob_iai.ObjectImageAI().analyse_image(mydict)  # noqa: F821 | ||||||
|  |     with open(JSON_2, "r") as file: | ||||||
|  |         out_dict = json.load(file) | ||||||
|  |     for key in mydict.keys(): | ||||||
|  |         assert mydict[key] == out_dict[key] | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def test_set_keys(default_objects): | ||||||
|  |     mydict = {"filename": TEST_IMAGE_1} | ||||||
|  |     key_objs = ob.ObjectDetector(mydict).set_keys() | ||||||
|  |     assert str(default_objects) == str(key_objs) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def test_analyse_image(): | ||||||
|  |     mydict = {"filename": TEST_IMAGE_1} | ||||||
|  |     ob.ObjectDetector.set_client_to_cvlib() | ||||||
|  |     ob.ObjectDetector(mydict).analyse_image() | ||||||
|  |     with open(JSON_1, "r") as file: | ||||||
|  |         out_dict = json.load(file) | ||||||
|  | 
 | ||||||
|  |     assert str(mydict) == str(out_dict) | ||||||
|  | |||||||
| @ -37,6 +37,9 @@ dependencies = [ | |||||||
|     "openpyxl", |     "openpyxl", | ||||||
|     "pytest", |     "pytest", | ||||||
|     "pytest-cov", |     "pytest-cov", | ||||||
|  |     "matplotlib", | ||||||
|  |     "pytest", | ||||||
|  |     "opencv-contrib-python", | ||||||
| ] | ] | ||||||
| 
 | 
 | ||||||
| [project.scripts] | [project.scripts] | ||||||
|  | |||||||
| @ -14,3 +14,5 @@ keras | |||||||
| openpyxl | openpyxl | ||||||
| pytest | pytest | ||||||
| pytest-cov | pytest-cov | ||||||
|  | matplotlib | ||||||
|  | opencv-contrib-python | ||||||
|  | |||||||
		Загрузка…
	
	
			
			x
			
			
		
	
		Ссылка в новой задаче
	
	Block a user
	 Inga Ulusoy
						Inga Ulusoy