<jupyter_start><jupyter_text>Styling & Helper Functions  Import PackagesIf some packages fail then google 'conda install ' or 'pip install '    You can run 'conda install -c anaconda psycopg2' directly in a new cell or in your anaconda terminal<jupyter_code>import psycopg2 as pg
import psycopg2.extras as pgx
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.ticker as mtick

import sqlalchemy
from sqlalchemy import create_engine
import pyarrow as pa
import pyarrow.parquet as pq
from IPython.display import HTML
import base64

from pylab import *
%matplotlib inline
import geopandas as gpd
# *** Styling Block ***
fmt = '${x:,.0f}'
tick = mtick.StrMethodFormatter(fmt)
SMALL_SIZE = 14
MEDIUM_SIZE = 16
BIGGER_SIZE = 18
FIG_SIZE = (12,9) #change figure size throughout for non-faceted figures
plt.rc('font', size=SMALL_SIZE)          # controls default text sizes
plt.rc('axes', titlesize=BIGGER_SIZE)     # fontsize of the axes title
plt.rc('axes', labelsize=MEDIUM_SIZE)    # fontsize of the x and y labels
plt.rc('xtick', labelsize=SMALL_SIZE)    # fontsize of the tick labels
plt.rc('ytick', labelsize=SMALL_SIZE)    # fontsize of the tick labels
plt.rc('legend', fontsize=SMALL_SIZE)    # legend fontsize
plt.rc('figure', titlesize=BIGGER_SIZE)  # fontsize of the figure title
sns.set_style('white')
cp = sns.color_palette() #color palette to use throughout 

def pull_data(sql,data,con):
    out = pd.DataFrame()
    for i,r in data.iterrows():
        sql_in = sql % (r['scenario'], r['schema'], r['year'])
        out = pd.concat([out, pd.read_sql(sql_in, con)])
    return out

# *** Helper function to easily download dataframes as csv files
def create_download_link( df, title = "Download CSV file", filename = "give_me_a_detailed_name.csv"):  
    csv = df.to_csv(index =True)
    b64 = base64.b64encode(csv.encode())
    payload = b64.decode()
    html = '<a download="{filename}" href="data:text/csv;base64,{payload}" target="_blank">{title}</a>'
    html = html.format(payload=payload,title=title,filename=filename)
    return HTML(html)<jupyter_output><empty_output><jupyter_text>Connect to your specific database<jupyter_code>x = 'dgen_db' # if you named your database something else then change the name here
engine = create_engine("postgresql+psycopg2://postgres:postgres@localhost:5432/{0}".format(x)) # must use localhost if that's how it appears in pgAdmin
con = engine.connect()<jupyter_output><empty_output><jupyter_text>Specify Databases to Load<jupyter_code>schema = 'diffusion_results_20210323_161833917278_scenario' # -- must input specific schema name here

