Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
203 changes: 173 additions & 30 deletions SWEET_python/city_params.py

Large diffs are not rendered by default.

89 changes: 34 additions & 55 deletions SWEET_python/class_defs.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,36 +226,20 @@ def create_advanced(
growth_rate_historic: float,
growth_rate_future: float,
implement_year: Optional[int] = None,
population_series: Optional[pd.Series] = None,
):
years = np.arange(start_year, end_year + 1)
t = years - year_of_data_pop

if (growth_rate_future == growth_rate_future) and (growth_rate_future == 0.0):
growth_factors = np.ones(len(years))
else:
# Create growth rate array, using growth_rate_historic for years before year_of_data_pop and growth_rate_future after
growth_rate = np.where(
years < year_of_data_pop, growth_rate_historic, growth_rate_future
)
growth_factors = growth_rate**t

# Apply growth factors to each row of the DataFrame
adjusted_data = waste_masses_df.multiply(growth_factors, axis=0)

# Repeat with the implement_year if it is provided
if implement_year is not None:
year_of_data_pop = implement_year
t = years - year_of_data_pop
growth_rate = np.where(
years < year_of_data_pop, growth_rate_historic, growth_rate_future
)
growth_factors = growth_rate**t
adjusted_data2 = waste_masses_df.multiply(growth_factors, axis=0)

# Update the original DataFrame
adjusted_data.loc[implement_year:] = adjusted_data2.loc[implement_year:]

return cls(df=adjusted_data)
# The same as create_advanced_2 with the scenario pivoting at implement_year.
return cls.create_advanced_2(
waste_masses_df,
start_year,
end_year,
year_of_data_pop,
implement_year,
growth_rate_historic,
growth_rate_future,
implement_year=implement_year,
population_series=population_series,
)

@classmethod
def create_advanced_2(
Expand All @@ -268,32 +252,29 @@ def create_advanced_2(
growth_rate_historic: float,
growth_rate_future: float,
implement_year: Optional[int] = None,
population_series: Optional[pd.Series] = None,
):
years = np.arange(start_year, end_year + 1)
t = years - year_of_data_pop_baseline

if (growth_rate_future == growth_rate_future) and (growth_rate_future == 0.0):
# By the population series when there is one, else the two rates
# (growth_factors_for_years). A future rate of exactly 0.0 with no series
# means "no growth model".
if population_series is None and growth_rate_future == 0.0:
growth_factors = np.ones(len(years))
else:
# Create growth rate array, using growth_rate_historic for years before year_of_data_pop and growth_rate_future after
growth_rate = np.where(
years < year_of_data_pop_baseline, growth_rate_historic, growth_rate_future
growth_factors = growth_factors_for_years(
years, year_of_data_pop_baseline, growth_rate_historic, growth_rate_future,
population_series,
)
growth_factors = growth_rate**t

# Apply growth factors to each row of the DataFrame
adjusted_data = waste_masses_df.multiply(growth_factors, axis=0)

# Repeat with the implement_year if it is provided
# From implement_year on, the scenario pivots on its own year.
if implement_year is not None:
t = years - year_of_data_pop_scenario
growth_rate = np.where(
years < year_of_data_pop_scenario, growth_rate_historic, growth_rate_future
growth_factors = growth_factors_for_years(
years, year_of_data_pop_scenario, growth_rate_historic, growth_rate_future,
population_series,
)
growth_factors = growth_rate**t
adjusted_data2 = waste_masses_df.multiply(growth_factors, axis=0)

# Update the original DataFrame
adjusted_data.loc[implement_year:] = adjusted_data2.loc[implement_year:]

return cls(df=adjusted_data)
Expand Down Expand Up @@ -428,8 +409,17 @@ def create_simple(
year_of_data_pop: int,
growth_rate_historic: float,
growth_rate_future: float,
population_series: Optional[pd.Series] = None,
) -> "DivsDF":

years = np.arange(start_year, end_year + 1)
# By the population series when there is one, else the two rates, as the waste
# it is diverted from (growth_factors_for_years). The same for all four streams.
growth_factors = growth_factors_for_years(
years, year_of_data_pop, growth_rate_historic, growth_rate_future,
population_series,
)

def create_div_df(baseline: WasteMasses, scenario: WasteMasses) -> pd.DataFrame:
# All waste types in order
waste_types = list(baseline.model_dump().keys())
Expand All @@ -438,17 +428,6 @@ def create_div_df(baseline: WasteMasses, scenario: WasteMasses) -> pd.DataFrame:
baseline_arr = np.array([getattr(baseline, w) for w in waste_types])
scenario_arr = np.array([getattr(scenario, w) for w in waste_types])

# Array of years
years = np.arange(start_year, end_year + 1)
# Compute time offsets
t = years - year_of_data_pop

# Compute growth factors
# If year < year_of_data_pop -> use growth_rate_historic, else growth_rate_future
growth_factors = np.where(
years < year_of_data_pop, growth_rate_historic**t, growth_rate_future**t
)

# Mask for baseline vs scenario (before or after implement_year)
baseline_mask = years < implement_year

Expand Down
7 changes: 7 additions & 0 deletions SWEET_python/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
silently drops every country back to the frozen-CAGR growth scalars. Regenerate
with diagnostic_scripts/generate_pops_yearly.py and upload to blob
static_data/pops_yearly.csv BEFORE the constant lands in a run.

THERE ARE TWO COPIES OF THAT TABLE, and nothing keeps them in step. Climate TRACE's
pipeline reads the blob copy; the WasteMAP tools read SWEET's own
SWEET_python/pops_yearly.csv (SWEET_python.population), which is the same table
extended back to 1950 because the site tool models from a landfill's real opening
year. Regenerate both from the same WPP file: the generator starts at
MODEL_START_YEAR, so SWEET's copy needs its START_YEAR set to 1950.
"""

MODEL_START_YEAR: int = 1970
Expand Down
Loading