black . -l 79

Former-commit-id: 8f0b33b3653c1866411d30cdaab7b41d8cae2a16
This commit is contained in:
Michael T. Kelbaugh
2020-03-17 12:54:08 -04:00
parent 90e1d3a044
commit 566dd85a0c
13 changed files with 553 additions and 350 deletions

View File

@@ -46,9 +46,13 @@ def temp_inc(init_data, year):
lon += 180
# precipitation (mm)
translated_pr[f"lat_{int(lat*100)}_lon_{int(lon*100)}"] = lon_values[0]
translated_pr[
f"lat_{int(lat*100)}_lon_{int(lon*100)}"
] = lon_values[0]
# evaporation (mm)
translated_ev[f"lat_{int(lat*100)}_lon_{int(lon*100)}"] = lon_values[1]
translated_ev[
f"lat_{int(lat*100)}_lon_{int(lon*100)}"
] = lon_values[1]
return (
temperature,

View File

@@ -7,7 +7,7 @@ import glob
import sys
import logging
sys.path.append('/')
sys.path.append("/")
from outer_wrapper import OuterWrapper
from climate_model import temp_inc
@@ -20,13 +20,13 @@ class InnerWrapper(OuterWrapper):
)
def configure(self, **kwargs):
self.raw_data = kwargs['rcp26data']
if 'rcp26data' in kwargs.keys():
self.raw_data = kwargs["rcp26data"]
if "rcp26data" in kwargs.keys():
self.global_temp, self.precipitation, self.evaporation = temp_inc(
self.raw_data, self.incstep
)
else:
logging.warning(f'incstep {self.incstep}: rcp26data not found')
logging.warning(f"incstep {self.incstep}: rcp26data not found")
def increment(self, **kwargs):
self.global_temp, self.precipitation, self.evaporation = temp_inc(
@@ -34,18 +34,18 @@ class InnerWrapper(OuterWrapper):
)
results = {
'gfdl_cm3': {
'global_temp': {
'data': {'global_temp': self.global_temp},
'granularity': None,
"gfdl_cm3": {
"global_temp": {
"data": {"global_temp": self.global_temp},
"granularity": None,
},
'precipitation': {
'data': self.precipitation,
'granularity': 'latlon',
"precipitation": {
"data": self.precipitation,
"granularity": "latlon",
},
'evaporation': {
'data': self.evaporation,
'granularity': 'latlon',
"evaporation": {
"data": self.evaporation,
"granularity": "latlon",
},
}
}

View File

@@ -7,7 +7,7 @@ import glob
import sys
import logging
sys.path.append('/')
sys.path.append("/")
from outer_wrapper import OuterWrapper
from PopulationSimulation import pop_sim, get_data
@@ -23,16 +23,18 @@ class InnerWrapper(OuterWrapper):
self.max_incstep = 50
def configure(self, **kwargs):
if 'county_populations' in kwargs.keys():
self.data = pop_sim(kwargs['county_populations'], self.max_incstep)
if "county_populations" in kwargs.keys():
self.data = pop_sim(kwargs["county_populations"], self.max_incstep)
else:
logging.warning(f'incstep {self.incstep}: county_populations not found')
logging.warning(
f"incstep {self.incstep}: county_populations not found"
)
def increment(self, **kwargs):
data = get_data(self.data, self.initial_year + self.incstep)
results = {
'population': {
'population': {'data': data, 'granularity': 'county'}
"population": {
"population": {"data": data, "granularity": "county"}
}
}
return results

View File

@@ -10,31 +10,31 @@ def pow_dem_sim(pop, cons):
temp = {}
count = pd.DataFrame(pop, index=['pop'])
count = pd.DataFrame(pop, index=["pop"])
count = count.T
count.reset_index(inplace=True)
count['State'] = count['index'].apply(lambda x: x[:-3])
state_pops = count.groupby('State').sum().reset_index()
count = pd.merge(count, state_pops, on='State', how='left')
count['perc'] = count.apply(lambda x: x.pop_x / x.pop_y, axis=1)
count["State"] = count["index"].apply(lambda x: x[:-3])
state_pops = count.groupby("State").sum().reset_index()
count = pd.merge(count, state_pops, on="State", how="left")
count["perc"] = count.apply(lambda x: x.pop_x / x.pop_y, axis=1)
cons_pc = pd.DataFrame(cons, index=['cons'])
cons_pc = pd.DataFrame(cons, index=["cons"])
cons_pc = cons_pc.T
count = pd.merge(
count,
cons_pc.reset_index(),
left_on='State',
right_on='index',
how='left',
left_on="State",
right_on="index",
how="left",
)
# simply multiplies current pop by state consumption per capita
count['demand'] = count.apply(
count["demand"] = count.apply(
lambda x: (x.pop_y * x.cons) * x.perc, axis=1
)
count = count[['index_x', 'demand']].set_index('index_x')
count = count[["index_x", "demand"]].set_index("index_x")
for index, row in count.iterrows():
temp[index] = row['demand']
temp[index] = row["demand"]
return temp

View File

@@ -7,7 +7,7 @@ import glob
import sys
import logging
sys.path.append('/')
sys.path.append("/")
from outer_wrapper import OuterWrapper
from DemandSimulation import pow_dem_sim
@@ -20,26 +20,30 @@ class InnerWrapper(OuterWrapper):
)
def configure(self, **kwargs):
if 'state_consumption_per_capita' in kwargs.keys():
self.cons = kwargs['state_consumption_per_capita']
if "state_consumption_per_capita" in kwargs.keys():
self.cons = kwargs["state_consumption_per_capita"]
else:
logging.warning(f'incstep {self.incstep}: state_consumption_per_capita not found')
if '2016_populations' in kwargs.keys():
self.pop = kwargs['2016_populations']
logging.warning(
f"incstep {self.incstep}: state_consumption_per_capita not found"
)
if "2016_populations" in kwargs.keys():
self.pop = kwargs["2016_populations"]
else:
logging.warning(f'incstep {self.incstep}: 2016_populations not found')
logging.warning(
f"incstep {self.incstep}: 2016_populations not found"
)
def increment(self, **kwargs):
if 'population' in kwargs.keys():
self.pop = kwargs['population']['population']['data']
if "population" in kwargs.keys():
self.pop = kwargs["population"]["population"]["data"]
elif self.incstep > 1:
logging.warning(f'incstep {self.incstep}: population not found')
logging.warning(f"incstep {self.incstep}: population not found")
demand = pow_dem_sim(self.pop, self.cons)
results = {
'power_demand': {
'power_demand': {'data': demand, 'granularity': 'county'}
"power_demand": {
"power_demand": {"data": demand, "granularity": "county"}
}
}

View File

@@ -9,41 +9,40 @@ import pandas as pd
def gen_sim(demand, prof):
counties = (
pd.DataFrame(demand, index=['demand'])
pd.DataFrame(demand, index=["demand"])
.T.reset_index()
.rename(columns={'index': 'county'})
.rename(columns={"index": "county"})
)
counties['state'] = counties.county.apply(lambda x: x[:-3])
state_demand = counties.groupby('state')['demand'].sum()
counties["state"] = counties.county.apply(lambda x: x[:-3])
state_demand = counties.groupby("state")["demand"].sum()
state_prof = (
pd.DataFrame(prof).T.reset_index().rename(columns={'index': 'state'})
pd.DataFrame(prof).T.reset_index().rename(columns={"index": "state"})
)
state_demand = state_demand.to_frame().reset_index()
counties = pd.merge(counties, state_demand, on='state', how='left')
counties = pd.merge(counties, state_prof, on='state', how='left')
counties['Fuel Used (MMBtu)'] = counties.apply(
lambda x: (x['MMBtu per MWh']) * (x.demand_x), axis=1
counties = pd.merge(counties, state_demand, on="state", how="left")
counties = pd.merge(counties, state_prof, on="state", how="left")
counties["Fuel Used (MMBtu)"] = counties.apply(
lambda x: (x["MMBtu per MWh"]) * (x.demand_x), axis=1
)
counties['CO2 Emissions (tons)'] = counties.apply(
lambda x: (x['Tons CO2 per MWh']) * (x.demand_x), axis=1
counties["CO2 Emissions (tons)"] = counties.apply(
lambda x: (x["Tons CO2 per MWh"]) * (x.demand_x), axis=1
)
counties['Water Used (Mgal)'] = counties.apply(
lambda x: (x['Mgal_per_MWh']) * (x.demand_x), axis=1
counties["Water Used (Mgal)"] = counties.apply(
lambda x: (x["Mgal_per_MWh"]) * (x.demand_x), axis=1
)
counties = counties[
[
'county',
'Fuel Used (MMBtu)',
'CO2 Emissions (tons)',
'Water Used (Mgal)',
"county",
"Fuel Used (MMBtu)",
"CO2 Emissions (tons)",
"Water Used (Mgal)",
]
].set_index('county')
].set_index("county")
co2 = {}
h2o = {}
for index, row in counties.iterrows():
co2[index] = row['CO2 Emissions (tons)']
h2o[index] = row['Water Used (Mgal)']
co2[index] = row["CO2 Emissions (tons)"]
h2o[index] = row["Water Used (Mgal)"]
return co2, h2o

View File

@@ -7,7 +7,7 @@ import glob
import sys
import logging
sys.path.append('/')
sys.path.append("/")
from outer_wrapper import OuterWrapper
from GenerationSimulation import gen_sim
@@ -20,27 +20,29 @@ class InnerWrapper(OuterWrapper):
)
def configure(self, **kwargs):
if 'state_energy_profiles' in kwargs.keys():
self.prof = kwargs['state_energy_profiles']
if "state_energy_profiles" in kwargs.keys():
self.prof = kwargs["state_energy_profiles"]
else:
logging.warning(f'incstep {self.incstep}: state_energy_profiles not found')
if '2016_demand' in kwargs.keys():
self.dem = kwargs['2016_demand']
logging.warning(
f"incstep {self.incstep}: state_energy_profiles not found"
)
if "2016_demand" in kwargs.keys():
self.dem = kwargs["2016_demand"]
else:
logging.warning(f'incstep {self.incstep}: 2016_demand not found')
logging.warning(f"incstep {self.incstep}: 2016_demand not found")
def increment(self, **kwargs):
if 'power_demand' in kwargs.keys():
self.dem = kwargs['power_demand']['power_demand']['data']
if "power_demand" in kwargs.keys():
self.dem = kwargs["power_demand"]["power_demand"]["data"]
elif self.incstep > 1:
logging.warning(f'incstep {self.incstep}: power_demand not found')
logging.warning(f"incstep {self.incstep}: power_demand not found")
emissions, water = gen_sim(self.dem, self.prof)
results = {
'power_supply': {
'co2': {'data': emissions, 'granularity': 'county'},
'thermo_water': {'data': water, 'granularity': 'county'},
"power_supply": {
"co2": {"data": emissions, "granularity": "county"},
"thermo_water": {"data": water, "granularity": "county"},
}
}
return results

View File

@@ -7,7 +7,7 @@ import glob
import sys
import logging
sys.path.append('/')
sys.path.append("/")
from outer_wrapper import OuterWrapper
from Water_Demand_Model import Water_Demand_Simulation
@@ -20,26 +20,28 @@ class InnerWrapper(OuterWrapper):
)
def configure(self, **kwargs):
if 'rates' in kwargs.keys():
self.rate = kwargs['rates']
if "rates" in kwargs.keys():
self.rate = kwargs["rates"]
else:
logging.warning(f'incstep {self.incstep}: rates not found')
if '2016_populations' in kwargs.keys():
self.countypop = kwargs['2016_populations']
logging.warning(f"incstep {self.incstep}: rates not found")
if "2016_populations" in kwargs.keys():
self.countypop = kwargs["2016_populations"]
else:
logging.warning(f'incstep {self.incstep}: 2016_populations not found')
logging.warning(
f"incstep {self.incstep}: 2016_populations not found"
)
def increment(self, **kwargs):
if 'population' in kwargs.keys():
self.countypop = kwargs['population']['population']['data']
if "population" in kwargs.keys():
self.countypop = kwargs["population"]["population"]["data"]
elif self.incstep > 1:
logging.warning(f'incstep {self.incstep}: population not found')
logging.warning(f"incstep {self.incstep}: population not found")
demand = Water_Demand_Simulation(self.countypop, self.rate)
results = {
'water_demand': {
'water_demand': {'data': demand, 'granularity': 'county'}
"water_demand": {
"water_demand": {"data": demand, "granularity": "county"}
}
}
return results

View File

@@ -6,7 +6,7 @@
import glob
import sys
sys.path.append('/')
sys.path.append("/")
from outer_wrapper import OuterWrapper
# import helper functions
@@ -26,26 +26,34 @@ class InnerWrapper(OuterWrapper):
def configure(self, **kwargs):
# config_datax refers to the names of the JSON files in the config directory
self.input_data1 = kwargs['config_data1']
self.input_data2 = kwargs['config_data2']
self.input_data3 = kwargs['config_data3']
self.input_data1 = kwargs["config_data1"]
self.input_data2 = kwargs["config_data2"]
self.input_data3 = kwargs["config_data3"]
# this model has two input schemas, and so expects input from two other models
def increment(self, **kwargs):
# input_model_name1 matches the name of a JSON file in the input schemas directory
if 'input_model_name1' in kwargs.keys():
if "input_model_name1" in kwargs.keys():
# example_input1 and example_input2 refer to fields in the input schema
self.input_data1 = kwargs['input_model_name1']['example_input1']['data']
self.input_data2 = kwargs['input_model_name1']['example_input2']['data']
self.input_data1 = kwargs["input_model_name1"]["example_input1"][
"data"
]
self.input_data2 = kwargs["input_model_name1"]["example_input2"][
"data"
]
# input_model_name2 matches the name of a JSON file in the input schemas directory
if 'input_model_name2' in kwargs.keys():
if "input_model_name2" in kwargs.keys():
# example_input3 refers to a field in the input schema
self.input_data3 = kwargs['input_model_name2']['example_input3']['data']
self.input_data3 = kwargs["input_model_name2"]["example_input3"][
"data"
]
# calculate the model's outputs
output1, output2 = my_function(self.input_data1, self.input_data2, self.input_data3)
output1, output2 = my_function(
self.input_data1, self.input_data2, self.input_data3
)
# template_output_schema matches the name of the JSON file in the output schemas directory
# example_output1 and example_output2 refer to fields in the output schema
@@ -53,9 +61,9 @@ class InnerWrapper(OuterWrapper):
# example_output2 is returned by my_function() in the latlon granularity
# before it is published, the data will automatically be translated to the granularities specified in template_output_schema
return {
'template_output_schema': {
'example_output1': {'data': output1, 'granularity': 'county'},
'example_output2': {'data': output2, 'granularity': 'latlon'}
"template_output_schema": {
"example_output1": {"data": output1, "granularity": "county"},
"example_output2": {"data": output2, "granularity": "latlon"},
}
}