plot.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import signal
  2. import sys
  3. from matplotlib import pyplot as plt
  4. import asyncio
  5. import os
  6. import time
  7. file_data = {}
  8. axes = {}
  9. labels = {}
  10. def tryParseInt(value):
  11. if value is None or value == "":
  12. return value, False
  13. try:
  14. return int(value), True
  15. except ValueError:
  16. return value, False
  17. def tryParseIntList(list):
  18. new_vars = []
  19. for i in list:
  20. y, success = tryParseInt(i)
  21. if not success:
  22. new_vars = None
  23. break
  24. new_vars.append(y)
  25. return new_vars
  26. async def follow(path):
  27. with open(path, "r") as f:
  28. labels[path] = f.readline().split("\t")
  29. while True:
  30. line = f.readline()
  31. if not line:
  32. # print("nothing new")
  33. await asyncio.sleep(0.1)
  34. continue
  35. yield line
  36. print("returned follow")
  37. def init_plt(paths):
  38. plt.ion()
  39. fig = plt.figure()
  40. nrows = 1
  41. ncolumns = 1
  42. index = 1
  43. while nrows * ncolumns < len(paths):
  44. nrows += 1
  45. if nrows * ncolumns >= len(paths):
  46. break
  47. ncolumns += 1
  48. for key in paths:
  49. axes[key] = fig.add_subplot(nrows, ncolumns, index)
  50. index += 1
  51. plt.subplots_adjust(hspace=0.5)
  52. return fig
  53. def get_files():
  54. return ["plots/{}".format(p) for p in [f for f in os.listdir("plots") if f.endswith(".tsv")]]
  55. async def read_file_data(path):
  56. print(f"reading data for {path}")
  57. lines = follow(path)
  58. xs = []
  59. ys = []
  60. async for line in lines:
  61. line_data = line.split("\t")
  62. x, success = tryParseInt(line_data[0])
  63. if success:
  64. xs.append(x / 1000)
  65. rest = tryParseIntList(line_data[1:])
  66. if rest is not None:
  67. if len(ys) == 0:
  68. ys = [[i] for i in rest]
  69. else:
  70. for i in range(len(rest)):
  71. ys[i].append(rest[i])
  72. file_data[path] = (xs, ys)
  73. # print("added to file_data")
  74. print("read all lines")
  75. async def plot_loop():
  76. print("before plot loop")
  77. while True:
  78. if len(axes) > 0:
  79. for key, ax in axes.items():
  80. if len(file_data) > 0:
  81. xs, ys = file_data[key]
  82. # print("reading file data")
  83. ax.clear()
  84. for i in range(len(ys)):
  85. y = ys[i]
  86. ax.plot(xs, y, label=labels[key][i + 1], linewidth=0.7)
  87. ax.set_title(key)
  88. ax.set_xlabel(labels[key][0])
  89. left, right = ax.get_xlim()
  90. if right - left > 10:
  91. ax.set_xlim(right - 10, right)
  92. plt.legend()
  93. plt.pause(0.2)
  94. await asyncio.sleep(0.2)
  95. # await asyncio.sleep(1)
  96. async def test():
  97. print("abc")
  98. def signal_handler(signal, frame):
  99. loop.stop()
  100. sys.exit(0)
  101. if __name__ == '__main__':
  102. loop = asyncio.get_event_loop()
  103. signal.signal(signal.SIGINT, signal_handler)
  104. paths = get_files()
  105. fig = init_plt(paths)
  106. asyncio.ensure_future(test())
  107. for p in paths:
  108. asyncio.ensure_future(read_file_data(p))
  109. asyncio.ensure_future(plot_loop())
  110. asyncio.ensure_future(test())
  111. loop.run_forever()