ChannelPainter.py 3.5 KB

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