Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
784ad97
misc: Added stubs
thetos7 May 2, 2022
c36d25c
misc: renamed accidents.ipynb into accidents_exp.ipynb
thetos7 May 2, 2022
0916538
feat: Added basic empty page for accidents
thetos7 May 2, 2022
65fb7a2
feat: Added file loading to notebook
thetos7 May 2, 2022
a8097bc
misc: Changed default port of accidents app
thetos7 May 3, 2022
e51ede0
feat: Implemented basic get_data script
thetos7 May 3, 2022
185b9d7
Added simple map visualisation to notebook
thetos7 May 3, 2022
fdab4c3
Added marker map to notebook
thetos7 May 3, 2022
da6d76b
M: accidents_exp.ipynb
QwarksDev May 3, 2022
d7656fd
Removed unused immatriculations csv from get_data script
thetos7 May 3, 2022
12c16ed
Removed cell using immatriculation data
thetos7 May 3, 2022
c45c2c1
Removed population data
thetos7 May 3, 2022
096afe2
M: accidents_exp.ipynb
QwarksDev May 3, 2022
1e287e4
Added mapbox visualization in notebook
thetos7 May 3, 2022
e4f7d60
Added basic folium integration into dash
thetos7 May 3, 2022
3ab34d7
Updated visualization page
thetos7 May 3, 2022
f384b02
Refactored dashboard
thetos7 May 3, 2022
76a0051
Removed unused data
thetos7 May 3, 2022
d9fd1f4
Refactored get_data script to get multiple years
thetos7 May 3, 2022
9ae421f
Switched notebook year back to 2018
thetos7 May 3, 2022
03ca17c
Fixed get_data.py script (cleanup data for consistency between years)
thetos7 May 3, 2022
b79450e
M: accidents_exp.ipynb
QwarksDev May 4, 2022
8abe511
M: accidents_exp.ipynb
QwarksDev May 4, 2022
4a16dec
M: accidents_exp.ipynb
QwarksDev May 4, 2022
4a3b8eb
M: get_data.py
QwarksDev May 6, 2022
a5095c6
data as pickle
QwarksDev May 6, 2022
55222a1
data as pickles (per year)
QwarksDev May 6, 2022
21735b0
working in accidents.py and no more on jupyter
QwarksDev May 7, 2022
1b6d398
M: accidents.py
QwarksDev May 8, 2022
44ba1c9
refactor: Renamed variables and modules in delta.py to avoid conflicts
thetos7 Jun 2, 2022
6c5e0a8
refactor: Changed import for dcc
thetos7 Jun 2, 2022
5144fb5
Synchronizing with upstream repo and conflicts fix
thetos7 Jun 2, 2022
1103b4e
fix: Prefixed ids with group initials
thetos7 Jun 2, 2022
a89ae8e
misc: Removed initials from link in delta.py
thetos7 Jun 2, 2022
90ec727
Merge remote-tracking branch 'upstream/main' into main
thetos7 Jun 2, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions data/acc_caracs_grav-2005.pkl
1 change: 1 addition & 0 deletions data/acc_caracs_grav-2006.pkl
1 change: 1 addition & 0 deletions data/acc_caracs_grav-2007.pkl
1 change: 1 addition & 0 deletions data/acc_caracs_grav-2008.pkl
1 change: 1 addition & 0 deletions data/acc_caracs_grav-2010.pkl
1 change: 1 addition & 0 deletions data/acc_caracs_grav-2011.pkl
1 change: 1 addition & 0 deletions data/acc_caracs_grav-2012.pkl
1 change: 1 addition & 0 deletions data/acc_caracs_grav-2013.pkl
1 change: 1 addition & 0 deletions data/acc_caracs_grav-2014.pkl
1 change: 1 addition & 0 deletions data/acc_caracs_grav-2015.pkl
1 change: 1 addition & 0 deletions data/acc_caracs_grav-2016.pkl
1 change: 1 addition & 0 deletions data/acc_caracs_grav-2017.pkl
1 change: 1 addition & 0 deletions data/acc_caracs_grav-2018.pkl
1 change: 1 addition & 0 deletions data/acc_caracs_grav-2019.pkl
1 change: 1 addition & 0 deletions data/acc_caracs_grav-2020.pkl
5 changes: 5 additions & 0 deletions delta.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
from formations import formations as formations_lib
from APAAL_criminalite_education import criminalite_education
from ADHD_Movies import movies
from pgab_accidents import accidents as pgab_accidents
from ab_wg_apb_parcoursup import apb_parcoursup

#@profile
Expand Down Expand Up @@ -120,6 +121,7 @@ def init():
formations = formations_lib.Formations(app)
crim_edu = criminalite_education.Criminalite_Education(app)
mvs = movies.MoviesStats(app)
pgab_acc = pgab_accidents.Accidents(app)
apb = apb_parcoursup.APB_PARCOURSUP(app)

