Update app.py
Browse files
app.py
CHANGED
|
@@ -106,6 +106,7 @@ class DicomAnalyzer:
|
|
| 106 |
except Exception as e:
|
| 107 |
print(f"Error loading DICOM file: {str(e)}")
|
| 108 |
return None, f"Error loading DICOM file: {str(e)}"
|
|
|
|
| 109 |
def normalize_image(self, image):
|
| 110 |
try:
|
| 111 |
normalized = cv2.normalize(
|
|
@@ -294,6 +295,49 @@ class DicomAnalyzer:
|
|
| 294 |
print(f"Error updating display: {str(e)}")
|
| 295 |
return self.original_display
|
| 296 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
def add_formulas_to_template(self, ws, row_pair, col_group):
|
| 298 |
"""Add formulas for each row pair and column group"""
|
| 299 |
try:
|
|
@@ -443,6 +487,7 @@ class DicomAnalyzer:
|
|
| 443 |
if self.marks:
|
| 444 |
self.marks.pop()
|
| 445 |
return self.update_display(), self.format_results()
|
|
|
|
| 446 |
def create_interface():
|
| 447 |
print("Creating interface...")
|
| 448 |
analyzer = DicomAnalyzer()
|
|
@@ -465,7 +510,7 @@ def create_interface():
|
|
| 465 |
zoom_in_btn = gr.Button("Zoom In (+)")
|
| 466 |
zoom_out_btn = gr.Button("Zoom Out (-)")
|
| 467 |
reset_btn = gr.Button("Reset View")
|
| 468 |
-
reset_all_btn = gr.Button("Reset All")
|
| 469 |
|
| 470 |
with gr.Column():
|
| 471 |
image_display = gr.Image(
|
|
@@ -479,7 +524,7 @@ def create_interface():
|
|
| 479 |
zero_btn = gr.Button("Add Zero Row")
|
| 480 |
undo_btn = gr.Button("Undo Last")
|
| 481 |
save_btn = gr.Button("Save Results")
|
| 482 |
-
save_formatted_btn = gr.Button("Save Formatted Results")
|
| 483 |
|
| 484 |
results_display = gr.Textbox(label="Results", interactive=False)
|
| 485 |
file_output = gr.File(label="Download Results")
|
|
@@ -540,7 +585,6 @@ def create_interface():
|
|
| 540 |
outputs=image_display
|
| 541 |
)
|
| 542 |
|
| 543 |
-
# New reset all button handler
|
| 544 |
reset_all_btn.click(
|
| 545 |
fn=analyzer.reset_all,
|
| 546 |
inputs=image_display,
|
|
|
|
| 106 |
except Exception as e:
|
| 107 |
print(f"Error loading DICOM file: {str(e)}")
|
| 108 |
return None, f"Error loading DICOM file: {str(e)}"
|
| 109 |
+
|
| 110 |
def normalize_image(self, image):
|
| 111 |
try:
|
| 112 |
normalized = cv2.normalize(
|
|
|
|
| 295 |
print(f"Error updating display: {str(e)}")
|
| 296 |
return self.original_display
|
| 297 |
|
| 298 |
+
def save_results(self):
|
| 299 |
+
"""
|
| 300 |
+
Basic save function for raw results with improved error handling and logging
|
| 301 |
+
|
| 302 |
+
Returns:
|
| 303 |
+
tuple: (str, str) - (filepath, message) or (None, error_message)
|
| 304 |
+
"""
|
| 305 |
+
try:
|
| 306 |
+
if not self.results:
|
| 307 |
+
logger.warning("Attempted to save with no results")
|
| 308 |
+
return None, "No results to save"
|
| 309 |
+
|
| 310 |
+
# Create DataFrame with ordered columns
|
| 311 |
+
df = pd.DataFrame(self.results)
|
| 312 |
+
columns_order = ['Area (mm²)', 'Mean', 'StdDev', 'Min', 'Max', 'Point']
|
| 313 |
+
df = df[columns_order]
|
| 314 |
+
|
| 315 |
+
# Generate timestamp for unique filename
|
| 316 |
+
timestamp = time.strftime("%Y%m%d_%H%M%S")
|
| 317 |
+
output_file = f"analysis_results_{timestamp}.xlsx"
|
| 318 |
+
|
| 319 |
+
# Save with Excel writer to properly format columns
|
| 320 |
+
with pd.ExcelWriter(output_file, engine='openpyxl') as writer:
|
| 321 |
+
df.to_excel(writer, index=False, sheet_name='Results')
|
| 322 |
+
|
| 323 |
+
# Auto-adjust column widths
|
| 324 |
+
worksheet = writer.sheets['Results']
|
| 325 |
+
for idx, col in enumerate(df.columns):
|
| 326 |
+
max_length = max(
|
| 327 |
+
df[col].astype(str).apply(len).max(),
|
| 328 |
+
len(str(col))
|
| 329 |
+
) + 2
|
| 330 |
+
worksheet.column_dimensions[get_column_letter(idx + 1)].width = max_length
|
| 331 |
+
|
| 332 |
+
logger.info(f"Results saved successfully to {output_file}")
|
| 333 |
+
return output_file, f"Results saved successfully to {output_file}"
|
| 334 |
+
|
| 335 |
+
except Exception as e:
|
| 336 |
+
error_msg = f"Error saving results: {str(e)}"
|
| 337 |
+
logger.error(error_msg)
|
| 338 |
+
logger.error(traceback.format_exc())
|
| 339 |
+
return None, error_msg
|
| 340 |
+
|
| 341 |
def add_formulas_to_template(self, ws, row_pair, col_group):
|
| 342 |
"""Add formulas for each row pair and column group"""
|
| 343 |
try:
|
|
|
|
| 487 |
if self.marks:
|
| 488 |
self.marks.pop()
|
| 489 |
return self.update_display(), self.format_results()
|
| 490 |
+
|
| 491 |
def create_interface():
|
| 492 |
print("Creating interface...")
|
| 493 |
analyzer = DicomAnalyzer()
|
|
|
|
| 510 |
zoom_in_btn = gr.Button("Zoom In (+)")
|
| 511 |
zoom_out_btn = gr.Button("Zoom Out (-)")
|
| 512 |
reset_btn = gr.Button("Reset View")
|
| 513 |
+
reset_all_btn = gr.Button("Reset All")
|
| 514 |
|
| 515 |
with gr.Column():
|
| 516 |
image_display = gr.Image(
|
|
|
|
| 524 |
zero_btn = gr.Button("Add Zero Row")
|
| 525 |
undo_btn = gr.Button("Undo Last")
|
| 526 |
save_btn = gr.Button("Save Results")
|
| 527 |
+
save_formatted_btn = gr.Button("Save Formatted Results")
|
| 528 |
|
| 529 |
results_display = gr.Textbox(label="Results", interactive=False)
|
| 530 |
file_output = gr.File(label="Download Results")
|
|
|
|
| 585 |
outputs=image_display
|
| 586 |
)
|
| 587 |
|
|
|
|
| 588 |
reset_all_btn.click(
|
| 589 |
fn=analyzer.reset_all,
|
| 590 |
inputs=image_display,
|