Update app.py
Browse files
app.py
CHANGED
|
@@ -312,83 +312,89 @@ class DicomAnalyzer:
|
|
| 312 |
print(f"Error saving results: {str(e)}")
|
| 313 |
return None, f"Error saving results: {str(e)}"
|
| 314 |
|
| 315 |
-
def save_results_to_template(self, template_path, output_path):
|
| 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 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
if result_idx >= len(self.results):
|
| 355 |
-
break
|
| 356 |
-
|
| 357 |
-
if col_group_idx >= len(column_groups):
|
| 358 |
-
break
|
| 359 |
|
| 360 |
-
|
| 361 |
-
|
|
|
|
| 362 |
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
|
|
|
| 366 |
break
|
| 367 |
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
# Write each metric to corresponding column
|
| 371 |
-
for metric_idx, metric in enumerate(metrics):
|
| 372 |
-
cell = f"{cols[metric_idx]}{row}"
|
| 373 |
-
value = result.get(metric, '')
|
| 374 |
-
if value:
|
| 375 |
-
cell_obj = ws[cell]
|
| 376 |
-
if not (isinstance(cell_obj.value, str) and cell_obj.value.startswith('=')):
|
| 377 |
-
try:
|
| 378 |
-
ws[cell] = float(value)
|
| 379 |
-
except ValueError:
|
| 380 |
-
ws[cell] = value
|
| 381 |
-
|
| 382 |
-
col_group_idx += 1
|
| 383 |
-
result_idx += 2
|
| 384 |
-
|
| 385 |
-
# Save while preserving formulas
|
| 386 |
-
wb.save(output_path)
|
| 387 |
-
return output_path, "Results saved successfully in template format"
|
| 388 |
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 392 |
|
| 393 |
def add_blank_row(self, image):
|
| 394 |
self.results.append({
|
|
@@ -418,7 +424,6 @@ def save_results_to_template(self, template_path, output_path):
|
|
| 418 |
if self.marks:
|
| 419 |
self.marks.pop()
|
| 420 |
return self.update_display(), self.format_results()
|
| 421 |
-
|
| 422 |
def create_interface():
|
| 423 |
print("Creating interface...")
|
| 424 |
analyzer = DicomAnalyzer()
|
|
|
|
| 312 |
print(f"Error saving results: {str(e)}")
|
| 313 |
return None, f"Error saving results: {str(e)}"
|
| 314 |
|
| 315 |
+
def save_results_to_template(self, template_path, output_path):
|
| 316 |
+
try:
|
| 317 |
+
if not self.results:
|
| 318 |
+
return None, "No results to save"
|
| 319 |
+
|
| 320 |
+
# Load the template while preserving formulas
|
| 321 |
+
wb = openpyxl.load_workbook(template_path, data_only=False)
|
| 322 |
+
ws = wb.active
|
| 323 |
+
|
| 324 |
+
# Define the column groups for horizontal saving
|
| 325 |
+
column_groups = [
|
| 326 |
+
('B', 'C', 'D', 'E', 'F'), # First group
|
| 327 |
+
('H', 'I', 'J', 'K', 'L'), # Second group
|
| 328 |
+
('N', 'O', 'P', 'Q', 'R'), # Third group
|
| 329 |
+
('T', 'U', 'V', 'W', 'X'), # Fourth group
|
| 330 |
+
('Z', 'AA', 'AB', 'AC', 'AD'), # Fifth group
|
| 331 |
+
('AF', 'AG', 'AH', 'AI', 'AJ'), # Sixth group
|
| 332 |
+
('AL', 'AM', 'AN', 'AO', 'AP'), # Seventh group
|
| 333 |
+
('AR', 'AS', 'AT', 'AU', 'AV'), # Eighth group
|
| 334 |
+
('AX', 'AY', 'AZ', 'BA', 'BB'), # Ninth group
|
| 335 |
+
('BD', 'BE', 'BF', 'BG', 'BH'), # Tenth group
|
| 336 |
+
('BJ', 'BK', 'BL', 'BM', 'BN'), # Eleventh group
|
| 337 |
+
('BP', 'BQ', 'BR', 'BS', 'BT'), # Twelfth group
|
| 338 |
+
('BV', 'BW', 'BX', 'BY', 'BZ') # Thirteenth group
|
| 339 |
+
]
|
| 340 |
+
|
| 341 |
+
# Define row pairs
|
| 342 |
+
row_pairs = [
|
| 343 |
+
(41, 42), # 7mm
|
| 344 |
+
(67, 68), # 6.5mm
|
| 345 |
+
(93, 94), # 6mm
|
| 346 |
+
(119, 120), # 5.5mm
|
| 347 |
+
(145, 146), # 5mm
|
| 348 |
+
(171, 172), # 4.5mm
|
| 349 |
+
(197, 198), # 4mm
|
| 350 |
+
(223, 224), # 3.5mm
|
| 351 |
+
(249, 250), # 3mm
|
| 352 |
+
(275, 276) # 2.5mm
|
| 353 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 354 |
|
| 355 |
+
# Process results in pairs
|
| 356 |
+
result_idx = 0
|
| 357 |
+
metrics = ['Area (mm²)', 'Mean', 'StdDev', 'Min', 'Max']
|
| 358 |
|
| 359 |
+
for row_pair in row_pairs:
|
| 360 |
+
col_group_idx = 0
|
| 361 |
+
while result_idx < len(self.results):
|
| 362 |
+
if col_group_idx >= len(column_groups):
|
| 363 |
break
|
| 364 |
|
| 365 |
+
# Get current column group
|
| 366 |
+
cols = column_groups[col_group_idx]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 367 |
|
| 368 |
+
# Process pair of results
|
| 369 |
+
for row_idx, row in enumerate(row_pair):
|
| 370 |
+
if result_idx >= len(self.results):
|
| 371 |
+
break
|
| 372 |
+
|
| 373 |
+
result = self.results[result_idx]
|
| 374 |
+
|
| 375 |
+
# Write each metric to corresponding column
|
| 376 |
+
for metric_idx, metric in enumerate(metrics):
|
| 377 |
+
cell = f"{cols[metric_idx]}{row}"
|
| 378 |
+
value = result.get(metric, '')
|
| 379 |
+
if value:
|
| 380 |
+
cell_obj = ws[cell]
|
| 381 |
+
if not (isinstance(cell_obj.value, str) and cell_obj.value.startswith('=')):
|
| 382 |
+
try:
|
| 383 |
+
ws[cell] = float(value)
|
| 384 |
+
except ValueError:
|
| 385 |
+
ws[cell] = value
|
| 386 |
+
|
| 387 |
+
result_idx += 1
|
| 388 |
+
|
| 389 |
+
col_group_idx += 1
|
| 390 |
+
|
| 391 |
+
# Save while preserving formulas
|
| 392 |
+
wb.save(output_path)
|
| 393 |
+
return output_path, "Results saved successfully in template format"
|
| 394 |
+
|
| 395 |
+
except Exception as e:
|
| 396 |
+
print(f"Error saving results to template: {str(e)}")
|
| 397 |
+
return None, f"Error saving results to template: {str(e)}"
|
| 398 |
|
| 399 |
def add_blank_row(self, image):
|
| 400 |
self.results.append({
|
|
|
|
| 424 |
if self.marks:
|
| 425 |
self.marks.pop()
|
| 426 |
return self.update_display(), self.format_results()
|
|
|
|
| 427 |
def create_interface():
|
| 428 |
print("Creating interface...")
|
| 429 |
analyzer = DicomAnalyzer()
|