Skip to content

Commit

Permalink
Finish v1.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
daniprec committed May 11, 2021
2 parents d59e35b + b86e7bb commit 14e26c6
Show file tree
Hide file tree
Showing 13 changed files with 143 additions and 29 deletions.
29 changes: 19 additions & 10 deletions covidnpi/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ movavg = 7
link = "https://cnecovid.isciii.es/covid19/resources/casos_tecnica_provincia.csv"

[postal_to_code]
1 = "VI"
2 = "AB"
3 = "A"
4 = "AL"
5 = "AV"
6 = "BA"
7 = "PM"
8 = "B"
9 = "BU"
01 = "VI"
02 = "AB"
03 = "A"
04 = "AL"
05 = "AV"
06 = "BA"
07 = "PM"
08 = "B"
09 = "BU"
10 = "CC"
11 = "CA"
12 = "CS"
Expand Down Expand Up @@ -67,7 +67,6 @@ link = "https://cnecovid.isciii.es/covid19/resources/casos_tecnica_provincia.csv
51 = "CE"
52 = "ML"


[code_to_provincia]
A = "Alacant"
AB = "Albacete"
Expand Down Expand Up @@ -159,6 +158,7 @@ navarra = "NA"
orense = "OR"
asturias = "O"
palencia = "P"
grancanaria = "GC"
gran_canaria = "GC"
pontevedra = "PO"
salamanca = "SA"
Expand All @@ -168,6 +168,7 @@ segovia = "SG"
sevilla = "SE"
soria = "SO"
tarragona = "T"
tenerife = "TF"
teruel = "TE"
toledo = "TO"
valencia = "V"
Expand All @@ -176,6 +177,14 @@ vizcaya = "BI"
zamora = "ZA"
zaragoza = "Z"

[isla_to_provincia]
elhierro = "grancanaria"
formentera = "tenerife"
fuerteventura = "grancanaria"
ibiza = "tenerife"
lagomera = "grancanaria"
lanzarote = "grancanaria"
menorca = "tenerife"

[code_to_poblacion]
AB = 388270
Expand Down
2 changes: 1 addition & 1 deletion covidnpi/preprocess_and_score.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
store_dict_scores,
store_dict_provincia_to_medidas,
)
from covidnpi.utils.logging import logger
from covidnpi.utils.log import logger
from covidnpi.utils.mobility import mobility_report_to_csv
from covidnpi.utils.preprocess import read_npi_and_build_dict
from covidnpi.utils.taxonomia import PATH_TAXONOMIA
Expand Down
2 changes: 1 addition & 1 deletion covidnpi/score/score_ambitos.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import typer

from covidnpi.utils.dictionaries import store_dict_scores, load_dict_scores
from covidnpi.utils.logging import logger
from covidnpi.utils.log import logger
from covidnpi.utils.taxonomia import return_item_ponderacion, PATH_TAXONOMIA


Expand Down
2 changes: 1 addition & 1 deletion covidnpi/score/score_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import typer

from covidnpi.utils.dictionaries import store_dict_scores, load_dict_scores
from covidnpi.utils.logging import logger
from covidnpi.utils.log import logger


def score_items(df: pd.DataFrame):
Expand Down
2 changes: 1 addition & 1 deletion covidnpi/score/score_medidas.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import typer

from covidnpi.utils.dictionaries import store_dict_scores, load_dict_medidas
from covidnpi.utils.logging import logger
from covidnpi.utils.log import logger
from covidnpi.utils.taxonomia import return_taxonomia, return_all_medidas

warnings.simplefilter(action="ignore", category=FutureWarning)
Expand Down
51 changes: 49 additions & 2 deletions covidnpi/utils/combine.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import pandas as pd
import typer
from pathlib import Path
from typing import Union

import numpy as np
import pandas as pd
import typer

from covidnpi.utils.config import load_config
from covidnpi.utils.dictionaries import reverse_dictionary
from covidnpi.utils.log import logger

