Skip to content

Commit

Permalink
Implement issue #1579: motorcycle road vs entire country accident rat…
Browse files Browse the repository at this point in the history
…io (#1635)

* Implement issue #1579: motorcycle road vs entire country accident ratio
  • Loading branch information
rsperer authored Dec 13, 2020
1 parent a3eb672 commit 7177f86
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 9 deletions.
3 changes: 3 additions & 0 deletions anyway/backend_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ class BackEndConstants(object):

# This is a type for the 'accident_severity' table field name
ACCIDENT_SEVERITY_DEADLY = 1
ACCIDENT_SEVERITY_SEVERE = 2
ACCIDENT_SEVERITY_LIGHT = 3

# This is a type for the 'road_type' table field name
ROAD_TYPE_NOT_IN_CITY_IN_INTERSECTION = 3
ROAD_TYPE_NOT_IN_CITY_NOT_IN_INTERSECTION = 4
NON_CITY_ROAD_TYPES = [ROAD_TYPE_NOT_IN_CITY_IN_INTERSECTION, ROAD_TYPE_NOT_IN_CITY_NOT_IN_INTERSECTION]

# other global constants (python only)
DEFAULT_NUMBER_OF_YEARS_AGO = 5
Expand Down
82 changes: 73 additions & 9 deletions anyway/infographics_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import pandas as pd
from collections import defaultdict
from sqlalchemy import func
from sqlalchemy import func, distinct, literal_column, case
from sqlalchemy import cast, Numeric
from sqlalchemy import desc
from flask_babel import _
Expand Down Expand Up @@ -964,25 +964,89 @@ class MotorcycleAccidentsVsAllAccidentsWidget(Widget):
def __init__(self, request_params: RequestParams):
super().__init__(request_params, type(self).name)
self.rank = 20
self.text = {"title": "תאונות אופנועים קשות וקטלניות בכביש 20 בהשוואה לכל הארץ"}
self.road_number = request_params.location_info["road1"]
self.text = {"title": f"תאונות אופנועים קשות וקטלניות בכביש {int(self.road_number)} בהשוואה לכל הארץ"}

# noinspection PyMethodMayBeStatic
def is_in_cache(self) -> bool:
return False

def generate_items(self) -> None:
self.items = (
MotorcycleAccidentsVsAllAccidentsWidget.motorcycle_accidents_vs_all_accidents_mock_data()
MotorcycleAccidentsVsAllAccidentsWidget.motorcycle_accidents_vs_all_accidents(
self.request_params.start_time, self.request_params.end_time, self.road_number)
)

@staticmethod
def motorcycle_accidents_vs_all_accidents_mock_data(): # Temporary for Frontend
def motorcycle_accidents_vs_all_accidents(start_time, end_time, road_number):
location_label = "location"
location_other = "שאר הארץ"
location_road = f'כביש {int(road_number)}'
case_location = case([
((InvolvedMarkerView.road1 == road_number) | (InvolvedMarkerView.road2 == road_number),
location_road)
],
else_=literal_column(f"'{location_other}'")
).label(location_label)

vehicle_label = "vehicle"
vehicle_other = "אחר"
vehicle_motorcycle = "אופנוע"
case_vehicle = case([(
InvolvedMarkerView.involve_vehicle_type.in_(BE_CONST.MOTORCYCLE_VEHICLE_TYPES),
literal_column(f"'{vehicle_motorcycle}'")
)],
else_=literal_column(f"'{vehicle_other}'")
).label(vehicle_label)

query = get_query(table_obj=InvolvedMarkerView, filters={}, start_time=start_time,
end_time=end_time)

num_accidents_label = "num_of_accidents"
query = (query.with_entities(case_location, case_vehicle,
func.count(distinct(InvolvedMarkerView.provider_and_id))
.label(num_accidents_label))
.filter(InvolvedMarkerView.road_type.in_(BE_CONST.NON_CITY_ROAD_TYPES))
.filter(InvolvedMarkerView.accident_severity.in_([BE_CONST.ACCIDENT_SEVERITY_DEADLY,
BE_CONST.ACCIDENT_SEVERITY_SEVERE]))
.group_by(location_label, vehicle_label)
.order_by(desc(num_accidents_label))
)
results = pd.read_sql_query(query.statement, query.session.bind)\
.to_dict(orient="records") # pylint: disable=no-member

counter_road_motorcycle = 0
counter_other_motorcycle = 0
counter_road_other = 0
counter_other_other = 0
for record in results:
if record[location_label] == location_other:
if record[vehicle_label] == vehicle_other:
counter_other_other = record[num_accidents_label]
else:
counter_other_motorcycle = record[num_accidents_label]
else:
if record[vehicle_label] == vehicle_other:
counter_road_other = record[num_accidents_label]
else:
counter_road_motorcycle = record[num_accidents_label]
sum_road = counter_road_other + counter_road_motorcycle
if sum_road == 0:
sum_road = 1 # prevent division by zero
sum_all = counter_other_other + counter_other_motorcycle + sum_road
percentage_label = "percentage"
location_all_label = "כל הארץ"

return [
{"location": "כביש 20", "vehicle": "אופנוע", "percentage": 0.50},
{"location": "כביש 20", "vehicle": "אחר", "percentage": 0.50},
{"location": "כל הארץ", "vehicle": "אחר", "percentage": 0.80},
{"location": "כל הארץ", "vehicle": "אופנוע", "percentage": 0.20},
]
{location_label: location_road, vehicle_label: vehicle_motorcycle,
percentage_label: counter_road_motorcycle / sum_road},
{location_label: location_road, vehicle_label: vehicle_other,
percentage_label: counter_road_other / sum_road},
{location_label: location_all_label, vehicle_label: vehicle_motorcycle,
percentage_label: (counter_other_motorcycle + counter_road_motorcycle) / sum_all},
{location_label: location_all_label, vehicle_label: vehicle_other,
percentage_label: (counter_other_other + counter_road_other) / sum_all},
]


@register
Expand Down

0 comments on commit 7177f86

Please sign in to comment.