HeshamAI commited on
Commit
ed500bb
·
verified ·
1 Parent(s): 118390a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -86
app.py CHANGED
@@ -295,98 +295,115 @@ class DicomAnalyzer:
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({
 
295
  df = df[columns_order]
296
  return df.to_string(index=False)
297
 
298
+ def save_results(self):
299
+ try:
300
+ if not self.results:
301
+ return None, "No results to save"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
302
 
303
+ df = pd.DataFrame(self.results)
304
+ columns_order = ['Area (mm²)', 'Mean', 'StdDev', 'Min', 'Max', 'Point']
305
+ df = df[columns_order]
 
 
 
 
 
 
 
 
 
 
 
306
 
307
+ temp_file = "analysis_results.xlsx"
308
+ df.to_excel(temp_file, index=False)
 
 
 
 
 
 
 
 
 
 
 
 
309
 
310
+ return temp_file, "Results saved successfully"
311
+ except Exception as e:
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
+ ]
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
+ # Process results
347
+ result_idx = 0
348
+ current_col_group = 0
349
+ current_row_pair = 0
350
 
351
+ while result_idx < len(self.results):
352
+ if current_col_group >= len(column_groups) or current_row_pair >= len(row_pairs):
353
+ break
354
+
355
+ # Get current positions
356
+ cols = column_groups[current_col_group]
357
+ rows = row_pairs[current_row_pair]
358
+
359
+ # Write first row if available
360
+ if result_idx < len(self.results):
361
+ result = self.results[result_idx]
362
+ # Write Area
363
+ ws[f"{cols[0]}{rows[0]}"] = float(result['Area (mm²)'])
364
+ # Write Mean
365
+ ws[f"{cols[1]}{rows[0]}"] = float(result['Mean'])
366
+ # Write StdDev
367
+ ws[f"{cols[2]}{rows[0]}"] = float(result['StdDev'])
368
+ # Write Min
369
+ ws[f"{cols[3]}{rows[0]}"] = float(result['Min'])
370
+ # Write Max
371
+ ws[f"{cols[4]}{rows[0]}"] = float(result['Max'])
372
+ result_idx += 1
373
+
374
+ # Write second row if available
375
+ if result_idx < len(self.results):
376
+ result = self.results[result_idx]
377
+ # Write Area
378
+ ws[f"{cols[0]}{rows[1]}"] = float(result['Area (mm²)'])
379
+ # Write Mean
380
+ ws[f"{cols[1]}{rows[1]}"] = float(result['Mean'])
381
+ # Write StdDev
382
+ ws[f"{cols[2]}{rows[1]}"] = float(result['StdDev'])
383
+ # Write Min
384
+ ws[f"{cols[3]}{rows[1]}"] = float(result['Min'])
385
+ # Write Max
386
+ ws[f"{cols[4]}{rows[1]}"] = float(result['Max'])
387
+ result_idx += 1
388
+
389
+ # Move to next column group if we've written both rows
390
+ current_col_group += 1
391
+
392
+ # If we've used all column groups, move to next row pair
393
+ if current_col_group >= len(column_groups):
394
+ current_col_group = 0
395
+ current_row_pair += 1
396
 
397
+ # Save while preserving formulas
398
+ wb.save(output_path)
399
+ print(f"Saved {result_idx} results to template")
400
+ return output_path, f"Results saved successfully in template format ({result_idx} measurements)"
401
 
402
+ except Exception as e:
403
+ print(f"Error saving results to template: {str(e)}")
404
+ logger.error(f"Error saving results to template: {str(e)}")
405
+ logger.error(traceback.format_exc())
406
+ return None, f"Error saving results to template: {str(e)}"
407
 
408
  def add_blank_row(self, image):
409
  self.results.append({