ChangvsStandardSNR.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #%% Plot all chang vs normal
  2. import seaborn as sns
  3. import matplotlib.pyplot as plt
  4. import pandas as pd
  5. import scipy.stats as stats
  6. import numpy as np
  7. import os
  8. sns.set_style('ticks')
  9. cm = 1/2.54 # centimeters in inches
  10. # Specify the path to your Excel file
  11. # Read data from the CSV file
  12. script_dir = os.path.dirname(__file__)
  13. excel_file_path = os.path.join(script_dir, '..', 'input', 'combined_data_anat.csv')
  14. out_path = os.path.join(script_dir, '..', 'figures')
  15. plt.figure(figsize=(10*cm,10*cm),dpi=300)
  16. # Read the data into a pandas DataFrame
  17. df = pd.read_csv(excel_file_path)
  18. # Drop rows with NaN values in the specified columns
  19. #df = df.dropna(subset=['SNR-Chang (dB)', 'SNR-Standard (dB)'])
  20. # Custom sorting key function
  21. def extract_number(dataset):
  22. # Extract numeric part from the 'dataset' column
  23. # Assuming the numeric part is always at the beginning of the string
  24. # If it's not, you might need a more sophisticated method
  25. return int(''.join(filter(str.isdigit, dataset)))
  26. df['sorting_key'] = df['dataset']#.apply(extract_number)
  27. df['SNR Chang'] = df['SNR Chang']#.apply(lambda x: np.power(10,x/20))
  28. df['SNR Normal'] = df['SNR Normal']#.apply(lambda x: np.power(10,x/20))
  29. df = df.sort_values(by=["sorting_key"],ascending=True)
  30. plt.rcParams['font.family'] = 'Times New Roman'
  31. plt.rcParams['font.size'] = 8
  32. #plt.title("All anatomical data",weight='bold', fontsize=10)
  33. # Calculate the correlation and p-value between 'SNR-Chang' and 'SNR-Standard'
  34. #correlation, p_value = stats.pearsonr(df['SNR-Chang (dB)'], df['SNR-Standard (dB)'])
  35. # Set seaborn style
  36. Wanted_datasets = ["117_m_Ra","94_m_Va","7_m_Se","7_rab_Mu","7_h_He"]
  37. # Filter DataFrame based on Wanted_datasets
  38. filtered_df = df[df['dataset'].isin(Wanted_datasets)]
  39. #
  40. # Your DataFrame and plot
  41. ax = sns.scatterplot(data=filtered_df, x='SNR Chang', y='SNR Normal', hue="dataset", palette= "Set1", s=20)
  42. #ax.set_title("All anatomical data", weight='bold', fontsize=11)
  43. # Set title and labels including the correlation and p-value
  44. plt.xlabel('SNR-Chang (db)', fontname='Times New Roman', fontsize=8)
  45. plt.ylabel('SNR-Standard (db)', fontname='Times New Roman', fontsize=8)
  46. # Calculate the correlation and p-value between 'SNR-Chang' and 'SNR-Standard'
  47. correlation, p_value = stats.spearmanr(df['SNR Chang'], df['SNR Normal'], nan_policy='omit', alternative='two-sided')
  48. # Sort the dataframe by 'SNR Chang' in descending order
  49. df_sorted = df.sort_values(by='SNR Chang', ascending=False)
  50. # Exclude the top 3 highest values
  51. top3_indices = df_sorted.head(1).index
  52. df_filtered = df.drop(top3_indices)
  53. ax.spines['top'].set_linewidth(0.5) # Top border
  54. ax.spines['right'].set_linewidth(0.5) # Right border
  55. ax.spines['bottom'].set_linewidth(0.5) # Bottom border
  56. ax.spines['left'].set_linewidth(0.5) # Left border
  57. ax.set_xlim(15,50)
  58. ax.set_ylim(10,50)
  59. # Move the legend outside the plot to the right side
  60. legend = plt.legend(title="Dataset", loc='center left', bbox_to_anchor=(1, 0.5), fontsize=8, handlelength=0.5)
  61. legend.get_title().set_fontfamily('Times New Roman')
  62. for text in legend.get_texts():
  63. text.set_fontfamily('Times New Roman')
  64. fig_path_png = os.path.join(out_path, 'StandardVSchangtogether.png')
  65. fig_path_svg = os.path.join(out_path, 'StandardVSchangtogether.svg')
  66. #plt.savefig(fig_path_png, format='png', bbox_inches='tight')
  67. #plt.savefig(fig_path_svg, format='svg', bbox_inches='tight')
  68. # Show the plot
  69. plt.show()