dbAnalyser.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Fri Nov 27 23:48:19 2020
  5. @author: bernadetteelena
  6. """
  7. import os
  8. import json
  9. import pandas as pd
  10. import matplotlib.pyplot as plt
  11. colorLookup = {
  12. "google": "r",
  13. "leaflet": "b",
  14. "mapbox_feature": "g",
  15. "mapbox_marker": "y",
  16. "mapgl": "m",
  17. "pigeons": "c"
  18. }
  19. def readFile():
  20. filePath = os.path.dirname(os.path.abspath(__file__)) + "/time.json"
  21. with open(filePath) as file:
  22. timeList = json.load(file)
  23. timeDf = pd.DataFrame(timeList)
  24. print("number of tests:", timeDf.shape[0])
  25. timeDf["markers"] = pd.to_numeric(timeDf["markers"], errors="raise")
  26. print(timeDf.value_counts(subset=["id"]))
  27. print(timeDf.value_counts(subset=["id", "type"]))
  28. return timeDf
  29. def timeMaps(df, plotTitle="maps.png"):
  30. fig, ax = plt.subplots(figsize=(20,12))
  31. plt.yticks(fontsize=15)
  32. plt.xticks(fontsize=15)
  33. for mapId, mapDf in df.groupby(["id"]):
  34. if mapId != "mapbox":
  35. ax.scatter(mapDf["markers"], mapDf["time"], color=colorLookup.get(mapId))
  36. mapAvgDf = mapDf.groupby(["markers"]).mean().reset_index()
  37. ax.plot(mapAvgDf["markers"], mapAvgDf["time"], label=mapId, color=colorLookup.get(mapId))
  38. else:
  39. for markerType, typeDf in mapDf.groupby(["type"]):
  40. mapTypeId = "mapbox_" + markerType
  41. ax.scatter(typeDf["markers"], typeDf["time"], color=colorLookup.get(mapTypeId))
  42. typeMapAvgDf = typeDf.groupby(["markers"]).mean().reset_index()
  43. ax.plot(typeMapAvgDf["markers"], typeMapAvgDf["time"], label=mapTypeId, color=colorLookup.get(mapTypeId))
  44. ax.legend(title="Marker Type", fontsize=17, title_fontsize=20)
  45. ax.set_xlabel("Number of Markers", fontsize=20)
  46. ax.set_ylabel("Time (ms)", fontsize=20)
  47. ax.set_title("Map Libraries - Marker", fontsize=25)
  48. folder = os.path.dirname(os.path.abspath(__file__)) + "/plots/"
  49. plt.savefig(folder + plotTitle, bbox_inches="tight")
  50. def mapboxTypes(df, plotTitle="mapbox.png"):
  51. mapboxDf = df.dropna(subset=["type"])
  52. fig, ax = plt.subplots(figsize=(20,12))
  53. plt.yticks(fontsize=15)
  54. plt.xticks(fontsize=15)
  55. for markerType, typeDf in mapboxDf.groupby(["type"]):
  56. mapId = "mapbox_" + markerType
  57. ax.scatter(typeDf["markers"], typeDf["time"], label=mapId, color=colorLookup.get(mapId))
  58. typeAvgDf = typeDf.groupby(["markers"]).mean().reset_index()
  59. ax.plot(typeAvgDf["markers"], typeAvgDf["time"], color=colorLookup.get(mapId))
  60. ax.legend(title="Marker Type", fontsize=17, title_fontsize=20)
  61. ax.set_xlabel("Number of Markers", fontsize=20)
  62. ax.set_ylabel("Time (ms)", fontsize=20)
  63. ax.set_title("Mapbox: Markers and Features", fontsize=25)
  64. folder = os.path.dirname(os.path.abspath(__file__)) + "/plots/"
  65. plt.savefig(folder + plotTitle, bbox_inches="tight")
  66. def smallValues(df):
  67. df = df.loc[df["markers"] < 300]
  68. timeMaps(df, "mapsSmall.png")
  69. mapboxTypes(df, "mapboxSmall.png")
  70. timeMapsHighlight(df, "mapsSmallHighlight.png")
  71. def avgPerMarkerNo(df, markerNo):
  72. print(f"\naverage time for {markerNo} markers")
  73. print("number of measurements")
  74. df = df.loc[df["markers"] == markerNo]
  75. resDf = pd.DataFrame(columns=["id", "timeMean", "rating"])
  76. for mapId, mapDf in df.groupby(["id"]):
  77. if mapId != "mapbox":
  78. print(mapId, mapDf.shape[0])
  79. resDf = resDf.append({"id": mapId, "timeMean": mapDf["time"].mean()}, ignore_index=True)
  80. else:
  81. for markerType, typeDf in mapDf.groupby(["type"]):
  82. print(markerType, typeDf.shape[0])
  83. resDf = resDf.append({"id": "mapbox_" + markerType, "timeMean": typeDf["time"].mean()}, ignore_index=True)
  84. resDf = resDf.assign(rating=lambda x: (round(4-((4-0)/(max(resDf["timeMean"])-min(resDf["timeMean"]))*(x["timeMean"]-max(resDf["timeMean"]))+4))))
  85. print(resDf)
  86. def timeMapsHighlight(df, plotTitle="mapsHightlight.png"):
  87. fig, ax = plt.subplots(figsize=(20,12))
  88. plt.yticks(fontsize=15)
  89. plt.xticks(fontsize=15)
  90. for mapId, mapDf in df.groupby(["id"]):
  91. if mapId != "mapbox":
  92. if mapId == "leaflet":
  93. ax.scatter(mapDf["markers"], mapDf["time"], color=colorLookup.get(mapId))
  94. mapAvgDf = mapDf.groupby(["markers"]).mean().reset_index()
  95. ax.plot(mapAvgDf["markers"], mapAvgDf["time"], label=mapId, color=colorLookup.get(mapId), linewidth=2)
  96. else:
  97. ax.scatter(mapDf["markers"], mapDf["time"], color=colorLookup.get(mapId), alpha=0.3)
  98. mapAvgDf = mapDf.groupby(["markers"]).mean().reset_index()
  99. ax.plot(mapAvgDf["markers"], mapAvgDf["time"], label=mapId, color=colorLookup.get(mapId), linewidth=1, alpha=0.3)
  100. else:
  101. for markerType, typeDf in mapDf.groupby(["type"]):
  102. mapTypeId = "mapbox_" + markerType
  103. if markerType == "feature":
  104. ax.scatter(typeDf["markers"], typeDf["time"], color=colorLookup.get(mapTypeId))
  105. typeMapAvgDf = typeDf.groupby(["markers"]).mean().reset_index()
  106. ax.plot(typeMapAvgDf["markers"], typeMapAvgDf["time"], label=mapTypeId, color=colorLookup.get(mapTypeId), linewidth=2)
  107. else:
  108. ax.scatter(typeDf["markers"], typeDf["time"], color=colorLookup.get(mapTypeId), alpha=0.3)
  109. typeMapAvgDf = typeDf.groupby(["markers"]).mean().reset_index()
  110. ax.plot(typeMapAvgDf["markers"], typeMapAvgDf["time"], label=mapTypeId, color=colorLookup.get(mapTypeId), linewidth=1, alpha=0.3)
  111. ax.legend(title="Marker Type", fontsize=17, title_fontsize=20)
  112. ax.set_xlabel("Number of Markers", fontsize=20)
  113. ax.set_ylabel("Time (ms)", fontsize=20)
  114. ax.set_title("Map Libraries - Marker", fontsize=25)
  115. folder = os.path.dirname(os.path.abspath(__file__)) + "/plots/"
  116. plt.savefig(folder + plotTitle, bbox_inches="tight")
  117. df = readFile()
  118. avgPerMarkerNo(df, 100)
  119. avgPerMarkerNo(df, 1000)
  120. avgPerMarkerNo(df, 10000)
  121. timeMaps(df)
  122. timeMapsHighlight(df)
  123. mapboxTypes(df)
  124. smallValues(df)