Skip to content

Commit

Permalink
Merge pull request #628 from Klimatbyran/staging
Browse files Browse the repository at this point in the history
Deploy to prod
  • Loading branch information
elvbom authored Jun 4, 2024
2 parents 6f72183 + 0fa9193 commit 2d2c501
Show file tree
Hide file tree
Showing 9 changed files with 355 additions and 305 deletions.
11 changes: 5 additions & 6 deletions data/climate_data_calculations.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
import pandas as pd

from solutions.cars.electric_car_change_rate import get_electric_car_change_rate
from solutions.cars.electric_vehicle_per_charge_points import (
get_electric_vehicle_per_charge_points,
)
from solutions.bicycles.bicycle_data_calculations import bicycle_calculations
from solutions.cars.electric_vehicle_per_charge_points import get_electric_vehicle_per_charge_points
from solutions.bicycles.bicycle_data_calculations import calculate_bike_lane_per_capita
from facts.plans.plans_data_prep import get_climate_plans
from facts.municipalities_counties import get_municipalities
from facts.procurements.climate_requirements_in_procurements import get_procurement_data
Expand All @@ -35,7 +33,8 @@ def create_dataframe(to_percentage: bool) -> pd.DataFrame:
df = get_climate_plans(df)
print('4. Climate plans added')

df = bicycle_calculations(df)
df_bike_lanes = calculate_bike_lane_per_capita()
df = df.merge(df_bike_lanes, on='Kommun', how='left')
print('5. Bicycle data added')

df = get_consumption_emissions(df)
Expand Down Expand Up @@ -84,7 +83,7 @@ def series_to_dict(row: pd.Series, numeric_columns: List[Any]) -> Dict:
'climatePlanLink': row['Länk till aktuell klimatplan'],
'climatePlanYear': row['Antagen år'],
'climatePlanComment': row['Namn, giltighetsår, kommentar'],
'bicycleMetrePerCapita': row['metrePerCapita'],
'bicycleMetrePerCapita': row['bikeMetrePerCapita'],
'totalConsumptionEmission': row['Total emissions'],
'electricVehiclePerChargePoints': row['EVPC'],
'procurementScore': row['procurementScore'],
Expand Down
578 changes: 289 additions & 289 deletions data/output/climate-data.json

Large diffs are not rendered by default.

Binary file not shown.
Binary file added data/solutions/bicycles/be0101_tabhel2023.xlsx
Binary file not shown.
46 changes: 37 additions & 9 deletions data/solutions/bicycles/bicycle_data_calculations.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,42 @@
import numpy as np
import pandas as pd


PATH_BICYCLE_DATA = 'solutions/bicycles/cykelstatistik.xlsx'
PATH_BICYCLE_DATA = 'solutions/bicycles/Cykelnät per komun 20231231.xlsx'
PATH_POPULATION_DATA = 'solutions/bicycles/be0101_tabhel2023.xlsx'


def bicycle_calculations(df):
df_raw_bicycles = pd.read_excel(PATH_BICYCLE_DATA)
df_raw_bicycles['metrePerCapita'] = df_raw_bicycles['Meter per capita']
df_bicycles = df_raw_bicycles.filter(
['Kommun', 'metrePerCapita'], axis=1)
df = df.merge(df_bicycles, on='Kommun', how='left')
return df
def calculate_bike_lane_per_capita():
"""
Perform calculations on bicycle data and population data on municipality level.
This function reads bicycle data and population data from Excel files, performs
data cleaning and merging, and calculates the bike lane per capita per municipality.
Returns:
pandas.DataFrame: A DataFrame containing the merged data and the calculated bike lane per capita.
"""

df_raw_bicycles = pd.read_excel(PATH_BICYCLE_DATA, skiprows=3)
df_bicycles = df_raw_bicycles[['Kommun', 'Totalsumma']]
df_bicycles.loc[df_bicycles['Kommun'] == 'Malung', 'Kommun'] = 'Malung-Sälen'
df_bicycles.loc[df_bicycles['Kommun'] == 'Upplands-Väsby', 'Kommun'] = 'Upplands Väsby'

df_raw_population = pd.read_excel(PATH_POPULATION_DATA, skiprows=5)
# Drop unnecessary rows
df_population_drop = df_raw_population.drop([0, 1, 2, 3])
# Filter out county rows (that have 2 codes in the 'Kommun' column instead of 4)
df_population_municipality = df_population_drop[df_population_drop['Kommun'].str.len() == 4]
# Filter out unnecessary columns
df_population_filter = df_population_municipality[['Kommunnamn', 'Folkmängd']]
# Rename 'Kommunnamn' to 'Kommun' to match the bicycle dataframe
df_population_renamed = df_population_filter.rename(columns={'Kommunnamn': 'Kommun'})
# Strip 'Kommun' column of whitespaces
df_population_renamed['Kommun'] = df_population_renamed['Kommun'].str.strip()

# Merge bicycle and population dataframes
df_merged = df_bicycles.merge(df_population_renamed, on='Kommun', how='left')

# Calculate bike lane per capita
df_merged['bikeMetrePerCapita'] = df_merged['Totalsumma'] / df_merged['Folkmängd']

return df_merged[['Kommun', 'bikeMetrePerCapita']]
Binary file removed data/solutions/bicycles/cykelstatistik.xlsx
Binary file not shown.
23 changes: 23 additions & 0 deletions data/tests/test_bike_lane_per_capita.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
import unittest
import pandas as pd

from solutions.bicycles.bicycle_data_calculations import calculate_bike_lane_per_capita


class TestBicycleCalculations(unittest.TestCase):

def test_calculate_bike_lane_per_capita(self):
df_expected = pd.DataFrame(
{
"Kommun": ["Ale", "Alingsås", "Alvesta"],
"bikeMetrePerCapita": [91548/32446, 122012/42382, 66699/20040],
}
)

df_result = calculate_bike_lane_per_capita()

pd.testing.assert_frame_equal(df_result.iloc[:3], df_expected, check_dtype=False)

if __name__ == "__main__":
unittest.main()
Binary file modified public/Utsläppsförändring_kommunerna_2020-2021.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion public/locales/sv/municipality.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"solutions": {
"bikes": {
"heading": "Antal meter cykelväg per invånare",
"info": "Antal meter cykelväg per invånare år 2022 totalt för alla väghållare (statlig, kommunal, enskild). Högre är generellt bättre.",
"info": "Antal meter cykelväg per invånare år 2023 totalt för alla väghållare (statlig, kommunal, enskild). Högre är generellt bättre.",
"meters": "{{meters}} meter",
"title": "Cyklarna"
},
Expand Down

0 comments on commit 2d2c501

Please sign in to comment.