HeshamAI commited on
Commit
47a4b5f
·
verified ·
1 Parent(s): 25ab708

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -47
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
- 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 data rows (excluding formula rows)
325
- data_rows = {
326
- "7mm": [41, 42],
327
- "6.5mm": [67, 68],
328
- "6mm": [93, 94],
329
- "5.5mm": [119, 120],
330
- "5mm": [145, 146],
331
- "4.5mm": [171, 172],
332
- "4mm": [197, 198],
333
- "3.5mm": [223, 224],
334
- "3mm": [249, 250],
335
- "2.5mm": [275, 276]
336
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
337
 
338
- # Define measurement columns
339
- columns = {
340
- 'Area (mm²)': "B",
341
- 'Mean': "C",
342
- 'StdDev': "D",
343
- 'Min': "E",
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)}")
376
- return None, f"Error saving results to template: {str(e)}"
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({