ChannelPainter.py 3.5 KB

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