plot.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. return fig
  52. def get_files():
  53. return ["plots/{}".format(p) for p in [f for f in os.listdir("plots") if f.endswith(".tsv")]]
  54. async def read_file_data(path):
  55. print(f"reading data for {path}")
  56. lines = follow(path)
  57. xs = []
  58. ys = []
  59. async for line in lines:
  60. line_data = line.split("\t")
  61. x, success = tryParseInt(line_data[0])
  62. if success:
  63. xs.append(x/1000)
  64. rest = tryParseIntList(line_data[1:])
  65. if rest is not None:
  66. if len(ys) == 0:
  67. ys = [[i] for i in rest]
  68. else:
  69. for i in range(len(rest)):
  70. ys[i].append(rest[i])
  71. file_data[path] = (xs, ys)
  72. print("added to file_data")
  73. print("read all lines")
  74. async def plot_loop():
  75. print("before plot loop")
  76. while True:
  77. if len(axes) > 0:
  78. for key, ax in axes.items():
  79. if len(file_data) > 0:
  80. xs, ys = file_data[key]
  81. print("reading file data")
  82. ax.clear()
  83. for i in range(len(ys)):
  84. y = ys[i]
  85. ax.plot(xs, y, label=labels[key][i + 1], linewidth = 0.7)
  86. ax.set_title(key)
  87. ax.set_xlabel(labels[key][0])
  88. print("plot.show")
  89. plt.legend()
  90. plt.pause(0.2)
  91. await asyncio.sleep(0.2)
  92. # await asyncio.sleep(1)
  93. async def test():
  94. print("abc")
  95. def signal_handler(signal, frame):
  96. loop.stop()
  97. sys.exit(0)
  98. if __name__ == '__main__':
  99. loop = asyncio.get_event_loop()
  100. signal.signal(signal.SIGINT, signal_handler)
  101. paths = get_files()
  102. fig = init_plt(paths)
  103. asyncio.ensure_future(test())
  104. for p in paths:
  105. asyncio.ensure_future(read_file_data(p))
  106. asyncio.ensure_future(plot_loop())
  107. asyncio.ensure_future(test())
  108. loop.run_forever()