plot_voxel-counts.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #!/usr/bin/env python3
  2. '''
  3. created on Mon March 29th 2021
  4. author: Christian Olaf Haeusler
  5. '''
  6. from glob import glob
  7. import argparse
  8. import ipdb
  9. import matplotlib.pyplot as plt
  10. from matplotlib.lines import Line2D
  11. import numpy as np
  12. import os
  13. import pandas as pd
  14. import seaborn as sns
  15. def parse_arguments():
  16. '''
  17. '''
  18. parser = argparse.ArgumentParser(
  19. description='loads a csv file to plot data as stripplot')
  20. parser.add_argument('-cronbachs',
  21. required=False,
  22. default='results/statistics_cronbachs.csv',
  23. help='the data as csv')
  24. parser.add_argument('-outdir',
  25. required=False,
  26. default='results',
  27. help='output directory')
  28. args = parser.parse_args()
  29. cronbachs = args.cronbachs
  30. outdir = args.outdir
  31. return cronbachs, outdir
  32. def plot_boxplot(data):
  33. '''
  34. '''
  35. sns.boxplot(
  36. data=data,
  37. # ax=axis,
  38. zorder=2, # boxplots are supposed to be behind the stripplot
  39. x='number of voxels',
  40. width=0.1,
  41. # median
  42. medianprops={'visible': True,
  43. 'color': 'dimgrey',
  44. 'ls': '-',
  45. 'lw': 1,
  46. # 'alpha': 0.3
  47. },
  48. # mean
  49. showmeans=True,
  50. meanline=True,
  51. meanprops={'color': 'dimgrey',
  52. 'ls': (0, (1, 1)),
  53. 'lw': 1,
  54. # 'alpha': 0.3
  55. },
  56. # outliers
  57. showfliers=False,
  58. # flierprops={'marker': 'x', 'markersize': 8},
  59. # box
  60. showbox=True,
  61. boxprops={'color': 'dimgrey',
  62. 'lw': 1,
  63. 'alpha': 0.3
  64. },
  65. # whiskers
  66. whiskerprops={'color': 'dimgrey',
  67. 'lw': 1,
  68. 'alpha': 0.3
  69. },
  70. # caps (horizontal lines at the ends of whiskers)
  71. showcaps=True,
  72. capprops={'color': 'dimgrey',
  73. 'alpha': 0.3
  74. },
  75. )
  76. # remove the handles and labels from the legend
  77. # handles, labels = axis.get_legend_handles_labels()
  78. plt.legend([], [], loc='lower right')
  79. # return axis
  80. def plot_stripplot(data):
  81. '''
  82. '''
  83. # do the actual plotting
  84. sns.stripplot(data=data,
  85. x='number of voxels',
  86. jitter=0.02,
  87. linewidth=1,
  88. color='dimgrey',
  89. edgecolor='black'
  90. )
  91. # limit the axes
  92. plt.xlim([1200, 2000])
  93. plt.ylim([-0.1, 0.1])
  94. # return ax
  95. if __name__ == "__main__":
  96. # take the number of voxels from the results from script calculating
  97. # cronbach
  98. cronbachsResults, outDir = parse_arguments()
  99. df = pd.read_csv(cronbachsResults)
  100. voxelDf = df.loc[df['stimulus'] == 'visual localizer']
  101. # some preparations for plotting
  102. # close figure
  103. plt.close()
  104. # set style for seaborn
  105. sns.set_theme(style='whitegrid')
  106. # set seed to always have the same jitter
  107. np.random.seed(seed=1984)
  108. # create the figure
  109. plt.figure(figsize=(12, 2))
  110. # plot the underlying boxplot
  111. plot_boxplot(voxelDf)
  112. # plot the stripplot (points per subject)
  113. plot_stripplot(voxelDf)
  114. # define the parameters for the legend
  115. lineSubject = Line2D([0], [0],
  116. label='subject',
  117. linestyle='None',
  118. marker='o',
  119. color='dimgrey',
  120. markeredgecolor='black'
  121. )
  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='center left')
  136. # use a tight layout
  137. plt.tight_layout()
  138. # save the figure
  139. os.makedirs(outDir, exist_ok=True)
  140. # save to different file formats
  141. extensions = ['pdf'] # , 'png', 'svg']
  142. for extension in extensions:
  143. fpath = os.path.join(outDir, f'plot_voxel-counts.{extension}')
  144. plt.savefig(fpath, bbox_inches='tight')
  145. plt.close()