confusion_matrix.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env python3
  2. from ChildProject.projects import ChildProject
  3. from ChildProject.annotations import AnnotationManager
  4. from ChildProject.metrics import segments_to_grid, conf_matrix
  5. import numpy as np
  6. import pandas as pd
  7. from sklearn.metrics import confusion_matrix
  8. from sklearn.preprocessing import normalize
  9. import seaborn as sns
  10. import matplotlib.pyplot as plt
  11. import sys
  12. speakers = ['CHI', 'OCH', 'FEM', 'MAL']
  13. path = sys.argv[1]
  14. if __name__ == '__main__':
  15. project = ChildProject(path)
  16. am = AnnotationManager(project)
  17. am.read()
  18. intersection = AnnotationManager.intersection(am.annotations, ['vtc', 'eaf'])
  19. segments = am.get_collapsed_segments(intersection)
  20. segments = segments[segments['speaker_type'].isin(speakers)]
  21. vtc = segments_to_grid(segments[segments['set'] == 'vtc'], 0, segments['segment_offset'].max(), 100, 'speaker_type', speakers)
  22. eaf = segments_to_grid(segments[segments['set'] == 'eaf'], 0, segments['segment_offset'].max(), 100, 'speaker_type', speakers)
  23. speakers.extend(['none'])
  24. confusion_counts = conf_matrix(vtc, eaf)
  25. plt.rcParams.update({'font.size': 12})
  26. plt.rc('xtick', labelsize = 10)
  27. plt.rc('ytick', labelsize = 10)
  28. fig, axes = plt.subplots(nrows = 1, ncols = 2, figsize=(6.4*2, 4.8))
  29. confusion = confusion_counts/np.sum(vtc, axis = 0)[:,None]
  30. sns.heatmap(confusion, annot = True, fmt = '.2f', ax = axes[0], cmap = 'Reds')
  31. axes[0].set_xlabel('eaf')
  32. axes[0].set_ylabel('vtc')
  33. axes[0].xaxis.set_ticklabels(speakers)
  34. axes[0].yaxis.set_ticklabels(speakers)
  35. confusion_counts = np.transpose(confusion_counts)
  36. confusion = confusion_counts/np.sum(eaf, axis = 0)[:,None]
  37. sns.heatmap(confusion, annot = True, fmt = '.2f', ax = axes[1], cmap = 'Reds')
  38. axes[1].set_xlabel('vtc')
  39. axes[1].set_ylabel('eaf')
  40. axes[1].xaxis.set_ticklabels(speakers)
  41. axes[1].yaxis.set_ticklabels(speakers)
  42. plt.savefig('Fig5.pdf', bbox_inches = 'tight')