-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from hyperledger-labs/add-plotting
Add plotting script for cost analysis test
- Loading branch information
Showing
3 changed files
with
111 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ typechain-types | |
ignition/deployments | ||
.openzeppelin | ||
package-lock.json | ||
**/*.csv | ||
**/*costs.csv | ||
**/*_plot.png |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import matplotlib.pyplot as plt\n", | ||
"import numpy as np\n", | ||
"import pandas as pd\n", | ||
"import os\n", | ||
"\n", | ||
"\n", | ||
"folder_path = './' # update with the actual folder path if using this notebook in a different place\n", | ||
"prefix = '2024-09-23T05:02:36.434Z' # update with specific file prefix that needs to be plotted\n", | ||
"\n", | ||
"window_size = 500 # how many transaction to measure to draw the moving average trend line\n", | ||
"threshold_tx = 5000 # highlight area after how many transactions\n", | ||
"\n", | ||
"# find a list of CSV files in the folder with the specified prefix\n", | ||
"csv_files = [f for f in os.listdir(folder_path) if f.startswith(prefix) and f.endswith('.csv')]\n", | ||
"\n", | ||
"figures = []\n", | ||
"\n", | ||
"for csv_file in csv_files:\n", | ||
" # read the gas costs from the file\n", | ||
" file_path = os.path.join(folder_path, csv_file)\n", | ||
" df = pd.read_csv(file_path)\n", | ||
" gas_costs = df['gas_costs'].tolist()\n", | ||
"\n", | ||
" # generate transactions index\n", | ||
" transactions = np.arange(1, len(gas_costs) + 1)\n", | ||
" df['transactions'] = transactions\n", | ||
"\n", | ||
" # calculates the moving average within the defined window\n", | ||
" df['moving_avg'] = df['gas_costs'].rolling(window=window_size).mean()\n", | ||
"\n", | ||
"\n", | ||
" # plot the data\n", | ||
" plt.figure(figsize=(10, 6))\n", | ||
" plt.plot(df['transactions'], df['gas_costs'], label=\"Gas Costs\", color='lightblue', marker='o')\n", | ||
" plt.plot(df['transactions'], df['moving_avg'], label=f\"{window_size}-TX Moving Average\", color='orange', linewidth=2)\n", | ||
"\n", | ||
" # create highlight\n", | ||
" plt.axvline(x=threshold_tx, color='darkred', linestyle='--', label=f'{threshold_tx} transactions')\n", | ||
" plt.fill_between(df['transactions'], df['gas_costs'], where=(df['transactions'] > threshold_tx), color='pink', alpha=0.5)\n", | ||
"\n", | ||
" plt.xlabel('Number of Transactions')\n", | ||
" plt.ylabel('Gas Cost')\n", | ||
" plt.title(f'Gas Costs and {window_size}-Transaction Moving Average for {csv_file}')\n", | ||
" plt.legend()\n", | ||
"\n", | ||
" # saving the plots as images\n", | ||
" output_file = os.path.join(folder_path, f\"{csv_file.replace('.csv', '')}_plot.png\")\n", | ||
" plt.savefig(output_file)\n", | ||
"\n", | ||
" # also store them in the array to display inline\n", | ||
" figures.append(plt.gcf()) \n", | ||
"\n", | ||
"# display all plots\n", | ||
"for fig in figures:\n", | ||
" plt.figure(fig.number)\n", | ||
" plt.show()\n" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.10.15" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters