HeatMaps_Accuracy.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Thu Nov 23 02:42:50 2023
  4. @author: arefk
  5. """
  6. import pandas as pd
  7. import seaborn as sns
  8. import matplotlib.pyplot as plt
  9. import matplotlib.font_manager as fm
  10. import matplotlib.ticker as ticker
  11. import os
  12. # Load your Excel file into a Pandas DataFrame
  13. # Read data from the CSV file
  14. script_dir = os.path.dirname(__file__)
  15. file_path = os.path.join(script_dir, '..', 'input')
  16. out_path = os.path.join(script_dir, '..', 'figures')
  17. csv_file = os.path.join(file_path,"Confusion_matrix_metrics.csv")
  18. df = pd.read_csv(csv_file)
  19. # Set the user-defined thresholds to get desired values
  20. Thresold_Human_Voters = 4 # Replace with the user-set threshold value
  21. Thresold_ML_Voters = 1
  22. # Filter the DataFrame based on the user-set threshold
  23. filtered_df = df[(df['Thresold_Human_Voters'] == Thresold_Human_Voters) &
  24. (df['Thresold_ML_Voters'] == Thresold_ML_Voters) &
  25. (df['TP']+df['FN'] > 0)]
  26. filtered_df.loc[filtered_df["sequence_name"] == "anat","sequence_name"] = "Anatomical"
  27. filtered_df.loc[filtered_df["sequence_name"] == "diff","sequence_name"] = "Diffusion"
  28. filtered_df.loc[filtered_df["sequence_name"] == "func","sequence_name"] = "Functional"
  29. cm = 1/2.54 # centimeters in inches
  30. # Create two subplots for Kappa and F1 scores heatmaps
  31. fig, axes = plt.subplots(1, 1, figsize=(20*cm, 4*cm), dpi=300)
  32. sns.set_style('darkgrid')
  33. # Specify the font properties
  34. font_properties = fm.FontProperties(family='Times New Roman', size=8)
  35. font_properties2 = fm.FontProperties(family='Times New Roman', size=10)
  36. Title = ["(a) Fleiss kappa score: inter rater reliability "," (b) F1_score: raters vs AIDAqc"]
  37. for i, metric in enumerate(['Accuracy']):
  38. ax = axes
  39. pivot_df = filtered_df.pivot(index='sequence_name', columns='dataset_name', values=metric)
  40. pivot_df['mean'] = pivot_df.mean(axis=1)
  41. pivot_df['std'] = pivot_df.std(axis=1)
  42. t=Title[i]
  43. sns.heatmap(pivot_df, annot=True, fmt=".1f", cmap="YlGnBu", cbar=True, ax=ax)
  44. # Set font properties for labels, titles, and annotations
  45. ax.set_xticklabels(ax.get_xticklabels(), fontproperties=font_properties)
  46. ax.set_yticklabels(ax.get_yticklabels(), fontproperties=font_properties)
  47. ax.set_xlabel('Datasets', fontproperties=font_properties)
  48. ax.set_ylabel('Sequences', fontproperties=font_properties)
  49. #ax.set_title(f'{t} ', fontsize=10, fontproperties=font_properties2, fontweight='bold')
  50. ax.set(xlabel=None)
  51. # Set the color bar legend font size and font properties
  52. cbar = ax.collections[0].colorbar
  53. cbar.ax.tick_params(labelsize=8)
  54. cbar.ax.set_yticklabels(cbar.ax.get_yticklabels(), fontproperties=font_properties)
  55. # Customize the color bar ticks (increase the number of ticks)
  56. cbar.ax.yaxis.set_major_locator(ticker.MaxNLocator(nbins=5))
  57. a = []
  58. for text in ax.texts:
  59. a.append(float(text.get_text()))
  60. for text in ax.texts:
  61. text.set_font("Times New Roman")
  62. text.set_size(10)
  63. # Show the plots
  64. plt.tight_layout()
  65. output_path = out_path
  66. output_filename = "HeatMap_Accurracy_ManualRater4_AIDAqc1"
  67. # Save as SVG
  68. #fig.savefig(f"{output_path}/{output_filename}.svg", format="svg")
  69. # Save as PNG
  70. #fig.savefig(f"{output_path}/{output_filename}.png", format="png")
  71. plt.show()
  72. #%% All Thresholds loops through
  73. import pandas as pd
  74. import seaborn as sns
  75. import matplotlib.pyplot as plt
  76. import matplotlib.font_manager as fm
  77. import matplotlib.ticker as ticker
  78. import os
  79. # Load your Excel file into a Pandas DataFrame
  80. # Read data from the CSV file
  81. script_dir = os.path.dirname(__file__)
  82. file_path = os.path.join(script_dir, '..', 'input')
  83. out_path = os.path.join(script_dir, '..', 'figures')
  84. csv_file = os.path.join(file_path,"Confusion_matrix_metrics.csv")
  85. df = pd.read_csv(csv_file)
  86. # Set the user-defined thresholds to get desired values
  87. for Thresold_Human_Voters in df["Thresold_Human_Voters"].unique():
  88. for Thresold_ML_Voters in df["Thresold_ML_Voters"].unique():
  89. # Filter the DataFrame based on the user-set threshold
  90. filtered_df = df[(df['Thresold_Human_Voters'] == Thresold_Human_Voters) &
  91. (df['Thresold_ML_Voters'] == Thresold_ML_Voters) &
  92. (df['TP']+df['FN'] > 0)]
  93. filtered_df.loc[filtered_df["sequence_name"] == "anat","sequence_name"] = "Anatomical"
  94. filtered_df.loc[filtered_df["sequence_name"] == "diff","sequence_name"] = "Diffusion"
  95. filtered_df.loc[filtered_df["sequence_name"] == "func","sequence_name"] = "Functional"
  96. cm = 1/2.54 # centimeters in inches
  97. # Create two subplots for Kappa and F1 scores heatmaps
  98. fig, axes = plt.subplots(1, 1, figsize=(20*cm, 5*cm), dpi=300)
  99. sns.set_style('darkgrid')
  100. # Specify the font properties
  101. font_properties = fm.FontProperties(family='Times New Roman', size=8)
  102. font_properties2 = fm.FontProperties(family='Times New Roman', size=10)
  103. Title = ["(a) Fleiss kappa score: inter rater reliability "," (b) F1_score: raters vs AIDAqc"]
  104. for i, metric in enumerate(['Accuracy']):
  105. ax = axes
  106. pivot_df = filtered_df.pivot(index='sequence_name', columns='dataset_name', values=metric)
  107. pivot_df['mean'] = pivot_df.mean(axis=1)
  108. pivot_df['std'] = pivot_df.std(axis=1)
  109. t = "Human: " + str(Thresold_Human_Voters) + "|| AIDAqc:" +str(Thresold_ML_Voters)
  110. sns.heatmap(pivot_df, annot=True, fmt=".2f", cmap="YlGnBu", cbar=True, ax=ax)
  111. # Set font properties for labels, titles, and annotations
  112. ax.set_xticklabels(ax.get_xticklabels(), fontproperties=font_properties)
  113. ax.set_yticklabels(ax.get_yticklabels(), fontproperties=font_properties)
  114. ax.set_xlabel('Datasets', fontproperties=font_properties)
  115. ax.set_ylabel('Sequences', fontproperties=font_properties)
  116. ax.set_title(f'{t} ', fontsize=10, fontproperties=font_properties2, fontweight='bold')
  117. ax.set(xlabel=None)
  118. # Set the color bar legend font size and font properties
  119. cbar = ax.collections[0].colorbar
  120. cbar.ax.tick_params(labelsize=8)
  121. cbar.ax.set_yticklabels(cbar.ax.get_yticklabels(), fontproperties=font_properties)
  122. # Customize the color bar ticks (increase the number of ticks)
  123. cbar.ax.yaxis.set_major_locator(ticker.MaxNLocator(nbins=5))
  124. a = []
  125. for text in ax.texts:
  126. a.append(float(text.get_text()))
  127. for text in ax.texts:
  128. text.set_font("Times New Roman")
  129. text.set_size(8)
  130. # Show the plots
  131. plt.tight_layout()
  132. plt.show()