SYNC COMMIT
Working on plots.py.
This commit is contained in:
parent
c4a139c6ef
commit
def2784231
@ -1,11 +1,13 @@
|
||||
import logging
|
||||
|
||||
import paramiko.util
|
||||
import sqlalchemy
|
||||
from sshtunnel import SSHTunnelForwarder
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from config import SSH_HOST, SSH_USERNAME, SSH_PASSWORD, DB_NAME, DB_USER, DB_PASSWORD, DB_HOST, DB_PORT, SSH_PORT
|
||||
|
||||
logging.getLogger("paramiko").setLevel(logging.WARNING)
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||||
logger = logging.getLogger('db_connector.py')
|
||||
|
||||
@ -41,6 +41,9 @@ def get_view(view_name, rows="*"):
|
||||
finally:
|
||||
remote_db.close()
|
||||
|
||||
def query_table(table_name):
|
||||
pass
|
||||
|
||||
|
||||
# Specialized DB methods ==============================================================================================
|
||||
def create_heat_view():
|
||||
|
||||
@ -93,7 +93,7 @@ def create_heat_map_toggle(folium_map):
|
||||
add_heat_year_toggle(heat_gdf, folium_map)
|
||||
|
||||
add_bike_heat_toggle(folium_map)
|
||||
add_ped_heat_map(folium_map)
|
||||
add_ped_heat_toggle(folium_map)
|
||||
# Add signald speeds data
|
||||
add_signaled_speeds(folium_map)
|
||||
|
||||
@ -217,7 +217,7 @@ def add_heat_year_toggle(heat_gdf, folium_map, name="All"):
|
||||
max_opacity=0.8,
|
||||
blur=10,
|
||||
show=False,
|
||||
name=f'Accidents involving {name} in {year}'
|
||||
name=f'{name} in {year}'
|
||||
)
|
||||
|
||||
heatmap_layer.add_to(folium_map)
|
||||
@ -229,7 +229,7 @@ def add_bike_heat_toggle(folium_map):
|
||||
add_heat_year_toggle(heat_gdf, folium_map, name="motorcycles")
|
||||
|
||||
|
||||
def add_ped_heat_map(folium_map):
|
||||
def add_ped_heat_toggle(folium_map):
|
||||
pedestrian_heat_view_data = get_view("pedestrianheat")
|
||||
heat_gdf = gpd.GeoDataFrame(pedestrian_heat_view_data, columns=['latitude', 'longitude', 'year'])
|
||||
add_heat_year_toggle(heat_gdf, folium_map, name="pedestrians")
|
||||
|
||||
@ -0,0 +1,77 @@
|
||||
import logging
|
||||
|
||||
from db_connector import RemoteDB
|
||||
import matplotlib.pyplot as plt
|
||||
import pandas as pd
|
||||
import plotly.express as px
|
||||
import kaleido
|
||||
import numpy as np
|
||||
|
||||
logging.getLogger("matplotlib").setLevel(logging.WARNING)
|
||||
|
||||
# TODO
|
||||
def plt_acc_by_year(db):
|
||||
acc_year_sql = """
|
||||
SELECT COUNT(*), accidentyear FROM accidents
|
||||
GROUP BY accidentyear
|
||||
ORDER BY accidentyear;
|
||||
"""
|
||||
result = db.execute_query(acc_year_sql)
|
||||
result_df = pd.DataFrame(result)
|
||||
plt.barh(result_df['accidentyear'],result_df['count'])
|
||||
plt.ylabel('Year')
|
||||
plt.xlabel('No. of Accidents')
|
||||
plt.show()
|
||||
|
||||
|
||||
def plt_acc_by_weekday(db):
|
||||
acc_weekday_sql = f"""
|
||||
SELECT COUNT(*) AS count, accidentweekday_en AS weekday
|
||||
FROM accidents
|
||||
GROUP BY accidentweekday_en
|
||||
ORDER BY COUNT(*);
|
||||
"""
|
||||
|
||||
result = db.execute_query(acc_weekday_sql)
|
||||
result_df = pd.DataFrame(result)
|
||||
|
||||
plt.barh(result_df['weekday'], result_df['count'])
|
||||
plt.ylabel('Weekday')
|
||||
plt.xlabel('No. of Accidents')
|
||||
plt.show()
|
||||
|
||||
|
||||
|
||||
|
||||
def plt_acc_by_daytime(db):
|
||||
acc_weekday_sql = f"""
|
||||
SELECT COUNT(*) AS count, accidenthour AS hour
|
||||
FROM accidents
|
||||
GROUP BY accidenthour
|
||||
ORDER BY COUNT(*);
|
||||
"""
|
||||
|
||||
result = db.execute_query(acc_weekday_sql)
|
||||
result_df = pd.DataFrame(result)
|
||||
|
||||
plt.barh(result_df['hour'], result_df['count'])
|
||||
plt.ylabel('hour')
|
||||
plt.xlabel('No. of Accidents')
|
||||
plt.show()
|
||||
|
||||
fig = px.bar(result_df, y='hour', x='count', orientation='h')
|
||||
fig.write_image("fig/acc_by_day.png")
|
||||
fig.write_html("html/acc_by_day.html")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
remote_db = RemoteDB()
|
||||
try:
|
||||
#plt_acc_by_year(remote_db)
|
||||
#plt_acc_by_weekday(remote_db)
|
||||
plt_acc_by_daytime(remote_db)
|
||||
except Exception as e:
|
||||
print(f"Exception {e} in plots.py")
|
||||
finally:
|
||||
remote_db.close()
|
||||
|
||||
661
analysis/plotting.ipynb
Normal file
661
analysis/plotting.ipynb
Normal file
File diff suppressed because one or more lines are too long
@ -306,20 +306,17 @@
|
||||
"# Your Folium map\n",
|
||||
"map = folium.Map(location=[47.368650, 8.539183], zoom_start=13, tiles=\"cartodb positron\")\n",
|
||||
"\n",
|
||||
"# Example WKB geometry\n",
|
||||
"wkb_geometry = '0105000020E6100000010000000102000000030000001C261AA4E0112140605793A7ACB047400EA2B5A2CD11214058E1968FA4B047406F47382D78112140983446EBA8B04740'\n",
|
||||
"\n",
|
||||
"# Convert WKB to Shapely Geometry\n",
|
||||
"shapely_geometry = wkb.loads(wkb_geometry, hex=True)\n",
|
||||
"\n",
|
||||
"# Convert Shapely Geometry to GeoJSON\n",
|
||||
"geojson_geometry = json.loads(json.dumps(shapely.geometry.mapping(shapely_geometry)))\n",
|
||||
"\n",
|
||||
"# Add to Folium Map\n",
|
||||
"\n",
|
||||
"folium.GeoJson(geojson_geometry).add_to(map)\n",
|
||||
"\n",
|
||||
"# Save or show map\n",
|
||||
"map.save(\"map.html\") # or use map.show() in a Jupyter environment\n"
|
||||
"\n",
|
||||
"map.save(\"map.html\") \n"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
|
||||
Reference in New Issue
Block a user