Skip to content

Commit

Permalink
wip - implementing Aatalya comment from 1-Jan on issue 2734
Browse files Browse the repository at this point in the history
  • Loading branch information
ziv17 committed Jan 12, 2025
1 parent 6a88300 commit 03ee28c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 79 deletions.
4 changes: 2 additions & 2 deletions alembic/versions/99364b16374f_add_safety_data_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def create_safety_data_involved_table():
sa.Column('injury_severity', sa.Integer(), nullable=True),
sa.Column('population_type', sa.Integer(), nullable=True),
sa.Column('sex', sa.Integer(), nullable=True),
sa.Column('vehicle_vehicle_type', sa.Integer(), nullable=True),
sa.Column('vehicle_type', sa.Integer(), nullable=True),
)
op.create_foreign_key(f'{sd_involved_table}_accident_id_fkey', # pylint: disable=no-member
sd_involved_table, sd_accident_table,
Expand All @@ -88,7 +88,7 @@ def create_safety_data_involved_table():
op.create_index(op.f(f'ix_{sd_involved_table}_inv_acc'), sd_involved_table, # pylint: disable=no-member
['accident_id', 'accident_year', 'provider_code'], unique=False)
for field in ['injury_severity', 'injured_type',
'age_group', 'sex', 'population_type']:
'age_group', 'sex', 'population_type', 'vehicle_type']:
# pylint: disable=no-member
op.create_index(op.f(f'ix_{sd_involved_table}_{field}'), sd_involved_table, [field], unique=False)

2 changes: 1 addition & 1 deletion anyway/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3094,7 +3094,7 @@ class SDInvolved(Base):
injury_severity = Column(Integer(), nullable=True)
population_type = Column(Integer(), nullable=True)
sex = Column(Integer(), nullable=True)
vehicle_vehicle_type = Column(Integer(), nullable=True)
vehicle_type = Column(Integer(), nullable=True)


class SDAccident(Base):
Expand Down
98 changes: 34 additions & 64 deletions anyway/views/safety_data/involved_query.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import List, Dict
from typing import List, Dict, Optional
import math
import pandas as pd
from sqlalchemy.orm import aliased
from sqlalchemy import and_
Expand Down Expand Up @@ -32,6 +33,8 @@
class InvolvedQuery:
PEDESTRIAN_IN_VEHICLE_TYPE_ENRICHED = 99
PEDESTRIAN_HEBREW = "הולך רגל"
INJURED_ENRICHED_MULT_FACTOR = 1000
INJURED_ENRICHED_ADD_FACTOR = 100
vehicle_type_to_str = [None for _ in range(1, 27)]
vehicle_type_to_str[1] = ("רכב נוסעים פרטי", "רכב נוסעים פרטי")
vehicle_type_to_str[2] = ("טרנזיט", "משא עד 3.5 טון - אחוד (טרנזיט)")
Expand Down Expand Up @@ -65,33 +68,16 @@ def __init__(self):
self.fill_text_tables()

def get_data(self):
def f(v: List[str]) -> List[str]:
res = []
[res.extend(x.split(",")) for x in v]
return res
params = request.values
vals = {k: params.getlist(key=k) for k in params.keys()}
vals = {k: f(params.getlist(key=k)) for k in params.keys()}
query = self.get_base_query()
query = ParamFilterExp.add_params_filter(query, vals)
# pylint: disable=no-member
df = pd.read_sql_query(query.statement, query.session.bind)
df.rename(
columns={
# 'accident_type': 'accident_type_hebrew',
# 'day_night': 'day_night_hebrew',
# "multi_lane": "multi_lane_hebrew",
# "one_lane": "one_lane_hebrew",
# "road_type": "road_type_hebrew",
# "road_width": "road_width_hebrew",
# "speed_limit": "speed_limit_hebrew",
# "street1": "street1_hebrew",
# "street2": "street2_hebrew",
# "location_accuracy": "location_accuracy_hebrew",
# "age_group": "age_group_hebrew",
# "injured_type": "injured_type_hebrew",
# "injury_severity": "injury_severity_hebrew",
# "population_type": "population_type_hebrew",
# "sex": "sex_hebrew",
"vehicle_vehicle_type": "vehicle_vehicle_type_hebrew",
},
inplace=True,
)
data = df.to_dict(orient="records") # pylint: disable=no-member
[self.add_text(d) for d in data]
return data
Expand Down Expand Up @@ -121,11 +107,13 @@ def get_base_query(self):
SDAccident.longitude,
SDInvolved._id,
AgeGroup.age_group_hebrew,
InjuredType.injured_type_hebrew,
SDInvolved.injured_type.label("injured_type_hebrew"),
InjurySeverity.injury_severity_hebrew,
PopulationType.population_type_hebrew,
SDInvolved.vehicle_vehicle_type.label("vehicle_vehicle_type_hebrew"),
SDInvolved.vehicle_type.label("vehicle_vehicle_type_hebrew"),
Sex.sex_hebrew,
SDInvolved.vehicle_type.label("TEST-vehicle_type"),
SDInvolved.injured_type.label("TEST-injured_type"),
)
.join(
SDAccident,
Expand Down Expand Up @@ -255,49 +243,34 @@ def get_base_query(self):
)
.outerjoin(City, SDAccident.accident_yishuv_symbol == City.yishuv_symbol)
.outerjoin(RoadSegments, SDAccident.road_segment_number == RoadSegments.segment)
# .filter(Column.__ge__(SDAccident.yishuv_symbol, 5000))
# .filter(SDInvolved.id == 26833652)
# .with_entities(
# SDAccident.accident_timestamp,
# SDAccident.accident_type,
# SDAccident.accident_year,
# SDAccident.vehicles,
# SDAccident.latitude,
# SDAccident.longitude,
# SDInvolved._id,
# SDInvolved.age_group,
# SDInvolved.injured_type,
# SDInvolved.injury_severity,
# SDInvolved.population_type,
# SDInvolved.sex,
# )
)
return query