state_abbr = 'ME' # -- must input specific state abbreviation here
sector_abbr = 'res' # -- must input specific sector abbreviation here
scenario = 'res_me' # -- must input specific scenario name here<jupyter_output><empty_output><jupyter_text>Define SQL Queries<jupyter_code>sql_in = '''
WITH a AS(
    SELECT *,'%s'::text as scenario
    FROM %s.agent_outputs
)
SELECT SUM(system_kw_cum)/1e3 as deployment_mw,
SUM(developable_agent_weight*developable_roof_sqft) as sum_dev_roof_sqft,
AVG(payback_period) as payback,
AVG(system_kw) as system_size,
(SUM(developable_agent_weight*load_kwh_per_customer_in_bin::numeric))/1E9 as load_twh,
(SUM(system_kw_cum*8760*capacity_factor)/1E9) AS Cum_DPV_Gen_TWh,
year, sector_abbr, scenario
FROM a
WHERE state_abbr='%s' and sector_abbr='%s' AND year<=2030
GROUP BY year, state_abbr, sector_abbr, scenario
ORDER BY year;'''%(scenario, schema, state_abbr, sector_abbr)
df = pd.read_sql(sql_in, con)<jupyter_output><empty_output><jupyter_text>Print Queries DataFrame<jupyter_code>df
pair_columns = ['scenario','payback','deployment_mw','load_twh']
pair_df = df # copy of df
pair_df = pair_df[pair_columns]

fg = sns.PairGrid(pair_df, hue='scenario')
fg = fg.map_diag(sns.histplot)
fg = fg.map_upper(plt.scatter, s=250, alpha = 0.2) # upper size is variable based on pv_MW_cum
fg = fg.map_lower(plt.scatter, s=40, alpha = 0.2) # lower size is fixed
fg = fg.add_legend()

plt.subplots_adjust(top=0.9)
fg.fig.suptitle('Pair Plots for Quick Summary')<jupyter_output><empty_output><jupyter_text>Format DataFrames<jupyter_code>df_sys_size = df.pivot_table(values = 'system_size', index = 'year', columns = ['scenario']).reset_index()
df_sys_size
df_payback = df.pivot_table(values = 'payback', index = 'year', columns = ['scenario']).reset_index()
df_payback
df_deployment = df.pivot_table(values = 'deployment_mw', index = 'year', columns = ['scenario']).reset_index()
df_deployment
df3 = df_deployment
df4 = df_sys_size
df5 = df_payback<jupyter_output><empty_output><jupyter_text>Deployment<jupyter_code>plt.figure(figsize = (9,4))
ax = plt.subplot(111)
chartBox = ax.get_position()

ax.plot('year', scenario, data = df3, linestyle = '--', linewidth = 1.5, color = 'C0')

plt.xlabel('Year', fontweight='bold')
plt.ylabel('Cumulative Deployment ($MW_{AC}$)', fontweight='bold')
plt.xlim(2013.5,2030.5)
plt.xticks(np.arange(2014, 2030, 2))
ax.yaxis.grid(True, color='#7e7e7e', linestyle=':')
ax.set_position([chartBox.x0, chartBox.y0, chartBox.width*0.6, chartBox.height])
ax.legend(loc='upper center', bbox_to_anchor=(1.27, 0.8), shadow=True, ncol=1)<jupyter_output><empty_output><jupyter_text>System Size<jupyter_code>plt.figure(figsize = (9,4))
ax = plt.subplot(111)
chartBox = ax.get_position()

ax.plot('year', scenario, data = df4, linestyle = '--', linewidth = 1.5, color = 'C0')

plt.xlabel('Year', fontweight='bold')
plt.ylabel('Average System Size($KW_{AC}$)', fontweight='bold')
plt.xlim(2013.5,2030.5)
plt.xticks(np.arange(2014, 2030, 2))
ax.yaxis.grid(True, color='#7e7e7e', linestyle=':')
ax.set_position([chartBox.x0, chartBox.y0, chartBox.width*0.6, chartBox.height])
ax.legend(loc='upper center', bbox_to_anchor=(1.27, 0.8), shadow=True, ncol=1)<jupyter_output><empty_output><jupyter_text>Payback Period<jupyter_code>plt.figure(figsize = (9,4))
ax = plt.subplot(111)
chartBox = ax.get_position()

ax.plot('year', scenario, data = df5, linestyle = '--', linewidth = 1.5, color = 'C0')

plt.xlabel('Year', fontweight='bold')
plt.ylabel('Average Payback Period (yrs)', fontweight='bold')
plt.xlim(2013.5,2030.5)
plt.xticks(np.arange(2014, 2030, 2))
ax.yaxis.grid(True, color='#7e7e7e', linestyle=':')
ax.set_position([chartBox.x0, chartBox.y0, chartBox.width*0.6, chartBox.height])
ax.legend(loc='upper center', bbox_to_anchor=(1.27, 0.8), shadow=True, ncol=1)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13995693632728393, 'fraction_numerical': 0.031067363888034452, 'mean_word_length': 6.339729119638826, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '363110', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 2561, 'n_tokens_neox': 2393, 'n_words': 530, 'path': 'dgen_os/notebooks/visualize_results.ipynb', 'repo_name': 'pswild/dgen', 'size': '6502'}