sample.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/env python3
  2. from ChildProject.projects import ChildProject
  3. from ChildProject.annotations import AnnotationManager
  4. import matplotlib
  5. import matplotlib.pyplot as plt
  6. matplotlib.use("pgf")
  7. matplotlib.rcParams.update({
  8. "pgf.texsystem": "pdflatex",
  9. 'font.family': 'serif',
  10. "font.serif" : "Times New Roman",
  11. 'text.usetex': True,
  12. 'pgf.rcfonts': False,
  13. })
  14. import numpy as np
  15. import os
  16. import pandas as pd
  17. import librosa
  18. if __name__ == "__main__":
  19. project = ChildProject("vandam-data")
  20. am = AnnotationManager(project)
  21. am.read()
  22. annotations = AnnotationManager.intersection(
  23. am.annotations, ["its", "cha", "eaf"]
  24. )
  25. annotations["converted_filename"] = annotations["recording_filename"].apply(
  26. lambda f: project.get_converted_recording_filename("standard", f)
  27. )
  28. annotations = annotations[
  29. annotations["range_onset"] == annotations["range_onset"].iloc[-1]
  30. ]
  31. annotations['range_onset'] += 20000
  32. annotations["range_offset"] = annotations["range_onset"] + 5000
  33. range_onset = annotations["range_onset"].iloc[0]
  34. range_offset = annotations["range_offset"].iloc[0]
  35. signal, sr = librosa.load(
  36. os.path.join(
  37. project.path,
  38. "recordings/converted/standard",
  39. annotations["converted_filename"].iloc[0],
  40. ),
  41. sr=8000,
  42. offset=range_onset / 1000,
  43. duration=(range_offset - range_onset) / 1000,
  44. )
  45. time = np.arange(
  46. range_onset / 1000,
  47. range_offset / 1000,
  48. 1 / sr,
  49. )
  50. plt.plot(time, .5*signal, color = 'black')
  51. positions = {"eaf": -0.4, "cha": -0.6, "its": -0.8}
  52. annotators = {"its": '\\textbf{LENA}', 'cha': '\\textbf{Annotator 2}\n\\textbf{(CHAT)}', 'eaf': '\\textbf{Annotator 1}\n\\textbf{(ELAN)}'}
  53. colors = {"MAL": "red", "FEM": "blue", "CHI": "green"}
  54. speakers = {"MAL": "male adult", "FEM": "female adult", "CHI": "key child"}
  55. ids = {'MA1': 'Father', 'FA1': 'Mother'}
  56. segments = am.get_segments(annotations)
  57. for segment in segments.to_dict(orient="records"):
  58. speaker_type = segment["speaker_type"]
  59. if speaker_type not in ["MAL", "FEM", "CHI"]:
  60. continue
  61. t1 = segment["segment_onset"] / 1000
  62. t2 = segment["segment_offset"] / 1000
  63. y = positions[segment["set"]]
  64. plt.plot([t1, t2], [y, y], color=colors[speaker_type], marker = "|")
  65. if segment["set"] == "cha":
  66. transcription = segment['transcription']
  67. if len(transcription) > 20:
  68. transcription = transcription[:20] + '...'
  69. text = f"``{transcription}''"
  70. elif segment["set"] == "its":
  71. text = '{}, {} words'.format(
  72. speakers[speaker_type], int(segment["words"])
  73. )
  74. else:
  75. text = '{}'.format(ids[segment['speaker_id']])
  76. plt.text(t1, y + 0.05, text)
  77. plt.text(range_onset/1000-0.8, 0, '\\textbf{Audio}', ha = 'center')
  78. for set in positions:
  79. y = positions[set]
  80. plt.text(range_onset/1000-0.8, y, annotators[set], ha = 'center')
  81. plt.axis("off")
  82. plt.savefig("Fig2.pdf", bbox_inches = 'tight')