plot_cronbachs.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #!/usr/bin/env python3
  2. '''
  3. created on Mon March 29th 2021
  4. author: Christian Olaf Haeusler
  5. '''
  6. import argparse
  7. import ipdb
  8. import matplotlib.pyplot as plt
  9. from matplotlib.lines import Line2D
  10. import numpy as np
  11. import os
  12. import pandas as pd
  13. import seaborn as sns
  14. STIMULICOLORS = {# 'anatomical alignment': 'grey',
  15. 'visual localizer': 'y',
  16. 'movie': 'red',
  17. 'audio-description': 'blue'
  18. }
  19. def parse_arguments():
  20. '''
  21. '''
  22. parser = argparse.ArgumentParser(
  23. description='loads a csv file to plot data as stripplot')
  24. parser.add_argument('-cronbachs',
  25. required=False,
  26. default='results/statistics_cronbachs.csv',
  27. help='the data as csv')
  28. parser.add_argument('-outdir',
  29. required=False,
  30. default='results',
  31. help='output directory')
  32. args = parser.parse_args()
  33. cronbachs = args.cronbachs
  34. outdir = args.outdir
  35. return cronbachs, outdir
  36. def plot_boxplot(axis, df):
  37. '''
  38. '''
  39. axis = sns.boxplot(
  40. data=df,
  41. ax=axis,
  42. zorder=1, # boxplots are supposed to be behind the stripplot
  43. x='stimulus',
  44. y="Cronbach's a",
  45. width=0.5,
  46. palette = STIMULICOLORS,
  47. # median
  48. medianprops={'visible': True,
  49. 'color': 'dimgrey',
  50. 'ls': '-',
  51. 'lw': 1,
  52. # 'alpha': 0.3
  53. },
  54. # mean
  55. showmeans=True,
  56. meanline=True,
  57. meanprops={'color': 'dimgrey',
  58. 'ls': (0, (1, 1)),
  59. 'lw': 1,
  60. # 'alpha': 0.3
  61. },
  62. # outliers
  63. showfliers=False,
  64. # flierprops={'marker': 'x', 'markersize': 8},
  65. # box
  66. showbox=True,
  67. boxprops={# 'color': STIMULICOLORS.items(),
  68. 'lw': 1,
  69. 'alpha': 0.3
  70. },
  71. # whiskers
  72. whiskerprops={'color': 'dimgrey',
  73. 'lw': 1,
  74. 'alpha': 0.3
  75. },
  76. # caps (horizontal lines at the ends of whiskers)
  77. showcaps=True,
  78. capprops={'color': 'dimgrey',
  79. 'alpha': 0.3
  80. },
  81. )
  82. # remove the handles and labels from the legend
  83. # handles, labels = axis.get_legend_handles_labels()
  84. # axis.legend([], [], loc='lower right')
  85. return axis
  86. def plot_stripplot(axis, df):
  87. '''
  88. '''
  89. ax = sns.stripplot(ax=axis,
  90. data=df,
  91. # hue = list(STIMULICOLORS.keys()),
  92. # hue=STIMULICOLORS.keys(),
  93. x='stimulus',
  94. y="Cronbach's a",
  95. palette = STIMULICOLORS,
  96. jitter=0.2,
  97. linewidth=1,
  98. dodge=True
  99. )
  100. # rename the x axis label
  101. # ax.set_xlabel('paradigm')
  102. # or just writen nothing
  103. ax.set_xlabel('')
  104. ax.set_ylabel(r"Cronbach's $\alpha$")
  105. if __name__ == "__main__":
  106. # for now hard code some stuff
  107. cronbachsResults, outDir = parse_arguments()
  108. # read command line arguments
  109. # visResults, avResults, aoResults, outDir = parse_arguments()
  110. df = pd.read_csv(cronbachsResults)
  111. # some preparations for plotting
  112. # close figure
  113. plt.close()
  114. # set style for seaborn
  115. sns.set_theme(style='whitegrid')
  116. # set seed to always have the same jitter
  117. np.random.seed(seed=1986)
  118. fig, axis = plt.subplots(1, 1, figsize=(6, 5), sharex=False)
  119. plot_boxplot(axis, df)
  120. plot_stripplot(axis, df)
  121. # define the parameters for the legend
  122. lineMedian = Line2D([0], [0],
  123. label='median',
  124. color='dimgrey',
  125. linestyle='-',
  126. linewidth=1
  127. )
  128. lineMean = Line2D([0], [0],
  129. label='mean',
  130. color='dimgrey',
  131. linestyle=(0, (1, 1)),
  132. linewidth=1
  133. )
  134. # plot the legend
  135. plt.legend(handles=[lineMedian, lineMean], loc='lower left')
  136. # calculate the medians per stimulus
  137. means = pd.pivot_table(df, index='stimulus', aggfunc=np.mean)
  138. medians = pd.pivot_table(df, index='stimulus', aggfunc=np.median)
  139. # save the figure
  140. os.makedirs(outDir, exist_ok=True)
  141. extensions = ['pdf'] # , 'png', 'svg']
  142. for extension in extensions:
  143. fpath = os.path.join(outDir, f'plot_cronbachs.{extension}')
  144. plt.savefig(fpath, bbox_inches='tight')
  145. plt.close()