Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions delta.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from nrj_energies import energies as nrj_lib
from wfr_fertilite_revenus import main as wfr_lib
from fdc_deces import deces as fdc_lib
from mor_map import mortality as mor_lib

# external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

Expand All @@ -14,6 +15,7 @@
wfr = wfr_lib.WorldPopulationStats(app)
nrj = nrj_lib.Energies(app)
fdc = fdc_lib.Deces(app)
mor = mor_lib.Mortality(app)

main_layout = html.Div([
html.Div(className = "row",
Expand All @@ -28,6 +30,8 @@
html.Br(),
dcc.Link(html.Button('Décès journaliers', style={'width':"100%"}), href='/fdc'),
html.Br(),
dcc.Link(html.Button('Causes de décès', style={'width':"100%"}), href='/mor'),
html.Br(),
html.Br(),
html.Br(),
html.Center(html.A('Code source', href='https://github.com/oricou/delta2')),
Expand Down Expand Up @@ -61,6 +65,8 @@ def display_page(pathname):
return wfr.main_layout
elif pathname == '/fdc':
return fdc.main_layout
elif pathname == '/mor':
return mor.main_layout
else:
return home_page

Expand Down
1 change: 1 addition & 0 deletions mor_map/.jukit/.jukit_info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"terminal": "nvimterm"}
16 changes: 16 additions & 0 deletions mor_map/assets/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
html { background-color: papayawhip; }
body {
min-height: 100vh;
max-width: 1600px;
background-color: papayawhip;
margin: 0 auto;
margin-top: 10px;
}

h1, h2, h3, h4, h5, h6 {
color: #222222;
}

.checkbox {
column-count: 2;
}
1 change: 1 addition & 0 deletions mor_map/data/.jukit/.jukit_info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"terminal": "nvimterm"}
141,751 changes: 141,751 additions & 0 deletions mor_map/data/IHME_GBD_2019_MORTALITY_1950_2019_45Q15_NOSHOCK_Y2020M07D31.csv

Large diffs are not rendered by default.

141,751 changes: 141,751 additions & 0 deletions mor_map/data/IHME_GBD_2019_MORTALITY_1950_2019_45Q15_WSHOCK_Y2020M11D13.csv

Large diffs are not rendered by default.

1,832 changes: 1,832 additions & 0 deletions mor_map/data/Untitled.ipynb

Large diffs are not rendered by default.

7,274 changes: 7,274 additions & 0 deletions mor_map/data/annual-number-of-deaths-by-cause.csv

Large diffs are not rendered by default.

261 changes: 261 additions & 0 deletions mor_map/data/countries.geojson

Large diffs are not rendered by default.

217 changes: 217 additions & 0 deletions mor_map/data/dataset.csv

Large diffs are not rendered by default.

7,274 changes: 7,274 additions & 0 deletions mor_map/data/second.csv

Large diffs are not rendered by default.

124 changes: 124 additions & 0 deletions mor_map/mortality.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import sys
import dash
import flask
from dash import dcc
from dash import html
import pandas as pd
import numpy as np
import plotly.graph_objs as go
import plotly.express as px
import dateutil as du
import json

class Mortality():


def __init__(self, application = None):
self.dir = 'mor_map/'
self.geo = json.load(open(self.dir + 'data/countries.geojson'))
self.df = pd.read_csv(self.dir + 'data/dataset.csv')
self.second = pd.read_csv(self.dir + 'data/second.csv')

self.main_layout = html.Div(children=[
html.H3(children='The different causes of death in the world'),
html.Div([dcc.Graph(figure=self.map(), id='map'), ], style={'width': '100%', }),

html.Br(),
html.Br(),

dcc.Markdown("""Here is the graph that represents the 9 most important causes of death in France""", id='Title'),
html.Div([dcc.Graph(figure=self.pie('France') , id='pie'), ], style={'width': '100%', }),

html.Div([dcc.Graph(figure=self.line('France',0) , id='line1'), ], style={'width': '100%', }),
html.Div([dcc.Graph(figure=self.line('France',1) , id='line2'), ], style={'width': '100%', }),
html.Div([dcc.Graph(figure=self.line('France',2) , id='line3'), ], style={'width': '100%', }),
html.Div([dcc.Graph(figure=self.line('France',3) , id='line4'), ], style={'width': '100%', }),
html.Div([dcc.Graph(figure=self.line('France',4) , id='line5'), ], style={'width': '100%', }),
html.Div([dcc.Graph(figure=self.line('France',5) , id='line6'), ], style={'width': '100%', }),
html.Div([dcc.Graph(figure=self.line('France',6) , id='line7'), ], style={'width': '100%', }),
html.Div([dcc.Graph(figure=self.line('France',7) , id='line8'), ], style={'width': '100%', }),
html.Div([dcc.Graph(figure=self.line('France',8) , id='line9'), ], style={'width': '100%', }),

], style={
'backgroundColor': 'white',
'padding': '10px 50px 10px 50px',
}
)

if application:
self.app = application
# application should have its own layout and use self.main_layout as a page or in a component
else:
self.app = dash.Dash(__name__)
self.app.layout = self.main_layout

self.app.callback(
[dash.dependencies.Output('Title', 'children'), dash.dependencies.Output('pie', 'figure'), dash.dependencies.Output('line1', 'figure'), dash.dependencies.Output('line2', 'figure'), dash.dependencies.Output('line3', 'figure'), dash.dependencies.Output('line4', 'figure'), dash.dependencies.Output('line5', 'figure'), dash.dependencies.Output('line6', 'figure'), dash.dependencies.Output('line7', 'figure'), dash.dependencies.Output('line8', 'figure'), dash.dependencies.Output('line9', 'figure')],
dash.dependencies.Input('map', 'clickData')
)(self.onClick)

# Draw the map of the world
def map(self):
df = self.df
geojson = self.geo

fig = px.choropleth(df, locations="Code",
color="Total", # lifeExp is a column of gapminder
hover_name="Entity", # column to add to hover information
color_continuous_scale=px.colors.sequential.Plasma)

fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})

