yearlyCosts, LCOE = tt.getCosts(folder, dbs, save_data=saveData, create_plots=createPlots) # Emissions yearlyEmissions, avgEmissions = tt.getEmissions(folder, dbs, save_data=saveData, create_plots=createPlots) if not onlySimple: # Analyze capacity and activity by fuel types switch = 'fuel' # capacityByFuel = tt.getCapacity(folder, dbs, switch=switch, save_data=saveData, create_plots=createPlots) ActivityByYearFuel = tt.getActivity(folder, dbs, switch=switch, save_data=saveData, create_plots=createPlots) # ActivityByTODFuel = tt.getActivityTOD(folder, dbs, switch=switch, sector_name=sectorName, save_data=saveData, # create_plots=createPlots) # Analyze capacity and activity by technology types # switch = 'tech' # capacityByTech = tt.getCapacity(folder, dbs, switch=switch, save_data=saveData, create_plots=createPlots) # ActivityByYearTech = tt.getActivity(folder, dbs, switch=switch, save_data=saveData, create_plots=createPlots) # ActivityByTODTech = tt.getActivityTOD(folder, dbs, switch=switch, sector_name=sectorName, save_data=saveData, # create_plots=createPlots)
yearlyEmissions, avgEmissions = tt.getEmissions(folder, db, saveData=saveData, createPlots=createPlots) # Analyze capacity and activity by fuel types switch = 'fuel' capacityByFuel = tt.getCapacity(folder, db, switch=switch, sectorName=sectorName, saveData=saveData, createPlots=createPlots) ActivityByYearFuel = tt.getActivity(folder, db, switch=switch, sectorName=sectorName, saveData=saveData, createPlots=createPlots) ActivityByTODFuel = tt.getActivityTOD(folder, db, switch=switch, sectorName=sectorName, saveData=saveData, createPlots=createPlots) # Analyze capacity and activity by technology types switch = 'tech' capacityByTech = tt.getCapacity(folder, db, switch=switch, sectorName=sectorName,
def analyze_results(task, folder_db, all_dbs_dict, db_shift, node_prob, tech_group_dict, prob_type_dict, infra_dict, carbon_tax_dict): # Read-in task inputs db = task['db'] metric = task['metric'] run_name = task['run_name'] folder_results = task['folder_results'] # display task inputs print(db) print(metric) # re-arrange for functions dbs = [db] all_dbs = [db, all_dbs_dict[db]] # -------------------------------- # Analyze with temoatools # -------------------------------- create_plots = 'N' # Create default plots save_data = 'Y' # Do not save data as a csv or xls sector_name = 'electric' # Name of sector to be analyzed # Costs if metric == 'costs_yearly': tt.getCosts(folder_db, dbs, save_data=save_data, create_plots=create_plots, run_name=run_name) # Emissions elif metric == 'emissions_yearly': tt.getEmissions(folder_db, dbs, save_data=save_data, create_plots=create_plots, run_name=run_name) # Analyze activity by fuel types elif metric == 'activity_by_fuel': switch = 'fuel' tt.getActivity(folder_db, dbs, switch=switch, sector_name=sector_name, save_data=save_data, create_plots=create_plots, run_name=run_name) # Analyze activity by fuel types elif metric == 'activity_by_tech': switch = 'tech' sector_name = 'all' tt.getActivity(folder_db, dbs, switch=switch, sector_name=sector_name, save_data=save_data, create_plots=create_plots, run_name=run_name) # Analyze capacity by fuel types elif metric == 'capacity_by_fuel': switch = 'fuel' tt.getCapacity(folder_db, dbs, switch=switch, sector_name=sector_name, save_data=save_data, create_plots=create_plots, run_name=run_name) # Analyze capacity by fuel types elif metric == 'capacity_by_tech': switch = 'tech' sector_name = 'all' tt.getCapacity(folder_db, dbs, switch=switch, sector_name=sector_name, save_data=save_data, create_plots=create_plots, run_name=run_name) # -------------------------------- # Expand Results (only costs and emissions) # -------------------------------- if metric == 'costs_yearly' or metric == 'emissions_yearly' \ or metric == 'activity_by_fuel' or metric == 'activity_by_tech' \ or metric == 'capacity_by_fuel' or metric == 'capacity_by_tech': if metric == 'costs_yearly': filename = 'costs_yearly' elif metric == 'emissions_yearly': filename = 'emissions_yearly' elif metric == 'activity_by_fuel': filename = 'activity_by_fuel' elif metric == 'activity_by_tech': filename = 'activity_by_tech' elif metric == 'capacity_by_fuel': filename = 'capacity_by_fuel' elif metric == 'capacity_by_tech': filename = 'capacity_by_tech' tt.stoch_expand(folder_results, filename, db_shift) # -------------------------------- # Resample Results (only costs and emissions) # -------------------------------- if metric == 'costs_yearly' or metric == 'emissions_yearly' \ or metric == 'activity_by_fuel' or metric == 'activity_by_tech' \ or metric == 'capacity_by_fuel' or metric == 'capacity_by_tech': if metric == 'costs_yearly': filename = 'costs_yearly_exp' elif metric == 'emissions_yearly': filename = 'emissions_yearly_exp' elif metric == 'activity_by_fuel': filename = 'activity_by_fuel_exp' elif metric == 'activity_by_tech': filename = 'activity_by_tech_exp' elif metric == 'capacity_by_fuel': filename = 'capacity_by_fuel_exp' elif metric == 'capacity_by_tech': filename = 'capacity_by_tech_exp' tt.stoch_resample(folder_results, filename, node_prob) # -------------------------------- # Move to results directory # -------------------------------- cwd = os.getcwd() os.chdir(folder_results) # -------------------------------- # Prepare for plotting # -------------------------------- # Naming conventions and conversions if metric == 'costs_yearly': filename = "costs_yearly_exp_resampled.csv" conversion = 1.0 / 100.0 # Convert from cents/kWh to $/kWh/yr id_vars = ["database", "scenario", "entry"] col_renames = {"scenario": "s", "database": "Scenario"} csv_file = "costs_yearly_toPlot.csv" elif metric == 'emissions_yearly': filename = "emissions_yearly_exp_resampled.csv" conversion = 1.0 / 1000.0 # Convert from kton/yr to Mton/yr id_vars = ["database", "scenario", "entry"] col_renames = {"scenario": "s", "database": "Scenario"} csv_file = "emissions_yearly_toPlot.csv" elif metric == 'activity_by_fuel': filename = "activity_by_fuel_exp_resampled.csv" conversion = 1.0 / 1000.0 # GWh to TWh id_vars = ["database", "scenario", "fuelOrTech", "entry"] col_renames = { "scenario": "s", "database": "Scenario", "fuelOrTech": "Type" } csv_file = "activity_by_fuel_toPlot.csv" elif metric == 'activity_by_tech': filename = "activity_by_tech_exp_resampled.csv" conversion = 1.0 / 1000.0 # GWh to TWh id_vars = ["database", "scenario", "fuelOrTech", "entry"] col_renames = { "scenario": "s", "database": "Scenario", "fuelOrTech": "Type" } csv_file = "activity_by_tech_toPlot.csv" elif metric == 'capacity_by_fuel': filename = "capacity_by_fuel_exp_resampled.csv" conversion = 1.0 # GW id_vars = ["database", "scenario", "fuelOrTech", "entry"] col_renames = { "scenario": "s", "database": "Scenario", "fuelOrTech": "Type" } csv_file = "capacity_by_fuel_toPlot.csv" elif metric == 'capacity_by_tech': filename = "capacity_by_tech_exp_resampled.csv" conversion = 1.0 # GW id_vars = ["database", "scenario", "fuelOrTech", "entry"] col_renames = { "scenario": "s", "database": "Scenario", "fuelOrTech": "Type" } csv_file = "capacity_by_tech_toPlot.csv" # Load and Process data df = pd.read_csv(filename, index_col=0) if metric == 'costs_yearly' or metric == 'emissions_yearly' \ or metric == 'activity_by_fuel' or metric == 'activity_by_tech'\ or metric == 'capacity_by_fuel' or metric == 'capacity_by_tech': df = df.drop("prob", axis=1) for col in df.columns: if 'Unnamed' in col: df = df.drop(col, axis=1) df2 = pd.melt(df, id_vars=id_vars, var_name="Year", value_name="Value") df2.case = "unknown" df2.Value = df2.Value * conversion for db in all_dbs: ind = df2.loc[:, "database"] == db df2.loc[ind, "case"] = prob_type_dict[db] + "-" + infra_dict[ db] + "-" + carbon_tax_dict[db] df2.loc[ind, "database"] = tech_group_dict[db] df2.loc[ind, "prob_type"] = prob_type_dict[db] df2.loc[ind, "infra"] = infra_dict[db] df2.loc[ind, "carbon_tax"] = carbon_tax_dict[db] df2.loc[ind, "infra_and_carbon_tax"] = infra_dict[ db] + "-" + carbon_tax_dict[db] df2 = df2.rename(columns=col_renames) # Save file df2.to_csv(csv_file) # -------------------------------- # Return to original directory # -------------------------------- os.chdir(cwd)
# Inputs db = dbs[0] createPlots = 'Y' # Create default plots saveData = 'Y' # Do not save data as a csv or xls sectorName = 'electric' # Name of sector to be analyzed # Costs yearlyCosts, LCOE = tt.getCosts(folder, db) # Emissions yearlyEmissions, avgEmissions = tt.getEmissions(folder, db) # Analyze capacity and activity by fuel types switch = 'fuel' capacityByFuel = tt.getCapacity(folder, db, switch=switch) ActivityByYearFuel = tt.getActivity(folder, db, switch=switch) ActivityByTODFuel = tt.getActivityTOD(folder, db, switch=switch) # Analyze capacity and activity by technology types switch = 'tech' capacityByTech = tt.getCapacity(folder, db, switch=switch) ActivityByYearFuel = tt.getActivity(folder, db, switch=switch) ActivityByTODTech = tt.getActivityTOD(folder, db, switch=switch) # =============== # Multiple # =============== else: # Inputs: createPlots = 'Y' # Create default plots saveData = 'Y' # Do not save data as a csv or xls
def evaluateMonteCarlo(modelInputs, scenarioXLSX, scenarioName, temoa_path, project_path, solver, cases, caseNum): # Unique filename model_filename = scenarioName + '_MC_' + str(caseNum) # Prepare monte carlo inputs cols = ['type', 'variable', 'tech', caseNum] MCinputs = cases.loc[:, cols] MCinputs = MCinputs.rename(columns={caseNum: 'value'}) # Build Model tt.build(modelInputs, scenarioXLSX, scenarioName, model_filename, MCinputs=MCinputs, path=project_path, mc_type='values') # Run Model error = tt.run(model_filename, saveEXCEL=False, temoa_path=temoa_path, debug=True, solver=solver) # series to store results folder = os.path.join(project_path, 'databases') db = model_filename + '.sqlite' output = pd.Series() output['db'] = db output['caseNum'] = caseNum # Analyze Results if not error: yearlyCosts, LCOE = tt.getCosts(folder, db) yearlyCosts = yearlyCosts.drop(columns=['database', 'scenario']) yearlyEmissions, avgEmissions = tt.getEmissions(folder, db) yearlyEmissions = yearlyEmissions.drop( columns=['database', 'scenario']) # Capacity and Activity by Fuel By Year switch = 'tech' capacityByFuel = tt.getCapacity(folder, db, switch=switch) capacityByFuel = capacityByFuel.drop(columns=['database', 'scenario']) capacityByFuel = capacityByFuel.set_index('fuelOrTech') ActivityByYearFuel = tt.getActivity(folder, db, switch=switch) ActivityByYearFuel = ActivityByYearFuel.drop( columns=['database', 'scenario']) ActivityByYearFuel = ActivityByYearFuel.set_index('fuelOrTech') # Package Outputs output['LCOE'] = LCOE.loc[0, 'LCOE'] output['avgEmissions'] = avgEmissions.loc[0, 'avgEmissions'] for col in yearlyCosts.columns: label = 'cost-' + str(col) output[label] = yearlyCosts.loc[0, col] for col in yearlyEmissions.columns: label = 'emis-' + str(col) output[label] = yearlyEmissions.loc[0, col] # CapacityByYearFuel for ind in capacityByFuel.index: for col in capacityByFuel.columns: label = 'cap_' + str(col) + '-' + str(ind) output[label] = capacityByFuel.loc[ind, col] # ActivityByYearFuel for ind in ActivityByYearFuel.index: for col in ActivityByYearFuel.columns: label = 'act_' + str(col) + '-' + str(ind) output[label] = ActivityByYearFuel.loc[ind, col] else: # if errors output['LCOE'] = np.nan output['avgEmissions'] = np.nan return output
def analyze_db(folder, db, scenario='default', iteration=0, switch='fuel', tod_analysis=False, debug=False): # ============================================================================== # required inputs: # 1) folder - path containing db # 2) db - names of database # optional inputs: # 3) scenario - unique name of this Monte Carlo simulation # 4) iteration - integer representing the iteration within the Monte Carlo simulation # 5) switch - 'fuel' or 'tech', basis of categorization # 6) tod_analysis - if True, performs time of day analysis # # outputs: # 1) output - pandas DataFrame holding all results # ============================================================================== if debug: t0 = time.time() # ----------------------------------- # create dataframe to hold outputs # ----------------------------------- output = pd.DataFrame() # ----------------------------------- # check for appropriate value of switch # ----------------------------------- if switch not in ['tech', 'fuel']: switch = 'fuel' # ----------------------------------- # yearly_costs and LCOE # ----------------------------------- yearly_costs, LCOE = tt.getCosts(folder, db) # LCOE row = get_series(scenario, iteration, db) row['quantity'] = 'LCOE' row['value'] = LCOE output = output.append(row, ignore_index=True) # yearly_costs yearly_costs = yearly_costs.drop(columns=['database', 'scenario']) df = get_df(scenario, iteration, db, yearly_costs.shape[1]) df.loc[:, 'quantity'] = 'costs_by_year' df.loc[:, 'year'] = yearly_costs.columns df.loc[:, 'value'] = yearly_costs.loc[0, :].values output = output.append(df, ignore_index=True) if debug: t1 = time.time() # ----------------------------------- # yearly_emissions and average_emissions # ----------------------------------- yearly_emissions, average_emissions = tt.getEmissions(folder, db) # average_emissions row = get_series(scenario, iteration, db) row['quantity'] = 'average_emissions' row['value'] = average_emissions output = output.append(row, ignore_index=True) # yearly_emissions yearly_emissions = yearly_emissions.drop(columns=['database', 'scenario']) df = get_df(scenario, iteration, db, yearly_costs.shape[1]) df.loc[:, 'quantity'] = 'emissions_by_year' df.loc[:, 'year'] = yearly_emissions.columns df.loc[:, 'value'] = yearly_emissions.loc[0, :].values output = output.append(df, ignore_index=True) if debug: t2 = time.time() # ----------------------------------- # capacity_by_year # ----------------------------------- # analyze capacity_by_year = tt.getCapacity(folder, db, switch=switch) capacity_by_year = capacity_by_year.drop(columns=['database', 'scenario']) # reorganize temp = pd.melt(capacity_by_year, id_vars=['fuelOrTech'], var_name='year') # store results df = get_df(scenario, iteration, db, temp.shape[0]) df.loc[:, 'quantity'] = 'capacity_by_year' df.loc[:, 'quantity_type'] = switch df.loc[:, 'tech_or_fuel'] = temp.loc[:, 'fuelOrTech'].values df.loc[:, 'year'] = temp.loc[:, 'year'].values df.loc[:, 'value'] = temp.loc[:, 'value'].values output = output.append(df, ignore_index=True) if debug: t3 = time.time() # ----------------------------------- # activity_by_year # ----------------------------------- # analyze activity_by_year = tt.getActivity(folder, db, switch=switch) activity_by_year = activity_by_year.drop(columns=['database', 'scenario']) # reorganize temp = pd.melt(activity_by_year, id_vars=['fuelOrTech'], var_name='year') # store results df = get_df(scenario, iteration, db, temp.shape[0]) df.loc[:, 'quantity'] = 'activity_by_year' df.loc[:, 'quantity_type'] = switch df.loc[:, 'tech_or_fuel'] = temp.loc[:, 'fuelOrTech'].values df.loc[:, 'year'] = temp.loc[:, 'year'].values df.loc[:, 'value'] = temp.loc[:, 'value'].values output = output.append(df, ignore_index=True) if debug: t4 = time.time() # ----------------------------------- # activity_by_tod # ----------------------------------- if tod_analysis: # analyze activity_by_tod = tt.getActivityTOD(folder, db, switch=switch) activity_by_tod = activity_by_tod.drop( columns=['database', 'scenario']) # store results df = get_df(scenario, iteration, db, activity_by_tod.shape[0]) df.loc[:, 'quantity'] = 'activity_by_tod' df.loc[:, 'quantity_type'] = switch df.loc[:, 'tech_or_fuel'] = activity_by_tod.loc[:, 'fuelOrTech'].values df.loc[:, 'year'] = activity_by_tod.loc[:, 'year'].values df.loc[:, 'season'] = activity_by_tod.loc[:, 'season'].values df.loc[:, 'tod'] = activity_by_tod.loc[:, 'tod'].values df.loc[:, 'value'] = activity_by_tod.loc[:, 'value'].values output = output.append(df, ignore_index=True) if debug: t5 = time.time() print('Run time per analysis (seconds):') print('costs :' + str(t1 - t0)) print('emissions :' + str(t2 - t1)) print('capacity_by_year :' + str(t3 - t2)) print('activity_by_year :' + str(t4 - t3)) print('activity_by_tod :' + str(t5 - t4)) return output
def evaluateMonteCarlo(modelInputs, scenarioXLSX, scenarioName, temoa_path, cases, caseNum): # Unique filename model_filename = scenarioName + '_MC_' + str(caseNum) # Prepare monte carlo inputs cols = ['type', 'variable', 'tech', caseNum] MCinputs = cases.ix[:, cols] MCinputs = MCinputs.rename(columns={caseNum: 'multiplier'}) # Build Model tt.build(modelInputs, scenarioXLSX, scenarioName, model_filename, MCinputs=MCinputs, path='data') # Run Model tt.run(model_filename, temoa_path=temoa_path, saveEXCEL=False) # Analyze Results folder = os.getcwd() + '\\Databases' db = model_filename + '.sqlite' yearlyCosts, LCOE = tt.getCosts(folder, db) yearlyEmissions, avgEmissions = tt.getEmissions(folder, db) # Capacity and Activity by Fuel By Year createPlots = 'N' # Create default plots saveData = 'N' # Do not save data as a csv or xls sectorName = 'electric' # Name of sector to be analyzed switch = 'fuel' capacityByFuel = tt.getCapacity(folder, db, switch=switch) key = capacityByFuel.keys()[0] cap = capacityByFuel[key] ActivityByYearFuel = tt.getActivity(folder, db, switch=switch) key = ActivityByYearFuel.keys()[0] act = ActivityByYearFuel[key] # Move results to series col = yearlyCosts.columns[0] yearlyCosts = yearlyCosts[col] LCOE = LCOE[col] col = yearlyEmissions.columns[0] yearlyEmissions = yearlyEmissions[col] avgEmissions = avgEmissions[col] # Package Outputs output = pd.Series() output['db'] = db output['caseNum'] = caseNum output['LCOE'] = LCOE output['avgEmissions'] = avgEmissions for ind in yearlyCosts.index: label = 'cost-' + str(ind) output[label] = yearlyCosts.loc[ind] for ind in yearlyEmissions.index: label = 'emis-' + str(ind) output[label] = yearlyEmissions.loc[ind] # CapacityByYearFuel for ind in cap.index: for col in cap.columns: label = 'cap_' + str(col) + '-' + str(ind) output[label] = cap.loc[ind, col] # ActivityByYearFuel for ind in act.index: for col in act.columns: label = 'act_' + str(col) + '-' + str(ind) output[label] = act.loc[ind, col] return output