def add_text(self, d: dict) -> None:
# n = d["injury_severity_hebrew"]
# d["injury_severity_hebrew"] = self.injury_severity[n] if n else None
# n = d["sex_hebrew"]
# d["sex_hebrew"] = self.sex[n] if n else None
# n = d["age_group_hebrew"]
# d["age_group_hebrew"] = ("85+" if n == 99 else self.age_group[n]) if n else None
# n = d["injured_type_hebrew"]
# d["injured_type_hebrew"] = self.injured_type[n] if n else None
# n = d["population_type_hebrew"]
# d["population_type_hebrew"] = self.population_type[n] if n else None
d["vehicles"] = self.vehicle_type_bit_2_heb(d["vehicles"])
n = d["day_in_week_hebrew"]
d["day_in_week_hebrew"] = self.day_in_week[n] if n else None
n = d["vehicle_vehicle_type_hebrew"]
injured_type = d["injured_type_hebrew"]
vehicle_type = d["vehicle_vehicle_type_hebrew"]
vehicle_type = None if math.isnan(vehicle_type) else int(vehicle_type)
d["vehicle_vehicle_type_hebrew"] = (
(
self.PEDESTRIAN_HEBREW
if n == self.PEDESTRIAN_IN_VEHICLE_TYPE_ENRICHED
else self.vehicle_type_to_str[n][1]
self.PEDESTRIAN_HEBREW if injured_type == 1 else (
self.vehicle_type_to_str[vehicle_type][1] if vehicle_type
else None
)
if n
else None
)
d["injured_type_hebrew"] = self.get_injured_type_enriched_hebrew(injured_type, vehicle_type)

def get_injured_type_enriched_hebrew(self, injured_type: Optional[int], vehicle_type: Optional[int]) -> Optional[str]:
vehicle_hebrew_short = self.vehicle_type_to_str[vehicle_type][0] if vehicle_type else None
injured_hebrew = self.injured_type[injured_type] if isinstance(injured_type, int) else None
if not injured_type:
return f"{vehicle_hebrew_short} - מעורב שלא נפגע"
elif vehicle_type is not None:
return f"{injured_hebrew.split(' - ')[0]} - {vehicle_hebrew_short}"
else:
return injured_hebrew


@staticmethod
def vehicle_type_bit_2_heb(bit_map: int) -> str:
Expand Down Expand Up @@ -420,12 +393,9 @@ class ParamFilterExp:
"mn": {
"col": SDAccident.accident_month,
},
# "acc": {
# "col": SDAccident.accident_type,
# },
# "vcl": {
# "col": SDAccident.vehicle_types,
# },
"acc": {
"col": SDAccident.accident_type,
},
"rt": {
"col": SDAccident.road_type,
},
Expand Down
15 changes: 3 additions & 12 deletions anyway/views/safety_data/sd_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from typing import Optional
from sqlalchemy.orm import aliased
from sqlalchemy import and_
from flask import request, Response
Expand Down Expand Up @@ -55,8 +56,6 @@ def get_data():
.join(AgeGroup, and_(Involved.age_group == AgeGroup.id,
Involved.accident_year == AgeGroup.year,
Involved.provider_code == AgeGroup.provider_code))
# .filter(AccidentMarkerView.yishuv_symbol == 5000)
# .filter(Involved.id == 26833652)
.with_entities(
Involved.id,
AccidentMarkerView.accident_year,
Expand Down Expand Up @@ -93,10 +92,6 @@ def sd_load_involved():
rows
)
rows = []
# db.get_engine().execute(
# SDInvolved.__table__.insert(),
# [d for d in get_involved_data()]
# )
db.session.commit()

def get_involved_data():
Expand All @@ -116,7 +111,6 @@ def get_involved_data():
Involved.sex,
Involved.vehicle_type,
):
# .limit(100000):
yield {
"_id": d.id,
"accident_id": d.accident_id,
Expand All @@ -127,10 +121,7 @@ def get_involved_data():
"injury_severity": d.injury_severity,
"population_type": d.population_type,
"sex": d.sex,
"vehicle_vehicle_type": (
InvolvedQuery.PEDESTRIAN_IN_VEHICLE_TYPE_ENRICHED
if d.injured_type == 1 else d.vehicle_type
),
"vehicle_type": d.vehicle_type,
}

def sd_load_accident():
Expand Down Expand Up @@ -192,7 +183,7 @@ def sd_load_accident_main():
AccidentMarkerView.latitude,
AccidentMarkerView.longitude,
)
.limit(10000)
.limit(100000)
]
)
db.session.commit()
Expand Down

0 comments on commit 03ee28c

Please sign in to comment.