from .DataStream import EChPosmap as pos_map from .ChannelReader import ChannelReader from .ChannelPainter import ChannelPainter, ratio_pathes from matplotlib.figure import Figure from matplotlib import ticker from io import BytesIO import numpy as np from .algorithm import calc_cov_chsuccs import time as time import logging logger = logging.getLogger('ChannelCumPainter') class ChannelCovSuccPainter(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() filter_wave = filter_wave or 3600 self._chnames, self._card_types, self._spec, self._filter_wave = chnames, card_types, spec, filter_wave days, self._start_time, self._end_time, self._interval = self.calc_time(self._reader, start_time, end_time) pass def paint(self): reader = self._reader days, _start_time, _end_time, _interval = self.calc_time(self._reader, self._start_time - self._filter_wave, self._end_time) if len(days) == 0: return BytesIO() tuple_pathes = reader.many_tuple_path(days, self._chnames, self._card_types, self._spec) gen = ratio_pathes(reader, tuple_pathes, days, self._spec) day_stamp = 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)]) window = np.ones(self._filter_wave) left_len = self._start_time - _start_time right_len = 0 stime = lambda t: time.strftime('%d-%H:%M:%S', time.localtime(t)) logger.debug("start_time %d, %s end_time=%s left_len=%d right_len=%d", self._start_time, stime(self._start_time), stime(self._end_time),left_len,right_len) chname_ratios = [] for _chname, _card_type, _spec, _data in gen: succ, commit,succs,counts = calc_cov_chsuccs(_data, pos_map, _start_time - day_stamp, _end_time - day_stamp, window, left_len,right_len) label, ratio = self._label(chname=_chname, succ=succs, count=counts, card_type=_card_type, spec=_spec) ax.plot(x, succ, ls='-', label=label) ax.plot(x, commit / 100, ls='--', label=label) if _card_type is None and _spec is None and _chname != 'all': chname_ratios.append((_chname, ratio)) ticks = self.calc_xticks(self._start_time, self._end_time, 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 def _fig_funs(self): def create(): fig = Figure(figsize=(19, 8)) ax = fig.subplots() ax.set_title('success count') ax.set(xlabel='time', ylabel='count') return ax, fig def flush(ax, fig, ticks, lables): ax.set_xticks(ticks=ticks) ax.set_xticklabels(lables) fig.autofmt_xdate() ax.grid() ax.legend(bbox_to_anchor=(1, 1), loc='upper left') fig.tight_layout() buf = BytesIO() fig.savefig(buf, format="png") return buf return create, flush