dbAnalyser.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. def avgPerMarkerNo(df, markerNo):
  71. print(f"\naverage time for {markerNo} markers")
  72. print("number of measurements")
  73. df = df.loc[df["markers"] == markerNo]
  74. resDf = pd.DataFrame(columns=["id", "timeMean", "rating"])
  75. for mapId, mapDf in df.groupby(["id"]):
  76. if mapId != "mapbox":
  77. print(mapId, mapDf.shape[0])
  78. resDf = resDf.append({"id": mapId, "timeMean": mapDf["time"].mean()}, ignore_index=True)
  79. else:
  80. for markerType, typeDf in mapDf.groupby(["type"]):
  81. print(markerType, typeDf.shape[0])
  82. resDf = resDf.append({"id": "mapbox_" + markerType, "timeMean": typeDf["time"].mean()}, ignore_index=True)
  83. resDf = resDf.assign(rating=lambda x: (round(4-((4-0)/(max(resDf["timeMean"])-min(resDf["timeMean"]))*(x["timeMean"]-max(resDf["timeMean"]))+4))))
  84. print(resDf)
  85. df = readFile()
  86. avgPerMarkerNo(df, 100)
  87. avgPerMarkerNo(df, 1000)
  88. avgPerMarkerNo(df, 10000)
  89. timeMaps(df)
  90. mapboxTypes(df)
  91. smallValues(df)