data_analysis.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import os
  2. import json
  3. import statistics
  4. import matplotlib.pyplot as plt
  5. from data_develop import (
  6. check_folder,
  7. get_file_name_json,
  8. merge_callback_result
  9. )
  10. from config import (
  11. value_x,
  12. folder_use,
  13. nats_subject,
  14. folder_use_plot
  15. )
  16. def value_selected_file_plot(delta:str, name:str) -> str:
  17. return os.path.join(folder_use_plot, f"{nats_subject}_{delta}_{name}.png")
  18. def develop_stats_delta(delta:str):
  19. with open(os.path.join(folder_use, get_file_name_json(False)), 'r') as file:
  20. value_revision = [f.get(delta, -1) for f in json.load(file)]
  21. return {
  22. "Maximum Value": max(value_revision),
  23. "Minimum Value": min(value_revision),
  24. "Mean Value of it": statistics.mean(value_revision),
  25. "Median Value of it": statistics.median(value_revision),
  26. "Mode Value of it": statistics.mode(value_revision),
  27. "Standard Deviation Value of it": statistics.stdev(value_revision),
  28. "Variance": statistics.variance(value_revision),
  29. "Length": len(value_revision),
  30. }
  31. def develop_stats_plot(delta:str):
  32. with open(os.path.join(folder_use, get_file_name_json(False)), 'r') as file:
  33. value_delta = [f.get(delta, -1) for f in json.load(file)]
  34. value_plot_mean = plt.scatter(
  35. value_x,
  36. [
  37. statistics.mean(value_delta[:i])
  38. for i in value_x
  39. ]
  40. )
  41. if delta == 'delta_full':
  42. k = "Between send and inserting values"
  43. elif delta == 'delta_send':
  44. k = "Between sent and receive of the consumer"
  45. elif delta == 'delta_proccessed':
  46. k = "Between receive of the consumer and insert"
  47. plt.title(f'{k}: mean')
  48. plt.ylabel("Seconds")
  49. plt.xlabel("Number of elements")
  50. plt.close()
  51. value_plot_var = plt.scatter(
  52. value_x,
  53. [
  54. statistics.variance(value_delta[:i])
  55. for i in value_x
  56. ]
  57. )
  58. plt.title(f'{k}: variance')
  59. plt.ylabel("Seconds")
  60. plt.xlabel("Number of elements")
  61. plt.close()
  62. value_plot_dev = plt.scatter(
  63. value_x,
  64. [
  65. statistics.stdev(value_delta[:i])
  66. for i in value_x
  67. ]
  68. )
  69. plt.title(f'{k}: deviation')
  70. plt.ylabel("Seconds")
  71. plt.xlabel("Number of elements")
  72. plt.close()
  73. value_plot_mean.figure.savefig(value_selected_file_plot(delta, 'mean'))
  74. value_plot_var.figure.savefig(value_selected_file_plot(delta, 'variance'))
  75. value_plot_dev.figure.savefig(value_selected_file_plot(delta, 'deviation'))
  76. def develop_stats():
  77. value_use = {
  78. "Between send and inserting values": develop_stats_delta('delta_full'),
  79. "Between sent and receive of the consumer": develop_stats_delta('delta_send'),
  80. "Between receive of the consumer and insert": develop_stats_delta('delta_proccessed'),
  81. }
  82. with open(os.path.join(folder_use, get_file_name_json('1')), 'w') as file_write:
  83. json.dump(
  84. value_use,
  85. file_write,
  86. indent=4
  87. )
  88. return value_use
  89. def develop_plot():
  90. if not os.path.exists(os.path.join(folder_use, get_file_name_json(False))):
  91. check_folder(folder_use)
  92. merge_callback_result()
  93. check_folder(folder_use_plot)
  94. for f in ['delta_full', 'delta_send', 'delta_proccessed']:
  95. develop_stats_plot(f)
  96. develop_stats()
  97. if __name__ == '__main__':
  98. develop_plot()