@@ -49,6 +49,7 @@ class WidgetId(Enum):
4949 vision_zero = auto ()
5050 accident_count_by_driver_type = auto ()
5151 accident_count_by_car_type = auto ()
52+ rain_accidents_by_severity = auto ()
5253 injured_accidents_with_pedestrians = auto ()
5354 accident_severity_by_cross_location = auto ()
5455 motorcycle_accidents_vs_all_accidents = auto ()
@@ -735,8 +736,10 @@ def __init__(self, request_params: RequestParams):
735736 }
736737
737738 def generate_items (self ) -> None :
738- self .items = AccidentCountByCarTypeWidget .get_stats_accidents_by_car_type_with_national_data (
739- self .request_params
739+ self .items = (
740+ AccidentCountByCarTypeWidget .get_stats_accidents_by_car_type_with_national_data (
741+ self .request_params
742+ )
740743 )
741744
742745 @staticmethod
@@ -759,8 +762,10 @@ def get_stats_accidents_by_car_type_with_national_data(
759762 data_by_segment = AccidentCountByCarTypeWidget .percentage_accidents_by_car_type (
760763 involved_by_vehicle_type_data
761764 )
762- national_data = AccidentCountByCarTypeWidget .percentage_accidents_by_car_type_national_data_cache (
763- start_time , end_time
765+ national_data = (
766+ AccidentCountByCarTypeWidget .percentage_accidents_by_car_type_national_data_cache (
767+ start_time , end_time
768+ )
764769 )
765770
766771 for k , v in national_data .items (): # pylint: disable=W0612
@@ -1041,6 +1046,66 @@ def pedestrian_injured_in_junctions_mock_data(): # Temporary for Frontend
10411046 ]
10421047
10431048
1049+ @WidgetCollection .register
1050+ class AccidentCausedByRainWidget (Widget ):
1051+ # the rain rate threshold after which we count the accident as a cause of the rain
1052+ ACCIDENT_RAIN_RATE_THRESHOLD = 4
1053+
1054+ def __init__ (self , request_params : RequestParams ):
1055+ super ().__init__ (request_params , WidgetId .rain_accidents_by_severity )
1056+ self .rank = 24
1057+ self .text = {
1058+ "title" : "תאונות שהתרחשו בזמן גשם במקטע "
1059+ + self .request_params .location_info ["road_segment_name" ]
1060+ }
1061+
1062+ def generate_items (self ) -> None :
1063+ self .items = AccidentCausedByRainWidget .stats_accidents_caused_by_rain_by_severity (
1064+ self .request_params .location_info ,
1065+ self .request_params .start_time ,
1066+ self .request_params .end_time ,
1067+ )
1068+
1069+ @staticmethod
1070+ def stats_accidents_caused_by_rain_by_severity (location_info , start_time , end_time ):
1071+ all_segment_accidents = get_accidents_stats (
1072+ table_obj = AccidentMarkerView ,
1073+ filters = location_info ,
1074+ start_time = start_time ,
1075+ end_time = end_time ,
1076+ raw = True ,
1077+ )
1078+
1079+ severity_to_severity_hebrew = {}
1080+ accidents_by_severity = defaultdict (int )
1081+ rain_accidents_by_severity = defaultdict (int )
1082+ for accident in all_segment_accidents :
1083+ severity = accident ["accident_severity" ]
1084+ severity_hebrew = accident ["accident_severity_hebrew" ]
1085+ severity_to_severity_hebrew [severity ] = severity_hebrew
1086+ accidents_by_severity [severity ] += 1
1087+ if (
1088+ accident ["accident_rain_rate" ]
1089+ > AccidentCausedByRainWidget .ACCIDENT_RAIN_RATE_THRESHOLD
1090+ ):
1091+ rain_accidents_by_severity [severity ] += 1
1092+
1093+ stats = []
1094+ for severity , rain_accidents_amount in rain_accidents_by_severity .items ():
1095+ stats .append (
1096+ {
1097+ "severity" : severity ,
1098+ "severity_hebrew" : severity_to_severity_hebrew [severity ],
1099+ "amount_of_accidents_caused_by_rain" : rain_accidents_amount ,
1100+ "accidents_caused_by_rain_percentage" : int (
1101+ rain_accidents_amount / accidents_by_severity [severity ] * 100
1102+ ),
1103+ }
1104+ )
1105+
1106+ return stats
1107+
1108+
10441109def extract_news_flash_location (news_flash_obj ):
10451110 resolution = news_flash_obj .resolution or None
10461111 if not news_flash_obj or not resolution or resolution not in resolution_dict :
@@ -1074,7 +1139,7 @@ def get_query(table_obj, filters, start_time, end_time):
10741139
10751140
10761141def get_accidents_stats (
1077- table_obj , filters = None , group_by = None , count = None , start_time = None , end_time = None
1142+ table_obj , filters = None , group_by = None , count = None , start_time = None , end_time = None , raw = False
10781143):
10791144 filters = filters or {}
10801145 filters ["provider_code" ] = [
@@ -1088,10 +1153,9 @@ def get_accidents_stats(
10881153 query = query .with_entities (group_by , func .count (count ))
10891154 df = pd .read_sql_query (query .statement , query .session .bind )
10901155 df .rename (columns = {"count_1" : "count" }, inplace = True ) # pylint: disable=no-member
1091- df .columns = [c .replace ("_hebrew" , "" ) for c in df .columns ]
1092- return ( # pylint: disable=no-member
1093- df .to_dict (orient = "records" ) if group_by or count else df .to_dict ()
1094- )
1156+ if not raw :
1157+ df .columns = [c .replace ("_hebrew" , "" ) for c in df .columns ]
1158+ return df .to_dict (orient = "records" ) # pylint: disable=no-member
10951159
10961160
10971161def get_injured_filters (location_info ):
0 commit comments