overwrites.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import json
  2. import numpy
  3. from labours.plotting import apply_plot_style, deploy_plot, get_plot_path, import_pyplot
  4. from labours.utils import default_json
  5. def load_overwrites_matrix(people, matrix, max_people, normalize=True):
  6. matrix = matrix.astype(float)
  7. if matrix.shape[0] > max_people:
  8. order = numpy.argsort(-matrix[:, 0])
  9. matrix = matrix[order[:max_people]][:, [0, 1] + list(2 + order[:max_people])]
  10. people = [people[i] for i in order[:max_people]]
  11. print("Warning: truncated people to most productive %d" % max_people)
  12. if normalize:
  13. zeros = matrix[:, 0] == 0
  14. matrix[zeros, :] = 1
  15. matrix /= matrix[:, 0][:, None]
  16. matrix[zeros, :] = 0
  17. matrix = -matrix[:, 1:]
  18. for i, name in enumerate(people):
  19. if len(name) > 40:
  20. people[i] = name[:37] + "..."
  21. return people, matrix
  22. def plot_overwrites_matrix(args, repo, people, matrix):
  23. if args.output and args.output.endswith(".json"):
  24. data = locals().copy()
  25. del data["args"]
  26. data["type"] = "overwrites_matrix"
  27. if args.mode == "all":
  28. output = get_plot_path(args.output, "matrix")
  29. else:
  30. output = args.output
  31. with open(output, "w") as fout:
  32. json.dump(data, fout, sort_keys=True, default=default_json)
  33. return
  34. matplotlib, pyplot = import_pyplot(args.backend, args.style)
  35. s = 4 + matrix.shape[1] * 0.3
  36. fig = pyplot.figure(figsize=(s, s))
  37. ax = fig.add_subplot(111)
  38. ax.xaxis.set_label_position("top")
  39. ax.matshow(matrix, cmap=pyplot.cm.OrRd)
  40. ax.set_xticks(numpy.arange(0, matrix.shape[1]))
  41. ax.set_yticks(numpy.arange(0, matrix.shape[0]))
  42. ax.set_yticklabels(people, va="center")
  43. ax.set_xticks(numpy.arange(0.5, matrix.shape[1] + 0.5), minor=True)
  44. ax.set_xticklabels(
  45. ["Unidentified"] + people,
  46. rotation=45,
  47. ha="left",
  48. va="bottom",
  49. rotation_mode="anchor",
  50. )
  51. ax.set_yticks(numpy.arange(0.5, matrix.shape[0] + 0.5), minor=True)
  52. ax.grid(False)
  53. ax.grid(which="minor")
  54. apply_plot_style(fig, ax, None, args.background, args.font_size, args.size)
  55. if not args.output:
  56. pos1 = ax.get_position()
  57. pos2 = (pos1.x0 + 0.15, pos1.y0 - 0.1, pos1.width * 0.9, pos1.height * 0.9)
  58. ax.set_position(pos2)
  59. if args.mode == "all" and args.output:
  60. output = get_plot_path(args.output, "matrix")
  61. else:
  62. output = args.output
  63. title = "%s %d developers overwrite" % (repo, matrix.shape[0])
  64. if args.output:
  65. # FIXME(vmarkovtsev): otherwise the title is screwed in savefig()
  66. title = ""
  67. deploy_plot(title, output, args.background)