speech_rate.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #!/usr/bin/env python3
  2. import pandas as pd
  3. import pickle
  4. import numpy as np
  5. from scipy.special import logit, expit
  6. import argparse
  7. import matplotlib
  8. import matplotlib.pyplot as plt
  9. from scipy.stats import gamma
  10. from os.path import basename
  11. matplotlib.use("pgf")
  12. matplotlib.rcParams.update(
  13. {
  14. "pgf.texsystem": "pdflatex",
  15. "font.family": "serif",
  16. "font.serif": "Times New Roman",
  17. "text.usetex": True,
  18. "pgf.rcfonts": False,
  19. }
  20. )
  21. def set_size(width, fraction=1, ratio=None):
  22. fig_width_pt = width * fraction
  23. inches_per_pt = 1 / 72.27
  24. if ratio is None:
  25. ratio = (5**0.5 - 1) / 2
  26. fig_width_in = fig_width_pt * inches_per_pt
  27. fig_height_in = fig_width_in * ratio
  28. return fig_width_in, fig_height_in
  29. parser = argparse.ArgumentParser(description="speech_rate")
  30. parser.add_argument("data")
  31. parser.add_argument("fit")
  32. parser.add_argument("output")
  33. parser.add_argument("--selected-corpus", type=int, default=-1)
  34. args = parser.parse_args()
  35. with open(args.data, "rb") as fp:
  36. data = pickle.load(fp)
  37. fit = np.load(args.fit)
  38. n_samples = fit["speech_rate_mu"].shape[0]
  39. # corpora = pd.read_csv("output/training_set.csv")
  40. # corpora = corpora["corpus"].map(basename).tolist()
  41. corpora = ["1", "2", "3", "4"]
  42. speakers = ["CHI", "OCH", "FEM", "MAL"]
  43. n_groups = data["n_groups"]
  44. n_corpora = data["n_corpora"]
  45. colors = ['#377eb8', '#ff7f00', '#4daf4a', '#f781bf']
  46. mu = np.zeros((data['n_corpora'], 4))
  47. mu_low = np.zeros((data['n_corpora'], 4))
  48. mu_high = np.zeros((data['n_corpora'], 4))
  49. inputs = np.zeros((data['n_corpora'], n_samples))
  50. input = np.zeros(data['n_corpora'])
  51. input_low = np.zeros(data['n_corpora'])
  52. input_high = np.zeros(data['n_corpora'])
  53. output_speech = np.zeros((data['n_corpora'], n_samples))
  54. input_speech = np.zeros((data['n_corpora'], n_samples))
  55. for c in range(data['n_corpora']):
  56. for i in range(4):
  57. mus = 1000*fit["speech_rate_mu"][:,i,c]
  58. mu[c,i] = np.mean(mus)
  59. mu_low[c,i] = np.quantile(mus, q=0.05/2)
  60. mu_high[c,i] = np.quantile(mus, q=1-0.05/2)
  61. inputs[c,:] += mus
  62. input[c] = inputs[c,:].mean()
  63. input_low[c] = np.quantile(inputs[c,:], q=0.05/2)
  64. input_high[c] = np.quantile(inputs[c,:], q=1-0.05/2)
  65. keep = [c not in ["winnipeg", "tsimane2017"] for c in corpora]
  66. corpora = [corpora[i] for i in range(len(corpora)) if keep[i]]
  67. mu = mu[keep,:]
  68. mu_low = mu_low[keep,:]
  69. mu_high = mu_high[keep,:]
  70. input = input[keep]
  71. input_low = input_low[keep]
  72. input_high = input_high[keep]
  73. print(corpora)
  74. print(mu.shape)
  75. fig, axes = plt.subplots(2, 2, sharex=True, sharey=True)
  76. for row in range (2):
  77. for col in range(2):
  78. i = row+2*col
  79. axes[row,col].scatter(np.arange(mu.shape[0]), mu[:,i])
  80. axes[row,col].errorbar(np.arange(mu.shape[0]), mu[:,i], (mu[:,i]-mu_low[:,i], mu_high[:,i]-mu[:,i]) ,ls="none")
  81. axes[row,col].set_title(speakers[i])
  82. axes[row,col].set_ylim(0,1200)
  83. axes[row,col].set_xticks(np.arange(mu.shape[0]))
  84. axes[row,col].set_xticklabels(corpora)
  85. axes[row,col].xaxis.set_tick_params(rotation=90)
  86. if col==0:
  87. axes[row,col].set_ylabel("voc/h")
  88. fig.suptitle("Latent population mean voc/h\n(human annotations)")
  89. fig.savefig("output/quantities.png", bbox_inches="tight")
  90. fig, axes = plt.subplots(1, 2, sharex=True, sharey=True)
  91. for col in range(2):
  92. if col == 0:
  93. axes[col].scatter(np.arange(mu.shape[0]), mu[:,0])
  94. axes[col].errorbar(np.arange(mu.shape[0]), mu[:,0], (mu[:,0]-mu_low[:,0], mu_high[:,0]-mu[:,0]) ,ls="none")
  95. else:
  96. axes[col].scatter(np.arange(len(input)), input)
  97. axes[col].errorbar(np.arange(len(input)), input, (input-input_low, input_high-input) ,ls="none")
  98. axes[col].set_title("output" if col == 0 else "input")
  99. axes[col].set_ylim(0,2000)
  100. axes[col].set_xticks(np.arange(mu.shape[0]))
  101. axes[col].set_xticklabels(corpora)
  102. axes[col].xaxis.set_tick_params(rotation=90)
  103. if col==0:
  104. axes[col].set_ylabel("voc/h")
  105. fig.suptitle("Latent population mean voc/h\n(human annotations)")
  106. fig.savefig("output/input_output.png", bbox_inches="tight")
  107. fig, ax = plt.subplots(1,1)
  108. for i in range(4):
  109. alphas = fit[f"speech_rate_alpha"][:,i,args.selected_corpus]
  110. scale = 1000*fit[f"speech_rate_mu"][:,i,args.selected_corpus]/alphas
  111. x = np.linspace(0,500,200,True)
  112. pdf = np.zeros((len(x), len(alphas)))
  113. for k in range(len(x)):
  114. pdf[k,:] = gamma.pdf(x[k], alphas, np.zeros(len(alphas)), scale)
  115. pdf_low = np.quantile(pdf, q=0.05, axis=1)
  116. pdf_high = np.quantile(pdf, q=0.95, axis=1)
  117. pdf_mean = np.mean(pdf, axis=1)
  118. ax.plot(x, pdf_mean, color=colors[i], label=speakers[i])
  119. ax.fill_between(x, pdf_low, pdf_high, color=colors[i], alpha=0.2)
  120. ax.set_xlim(0, 500)
  121. ax.set_xlabel("voc/h")
  122. # ax.axvline(np.mean(data), linestyle="--", linewidth=0.5, color="#333", alpha=1)
  123. # ax.text(0.5, 4.5, f"{low:.2f} - {high:.2f}", ha="center", va="center")
  124. ax.legend()
  125. corpus = corpora[args.selected_corpus]
  126. fig.suptitle(f"voc/h distribution for each kind of speaker ({corpus})")
  127. fig.savefig(f"output/speech_distribution_{corpus}.png", bbox_inches="tight")
  128. plt.show()