ChannelCumPainter.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. from .DataStream import EChPosmap as pos_map, day_stamp, span_days, time_border, calc_interval
  2. from .ChannelReader import ChannelReader
  3. from .ChannelPainter import ChannelPainter
  4. from collections import defaultdict
  5. from matplotlib.figure import Figure
  6. from matplotlib import ticker
  7. from io import BytesIO
  8. import numpy as np
  9. from .algorithm import calc_chratios
  10. import time as time
  11. import logging
  12. logger = logging.getLogger('ChannelCumPainter')
  13. class ChannelCumPainter(ChannelPainter):
  14. def __init__(self, start_time: int, end_time: int, chnames: set = None, card_types: set = None, spec: int = None, filter_wave: int = None):
  15. self._reader = ChannelReader()
  16. self._chnames, self._card_types, self._spec, self._filter_wave = chnames, card_types, spec, filter_wave
  17. self._days, self._start_time, self._end_time = self.calc_time(self._reader, start_time, end_time)
  18. self._interval = calc_interval(start_time, end_time)
  19. pass
  20. def paint(self):
  21. reader = ChannelReader()
  22. tuple_pathes = reader.many_tuple_path(self._days, self._chnames, self._card_types, self._spec)
  23. gen = ratio_pathes(self._reader, tuple_pathes, self._days, self._spec)
  24. if len(self._days) == 0:
  25. return BytesIO()
  26. day_stamp = self._days[0]
  27. fig_create, fig_flush = self._fig_funs()
  28. ax, fig = fig_create()
  29. x = np.array([d - self._start_time for d in range(self._start_time, self._end_time)])
  30. if self._filter_wave is not None and self._filter_wave > 1:
  31. window = np.ones(self._filter_wave) / self._filter_wave
  32. else:
  33. window = None
  34. chname_ratios = []
  35. for _chname, _card_type, _spec, _data in gen:
  36. succ, count, y = calc_chratios(_data, pos_map, self._start_time - day_stamp, self._end_time - day_stamp)
  37. y = np.convolve(y, window, 'same') if window is not None else y
  38. label, ratio = self._label(chname=_chname, succ=succ, count=count, card_type=_card_type, spec=_spec)
  39. ax.plot(x, y, ls='-', label=label)
  40. if _card_type is None and _spec is None and _chname != 'all':
  41. chname_ratios.append((_chname, ratio))
  42. ticks = [d - self._start_time for d in range(self._start_time, self._end_time + 1, self._interval)]
  43. xlables = [time.strftime('%d-%H:%M:%S', time.localtime(d + self._start_time)) for d in ticks]
  44. buf = fig_flush(ax, fig, ticks, xlables)
  45. chname_ratios = sorted(chname_ratios, key=lambda x: (x[1], x[0]), reverse=True)
  46. result = []
  47. for name, ratio in chname_ratios:
  48. result.append(f'{name}:{ratio}')
  49. return buf, result