COLS_AMBITO = [
"fecha",
"provincia",
Expand Down Expand Up @@ -38,12 +44,53 @@ def combine_csv(path: Union[Path, str], colname: str) -> pd.DataFrame:
return pd.concat(df_dict, names=[colname]).reset_index().drop(columns="level_1")


def add_unidad_territorial(
df: pd.DataFrame, path_config: str = "covidnpi/config.toml"
) -> pd.DataFrame:
# Load all conversion dictionaries
isle_to_province = load_config(path_config, "isla_to_provincia")
# Check for islands
unidad = df["provincia"].copy()
province = df["provincia"].replace(isle_to_province)
# Create unidad_territorial column, that contains the islands
df.insert(loc=2, column="unidad_territorial", value=unidad)
df.loc[unidad == province, "unidad_territorial"] = np.nan
df["provincia"] = province
return df


def add_province_code(
df: pd.DataFrame, path_config: str = "covidnpi/config.toml"
) -> pd.DataFrame:
# Load all conversion dictionaries
province_to_code = load_config(path_config, "provincia_to_code")
code_to_province = load_config(path_config, "code_to_provincia")
postal_to_code = load_config(path_config, "postal_to_code")
code_to_postal = reverse_dictionary(postal_to_code)
# Get codes
code = df["provincia"].map(province_to_code)
# Replace province name and add code
df["provincia"] = code.map(code_to_province)
df.insert(loc=1, column="cod_prov", value=code.map(code_to_postal))
return df


def add_ccaa(df: pd.DataFrame, path_ccaa: str = "data/CCAA.csv") -> pd.DataFrame:
ccaa = pd.read_csv(path_ccaa, dtype={"Codigo": str, "Cod_CCAA": str})
code_to_ccaa = dict(zip(ccaa["Codigo"], ccaa["CCAA"]))
df.insert(loc=1, column="ccaa", value=df["cod_prov"].map(code_to_ccaa))
code_to_codccaa = dict(zip(ccaa["Codigo"], ccaa["Cod_CCAA"]))
df.insert(loc=2, column="cod_ccaa", value=df["cod_prov"].map(code_to_codccaa))
return df


def combine_csv_ambito(
path_data: str = "output/score_ambito", path_output: str = "npi_stringency.csv"
) -> pd.DataFrame:
df = combine_csv(path_data, "provincia")
# Tomar las columnas relevantes y ordenar por fecha
df = df[COLS_AMBITO].sort_values(["fecha", "provincia"])
df = df.pipe(add_unidad_territorial).pipe(add_province_code).pipe(add_ccaa)
df.to_csv(path_output, index=False)


Expand Down
5 changes: 5 additions & 0 deletions covidnpi/utils/dictionaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,8 @@ def load_dict_scores(path_scores: str = "output/score_medidas"):
df = pd.read_csv(path_file, index_col="fecha")
dict_scores.update({provincia: df})
return dict_scores


def reverse_dictionary(d: dict) -> dict:
reversed_dictionary = {value: key for (key, value) in d.items()}
return reversed_dictionary
File renamed without changes.
2 changes: 1 addition & 1 deletion covidnpi/utils/mobility.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from covidnpi.utils.casos import load_casos_df, return_casos_of_provincia_normed
from covidnpi.utils.config import load_config
from covidnpi.utils.logging import logger
from covidnpi.utils.log import logger
from covidnpi.utils.series import (
cumulative_incidence,
compute_growth_rate,
Expand Down
20 changes: 10 additions & 10 deletions covidnpi/utils/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import xlrd

from covidnpi.utils.dictionaries import store_dict_provincia_to_medidas
from covidnpi.utils.logging import logger, raise_type_warning, raise_value_warning
from covidnpi.utils.log import logger, raise_type_warning, raise_value_warning
from covidnpi.utils.taxonomia import return_all_medidas, PATH_TAXONOMIA

LIST_BASE_SHEET = ["base", "base-regional-provincias", "BASE"]
Expand Down Expand Up @@ -252,15 +252,6 @@ def rename_unidad(df, rename: dict = None) -> pd.DataFrame:

df = df.copy()

# Listamos los valores de unidad que no se corresponden a los esperados
list_unidad = df["unidad"].dropna().astype(str).unique()
list_unidad = sorted(set(list_unidad) - set(DICT_UNIDAD_RENAME.values()))
if len(list_unidad) > 0:
logger.warning(
f"Valores no esperados encontrados en la columna 'unidad': "
f"{', '.join(list_unidad)}"
)

# If any value contains the exact word, change value to word
list_rename = set(rename.values())
for word in list_rename:
Expand All @@ -271,6 +262,15 @@ def rename_unidad(df, rename: dict = None) -> pd.DataFrame:

# Rename the rest
df["unidad"] = df["unidad"].replace(rename)

# Listamos los valores de unidad que no se corresponden a los esperados
list_unidad = df["unidad"].dropna().astype(str).unique()
list_unidad = sorted(set(list_unidad) - list_rename)
if len(list_unidad) > 0:
logger.warning(
f"Valores no esperados encontrados en la columna 'unidad': "
f"{', '.join(list_unidad)}"
)
return df


Expand Down
2 changes: 1 addition & 1 deletion covidnpi/web/datastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
return_casos_of_provincia_normed,
)
from covidnpi.utils.config import load_config
from covidnpi.utils.logging import logger
from covidnpi.utils.log import logger
from covidnpi.utils.series import cumulative_incidence, compute_growth_rate
from covidnpi.utils.taxonomia import return_taxonomia, PATH_TAXONOMIA
from covidnpi.web.mongo import load_mongo
Expand Down
2 changes: 1 addition & 1 deletion covidnpi/web/generate_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import typer

from covidnpi.utils.config import load_config
from covidnpi.utils.logging import logger
from covidnpi.utils.log import logger
from covidnpi.web.mongo import load_mongo


Expand Down
53 changes: 53 additions & 0 deletions data/CCAA.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
CCAA,Cod_CCAA,Provincia,Texto,Codigo
Comunitat Valenciana,10,Castelló,Castellón,12
Andalucía,01,Huelva,Huelva,21
Canarias,05,Santa Cruz de Tenerife,Santa Cruz de Tenerife,38
Castilla y León,07,Palencia,Palencia,34
Castilla - La Mancha,08,Guadalajara,Guadalajara,19
Comunidad de Madrid,13,Madrid,Madrid,28
Cantabria,06,Cantabria,Cantabria,39
País Vasco,16,Bizkaia,Vizcaya,48
Castilla y León,07,Segovia,Segovia,40
Castilla y León,07,Zamora,Zamora,49
Extremadura,11,Badajoz,Badajoz,06
Aragón,02,Huesca,Huesca,22
Galicia,12,Lugo,Lugo,27
Illes Balears,04,Illes Balears,Islas Baleares,07
Comunidad Foral de Navarra,15,Navarra,Navarra,31
Comunitat Valenciana,10,Alacant,Alicante,03
Región de Murcia,14,Murcia,Murcia,30
Castilla y León,07,Ávila,Ávila,05
Andalucía,01,Cádiz,Cádiz,11
Comunitat Valenciana,10,València,Valencia,46
Cataluña,09,Barcelona,Barcelona,08
Castilla - La Mancha,08,Cuenca,Cuenca,16
Castilla - La Mancha,08,Albacete,Albacete,02
Extremadura,11,Cáceres,Cáceres,10
Castilla - La Mancha,08,Ciudad Real,Ciudad Real,13
Cataluña,09,Lleida,Lleida,25
Galicia,12,Ourense,Orense,32
Cataluña,09,Tarragona,Tarragona,43
Castilla y León,07,Burgos,Burgos,09
País Vasco,16,Gipuzcoa,Guipúzcoa,20
Castilla y León,07,Salamanca,Salamanca,37
Melilla,19,Melilla,Melilla,52
Andalucía,01,Sevilla,Sevilla,41
Castilla y León,07,Valladolid,Valladolid,47
Ceuta,18,Ceuta,Ceuta,51
Andalucía,01,Jaén,Jaén,23
Andalucía,01,Málaga,Málaga,29
Andalucía,01,Córdoba,Córdoba,14
Castilla y León,07,León,León,24
País Vasco,16,Araba,Álava,01
Cataluña,09,Girona,Gerona,17
Andalucía,01,Granada,Granada,18
"Rioja, La",17,La Rioja,La Rioja,26
Canarias,05,Las Palmas,Las Palmas,35
Galicia,12,Pontevedra,Pontevedra,36
"Asturias, Principado de",03,Asturias,Asturias,33
Castilla y León,07,Soria,Soria,42
Castilla - La Mancha,08,Toledo,Toledo,45
Galicia,12,A Coruña,La Coruña,15
Aragón,02,Teruel,Teruel,44
Aragón,02,Zaragoza,Zaragoza,50
Andalucía,01,Almería,Almería,04

0 comments on commit 14e26c6

Please sign in to comment.