Skip to content

Commit

Permalink
moved
Browse files Browse the repository at this point in the history
  • Loading branch information
cafriedb committed Aug 16, 2024
1 parent 652b5a0 commit ee57602
Show file tree
Hide file tree
Showing 5 changed files with 1,437 additions and 207 deletions.
Binary file added dev/EA7E5000
Binary file not shown.
165 changes: 53 additions & 112 deletions dev/general_scan.ipynb → dev/archiv/general_scan.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,11 @@
"ei39SSP2=bw.Database('ei_cutoff_3.9_image_SSP2-RCP19_2050 2024-06-27')"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [],
"source": [
"#Get 5 sectors from 5 yaml files and run LCA scores to plot lvl 1 plot. Save each method in a different sheet."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"LCA DATA TO EXCEL WORKFLOW"
"LCA DATA TO EXCEL "
]
},
{
Expand Down Expand Up @@ -234,31 +218,22 @@
},
{
"cell_type": "code",
"execution_count": 122,
"execution_count": 217,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'activities': ['electricity production, at biomass-fired IGCC power plant' (kilowatt hour, CEU, None),\n",
" 'electricity production, at biomass-fired IGCC power plant' (kilowatt hour, BRA, None),\n",
" 'electricity production, at biomass-fired IGCC power plant' (kilowatt hour, USA, None),\n",
" 'electricity production, at biomass-fired IGCC power plant' (kilowatt hour, TUR, None),\n",
" 'electricity production, at biomass-fired IGCC power plant' (kilowatt hour, RUS, None),\n",
" 'electricity production, at biomass-fired IGCC power plant' (kilowatt hour, RSAM, None),\n",
" 'electricity production, at biomass-fired IGCC power plant' (kilowatt hour, SAF, None),\n",
" 'electricity production, at biomass-fired IGCC power plant' (kilowatt hour, CHN, None),\n",
" 'electricity production, at biomass-fired IGCC power plant' (kilowatt hour, KOR, None),\n",
" 'electricity production, at biomass-fired IGCC power plant' (kilowatt hour, UKR, None)]}"
"'electricity production, at biomass-fired IGCC power plant' (kilowatt hour, BRA, None)"
]
},
"execution_count": 122,
"execution_count": 217,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sectors_dict['Electricity']"
"sectors_dict['Electricity']['activities'][1]"
]
},
{
Expand Down Expand Up @@ -822,61 +797,7 @@
]
}
],
"source": [
"import pandas as pd\n",
"import openpyxl\n",
"from openpyxl.chart import (\n",
" ScatterChart,\n",
" Reference,\n",
" Series\n",
")\n",
"\n",
"# Helper function to convert a pandas DataFrame to a list of rows\n",
"def dataframe_to_rows(df, include_index=True, include_column_header=True):\n",
" rows = []\n",
" if include_column_header:\n",
" rows.append([str(c) for c in df.columns])\n",
" for _, row in df.iterrows():\n",
" if include_index:\n",
" rows.append([str(row.name)] + [str(v) for v in row])\n",
" else:\n",
" rows.append([str(v) for v in row])\n",
" return rows\n",
"\n",
"# Assuming you have a dictionary 'my_dict' and a dataframe 'df' in the dictionary\n",
"df = df_list[0]\n",
"\n",
"# Create a new workbook\n",
"wb = openpyxl.Workbook()\n",
"\n",
"# Write the dataframe to the first worksheet\n",
"ws_data = wb.active\n",
"for r in dataframe_to_rows(df, index=False, header=True):\n",
" ws_data.append(r)\n",
"\n",
"# Create a new worksheet for the chart\n",
"ws_chart = wb.create_sheet(\"Scatter Plot\")\n",
"\n",
"# Create the scatter chart\n",
"scatter_chart = ScatterChart()\n",
"scatter_chart.title = \"Scatter Plot of Total Values\"\n",
"scatter_chart.style = 13\n",
"scatter_chart.x_axis.title = \"Index\"\n",
"scatter_chart.y_axis.title = \"Total\"\n",
"\n",
"# Get the data range for the scatter plot\n",
"x_values = Reference(ws_data, min_col=1, min_row=2, max_row=len(df)+1)\n",
"y_values = Reference(ws_data, min_col=df.columns.get_loc('total')+1, min_row=2, max_row=len(df)+1)\n",
"\n",
"# Add the data series to the chart\n",
"scatter_chart.series.append(Series(y_values, x_values))\n",
"\n",
"# Add the chart to the chart worksheet\n",
"ws_chart.add_chart(scatter_chart, \"A1\")\n",
"\n",
"# Save the workbook\n",
"wb.save(\"scatter_plot.xlsx\")"
]
"source": []
},
{
"cell_type": "markdown",
Expand All @@ -896,9 +817,42 @@
},
{
"cell_type": "code",
"execution_count": 195,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Load the Excel file\n",
"wb = openpyxl.load_workbook('output_v2.xlsx')\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 224,
"metadata": {},
"outputs": [
{
"ename": "error",
"evalue": "Error -3 while decompressing data: invalid distance too far back",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31merror\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[224], line 6\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mopenpyxl\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mchart\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m ScatterChart, Series\n\u001b[0;32m 5\u001b[0m \u001b[38;5;66;03m# Load the Excel file\u001b[39;00m\n\u001b[1;32m----> 6\u001b[0m wb \u001b[38;5;241m=\u001b[39m openpyxl\u001b[38;5;241m.\u001b[39mload_workbook(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124moutput_v2.xlsx\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m 8\u001b[0m \u001b[38;5;66;03m# Create a dictionary to store the charts for each sector\u001b[39;00m\n\u001b[0;32m 9\u001b[0m sector_charts \u001b[38;5;241m=\u001b[39m {}\n",
"File \u001b[1;32mc:\\Users\\fried\\miniconda3\\envs\\premise\\Lib\\site-packages\\openpyxl\\reader\\excel.py:348\u001b[0m, in \u001b[0;36mload_workbook\u001b[1;34m(filename, read_only, keep_vba, data_only, keep_links, rich_text)\u001b[0m\n\u001b[0;32m 318\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Open the given filename and return the workbook\u001b[39;00m\n\u001b[0;32m 319\u001b[0m \n\u001b[0;32m 320\u001b[0m \u001b[38;5;124;03m:param filename: the path to open or a file-like object\u001b[39;00m\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 344\u001b[0m \n\u001b[0;32m 345\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m 346\u001b[0m reader \u001b[38;5;241m=\u001b[39m ExcelReader(filename, read_only, keep_vba,\n\u001b[0;32m 347\u001b[0m data_only, keep_links, rich_text)\n\u001b[1;32m--> 348\u001b[0m reader\u001b[38;5;241m.\u001b[39mread()\n\u001b[0;32m 349\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m reader\u001b[38;5;241m.\u001b[39mwb\n",
"File \u001b[1;32mc:\\Users\\fried\\miniconda3\\envs\\premise\\Lib\\site-packages\\openpyxl\\reader\\excel.py:303\u001b[0m, in \u001b[0;36mExcelReader.read\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 301\u001b[0m apply_stylesheet(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39marchive, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mwb)\n\u001b[0;32m 302\u001b[0m action \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mread worksheets\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m--> 303\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mread_worksheets()\n\u001b[0;32m 304\u001b[0m action \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124massign names\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 305\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mparser\u001b[38;5;241m.\u001b[39massign_names()\n",
"File \u001b[1;32mc:\\Users\\fried\\miniconda3\\envs\\premise\\Lib\\site-packages\\openpyxl\\reader\\excel.py:237\u001b[0m, in \u001b[0;36mExcelReader.read_worksheets\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 235\u001b[0m ws\u001b[38;5;241m.\u001b[39m_rels \u001b[38;5;241m=\u001b[39m rels\n\u001b[0;32m 236\u001b[0m ws_parser \u001b[38;5;241m=\u001b[39m WorksheetReader(ws, fh, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mshared_strings, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdata_only, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mrich_text)\n\u001b[1;32m--> 237\u001b[0m ws_parser\u001b[38;5;241m.\u001b[39mbind_all()\n\u001b[0;32m 238\u001b[0m fh\u001b[38;5;241m.\u001b[39mclose()\n\u001b[0;32m 240\u001b[0m \u001b[38;5;66;03m# assign any comments to cells\u001b[39;00m\n",
"File \u001b[1;32mc:\\Users\\fried\\miniconda3\\envs\\premise\\Lib\\site-packages\\openpyxl\\worksheet\\_reader.py:465\u001b[0m, in \u001b[0;36mWorksheetReader.bind_all\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 464\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mbind_all\u001b[39m(\u001b[38;5;28mself\u001b[39m):\n\u001b[1;32m--> 465\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mbind_cells()\n\u001b[0;32m 466\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mbind_merged_cells()\n\u001b[0;32m 467\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mbind_hyperlinks()\n",
"File \u001b[1;32mc:\\Users\\fried\\miniconda3\\envs\\premise\\Lib\\site-packages\\openpyxl\\worksheet\\_reader.py:368\u001b[0m, in \u001b[0;36mWorksheetReader.bind_cells\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 367\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mbind_cells\u001b[39m(\u001b[38;5;28mself\u001b[39m):\n\u001b[1;32m--> 368\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m idx, row \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mparser\u001b[38;5;241m.\u001b[39mparse():\n\u001b[0;32m 369\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m cell \u001b[38;5;129;01min\u001b[39;00m row:\n\u001b[0;32m 370\u001b[0m style \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mws\u001b[38;5;241m.\u001b[39mparent\u001b[38;5;241m.\u001b[39m_cell_styles[cell[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mstyle_id\u001b[39m\u001b[38;5;124m'\u001b[39m]]\n",
"File \u001b[1;32mc:\\Users\\fried\\miniconda3\\envs\\premise\\Lib\\site-packages\\openpyxl\\worksheet\\_reader.py:156\u001b[0m, in \u001b[0;36mWorkSheetParser.parse\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 137\u001b[0m properties \u001b[38;5;241m=\u001b[39m {\n\u001b[0;32m 138\u001b[0m PRINT_TAG: (\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mprint_options\u001b[39m\u001b[38;5;124m'\u001b[39m, PrintOptions),\n\u001b[0;32m 139\u001b[0m MARGINS_TAG: (\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mpage_margins\u001b[39m\u001b[38;5;124m'\u001b[39m, PageMargins),\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 151\u001b[0m \n\u001b[0;32m 152\u001b[0m }\n\u001b[0;32m 154\u001b[0m it \u001b[38;5;241m=\u001b[39m iterparse(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msource) \u001b[38;5;66;03m# add a finaliser to close the source when this becomes possible\u001b[39;00m\n\u001b[1;32m--> 156\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m _, element \u001b[38;5;129;01min\u001b[39;00m it:\n\u001b[0;32m 157\u001b[0m tag_name \u001b[38;5;241m=\u001b[39m element\u001b[38;5;241m.\u001b[39mtag\n\u001b[0;32m 158\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m tag_name \u001b[38;5;129;01min\u001b[39;00m dispatcher:\n",
"File \u001b[1;32mc:\\Users\\fried\\miniconda3\\envs\\premise\\Lib\\xml\\etree\\ElementTree.py:1253\u001b[0m, in \u001b[0;36miterparse.<locals>.iterator\u001b[1;34m(source)\u001b[0m\n\u001b[0;32m 1251\u001b[0m \u001b[38;5;28;01myield from\u001b[39;00m pullparser\u001b[38;5;241m.\u001b[39mread_events()\n\u001b[0;32m 1252\u001b[0m \u001b[38;5;66;03m# load event buffer\u001b[39;00m\n\u001b[1;32m-> 1253\u001b[0m data \u001b[38;5;241m=\u001b[39m source\u001b[38;5;241m.\u001b[39mread(\u001b[38;5;241m16\u001b[39m \u001b[38;5;241m*\u001b[39m \u001b[38;5;241m1024\u001b[39m)\n\u001b[0;32m 1254\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m data:\n\u001b[0;32m 1255\u001b[0m \u001b[38;5;28;01mbreak\u001b[39;00m\n",
"File \u001b[1;32mc:\\Users\\fried\\miniconda3\\envs\\premise\\Lib\\zipfile.py:965\u001b[0m, in \u001b[0;36mZipExtFile.read\u001b[1;34m(self, n)\u001b[0m\n\u001b[0;32m 963\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_offset \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m\n\u001b[0;32m 964\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m n \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m0\u001b[39m \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_eof:\n\u001b[1;32m--> 965\u001b[0m data \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_read1(n)\n\u001b[0;32m 966\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m n \u001b[38;5;241m<\u001b[39m \u001b[38;5;28mlen\u001b[39m(data):\n\u001b[0;32m 967\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_readbuffer \u001b[38;5;241m=\u001b[39m data\n",
"File \u001b[1;32mc:\\Users\\fried\\miniconda3\\envs\\premise\\Lib\\zipfile.py:1041\u001b[0m, in \u001b[0;36mZipExtFile._read1\u001b[1;34m(self, n)\u001b[0m\n\u001b[0;32m 1039\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_compress_type \u001b[38;5;241m==\u001b[39m ZIP_DEFLATED:\n\u001b[0;32m 1040\u001b[0m n \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mmax\u001b[39m(n, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mMIN_READ_SIZE)\n\u001b[1;32m-> 1041\u001b[0m data \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_decompressor\u001b[38;5;241m.\u001b[39mdecompress(data, n)\n\u001b[0;32m 1042\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_eof \u001b[38;5;241m=\u001b[39m (\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_decompressor\u001b[38;5;241m.\u001b[39meof \u001b[38;5;129;01mor\u001b[39;00m\n\u001b[0;32m 1043\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_compress_left \u001b[38;5;241m<\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m \u001b[38;5;129;01mand\u001b[39;00m\n\u001b[0;32m 1044\u001b[0m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_decompressor\u001b[38;5;241m.\u001b[39munconsumed_tail)\n\u001b[0;32m 1045\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_eof:\n",
"\u001b[1;31merror\u001b[0m: Error -3 while decompressing data: invalid distance too far back"
]
}
],
"source": [
"import openpyxl\n",
"from openpyxl.chart import ScatterChart, Reference, Series\n",
Expand All @@ -910,35 +864,21 @@
"sector_charts = {}\n",
"\n",
"# Loop through each worksheet\n",
"for worksheet_name in wb.sheetnames:\n",
" # Extract the sector name from the worksheet name\n",
" sector_name = worksheet_name.split('_')[0]\n",
"\n",
" # Get the worksheet\n",
" ws = wb[worksheet_name]\n",
"\n",
" # Determine the number of rows in the worksheet\n",
" row_count = ws.max_row\n",
"for ws_name in wb.sheetnames:\n",
" ws = wb[ws_name]\n",
"\n",
" # Find the column index for the 'total' column (assuming this is the column you want to plot)\n",
" for col_num in range(1, ws.max_column + 1):\n",
" if ws.cell(row=1, column=col_num).value == 'total':\n",
" y_col = col_num\n",
" break\n",
" else:\n",
" # If the 'total' column is not found, skip this worksheet\n",
" continue\n",
" #read the worksheet into a DataFrame\n",
" df = pd.read_excel(wb, sheet_name = ws_name)\n",
"\n",
" # Create the scatter chart\n",
" chart = ScatterChart()\n",
" chart.title = f\"LCA Scores - {worksheet_name}\"\n",
" chart.y_axis.title = str(ws['B1'].value) # Assuming the method is in cell B1\n",
" chart.title = f\"LCA Scores\"\n",
" chart.y_axis.title = df[df.columns['method unit']]\n",
" chart.x_axis.title = 'activities indexes'\n",
"\n",
" # Define the data range for the chart\n",
" y_values = Reference(ws, min_col=y_col, min_row=2, max_row=row_count) # Assuming the data starts in column 2\n",
" x_values = Reference(ws, min_col=1, min_row=2, max_row=row_count)\n",
"\n",
" y_values = df[df.columns['total']] # Assuming the data starts in column 2\n",
" x_values = df[df.index]\n",
" # Create a series and add it to the chart\n",
" series = Series(y_values, x_values, title_from_data=False)\n",
" chart.series.append(series)\n",
Expand All @@ -948,12 +888,13 @@
" series.marker.size = 5\n",
" series.graphicalProperties.line.noFill = True\n",
"\n",
" # Add the chart to the sector-specific worksheet\n",
" if sector_name not in sector_charts:\n",
" sector_charts[sector_name] = wb.create_sheet(f\"Plot - {sector_name}\")\n",
" sector_charts[sector_name].add_chart(chart, f\"A{len(sector_charts[sector_name]['A'])+1}\")\n",
" # Add the chart to the dictionary\n",
" sector_charts[ws_name] = chart\n",
"\n",
"# Save the updated workbook\n",
"for ws_name, chart in sector_charts.items():\n",
" ws = wb[ws_name]\n",
" ws.add_chart(chart, \"A1\")\n",
"wb.save('output_v2.xlsx')"
]
},
Expand Down
Loading

0 comments on commit ee57602

Please sign in to comment.