12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- from .DataStream import EChPosmap as pos_map, day_stamp, span_days, time_border, calc_interval
- from .ChannelReader import ChannelReader
- from .ChannelPainter import ChannelPainter
- from collections import defaultdict
- from matplotlib.figure import Figure
- from matplotlib import ticker
- from io import BytesIO
- import numpy as np
- from .algorithm import calc_chratios
- import time as time
- import logging
- logger = logging.getLogger('ChannelCumPainter')
- class ChannelCumPainter(ChannelPainter):
- def __init__(self, start_time: int, end_time: int, chnames: set = None, card_types: set = None, spec: int = None, filter_wave: int = None):
- self._reader = ChannelReader()
- self._chnames, self._card_types, self._spec, self._filter_wave = chnames, card_types, spec, filter_wave
- self._days, self._start_time, self._end_time = self.calc_time(self._reader, start_time, end_time)
- self._interval = calc_interval(start_time, end_time)
- pass
- def paint(self):
- reader = ChannelReader()
- tuple_pathes = reader.many_tuple_path(self._days, self._chnames, self._card_types, self._spec)
- gen = ratio_pathes(self._reader, tuple_pathes, self._days, self._spec)
- if len(self._days) == 0:
- return BytesIO()
- day_stamp = self._days[0]
- fig_create, fig_flush = self._fig_funs()
- ax, fig = fig_create()
- x = np.array([d - self._start_time for d in range(self._start_time, self._end_time)])
- if self._filter_wave is not None and self._filter_wave > 1:
- window = np.ones(self._filter_wave) / self._filter_wave
- else:
- window = None
- chname_ratios = []
- for _chname, _card_type, _spec, _data in gen:
- succ, count, y = calc_chratios(_data, pos_map, self._start_time - day_stamp, self._end_time - day_stamp)
- y = np.convolve(y, window, 'same') if window is not None else y
- label, ratio = self._label(chname=_chname, succ=succ, count=count, card_type=_card_type, spec=_spec)
- ax.plot(x, y, ls='-', label=label)
- if _card_type is None and _spec is None and _chname != 'all':
- chname_ratios.append((_chname, ratio))
- ticks = [d - self._start_time for d in range(self._start_time, self._end_time + 1, self._interval)]
- xlables = [time.strftime('%d-%H:%M:%S', time.localtime(d + self._start_time)) for d in ticks]
- buf = fig_flush(ax, fig, ticks, xlables)
- chname_ratios = sorted(chname_ratios, key=lambda x: (x[1], x[0]), reverse=True)
- result = []
- for name, ratio in chname_ratios:
- result.append(f'{name}:{ratio}')
- return buf, result
|