Skip to content

Commit

Permalink
Change test dir to tests. Make small changes to scenario import and t…
Browse files Browse the repository at this point in the history
…weaks to visuals
  • Loading branch information
Merrielle Ondreicka committed Nov 8, 2019
1 parent e1617e4 commit 34107e4
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 27 deletions.
9 changes: 6 additions & 3 deletions postreise/plot/multi/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
SCENARIO_RESOURCE_TYPES = ['wind', 'solar', 'ng', 'coal', 'nuclear', 'geothermal', 'hydro']
ALL_RESOURCE_TYPES = SCENARIO_RESOURCE_TYPES + ['other inc. biomass']
RESOURCE_LABELS = {'wind': 'Wind', 'solar': 'Solar', 'ng': 'Natural Gas', 'coal': 'Coal', 'nuclear': 'Nuclear', 'geothermal': 'Geothermal', 'hydro': 'Hydro', 'other inc. biomass': 'Other inc. Biomass'}
RESOURCE_COLORS = type2color = {
RESOURCE_COLORS = {
'wind': sns.xkcd_rgb["green"],
'solar': sns.xkcd_rgb["amber"],
'hydro': sns.xkcd_rgb["light blue"],
'ng': sns.xkcd_rgb["orchid"],
'nuclear': sns.xkcd_rgb["silver"],
'coal': sns.xkcd_rgb["light brown"],
'geothermal': sns.xkcd_rgb["hot pink"],
'dfo': sns.xkcd_rgb["royal blue"],
'storage': sns.xkcd_rgb["orange"],
'other inc. biomass': 'rebeccapurple',
'other': 'royalblue'
}
Expand All @@ -33,15 +36,15 @@
'Western': 223.369046}
CA_BASELINES = {
'Arizona': 4.567094,
'California': 48.954528,
'California': 81.0,
'Colorado': 11.039050999999999,
'Idaho': 2.5057110000000002,
'Montana Western': 2.28368,
'Nevada': 7.047959,
'New Mexico Western': 2.5586450000000003,
'Oregon': 7.509413,
'Utah': 2.798768,
'Washington': 96.129826,
'Washington': 7.24,
'Wyoming': 5.9288989999999995,
'El Paso': 0.021684000000000002,
'Western': 191.34526}
Expand Down
6 changes: 3 additions & 3 deletions postreise/plot/multi/plot_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def _construct_bar_visuals(zone, ax_data_list):
ax.set_yticks([])
ax.set_ylim(top=1.3*ax.get_ylim()[1])

ax.legend(bbox_to_anchor=(-.03, -.67), loc='lower left', fontsize=16)
ax.legend(bbox_to_anchor=(-.03, -.67), loc='upper left', fontsize=16)

for p in ax.patches:
b = p.get_bbox()
Expand All @@ -115,7 +115,7 @@ def _construct_hbar_visuals(zone, ax_data_list):

fig, axes = plt.subplots(1, 2, figsize=(20, 0.7*num_scenarios*num_resource_types))
plt.suptitle(zone, fontsize=30, verticalalignment="bottom")
#plt.subplots_adjust(wspace=1)
plt.subplots_adjust(wspace=1)

for ax_data, ax in zip(ax_data_list, axes):
df = pd.DataFrame(ax_data['values'], index=ax_data['labels'])
Expand All @@ -131,7 +131,7 @@ def _construct_hbar_visuals(zone, ax_data_list):
ax.set_xticks([])

handles, labels = ax.get_legend_handles_labels()
ax.legend(reversed(handles), reversed(labels), bbox_to_anchor=(-.03, -0.5), loc='lower left', fontsize=16)
ax.legend(reversed(handles), reversed(labels), bbox_to_anchor=(-.03, 0), loc='upper left', fontsize=16)

for p in ax.patches:
b = p.get_bbox()
Expand Down
4 changes: 2 additions & 2 deletions postreise/plot/multi/plot_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ def _format_scenario_data(data_chart, scenario_name):
gen_data[zone] = {}
cap_data[zone] = {}

