overwrites.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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(["Unidentified"] + people, rotation=45, ha="left",
  45. va="bottom", rotation_mode="anchor")
  46. ax.set_yticks(numpy.arange(0.5, matrix.shape[0] + 0.5), minor=True)
  47. ax.grid(False)
  48. ax.grid(which="minor")
  49. apply_plot_style(fig, ax, None, args.background, args.font_size, args.size)
  50. if not args.output:
  51. pos1 = ax.get_position()
  52. pos2 = (pos1.x0 + 0.15, pos1.y0 - 0.1, pos1.width * 0.9, pos1.height * 0.9)
  53. ax.set_position(pos2)
  54. if args.mode == "all" and args.output:
  55. output = get_plot_path(args.output, "matrix")
  56. else:
  57. output = args.output
  58. title = "%s %d developers overwrite" % (repo, matrix.shape[0])
  59. if args.output:
  60. # FIXME(vmarkovtsev): otherwise the title is screwed in savefig()
  61. title = ""
  62. deploy_plot(title, output, args.background)