dbAnalyser.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 json
  8. import pandas as pd
  9. import matplotlib.pyplot as plt
  10. colorLookup = {
  11. "google": "r",
  12. "leaflet": "b",
  13. "mapbox_feature": "g",
  14. "mapbox_marker": "y",
  15. "mapgl": "m",
  16. "pigeons": "c"
  17. }
  18. def readFile():
  19. with open("time.json") as file:
  20. timeList = json.load(file)
  21. timeDf = pd.DataFrame(timeList)
  22. print("number of tests:", timeDf.shape[0])
  23. timeDf["markers"] = pd.to_numeric(timeDf["markers"], errors="raise")
  24. print(timeDf.value_counts(subset=["id"]))
  25. print(timeDf.value_counts(subset=["id", "type"]))
  26. return timeDf
  27. def timeMaps(df, plotTitle="plots/maps.png"):
  28. fig, ax = plt.subplots(figsize=(20,12))
  29. for mapId, mapDf in df.groupby(["id"]):
  30. if mapId != "mapbox":
  31. ax.scatter(mapDf["markers"], mapDf["time"], color=colorLookup.get(mapId))
  32. mapAvgDf = mapDf.groupby(["markers"]).mean().reset_index()
  33. ax.plot(mapAvgDf["markers"], mapAvgDf["time"], label=mapId, color=colorLookup.get(mapId))
  34. else:
  35. for markerType, typeDf in mapDf.groupby(["type"]):
  36. mapTypeId = "mapbox_" + markerType
  37. ax.scatter(typeDf["markers"], typeDf["time"], color=colorLookup.get(mapTypeId))
  38. typeMapAvgDf = typeDf.groupby(["markers"]).mean().reset_index()
  39. ax.plot(typeMapAvgDf["markers"], typeMapAvgDf["time"], label=mapTypeId, color=colorLookup.get(mapTypeId))
  40. ax.legend(title="Maps")
  41. plt.savefig(plotTitle)
  42. def mapboxTypes(df, plotTitle="plots/mapbox.png"):
  43. mapboxDf = df.dropna(subset=["type"])
  44. fig, ax = plt.subplots(figsize=(20,12))
  45. for markerType, typeDf in mapboxDf.groupby(["type"]):
  46. mapId = "mapbox_" + markerType
  47. ax.scatter(typeDf["markers"], typeDf["time"], label=mapId, color=colorLookup.get(mapId))
  48. typeAvgDf = typeDf.groupby(["markers"]).mean().reset_index()
  49. ax.plot(typeAvgDf["markers"], typeAvgDf["time"], color=colorLookup.get(mapId))
  50. ax.legend(title="Marker Type")
  51. plt.savefig(plotTitle)
  52. def smallValues(df):
  53. df = df.loc[df["markers"] < 300]
  54. timeMaps(df, "plots/mapsSmall.png")
  55. mapboxTypes(df, "plots/mapboxSmall.png")
  56. df = readFile()
  57. timeMaps(df)
  58. mapboxTypes(df)
  59. smallValues(df)