for resource in data_chart[zone]['Generation'].keys():
gen_data[zone][resource] = unit_conversion(sum(data_chart[zone]['Generation'][resource].values()), 2) #MWh to TWh
for resource in data_chart[zone]['Generation'].to_dict().keys():
gen_data[zone][resource] = unit_conversion(sum(data_chart[zone]['Generation'][resource].to_dict().values()), 2) #MWh to TWh
cap_data[zone][resource] = unit_conversion(data_chart[zone]['Capacity'][resource], 1) #MW to GW

formatted_data['gen']['data'] = gen_data
Expand Down
2 changes: 1 addition & 1 deletion postreise/plot/multi/plot_pie.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def _construct_pie_ax_data(zone, scenarios, min_percentage):
ax_data, labels = _roll_up_small_pie_wedges(scenario[side]['data'][zone], min_percentage)

ax_data_list.append({
'title': scenario[side]['label'],
'title': '{0}\n{1}'.format(scenario['label'], scenario[side]['label']),
'labels': labels,
'values': list(ax_data.values()),
'colors': [RESOURCE_COLORS[resource] for resource in ax_data.keys()],
Expand Down
17 changes: 10 additions & 7 deletions postreise/plot/multi/plot_shortfall.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def plot_shortfall(interconnect, scenario_ids=None, custom_data=None, is_match_C
ax_data, shortfall_pct_list = _construct_shortfall_data_for_western(graph_data, is_match_CA, has_collaborative_scenarios, baselines[zone], targets, demand[zone])
else:
ax_data, shortfall_pct_list = _construct_shortfall_ax_data(zone, graph_data, is_match_CA, baselines[zone], targets[zone], demand[zone])
_construct_shortfall_visuals(zone, ax_data, shortfall_pct_list, targets[zone], is_match_CA)
_construct_shortfall_visuals(zone, ax_data, shortfall_pct_list, is_match_CA, targets[zone], demand[zone])
print(f'\nDone\n')

# returns: dictionary of data to visualize { '2016 NREL': {'2016 Renewables': 100, 'Simulated increase in renewables': 30, 'Missed target': 10 }, ...}
Expand All @@ -45,8 +45,8 @@ def _construct_shortfall_ax_data(zone, scenarios, is_match_CA, baseline, target,
shortfall_pct_list.append(shortfall_pct)

#TODO this breaks the visuals if renewables decrease - decide with team on how to show

#print(baseline, '|', total_renewables, '|', target, '|', shortfall)
print(f'\n\n{zone}\n')
print(baseline, '|', total_renewables, '|', target, '|', shortfall)
ax_data.update({scenario['label']: {
'2016 Renewables': baseline,
'Simulated increase in renewables': max(0, round(total_renewables - baseline, 2)),
Expand Down Expand Up @@ -81,7 +81,10 @@ def _construct_shortfall_data_for_western(scenarios, is_match_CA, has_collaborat

shortfall_pct_list.append(round(shortfall/demand*100, 1))

#print(baseline, '|', total_renewables, '|', target, '|', shortfall)
# If increase in renewables is 0 we have a base case scenario and thus the baseline is used as the source of truth
shortfall = max(0, targets['Western'] - baseline) if total_renewables <= baseline else shortfall
print(f'\n\nWestern\n')
print(baseline, '|', total_renewables, '|', targets['Western'], '|', shortfall)
ax_data.update({scenario['label']: {
'2016 Renewables': baseline,
'Simulated increase in renewables': max(0, round(total_renewables - baseline, 2)),
Expand All @@ -106,20 +109,20 @@ def _get_total_generated_renewables(zone, resource_data, is_match_CA):

return total_renewables

def _construct_shortfall_visuals(zone, ax_data, shortfall_pct_list, target, is_match_CA):
def _construct_shortfall_visuals(zone, ax_data, shortfall_pct_list, is_match_CA, target, demand):
df = pd.DataFrame(ax_data).T
ax = df.plot.bar(stacked=True, color=['darkgreen', 'yellowgreen', 'salmon'], figsize=(10, 8), fontsize=16)
ax.set_title(zone, fontsize=26)
ax.set_ylim(top=1.33*ax.get_ylim()[1])
ax.xticks(rotation=45, horizontalalignment='right')
ax.set_xticklabels(ax.get_xticklabels(), rotation=45, horizontalalignment='right')

#Legend
handles, labels = ax.get_legend_handles_labels()
ax.legend(reversed(handles), reversed(labels), bbox_to_anchor=(1.556, 1.015), fontsize=14)

# Add target line
if target > 0:
ax_text = f'Target {target} of\n2030 demand'
ax_text = f'Target {int(round(target/demand*100, 0))}% of\n2030 demand'
if is_match_CA and zone != 'California':
ax_text = 'CA ' + ax_text

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import pandas as pd

# data_chart: zone -> gen/cap -> resource_type -> vals for each date
_MOCK_DATA_CHART = {
'Washington': {
'Generation': {
'Generation': pd.DataFrame({
'coal': {'12252016': 2000000, '12262016': 2000000},
'solar': {'12252016': 0000000, '12262016': 1000000},
'hydro': {'12252016': 3000000, '12262016': 2000000}
},
}),
'Capacity': {
'coal': 5000,
'solar': 1000,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from postreise.plot.multi.test.mock_graph_data import create_mock_graph_data
from postreise.plot.multi.tests.mock_graph_data import create_mock_graph_data
from postreise.plot.multi.plot_bar import _construct_bar_ax_data, _get_bar_display_val, _get_bar_resource_types

# Since these are unit tests we're intentionally not testing methods that only have visualization code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from pprint import pprint

from postreise.plot.multi.constants import ZONES, BASELINES, CA_BASELINES, TARGETS, CA_TARGETS, DEMAND
from postreise.plot.multi.test.mock_graph_data import create_mock_graph_data
from postreise.plot.multi.test.mock_data_chart import create_mock_data_chart
from postreise.plot.multi.tests.mock_graph_data import create_mock_graph_data
from postreise.plot.multi.tests.mock_data_chart import create_mock_data_chart
from postreise.plot.multi.plot_helpers import handle_plot_inputs, handle_shortfall_inputs, _format_scenario_data, unit_conversion

# Since these are unit tests we're intentionally not testing methods that pull in data
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
from postreise.plot.multi.constants import RESOURCE_COLORS
from postreise.plot.multi.test.mock_graph_data import create_mock_graph_data
from postreise.plot.multi.tests.mock_graph_data import create_mock_graph_data
from postreise.plot.multi.plot_pie import _construct_pie_ax_data, _roll_up_small_pie_wedges

# Since these are unit tests we're intentionally not testing methods that only have visualization code

def test_construct_pie_ax_data():
ax_data_list = _construct_pie_ax_data('Washington', create_mock_graph_data(), min_percentage=0)
expected_output = [{
'title': 'Generation',
'title': '2016 Simulated Base Case\nGeneration',
'labels': ['Coal', 'Solar', 'Hydro'],
'values': [4,1,5],
'colors': [RESOURCE_COLORS['coal'], RESOURCE_COLORS['solar'], RESOURCE_COLORS['hydro']],
'unit': 'TWh'
},{
'title': 'Capacity',
'title': '2016 Simulated Base Case\nCapacity',
'labels': ['Coal', 'Solar', 'Hydro'],
'values': [5,1,4],
'colors': [RESOURCE_COLORS['coal'], RESOURCE_COLORS['solar'], RESOURCE_COLORS['hydro']],
'unit': 'GW'
},{
'title': 'Generation',
'title': '2016 NREL Data\nGeneration',
'labels': ['Coal', 'Solar', 'Hydro'],
'values': [4,1,5],
'colors': [RESOURCE_COLORS['coal'], RESOURCE_COLORS['solar'], RESOURCE_COLORS['hydro']],
'unit': 'TWh'
},{
'title': 'Capacity',
'title': '2016 NREL Data\nCapacity',
'labels': ['Coal', 'Solar', 'Hydro', 'Wind'],
'values': [3,1,4,2],
'colors': [RESOURCE_COLORS['coal'], RESOURCE_COLORS['solar'], RESOURCE_COLORS['hydro'], RESOURCE_COLORS['wind']],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from postreise.plot.multi.constants import ZONES
from postreise.plot.multi.test.mock_graph_data import create_mock_graph_data
from postreise.plot.multi.tests.mock_graph_data import create_mock_graph_data
from postreise.plot.multi.plot_shortfall import _construct_shortfall_ax_data, _construct_shortfall_data_for_western, _get_total_generated_renewables

# Since these are unit tests we're intentionally not testing methods that only have visualization code
Expand Down

0 comments on commit 34107e4

Please sign in to comment.