Update app.py
Browse files
app.py
CHANGED
|
@@ -161,7 +161,6 @@ class DicomAnalyzer:
|
|
| 161 |
except Exception as e:
|
| 162 |
print(f"Error handling keyboard input: {str(e)}")
|
| 163 |
return self.display_image
|
| 164 |
-
|
| 165 |
def analyze_roi(self, evt: gr.SelectData):
|
| 166 |
try:
|
| 167 |
if self.current_image is None:
|
|
@@ -232,6 +231,7 @@ class DicomAnalyzer:
|
|
| 232 |
except Exception as e:
|
| 233 |
print(f"Error analyzing ROI: {str(e)}")
|
| 234 |
return self.display_image, f"Error analyzing ROI: {str(e)}"
|
|
|
|
| 235 |
def update_display(self):
|
| 236 |
try:
|
| 237 |
if self.original_display is None:
|
|
@@ -317,12 +317,12 @@ class DicomAnalyzer:
|
|
| 317 |
if not self.results:
|
| 318 |
return None, "No results to save"
|
| 319 |
|
| 320 |
-
# Load the
|
| 321 |
-
wb = openpyxl.load_workbook(template_path)
|
| 322 |
ws = wb.active
|
| 323 |
|
| 324 |
-
# Define
|
| 325 |
-
|
| 326 |
"7mm": [41, 42],
|
| 327 |
"6.5mm": [67, 68],
|
| 328 |
"6mm": [93, 94],
|
|
@@ -335,7 +335,7 @@ class DicomAnalyzer:
|
|
| 335 |
"2.5mm": [275, 276]
|
| 336 |
}
|
| 337 |
|
| 338 |
-
# Define columns
|
| 339 |
columns = {
|
| 340 |
'Area (mm²)': "B",
|
| 341 |
'Mean': "C",
|
|
@@ -344,32 +344,32 @@ class DicomAnalyzer:
|
|
| 344 |
'Max': "F"
|
| 345 |
}
|
| 346 |
|
| 347 |
-
# Map results to phantom sizes (assuming results are ordered for each size)
|
| 348 |
result_idx = 0
|
| 349 |
-
|
| 350 |
-
for size, rows in row_groups.items():
|
| 351 |
for row in rows:
|
| 352 |
if result_idx >= len(self.results):
|
| 353 |
break
|
| 354 |
|
| 355 |
result = self.results[result_idx]
|
| 356 |
|
| 357 |
-
#
|
| 358 |
for metric, col in columns.items():
|
| 359 |
cell = f"{col}{row}"
|
| 360 |
value = result.get(metric, '')
|
| 361 |
if value:
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
|
|
|
|
|
|
|
|
|
| 366 |
|
| 367 |
result_idx += 1
|
| 368 |
|
| 369 |
-
# Save
|
| 370 |
wb.save(output_path)
|
| 371 |
-
|
| 372 |
-
return output_path, "Results saved successfully in the template format"
|
| 373 |
|
| 374 |
except Exception as e:
|
| 375 |
print(f"Error saving results to template: {str(e)}")
|
|
@@ -562,11 +562,14 @@ if __name__ == "__main__":
|
|
| 562 |
print("Starting application...")
|
| 563 |
interface = create_interface()
|
| 564 |
print("Launching interface...")
|
|
|
|
| 565 |
interface.launch(
|
| 566 |
server_name="0.0.0.0",
|
| 567 |
server_port=7860,
|
| 568 |
share=True,
|
| 569 |
-
debug=True
|
|
|
|
|
|
|
| 570 |
)
|
| 571 |
except Exception as e:
|
| 572 |
print(f"Error launching application: {str(e)}")
|
|
|
|
| 161 |
except Exception as e:
|
| 162 |
print(f"Error handling keyboard input: {str(e)}")
|
| 163 |
return self.display_image
|
|
|
|
| 164 |
def analyze_roi(self, evt: gr.SelectData):
|
| 165 |
try:
|
| 166 |
if self.current_image is None:
|
|
|
|
| 231 |
except Exception as e:
|
| 232 |
print(f"Error analyzing ROI: {str(e)}")
|
| 233 |
return self.display_image, f"Error analyzing ROI: {str(e)}"
|
| 234 |
+
|
| 235 |
def update_display(self):
|
| 236 |
try:
|
| 237 |
if self.original_display is None:
|
|
|
|
| 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 data rows (excluding formula rows)
|
| 325 |
+
data_rows = {
|
| 326 |
"7mm": [41, 42],
|
| 327 |
"6.5mm": [67, 68],
|
| 328 |
"6mm": [93, 94],
|
|
|
|
| 335 |
"2.5mm": [275, 276]
|
| 336 |
}
|
| 337 |
|
| 338 |
+
# Define measurement columns
|
| 339 |
columns = {
|
| 340 |
'Area (mm²)': "B",
|
| 341 |
'Mean': "C",
|
|
|
|
| 344 |
'Max': "F"
|
| 345 |
}
|
| 346 |
|
|
|
|
| 347 |
result_idx = 0
|
| 348 |
+
for size, rows in data_rows.items():
|
|
|
|
| 349 |
for row in rows:
|
| 350 |
if result_idx >= len(self.results):
|
| 351 |
break
|
| 352 |
|
| 353 |
result = self.results[result_idx]
|
| 354 |
|
| 355 |
+
# Update only measurement cells, skip formula cells
|
| 356 |
for metric, col in columns.items():
|
| 357 |
cell = f"{col}{row}"
|
| 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 |
result_idx += 1
|
| 369 |
|
| 370 |
+
# Save while preserving formulas
|
| 371 |
wb.save(output_path)
|
| 372 |
+
return output_path, "Results saved successfully in template format"
|
|
|
|
| 373 |
|
| 374 |
except Exception as e:
|
| 375 |
print(f"Error saving results to template: {str(e)}")
|
|
|
|
| 562 |
print("Starting application...")
|
| 563 |
interface = create_interface()
|
| 564 |
print("Launching interface...")
|
| 565 |
+
interface.queue()
|
| 566 |
interface.launch(
|
| 567 |
server_name="0.0.0.0",
|
| 568 |
server_port=7860,
|
| 569 |
share=True,
|
| 570 |
+
debug=True,
|
| 571 |
+
show_error=True,
|
| 572 |
+
quiet=False
|
| 573 |
)
|
| 574 |
except Exception as e:
|
| 575 |
print(f"Error launching application: {str(e)}")
|