Skip to content

Commit

Permalink
Adding location accuracy filter.
Browse files Browse the repository at this point in the history
Add resolution parameter to get_accidents_stats() to add the location accuracy filter.
In widgets that do not use it, handle the filter dicts that is passed to get_quary, or the filter directly.
  • Loading branch information
ziv17 committed May 5, 2024
1 parent aeebaeb commit df25a5c
Show file tree
Hide file tree
Showing 31 changed files with 334 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,23 @@ def __init__(self, request_params: RequestParams):

def generate_items(self) -> None:
# noinspection PyUnresolvedReferences
self.items = AccidentCountByAccidentTypeWidget.get_accident_count_by_accident_type(
self.items = self.get_accident_count_by_accident_type(
location_info=self.request_params.location_info,
start_time=self.request_params.start_time,
end_time=self.request_params.end_time,
resolution=self.request_params.resolution
)

@staticmethod
def get_accident_count_by_accident_type(location_info, start_time, end_time):
def get_accident_count_by_accident_type(location_info, start_time, end_time, resolution):
all_accident_type_count = get_accidents_stats(
table_obj=AccidentMarkerView,
filters=location_info,
group_by="accident_type",
count="accident_type",
start_time=start_time,
end_time=end_time,
resolution=resolution
)
merged_accident_type_count = [{"accident_type": "Collision", "count": 0}]
for item in all_accident_type_count:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def generate_items(self) -> None:
count="accident_severity",
start_time=self.request_params.start_time,
end_time=self.request_params.end_time,
resolution=self.request_params.resolution,
)
res2 = sort_and_fill_gaps_for_stacked_bar(
res1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def generate_items(self) -> None:
count="day_night_hebrew",
start_time=self.request_params.start_time,
end_time=self.request_params.end_time,
resolution=self.request_params.resolution,
)

@staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,23 @@ def __init__(self, request_params: RequestParams):
self.rank = 1

