Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
thornoe committed Mar 26, 2024
1 parent 4fbdd3e commit 2300b29
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 30 deletions.
48 changes: 24 additions & 24 deletions gis/sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,30 +144,27 @@

# Create a DataFrame with observed biophysical indicator by year
# df_ind_obs, df_VP = c.observed_indicator(j)
df_ind_obs = pd.read_csv("output\\" + j + "_ind_obs.csv", index_col="wb")
df_ind_obs.columns = df_ind_obs.columns.astype(int)
# df_ind_obs = pd.read_csv("output\\" + j + "_ind_obs.csv", index_col="wb")
# df_ind_obs.columns = df_ind_obs.columns.astype(int)
df_VP = pd.read_csv("output\\" + j + "_VP.csv", index_col="wb")

# Report ecological status based on observed biophysical indicator
df_eco_obs, stats_obs_j[j], index_sorted = c.ecological_status(j, df_ind_obs, df_VP)
# df_eco_obs, stats_obs_j[j], index_sorted = c.ecological_status(j, df_ind_obs, df_VP)

# if j == 'streams':
# # Create a map book with yearly maps of observed ecological status
# c.map_book(j, df_eco_obs)

# Impute missing values for biophysical indicator and return ecological status
df_eco_imp, df_eco_imp_MA, stats_imp_j[j], stats_imp_MA_j[j] = c.impute_missing(
j, df_eco_obs, df_VP, index_sorted
)
df_eco_imp_MA = pd.read_csv("output\\" + j + "_eco_imp.csv", index_col="wb")
# df_eco_imp, df_eco_imp_MA, stats_imp_j[j], stats_imp_MA_j[j] = c.impute_missing(
# j, df_eco_obs, df_VP, index_sorted
# )
df_eco_imp_MA = pd.read_csv("output\\" + j + "_eco_imp_MA.csv", index_col="wb")
df_eco_imp_MA.columns = df_eco_imp_MA.columns.astype(int)

# Set up df with variables by coastal catchment area for the Benefit Transfer equation
frames_j[j], shores_j[j] = c.values_by_catchment_area(j, df_eco_imp_MA, df_VP)

c.years + ["Basis"] = c.years + ["Basis"]


########################################################################################
# 4.a Stats for all categories j: Shore length and share of it where eco status < Good
########################################################################################
Expand Down Expand Up @@ -445,7 +442,6 @@ def replace_row(row):
# (...)



# def longitudinal(self, j, f, d, x, y, valueCol, parameterCol=0, parameter=0):
"""Set up a longitudinal DataFrame for all stations in category j by year t.
Streams: For a given year, find the DVFI index value of bottom fauna for a station with multiple observations by taking the median and rounding down
Expand Down Expand Up @@ -535,7 +531,6 @@ def replace_row(row):
long = long.merge(dfYear, how="left", on="station")



# def impute_missing(self, dfEcoObs, dfVP, index):
"""Impute ecological status for all water bodies from the observed indicator."""
# DataFrames for observed biophysical indicator and typology
Expand Down Expand Up @@ -633,7 +628,16 @@ def process_string(s):
dicts = {**dict1, **dict2} # combine the dictionaries
typ = typ.rename(columns=dicts) # rename columns to full names
# Dummies used for imputation chosen via Forward Stepwise Selection (CV)
cols = ['Water exchange', 'Belt Sea', 'Kattegat', 'North Sea', 'Sediment', 'North Sea fjord', 'Baltic Sea', 'Fjord']
cols = [
"Water exchange",
"Belt Sea",
"Kattegat",
"North Sea",
"Sediment",
"North Sea fjord",
"Baltic Sea",
"Fjord",
]

# Merge DataFrame for observed values with DataFrame for dummies
dfEcoSelected = dfEco.merge(typ[cols], on="wb") # with selected predictors
Expand All @@ -658,11 +662,10 @@ def process_string(s):
impStatsMA = c.ecological_status(j, dfImpMA, dfVP, "imp_MA", index)

df_eco_imp, df_eco_imp_MA = dfImp[c.years], dfImpMA[c.years]
stats_imp_j[j], stats_imp_MA_j[j] = impStats, impStatsMA
stats_imp_j[j], stats_imp_MA_j[j] = impStats, impStatsMA
# return dfImp[c.years], dfImpMA[c.years], impStats, impStatsMA



# def ecological_status(self, j, dfIndicator, dfTyp, suffix="obs", index=None):
"""Call indicator_to_status() to convert the longitudinal DataFrame to the EU index of ecological status, i.e., from 0-4 for Bad, Poor, Moderate, Good, and High water quality based on the category and typology of each water body.
Also call missing_values_graph() to map missing observations by year.
Expand Down Expand Up @@ -777,7 +780,7 @@ def process_string(s):
df_eco_obs = dfEco[dfEcoObs.columns]
stats_obs_j[j] = stats["not good"]
index_sorted = indexSorted
# return dfEco[dfEcoObs.columns], stats["not good"], indexSorted
# return dfEco[dfEcoObs.columns], stats["not good"], indexSorted

