old_vs_new.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from argparse import Namespace
  2. from datetime import datetime, timedelta
  3. from itertools import chain
  4. from typing import Dict, List
  5. import numpy
  6. from labours.objects import DevDay
  7. from labours.plotting import deploy_plot, get_plot_path, import_pyplot
  8. def show_old_vs_new(
  9. args: Namespace,
  10. name: str,
  11. start_date: int,
  12. end_date: int,
  13. people: List[str],
  14. days: Dict[int, Dict[int, DevDay]],
  15. ) -> None:
  16. from scipy.signal import convolve, slepian
  17. start_date = datetime.fromtimestamp(start_date)
  18. start_date = datetime(start_date.year, start_date.month, start_date.day)
  19. end_date = datetime.fromtimestamp(end_date)
  20. end_date = datetime(end_date.year, end_date.month, end_date.day)
  21. new_lines = numpy.zeros((end_date - start_date).days + 2)
  22. old_lines = numpy.zeros_like(new_lines)
  23. for day, devs in days.items():
  24. for stats in devs.values():
  25. new_lines[day] += stats.Added
  26. old_lines[day] += stats.Removed + stats.Changed
  27. resolution = 32
  28. window = slepian(max(len(new_lines) // resolution, 1), 0.5)
  29. new_lines = convolve(new_lines, window, "same")
  30. old_lines = convolve(old_lines, window, "same")
  31. matplotlib, pyplot = import_pyplot(args.backend, args.style)
  32. plot_x = [start_date + timedelta(days=i) for i in range(len(new_lines))]
  33. pyplot.fill_between(plot_x, new_lines, color="#8DB843", label="Changed new lines")
  34. pyplot.fill_between(
  35. plot_x, old_lines, color="#E14C35", label="Changed existing lines"
  36. )
  37. pyplot.legend(loc=2, fontsize=args.font_size)
  38. for tick in chain(
  39. pyplot.gca().xaxis.get_major_ticks(), pyplot.gca().yaxis.get_major_ticks()
  40. ):
  41. tick.label.set_fontsize(args.font_size)
  42. if args.mode == "all" and args.output:
  43. output = get_plot_path(args.output, "old_vs_new")
  44. else:
  45. output = args.output
  46. deploy_plot("Additions vs changes", output, args.background)