HeshamAI commited on
Commit
1929f10
·
verified ·
1 Parent(s): e26e310

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -106
app.py CHANGED
@@ -338,118 +338,118 @@ class DicomAnalyzer:
338
  logger.error(traceback.format_exc())
339
  return None, error_msg
340
 
341
- def add_formulas_to_template(self, ws, row_pair, col_group, red_font):
342
- """Add formulas for each row pair and column group"""
343
- try:
344
- # Get base column for formulas
345
- base_col = col_group[1] # Mean column
346
- std_col = col_group[2] # StdDev column
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
347
 
348
- row1, row2 = row_pair
 
 
 
 
349
 
350
- # Formula for first row: =Mean/StdDev
351
- formula1 = f"={base_col}{row1}/{std_col}{row1}"
352
- formula_col = get_column_letter(column_index_from_string(col_group[-1]) + 1)
353
- ws[f"{formula_col}{row1}"] = formula1
354
- cell1.value = formula1
355
- cell1.font = red_font
356
 
357
- # Formula for second row: =(Mean1-Mean2)/StdDev2
358
- formula2 = f"=({base_col}{row1}-{base_col}{row2})/{std_col}{row2}"
359
- ws[f"{formula_col}{row2}"] = formula2
360
- cell2.value = formula2
361
- cell2.font = red_font
362
 
363
- logger.debug(f"Added formulas for rows {row1},{row2} in column {formula_col}")
364
- except Exception as e:
365
- logger.error(f"Error adding formulas: {str(e)}")
 
366
 
367
- def save_formatted_results(self, output_path):
368
- try:
369
- if not self.results:
370
- return None, "No results to save"
371
-
372
- # Create new workbook
373
- wb = openpyxl.Workbook()
374
- ws = wb.active
375
-
376
- # Create red font style
377
- red_font = openpyxl.styles.Font(color="FF0000") # Red color in RGB
378
-
379
- # Define column groups and row pairs
380
- column_groups = [
381
- ('B', 'C', 'D', 'E', 'F'), # First group
382
- ('H', 'I', 'J', 'K', 'L'), # Second group
383
- ('N', 'O', 'P', 'Q', 'R'), # Third group
384
- ('T', 'U', 'V', 'W', 'X'), # Fourth group
385
- ('Z', 'AA', 'AB', 'AC', 'AD'), # Fifth group
386
- ('AF', 'AG', 'AH', 'AI', 'AJ'), # Sixth group
387
- ('AL', 'AM', 'AN', 'AO', 'AP'), # Seventh group
388
- ('AR', 'AS', 'AT', 'AU', 'AV'), # Eighth group
389
- ('AX', 'AY', 'AZ', 'BA', 'BB'), # Ninth group
390
- ('BD', 'BE', 'BF', 'BG', 'BH'), # Tenth group
391
- ('BJ', 'BK', 'BL', 'BM', 'BN'), # Eleventh group
392
- ('BP', 'BQ', 'BR', 'BS', 'BT'), # Twelfth group
393
- ('BV', 'BW', 'BX', 'BY', 'BZ') # Thirteenth group
394
- ]
395
-
396
- row_pairs = [
397
- (2, 3), # 7mm
398
- (5, 6), # 6.5mm
399
- (8, 9), # 6mm
400
- (11, 12), # 5.5mm
401
- (14, 15), # 5mm
402
- (17, 18), # 4.5mm
403
- (20, 21), # 4mm
404
- (23, 24), # 3.5mm
405
- (26, 27), # 3mm
406
- (29, 30) # 2.5mm
407
- ]
408
-
409
- # Add headers
410
- phantom_sizes = ['(7mm)', '(6.5mm)', '(6mm)', '(5.5mm)', '(5mm)',
411
- '(4.5mm)', '(4mm)', '(3.5mm)', '(3mm)', '(2.5mm)']
412
- for i, size in enumerate(phantom_sizes):
413
- header_cell = ws.cell(row=row_pairs[i][0]-1, column=1, value=size)
414
- header_cell.font = red_font
415
-
416
- # Process results
417
- result_idx = 0
418
- current_col_group = 0
419
- current_row_pair = 0
420
-
421
- while result_idx < len(self.results):
422
- if current_row_pair >= len(row_pairs):
423
- break
424
-
425
- cols = column_groups[current_col_group]
426
- rows = row_pairs[current_row_pair]
427
-
428
- # Write data and add formulas
429
- if result_idx < len(self.results):
430
- result = self.results[result_idx]
431
- self._write_result_to_cells(ws, result, cols, rows[0])
432
- result_idx += 1
433
-
434
- if result_idx < len(self.results):
435
- result = self.results[result_idx]
436
- self._write_result_to_cells(ws, result, cols, rows[1])
437
- result_idx += 1
438
-
439
- # Add formulas for this group
440
- self.add_formulas_to_template(ws, rows, cols, red_font)
441
-
442
- current_col_group += 1
443
- if current_col_group >= len(column_groups):
444
- current_col_group = 0
445
- current_row_pair += 1
446
 
447
- wb.save(output_path)
448
- return output_path, f"Results saved successfully ({result_idx} measurements)"
449
-
450
- except Exception as e:
451
- logger.error(f"Error saving formatted results: {str(e)}")
452
- return None, f"Error saving results: {str(e)}"
453
 
454
  def _write_result_to_cells(self, ws, result, cols, row):
455
  """Helper method to write a single result to worksheet cells"""
 
338
  logger.error(traceback.format_exc())
