add draw.py
This commit is contained in:
		
							
								
								
									
										122
									
								
								draw.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										122
									
								
								draw.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,122 @@ | ||||
| import matplotlib.pyplot as plt | ||||
| import matplotlib.ticker as ticker | ||||
| import numpy as np | ||||
| import pandas as pd | ||||
| import os | ||||
| import seaborn as sns | ||||
| import json | ||||
| from matplotlib.colors import LinearSegmentedColormap | ||||
|  | ||||
|  | ||||
| def read_data(file_name: str): | ||||
|     with open(file_name, 'r') as f: | ||||
|         data = json.load(f) | ||||
|     for key, value in data.items(): | ||||
|         for subkey, subvalue in value.items(): | ||||
|             data[key][subkey] = float(subvalue) | ||||
|     df = pd.DataFrame.from_dict(data, orient='index') | ||||
|     df.index = pd.to_numeric(df.index) | ||||
|     df.columns = pd.to_numeric(df.columns) | ||||
|     return df | ||||
|  | ||||
| def draw_results(results, filename, title, annot_benefit=False, figure_size=(10, 10)): | ||||
|     df= results | ||||
|     df = df.astype(float) | ||||
|     df.index = df.index / 1000 | ||||
|     df.columns = df.columns / 1000 | ||||
|     min_value = df.min().min() | ||||
|     max_value = df.max().max() | ||||
|     max_scale = max(abs(min_value/1000), abs(max_value/1000)) | ||||
|     plt.figure(figsize=figure_size) | ||||
|     cmap = sns.color_palette("coolwarm", as_cmap=True) | ||||
|     ax = sns.heatmap(df/1000, fmt=".1f", cmap=cmap, vmin=-max_scale, vmax=max_scale, annot=annot_benefit) | ||||
|     ax.xaxis.set_major_formatter(ticker.FuncFormatter(lambda x, _: f"{x:.2f}")) | ||||
|     # ax.yaxis.set_major_formatter(ticker.FormatStrFormatter('%.1f')) | ||||
|     plt.title(title) | ||||
|     plt.gca().invert_yaxis() | ||||
|     plt.xlabel('ESS Capacity (MWh)') | ||||
|     plt.ylabel('PV Capacity (MW)') | ||||
|     plt.savefig(filename) | ||||
|  | ||||
| def draw_cost(costs, filename, title_cost, annot_cost=False, figure_size=(10, 10)): | ||||
|     df = costs | ||||
|     df = df.astype(int) | ||||
|     print(df.index) | ||||
|     df.index = df.index / 1000 | ||||
|     print(df.columns) | ||||
|     df.columns = df.columns / 1000 | ||||
|  | ||||
|     plt.figure(figsize=figure_size) | ||||
|     sns.heatmap(df/1000000,  fmt=".1f", cmap='viridis', annot=annot_cost) | ||||
|     plt.title(title_cost) | ||||
|     plt.gca().invert_yaxis() | ||||
|     plt.xlabel('ESS Capacity (MWh)') | ||||
|     plt.ylabel('PV Capacity (MW)') | ||||
|     print(filename) | ||||
|     plt.savefig(filename) | ||||
|  | ||||
| def draw_overload(overload_cnt, filename, title_unmet, annot_unmet=False, figure_size=(10, 10)): | ||||
|     df = overload_cnt | ||||
|     df = df.astype(int) | ||||
|     df.index = df.index / 1000 | ||||
|     df.columns = df.columns / 1000 | ||||
|     min_value = df.min().min() | ||||
|     max_value = df.max().max() | ||||
|     max_scale = max(abs(min_value/1000), abs(max_value/1000)) | ||||
|  | ||||
|     plt.figure(figsize=figure_size) | ||||
|     cmap = LinearSegmentedColormap.from_list("", ["white", "blue"]) | ||||
|     ax = sns.heatmap(df/(4*24*365), fmt=".00%", cmap=cmap, vmin=0, vmax=1, annot=annot_unmet) | ||||
|     cbar = ax.collections[0].colorbar | ||||
|     cbar.set_ticks([0, 0.25, 0.5, 0.75, 1]) | ||||
|     cbar.set_ticklabels(['0%', '25%', '50%', '75%', '100%']) | ||||
|     cbar.ax.yaxis.set_major_formatter(ticker.FuncFormatter(lambda x, pos: f'{x:.0%}')) | ||||
|  | ||||
|     plt.title(title_unmet) | ||||
|     plt.gca().invert_yaxis() | ||||
|     plt.xlabel('ESS Capacity (MWh)') | ||||
|     plt.ylabel('PV Capacity (MW)') | ||||
|     plt.savefig(filename) | ||||
|  | ||||
| directory = 'data/' | ||||
|  | ||||
| file_list = [f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))] | ||||
|  | ||||
|  | ||||
| split_files = [f.split('-') for f in file_list] | ||||
|  | ||||
| for f in split_files: | ||||
|     print(f[-1]) | ||||
| costs_files = [f for f in split_files if f[-1].endswith('costs.json')] | ||||
| print(f'find costs files: {costs_files}') | ||||
| overload_files = [f for f in split_files if f[-1].endswith('overload_cnt.json')] | ||||
| print(f'find overload files: {overload_files}') | ||||
| results_files = [f for f in split_files if f[-1].endswith('results.json')] | ||||
| print(f'find results files: {results_files}') | ||||
|  | ||||
| costs_dfs = [read_data(directory + '-'.join(f)) for f in costs_files] | ||||
| overload_dfs = [read_data(directory + '-'.join(f)) for f in overload_files] | ||||
| results_dfs = [read_data(directory + '-'.join(f)) for f in results_files] | ||||
|  | ||||
| for costs_df, overload_df, results_df in zip(costs_dfs, overload_dfs, results_dfs): | ||||
|     # print(costs_df.index) | ||||
|     # print(pd.to_numeric(costs_df.columns)) | ||||
|     # costs_df.index = pd.to_numeric(costs_df.columns ) | ||||
|     # costs_df.columns = pd.to_numeric(costs_df.index) | ||||
|     print(costs_df) | ||||
|     draw_cost(costs_df, f'plots/costs-{int(costs_df.columns[-1])}.png', f'Costs for PV-{int(costs_df.columns[-1])}MW ESS-{int(costs_df.index[-1])}MWh', annot_cost=True) | ||||
|     # overload_df.index = pd.to_numeric(overload_df.columns, errors='coerce') | ||||
|     # overload_df.columns = pd.to_numeric(overload_df.columns, errors='coerce') | ||||
|     print(overload_df) | ||||
|     # draw_overload(overload_df, f'plots/overload-{overload_df.columns[-1]}', f'Overload for PV-{overload_df.columns[-1]}MW ESS-{overload_df.index[-1]}MWh', annot_unmet=True) | ||||
|     # results_df.index = pd.to_numeric(results_df.columns, errors='coerce') | ||||
|     # results_df.columns = pd.to_numeric(results_df.columns, errors='coerce') | ||||
|     # draw_results(results_df, f'plots/results-{results_df.columns[-1]}', f'Results for PV-{results_df.columns[-1]}MW ESS-{results_df.index[-1]}MWh', annot_benefit=True) | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
		Reference in New Issue
	
	Block a user