Update app.py
Browse files
app.py
CHANGED
|
@@ -312,68 +312,83 @@ class DicomAnalyzer:
|
|
| 312 |
print(f"Error saving results: {str(e)}")
|
| 313 |
return None, f"Error saving results: {str(e)}"
|
| 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 |
-
'Area (mm²)': "B",
|
| 341 |
-
'Mean': "C",
|
| 342 |
-
'StdDev': "D",
|
| 343 |
-
'Min': "E",
|
| 344 |
-
'Max': "F"
|
| 345 |
-
}
|
| 346 |
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
if result_idx >= len(self.results):
|
| 351 |
break
|
| 352 |
|
| 353 |
-
result = self.results[result_idx]
|
| 354 |
-
|
| 355 |
-
#
|
| 356 |
-
for
|
| 357 |
-
cell = f"{
|
| 358 |
value = result.get(metric, '')
|
| 359 |
if value:
|
| 360 |
cell_obj = ws[cell]
|
| 361 |
-
# Check if cell contains formula
|
| 362 |
if not (isinstance(cell_obj.value, str) and cell_obj.value.startswith('=')):
|
| 363 |
try:
|
| 364 |
ws[cell] = float(value)
|
| 365 |
except ValueError:
|
| 366 |
ws[cell] = value
|
| 367 |
|
| 368 |
-
|
|
|
|
| 369 |
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
|
| 378 |
def add_blank_row(self, image):
|
| 379 |
self.results.append({
|
|
|
|
| 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 |
+
# Add more groups as needed
|
| 331 |
+
]
|
| 332 |
+
|
| 333 |
+
# Define row pairs
|
| 334 |
+
row_pairs = [
|
| 335 |
+
(41, 42), # 7mm
|
| 336 |
+
(67, 68), # 6.5mm
|
| 337 |
+
(93, 94), # 6mm
|
| 338 |
+
(119, 120), # 5.5mm
|
| 339 |
+
(145, 146), # 5mm
|
| 340 |
+
(171, 172), # 4.5mm
|
| 341 |
+
(197, 198), # 4mm
|
| 342 |
+
(223, 224), # 3.5mm
|
| 343 |
+
(249, 250), # 3mm
|
| 344 |
+
(275, 276) # 2.5mm
|
| 345 |
+
]
|
| 346 |
+
|
| 347 |
+
# Process results in pairs
|
| 348 |
+
result_idx = 0
|
| 349 |
+
metrics = ['Area (mm²)', 'Mean', 'StdDev', 'Min', 'Max']
|
| 350 |
+
|
| 351 |
+
for row_pair in row_pairs:
|
| 352 |
+
col_group_idx = 0
|
| 353 |
+
for i in range(0, min(len(self.results), 2), 2):
|
| 354 |
+
if result_idx >= len(self.results):
|
| 355 |
+
break
|
| 356 |
+
|
| 357 |
+
if col_group_idx >= len(column_groups):
|
| 358 |
+
break
|
| 359 |
|
| 360 |
+
# Get current column group
|
| 361 |
+
cols = column_groups[col_group_idx]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 362 |
|
| 363 |
+
# Process pair of results
|
| 364 |
+
for row_idx, row in enumerate(row_pair):
|
| 365 |
+
if result_idx + row_idx >= len(self.results):
|
|
|
|
| 366 |
break
|
| 367 |
|
| 368 |
+
result = self.results[result_idx + row_idx]
|
| 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 |
+
except Exception as e:
|
| 390 |
+
print(f"Error saving results to template: {str(e)}")
|
| 391 |
+
return None, f"Error saving results to template: {str(e)}"
|
| 392 |
|
| 393 |
def add_blank_row(self, image):
|
| 394 |
self.results.append({
|