339
  return None, error_msg
340
 
341
+ def add_formulas_to_template(self, ws, row_pair, col_group, red_font):
342
+ """Add formulas for each row pair and column group"""
343
+ try:
344
+ # Get base column for formulas
345
+ base_col = col_group[1] # Mean column
346
+ std_col = col_group[2] # StdDev column
347
+
348
+ row1, row2 = row_pair
349
+
350
+ # Formula for first row: =Mean/StdDev
351
+ formula1 = f"={base_col}{row1}/{std_col}{row1}"
352
+ formula_col = get_column_letter(column_index_from_string(col_group[-1]) + 1)
353
+ cell1 = ws[f"{formula_col}{row1}"] # Get cell reference first
354
+ cell1.value = formula1
355
+ cell1.font = red_font
356
+
357
+ # Formula for second row: =(Mean1-Mean2)/StdDev2
358
+ formula2 = f"=({base_col}{row1}-{base_col}{row2})/{std_col}{row2}"
359
+ cell2 = ws[f"{formula_col}{row2}"] # Get cell reference first
360
+ cell2.value = formula2
361
+ cell2.font = red_font
362
+
363
+ logger.debug(f"Added formulas for rows {row1},{row2} in column {formula_col}")
364
+ except Exception as e:
365
+ logger.error(f"Error adding formulas: {str(e)}")
366
+
367
+ def save_formatted_results(self, output_path):
368
+ try:
369
+ if not self.results:
370
+ return None, "No results to save"
371
+
372
+ # Create new workbook
373
+ wb = openpyxl.Workbook()
374
+ ws = wb.active
375
+
376
+ # Create font styles
377
+ red_font = openpyxl.styles.Font(color="FF0000") # Red color in RGB
378
+
379
+ # Define column groups and row pairs
380
+ column_groups = [
381
+ ('B', 'C', 'D', 'E', 'F'), # First group
382
+ ('H', 'I', 'J', 'K', 'L'), # Second group
383
+ ('N', 'O', 'P', 'Q', 'R'), # Third group
384
+ ('T', 'U', 'V', 'W', 'X'), # Fourth group
385
+ ('Z', 'AA', 'AB', 'AC', 'AD'), # Fifth group
386
+ ('AF', 'AG', 'AH', 'AI', 'AJ'), # Sixth group
387
+ ('AL', 'AM', 'AN', 'AO', 'AP'), # Seventh group
388
+ ('AR', 'AS', 'AT', 'AU', 'AV'), # Eighth group
389
+ ('AX', 'AY', 'AZ', 'BA', 'BB'), # Ninth group
390
+ ('BD', 'BE', 'BF', 'BG', 'BH'), # Tenth group
391
+ ('BJ', 'BK', 'BL', 'BM', 'BN'), # Eleventh group
392
+ ('BP', 'BQ', 'BR', 'BS', 'BT'), # Twelfth group
393
+ ('BV', 'BW', 'BX', 'BY', 'BZ') # Thirteenth group
394
+ ]
395
+
396
+ row_pairs = [
397
+ (2, 3), # 7mm
398
+ (5, 6), # 6.5mm
399
+ (8, 9), # 6mm
400
+ (11, 12), # 5.5mm
401
+ (14, 15), # 5mm
402
+ (17, 18), # 4.5mm
403
+ (20, 21), # 4mm
404
+ (23, 24), # 3.5mm
405
+ (26, 27), # 3mm
406
+ (29, 30) # 2.5mm
407
+ ]
408
+
409
+ # Add headers with red font
410
+ phantom_sizes = ['(7mm)', '(6.5mm)', '(6mm)', '(5.5mm)', '(5mm)',
411
+ '(4.5mm)', '(4mm)', '(3.5mm)', '(3mm)', '(2.5mm)']
412
+ for i, size in enumerate(phantom_sizes):
413
+ header_cell = ws.cell(row=row_pairs[i][0]-1, column=1, value=size)
414
+ header_cell.font = red_font
415
+
416
+ # Process results
417
+ result_idx = 0
418
+ current_col_group = 0
419
+ current_row_pair = 0
420
+
421
+ while result_idx < len(self.results):
422
+ if current_row_pair >= len(row_pairs):
423
+ break
424
+
425
+ cols = column_groups[current_col_group]
426
+ rows = row_pairs[current_row_pair]
427
 
428
+ # Write data and add formulas
429
+ if result_idx < len(self.results):
430
+ result = self.results[result_idx]
431
+ self._write_result_to_cells(ws, result, cols, rows[0])
432
+ result_idx += 1
433
 
434
+ if result_idx < len(self.results):
435
+ result = self.results[result_idx]
436
+ self._write_result_to_cells(ws, result, cols, rows[1])
437
+ result_idx += 1
 
 
438
 
439
+ # Add formulas for this group
440
+ self.add_formulas_to_template(ws, rows, cols, red_font)
 
 
 
441
 
442
+ current_col_group += 1
443
+ if current_col_group >= len(column_groups):
444
+ current_col_group = 0
445
+ current_row_pair += 1
446
 
447
+ wb.save(output_path)
448
+ return output_path, f"Results saved successfully ({result_idx} measurements)"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
449
 
450
+ except Exception as e:
451
+ logger.error(f"Error saving formatted results: {str(e)}")
452
+ return None, f"Error saving results: {str(e)}"
 
 
 
453
 
454
  def _write_result_to_cells(self, ws, result, cols, row):
455
  """Helper method to write a single result to worksheet cells"""