# external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
Expand All @@ -136,6 +138,7 @@ def init():
dcc.Link(html.Button('Décès journaliers', style={'width':"100%"}), href='/deces'),
dcc.Link(html.Button('MDMR_NYPDCallsMeteoNY', style={'width':"100%"}), href='/MDMR_NYPDCallsMeteoNY'),
dcc.Link(html.Button('Accident Routiers', style={'width':"100%", 'margin':0, 'padding': 0}), href='/accidents_routiers'),
dcc.Link(html.Button('Accidents de la route', style={'width':"100%"}), href='/pgab_accidents'),
dcc.Link(html.Button('Médailles Olympique', style={'width': "100%"}), href='/olympics'),
dcc.Link(html.Button("Génération d'énergie UE", style={'width':"100%"}), href='/Energy_generation'),
dcc.Link(html.Button('Utilisation Vélibs', style={'width':"100%"}), href='/EVHB_velib'),
Expand Down Expand Up @@ -228,6 +231,8 @@ def display_page(pathname):
return dec.main_layout
elif pathname == '/accidents_routiers':
return pm.main_layout
elif pathname == '/pgab_accidents':
return pgab_acc.main_layout
elif pathname == '/olympics':
return oly.main_layout
elif pathname == '/Energy_generation':
Expand Down
3 changes: 3 additions & 0 deletions pgab_accidents/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data/*.csv
data/20*/
map.html
198 changes: 198 additions & 0 deletions pgab_accidents/accidents.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
import glob
import dash
from dash import html
from dash import dcc
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import plotly.io as pio
import calendar as cal

class Accidents:
START = 'Start'
STOP = 'Stop'

def __init__(self, application = None):
self.df = pd.concat([pd.read_pickle(f) for f in glob.glob('data/acc_caracs_grav-*')]).sort_values(['year', 'grav'],
ascending = [True, False])
self.df.rename(columns = {'grav':'Gravité', 'year':'Année', 'mois':'Mois'}, inplace = True)
bar_fig = px.histogram(self.df, x="Année", color="Gravité")
self.zoom=4
self.center=dict(lat=46.7111, lon=1.7191)

pio.templates["missing"] = go.layout.Template(
layout_annotations=[
dict(
name="missing data watermark",
text="MISSING DATA",
textangle=-20,
opacity=.4,
font=dict(color="black", size=100),
xref="paper",
yref="paper",
x=0.5,
y=0.5,
showarrow=False,
)
]
)

self.main_layout = html.Div(children=[
html.H3(children='Répartition des accidents de la route en France métropolitaine entre 2005 et 2020'),
html.Div([
html.Div([ dcc.Graph(id='pgab-acc-main-graph'), ], style={'width':'100%', }),
html.Div([ dcc.RadioItems(id='pgab-acc-type',
options=[{'label':'Heatmap', 'value':0},
{'label':'Emplacements exacts', 'value':1}],
value=0,
labelStyle={'display':'block'}),
html.Div(html.Button(self.START, id='pgab-button-start-stop', style={'display':'inline-block'}), style={'margin-right':'15px', 'width': '7em', 'float':'right'}),
]),
]),
html.Div([
html.Br(),
html.Div(
dcc.Slider(
id='pgab-year-slider',
min=2005,
max=2020,
step = 1,
value=2005,
marks={str(year): str(year) for year in range(2005, 2021)},
),
style={'display':'inline-block', 'width':"90%"}
),
dcc.Interval(
id='pgab-auto-stepper',
interval=1500,
max_intervals = -1,
n_intervals = 0
),
], style={
'padding': '0px 50px',
'width':'100%'
}),
html.Div([ dcc.Graph(id='pgab-acc-month-bar-graph'), ], style={'width':'100%', }),
dcc.Markdown("""
Carte interactive des accidents de la route recensés entre 2005 et 2020.

Utilisez le slider afin de sélectionner l'année.

L'année 2009 reste manquante (fichier invalide), et avant 2015 la plupart des régions ne donnent pas de coordonnées GPS, les données mises à disposition sont incomplètes.

La carte comporte plusieurs calques:
* **Heatmap** des accidents
* **Emplacements exacts** des accidents

__Information complémentaire__, la gravité d'un accident correspond à:
* **1** | Indemne
* **2** | Blessures légères
* **3** | Blessures graves
* **4** | Blessures mortelles
""", style={'margin-top': '3rem'}),

html.Div([ dcc.Graph(id='acc-bar-graph', figure=bar_fig), ], style={'width':'100%', }),
dcc.Markdown("""
Histogramme des accidents de la route recensés entre 2005 et 2020.

L'année 2009 reste manquante, les données mises à disposition sont incomplètes.

#### À propos
* Données: [data.gouv.fr](https://www.data.gouv.fr/fr/datasets/bases-de-donnees-annuelles-des-accidents-corporels-de-la-circulation-routiere-annees-de-2005-a-2020/)

© 2022 Paul Galand & Ancelin Bouchet
""", style={'margin-top': '3rem'}),
], style={
'backgroundColor': 'white',
'padding': '10px 50px 10px 50px',
'display': 'flex',
'flex-direction': 'column'
})

