Skip to content

Use Case: Adding a New Agent

Kseniya Husak edited this page Aug 2, 2021 · 3 revisions

Although all DBs in dGen come with pre-generated agents with respective weights and buildings, users can add new agents. This section outlines the process for revising the agent files. You will need to launch Jupyter as well as PgAdmin.

A pre-populated jupyter notebook detailing these steps can be found here.

Step 1: Open a new Jupyter Notebook and Import the necessary modules/packages:

import pandas as pd 
import numpy as np 
import psycopg2 as pg 
import sqlalchemy 
from sqlalchemy import create_engine 

Step 2: Launch PgAdmin and Connect to your restored DB

Step 3: In your Jupyter NB connect to the docker OS DB

engine2 = create_engine("postgresql+psycopg2://postgres:[email protected]:5432/dgen_db")
con2 = engine2.connect() 

Note: If you used a port other than 5432 to restore the database or if you named your DB something other than “dgen_db”, you will need to update the string to reflect that.

Step 4: Load the original agent pkl file downloaded from Great Lakes into your Jupyter NB

rp2='/Users/...//agent_df_base_res_wi_revised.pkl' 
res_agents2 = pd.read_pickle(rp2) 

Step 5: Amend the pkl file by adding a new building + agent row to it and saving it under a new file name.

In this case, we are adding a new building with a bldg_id of 2367600 to the agent file. We are going to use the first agent in the pkl file (you can pick any row that corresponds to the utility for which you wish to add an agent), modify the bldg_id and append the result as a new row, thus generating a new agent:

row_to_amend = res_agents2.iloc[0].values.tolist() 
new_row = row_to_amend[:3] + [2367600] + row_to_amend[4:] 
res_agents2.loc[res_agents2.index.max()+1] = new_row 
new_agents = '/Users/.../agent_df_base_res_wi_new_row.pkl' 
res_agents2.to_pickle(new_agents) 

Step 6: Amend the Agent(s) Load Profile

If you are adding a residential agent row with a new building id, you will need to amend the “restock_load_profiles” table that maps the new building id to a load profile. For commercial agents, you will need to amend the “comstock_load_profiles” table respectively. In this example we are adding only one new agent row, so we only need to amend one load profile. 2367600 refers to the new agent id and was created by taking the id of the last agent + 00. As long as you use a unique id, however, it could be any number.

res_sql = '''SELECT * FROM diffusion_load_profiles.resstock_load_profiles''' 
res_df = pd.read_sql(res_sql,con2) 
res_load_row = res_df[res_df.bldg_id==23676].iloc[0] 
new_res_load_row = [2367600] + res_load_row.values.tolist()[1:] 
res_df.loc[res_df.index.max()+1] = new_res_load_row 

Step 7: Rename the "resstock_load_profiles" table in PgAdmin

Open the Query Tool in PgAdmin:

Rename the load profile table by running the following command:

ALTER TABLE diffusion_load_profiles.resstock_load_profiles RENAME TO resstock_load_profiles_og; 

Then run the below command in Query Tool to create a new table:

CREATE TABLE diffusion_load_profiles.resstock_load_profiles 

( 
    bldg_id integer, 
    sector_abbr text COLLATE pg_catalog."default", 
    state_abbr text COLLATE pg_catalog."default", 
    kwh_load_profile real[] 
) 

WITH ( 
    OIDS = FALSE 
) 
TABLESPACE pg_default; 

Step 8: Push the DF created in Step 6 to the new table created above:

tbl_name = 'resstock_load_profiles' 
res_df.to_sql(tbl_name, engine2, schema='diffusion_load_profiles', if_exists='append', index=False)