return fig


def onClick(self, clickData):
if clickData:
return "Here is the graph that represents the 9 most important causes of death in " + clickData['points'][0]['hovertext'], self.pie(clickData['points'][0]['hovertext']),self.line(clickData['points'][0]['hovertext'],0),self.line(clickData['points'][0]['hovertext'],1),self.line(clickData['points'][0]['hovertext'],2),self.line(clickData['points'][0]['hovertext'],3),self.line(clickData['points'][0]['hovertext'],4),self.line(clickData['points'][0]['hovertext'],5),self.line(clickData['points'][0]['hovertext'],6),self.line(clickData['points'][0]['hovertext'],7),self.line(clickData['points'][0]['hovertext'],8)


return 'Here is the graph that represents the 9 most important causes of death in France', self.pie('France'),self.line('France',0),self.line('France',1),self.line('France',2),self.line('France',3),self.line('France',4),self.line('France',5),self.line('France',6),self.line('France',7),self.line('France',8)

# Draw the bar 9 upper charts without Entity, Code and Total of the selected country

def pie(self, country):
df = self.df
df = df[df['Entity'] == country]
df = df.drop(['Entity', 'Code', 'Total'], axis=1)
df = df.transpose()
df = df.reset_index()
df.columns = ['Cause', 'Deaths']
df = df.sort_values('Deaths', ascending=False)
df = df.head(9)

fig = px.bar(df, x='Cause', y='Deaths', color='Cause', color_discrete_sequence=px.colors.qualitative.Dark24)
return fig

# Draw the evolution in time of a cause of death in the selected country
def line(self, country, n):
df = self.df
df = df[df['Entity'] == country]
df = df.drop(['Entity', 'Code', 'Total'], axis=1)
df = df.transpose()
df = df.reset_index()
df.columns = ['Cause', 'Deaths']
df = df.sort_values('Deaths', ascending=False)
df = df.head(9)

# get the first cause of death
cause = df['Cause'].iat[n]

df = self.second
df = df[df['Entity'] == country]
df = df.drop(['Entity', 'Code'], axis=1)

df.sort_values('Year', ascending=True, inplace=True)

fig = px.line(df, x='Year', y=cause, title='Evolution of '+cause+'in '+country)


return fig

if __name__ == '__main__':
nrg = Mortality()
nrg.app.run_server(debug=True, port=8062)
68 changes: 68 additions & 0 deletions mor_map/prepare_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import sys
import pandas as pd
import numpy as np
import dateutil as du


mois = {'janv': 1, 'févr': 2, 'mars': 3, 'avr': 4, 'mai': 5, 'juin': 6, 'juil': 7, 'août': 8, 'sept': 9, 'oct': 10,
'nov': 11, 'déc': 12}

