ownership.py 3.3 KB

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