def get_plot_avg_accidents_by_weekdays(start_datetime=None, end_datetime=None): data = d.get_traffic_accident_by_date( get_start_datetime(start_datetime), get_end_datetime(end_datetime))['overallStartTime'] data = data.map(lambda p: p.date()) avg_accident_per_weekdays = data.map(lambda p: p.weekday()).value_counts( ).sort_index() / pd.Series( data.unique()).map(lambda p: p.weekday()).value_counts().sort_index() sns.set_style("whitegrid") fig = plt.figure(figsize=(25, 15)) plt.title("Priemerný počet nehôd podľa dňa v týždni", fontsize=30, pad=20) plt.subplots_adjust(left=0.07, right=0.99) g = sns.barplot(x=avg_accident_per_weekdays.index, y=avg_accident_per_weekdays.values, palette=get_min_max_colors( avg_accident_per_weekdays.values)) #g.set_xlabel("Deň v týždni",fontsize=30, labelpad=15) g.set_ylabel("Priemerný počet nehôd", fontsize=30, labelpad=20) g.set_xticklabels(get_weekday_xtickslabels( avg_accident_per_weekdays.index), rotation=45, fontsize=30) g.tick_params(axis='y', labelsize=25) return encode_plot(fig)
def get_county_choropleth(start_datetime, end_datetime, output='json'): data = d.get_traffic_accident_by_date(start_datetime, end_datetime)['countyId'] county_names = d.get_county() data = data.value_counts() data.index = data.index.map(lambda p: county_names.loc[p]['name']) zeroes = pd.Series(data=0, index=county_names.name) data = data + zeroes data = data.fillna(0) data = data.sort_values() df = pd.DataFrame(dict(county=data.index, count=data.values)) geojson_file = os.path.join(os.path.dirname(__file__), 'regions_epsg_4326.geojson.txt') with open(geojson_file, encoding='utf-8') as file: geo_counties = json.loads(file.read()) fig = px.choropleth(data_frame=df, geojson=geo_counties, featureidkey='properties.NM4', locations='county', color='count', color_continuous_scale='tealrose', range_color=(0, df['count'].mean()*2), projection='sinusoidal', labels={'count':'Počet nehôd'}, hover_data={'county':False}, hover_name=df['county']) fig.update_geos(fitbounds="locations", visible=False) fig.update_layout( margin={"r":0,"t":0,"l":0,"b":0}, paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)', geo=dict(bgcolor= 'rgba(0,0,0,0)'), coloraxis_showscale=False, ) return plots.encode_plot(fig, output)
def get_plot_total_accident_trend(start_datetime, end_datetime, output='json'): data = d.get_traffic_accident_by_date(start_datetime, end_datetime)['overallStartTime'] df = prepare_data_for_trend_plot(data, start_datetime, end_datetime) fig = get_plot_accident_trend(df) return encode_plot(fig, output)
def get_total_accidents_for_road(road_number, start_datetime, end_datetime, start_km=0, end_km=999999999): data = d.get_traffic_accident_by_date(start_datetime, end_datetime) data = data.loc[(data.roadPosition >= start_km) & (data.roadPosition <= end_km)] data = data.loc[data.roadNumber == road_number]['overallStartTime'] return len(data.index)
def get_plot_accident_trend_in_district(district_id, start_datetime, end_datetime, output='json'): data = d.get_traffic_accident_by_date(start_datetime, end_datetime) data = data.loc[data.districtId == district_id]['overallStartTime'] df = prepare_data_for_trend_plot(data, start_datetime, end_datetime) fig = get_plot_accident_trend(df) return encode_plot(fig, output)
def get_plot_total_accidents_ratio_by_roads(start_datetime, end_datetime, max_records, output='json'): data = d.get_traffic_accident_by_date(start_datetime, end_datetime)['roadNumber'] data = data.value_counts() road = d.get_road() road = road.loc[road.direction == 1] data = pd.merge(data, road, left_index=True, right_on='number') if len(data.index) == 0: return encode_plot(get_empty_plot(), output) data.rename(columns={'roadNumber': 'count'}, inplace=True) data['shape_length'] = data['shape_length'] / 1000 data['ratio'] = data['count'] / data['shape_length'] data = data.sort_values(by='ratio').tail(max_records) fig = px.bar( data, x='number', y='ratio', labels={ 'ratio': 'Počet nehôd na 1km', 'shape_length': 'Dĺžka v km', 'count': 'Počet nehôd', 'number': 'Cesta' }, hover_data={ 'shape_length': True, 'count': True }, ) fig.update_layout( xaxis=dict( title_text='Cesta', titlefont=dict(size=20), type='category', ), yaxis=dict( title_text='Počet nehôd na 1km', gridcolor='rgb(140,140,140)', titlefont=dict(size=20), ), dragmode=False, margin={ "r": 0, "t": 0, "l": 0, "b": 0 }, paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)', font=dict(size=14, ), ) return encode_plot(fig, output)
def get_plot_accident_trend_on_road(road_number, start_datetime, end_datetime, start_km=0, end_km=999999999, output='json'): data = d.get_traffic_accident_by_date(start_datetime, end_datetime) data = data.loc[data.roadNumber == road_number] data = data.loc[(data.roadPosition >= start_km) & (data.roadPosition <= end_km)] data = data['overallStartTime'] df = prepare_data_for_trend_plot(data, start_datetime, end_datetime) fig = get_plot_accident_trend(df) return encode_plot(fig, output)
def get_plot_total_accidents_by_roads(start_datetime, end_datetime, max_records, output='json'): data = d.get_traffic_accident_by_date(start_datetime, end_datetime)['roadNumber'] road = d.get_road() road = road.loc[road.direction == 1] data = pd.merge(data, road, left_on='roadNumber', right_on='number') if len(data.index) == 0: return encode_plot(get_empty_plot(), output) data = data['roadNumber'].value_counts() data = data.sort_values().tail(max_records) df = pd.DataFrame(dict(road=data.index, count=data.values)) fig = px.bar( df, x='road', y='count', labels={ 'count': 'Počet nehôd', 'road': 'Cesta' }, ) fig.update_layout( xaxis=dict( title_text='Cesta', titlefont=dict(size=20), type='category', ), yaxis=dict( title_text='Celkový počet nehôd', gridcolor='rgb(140,140,140)', titlefont=dict(size=20), ), dragmode=False, margin={ "r": 0, "t": 0, "l": 0, "b": 0 }, paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)', font=dict(size=14, ), ) return encode_plot(fig, output)
def get_plot_total_accidents_by_city(start_datetime, end_datetime, output='json'): data = d.get_traffic_accident_by_date(start_datetime, end_datetime)['cityId'] city_names = d.get_city() data = data.value_counts() data.index = data.index.map(lambda p: city_names.loc[p]['name'] if p in city_names.index else 'NA') zeroes = pd.Series(data=0, index=city_names.name) data = data + zeroes data = data.fillna(0) data = data.sort_values().tail(50) df = pd.DataFrame(dict(city=data.index, count=data.values)) fig = px.bar( df, x='city', y='count', labels={ 'count': 'Počet nehôd', 'city': 'Obec' }, ) fig.update_layout( xaxis=dict( title_text='Obec', titlefont=dict(size=20), ), height=700, yaxis=dict( title_text='Celkový počet nehôd', gridcolor='rgb(140,140,140)', titlefont=dict(size=20), ), dragmode=False, margin={ "r": 0, "t": 0, "l": 0, "b": 0 }, paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)', font=dict(size=14, ), ) return encode_plot(fig, output)
def get_plot_avg_accidents_by_weekdays(start_datetime, end_datetime, output='json'): data = d.get_traffic_accident_by_date(start_datetime, end_datetime)['overallStartTime'] data = data.map(lambda p: p.date()) data = data.map(lambda p: p.weekday()).value_counts().sort_index( ) / pd.Series( data.unique()).map(lambda p: p.weekday()).value_counts().sort_index() df = pd.DataFrame(dict(weekday=data.index, avg_count=data.values)) fig = px.bar( df, x='weekday', y='avg_count', labels={ 'weekday': 'Deň v týždni', 'avg_count': 'Priemerný počet nehôd' }, ) fig.update_layout( xaxis=dict( tickmode='array', tickvals=df['weekday'], ticktext=get_weekday_xtickslabels(df['weekday']), title_text='Deň v týždni', titlefont=dict(size=20), ), height=500, yaxis=dict( title_text='Priemerný počet nehôd', gridcolor='rgb(140,140,140)', titlefont=dict(size=20), ), dragmode=False, margin={ "r": 0, "t": 0, "l": 0, "b": 0 }, paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)', font=dict(size=14, ), ) return encode_plot(fig, output)
def get_plot_total_accidents_by_county(start_datetime, end_datetime, output='json'): acc = d.get_traffic_accident_by_date(start_datetime, end_datetime)['countyId'] acc = acc.value_counts() data = d.get_county() data['count'] = 0 data['count'] += acc data['count'] = data['count'].fillna(0) data.sort_values(by='count', inplace=True) fig = px.bar( data, x='name', y='count', custom_data=[data.index], labels={ 'count': 'Počet nehôd', 'name': 'Kraj' }, ) fig.update_layout( xaxis=dict( title_text='Kraj', titlefont=dict(size=20), ), height=600, yaxis=dict( title_text='Celkový počet nehôd', gridcolor='rgb(140,140,140)', titlefont=dict(size=20), ), dragmode=False, margin={ "r": 0, "t": 0, "l": 0, "b": 0 }, paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)', font=dict(size=14, ), ) return encode_plot(fig, output)
def get_plot_accident_by_time_in_day(start_datetime, end_datetime, output='json'): data = d.get_traffic_accident_by_date(start_datetime, end_datetime)['overallStartTime'] total_count = data.size data = data.map(lambda p: p.time().hour).value_counts().sort_index() data = data * 100 / total_count df = pd.DataFrame(dict(hour=data.index, pct=data.values)) fig = px.bar( df, x='hour', y='pct', labels={ 'hour': 'Hodina počas dňa', 'pct': 'Percento nehôd' }, hover_data={'pct': ':.2f'}, ) fig.update_layout( xaxis=dict( tickmode='array', tickvals=df['hour'], title_text='Hodina počas dňa', titlefont=dict(size=20), ), height=500, yaxis=dict( title_text='Percento nehôd', gridcolor='rgb(140,140,140)', titlefont=dict(size=20), ), dragmode=False, margin={ "r": 0, "t": 0, "l": 0, "b": 0 }, paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)', font=dict(size=14, ), ) return encode_plot(fig, output)
def get_map_with_most_frequent_accidents_for_road(road_number, max_number_accidents_returned, start_datetime, end_datetime, start_km=0, end_km=999999999, output='json'): data = d.get_traffic_accident_by_date(start_datetime, end_datetime) data = data.loc[data.roadNumber == road_number] data = data.loc[(data.roadPosition >= start_km) & (data.roadPosition <= end_km)] fig = get_map_with_most_frequent_accidents(max_number_accidents_returned, data, 8) if data.size > 0: shape = d.get_road_shape(road_number) if shape is not None: shape_list = parse_shape_string(shape) shape_list = filter_shape(shape_list, start_km, end_km) for s in shape_list: fig.add_trace(go.Scattermapbox( mode = 'lines', line=dict(width=4, color="#006699"), showlegend=False, lon = s[1], lat = s[0], hoverinfo='skip')) return plots.encode_plot(fig, output)
def get_plot_accidents_by_county(start_datetime=None, end_datetime=None): data = d.get_traffic_accident_by_date( get_start_datetime(start_datetime), get_end_datetime(end_datetime))['countyId'] data = data.value_counts().sort_values() sns.set_style("whitegrid") fig = plt.figure(figsize=(25, 15)) plt.title("Absolútny počet nehôd podľa kraju", fontsize=30, pad=20) plt.subplots_adjust(top=0.93, bottom=0.18, left=0.07, right=0.99) g = sns.barplot(x=get_county_xticklabels(data.index), y=data.values, palette=get_min_max_colors(data.values)) g.set_ylabel("Absolútny počet nehôd", fontsize=30, labelpad=20) g.set_xticklabels(get_county_xticklabels(data.index), rotation=45, fontsize=20) g.tick_params(axis='y', labelsize=25) return encode_plot(fig)
def get_plot_accident_by_time_in_day(start_datetime=None, end_datetime=None): data = d.get_traffic_accident_by_date( get_start_datetime(start_datetime), get_end_datetime(end_datetime))['overallStartTime'] total_count = data.size data = data.map(lambda p: p.time().hour).value_counts().sort_index() data = data * 100 / total_count sns.set_style("whitegrid") fig = plt.figure(figsize=(25, 15)) plt.title("Percento nehôd podľa hodiny v dni", fontsize=30, pad=20) plt.subplots_adjust(left=0.07, right=0.99) g = sns.barplot(x=data.index, y=data.values, palette=get_min_max_colors(data.values)) g.set_xlabel("Hodina počas dňa", fontsize=30, labelpad=15) g.set_ylabel("Percento nehôd", fontsize=30, labelpad=20) g.tick_params(axis='x', labelsize=25) g.tick_params(axis='y', labelsize=25) return encode_plot(fig)
def get_plot_total_accidents_by_days(start_datetime=None, end_datetime=None): data = d.get_traffic_accident_by_date( get_start_datetime(start_datetime), get_end_datetime(end_datetime))['overallStartTime'] days = data.map(lambda p: p.date()).value_counts().sort_index() sns.set_style("whitegrid") fig = plt.figure(figsize=(25, 15)) plt.title("Histogram nehôd - počet nehôd za deň", fontsize=30, pad=20) plt.subplots_adjust(left=0.07, right=0.99) g = sns.barplot(x=days.index, y=days.values, palette=get_min_max_colors(days.values)) #g.set_xlabel("Deň",fontsize=30, labelpad=15) g.set_ylabel("Počet nehôd", fontsize=30, labelpad=20) g.set_xticklabels(get_date_xtickslabels(days.index), rotation=90, fontsize=20) g.tick_params(axis='y', labelsize=25) return encode_plot(fig)
def get_plot_accident_trend_in_district(district_id, start_datetime=None, end_datetime=None): data = d.get_traffic_accident_by_date(get_start_datetime(start_datetime), get_end_datetime(end_datetime)) data = data.loc[data.districtId == district_id]['overallStartTime'] data = data.map(lambda p: p.date()) data = data.value_counts().sort_index() sns.set_style("whitegrid") fig = plt.figure(figsize=(25, 15)) plt.title("Vývoj nehôd v čase pre okres " + d.get_district().loc[district_id]['name'], fontsize=30, pad=20) plt.subplots_adjust(left=0.07, right=0.99) g = sns.lineplot(x=data.index, y=data.values) #g.set_xlabel("Deň",fontsize=30, labelpad=15) g.set_ylabel("Počet nehôd", fontsize=30, labelpad=20) #g.set_xticklabels(get_date_xtickslabels(data.index), rotation=90, fontsize=20) g.tick_params(axis='x', labelsize=25, rotation=45) g.tick_params(axis='y', labelsize=25) return encode_plot(fig)
def get_plot_accidents_by_district(start_datetime=None, end_datetime=None): data = d.get_traffic_accident_by_date( get_start_datetime(start_datetime), get_end_datetime(end_datetime))['districtId'] data = data.value_counts() zeroes = pd.Series(index=d.get_district().index) zeroes.values[:] = 0 zeroes += data data = zeroes.fillna(0).sort_values() sns.set_style("whitegrid") fig = plt.figure(figsize=(25, 15)) fig.tight_layout() plt.title("Absolútny počet nehôd podľa okresu", fontsize=30, pad=20) plt.subplots_adjust(top=0.93, bottom=0.2, left=0.07, right=0.99) g = sns.barplot(x=get_district_xticklabels(data.index), y=data.values, palette=get_min_max_colors(data.values)) g.set_ylabel("Absolútny počet nehôd", fontsize=30, labelpad=20) g.set_xticklabels(get_district_xticklabels(data.index), rotation=90, fontsize=13) g.tick_params(axis='y', labelsize=25) return encode_plot(fig)
def get_min_date(): retval = d.get_traffic_accident_by_date()['overallStartTime'].min() retval = retval.replace(hour=0, minute=0, second=0, microsecond=0) return retval
def get_map_with_most_frequent_accidents_for_country(max_number_accidents_returned, start_datetime, end_datetime, output='json'): data = d.get_traffic_accident_by_date(start_datetime, end_datetime) fig = get_map_with_most_frequent_accidents(max_number_accidents_returned, data, 6, center = {'lat':48.663863, 'lon':19.502998}) return plots.encode_plot(fig, output)
def get_map_with_most_frequent_accidents_for_district(district_id, max_number_accidents_returned, start_datetime, end_datetime, output='json'): data = d.get_traffic_accident_by_date(start_datetime, end_datetime) data = data.loc[data.districtId == district_id] fig = get_map_with_most_frequent_accidents(max_number_accidents_returned, data, 9.5) return plots.encode_plot(fig, output)