ownership.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. from datetime import datetime, timedelta
  2. import json
  3. from typing import Any, Dict, List, Tuple
  4. import numpy
  5. from labours.plotting import apply_plot_style, deploy_plot, get_plot_path, import_pyplot
  6. from labours.utils import default_json, floor_datetime, import_pandas, parse_date
  7. def load_ownership(
  8. header: Tuple[int, int, int, int, float],
  9. sequence: List[Any],
  10. contents: Dict[Any, Any],
  11. max_people: int,
  12. order_by_time: bool,
  13. ):
  14. pandas = import_pandas()
  15. start, last, sampling, _, tick = header
  16. start = datetime.fromtimestamp(start)
  17. start = floor_datetime(start, tick)
  18. last = datetime.fromtimestamp(last)
  19. people = []
  20. for name in sequence:
  21. people.append(contents[name].sum(axis=1))
  22. people = numpy.array(people)
  23. date_range_sampling = pandas.date_range(
  24. start + timedelta(seconds=sampling * tick),
  25. periods=people[0].shape[0],
  26. freq="%dD" % sampling,
  27. )
  28. if people.shape[0] > max_people:
  29. chosen = numpy.argpartition(-numpy.sum(people, axis=1), max_people)
  30. others = people[chosen[max_people:]].sum(axis=0)
  31. people = people[chosen[: max_people + 1]]
  32. people[max_people] = others
  33. sequence = [sequence[i] for i in chosen[:max_people]] + ["others"]
  34. print("Warning: truncated people to the most owning %d" % max_people)
  35. if order_by_time:
  36. appearances = numpy.argmax(people > 0, axis=1)
  37. if people.shape[0] > max_people:
  38. appearances[-1] = people.shape[1]
  39. else:
  40. appearances = -people.sum(axis=1)
  41. if people.shape[0] > max_people:
  42. appearances[-1] = 0
  43. order = numpy.argsort(appearances)
  44. people = people[order]
  45. sequence = [sequence[i] for i in order]
  46. for i, name in enumerate(sequence):
  47. if len(name) > 40:
  48. sequence[i] = name[:37] + "..."
  49. return sequence, people, date_range_sampling, last
  50. def plot_ownership(args, repo, names, people, date_range, last):
  51. if args.output and args.output.endswith(".json"):
  52. data = locals().copy()
  53. del data["args"]
  54. data["type"] = "ownership"
  55. if args.mode == "all" and args.output:
  56. output = get_plot_path(args.output, "people")
  57. else:
  58. output = args.output
  59. with open(output, "w") as fout:
  60. json.dump(data, fout, sort_keys=True, default=default_json)
  61. return
  62. matplotlib, pyplot = import_pyplot(args.backend, args.style)
  63. polys = pyplot.stackplot(date_range, people, labels=names)
  64. if names[-1] == "others":
  65. polys[-1].set_hatch("/")
  66. pyplot.xlim(
  67. parse_date(args.start_date, date_range[0]), parse_date(args.end_date, last)
  68. )
  69. if args.relative:
  70. for i in range(people.shape[1]):
  71. people[:, i] /= people[:, i].sum()
  72. pyplot.ylim(0, 1)
  73. legend_loc = 3
  74. else:
  75. legend_loc = 2
  76. ncol = 1 if len(names) < 15 else 2
  77. legend = pyplot.legend(loc=legend_loc, fontsize=args.font_size, ncol=ncol)
  78. apply_plot_style(
  79. pyplot.gcf(), pyplot.gca(), legend, args.background, args.font_size, args.size
  80. )
  81. if args.mode == "all" and args.output:
  82. output = get_plot_path(args.output, "people")
  83. else:
  84. output = args.output
  85. deploy_plot("%s code ownership through time" % repo, output, args.background)