ChannelCumPainter.py 2.5 KB

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