Update app.py
Browse files
app.py
CHANGED
|
@@ -295,97 +295,98 @@ class DicomAnalyzer:
|
|
| 295 |
df = df[columns_order]
|
| 296 |
return df.to_string(index=False)
|
| 297 |
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 306 |
|
| 307 |
-
|
| 308 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 309 |
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 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 |
-
]
|
| 331 |
-
|
| 332 |
-
# Define row pairs
|
| 333 |
-
row_pairs = [
|
| 334 |
-
(2, 3), # 7mm
|
| 335 |
-
(5, 6), # 6.5mm
|
| 336 |
-
(8, 9), # 6mm
|
| 337 |
-
(11, 12), # 5.5mm
|
| 338 |
-
(14, 15), # 5mm
|
| 339 |
-
(17, 18), # 4.5mm
|
| 340 |
-
(20, 21), # 4mm
|
| 341 |
-
(23, 24), # 3.5mm
|
| 342 |
-
(26, 27), # 3mm
|
| 343 |
-
(29, 30) # 2.5mm
|
| 344 |
-
]
|
| 345 |
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
for row_pair in row_pairs:
|
| 351 |
-
col_group_idx = 0
|
| 352 |
-
while result_idx < len(self.results):
|
| 353 |
-
if col_group_idx >= len(column_groups):
|
| 354 |
-
break
|
| 355 |
-
|
| 356 |
-
# Get current column group
|
| 357 |
-
cols = column_groups[col_group_idx]
|
| 358 |
-
|
| 359 |
-
# Process pair of results
|
| 360 |
-
for row_idx, row in enumerate(row_pair):
|
| 361 |
-
if result_idx >= len(self.results):
|
| 362 |
-
break
|
| 363 |
-
|
| 364 |
-
result = self.results[result_idx]
|
| 365 |
-
|
| 366 |
-
# Write each metric to corresponding column
|
| 367 |
-
for metric_idx, metric in enumerate(metrics):
|
| 368 |
-
cell = f"{cols[metric_idx]}{row}"
|
| 369 |
-
value = result.get(metric, '')
|
| 370 |
-
if value:
|
| 371 |
-
cell_obj = ws[cell]
|
| 372 |
-
if not (isinstance(cell_obj.value, str) and cell_obj.value.startswith('=')):
|
| 373 |
-
try:
|
| 374 |
-
ws[cell] = float(value)
|
| 375 |
-
except ValueError:
|
| 376 |
-
ws[cell] = value
|
| 377 |
-
|
| 378 |
-
result_idx += 1
|
| 379 |
-
|
| 380 |
-
col_group_idx += 1
|
| 381 |
-
|
| 382 |
-
# Save while preserving formulas
|
| 383 |
-
wb.save(output_path)
|
| 384 |
-
return output_path, "Results saved successfully in template format"
|
| 385 |
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
|
|
|
|
|
|
| 389 |
|
| 390 |
def add_blank_row(self, image):
|
| 391 |
self.results.append({
|
|
|
|
| 295 |
df = df[columns_order]
|
| 296 |
return df.to_string(index=False)
|
| 297 |
|
| 298 |
+
def save_results_to_template(self, template_path, output_path):
|
| 299 |
+
try:
|
| 300 |
+
if not self.results:
|
| 301 |
+
return None, "No results to save"
|
| 302 |
+
|
| 303 |
+
# Load the template while preserving formulas
|
| 304 |
+
wb = openpyxl.load_workbook(template_path, data_only=False)
|
| 305 |
+
ws = wb.active
|
| 306 |
+
|
| 307 |
+
# Define the column groups for horizontal saving
|
| 308 |
+
column_groups = [
|
| 309 |
+
('B', 'C', 'D', 'E', 'F'), # First group
|
| 310 |
+
('H', 'I', 'J', 'K', 'L'), # Second group
|
| 311 |
+
('N', 'O', 'P', 'Q', 'R'), # Third group
|
| 312 |
+
('T', 'U', 'V', 'W', 'X'), # Fourth group
|
| 313 |
+
]
|
| 314 |
+
|
| 315 |
+
# Define row pairs
|
| 316 |
+
row_pairs = [
|
| 317 |
+
(2, 3), # 7mm
|
| 318 |
+
(5, 6), # 6.5mm
|
| 319 |
+
(8, 9), # 6mm
|
| 320 |
+
(11, 12), # 5.5mm
|
| 321 |
+
(14, 15), # 5mm
|
| 322 |
+
(17, 18), # 4.5mm
|
| 323 |
+
(20, 21), # 4mm
|
| 324 |
+
(23, 24), # 3.5mm
|
| 325 |
+
(26, 27), # 3mm
|
| 326 |
+
(29, 30) # 2.5mm
|
| 327 |
+
]
|
| 328 |
+
|
| 329 |
+
# Process results
|
| 330 |
+
result_idx = 0
|
| 331 |
+
current_col_group = 0
|
| 332 |
+
current_row_pair = 0
|
| 333 |
+
|
| 334 |
+
while result_idx < len(self.results):
|
| 335 |
+
if current_col_group >= len(column_groups) or current_row_pair >= len(row_pairs):
|
| 336 |
+
break
|
| 337 |
+
|
| 338 |
+
# Get current positions
|
| 339 |
+
cols = column_groups[current_col_group]
|
| 340 |
+
rows = row_pairs[current_row_pair]
|
| 341 |
|
| 342 |
+
# Write first row if available
|
| 343 |
+
if result_idx < len(self.results):
|
| 344 |
+
result = self.results[result_idx]
|
| 345 |
+
# Write Area
|
| 346 |
+
ws[f"{cols[0]}{rows[0]}"] = float(result['Area (mm²)'])
|
| 347 |
+
# Write Mean
|
| 348 |
+
ws[f"{cols[1]}{rows[0]}"] = float(result['Mean'])
|
| 349 |
+
# Write StdDev
|
| 350 |
+
ws[f"{cols[2]}{rows[0]}"] = float(result['StdDev'])
|
| 351 |
+
# Write Min
|
| 352 |
+
ws[f"{cols[3]}{rows[0]}"] = float(result['Min'])
|
| 353 |
+
# Write Max
|
| 354 |
+
ws[f"{cols[4]}{rows[0]}"] = float(result['Max'])
|
| 355 |
+
result_idx += 1
|
| 356 |
|
| 357 |
+
# Write second row if available
|
| 358 |
+
if result_idx < len(self.results):
|
| 359 |
+
result = self.results[result_idx]
|
| 360 |
+
# Write Area
|
| 361 |
+
ws[f"{cols[0]}{rows[1]}"] = float(result['Area (mm²)'])
|
| 362 |
+
# Write Mean
|
| 363 |
+
ws[f"{cols[1]}{rows[1]}"] = float(result['Mean'])
|
| 364 |
+
# Write StdDev
|
| 365 |
+
ws[f"{cols[2]}{rows[1]}"] = float(result['StdDev'])
|
| 366 |
+
# Write Min
|
| 367 |
+
ws[f"{cols[3]}{rows[1]}"] = float(result['Min'])
|
| 368 |
+
# Write Max
|
| 369 |
+
ws[f"{cols[4]}{rows[1]}"] = float(result['Max'])
|
| 370 |
+
result_idx += 1
|
| 371 |
|
| 372 |
+
# Move to next column group if we've written both rows
|
| 373 |
+
current_col_group += 1
|
| 374 |
+
|
| 375 |
+
# If we've used all column groups, move to next row pair
|
| 376 |
+
if current_col_group >= len(column_groups):
|
| 377 |
+
current_col_group = 0
|
| 378 |
+
current_row_pair += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 379 |
|
| 380 |
+
# Save while preserving formulas
|
| 381 |
+
wb.save(output_path)
|
| 382 |
+
print(f"Saved {result_idx} results to template")
|
| 383 |
+
return output_path, f"Results saved successfully in template format ({result_idx} measurements)"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
|
| 385 |
+
except Exception as e:
|
| 386 |
+
print(f"Error saving results to template: {str(e)}")
|
| 387 |
+
logger.error(f"Error saving results to template: {str(e)}")
|
| 388 |
+
logger.error(traceback.format_exc())
|
| 389 |
+
return None, f"Error saving results to template: {str(e)}"
|
| 390 |
|
| 391 |
def add_blank_row(self, image):
|
| 392 |
self.results.append({
|