-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #617 from Klimatbyran/fix/merge-conflicts
Deploy to production
- Loading branch information
Showing
12 changed files
with
591 additions
and
336 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ yarn-error.log* | |
*.tsbuildinfo | ||
|
||
/.vscode/ | ||
/.idea/ | ||
|
||
# python | ||
__pycache__/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import numpy as np | ||
|
||
|
||
def calculate_approximated_historical(df, last_year_with_smhi_data, current_year): | ||
""" | ||
Calculate approximated historical data values for years passed since the last year with SMHI data. | ||
This is done by interpolation using previously calculated linear trend coefficients. | ||
Args: | ||
df (pandas.DataFrame): The input DataFrame containing the data. | ||
last_year_with_smhi_data (int): The last year with SMHI data. | ||
current_year (int): The current year. | ||
Returns: | ||
pandas.DataFrame: The DataFrame with the approximated historical data values added. | ||
""" | ||
|
||
# Get the years passed since last year with SMHI data (including current year) | ||
approximated_years = range(last_year_with_smhi_data+1, current_year+1) | ||
|
||
temp = [] # temporary list that we will append to | ||
df = df.sort_values('Kommun', ascending=True) | ||
for i in range(len(df)): | ||
# We'll store the approximated values for each municipality | ||
# in a dictionary where the keys are the years | ||
approximated_data_dict = {} | ||
|
||
if list(approximated_years): # only fill dict if approximation is needed | ||
# Add the latest recorded datapoint to the dict | ||
# The rest of the years will be added below | ||
approximated_data_dict = {last_year_with_smhi_data: | ||
df.iloc[i][last_year_with_smhi_data]} | ||
# Get trend coefficients | ||
fit = df.iloc[i]['trendCoefficients'] | ||
|
||
for year in approximated_years: | ||
# Add the approximated value for each year using the trend line coefficients | ||
# Max function so we don't get negative values | ||
approximated_data_dict[year] = max(0, fit[0]*year+fit[1]) | ||
|
||
temp.append(approximated_data_dict) | ||
|
||
df['approximatedHistorical'] = temp | ||
|
||
temp = [ | ||
np.trapz( | ||
list(df.iloc[i]['approximatedHistorical'].values()), | ||
list(df.iloc[i]['approximatedHistorical'].keys()), | ||
) | ||
for i in range(len(df)) | ||
] | ||
df['totalApproximatedHistorical'] = temp | ||
|
||
return df |
Oops, something went wrong.