-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_machine_log_analyzer.py
More file actions
344 lines (275 loc) · 10.8 KB
/
test_machine_log_analyzer.py
File metadata and controls
344 lines (275 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Script kiểm thử chức năng phân tích log file máy điều trị.
Script này thực hiện kiểm thử các tính năng của module machine_log_analyzer.py,
cho phép phân tích file log của máy điều trị và tạo báo cáo QA.
"""
import os
import sys
import argparse
import logging
import tempfile
import matplotlib.pyplot as plt
from typing import Dict, Any, Optional
import numpy as np
import pandas as pd
from quangtps.evaluation.qa.machine_log_analyzer import LogFileAnalyzer, LogFileType
from quangtps.evaluation.qa.deviation_report import create_deviation_report
from quangtps.evaluation.qa.delivery_qa import DeliveryQA
# Thiết lập logging
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
def create_test_log_file(file_type: str, output_dir: Optional[str] = None) -> str:
"""
Tạo file log test có chứa thông tin giả lập để kiểm thử.
Parameters
----------
file_type : str
Loại file log cần tạo: "varian_trajectory", "varian_dynalogs",
"elekta_integrity", "elekta_iviewgt"
output_dir : Optional[str], optional
Thư mục đầu ra, mặc định là None (sử dụng thư mục tạm)
Returns
-------
str
Đường dẫn đến file log đã tạo
"""
if output_dir is None:
output_dir = tempfile.mkdtemp(prefix="quangtps_test_")
# Tạo tên file dựa trên loại
if file_type == "varian_trajectory":
file_path = os.path.join(output_dir, "test_trajectory.bin")
# Tạo nội dung giả lập cho file log Varian TrajectoryLog
content = "Varian Medical Systems TrajectoryLog Version 1.0\n"
content += "Machine ID: TB01\n"
content += "Patient ID: TEST01\n"
content += "Plan Name: TEST_PLAN\n"
with open(file_path, "w") as f:
f.write(content)
elif file_type == "varian_dynalogs":
file_path = os.path.join(output_dir, "test_dynalogs.dlg")
# Tạo nội dung giả lập cho file log Varian Dynalogs
content = "Dynalog File for Machine: TB01\n"
content += "State:Actual\n"
with open(file_path, "w") as f:
f.write(content)
elif file_type == "elekta_integrity":
file_path = os.path.join(output_dir, "test_integrity.xml")
# Tạo nội dung giả lập cho file log Elekta Integrity
content = "<Elekta>\n"
content += " <Integrity>\n"
content += " <MachineName>VersaHD01</MachineName>\n"
content += " </Integrity>\n"
content += "</Elekta>\n"
with open(file_path, "w") as f:
f.write(content)
elif file_type == "elekta_iviewgt":
file_path = os.path.join(output_dir, "test_iviewgt.xml")
# Tạo nội dung giả lập cho file log Elekta iViewGT
content = "<Elekta>\n"
content += " <iViewGT>\n"
content += " <MachineName>VersaHD01</MachineName>\n"
content += " </iViewGT>\n"
content += "</Elekta>\n"
with open(file_path, "w") as f:
f.write(content)
else:
# Loại không xác định
file_path = os.path.join(output_dir, "unknown_log.txt")
with open(file_path, "w") as f:
f.write("Unknown log file format\n")
logger.info(f"Đã tạo file log test: {file_path}")
return file_path
def create_test_plan_data() -> Dict[str, Any]:
"""
Tạo dữ liệu kế hoạch xạ trị giả lập cho việc kiểm thử.
Returns
-------
Dict[str, Any]
Dữ liệu kế hoạch xạ trị
"""
# Tạo dữ liệu kế hoạch đơn giản
plan_data = {
"patient_id": "TEST01",
"plan_name": "TEST_PLAN",
"fraction": 1,
"beam_data": [
{
"beam_name": "Field1",
"gantry_angle": 0.0,
"collimator_angle": 0.0,
"couch_angle": 0.0,
"mu": 100.0,
"dose_rate": 600.0,
"jaw_positions": {"x1": -5.0, "x2": 5.0, "y1": -5.0, "y2": 5.0},
# Thông tin MLC đơn giản cho mô phỏng
"mlc_positions": [
[-2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0],
[2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0],
],
}
],
}
return plan_data
def test_log_analyzer(
log_file_path: str,
plan_data: Dict[str, Any],
output_dir: str,
fast_mode: bool = False,
) -> Dict[str, Any]:
"""
Kiểm thử bộ phân tích log file với file đã cho.
Parameters
----------
log_file_path : str
Đường dẫn đến file log cần phân tích
plan_data : Dict[str, Any]
Dữ liệu kế hoạch xạ trị
output_dir : str
Thư mục đầu ra cho báo cáo và biểu đồ
fast_mode : bool, optional
Chế độ chạy nhanh (bỏ qua tạo biểu đồ), mặc định là False
Returns
-------
Dict[str, Any]
Kết quả phân tích
"""
# Thiết lập dung sai tùy chỉnh để dễ kiểm thử
custom_tolerance = {
"gantry_angle": {"minor": 0.2, "moderate": 0.5, "major": 1.0, "critical": 2.0},
"mlc_position": {"minor": 0.2, "moderate": 0.5, "major": 1.0, "critical": 2.0},
}
logger.info(f"Bắt đầu phân tích log file: {log_file_path}")
# Thực hiện phân tích
results = LogFileAnalyzer.analyze_log_file(
log_file_path=log_file_path,
plan_data=plan_data,
tolerance_levels=custom_tolerance,
)
# In kết quả tóm tắt
logger.info(f"Loại log: {results['log_type']}")
logger.info(f"Tỷ lệ đạt: {results['pass_rate']:.2f}%")
logger.info(f"Số lượng sai lệch: {len(results['deviations'])}")
if not fast_mode:
# Tạo báo cáo QA nếu không ở chế độ nhanh
logger.info("Tạo báo cáo QA...")
report_path = create_deviation_report(
{
"deviations": results["deviations"],
"summary": results["summary"],
"log_data": None, # Thực tế sẽ truyền vào DataFrame từ LogFileAnalyzer
},
output_dir=output_dir,
)
if report_path:
logger.info(f"Đã tạo báo cáo tại: {report_path}")
# Vẽ biểu đồ sai lệch cho một số loại sai lệch phổ biến
deviation_types = set(dev["type"] for dev in results["deviations"])
for dev_type in deviation_types:
logger.info(f"Vẽ biểu đồ sai lệch cho {dev_type}...")
analyzer = results["analyzer"]
fig = analyzer.plot_deviations(dev_type)
# Lưu biểu đồ
plot_path = os.path.join(output_dir, f"{dev_type}_deviations.png")
fig.savefig(plot_path, dpi=300, bbox_inches="tight")
plt.close(fig)
logger.info(f"Đã lưu biểu đồ tại: {plot_path}")
return results
def test_delivery_qa(
log_file_path: str, output_dir: str, fast_mode: bool = False
) -> None:
"""
Kiểm thử luồng làm việc đầy đủ của DeliveryQA.
Parameters
----------
log_file_path : str
Đường dẫn đến file log cần phân tích
output_dir : str
Thư mục đầu ra cho báo cáo và biểu đồ
fast_mode : bool, optional
Chế độ chạy nhanh (bỏ qua tạo biểu đồ), mặc định là False
"""
logger.info("Bắt đầu kiểm thử DeliveryQA...")
# Tạo đối tượng DeliveryQA
qa = DeliveryQA()
# Thiết lập dữ liệu kế hoạch
plan_data = create_test_plan_data()
qa.set_plan_data(plan_data)
# Thiết lập dung sai
custom_tolerance = {
"gantry_angle": {"minor": 0.2, "moderate": 0.5, "major": 1.0, "critical": 2.0},
"mlc_position": {"minor": 0.2, "moderate": 0.5, "major": 1.0, "critical": 2.0},
}
qa.set_tolerance_levels(custom_tolerance)
# Phân tích log file
logger.info(f"Phân tích log file: {log_file_path}")
qa.analyze_machine_logs(log_file_path)
if not fast_mode:
# Tạo báo cáo QA
logger.info("Tạo báo cáo QA...")
report_path = qa.generate_qa_report(output_dir)
if report_path:
logger.info(f"Đã tạo báo cáo tại: {report_path}")
# Vẽ biểu đồ sai lệch
logger.info("Tạo các biểu đồ sai lệch...")
plot_paths = qa.plot_all_deviations(output_dir)
for path in plot_paths:
logger.info(f"Đã tạo biểu đồ: {path}")
logger.info("Hoàn thành kiểm thử DeliveryQA.")
def main():
parser = argparse.ArgumentParser(
description="Kiểm thử phân tích log file máy điều trị"
)
parser.add_argument(
"--log-file",
help="Đường dẫn đến file log cần phân tích. Nếu không cung cấp, sẽ tạo file test.",
)
parser.add_argument(
"--log-type",
choices=[
"varian_trajectory",
"varian_dynalogs",
"elekta_integrity",
"elekta_iviewgt",
],
default="varian_trajectory",
help="Loại log file cần tạo nếu không cung cấp file log",
)
parser.add_argument(
"--output-dir",
default="./qa_test_results",
help="Thư mục đầu ra cho kết quả kiểm thử",
)
parser.add_argument(
"--test-mode",
choices=["analyzer", "delivery_qa", "both"],
default="both",
help="Chế độ kiểm thử: analyzer (chỉ machine_log_analyzer), delivery_qa (chỉ DeliveryQA), hoặc both (cả hai)",
)
parser.add_argument(
"--fast",
action="store_true",
help="Chế độ chạy nhanh, bỏ qua tạo biểu đồ và báo cáo",
)
args = parser.parse_args()
# Tạo thư mục đầu ra nếu chưa tồn tại
os.makedirs(args.output_dir, exist_ok=True)
# Xác định file log
log_file_path = args.log_file
if not log_file_path:
log_file_path = create_test_log_file(args.log_type, args.output_dir)
# Tạo dữ liệu kế hoạch
plan_data = create_test_plan_data()
# Chạy kiểm thử theo chế độ đã chọn
if args.test_mode in ["analyzer", "both"]:
logger.info("=== KIỂM THỬ MACHINE LOG ANALYZER ===")
test_log_analyzer(log_file_path, plan_data, args.output_dir, args.fast)
if args.test_mode in ["delivery_qa", "both"]:
logger.info("=== KIỂM THỬ DELIVERY QA ===")
test_delivery_qa(log_file_path, args.output_dir, args.fast)
logger.info("Hoàn thành kiểm thử!")
if __name__ == "__main__":
main()