if application:
self.app = application
else:
self.app = dash.Dash(__name__)
self.app.layout = self.main_layout

self.app.callback(
dash.dependencies.Output('pgab-acc-month-bar-graph', 'figure'),
dash.dependencies.Input('pgab-year-slider', 'value'))(self.update_month_bar_graph)
self.app.callback(
dash.dependencies.Output('pgab-acc-main-graph', 'figure'),
[ dash.dependencies.Input('pgab-acc-type', 'value'),
dash.dependencies.Input('pgab-year-slider', 'value')])(self.update_main_graph)
self.app.callback(
dash.dependencies.Output('pgab-button-start-stop', 'children'),
dash.dependencies.Input('pgab-button-start-stop', 'n_clicks'),
dash.dependencies.State('pgab-button-start-stop', 'children'))(self.button_on_click)
self.app.callback(
dash.dependencies.Output('pgab-auto-stepper', 'max_interval'),
[dash.dependencies.Input('pgab-button-start-stop', 'children')])(self.run_movie)
self.app.callback(
dash.dependencies.Output('pgab-year-slider', 'value'),
dash.dependencies.Input('pgab-auto-stepper', 'n_intervals'),
[dash.dependencies.State('pgab-year-slider', 'value'),
dash.dependencies.State('pgab-button-start-stop', 'children')])(self.on_interval)

def update_month_bar_graph(self, year):
dfg = self.df
dfg = dfg[dfg['Année'] == str(year)]
if dfg.empty:
pio.templates.default = "missing"
else:
pio.templates.default = None
return px.histogram(dfg, x="Mois", color="Gravité")

def update_main_graph(self, acc_type, year):
dfg = self.df
dfg = dfg[dfg['Année'] == str(year)]
if dfg.empty:
pio.templates.default = "missing"
else:
pio.templates.default = None
if acc_type:
fig=px.scatter_mapbox(dfg, lat='lat', lon='long',
color='Gravité', mapbox_style='carto-positron',
zoom=self.zoom, center=self.center,
color_continuous_scale=px.colors.sequential.Bluered,
custom_data=['jour', 'Mois', 'Année', 'Gravité'])
fig.update_layout(height=800)
else:
fig=px.density_mapbox(dfg, z=None, lat='lat', lon='long',
radius=5, opacity=.6, mapbox_style='carto-positron',
zoom=self.zoom, center=self.center,
color_continuous_scale=px.colors.diverging.Picnic,
custom_data=['jour', 'Mois', 'Année', 'Gravité'])
fig.update_layout(coloraxis_showscale=False, height=800)
fig.update_traces(hovertemplate="Date: %{customdata[0]}/%{customdata[1]}/%{customdata[2]} | Gravité: %{customdata[3]}")
fig.update_layout(uirevision='constant')
return fig

def button_on_click(self, n_clicks, text):
if text == self.START:
return self.STOP
else:
return self.START

def run_movie(self, text):
if text == self.START:
return 0
else:
return -1

def on_interval(self, n_intervals, year, text):
if text == self.STOP:
if year == 2020:
return 2005
if year == 2008:
return 2010
return year + 1
return year

def run(self, debug=False, port=8050):
self.app.run_server(host="0.0.0.0", debug=debug, port=port)

if __name__ == '__main__':
acc = Accidents()
acc.run(port=8065)
Binary file added pgab_accidents/data/acc_caracs_grav-2005.pkl
Binary file not shown.
Binary file added pgab_accidents/data/acc_caracs_grav-2006.pkl
Binary file not shown.
Binary file added pgab_accidents/data/acc_caracs_grav-2007.pkl
Binary file not shown.
Binary file added pgab_accidents/data/acc_caracs_grav-2008.pkl
Binary file not shown.
Binary file added pgab_accidents/data/acc_caracs_grav-2010.pkl
Binary file not shown.
Binary file added pgab_accidents/data/acc_caracs_grav-2011.pkl
Binary file not shown.
Binary file added pgab_accidents/data/acc_caracs_grav-2012.pkl
Binary file not shown.
Binary file added pgab_accidents/data/acc_caracs_grav-2013.pkl
Binary file not shown.
Binary file added pgab_accidents/data/acc_caracs_grav-2014.pkl
Binary file not shown.
Binary file added pgab_accidents/data/acc_caracs_grav-2015.pkl
Binary file not shown.
Binary file added pgab_accidents/data/acc_caracs_grav-2016.pkl
Binary file not shown.
Binary file added pgab_accidents/data/acc_caracs_grav-2017.pkl
Binary file not shown.
Binary file added pgab_accidents/data/acc_caracs_grav-2018.pkl
Binary file not shown.
Binary file added pgab_accidents/data/acc_caracs_grav-2019.pkl
Binary file not shown.
Binary file added pgab_accidents/data/acc_caracs_grav-2020.pkl
Binary file not shown.
Loading