From 9c2c71aaa547ab838e19a54a69069dad9b84171d Mon Sep 17 00:00:00 2001 From: ziv Date: Thu, 23 May 2024 22:46:44 +0300 Subject: [PATCH 1/2] Support multiple suburban junctions in same km. --- anyway/widgets/segment_junctions.py | 16 ++++++++++------ tests/test_infographics_utils.py | 8 +++++++- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/anyway/widgets/segment_junctions.py b/anyway/widgets/segment_junctions.py index 447153cb..a32934e1 100644 --- a/anyway/widgets/segment_junctions.py +++ b/anyway/widgets/segment_junctions.py @@ -36,7 +36,12 @@ def __calc_fill_segment_junctions(): if t.road not in rkj: rkj[t.road] = {} road_last_junction_km[t.road] = -1 - rkj[t.road][t.km] = t.non_urban_intersection + if t.km not in rkj[t.road]: + rkj[t.road][t.km] = [] + else: + logging.debug(f"Two junctions in same location:road:{t.road},km:{t.km},1:" + f"{rkj[t.road][t.km]},2:{t.non_urban_intersection}.") + rkj[t.road][t.km].append(t.non_urban_intersection) if road_last_junction_km[t.road] < t.km: road_last_junction_km[t.road] = t.km tmp: List[RoadSegments] = db.session.query(RoadSegments).all() @@ -45,11 +50,10 @@ def __calc_fill_segment_junctions(): if seg.road not in rkj: logging.warning(f"No junctions in road {seg.road}.") continue - junctions = [ - rkj[seg.road][km] - for km in rkj[seg.road].keys() - if is_junction_km_in_segment(km, seg, road_last_junction_km.get(seg.road)) - ] + junctions = [] + for km in rkj[seg.road].keys(): + if is_junction_km_in_segment(km, seg, road_last_junction_km.get(seg.road)): + junctions.extend(rkj[seg.road][km]) res[seg_id] = junctions return res diff --git a/tests/test_infographics_utils.py b/tests/test_infographics_utils.py index dfe289e5..371828a9 100644 --- a/tests/test_infographics_utils.py +++ b/tests/test_infographics_utils.py @@ -54,9 +54,11 @@ class TestInfographicsUtilsCase(unittest.TestCase): rjks = [ RoadJunctionKM(road=1, non_urban_intersection=1, km=1.0), RoadJunctionKM(road=1, non_urban_intersection=2, km=2.0), + RoadJunctionKM(road=1, non_urban_intersection=22, km=2.0), RoadJunctionKM(road=1, non_urban_intersection=3, km=3.0), RoadJunctionKM(road=10, non_urban_intersection=1, km=10.0), RoadJunctionKM(road=20, non_urban_intersection=2, km=10.0), + RoadJunctionKM(road=20, non_urban_intersection=22, km=10.0), RoadJunctionKM(road=30, non_urban_intersection=3, km=10.0), ] segments = [ @@ -90,7 +92,11 @@ def test_get_segment_junctions(self, db): actual = sg.get_segment_junctions(2) self.assertEqual([1], actual, "2") actual = sg.get_segment_junctions(3) - self.assertEqual([1, 2, 3], actual, "3") + self.assertEqual([1, 2, 22, 3], actual, "3") + actual = sg.get_segment_junctions(4) + self.assertEqual([3], actual, "4") + actual = sg.get_segment_junctions(21) + self.assertEqual([2, 22], actual, "5") def test_get_filter_expression(self): actual = get_filter_expression(AccidentMarkerView, "road_segment_name", "seg1") From a0f9a2d68a159d5df2d832bec997fde91340a380 Mon Sep 17 00:00:00 2001 From: ziv Date: Thu, 23 May 2024 23:38:56 +0300 Subject: [PATCH 2/2] Fix lint and black errors --- .../killed_and_injured_count_per_age_group_widget_utils.py | 5 +++-- .../head_on_collisions_comparison_widget.py | 4 ++-- anyway/widgets/segment_junctions.py | 6 ++++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/anyway/widgets/all_locations_widgets/killed_and_injured_count_per_age_group_widget_utils.py b/anyway/widgets/all_locations_widgets/killed_and_injured_count_per_age_group_widget_utils.py index 1bc72edc..d3332e64 100644 --- a/anyway/widgets/all_locations_widgets/killed_and_injured_count_per_age_group_widget_utils.py +++ b/anyway/widgets/all_locations_widgets/killed_and_injured_count_per_age_group_widget_utils.py @@ -4,6 +4,7 @@ from flask_sqlalchemy import BaseQuery from sqlalchemy import func, asc +# noinspection PyProtectedMember from flask_babel import _ from anyway.app_and_db import db @@ -34,8 +35,8 @@ def filter_and_group_injured_count_per_age_group( ) -> Dict[str, Dict[int, int]]: start_time = request_params.start_time end_time = request_params.end_time - cache_key = None #prevent pylint warning - + cache_key = None # prevent pylint warning + if request_params.resolution == BE_CONST.ResolutionCategories.STREET: involve_yishuv_name = request_params.location_info["yishuv_name"] street1_hebrew = request_params.location_info["street1_hebrew"] diff --git a/anyway/widgets/road_segment_widgets/head_on_collisions_comparison_widget.py b/anyway/widgets/road_segment_widgets/head_on_collisions_comparison_widget.py index 17c4fdd3..f930a9eb 100644 --- a/anyway/widgets/road_segment_widgets/head_on_collisions_comparison_widget.py +++ b/anyway/widgets/road_segment_widgets/head_on_collisions_comparison_widget.py @@ -107,7 +107,7 @@ def is_included(self) -> bool: segment_others = item["count"] else: raise ValueError - segment_total = segment_h2h + segment_others + segment_total = segment_h2h + segment_others # pylint: disable=E0606 all_items = self.items[self.ALL_ROADS_SUBTITLE] for item in all_items: if item["desc"] == "frontal": @@ -116,7 +116,7 @@ def is_included(self) -> bool: all_others = item["count"] else: raise ValueError - all_total = all_h2h + all_others + all_total = all_h2h + all_others # pylint: disable=E0606 return segment_h2h > 0 and (segment_h2h / segment_total) > all_h2h / all_total diff --git a/anyway/widgets/segment_junctions.py b/anyway/widgets/segment_junctions.py index a32934e1..a261eaa3 100644 --- a/anyway/widgets/segment_junctions.py +++ b/anyway/widgets/segment_junctions.py @@ -39,8 +39,10 @@ def __calc_fill_segment_junctions(): if t.km not in rkj[t.road]: rkj[t.road][t.km] = [] else: - logging.debug(f"Two junctions in same location:road:{t.road},km:{t.km},1:" - f"{rkj[t.road][t.km]},2:{t.non_urban_intersection}.") + logging.debug( + f"Two junctions in same location:road:{t.road},km:{t.km},1:" + f"{rkj[t.road][t.km]},2:{t.non_urban_intersection}." + ) rkj[t.road][t.km].append(t.non_urban_intersection) if road_last_junction_km[t.road] < t.km: road_last_junction_km[t.road] = t.km