CorroloationDTISNR_SNR.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import pandas as pd
  2. import os
  3. # Step 1: Read three CSV files
  4. # Read data from the CSV file
  5. script_dir = os.path.dirname(__file__)
  6. file_path = os.path.join(script_dir, '..', 'input')
  7. out_path = os.path.join(script_dir, '..', 'figures')
  8. func_df = pd.read_csv(os.path.join(file_path, 'combined_data_func.csv'))
  9. struct_df = pd.read_csv(os.path.join(file_path, 'combined_data_struct.csv'))
  10. anat_df = pd.read_csv(os.path.join(file_path, 'combined_data_anat.csv'))
  11. # Step 4: Sort all dataframes based on the FileAddress column
  12. func_df.sort_values(by='FileAddress', inplace=True)
  13. struct_df.sort_values(by='FileAddress', inplace=True)
  14. anat_df.sort_values(by='FileAddress', inplace=True)
  15. # Step 5: Process the FileAddress column
  16. def process_file_address(file_address):
  17. elements = file_address.split('\\') # Use '\\' to split on backslash
  18. return '\\'.join(elements[:-1]) # Use '\\' to join elements with backslash
  19. struct_df['FileAddress'] = struct_df['FileAddress'].apply(process_file_address)
  20. anat_df['FileAddress'] = anat_df['FileAddress'].apply(process_file_address)
  21. # Step 6: Create a new dataframe
  22. common_file_addresses = set(anat_df['FileAddress']).intersection(set(struct_df['FileAddress']))
  23. result_data = []
  24. for file_address in common_file_addresses:
  25. anat_rows = anat_df[anat_df['FileAddress'] == file_address]
  26. struct_rows = struct_df[struct_df['FileAddress'] == file_address]
  27. # Calculate the average of 'SNR Chang' and 'tSNR (Averaged Brain ROI)' values, ignoring NaNs
  28. avg_snr_chang = anat_rows['SNR Normal'].mean()
  29. avg_tsnr_avg_brain_roi = struct_rows['SNR Normal'].mean()
  30. result_data.append({
  31. 'Common FileAddress': file_address,
  32. 'Average SNR Chang': avg_snr_chang,
  33. 'Average tSNR (Averaged Brain ROI)': avg_tsnr_avg_brain_roi
  34. })
  35. # Create the result DataFrame
  36. result_df = pd.DataFrame(result_data)
  37. # Print the result
  38. print(result_df)
  39. import pandas as pd
  40. import os
  41. import seaborn as sns
  42. import matplotlib.pyplot as plt
  43. from scipy.stats import pearsonr
  44. from scipy.stats import spearmanr
  45. # Calculate the Spearman correlation coefficient and p-value
  46. corr, p_value = spearmanr(result_df['Average SNR Chang'], result_df['Average tSNR (Averaged Brain ROI)'])
  47. # Define the centimeters to inches conversion
  48. cm = 1/2.54 # centimeters in inches
  49. sns.set_style("ticks")
  50. # Set the font style to Times New Roman
  51. plt.rcParams['font.family'] = 'Times New Roman'
  52. # Create the plot with a specified figure size
  53. h = 5 * cm
  54. width = 9.5 * cm
  55. aspect = width / h
  56. fig = sns.lmplot(x='Average SNR Chang', y='Average tSNR (Averaged Brain ROI)',
  57. data=result_df, palette='Set1', ci=100,
  58. scatter_kws={'s': 8, 'color': '#4C72B0', 'edgecolor': 'w', 'linewidths': .3},
  59. line_kws={'lw': 2, 'color': '#4682b4'}, aspect=aspect, height=h)
  60. plt.rcParams['figure.dpi'] = 300
  61. plt.xlabel('Anatomical SNR-standard (dB)', fontsize=8)
  62. plt.ylabel('Diffusion SNR-standard (dB)', fontsize=8)
  63. ax = plt.gca()
  64. # Set font size for tick labels
  65. ax.tick_params(axis='both', which='both', labelsize=8)
  66. # Explicitly set the visibility of top and right spines
  67. ax.spines['top'].set_visible(True)
  68. ax.spines['right'].set_visible(True)
  69. # Customize the border linewidth
  70. ax.spines['top'].set_linewidth(0.5) # Top border
  71. ax.spines['right'].set_linewidth(0.5) # Right border
  72. ax.spines['bottom'].set_linewidth(0.5) # Bottom border
  73. ax.spines['left'].set_linewidth(0.5) # Left border
  74. ax.locator_params(axis='y', nbins=8)
  75. ax.locator_params(axis='x', nbins=16)
  76. # Add horizontal lines from yticks
  77. ax.tick_params(axis='both', which='both', width=0.5, color='gray', length=2)
  78. #ax.set_title("(b) SNR structural vs anatomical", weight='bold', fontsize=10)
  79. # Save the figure as PNG and SVG
  80. fig_path_png = os.path.join(out_path, 'AnatVSDiffAllDataCorr.png')
  81. fig_path_svg = os.path.join(out_path, 'AnatVSDiffAllDataCorr.svg')
  82. fig.savefig(fig_path_png, format='png', bbox_inches='tight')
  83. fig.savefig(fig_path_svg, format='svg', bbox_inches='tight')
  84. plt.show()