|
| 1 | +import pytest |
| 2 | +import operator |
1 | 3 | import os |
2 | 4 | import shutil |
| 5 | +import numpy as np |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture(scope="session") |
| 9 | +def get_params(): |
| 10 | + param_file = os.path.join("tests", "accuracy_tests", "simulation_parameters.txt") |
| 11 | + with open(param_file, "r") as file: |
| 12 | + sim_params = [line.strip().split() for line in file] |
| 13 | + |
| 14 | + params = {} |
| 15 | + for param_name, param_value in sim_params: |
| 16 | + params[param_name] = float(param_value) |
| 17 | + |
| 18 | + return params |
3 | 19 |
|
4 | 20 |
|
5 | 21 | def pytest_configure(config): |
6 | 22 | """ |
7 | | - Prepare path and report file for functional tests |
| 23 | + Prepare path and report file for accuracy tests and functional tests |
8 | 24 | """ |
| 25 | + accu_output_path = os.path.join("tests", "accuracy_tests", "outputs") |
| 26 | + if os.path.exists(accu_output_path): |
| 27 | + shutil.rmtree(accu_output_path) |
| 28 | + os.mkdir(accu_output_path) |
| 29 | + |
9 | 30 | func_output_path = os.path.join("tests", "functional_tests", "outputs") |
10 | 31 | if os.path.exists(func_output_path): |
11 | 32 | shutil.rmtree(func_output_path) |
12 | 33 | os.mkdir(func_output_path) |
| 34 | + |
| 35 | + report_path = os.path.join("tests", "accuracy_tests", "accu_report.txt") |
| 36 | + f = open(report_path, "w") |
| 37 | + f.close() |
| 38 | + |
| 39 | + |
| 40 | +@pytest.hookimpl(hookwrapper=True) |
| 41 | +def pytest_runtest_makereport(): |
| 42 | + out = yield |
| 43 | + report = out.get_result() |
| 44 | + if ( |
| 45 | + report.nodeid[:37] == "tests/accuracy_tests/run_accu_test.py" |
| 46 | + and report.when == "call" |
| 47 | + ): |
| 48 | + stdout = report.sections |
| 49 | + if "combined" in report.nodeid or "ped_only" in report.nodeid: |
| 50 | + num_file = 3 |
| 51 | + else: |
| 52 | + num_file = 2 |
| 53 | + accu = stdout[-1][-1].split("\n")[-(2 + 3 * num_file) :] |
| 54 | + name = accu[0].split()[-1] |
| 55 | + with open("tests/accuracy_tests/accu_report.txt", "a") as file: |
| 56 | + for i in range(num_file): |
| 57 | + assessed_file = accu[i * 3 + 1].split()[-1] |
| 58 | + file.write(name + " " + assessed_file + " " + accu[i * 3 + 2] + "\n") |
| 59 | + file.write(name + " " + assessed_file + " " + accu[i * 3 + 3] + "\n") |
| 60 | + |
| 61 | + |
| 62 | +@pytest.hookimpl() |
| 63 | +def pytest_terminal_summary(terminalreporter): |
| 64 | + param_file = os.path.join("tests", "accuracy_tests", "simulation_parameters.txt") |
| 65 | + with open(param_file, "r") as file: |
| 66 | + sim_params = [line.strip().split() for line in file] |
| 67 | + |
| 68 | + params = {} |
| 69 | + for param_name, param_value in sim_params: |
| 70 | + params[param_name] = float(param_value) |
| 71 | + |
| 72 | + nGen = int(params["nGen"]) |
| 73 | + |
| 74 | + file_types = [ |
| 75 | + "genotypes", |
| 76 | + "haplotypes", |
| 77 | + "segregation", |
| 78 | + ] |
| 79 | + columns = ( |
| 80 | + "Test Name", |
| 81 | + "File Name", |
| 82 | + "Accu Type", |
| 83 | + "Population Accu", |
| 84 | + "Gen1 Accu", |
| 85 | + "Gen2 Accu", |
| 86 | + "Gen3 Accu", |
| 87 | + "Gen4 Accu", |
| 88 | + "Gen5 Accu", |
| 89 | + ) |
| 90 | + dt = {"names": columns, "formats": ("U76", "U28", "U25") + ("f4",) * (nGen + 1)} |
| 91 | + accu = np.loadtxt("tests/accuracy_tests/accu_report.txt", encoding=None, dtype=dt) |
| 92 | + |
| 93 | + mkr_accu = list( |
| 94 | + filter( |
| 95 | + lambda x: x["Accu Type"] == "Marker_accuracies", |
| 96 | + accu, |
| 97 | + ) |
| 98 | + ) |
| 99 | + ind_accu = list( |
| 100 | + filter( |
| 101 | + lambda x: x["Accu Type"] == "Individual_accuracies", |
| 102 | + accu, |
| 103 | + ) |
| 104 | + ) |
| 105 | + |
| 106 | + mkr_accu_file = {} |
| 107 | + ind_accu_file = {} |
| 108 | + for file_type in file_types: |
| 109 | + mkr_accu_file[file_type] = list( |
| 110 | + filter( |
| 111 | + lambda x: x["File Name"] == file_type, |
| 112 | + mkr_accu, |
| 113 | + ) |
| 114 | + ) |
| 115 | + ind_accu_file[file_type] = list( |
| 116 | + filter( |
| 117 | + lambda x: x["File Name"] == file_type, |
| 118 | + ind_accu, |
| 119 | + ) |
| 120 | + ) |
| 121 | + |
| 122 | + test_names = list( |
| 123 | + map( |
| 124 | + lambda x: x["Test Name"], |
| 125 | + filter( |
| 126 | + lambda x: x["File Name"] == file_types[0], |
| 127 | + mkr_accu, |
| 128 | + ), |
| 129 | + ) |
| 130 | + ) |
| 131 | + test_nums = {} |
| 132 | + count = 0 |
| 133 | + for test_name in test_names: |
| 134 | + count += 1 |
| 135 | + test_nums[test_name] = count |
| 136 | + |
| 137 | + sorted_mkr_accu_file = {} |
| 138 | + sorted_ind_accu_file = {} |
| 139 | + for file_type in file_types: |
| 140 | + sorted_mkr_accu_file[file_type] = sorted( |
| 141 | + mkr_accu_file[file_type], |
| 142 | + key=operator.itemgetter(*columns[3:]), |
| 143 | + reverse=True, |
| 144 | + ) |
| 145 | + sorted_ind_accu_file[file_type] = sorted( |
| 146 | + ind_accu_file[file_type], |
| 147 | + key=operator.itemgetter(*columns[3:]), |
| 148 | + reverse=True, |
| 149 | + ) |
| 150 | + |
| 151 | + format_first_row = "{:<20} " * (nGen + 2) + "{:<35} " |
| 152 | + format_row = "{:<20.0f} " + "{:<20.3f} " * (nGen + 1) + "{:<35} " |
| 153 | + out_columns = ( |
| 154 | + "Test Num", |
| 155 | + "Population Accu", |
| 156 | + "Gen1 Accu", |
| 157 | + "Gen2 Accu", |
| 158 | + "Gen3 Accu", |
| 159 | + "Gen4 Accu", |
| 160 | + "Gen5 Accu", |
| 161 | + "Test Name", |
| 162 | + ) |
| 163 | + |
| 164 | + def write_report(accu_type, sorted=False): |
| 165 | + if not sorted: |
| 166 | + terminalreporter.write_sep("=", accu_type + " Accuracy") |
| 167 | + if accu_type == "Marker": |
| 168 | + reports = mkr_accu_file |
| 169 | + else: |
| 170 | + reports = ind_accu_file |
| 171 | + else: |
| 172 | + terminalreporter.write_sep("=", accu_type + " Accuracy (Order by accuracy)") |
| 173 | + if accu_type == "Marker": |
| 174 | + reports = sorted_mkr_accu_file |
| 175 | + else: |
| 176 | + reports = sorted_ind_accu_file |
| 177 | + |
| 178 | + for file_type in file_types: |
| 179 | + terminalreporter.write_sep("-", file_type) |
| 180 | + terminalreporter.write_line(format_first_row.format(*out_columns)) |
| 181 | + for test in reports[file_type]: |
| 182 | + terminalreporter.write_line( |
| 183 | + format_row.format( |
| 184 | + test_nums[test["Test Name"]], |
| 185 | + test["Population Accu"], |
| 186 | + test["Gen1 Accu"], |
| 187 | + test["Gen2 Accu"], |
| 188 | + test["Gen3 Accu"], |
| 189 | + test["Gen4 Accu"], |
| 190 | + test["Gen5 Accu"], |
| 191 | + test["Test Name"], |
| 192 | + ) |
| 193 | + ) |
| 194 | + |
| 195 | + write_report("Marker", sorted=False) |
| 196 | + write_report("Individual", sorted=False) |
| 197 | + write_report("Marker", sorted=True) |
| 198 | + write_report("Individual", sorted=True) |
0 commit comments