quoi = {"Prix d'une tonne de propane": [1000, 'Propane'], "Bouteille de butane de 13 kg": [13, 'Butane'],
"100 litres de FOD au tarif C1": [100, 'Fioul'], "Un litre d'essence ordinaire": [1, 'Essence'],
"Un litre de super carburant ARS": [1, 'Essence'], "Un litre de super sans plomb 95": [1, 'Essence'],
"Un litre de super sans plomb 98": [1, 'Essence'], "Un litre de gazole": [1, 'Gazole'],
"Un litre de GPLc": [1, 'GPL'], "1 kWh (contrat 3 kW)": [1, 'Electricité'],
"1 kWh (contrat 9 kW)": [1, 'Electricité'],
"Une tonne de granulés de bois en vrac": [1000 * 4.8, 'Electricité'],
"100 kWh PCI de bois en vrac": [100, 'Electricité']}

densité = {'Essence': 0.75, 'Gazole': 0.85, 'Fioul': 0.85, 'GPL': 0.55} # kg / l

# https://fr.wikipedia.org/wiki/Pouvoir_calorifique
calor = {'Essence': 47.3, 'Gazole': 44.8, 'Fioul': 42.6, 'Propane': 50.35, 'Butane': 49.51, 'GPL': 46, 'Bois': 15,
'Charbon': 20,
'Electricité': 3.6} # en MJ / kg sauf électicité en MJ / kWh

def _conv_date(d):
ma = d.split('-') # coupe la chaine au - et ainsi ma[0] est le mois et ma[1] l'année
return du.parser.parse(
f"15-{mois[ma[0].lower()]}-{ma[1]}") # parfois le mois a une majuscule d'où lower()

def _make_dataframe_from_pegase(filename):
df = pd.read_csv(filename, sep=";", encoding="latin1", skiprows=[0, 1, 3], header=None)
df = df.set_index(0).T
df['date'] = df['Période'].apply(_conv_date)
df = df.set_index('date')
df.drop(columns=['Période'], inplace=True)
df = df.replace('-', np.nan).astype('float64')
return df

bois = _make_dataframe_from_pegase("data/pegase_prix_bois_particulier.csv")
petrole = _make_dataframe_from_pegase("data/pegase_prix_petrole_particulier.csv")

bois.drop(columns=['Une tonne de granulés de bois en sacs', '100 kWh PCI de bois en sacs'], inplace=True)
petrole.drop(columns=["Tarif d'une tonne de propane en citerne", "100 kWh PCI de propane en citerne",
"100 kWh PCS de propane",
"Un litre d'essence ordinaire",
"100 kWh PCI de propane", "100 kWh PCI de FOD au tarif C1"], inplace=True) # doublons

electricite = pd.read_csv('data/prix_reglemente_electricite.csv', sep=';', decimal=',', parse_dates=['DATE_DEBUT'], infer_datetime_format=True)
electricite = electricite.set_index(['P_SOUSCRITE',"DATE_DEBUT"])
electricite.drop(columns=['DATE_FIN', 'PART_FIXE_HT', 'PART_FIXE_TTC', 'PART_VARIABLE_HT'], inplace=True)
electricite.dropna(inplace=True)
electricite = electricite.unstack().T.reset_index(0, drop=True)
electricite = electricite.rename(columns={i: f"1 kWh (contrat {i:.0f} kW)" for i in electricite.columns})
duration = pd.DatetimeIndex(electricite.index.values)
new_index = pd.DatetimeIndex(
[f"{y}-{m:02d}-15" for y in range(duration[0].year, duration[-1].year + 1) for m in range(1, 13)])
electricite = electricite.reindex(np.concatenate([electricite.index.values, new_index.values]))
electricite.sort_index(inplace=True)
electricite = electricite.fillna(method='ffill')
electricite = electricite.loc[new_index]
electricite = electricite.reindex(bois.index)
electricite.drop(columns=["1 kWh (contrat 6 kW)", "1 kWh (contrat 12 kW)", "1 kWh (contrat 15 kW)"],
inplace=True)

energie = petrole.join(bois, how='outer').join(electricite, how='outer')
#energie = pd.concat([petrole, bois, electricite]) # cela devrait faire comme ci-dessus mais j'ai 3 lignes par index...
energie.to_pickle('data/energies.pkl')