plot_pvalue_overview.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import pandas as pd
  2. import numpy as np
  3. import argparse
  4. import seaborn as sns
  5. import matplotlib.pyplot as plt
  6. from mpl_toolkits.axes_grid1 import make_axes_locatable
  7. import sys
  8. from pathlib import Path
  9. sys.path.append(str(Path.cwd().parents[0]))
  10. sys.path.append(str(Path.cwd().parents[0] / 'scripts'))
  11. from utils import load_df, colormap, stack_columns
  12. def plot_pvalues(data, ax=None, x='matrix', order=['weights', 'correlations'],
  13. border=0.1, alpha=0.5, lineplot=False):
  14. if ax is None:
  15. fig, ax = plt.subplots()
  16. data = stack_columns(data, new_column='matrix',
  17. prefixes=['pvalue'],
  18. suffixes=['weights', 'correlations'])
  19. sns.stripplot(x=x, y="pvalue", hue="matrix", data=data,
  20. ax=ax, palette=colormap, order=order, alpha=alpha)
  21. if lineplot:
  22. sns.lineplot(data=data, x=x, y='pvalue', hue='matrix',
  23. ax=ax, palette=colormap, alpha=0)
  24. ax.set_xlabel('')
  25. ax.set_yscale('linear')
  26. ax.set_ylim((border, 1.05))
  27. sns.despine(ax=ax, left=True, right=True, top=True, bottom=True)
  28. ax.get_legend().remove()
  29. ax.axhline(0.1, color='0.8', linestyle=':', linewidth=4)
  30. divider = make_axes_locatable(ax)
  31. axlog = divider.append_axes("bottom", size=3.5, pad=0, sharex=ax)
  32. axlog.set_yscale('log')
  33. minp = data[data.pvalue > 0].pvalue.min()
  34. axlog.set_ylim((minp/10, border))
  35. sns.stripplot(x=x, y="pvalue", hue="matrix",
  36. data=data, ax=axlog, palette=colormap,
  37. order=order, alpha=alpha)
  38. if lineplot:
  39. sns.lineplot(data=data, x=x, y='pvalue', hue='matrix',
  40. ax=axlog, palette=colormap, alpha=0)
  41. axlog.set_ylabel('')
  42. sns.despine(ax=axlog, left=True, right=True, top=True, bottom=True)
  43. axlog.set_xlabel('')
  44. axlog.get_legend().remove()
  45. return axlog
  46. def plot_pvalue_overview(data):
  47. fig, axes = plt.subplots(ncols=3, figsize=(12,7),
  48. gridspec_kw=dict(wspace=.5, width_ratios=[1,.2,.2]))
  49. alpha = 0.3
  50. # weights and correlation
  51. plot_pvalues(data, axes[0], alpha=alpha)
  52. # ratio
  53. ax = axes[1]
  54. sns.stripplot(y="pvalue_ratio", data=data, ax=ax,
  55. color=colormap['ratio'], alpha=alpha)
  56. ax.axhline(1, color='0.3', linestyle=':')
  57. ax.set_yscale('log')
  58. ax.yaxis.set_ticks_position('right')
  59. ax.yaxis.set_label_position('right')
  60. ax.set_ylabel(r'$p_c / p_w$')
  61. ax.set_xlabel('')
  62. ax.set_xticklabels(['ratio'])
  63. sns.despine(ax=ax, left=True, right=True, top=True, bottom=True)
  64. # rate correlation
  65. ax = axes[2]
  66. sns.stripplot(y="rate_correlation", data=data, ax=ax,
  67. color=colormap['rate_correlation'], alpha=alpha)
  68. ax.yaxis.set_ticks_position('right')
  69. ax.yaxis.set_label_position('right')
  70. ax.set_ylabel('rate vector correlation')
  71. ax.set_xlabel('')
  72. ax.set_xticklabels(['rates'])
  73. sns.despine(ax=ax, left=True, right=True, top=True, bottom=True)
  74. return fig
  75. if __name__ == '__main__':
  76. CLI = argparse.ArgumentParser()
  77. CLI.add_argument("--input", nargs='?', type=Path)
  78. CLI.add_argument("--output", nargs='?', type=Path)
  79. CLI.add_argument("--protocol", nargs='?', type=str)
  80. args, unknown = CLI.parse_known_args()
  81. df = load_df(args.input)
  82. data = df[df.protocol.str.contains(args.protocol)]
  83. if 'redraw' not in args.protocol:
  84. data = data[~data.protocol.str.contains('redraw')]
  85. sns.set(style='ticks', palette='deep', context='talk')
  86. fig = plot_pvalue_overview(data)
  87. fig.suptitle(f'{Path(args.output).name.strip(".png")}', fontsize=17)
  88. <<<<<<< HEAD
  89. plt.savefig(args.output, dpi=300, bbox_inches=None)
  90. =======
  91. plt.savefig(args.output, bbox_inches=None)
  92. >>>>>>> refs/remotes/origin/synced/master