correlations.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. from email.errors import MalformedHeaderDefect
  2. from logging import logMultiprocessing
  3. import pandas as pd
  4. import numpy as np
  5. from matplotlib import pyplot as plt
  6. import matplotlib
  7. matplotlib.use("pgf")
  8. matplotlib.rcParams.update(
  9. {
  10. "pgf.texsystem": "pdflatex",
  11. "font.family": "serif",
  12. "font.serif": "Times New Roman",
  13. "text.usetex": True,
  14. "pgf.rcfonts": False,
  15. }
  16. )
  17. import pickle
  18. from os.path import join as opj
  19. import argparse
  20. parser = argparse.ArgumentParser()
  21. parser.add_argument("--vtc")
  22. parser.add_argument("--lena")
  23. args = parser.parse_args()
  24. data = {
  25. "truth": "output/aggregates_lena_age24_human.pickle",
  26. "lena_raw": "output/aggregates_lena_age24_algo.pickle",
  27. "vtc_raw": "output/aggregates_vtc_age24_algo.pickle",
  28. "lena_calibrated": "output/aggregates_lena_age24_dev_siblings_binomial_hurdle_fast.pickle",
  29. "vtc_calibrated": "output/aggregates_vtc_age24_dev_siblings_binomial_hurdle_fast.pickle",
  30. }
  31. for key in data:
  32. with open(data[key], "rb") as f:
  33. data[key] = pickle.load(f)
  34. samples = {
  35. "truth": np.load("output/aggregates_lena_age24_human.npz"),
  36. "lena_raw": np.load("output/aggregates_lena_age24_algo.npz"),
  37. "vtc_raw": np.load("output/aggregates_vtc_age24_algo.npz"),
  38. # "lena_calibrated": np.load("output/aggregates_lena_dev_siblings_effect.npz"),
  39. # "vtc_calibrated": np.load("output/aggregates_vtc_dev_siblings_effect.npz"),
  40. "lena_calibrated": np.load("output/aggregates_lena_age24_dev_siblings_binomial_hurdle_fast.npz"),
  41. "vtc_calibrated": np.load("output/aggregates_vtc_age24_dev_siblings_binomial_hurdle_fast.npz")
  42. }
  43. labels = {
  44. "prior": "Prior",
  45. "truth": "Manual annotations",
  46. "lena_raw": "LENA (uncalibrated)",
  47. "vtc_raw": "VTC (uncalibrated)",
  48. "lena_calibrated": "LENA (calibrated)",
  49. "vtc_calibrated": "VTC (calibrated)"
  50. }
  51. speakers = ["CHI", "OCH", "FEM", "MAL"]
  52. cb_colors = [
  53. "#377eb8",
  54. "#ff7f00",
  55. "#f781bf",
  56. "#4daf4a",
  57. "#a65628",
  58. "#984ea3",
  59. "#999999",
  60. "#e41a1c",
  61. "#dede00",
  62. ]
  63. corpora = {
  64. 'bergelson': 0, 'cougar': 1, 'fausey-trio': 2, 'lucid': 3, 'warlaumont': 4, 'winnipeg': 5
  65. }
  66. corpora_names = {
  67. corpora[corpus]: corpus
  68. for corpus in corpora
  69. }
  70. def correlations(data, samples):
  71. # keep_calibration = (data["group_corpus"]==1)|(data["group_corpus"]==4)
  72. # calibration_truth = data["speech_rates"][keep_calibration,:]
  73. vtc = data["vtc_calibrated"]["vocs"]
  74. lena = data["lena_calibrated"]["vocs"]
  75. truth_vtc = samples["vtc_calibrated"]["truth_vocs"]
  76. truth_lena = samples["lena_calibrated"]["truth_vocs"]
  77. n_samples_vtc = truth_vtc.shape[0]
  78. n_samples_lena = truth_lena.shape[0]
  79. fig, axes = plt.subplots(nrows=3, ncols=3, sharex=True, sharey=True, figsize=([9.6, 9.6]))
  80. # FEM,MAL
  81. # OCH,FEM OCH,MAL
  82. # CHI,OCH CHI,FEM CHI,MAL
  83. for i in range(3):
  84. for j in range(3):
  85. if i<2-j:
  86. axes[i, j].axis('off')
  87. continue
  88. a = 2-i
  89. b = j+1
  90. vtc_r = np.corrcoef(vtc[:,a], vtc[:,b])[0,1]
  91. lena_r = np.corrcoef(lena[:,a], lena[:,b])[0,1]
  92. # calibration_r = np.corrcoef(calibration_truth[:,a], calibration_truth[:,b])[0,1]
  93. truth_vtc_r = [np.corrcoef(truth_vtc[k,:,a], truth_vtc[k,:,b])[0,1] for k in range(n_samples_vtc)]
  94. truth_lena_r = [np.corrcoef(truth_lena[k,:,a], truth_lena[k,:,b])[0,1] for k in range(n_samples_lena)]
  95. bins = np.linspace(-1,1,100)
  96. low_vtc = np.quantile(truth_vtc_r,q=0.05/2)
  97. high_vtc = np.quantile(truth_vtc_r,q=1-0.05/2)
  98. r_vtc = np.mean(truth_vtc_r)
  99. low_lena = np.quantile(truth_lena_r,q=0.05/2)
  100. high_lena = np.quantile(truth_lena_r,q=1-0.05/2)
  101. r_lena = np.mean(truth_lena_r)
  102. axes[i,j].axvline(vtc_r, color=cb_colors[0], lw=1, ls="dashed")
  103. axes[i,j].axvline(lena_r, color=cb_colors[1], lw=1, ls="dashed")
  104. # axes[i,j].axvline(calibration_r, color="olive", lw=0.5, ls="dashed")
  105. axes[i,j].hist(truth_vtc_r, bins=bins, histtype="step", density=True, color=cb_colors[0])
  106. axes[i,j].text(
  107. 1-0.05, 0.95, "\\scriptsize\\textbf{VTC}:", ha="right", transform=axes[i,j].transAxes, color=cb_colors[0]
  108. )
  109. axes[i,j].text(
  110. 1-0.05, 0.9,
  111. f"\\scriptsize $r(\\mathrm{{{speakers[a]}}},\\mathrm{{{speakers[b]}}})={r_vtc:.2f}$",
  112. ha="right",
  113. transform=axes[i,j].transAxes,
  114. color="black"
  115. )
  116. axes[i,j].text(
  117. 1-0.05, 0.85,
  118. f"\\scriptsize $\\mathrm{{CI}}_{{95\\%}}$[{low_vtc:.2f}, {high_vtc:.2f}]",
  119. ha="right",
  120. transform = axes[i,j].transAxes,
  121. color="black"
  122. )
  123. axes[i,j].hist(truth_lena_r, bins=bins, histtype="step", density=True, color=cb_colors[1])
  124. axes[i,j].text(
  125. 1-0.05, 0.7, "\\scriptsize\\textbf{LENA}:", ha="right", transform=axes[i,j].transAxes, color=cb_colors[1]
  126. )
  127. axes[i,j].text(
  128. 1-0.05, 0.65,
  129. f"\\scriptsize $r(\\mathrm{{{speakers[a]}}},\\mathrm{{{speakers[b]}}})={r_lena:.2f}$",
  130. ha="right",
  131. transform=axes[i,j].transAxes,
  132. color="black"
  133. )
  134. axes[i,j].text(
  135. 1-0.05, 0.6,
  136. f"\\scriptsize $\\mathrm{{CI}}_{{95\\%}}$[{low_lena:.2f}, {high_lena:.2f}]",
  137. ha="right",
  138. transform = axes[i,j].transAxes,
  139. color="black"
  140. )
  141. axes[i,j].text(
  142. 1-0.05, 0.8,
  143. f"\\scriptsize $r(\\mathrm{{uncalib.}})={vtc_r:.2f}$",
  144. ha="right",
  145. transform = axes[i,j].transAxes,
  146. color="black"
  147. )
  148. axes[i,j].text(
  149. 1-0.05, 0.55,
  150. f"\\scriptsize $r(\\mathrm{{uncalib.}})={lena_r:.2f}$",
  151. ha="right",
  152. transform = axes[i,j].transAxes,
  153. color="black"
  154. )
  155. # axes[i,j].text(
  156. # 0.05, 0.6,
  157. # f"\\scriptsize $r(\\mathrm{{human}})={calibration_r:.2f}\\ast$",
  158. # ha="left",
  159. # transform = axes[i,j].transAxes,
  160. # color="olive"
  161. # )
  162. axes[i,j].set_yticks([])
  163. axes[i,j].set_yticklabels([])
  164. axes[i,j].set_xlim(-0.25, 0.8)
  165. plt.subplots_adjust(wspace=0.05, hspace=0.05)
  166. fig.savefig("output/correlations_all.eps", bbox_inches="tight")
  167. fig.savefig("output/correlations_all.png", bbox_inches="tight", dpi=720)
  168. correlations(data, samples)