dbAnalyser.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. for mapId, mapDf in df.groupby(["id"]):
  32. if mapId != "mapbox":
  33. ax.scatter(mapDf["markers"], mapDf["time"], color=colorLookup.get(mapId))
  34. mapAvgDf = mapDf.groupby(["markers"]).mean().reset_index()
  35. ax.plot(mapAvgDf["markers"], mapAvgDf["time"], label=mapId, color=colorLookup.get(mapId))
  36. else:
  37. for markerType, typeDf in mapDf.groupby(["type"]):
  38. mapTypeId = "mapbox_" + markerType
  39. ax.scatter(typeDf["markers"], typeDf["time"], color=colorLookup.get(mapTypeId))
  40. typeMapAvgDf = typeDf.groupby(["markers"]).mean().reset_index()
  41. ax.plot(typeMapAvgDf["markers"], typeMapAvgDf["time"], label=mapTypeId, color=colorLookup.get(mapTypeId))
  42. ax.legend(title="Maps")
  43. ax.legend(title="Marker Type")
  44. ax.set_xlabel("Number of Markers", fontsize=15)
  45. ax.set_ylabel("Time (ms)", fontsize=15)
  46. ax.set_title("Map Libraries", fontsize=20)
  47. folder = os.path.dirname(os.path.abspath(__file__)) + "/plots/"
  48. plt.savefig(folder + plotTitle)
  49. def mapboxTypes(df, plotTitle="mapbox.png"):
  50. mapboxDf = df.dropna(subset=["type"])
  51. fig, ax = plt.subplots(figsize=(20,12))
  52. for markerType, typeDf in mapboxDf.groupby(["type"]):
  53. mapId = "mapbox_" + markerType
  54. ax.scatter(typeDf["markers"], typeDf["time"], label=mapId, color=colorLookup.get(mapId))
  55. typeAvgDf = typeDf.groupby(["markers"]).mean().reset_index()
  56. ax.plot(typeAvgDf["markers"], typeAvgDf["time"], color=colorLookup.get(mapId))
  57. ax.legend(title="Marker Type")
  58. ax.set_xlabel("Number of Markers", fontsize=15)
  59. ax.set_ylabel("Time (ms)", fontsize=15)
  60. ax.set_title("Mapbox: Markers and Features", fontsize=20)
  61. folder = os.path.dirname(os.path.abspath(__file__)) + "/plots/"
  62. plt.savefig(folder + plotTitle)
  63. def smallValues(df):
  64. df = df.loc[df["markers"] < 300]
  65. timeMaps(df, "mapsSmall.png")
  66. mapboxTypes(df, "mapboxSmall.png")
  67. def avgPerMarkerNo(df, markerNo):
  68. print(f"\naverage time for {markerNo} markers")
  69. print("number of measurements")
  70. df = df.loc[df["markers"] == markerNo]
  71. resDf = pd.DataFrame(columns=["id", "timeMean", "rating"])
  72. for mapId, mapDf in df.groupby(["id"]):
  73. if mapId != "mapbox":
  74. print(mapId, mapDf.shape[0])
  75. resDf = resDf.append({"id": mapId, "timeMean": mapDf["time"].mean()}, ignore_index=True)
  76. else:
  77. for markerType, typeDf in mapDf.groupby(["type"]):
  78. print(markerType, typeDf.shape[0])
  79. resDf = resDf.append({"id": "mapbox_" + markerType, "timeMean": typeDf["time"].mean()}, ignore_index=True)
  80. resDf = resDf.assign(rating=lambda x: (round(4-((4-0)/(max(resDf["timeMean"])-min(resDf["timeMean"]))*(x["timeMean"]-max(resDf["timeMean"]))+4))))
  81. print(resDf)
  82. df = readFile()
  83. print(df.columns)
  84. avgPerMarkerNo(df, 100)
  85. avgPerMarkerNo(df, 1000)
  86. avgPerMarkerNo(df, 10000)
  87. timeMaps(df)
  88. mapboxTypes(df)
  89. smallValues(df)