Skip to content

Commit 124311f

Browse files
elvbomLudwikJaniukallazis
authored
Feat/emission sectors to backend and rewrite of emissions calculations (Klimatbyran#594)
* my current work, see if this works * npm audit * Revert "npm audit" This reverts commit a6084d7. * Lint * wip - all sectors * Update climate-data.json * Add test, lowercase input in getMunicipality * All sectors * Multiple sectors presentation Still missing cement+ and better legend * lint * Industri (energi + processer) * better colors * Tests showing negative emissions * type fixes * tests on data instead * Colors updated and clumped * cleanup + colors * cleanup * Transparent colors wohoo * Fix typos * borders * colrs * Dedup colors * Legend * percs and numbrees * skip cement munips for now * cleanup * Total in tooltip * cleanup2 * cleanup3 * cleanup * rm Named * Fixes for the commit * Remove function dependent on current year as constant set in frontend utils * Replace use of function deleted in previous commit * Make kilotonstring conversion in the same way everywhere in municipality graph numbers * Rename variables for better distinction * Remove out commented code * Add checkbox for showing sectors. Adjust last historical year depending on whether sectors are shown or not. * Fix linting * group emission sectors into same categories as panorama (Klimatbyran#446) * Feat/cleanup sector emissons and emission calculations (Klimatbyran#600) * group emission sectors into same categories as panorama * add first draft of calculate approximated sector emissions def * refactor sector emissions extraction and refactoring of emission defs * sector emissions are added to climate data * add todo * correct indent * fix for minor merge conflict * fix typo in grouping of sectors * fix merge conflicts in emission calculations * fix long line in emission calcs * cleanup and add docstrings * minor cleanup in data validation step * remove sectors from frontend * add docstrings, rm files and cleanup * rm old function call * restore climate data to staging version so we dont introduce 2022 data in advance * rm old file * minor cleanup * add docstrings, cleanup and fix merge conflicts in tests * make budget year arg for functions * restore climate data to staging branch version --------- Co-authored-by: Ludvig Janiuk <[email protected]> Co-authored-by: Charlotta Jaunviksna <[email protected]>
1 parent b969369 commit 124311f

10 files changed

+438
-274
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ yarn-error.log*
3939
*.tsbuildinfo
4040

4141
/.vscode/
42+
/.idea/
4243

4344
# python
4445
__pycache__/

data/climate_data_calculations.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import json
44

55
from solutions.cars.electric_car_change_rate import get_electric_car_change_rate
6-
from solutions.cars.electric_vehicle_per_charge_points import get_electric_vehicle_per_charge_points
6+
from solutions.cars.electric_vehicle_per_charge_points import (
7+
get_electric_vehicle_per_charge_points,
8+
)
79
from solutions.bicycles.bicycle_data_calculations import bicycle_calculations
810
from facts.plans.plans_data_prep import get_climate_plans
911
from facts.municipalities_counties import get_municipalities
@@ -55,12 +57,8 @@
5557
'totalApproximatedHistoricalEmission': df.iloc[i]['totalApproximatedHistorical'],
5658
'trend': df.iloc[i]['trend'],
5759
'trendEmission': df.iloc[i]['trendEmission'],
58-
'historicalEmissionChangePercent': df.iloc[i][
59-
'historicalEmissionChangePercent'
60-
],
61-
'neededEmissionChangePercent': df.iloc[i][
62-
'neededEmissionChangePercent'
63-
],
60+
'historicalEmissionChangePercent': df.iloc[i]['historicalEmissionChangePercent'],
61+
'neededEmissionChangePercent': df.iloc[i]['neededEmissionChangePercent'],
6462
'hitNetZero': df.iloc[i]['hitNetZero'],
6563
'budgetRunsOut': df.iloc[i]['budgetRunsOut'],
6664
'electricCarChangePercent': df.iloc[i]['electricCarChangePercent'],

data/export_data.py

-53
This file was deleted.

data/facts/municipalities_counties.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import pandas as pd
22

3-
43
def get_municipalities():
54
# Load the data
65
df = pd.read_excel('facts/kommunlankod_2023.xls')
@@ -25,4 +24,4 @@ def get_municipalities():
2524
result = pd.concat([result, pd.DataFrame({'Kommun': [municipality], 'Kod': [code], 'Län': [county]})], ignore_index=True)
2625

2726
# Return the resulting dataframe
28-
return result
27+
return result
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import numpy as np
2+
3+
4+
def calculate_approximated_historical(df, last_year_with_smhi_data, current_year):
5+
"""
6+
Calculate approximated historical data values for years passed since the last year with SMHI data.
7+
This is done by interpolation using previously calculated linear trend coefficients.
8+
9+
Args:
10+
df (pandas.DataFrame): The input DataFrame containing the data.
11+
last_year_with_smhi_data (int): The last year with SMHI data.
12+
current_year (int): The current year.
13+
14+
Returns:
15+
pandas.DataFrame: The DataFrame with the approximated historical data values added.
16+
17+
"""
18+
19+
# Get the years passed since last year with SMHI data (including current year)
20+
approximated_years = range(last_year_with_smhi_data+1, current_year+1)
21+
22+
temp = [] # temporary list that we will append to
23+
df = df.sort_values('Kommun', ascending=True)
24+
for i in range(len(df)):
25+
# We'll store the approximated values for each municipality
26+
# in a dictionary where the keys are the years
27+
approximated_data_dict = {}
28+
29+
if list(approximated_years): # only fill dict if approximation is needed
30+
# Add the latest recorded datapoint to the dict
31+
# The rest of the years will be added below
32+
approximated_data_dict = {last_year_with_smhi_data:
33+
df.iloc[i][last_year_with_smhi_data]}
34+
# Get trend coefficients
35+
fit = df.iloc[i]['trendCoefficients']
36+
37+
for year in approximated_years:
38+
# Add the approximated value for each year using the trend line coefficients
39+
# Max function so we don't get negative values
40+
approximated_data_dict[year] = max(0, fit[0]*year+fit[1])
41+
42+
temp.append(approximated_data_dict)
43+
44+
df['approximatedHistorical'] = temp
45+
46+
temp = [
47+
np.trapz(
48+
list(df.iloc[i]['approximatedHistorical'].values()),
49+
list(df.iloc[i]['approximatedHistorical'].keys()),
50+
)
51+
for i in range(len(df))
52+
]
53+
df['totalApproximatedHistorical'] = temp
54+
55+
return df

0 commit comments

Comments
 (0)