def generate_items(self) -> None:
self.items = AccidentCountBySeverityWidget.get_accident_count_by_severity(
self.request_params.location_info,
self.request_params.start_time,
self.request_params.end_time,
self.items = self.get_accident_count_by_severity(
location_info=self.request_params.location_info,
start_time=self.request_params.start_time,
end_time=self.request_params.end_time,
resolution=self.request_params.resolution,
)

@staticmethod
def get_accident_count_by_severity(location_info, start_time, end_time):
def get_accident_count_by_severity(location_info, start_time, end_time, resolution):
count_by_severity = get_accidents_stats(
table_obj=AccidentMarkerView,
filters=location_info,
group_by="accident_severity",
count="accident_severity",
start_time=start_time,
end_time=end_time,
resolution=resolution
)
found_severities = [d["accident_severity"] for d in count_by_severity]
items = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

from anyway.request_params import RequestParams
from anyway.backend_constants import AccidentSeverity, BE_CONST
from anyway.widgets.widget_utils import get_query, get_location_text
from anyway.widgets.widget_utils import (
get_query, get_location_text, add_resolution_location_accuracy_filter
)
from anyway.models import AccidentMarkerView
from anyway.widgets.widget import register
from anyway.widgets.all_locations_widgets.all_locations_widget import AllLocationsWidget
Expand All @@ -21,14 +23,17 @@ def __init__(self, request_params: RequestParams):
self.rank = 7

def generate_items(self) -> None:
accidents_heat_map_filters = self.request_params.location_info.copy()
accidents_heat_map_filters = add_resolution_location_accuracy_filter(
self.request_params.location_info.copy(),
self.request_params.resolution
)
accidents_heat_map_filters["accident_severity"] = [
# pylint: disable=no-member
AccidentSeverity.FATAL.value,
# pylint: disable=no-member
AccidentSeverity.SEVERE.value,
]
self.items = AccidentsHeatMapWidget.get_accidents_heat_map(
self.items = self.get_accidents_heat_map(
filters=accidents_heat_map_filters,
start_time=self.request_params.start_time,
end_time=self.request_params.end_time,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def generate_items(self) -> None:
count="injury_severity",
start_time=self.request_params.start_time,
end_time=self.request_params.end_time,
resolution=self.request_params.resolution,
)
res2 = sort_and_fill_gaps_for_stacked_bar(
res1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ def get_injured_count_by_severity(
InjurySeverity.SEVERE_INJURED.value,
InjurySeverity.LIGHT_INJURED.value,
]

count_by_severity = get_accidents_stats(
table_obj=InvolvedMarkerView,
filters=filters,
group_by="injury_severity",
count="injury_severity",
start_time=start_time,
end_time=end_time,
resolution=resolution,
)
found_severities = [d["injury_severity"] for d in count_by_severity]
items = {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
from typing import Dict, List

# noinspection PyProtectedMember
from flask_babel import _

from anyway.backend_constants import InjurySeverity, BE_CONST as BE
from anyway.request_params import RequestParams
from anyway.widgets.all_locations_widgets.killed_and_injured_count_per_age_group_widget_utils import (
KilledAndInjuredCountPerAgeGroupWidgetUtils,
AGE_RANGE_DICT,
)
from anyway.widgets.all_locations_widgets import killed_and_injured_count_per_age_group_widget_utils

from anyway.widgets.all_locations_widgets.all_locations_widget import AllLocationsWidget
from anyway.widgets.widget import register
from anyway.widgets.widget_utils import (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
from collections import defaultdict, OrderedDict
from typing import Dict, Tuple, Callable

Expand All @@ -10,7 +11,10 @@
from anyway.models import InvolvedMarkerView
from anyway.request_params import RequestParams
from anyway.utilities import parse_age_from_range
from anyway.widgets.widget_utils import get_expression_for_road_segment_location_fields
from anyway.widgets.widget_utils import (
get_expression_for_fields,
add_resolution_location_accuracy_filter,
)

# RequestParams is not hashable, so we can't use functools.lru_cache
cache_dict = OrderedDict()
Expand Down Expand Up @@ -105,20 +109,10 @@ def defaultdict_int_factory() -> Callable:
def create_query_for_killed_and_injured_count_per_age_group(
end_time, start_time, location_info, resolution
) -> BaseQuery:
if resolution == BE_CONST.ResolutionCategories.SUBURBAN_ROAD:
location_filter = get_expression_for_road_segment_location_fields(
{"road_segment_id": location_info["road_segment_id"]}, InvolvedMarkerView
)
# (InvolvedMarkerView.road1 == location_info["road1"])
# | (InvolvedMarkerView.road2 == location_info["road1"])
# ) & (InvolvedMarkerView.road_segment_name == location_info["road_segment_name"])
elif resolution == BE_CONST.ResolutionCategories.STREET:
location_filter = (
InvolvedMarkerView.involve_yishuv_name == location_info["yishuv_name"]
) & (
(InvolvedMarkerView.street1_hebrew == location_info["street1_hebrew"])
| (InvolvedMarkerView.street2_hebrew == location_info["street1_hebrew"])
)
loc_filter = adapt_location_fields_to_involve_table(location_info)
loc_filter = add_resolution_location_accuracy_filter(loc_filter,
resolution)
loc_ex = get_expression_for_fields(loc_filter, InvolvedMarkerView)

query = (
db.session.query(InvolvedMarkerView)
Expand All @@ -138,7 +132,7 @@ def create_query_for_killed_and_injured_count_per_age_group(
]
)
)
.filter(location_filter)
.filter(loc_ex)
.group_by(InvolvedMarkerView.age_group, InvolvedMarkerView.injury_severity)
.with_entities(
InvolvedMarkerView.age_group,
Expand All @@ -148,3 +142,11 @@ def create_query_for_killed_and_injured_count_per_age_group(
.order_by(asc(InvolvedMarkerView.age_group))
)
return query


def adapt_location_fields_to_involve_table(filter: dict) -> dict:
res = copy.copy(filter)
for field in ["yishuv_name", "yishuv_symbol"]:
if field in res:
res[f"involve_{field}"] = res.pop(field)
return res
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
from anyway.request_params import RequestParams
from anyway.backend_constants import BE_CONST, AccidentSeverity, AccidentType, InjurySeverity
from anyway.infographics_dictionaries import segment_dictionary
from anyway.widgets.widget_utils import get_query, get_accidents_stats
from anyway.widgets.widget_utils import (
get_query, get_accidents_stats, add_resolution_location_accuracy_filter,
)
from anyway.models import AccidentMarkerView, InvolvedMarkerView
from anyway.widgets.all_locations_widgets.all_locations_widget import AllLocationsWidget
from anyway.widgets.widget import register
Expand All @@ -28,6 +30,7 @@ def get_most_severe_accidents_with_entities(
]
# pylint: disable=no-member
filters["accident_severity"] = [AccidentSeverity.FATAL.value, AccidentSeverity.SEVERE.value]
filters = add_resolution_location_accuracy_filter(filters, resolution)
query = get_query(table_obj, filters, start_time, end_time)
query = query.with_entities(*entities)
query = query.order_by(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from typing import Dict
from anyway.widgets.widget import Widget
from anyway.widgets.widget import register
from anyway.request_params import RequestParams
from typing import Dict, Optional
# noinspection PyProtectedMember
from flask_babel import _
import logging

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import anyway.widgets.widget_utils as widget_utils
from anyway.backend_constants import BE_CONST
RC = BE_CONST.ResolutionCategories
from anyway.infographics_dictionaries import segment_dictionary
from anyway.models import VehicleMarkerView
from anyway.request_params import RequestParams
Expand Down Expand Up @@ -44,6 +45,7 @@ def get_stats_accidents_by_car_type_with_national_data(
count="provider_and_id",
start_time=request_params.start_time,
end_time=request_params.end_time,
resolution=request_params.resolution,
)

start_time = request_params.start_time
Expand Down Expand Up @@ -127,6 +129,7 @@ def percentage_accidents_by_car_type_national_data_cache(
count="provider_and_id",
start_time=start_time,
end_time=end_time,
resolution=RC.SUBURBAN_ROAD,
)
return AccidentCountByCarTypeWidget.percentage_accidents_by_car_type(
vehicle_grouped_by_type_count_unique
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def generate_items(self) -> None:
)

@staticmethod
def count_accidents_by_driver_type(request_params):
def count_accidents_by_driver_type(request_params: RequestParams):
filters = get_injured_filters(request_params)
filters["involved_type"] = [
consts.InvolvedType.DRIVER.value,
Expand All @@ -42,6 +42,7 @@ def count_accidents_by_driver_type(request_params):
cnt_distinct=True,
start_time=request_params.start_time,
end_time=request_params.end_time,
resolution=request_params.resolution,
)
driver_types = defaultdict(int)
for item in involved_by_vehicle_type_data:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def generate_items(self) -> None:
count="accident_hour",
start_time=self.request_params.start_time,
end_time=self.request_params.end_time,
resolution=self.request_params.resolution,
)

@staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def generate_items(self) -> None:
count="road_light_hebrew",
start_time=self.request_params.start_time,
end_time=self.request_params.end_time,
resolution=self.request_params.resolution,
)

@staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@

from flask_babel import _
from sqlalchemy import func, distinct, desc

from anyway.backend_constants import BE_CONST
RC = BE_CONST.ResolutionCategories
from anyway.request_params import RequestParams
from anyway.app_and_db import db
from anyway.widgets.widget_utils import get_query, run_query
from anyway.widgets.widget_utils import (
get_query,
run_query,
add_resolution_location_accuracy_filter,
get_expression_for_fields,
)
from anyway.models import VehicleMarkerView, AccidentType
from anyway.vehicle_type import VehicleCategory
from anyway.widgets.road_segment_widgets.road_segment_widget import RoadSegmentWidget
Expand Down Expand Up @@ -55,6 +61,9 @@ def accident_type_road_vs_all_count(
road_query = all_roads_query.filter(
(VehicleMarkerView.road1 == road_number) | (VehicleMarkerView.road2 == road_number)
)
loc_filter = add_resolution_location_accuracy_filter(None, RC.SUBURBAN_ROAD)
loc_ex = get_expression_for_fields(loc_filter, VehicleMarkerView)
road_query = road_query.filter(loc_ex)
road_query_result = run_query(road_query)
road_sum_accidents = 0
types_to_report = []
Expand Down Expand Up @@ -84,12 +93,16 @@ def get_accident_count_by_vehicle_type_query(
num_accidents_label: str,
vehicle_types: List[int],
) -> db.session.query:
filters = add_resolution_location_accuracy_filter(
{VehicleMarkerView.vehicle_type.name: vehicle_types},
RC.SUBURBAN_ROAD
)
return (
get_query(
table_obj=VehicleMarkerView,
start_time=start_time,
end_time=end_time,
filters={VehicleMarkerView.vehicle_type.name: vehicle_types},
filters=filters,
)
.with_entities(
VehicleMarkerView.accident_type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def generate_items(self) -> None:
count=InvolvedMarkerView.injury_severity.name,
start_time=self.request_params.start_time,
end_time=self.request_params.end_time,
resolution=self.request_params.resolution,
):
structured_data_list.append(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
from flask_babel import _
from sqlalchemy import case, func, distinct

from anyway.backend_constants import AccidentType, AccidentSeverity
from anyway.backend_constants import AccidentType, AccidentSeverity, BE_CONST
RC = BE_CONST.ResolutionCategories
from anyway.models import AccidentMarkerView
from anyway.request_params import RequestParams
from anyway.widgets.road_segment_widgets.road_segment_widget import RoadSegmentWidget
from anyway.widgets.widget import register
from anyway.widgets.widget_utils import get_query
from anyway.widgets.widget_utils import get_query, add_resolution_location_accuracy_filter

ROAD_SEGMENT_ACCIDENTS = "specific_road_segment_accidents"

Expand Down Expand Up @@ -100,9 +101,13 @@ def _get_raw_front_to_side_accidents(
)
]
)
filters = add_resolution_location_accuracy_filter(
{"road_segment_id": road_segment_id},
RC.SUBURBAN_ROAD,
)
query = get_query(
table_obj=AccidentMarkerView,
filters={"road_segment_id": road_segment_id},
filters=filters,
start_time=start_date,
end_time=end_date,
)
Expand Down
Loading

0 comments on commit df25a5c

Please sign in to comment.