ChannelPainter.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. from .DataStream import EChPosmap as pos_map, day_stamp, span_days, time_border, calc_interval
  2. from .ChannelReader import ChannelReader
  3. from .PainterBase import PainterBase
  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('ChannelPainter')
  13. _all_channels = set()
  14. def _add_channel(channel):
  15. if channel not in _all_channels:
  16. _all_channels.add(channel)
  17. def get_channels():
  18. logger.debug(_all_channels)
  19. return list(_all_channels)
  20. class ChPathFilter(object):
  21. def __init__(self,chnames: set = None, card_types: set = None,spec: int = None,show_all: bool=False):
  22. self._chnames = chnames
  23. self._card_types = card_types
  24. self._spec = spec
  25. self._show_all = show_all
  26. pass
  27. def ratio_pathes(reader: ChannelReader, tuple_pathes: dict, days: list, spec=None):
  28. count = len(days)
  29. show_detail = True if len(list(tuple_pathes.keys())) == 1 else False
  30. if show_detail == False:
  31. all_datas = reader.init_data(count)
  32. else:
  33. all_datas = None
  34. for name, tup in tuple_pathes.items():
  35. _add_channel(name)
  36. mch_datas = reader.init_data(count)
  37. for _card_type, _spec in tup:
  38. if spec is not None and _spec != spec:
  39. continue
  40. if show_detail:
  41. detail_datas = reader.init_data(count)
  42. else:
  43. detail_datas = None
  44. for i, day in enumerate(days):
  45. data = reader.read(day, name, _card_type, _spec)
  46. if data is not None:
  47. column_pos = i * 86400
  48. view = mch_datas[:, column_pos:column_pos + 86400]
  49. view += data
  50. if show_detail:
  51. view = detail_datas[:, column_pos:column_pos + 86400]
  52. view += data
  53. if show_detail:
  54. yield name, _card_type, _spec, detail_datas
  55. if all_datas is not None:
  56. all_datas += mch_datas
  57. yield name, None, None, mch_datas
  58. if show_detail == False:
  59. yield 'all', None, None, all_datas
  60. class ChannelPainter(PainterBase):
  61. def _fig_funs(self):
  62. def create():
  63. fig = Figure(figsize=(19, 8))
  64. ax = fig.subplots()
  65. ax.set_title('success ratio')
  66. ax.set(xlabel='time', ylabel='ratio')
  67. return ax, fig
  68. def flush(ax, fig, ticks, lables):
  69. ax.yaxis.set_major_formatter(ticker.PercentFormatter(xmax=1, decimals=4))
  70. ax.set_xticks(ticks=ticks)
  71. ax.set_xticklabels(lables)
  72. fig.autofmt_xdate()
  73. ax.grid()
  74. ax.legend(bbox_to_anchor=(1, 1), loc='upper left')
  75. fig.tight_layout()
  76. buf = BytesIO()
  77. fig.savefig(buf, format="png")
  78. return buf
  79. return create, flush
  80. def _label(self, chname, succ, count, card_type=None, spec=None):
  81. _card_type = None
  82. if card_type == 1:
  83. _card_type = 'SY'
  84. elif card_type == 2:
  85. _card_type = 'SH'
  86. elif card_type == 4:
  87. _card_type = 'YD'
  88. elif card_type == 5:
  89. _card_type = 'LT'
  90. elif card_type == 6:
  91. _card_type = 'DX'
  92. elif card_type == 7:
  93. _card_type = 'TH'
  94. lable = f"{chname}"
  95. if _card_type is not None:
  96. lable += f"-{_card_type}"
  97. if spec is not None:
  98. lable += f"-{spec}"
  99. if count > 0:
  100. ratio = round(succ * 100 / count, 5)
  101. else:
  102. ratio = 0.00
  103. lable += f":{succ}/{count}={ratio}%"
  104. return lable, ratio