# Elaborate column names of statistics for online presentation
stats.columns = [
Expand All @@ -795,7 +798,6 @@ def process_string(s):
# return stats["not good"]



# def indicator_to_status(self, j, dfIndicator, df_VP):
"""Convert biophysical indicators to ecological status."""
dfIndicator, dfVP = df_ind_obs, df_VP
Expand Down Expand Up @@ -868,7 +870,6 @@ def SetThreshold(row):
df = df.drop(columns=cols)



# def missing_values_graph(self, j, frame, suffix="obs", index=None):
"""Heatmap visualizing observations of ecological status as either missing or using the EU index of ecological status, i.e., from 0-4 for Bad, Poor, Moderate, Good, and High water quality respectively.
Saves a figure of the heatmap."""
Expand All @@ -892,19 +893,21 @@ def SetThreshold(row):
if df.isna().sum().sum() > 0:
# Replace missing values with -1
df.fillna(-1, inplace=True)

# Specify heatmap to show missing values as gray (xkcd spells it "grey")
colors = ["grey", "red", "orange", "yellow", "green", "blue"]
uniqueValues = [-1, 0, 1, 2, 3, 4]

# Description for heatmap of observed eco status (instead of fig legend)
description = "Bad (red), Poor (orange), Moderate (yellow), Good (green), High (blue), missing value (gray)"

else:
# Specify heatmap without any missing values (only for imputed coastal)
colors = ["red", "orange", "yellow", "green", "blue"]
uniqueValues = [0, 1, 2, 3, 4]
description = "Bad (red), Poor (orange), Moderate (yellow), Good (green), High (blue)"
description = (
"Bad (red), Poor (orange), Moderate (yellow), Good (green), High (blue)"
)

# Plot heatmap
colorMap = sns.xkcd_palette(colors)
Expand All @@ -925,7 +928,6 @@ def SetThreshold(row):
# return index



# def values_by_catchment_area(self, j, dfEcoImpMA, dfVP):
"""Assign water bodies to coastal catchment areas and calculate the weighted arithmetic mean of ecological status after truncating from above at Good status.
For each year t, set up df with variables for the Benefit Transfer equation."""
Expand Down Expand Up @@ -1067,7 +1069,6 @@ def SetThreshold(row):
shores_j[j] = shores_v



def BT(df, elast=1):
"""Apply Benefit Transfer equation from meta study (Zandersen et al., 2022)"""
# ln MWTP for improvement from current ecological status to "Good"
Expand All @@ -1086,7 +1087,6 @@ def BT(df, elast=1):
return MWTP



# def valuation(self, dfBT, real=True, investment=False):
"""Valuation as either Cost of Water Pollution (CWP) or Investment Value (IV).
If not set to return real values (2018 prices), instead returns values in the prices of both the current year and the preceding year (for year-by-year chain linking)."""
Expand Down
14 changes: 8 additions & 6 deletions gis/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,23 @@

# Create a DataFrame with observed biophysical indicator by year
# df_ind_obs, df_VP = c.observed_indicator(j)
df_ind_obs = pd.read_csv("output\\" + j + "_ind_obs.csv", index_col="wb")
df_ind_obs.columns = df_ind_obs.columns.astype(int)
# df_ind_obs = pd.read_csv("output\\" + j + "_ind_obs.csv", index_col="wb")
# df_ind_obs.columns = df_ind_obs.columns.astype(int)
df_VP = pd.read_csv("output\\" + j + "_VP.csv", index_col="wb")

# Report ecological status based on observed biophysical indicator
df_eco_obs, stats_obs_j[j], index_sorted = c.ecological_status(j, df_ind_obs, df_VP)
# df_eco_obs, stats_obs_j[j], index_sorted = c.ecological_status(j, df_ind_obs, df_VP)

# if j == 'streams':
# # Create a map book with yearly maps of observed ecological status
# c.map_book(j, df_eco_obs)

# Impute missing values for biophysical indicator and return ecological status
df_eco_imp, df_eco_imp_MA, stats_imp_j[j], stats_imp_MA_j[j] = c.impute_missing(
j, df_eco_obs, df_VP, index_sorted
)
# df_eco_imp, df_eco_imp_MA, stats_imp_j[j], stats_imp_MA_j[j] = c.impute_missing(
# j, df_eco_obs, df_VP, index_sorted
# )
df_eco_imp_MA = pd.read_csv("output\\" + j + "_eco_imp_MA.csv", index_col="wb")
df_eco_imp_MA.columns = df_eco_imp_MA.columns.astype(int)

# df with variables by coastal catchment area for the Benefit Transfer equation
frames_j[j], shores_j[j] = c.values_by_catchment_area(j, df_eco_imp_MA, df_VP)
Expand Down

0 comments on commit 2300b29

Please sign in to comment.