CSVplot.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #%% SNR Chang vs Standard plot for RT and Cryp
  2. p_address = r"C:\Users\aswen\Documents\Data\2023_Kalantari_AIDAqc\outputs\validation\QC_Standard\94_m_As_allslice\calculated_features\caculated_features_structural.csv"
  3. p_address2= r"C:\Users\aswen\Documents\Data\2023_Kalantari_AIDAqc\outputs\validation\QC_Standard\94c_m_As_allslices\calculated_features\caculated_features_structural.csv"
  4. #p_save = r"\\10.209.5.114\Publications\2023_Kalantari_AIDAqc\outputs\QC_Final\validation\ChangVSStandard"
  5. import pandas as pd
  6. import matplotlib.pyplot as plt
  7. import seaborn as sns
  8. import pandas as pd
  9. import matplotlib.pyplot as plt
  10. import seaborn as sns
  11. import numpy as np
  12. cm = 1/2.54 # centimeters in inches
  13. # Set the color palette to 'Set2'
  14. plt.figure(figsize=(18*cm/3,7.30*cm),dpi=300)
  15. # Load the CSV data into a pandas DataFrame
  16. data = pd.read_csv(p_address)
  17. data2 = pd.read_csv(p_address2)
  18. chang = data["SNR Chang"].to_frame().rename(columns={"SNR Chang": "SNR"})
  19. normal = data["SNR Normal"].to_frame().rename(columns={"SNR Normal": "SNR"})
  20. chang["type"] = "chang_RT"
  21. normal["type"] = "normal_RT"
  22. chang2 = data2["SNR Chang"].to_frame().rename(columns={"SNR Chang": "SNR"})
  23. normal2 = data2["SNR Normal"].to_frame().rename(columns={"SNR Normal": "SNR"})
  24. chang2["type"] = "chang_Cryo"
  25. normal2["type"] = "normal_Cryo"
  26. Data_merged = data[["SNR Chang","SNR Normal"]]
  27. Data_merged.rename(columns={"SNR Normal":"SNR-standard"})
  28. #Data_merged[["SNR Chang","SNR Normal"]] = Data_merged[["SNR Chang","SNR Normal"]].apply(lambda x: np.power((x/20),10))
  29. Data_merged2 = data2[["SNR Chang","SNR Normal"]]
  30. Data_merged2.rename(columns={"SNR Normal":"SNR-standard"})
  31. #Data_merged2[["SNR Chang","SNR Normal"]] = Data_merged2[["SNR Chang","SNR Normal"]].apply(lambda x: np.power((x/20),10))
  32. SNR = pd.concat([chang, normal,chang2,normal2])
  33. #SNR["SNR"] = SNR["SNR"].apply(lambda x: np.power(x/20,10))
  34. tips = SNR
  35. x = "type"
  36. y = "SNR"
  37. sns.set_style('ticks')
  38. # Create circular marker style for left side
  39. circle_marker = {"marker": "o", "markerfacecolor": "black", "markersize": 3}
  40. #tips = sns.load_dataset("tips")
  41. plt.rcParams['font.family'] = 'Times New Roman'
  42. plt.rcParams['font.size'] = 8
  43. plt.title("(c) Chang vs Standard SNR",weight='bold')
  44. ax=sns.barplot(x="type", y="SNR", data=tips, capsize=0 ,palette="pastel",errorbar="se",ci=None)
  45. #sns.swarmplot(x="type", y="SNR", data=tips, color="0", alpha=.5)
  46. for i in range(len(Data_merged)):
  47. plt.plot(["chang_RT", "normal_RT"], Data_merged.iloc[i], color="black", **circle_marker,linewidth=0.5)
  48. for i in range(len(Data_merged2)):
  49. plt.plot(["chang_Cryo", "normal_Cryo"], Data_merged2.iloc[i], color="black", **circle_marker,linewidth=0.5)
  50. ax.spines['top'].set_linewidth(0) # Top border
  51. ax.spines['right'].set_linewidth(0) # Right border
  52. ax.spines['bottom'].set_linewidth(0.5) # Bottom border
  53. ax.spines['left'].set_linewidth(0.5) # Left border
  54. # Set the font to Times New Roman and font size to 8 points
  55. #ax.set_ylim(bottom=0, top=45)
  56. ax.set_xticklabels(ax.get_xticklabels(), rotation=20,fontsize=8)
  57. # Rest of your code...
  58. ax.set_xlabel('')
  59. ax.tick_params(axis='both', which='both', width=0.5,color='gray',length=2)
  60. plt.show()
  61. #%% SNR Chang vs Standard plot for RT and Cryp Version 2
  62. p_address = r"C:\Users\aswen\Documents\Data\2023_Kalantari_AIDAqc\outputs\validation\QC_Standard\94_m_As\calculated_features\caculated_features_anatomical.csv"
  63. p_address2= r"C:\Users\aswen\Documents\Data\2023_Kalantari_AIDAqc\outputs\validation\QC_Standard\94c_m_As\calculated_features\caculated_features_anatomical.csv"
  64. #p_save = r"\\10.209.5.114\Publications\2023_Kalantari_AIDAqc\outputs\QC_Final\validation\ChangVSStandard"
  65. import pandas as pd
  66. import matplotlib.pyplot as plt
  67. import seaborn as sns
  68. import pandas as pd
  69. import matplotlib.pyplot as plt
  70. import seaborn as sns
  71. import numpy as np
  72. cm = 1/2.54 # centimeters in inches
  73. # Set the color palette to 'Set2'
  74. plt.figure(figsize=(18*cm/3,7.30*cm),dpi=300)
  75. # Load the CSV data into a pandas DataFrame
  76. data = pd.read_csv(p_address)
  77. data2 = pd.read_csv(p_address2)
  78. chang = data["SNR Chang"].to_frame().rename(columns={"SNR Chang": "SNR"})
  79. normal = data["SNR Normal"].to_frame().rename(columns={"SNR Normal": "SNR"})
  80. chang["type"] = "chang_rt"
  81. normal["type"] = "normal_rt"
  82. chang2 = data2["SNR Chang"].to_frame().rename(columns={"SNR Chang": "SNR"})
  83. normal2 = data2["SNR Normal"].to_frame().rename(columns={"SNR Normal": "SNR"})
  84. chang2["type"] = "chang_cryo"
  85. normal2["type"] = "normal_cryo"
  86. SNR = pd.concat([chang, normal,chang2,normal2])
  87. SNR["SNR"] = SNR["SNR"].apply(lambda x: np.power(10,x/20))
  88. SNR = SNR.sort_values('type')
  89. #Data_of_selected_feature = Data_of_selected_feature.sort_values("Dataset",ascending=False)
  90. # creating boxplots
  91. #plt.figure(figsize=(6*cm,6*cm*np.e),dpi=300)
  92. sns.set_style('ticks')
  93. sns.set(font='Times New Roman', font_scale=0.9,style=None) # Set font to Times New Roman and font size to 9
  94. palette = 'Set2'
  95. ax = sns.violinplot(x="type", y="SNR", data=SNR, hue="type", dodge=False,
  96. palette=palette,
  97. scale="width", inner=None,linewidth=1)
  98. patches = ax.patches
  99. #legend_colors = [patch.get_facecolor() for patch in patches[:]]
  100. xlim = ax.get_xlim()
  101. ylim = ax.get_ylim()
  102. for violin in ax.collections:
  103. bbox = violin.get_paths()[0].get_extents()
  104. x0, y0, width, height = bbox.bounds
  105. violin.set_clip_path(plt.Rectangle((x0, y0), width / 2, height, transform=ax.transData))
  106. sns.boxplot(x="type", y="SNR", data=SNR, saturation=1, showfliers=False,
  107. width=0.3, boxprops={'zorder': 3, 'facecolor': 'none'}, ax=ax, linewidth=1)
  108. old_len_collections = len(ax.collections)
  109. sns.stripplot(x="type", y="SNR", data=SNR, size=1.1, hue="type", palette=palette, dodge=False, ax=ax)
  110. for dots in ax.collections[old_len_collections:]:
  111. dots.set_offsets(dots.get_offsets() + np.array([0.12, 0]))
  112. ax.set_xlim(xlim)
  113. ax.set_ylim(ylim)
  114. ax.legend_.remove()
  115. ax
  116. ax.set_xlabel('')
  117. ax.set_xticklabels(ax.get_xticklabels(), rotation=20,fontsize=8)
  118. #ax.set_ylim(bottom=15, top=40)
  119. #%% Plot all chang vs normal
  120. import seaborn as sns
  121. import matplotlib.pyplot as plt
  122. import pandas as pd
  123. import scipy.stats as stats
  124. import numpy as np
  125. import os
  126. cm = 1/2.54 # centimeters in inches
  127. # Specify the path to your Excel file
  128. excel_file_path = r"C:\Users\arefk\OneDrive\Desktop\Projects\2023_Kalantari_AIDAqc\outputs\files_4figs\combined_data_anat.csv"
  129. plt.figure(figsize=(10*cm,10*cm),dpi=300)
  130. # Read the data into a pandas DataFrame
  131. df = pd.read_csv(excel_file_path)
  132. # Drop rows with NaN values in the specified columns
  133. #df = df.dropna(subset=['SNR-Chang (dB)', 'SNR-Standard (dB)'])
  134. df['SNR Chang'] = df['SNR Chang'].apply(lambda x: np.power(10,x/20))
  135. df['SNR Normal'] = df['SNR Normal'].apply(lambda x: np.power(10,x/20))
  136. df['names'] = df['FileAddress'].apply(lambda x:x.split("mri")[1].split(os.path.sep)[1])
  137. plt.rcParams['font.family'] = 'Times New Roman'
  138. plt.rcParams['font.size'] = 8
  139. plt.title("All anatomical data",weight='bold', fontsize=10)
  140. # Calculate the correlation and p-value between 'SNR-Chang' and 'SNR-Standard'
  141. #correlation, p_value = stats.pearsonr(df['SNR-Chang (dB)'], df['SNR-Standard (dB)'])
  142. correlation, p_value = stats.spearmanr(df['SNR Chang'], df['SNR Normal'], nan_policy='omit',alternative='two-sided')
  143. # Set seaborn style
  144. sns.set_style('whitegrid')
  145. # Create a scatter plot
  146. #ax=sns.scatterplot(data=df, x='SNR-Chang', y='SNR-Standard',palette="gray_r",s=7)
  147. ax=sns.scatterplot(data=df, x='SNR Chang', y='SNR Normal',hue="names",palette="Spectral_r",s=7)
  148. ax.set_title("All anatomical data", weight='bold', fontsize=11)
  149. # Set title and labels including the correlation and p-value
  150. #plt.title(f'Correlation: {correlation:.8f}, P-value: {p_value:.8f}')
  151. plt.xlabel('SNR-Chang')
  152. plt.ylabel('SNR-Standard')
  153. ax.set_xlim(0,200)
  154. ax.set_ylim(0,140)
  155. ax.spines['top'].set_linewidth(0) # Top border
  156. ax.spines['right'].set_linewidth(0) # Right border
  157. ax.spines['bottom'].set_linewidth(0.5) # Bottom border
  158. ax.spines['left'].set_linewidth(0.5) # Left border
  159. # Show the plot
  160. # Move the legend outside the plot to the right side
  161. legend =plt.legend(title="Dataset", loc='center left', bbox_to_anchor=(1, 0.5), fontsize=5,handlelength=0.5)
  162. legend.get_title().set_fontfamily('Times New Roman')
  163. for text in legend.get_texts():
  164. text.set_fontfamily('Times New Roman')
  165. #ax.legend_.remove()
  166. plt.show()
  167. #%% Plot all chang vs normal and corrolate 0 to 50 and 50 to end
  168. import seaborn as sns
  169. import matplotlib.pyplot as plt
  170. import pandas as pd
  171. import scipy.stats as stats
  172. import numpy as np
  173. cm = 1/2.54 # centimeters in inches
  174. # Specify the path to your Excel file
  175. excel_file_path = r"Y:\2023_Kalantari_AIDAqc\outputs\QC_Final\validation\Chang&normal_all.xlsx"
  176. plt.figure(figsize=(8*cm,8*cm),dpi=300)
  177. # Read the data into a pandas DataFrame
  178. df = pd.read_excel(excel_file_path, engine='openpyxl')
  179. # Drop rows with NaN values in the specified columns
  180. #df = df.dropna(subset=['SNR-Chang (dB)', 'SNR-Standard (dB)'])
  181. df['SNR-Chang'] = df['SNR-Chang'].apply(lambda x: np.power(10,x/20))
  182. df['SNR-Standard'] = df['SNR-Standard'].apply(lambda x: np.power(10,x/20))
  183. df = df.sort_values('sort')
  184. filtered_df = df[df['Dataset'].isin(['94_m_We', '94_m_Va'])]
  185. plt.rcParams['font.family'] = 'Times New Roman'
  186. plt.rcParams['font.size'] = 8
  187. plt.title("All anatomical data",weight='bold')
  188. # Create two dataframes, one for values smaller than 50 and another for values 50 or greater
  189. df_smaller_than_50 = df[df['SNR-Chang'] < 60 & df['SNR-Standard'] < 75]
  190. # Calculate the correlation and p-value for the group where 'SNR-Chang' < 50
  191. correlation_smaller_than_50, p_value_smaller_than_50 = stats.spearmanr(df_smaller_than_50['SNR-Chang'], df_smaller_than_50['SNR-Standard'], nan_policy='omit', alternative='two-sided')
  192. print("Correlation:", correlation_smaller_than_50)
  193. print("P-value:", p_value_smaller_than_50)