diff --git a/dev/EA7E5000 b/dev/EA7E5000 new file mode 100644 index 0000000..64dd0c4 Binary files /dev/null and b/dev/EA7E5000 differ diff --git a/dev/general_scan.ipynb b/dev/archiv/general_scan.ipynb similarity index 77% rename from dev/general_scan.ipynb rename to dev/archiv/general_scan.ipynb index c392aad..43e4b61 100644 --- a/dev/general_scan.ipynb +++ b/dev/archiv/general_scan.ipynb @@ -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 " ] }, { @@ -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]" ] }, { @@ -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", @@ -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..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", @@ -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", @@ -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')" ] }, diff --git a/dev/general_scan_v2.ipynb b/dev/archiv/general_scan_v2.ipynb similarity index 61% rename from dev/general_scan_v2.ipynb rename to dev/archiv/general_scan_v2.ipynb index 6ebca80..2c7f2a3 100644 --- a/dev/general_scan_v2.ipynb +++ b/dev/archiv/general_scan_v2.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 14, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -22,7 +22,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -51,7 +51,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -66,7 +66,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -77,7 +77,7 @@ " 'yaml identifier': 'Electricity'}}" ] }, - "execution_count": 17, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -93,7 +93,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -118,7 +118,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -127,7 +127,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -136,7 +136,7 @@ "bw2data.backends.peewee.proxies.Activity" ] }, - "execution_count": 20, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -147,7 +147,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -156,7 +156,7 @@ "'electricity production, at biomass-fired IGCC power plant'" ] }, - "execution_count": 21, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -167,7 +167,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -176,7 +176,7 @@ "'electricity production, at biomass-fired IGCC power plant'" ] }, - "execution_count": 22, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -187,7 +187,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 10, "metadata": {}, "outputs": [ { @@ -207,7 +207,7 @@ " 'unit': 'megajoule'}}" ] }, - "execution_count": 23, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } @@ -225,7 +225,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ @@ -265,7 +265,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -288,7 +288,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 54, "metadata": {}, "outputs": [ { @@ -297,7 +297,7 @@ "dict_keys(['Cement_global_warming_potential_(gwp100)', 'Cement_cumulative_energy_demand_-_non-renewable_energy_resources'])" ] }, - "execution_count": 23, + "execution_count": 54, "metadata": {}, "output_type": "execute_result" } @@ -308,7 +308,27 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 55, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_keys(['Electricity_global_warming_potential_(gwp100)', 'Electricity_cumulative_energy_demand_-_non-renewable_energy_resources'])" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "scores_dict['Electricity']['lca_scores'].keys()" + ] + }, + { + "cell_type": "code", + "execution_count": null, "metadata": {}, "outputs": [ { @@ -328,24 +348,30 @@ }, { "cell_type": "code", - "execution_count": 41, + "execution_count": 19, "metadata": {}, "outputs": [], "source": [ - "# Function to add statistics to DataFrame\n", "def add_statistics(df, column_name='total'):\n", " # Calculate mean, standard deviation, and IQR\n", " df['mean'] = df[column_name].mean()\n", - " df['2std_abv'] = df['mean']+df[column_name].std()*2\n", - " df['2std_blw'] = df['mean']-df[column_name].std()*2\n", + " df['2std_abv'] = df['mean'] + df[column_name].std() * 2\n", + " df['2std_blw'] = df['mean'] - df[column_name].std() * 2\n", " df['q1'] = df[column_name].quantile(0.25)\n", " df['q3'] = df[column_name].quantile(0.75)\n", - " return df" + " \n", + " # Reorder the columns to place the new columns after 'total'\n", + " cols = df.columns.tolist()\n", + " total_index = cols.index(column_name) + 1\n", + " new_cols = ['mean', '2std_abv', '2std_blw', 'q1', 'q3']\n", + " cols = cols[:total_index] + new_cols + cols[total_index:-len(new_cols)]\n", + " \n", + " return df[cols]\n" ] }, { "cell_type": "code", - "execution_count": 52, + "execution_count": 43, "metadata": {}, "outputs": [], "source": [ @@ -374,11 +400,11 @@ }, { "cell_type": "code", - "execution_count": 53, + "execution_count": 51, "metadata": {}, "outputs": [], "source": [ - "lca_scores_in_excel=sector_lca_scores_to_excel(scores_dict, 'output_v7.xlsx')" + "lca_scores_in_excel=sector_lca_scores_to_excel(scores_dict, 'output_v2_2.xlsx')" ] }, { @@ -388,6 +414,78 @@ "PLOTS IN EXCEL" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Input : one excel file which holds the sector workesheets\n", + "# create a dictionary of worksheet names which fullfill the criteria.\n", + "# iterate through the list to get the worksheets\n", + "# Grouping sector graphs in one worksheet - for worksheets with same sector create a chart each in a worksheet called sector_charts\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [], + "source": [ + "from openpyxl import load_workbook\n", + "def categorize_sheets_by_sector(file_path):\n", + " # Load the workbook\n", + " workbook = load_workbook(filename=file_path, read_only=True)\n", + " \n", + " # Initialize a dictionary to hold sectors and their corresponding sheet names\n", + " worksheet_dict = {}\n", + " \n", + " # Iterate over all sheet names in the workbook\n", + " for sheet_name in workbook.sheetnames:\n", + " # Split the sheet name to extract the sector (assumes sector is the first part)\n", + " sector = sheet_name.split('_')[0]\n", + " \n", + " # Add the sheet name to the corresponding sector in the dictionary\n", + " if sector in worksheet_dict:\n", + " worksheet_dict[sector].append(sheet_name)\n", + " else:\n", + " worksheet_dict[sector] = [sheet_name]\n", + " \n", + " return worksheet_dict" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Cement': ['Cement_Cement_global_warming_po',\n", + " 'Cement_Cement_cumulative_energy'],\n", + " 'Electricity': ['Electricity_Electricity_global_',\n", + " 'Electricity_Electricity_cumulat'],\n", + " 'Charts': ['Charts_cement']}" + ] + }, + "execution_count": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "categorize_sheets_by_sector('output_v2_2.xlsx')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -397,7 +495,7 @@ }, { "cell_type": "code", - "execution_count": 91, + "execution_count": 52, "metadata": {}, "outputs": [], "source": [ @@ -405,11 +503,16 @@ "from openpyxl import load_workbook\n", "from openpyxl.drawing.text import CharacterProperties\n", "\n", - "wb = load_workbook('output_v7.xlsx')\n", + "wb = load_workbook('output_v2_2.xlsx')\n", "ws = wb['Cement_Cement_global_warming_po']\n", - "ws_charts = wb.create_sheet('Charts')\n", + "ws_charts = wb.create_sheet('Charts_cement')\n", "\n", "chart = ScatterChart() # thats the name of the chart, we could also give it a different one\n", + "location_chart=[\"B3\", \"M3\", \"X3\",\n", + " \"B25\", \"M25\", \"X25\",\n", + " \"B46\", \"M46\", \"X46\" \n", + " ]\n", + "#add max_row= len(df)... smthn like this\n", "\n", "# Chart titles\n", "chart.title = 'LCA scores of portland cement production'\n", @@ -452,7 +555,7 @@ "# ADJUST Y-AXIS\n", "\n", "# Calculate the min and max y-values to determine the axis range\n", - "y_values_list = [ws.cell(row=row, column=8).value for row in range(2, 9)]\n", + "y_values_list = [ws.cell(row=row, column=8).value for row in range(2,8)] #number of rows need to be defined based on dataframe\n", "min_y_value = min(y_values_list)\n", "max_y_value = max(y_values_list)\n", "\n", @@ -474,7 +577,147 @@ "# ---------\n", "# MEAN\n", "# Add a new series to the chart for the mean line\n", - "mean_y = Reference(ws, min_col=14, min_row=1, max_row=8)\n", + "mean_y = Reference(ws, min_col=9, min_row=1, max_row=8)\n", + "mean_series = Series(mean_y, x_values, title_from_data=\"True\")\n", + "chart.series.append(mean_series)\n", + "\n", + "# Customize the mean series to show as a line\n", + "mean_series.marker.symbol = \"none\" # No markers, just a line\n", + "mean_series.graphicalProperties.line.solidFill = \"FF0000\" # Red line for mean value\n", + "mean_series.graphicalProperties.line.width = 10000 # Set line width (default units are EMUs)\n", + "\n", + "# IQR\n", + "# Add a new series to the chart for the iqr lines\n", + "iqr1= Reference(ws, min_col=12, min_row=1, max_row=8)\n", + "iqr3= Reference(ws, min_col=13, min_row=1, max_row=8)\n", + "iqr1_series = Series(iqr1, x_values, title_from_data=\"True\")\n", + "iqr3_series = Series(iqr3, x_values, title_from_data=\"True\")\n", + "chart.series.append(iqr1_series)\n", + "chart.series.append(iqr3_series)\n", + "\n", + "# Customize the iqr 1,3 series to show as a line\n", + "iqr1_series.marker.symbol = \"none\" # No markers, just a line\n", + "iqr3_series.marker.symbol = \"none\"\n", + "iqr1_series.graphicalProperties.line.solidFill = \"6082B6\" # Blue line \n", + "iqr3_series.graphicalProperties.line.solidFill = \"6082B6\" \n", + "iqr1_series.graphicalProperties.line.width = 10000 # Set line width (default units are EMUs)\n", + "iqr3_series.graphicalProperties.line.width = 10000 # Set line width (default units are EMUs)\n", + "\n", + "# STD\n", + "# Add a new series to the chart for the std lines\n", + "std_abv= Reference(ws, min_col=10, min_row=1, max_row=8)\n", + "std_blw= Reference(ws, min_col=11, min_row=1, max_row=8)\n", + "std_abv_series = Series(std_abv, x_values, title_from_data=\"True\")\n", + "std_blw_series = Series(std_blw, x_values, title_from_data=\"True\")\n", + "chart.series.append(std_abv_series)\n", + "chart.series.append(std_blw_series)\n", + "\n", + "# Customize the iqr 1,3 series to show as a line\n", + "std_abv_series.marker.symbol = \"none\" # No markers, just a line\n", + "std_blw_series.marker.symbol = \"none\"\n", + "std_abv_series.graphicalProperties.line.solidFill = \"FFC300\" # yellow line\n", + "std_blw_series.graphicalProperties.line.solidFill = \"FFC300\" \n", + "std_abv_series.graphicalProperties.line.width = 10000 # Set line width (default units are EMUs)\n", + "std_blw_series.graphicalProperties.line.width = 10000 # Set line width (default units are EMUs)\n", + "\n", + "# Set legend position to the right of the plot area\n", + "chart.legend.position = 'r' # 'r' for right\n", + "chart.legend.overlay= False\n", + "\n", + "# Adjust chart dimensions and position to make space for the legend\n", + "chart.width = 18 # Example width, adjust as needed\n", + "chart.height = 10 # Example height, adjust as needed\n", + "\n", + "ws_charts.add_chart(chart, location_chart) #wher top left corner begins\n", + "wb.save('output_v2_2.xlsx')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [], + "source": [ + "from openpyxl.chart import ScatterChart, Reference, Series\n", + "from openpyxl import load_workbook\n", + "from openpyxl.drawing.text import CharacterProperties\n", + "\n", + "wb = load_workbook('output_v2_2.xlsx')\n", + "ws = wb['Cement_Cement_cumulative_energy']\n", + "\n", + "#add clause... if sheet already existst... add to sheet. If not create as new sheet.\n", + "ws_charts = wb['Charts_cement']\n", + "\n", + "chart = ScatterChart() # thats the name of the chart, we could also give it a different one\n", + "\n", + "# Chart titles\n", + "chart.title = 'LCA scores of portland cement production'\n", + "chart.x_axis.title = 'activities'\n", + "chart.y_axis.title = 'method unit'\n", + "# Avoid overlap\n", + "chart.title.overlay = False\n", + "chart.x_axis.title.overlay = False\n", + "chart.y_axis.title.overlay = False \n", + "\n", + "\n", + "# Define the data range for the chart\n", + "y_values = Reference(ws, min_col=8, min_row=1, max_row=8)\n", + "x_values = Reference(ws, min_col=4, min_row=2, max_row=8)\n", + "\n", + "# Create a series and add it to the chart\n", + "series = Series(y_values, x_values, title_from_data=True)\n", + "chart.series.append(series)\n", + "chart.style = 9\n", + "\n", + "# Customize the series to show only markers (dots)\n", + "series.marker.symbol = \"circle\"\n", + "series.marker.size = 5\n", + "series.graphicalProperties.line.noFill = True\n", + "\n", + "# ADJUST X-AXIS\n", + "\n", + "# # ADJUST X-AXIS\n", + "chart.x_axis.tickLblPos = \"low\"\n", + "chart.x_axis.majorGridlines = None\n", + "chart.x_axis.tickMarkSkip = 0 # Show all tick marks\n", + "chart.x_axis.tickLblSkip = 0 # Show all labels\n", + "\n", + "# Set x-axis to be a value axis instead of a category axis\n", + "chart.x_axis.scaling.orientation = \"minMax\"\n", + "chart.x_axis.crosses = \"autoZero\"\n", + "chart.x_axis.axPos = \"b\"\n", + "chart.x_axis.delete = False\n", + "\n", + "# ADJUST Y-AXIS\n", + "\n", + "# Calculate the min and max y-values to determine the axis range\n", + "y_values_list = [ws.cell(row=row, column=8).value for row in range(2, 9)]\n", + "min_y_value = min(y_values_list)\n", + "max_y_value = max(y_values_list)\n", + "\n", + "# Set y-axis range with some padding\n", + "padding = (max_y_value - min_y_value) * 0.1 # 10% padding on each side\n", + "chart.y_axis.scaling.min = min_y_value - padding\n", + "chart.y_axis.scaling.max = max_y_value + padding\n", + "\n", + "# Explicitly set tick labels to be visible\n", + "chart.y_axis.tickLblPos = \"nextTo\" # Position the labels next to the tick marks\n", + "#chart.y_axis.majorGridlines = True # Ensure major gridlines are enabled (though you mentioned they're visible)\n", + "chart.y_axis.delete = False # Ensure axis is not deleted\n", + "# Format the y-axis to show 2 decimal places\n", + "chart.y_axis.number_format = '0.00'\n", + "\n", + "# ---------\n", + "# ADD STATS\n", + "# ---------\n", + "# MEAN\n", + "# Add a new series to the chart for the mean line\n", + "mean_y = Reference(ws, min_col=9, min_row=1, max_row=8)\n", "mean_series = Series(mean_y, x_values, title_from_data=\"True\")\n", "chart.series.append(mean_series)\n", "\n", @@ -485,8 +728,8 @@ "\n", "# IQR\n", "# Add a new series to the chart for the iqr lines\n", - "iqr1= Reference(ws, min_col=17, min_row=1, max_row=8)\n", - "iqr3= Reference(ws, min_col=18, min_row=1, max_row=8)\n", + "iqr1= Reference(ws, min_col=12, min_row=1, max_row=8)\n", + "iqr3= Reference(ws, min_col=13, min_row=1, max_row=8)\n", "iqr1_series = Series(iqr1, x_values, title_from_data=\"True\")\n", "iqr3_series = Series(iqr3, x_values, title_from_data=\"True\")\n", "chart.series.append(iqr1_series)\n", @@ -502,8 +745,8 @@ "\n", "# STD\n", "# Add a new series to the chart for the std lines\n", - "std_abv= Reference(ws, min_col=15, min_row=1, max_row=8)\n", - "std_blw= Reference(ws, min_col=16, min_row=1, max_row=8)\n", + "std_abv= Reference(ws, min_col=10, min_row=1, max_row=8)\n", + "std_blw= Reference(ws, min_col=11, min_row=1, max_row=8)\n", "std_abv_series = Series(std_abv, x_values, title_from_data=\"True\")\n", "std_blw_series = Series(std_blw, x_values, title_from_data=\"True\")\n", "chart.series.append(std_abv_series)\n", @@ -525,8 +768,8 @@ "chart.width = 18 # Example width, adjust as needed\n", "chart.height = 10 # Example height, adjust as needed\n", "\n", - "ws_charts.add_chart(chart, \"D5\") #wher top left corner begins\n", - "wb.save('output_v7.xlsx')" + "ws_charts.add_chart(chart, \"M3\") #wher top left corner begins\n", + "wb.save('output_v2_2.xlsx')" ] } ], diff --git a/dev/general_scan_v3.ipynb b/dev/archiv/general_scan_v3.ipynb similarity index 86% rename from dev/general_scan_v3.ipynb rename to dev/archiv/general_scan_v3.ipynb index e7f67b9..5fa7f00 100644 --- a/dev/general_scan_v3.ipynb +++ b/dev/archiv/general_scan_v3.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 59, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -22,7 +22,7 @@ }, { "cell_type": "code", - "execution_count": 60, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -51,7 +51,7 @@ }, { "cell_type": "code", - "execution_count": 61, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -66,7 +66,7 @@ }, { "cell_type": "code", - "execution_count": 62, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -77,7 +77,7 @@ " 'yaml identifier': 'Electricity'}}" ] }, - "execution_count": 62, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -93,7 +93,7 @@ }, { "cell_type": "code", - "execution_count": 63, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -118,7 +118,7 @@ }, { "cell_type": "code", - "execution_count": 64, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -127,34 +127,67 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "bw2data.backends.peewee.proxies.Activity" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "type(sectors_dict['Electricity']['activities'][0])" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'electricity production, at biomass-fired IGCC power plant'" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "sectors_dict['Electricity']['activities'][0].as_dict()['name']" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'electricity production, at biomass-fired IGCC power plant'" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "sectors_dict['Electricity']['activities'][4].as_dict()['name']" ] }, { "cell_type": "code", - "execution_count": 65, + "execution_count": 10, "metadata": {}, "outputs": [ { @@ -174,7 +207,7 @@ " 'unit': 'megajoule'}}" ] }, - "execution_count": 65, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } @@ -192,7 +225,7 @@ }, { "cell_type": "code", - "execution_count": 66, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ @@ -232,7 +265,7 @@ }, { "cell_type": "code", - "execution_count": 67, + "execution_count": 12, "metadata": {}, "outputs": [ { @@ -255,41 +288,74 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "dict_keys(['Cement_global_warming_potential_(gwp100)', 'Cement_cumulative_energy_demand_-_non-renewable_energy_resources'])" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "scores_dict['Cement']['lca_scores'].keys()" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "dict_keys(['Electricity_global_warming_potential_(gwp100)', 'Electricity_cumulative_energy_demand_-_non-renewable_energy_resources'])" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "scores_dict['Electricity']['lca_scores'].keys()" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "dict_keys(['yaml', 'yaml identifier', 'activities', 'lca_scores'])" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "scores_dict['Cement'].keys()" ] }, { "cell_type": "code", - "execution_count": 77, + "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "def add_statistics(df, column_name='total'):\n", "\n", " #Need a rank row to plot the total LCA scores in descending order (satter opepyxl function takes in non categorial values)\n", - " df['rank'] = df[column_name].rank(method='max') \n", + " df['rank'] = df[column_name].rank(method=\"first\", ascending=\"False\")\n", " # Calculate mean, standard deviation, and IQR\n", " df['mean'] = df[column_name].mean()\n", " df['2std_abv'] = df['mean'] + df[column_name].std() * 2\n", @@ -308,7 +374,7 @@ }, { "cell_type": "code", - "execution_count": 69, + "execution_count": 17, "metadata": {}, "outputs": [], "source": [ @@ -337,11 +403,11 @@ }, { "cell_type": "code", - "execution_count": 141, + "execution_count": 26, "metadata": {}, "outputs": [], "source": [ - "lca_scores_in_excel=sector_lca_scores_to_excel(scores_dict, 'output_v20_2.xlsx')" + "lca_scores_in_excel=sector_lca_scores_to_excel(scores_dict, 'output_v26_2.xlsx')" ] }, { @@ -366,7 +432,7 @@ }, { "cell_type": "code", - "execution_count": 102, + "execution_count": 19, "metadata": {}, "outputs": [], "source": [ @@ -395,11 +461,11 @@ }, { "cell_type": "code", - "execution_count": 142, + "execution_count": 27, "metadata": {}, "outputs": [], "source": [ - "worksheet_dict=categorize_sheets_by_sector('output_v20_2.xlsx')" + "worksheet_dict=categorize_sheets_by_sector('output_v26_2.xlsx')" ] }, { @@ -411,7 +477,7 @@ }, { "cell_type": "code", - "execution_count": 143, + "execution_count": 24, "metadata": {}, "outputs": [], "source": [ @@ -430,7 +496,7 @@ " for sector, worksheet_names in worksheet_dict.items():\n", " \n", " # Create or get the chart sheet for the current sector\n", - " chart_sheet_name = f\"{sector}_charts\"\n", + " chart_sheet_name = f\"{sector}_dot_charts\"\n", " if chart_sheet_name in wb.sheetnames:\n", " ws_charts = wb[chart_sheet_name]\n", " else:\n", @@ -444,9 +510,21 @@ " for worksheet_name in worksheet_names:\n", " ws = wb[worksheet_name]\n", "\n", - " # Find max_row and max_column\n", + " # Find min_row, max_row and max_column\n", " max_row = ws.max_row\n", + " print(max_row)\n", " max_column = ws.max_column\n", + " min_row=1\n", + "\n", + " #define columns\n", + " total_col=8\n", + " rank_col=9\n", + " index_col=1\n", + " mean_col=10\n", + " std_adv_col=11\n", + " std_blw_col=12\n", + " q1_col=13\n", + " q3_col=14\n", " \n", " # Create a ScatterChart (or other chart type as needed)\n", " chart = ScatterChart()\n", @@ -454,7 +532,7 @@ " # Chart titles\n", " chart.title = f\"{ws['F2'].value} LCA scores for {sector} sector\"\n", " chart.y_axis.title = f\"{ws['G2'].value}\"\n", - " chart.x_axis.title = 'activity index'\n", + " chart.x_axis.title = 'activity rank'\n", " # Avoid overlap\n", " chart.title.overlay = False\n", " chart.x_axis.title.overlay = False\n", @@ -462,8 +540,10 @@ "\n", "\n", " # Define the data range for the chart\n", - " y_values = Reference(ws, min_col=8, min_row=1, max_row=max_row)\n", - " x_values = Reference(ws, min_col=9, min_row=1, max_row=max_row)\n", + " y_values = Reference(ws, min_col=total_col, min_row=min_row, max_row=max_row)\n", + " print(y_values)\n", + " x_values = Reference(ws, min_col=rank_col, min_row=min_row, max_row=max_row)\n", + " print(x_values)\n", "\n", " # Create a series and add it to the chart\n", " series = Series(y_values, x_values, title_from_data=True)\n", @@ -482,7 +562,6 @@ " chart.x_axis.tickMarkSkip = 0 # Show all tick marks\n", " chart.x_axis.tickLblSkip = 0 # Show all labels\n", "\n", - " # Set x-axis to be a value axis instead of a category axis\n", " chart.x_axis.scaling.orientation = \"minMax\"\n", " chart.x_axis.crosses = \"autoZero\"\n", " chart.x_axis.axPos = \"b\"\n", @@ -491,7 +570,7 @@ " # ADJUST Y-AXIS\n", "\n", " # Calculate the min and max y-values to determine the axis range\n", - " y_values_list = [ws.cell(row=row, column=8).value for row in range(2,8)] #number of rows need to be defined based on dataframe\n", + " y_values_list = [ws.cell(row=row, column=total_col).value for row in range(2,max_row+1)] #number of rows need to be defined based on dataframe\n", " min_y_value = min(y_values_list)-min(y_values_list)*0.1\n", " max_y_value = max(y_values_list)+ max(y_values_list)*0.1\n", "\n", @@ -505,7 +584,7 @@ " #chart.y_axis.majorGridlines = True # Ensure major gridlines are enabled (though you mentioned they're visible)\n", " chart.y_axis.delete = False # Ensure axis is not deleted\n", " # Format the y-axis to show 2 decimal places\n", - " chart.y_axis.number_format = '0.00'\n", + " chart.y_axis.number_format = '0.000'\n", "\n", "\n", " # ---------\n", @@ -513,7 +592,7 @@ " # ---------\n", " # MEAN\n", " # Add a new series to the chart for the mean line\n", - " mean_y = Reference(ws, min_col=10, min_row=1, max_row=max_row)\n", + " mean_y = Reference(ws, min_col=mean_col, min_row=min_row, max_row=max_row)\n", " mean_series = Series(mean_y, x_values, title_from_data=\"True\")\n", " chart.series.append(mean_series)\n", "\n", @@ -524,8 +603,8 @@ "\n", " # IQR\n", " # Add a new series to the chart for the iqr lines\n", - " iqr1= Reference(ws, min_col=13, min_row=1, max_row=max_row)\n", - " iqr3= Reference(ws, min_col=14, min_row=1, max_row=max_row)\n", + " iqr1= Reference(ws, min_col=q1_col, min_row=min_row, max_row=max_row)\n", + " iqr3= Reference(ws, min_col=q3_col, min_row=min_row, max_row=max_row)\n", " iqr1_series = Series(iqr1, x_values, title_from_data=\"True\")\n", " iqr3_series = Series(iqr3, x_values, title_from_data=\"True\")\n", " chart.series.append(iqr1_series)\n", @@ -541,8 +620,8 @@ "\n", " # STD\n", " # Add a new series to the chart for the std lines\n", - " std_abv= Reference(ws, min_col=11, min_row=1, max_row=max_row)\n", - " std_blw= Reference(ws, min_col=12, min_row=1, max_row=max_row)\n", + " std_abv= Reference(ws, min_col=std_adv_col, min_row=min_row, max_row=max_row)\n", + " std_blw= Reference(ws, min_col=std_blw_col, min_row=min_row, max_row=max_row)\n", " std_abv_series = Series(std_abv, x_values, title_from_data=\"True\")\n", " std_blw_series = Series(std_blw, x_values, title_from_data=\"True\")\n", " chart.series.append(std_abv_series)\n", @@ -577,18 +656,46 @@ }, { "cell_type": "code", - "execution_count": 144, + "execution_count": 28, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "8\n", + "'Cement_Cement_global_warming_po'!$H$1:$H$8\n", + "'Cement_Cement_global_warming_po'!$I$1:$I$8\n", + "8\n", + "'Cement_Cement_cumulative_energy'!$H$1:$H$8\n", + "'Cement_Cement_cumulative_energy'!$I$1:$I$8\n", + "11\n", + "'Electricity_Electricity_global_'!$H$1:$H$11\n", + "'Electricity_Electricity_global_'!$I$1:$I$11\n", + "11\n", + "'Electricity_Electricity_cumulat'!$H$1:$H$11\n", + "'Electricity_Electricity_cumulat'!$I$1:$I$11\n" + ] + } + ], "source": [ - "plot('output_v20_2.xlsx', worksheet_dict=worksheet_dict)" + "plot('output_v26_2.xlsx', worksheet_dict=worksheet_dict)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "IndentationError", + "evalue": "expected an indented block after 'for' statement on line 5 (571549965.py, line 7)", + "output_type": "error", + "traceback": [ + "\u001b[1;36m Cell \u001b[1;32mIn[21], line 7\u001b[1;36m\u001b[0m\n\u001b[1;33m def plot1 (filepath_workbook):\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m expected an indented block after 'for' statement on line 5\n" + ] + } + ], "source": [ "from openpyxl.chart import ScatterChart, Reference, Series\n", "from openpyxl import load_workbook\n", @@ -598,7 +705,7 @@ "\n", "def plot1 (filepath_workbook):\n", " \n", - "wb = load_workbook('output_v2_2.xlsx')\n", + "wb = load_workbook('output_v25_2.xlsx')\n", "\n", "\n", "ws = wb['Cement_Cement_global_warming_po']\n", @@ -739,7 +846,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": {}, "outputs": [], "source": [ @@ -747,11 +854,13 @@ "from openpyxl import load_workbook\n", "from openpyxl.drawing.text import CharacterProperties\n", "\n", - "wb = load_workbook('output_v2_2.xlsx')\n", - "ws = wb['Cement_Cement_cumulative_energy']\n", + "wb = load_workbook('output_v25_2.xlsx')\n", + "ws_charts = wb.create_sheet('Charts_cement')\n", + "#ws = wb['Cement_Cement_cumulative_energy']\n", "\n", "#add clause... if sheet already existst... add to sheet. If not create as new sheet.\n", - "ws_charts = wb['Charts_cement']\n", + "#ws_charts = wb['Charts_cement']\n", + "ws_charts = wb.create_sheet('Charts_cement')\n", "\n", "chart = ScatterChart() # thats the name of the chart, we could also give it a different one\n", "\n", @@ -869,7 +978,7 @@ "chart.height = 10 # Example height, adjust as needed\n", "\n", "ws_charts.add_chart(chart, \"M3\") #wher top left corner begins\n", - "wb.save('output_v2_2.xlsx')" + "wb.save('output_v25_2.xlsx')" ] } ], diff --git a/dev/archiv/general_scan_v4.ipynb b/dev/archiv/general_scan_v4.ipynb new file mode 100644 index 0000000..fc9969f --- /dev/null +++ b/dev/archiv/general_scan_v4.ipynb @@ -0,0 +1,937 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 175, + "metadata": {}, + "outputs": [], + "source": [ + "from functions_v2 import*\n", + "from methods import MethodFinder\n", + "\n", + "import brightway2 as bw\n", + "import bw2data as bd\n", + "import bw2analyzer as ba\n", + "\n", + "#reduce?\n", + "import ast\n", + "import pandas as pd\n", + "import matplotlib.pyplot as plt\n", + "import seaborn as sns" + ] + }, + { + "cell_type": "code", + "execution_count": 176, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Biosphere database already present!!! No setup is needed\n" + ] + } + ], + "source": [ + "bd.projects.set_current(\"premise-validation-try1\")\n", + "bw.bw2setup()\n", + "\n", + "bio3=bw.Database('biosphere3')\n", + "ei39=bw.Database('ecoinvent 3.9.1 cutoff')\n", + "ei39SSP2=bw.Database('ei_cutoff_3.9_image_SSP2-RCP19_2050 2024-06-27')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "LCA DATA TO EXCEL " + ] + }, + { + "cell_type": "code", + "execution_count": 177, + "metadata": {}, + "outputs": [], + "source": [ + "#sector filters file names/paths\n", + "\n", + "cement = 'cement_small.yaml'\n", + "electricity = 'electricity_small.yaml'\n", + "fuels= 'fuels_small.yaml'\n", + "steel = 'steel_small.yaml'\n", + "transport = 'transport_small.yaml'" + ] + }, + { + "cell_type": "code", + "execution_count": 178, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Cement': {'yaml': 'yamls\\\\cement_small.yaml', 'yaml identifier': 'Cement'},\n", + " 'Electricity': {'yaml': 'yamls\\\\electricity_small.yaml',\n", + " 'yaml identifier': 'Electricity'}}" + ] + }, + "execution_count": 178, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "files_dict={}\n", + "files_dict['Cement']={'yaml': 'yamls\\cement_small.yaml',\n", + " 'yaml identifier': 'Cement'}\n", + "files_dict['Electricity']= {'yaml':'yamls\\electricity_small.yaml',\n", + " 'yaml identifier': 'Electricity'} #yaml identifier is the name of the filter in the yaml file, in the first line.\n", + "files_dict" + ] + }, + { + "cell_type": "code", + "execution_count": 179, + "metadata": {}, + "outputs": [], + "source": [ + "# Assuming generate_sets_from_filters is a predefined function\n", + "def process_yaml_files(files_dict, database):\n", + " main_dict=files_dict.copy()\n", + " for key, value in main_dict.items():\n", + " yaml_file = value['yaml']\n", + " yaml_identifier = value['yaml identifier']\n", + " \n", + " # Generate the sector activities\n", + " sector_activities = generate_sets_from_filters(yaml_file, database=database)\n", + " \n", + " # Convert the set of activities to a list\n", + " activities_list = list(sector_activities[yaml_identifier])\n", + " \n", + " # Add to the sectors_dict\n", + " main_dict[key]['activities'] = activities_list\n", + " \n", + " return main_dict\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sectors_dict= process_yaml_files(files_dict, ei39SSP2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "type(sectors_dict['Electricity']['activities'][0])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sectors_dict['Electricity']['activities'][0].as_dict()['name']" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sectors_dict['Electricity']['activities'][4].as_dict()['name']" + ] + }, + { + "cell_type": "code", + "execution_count": 180, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'method_1': {'object': Brightway2 Method: IPCC 2013: climate change: global warming potential (GWP100),\n", + " 'method name': ('IPCC 2013',\n", + " 'climate change',\n", + " 'global warming potential (GWP100)'),\n", + " 'short name': 'global warming potential (GWP100)',\n", + " 'unit': 'kg CO2-Eq'},\n", + " 'method_2': {'object': Brightway2 Method: EN15804: inventory indicators ISO21930: Cumulative Energy Demand - non-renewable energy resources,\n", + " 'method name': ('EN15804',\n", + " 'inventory indicators ISO21930',\n", + " 'Cumulative Energy Demand - non-renewable energy resources'),\n", + " 'short name': 'Cumulative Energy Demand - non-renewable energy resources',\n", + " 'unit': 'megajoule'}}" + ] + }, + "execution_count": 180, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Get Methods\n", + "finder=MethodFinder()\n", + "finder.find_and_create_method(criteria=['IPCC', '2013', 'GWP100'], exclude=['no LT'])\n", + "finder.find_and_create_method(criteria=['EN15804','Cumulative', 'non-renewable' ])\n", + "# finder.find_and_create_method(criteria=['land occupation','selected'])\n", + "# finder.find_and_create_method(criteria=['EN15804','fresh water'])\n", + "method_dict=finder.get_all_methods()\n", + "method_dict" + ] + }, + { + "cell_type": "code", + "execution_count": 181, + "metadata": {}, + "outputs": [], + "source": [ + "def sector_lca_scores(main_dict, method_dict):\n", + " '''\n", + " Generates the LCA scores for the sectors activities in the main dictionary \n", + " for the different methods in the method dictionary.\n", + "\n", + " It returns the main dictionary updated as scores dictionary which also holds the former information for each sector.\n", + " The LCA scores are stored by method name in the respective sector dictionary within the main dictionary.\n", + " '''\n", + "\n", + " # Initialize scores_dict as a copy of main_dict\n", + " scores_dict = main_dict.copy()\n", + "\n", + " # Loop through each sector in main_dict\n", + " for sector in scores_dict.keys():\n", + " # Extract activities for the current sector\n", + " sector_activities = scores_dict[sector]['activities']\n", + " \n", + " # Calculate LCA scores using the specified method\n", + " lca_scores = compare_activities_multiple_methods(\n", + " activities_list=sector_activities,\n", + " methods=method_dict,\n", + " identifier=sector,\n", + " mode='absolute'\n", + " )\n", + " \n", + " # Apply the small_inputs_to_other_column function with the cutoff value\n", + " lca_scores = small_inputs_to_other_column(lca_scores, cutoff=0.02)\n", + " \n", + " # Save the LCA scores to the scores_dict\n", + " scores_dict[sector]['lca_scores'] = lca_scores\n", + "\n", + " return scores_dict" + ] + }, + { + "cell_type": "code", + "execution_count": 182, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Omitting activity name common prefix: 'cement production, '\n", + "Omitting activity name common prefix: 'cement production, '\n", + "Omitting activity name common prefix: 'electricity production, at biomass-fired IGCC power '\n", + "Omitting activity name common prefix: 'electricity production, at biomass-fired IGCC power '\n" + ] + } + ], + "source": [ + "scores_dict=sector_lca_scores(sectors_dict, method_dict) \n", + "\n", + "# this function is omitting common parts in the activities names, because only one type of activity portland and biomass from ICGGC is looked at \n", + "# the results are 'Portland' and 'plant' in the activity column" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "scores_dict['Cement']['lca_scores'].keys()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "scores_dict['Electricity']['lca_scores'].keys()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "scores_dict['Cement'].keys()" + ] + }, + { + "cell_type": "code", + "execution_count": 183, + "metadata": {}, + "outputs": [], + "source": [ + "def add_statistics(df, column_name='total'):\n", + "\n", + " #Need a rank row to plot the total LCA scores in descending order (satter opepyxl function takes in non categorial values)\n", + " df['rank'] = df[column_name].rank(method=\"first\", ascending=\"False\")\n", + "\n", + " # Calculate mean, standard deviation, and IQR\n", + " df['mean'] = df[column_name].mean()\n", + " df['2std_abv'] = df['mean'] + df[column_name].std() * 2\n", + " df['2std_blw'] = df['mean'] - df[column_name].std() * 2\n", + " df['q1'] = df[column_name].quantile(0.25)\n", + " df['q3'] = df[column_name].quantile(0.75)\n", + " \n", + " # Reorder the columns to place the new columns after 'total'\n", + " cols = df.columns.tolist()\n", + " total_index = cols.index(column_name) + 1\n", + " new_cols = ['rank', 'mean', '2std_abv', '2std_blw', 'q1', 'q3']\n", + " cols = cols[:total_index] + new_cols + cols[total_index:-len(new_cols)]\n", + " \n", + " return df[cols]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 184, + "metadata": {}, + "outputs": [], + "source": [ + "def sector_lca_scores_to_excel(scores_dict, excel_file_name):\n", + " # Prepare to save each LCA score table to a different worksheet in the same Excel file\n", + " excel_file = excel_file_name\n", + " with pd.ExcelWriter(excel_file, engine='openpyxl') as writer:\n", + " for sector in scores_dict.keys():\n", + " lca_scores = scores_dict[sector]['lca_scores']\n", + " for method, table in lca_scores.items():\n", + " # Create a DataFrame for the current LCA score table\n", + " df = pd.DataFrame(table)\n", + " # Add the index as a new column at the beginning\n", + " df.insert(0, 'Index', df.index)\n", + " # Add statistics to the DataFrame\n", + " df = add_statistics(df)\n", + " # Add sector marker column\n", + " df['sector']=str(sector) # potentially remove!\n", + " # Reorder the columns to move 'sector' after 'product'\n", + " columns = list(df.columns)\n", + " product_index = columns.index('product')\n", + " # Insert 'sector' after 'product'\n", + " columns.insert(product_index + 1, columns.pop(columns.index('sector')))\n", + " # Reassign the DataFrame with the new column order\n", + " df = df[columns]\n", + "\n", + " # Generate a worksheet name\n", + " worksheet_name = f\"{method}\" #f\"{sector}_{method}\"\n", + " if len(worksheet_name) > 31:\n", + " worksheet_name = worksheet_name[:31]\n", + " # Save the DataFrame to the Excel file in a new worksheet\n", + " df.to_excel(writer, sheet_name=worksheet_name, index=False)" + ] + }, + { + "cell_type": "code", + "execution_count": 205, + "metadata": {}, + "outputs": [], + "source": [ + "lca_scores_in_excel=sector_lca_scores_to_excel(scores_dict, 'output_v51_2.xlsx')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "PLOTS IN EXCEL" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Input : one excel file which holds the sector workesheets\n", + "# create a dictionary of worksheet names which fullfill the criteria.\n", + "# iterate through the list to get the worksheets\n", + "# Grouping sector graphs in one worksheet - for worksheets with same sector create a chart each in a worksheet called sector_charts\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from openpyxl import load_workbook\n", + "\n", + "def categorize_sheets_by_sector(file_path):\n", + " # Load the workbook\n", + " workbook = load_workbook(filename=file_path, read_only=True)\n", + " \n", + " # Initialize a dictionary to hold sectors and their corresponding sheet names\n", + " worksheet_dict = {}\n", + " \n", + " # Iterate over all sheet names in the workbook\n", + " for sheet_name in workbook.sheetnames:\n", + " # Split the sheet name to extract the sector (assumes sector is the first part)\n", + " sector = sheet_name.split('_')[0]\n", + " \n", + " # Add the sheet name to the corresponding sector in the dictionary\n", + " if sector in worksheet_dict:\n", + " worksheet_dict[sector].append(sheet_name)\n", + " else:\n", + " worksheet_dict[sector] = [sheet_name]\n", + " \n", + " return worksheet_dict" + ] + }, + { + "cell_type": "code", + "execution_count": 206, + "metadata": {}, + "outputs": [], + "source": [ + "worksheet_dict=categorize_sheets_by_sector('output_v51_2.xlsx')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Function to plot categorical scatter plots (Note: Axis can't take on categorical labels)" + ] + }, + { + "cell_type": "code", + "execution_count": 186, + "metadata": {}, + "outputs": [], + "source": [ + "from openpyxl.chart import ScatterChart, Reference, Series\n", + "from openpyxl import load_workbook\n", + "from openpyxl.drawing.text import CharacterProperties\n", + "from openpyxl.chart.label import DataLabelList\n", + "\n", + "\n", + "def dot_plots(filepath_workbook, worksheet_dict):\n", + " \n", + " # Load the workbook\n", + " wb = load_workbook(filepath_workbook)\n", + " \n", + " # Iterate over each sector and its associated worksheets\n", + " for sector, worksheet_names in worksheet_dict.items():\n", + " \n", + " # Create or get the chart sheet for the current sector\n", + " chart_sheet_name = f\"{sector}_dot_charts\"\n", + " if chart_sheet_name in wb.sheetnames:\n", + " ws_charts = wb[chart_sheet_name]\n", + " else:\n", + " ws_charts = wb.create_sheet(chart_sheet_name)\n", + " \n", + " # Initial position for the first chart\n", + " current_row = 1 # Start placing charts from row 1\n", + " chart_height = 21 # Number of rows a chart occupies, adjust as needed\n", + " \n", + " # Iterate over each worksheet name in the current sector\n", + " for worksheet_name in worksheet_names:\n", + " ws = wb[worksheet_name]\n", + "\n", + " # Find min_row, max_row and max_column\n", + " max_row = ws.max_row\n", + " print(max_row)\n", + " max_column = ws.max_column\n", + " min_row=1\n", + "\n", + " #define columns\n", + " total_col=9\n", + " rank_col=10\n", + " index_col=1\n", + " mean_col=11\n", + " std_adv_col=12\n", + " std_blw_col=13\n", + " q1_col=14\n", + " q3_col=15\n", + " \n", + " # Create a ScatterChart (or other chart type as needed)\n", + " chart = ScatterChart()\n", + "\n", + " # Chart titles\n", + " chart.title = f\"{ws['F2'].value} LCA scores for {sector} sector\"\n", + " chart.y_axis.title = f\"{ws['G2'].value}\"\n", + " chart.x_axis.title = 'activity rank'\n", + " # Avoid overlap\n", + " chart.title.overlay = False\n", + " chart.x_axis.title.overlay = False\n", + " chart.y_axis.title.overlay = False \n", + "\n", + "\n", + " # Define the data range for the chart\n", + " y_values = Reference(ws, min_col=total_col, min_row=min_row, max_row=max_row)\n", + " print(y_values)\n", + " x_values = Reference(ws, min_col=rank_col, min_row=min_row, max_row=max_row)\n", + " print(x_values)\n", + "\n", + " # Create a series and add it to the chart\n", + " series = Series(y_values, x_values, title_from_data=True)\n", + " chart.series.append(series)\n", + " chart.style = 9\n", + "\n", + " # Customize the series to show only markers (dots)\n", + " series.marker.symbol = \"circle\"\n", + " series.marker.size = 5\n", + " series.graphicalProperties.line.noFill = True\n", + "\n", + "\n", + " # ADJUST X-AXIS\n", + " chart.x_axis.tickLblPos = \"low\"\n", + " chart.x_axis.majorGridlines = None\n", + " chart.x_axis.tickMarkSkip = 0 # Show all tick marks\n", + " chart.x_axis.tickLblSkip = 0 # Show all labels\n", + "\n", + " chart.x_axis.scaling.orientation = \"minMax\"\n", + " chart.x_axis.crosses = \"autoZero\"\n", + " chart.x_axis.axPos = \"b\"\n", + " chart.x_axis.delete = False\n", + "\n", + " # ADJUST Y-AXIS\n", + "\n", + " # Calculate the min and max y-values to determine the axis range\n", + " y_values_list = [ws.cell(row=row, column=total_col).value for row in range(2,max_row+1)] #number of rows need to be defined based on dataframe\n", + " min_y_value = min(y_values_list)-min(y_values_list)*0.1\n", + " max_y_value = max(y_values_list)+ max(y_values_list)*0.1\n", + "\n", + " # Set y-axis range with some padding\n", + " padding = (max_y_value - min_y_value) * 0.1 # 10% padding on each side\n", + " chart.y_axis.scaling.min = min_y_value - padding\n", + " chart.y_axis.scaling.max = max_y_value + padding\n", + "\n", + " # Explicitly set tick labels to be visible\n", + " chart.y_axis.tickLblPos = \"nextTo\" # Position the labels next to the tick marks\n", + " #chart.y_axis.majorGridlines = True # Ensure major gridlines are enabled (though you mentioned they're visible)\n", + " chart.y_axis.delete = False # Ensure axis is not deleted\n", + " # Format the y-axis to show 2 decimal places\n", + " chart.y_axis.number_format = '0.000'\n", + "\n", + "\n", + " # ---------\n", + " # ADD STATS\n", + " # ---------\n", + " # MEAN\n", + " # Add a new series to the chart for the mean line\n", + " mean_y = Reference(ws, min_col=mean_col, min_row=min_row, max_row=max_row)\n", + " mean_series = Series(mean_y, x_values, title_from_data=\"True\")\n", + " chart.series.append(mean_series)\n", + "\n", + " # Customize the mean series to show as a line\n", + " mean_series.marker.symbol = \"none\" # No markers, just a line\n", + " mean_series.graphicalProperties.line.solidFill = \"FF0000\" # Red line for mean value\n", + " mean_series.graphicalProperties.line.width = 10000 # Set line width (default units are EMUs)\n", + "\n", + " # IQR\n", + " # Add a new series to the chart for the iqr lines\n", + " iqr1= Reference(ws, min_col=q1_col, min_row=min_row, max_row=max_row)\n", + " iqr3= Reference(ws, min_col=q3_col, min_row=min_row, max_row=max_row)\n", + " iqr1_series = Series(iqr1, x_values, title_from_data=\"True\")\n", + " iqr3_series = Series(iqr3, x_values, title_from_data=\"True\")\n", + " chart.series.append(iqr1_series)\n", + " chart.series.append(iqr3_series)\n", + "\n", + " # Customize the iqr 1,3 series to show as a line\n", + " iqr1_series.marker.symbol = \"none\" # No markers, just a line\n", + " iqr3_series.marker.symbol = \"none\"\n", + " iqr1_series.graphicalProperties.line.solidFill = \"6082B6\" # Blue line \n", + " iqr3_series.graphicalProperties.line.solidFill = \"6082B6\" \n", + " iqr1_series.graphicalProperties.line.width = 10000 # Set line width (default units are EMUs)\n", + " iqr3_series.graphicalProperties.line.width = 10000 # Set line width (default units are EMUs)\n", + "\n", + " # STD\n", + " # Add a new series to the chart for the std lines\n", + " std_abv= Reference(ws, min_col=std_adv_col, min_row=min_row, max_row=max_row)\n", + " std_blw= Reference(ws, min_col=std_blw_col, min_row=min_row, max_row=max_row)\n", + " std_abv_series = Series(std_abv, x_values, title_from_data=\"True\")\n", + " std_blw_series = Series(std_blw, x_values, title_from_data=\"True\")\n", + " chart.series.append(std_abv_series)\n", + " chart.series.append(std_blw_series)\n", + "\n", + " # Customize the iqr 1,3 series to show as a line\n", + " std_abv_series.marker.symbol = \"none\" # No markers, just a line\n", + " std_blw_series.marker.symbol = \"none\"\n", + " std_abv_series.graphicalProperties.line.solidFill = \"FFC300\" # yellow line\n", + " std_blw_series.graphicalProperties.line.solidFill = \"FFC300\" \n", + " std_abv_series.graphicalProperties.line.width = 10000 # Set line width (default units are EMUs)\n", + " std_blw_series.graphicalProperties.line.width = 10000 # Set line width (default units are EMUs)\n", + "\n", + " # Set legend position to the right of the plot area\n", + " chart.legend.position = 'r' # 'r' for right\n", + " chart.legend.overlay= False\n", + "\n", + " # Adjust chart dimensions and position to make space for the legend\n", + " chart.width = 18 # Example width, adjust as needed\n", + " chart.height = 10 # Example height, adjust as needed\n", + "\n", + " # Add the chart to the chart worksheet\n", + " # Calculate the position for this chart\n", + " position = f\"A{current_row}\"\n", + " ws_charts.add_chart(chart, position)\n", + " \n", + " # Update current_row to position the next chart below the current one\n", + " current_row += chart_height\n", + "\n", + " wb.save(filepath_workbook)" + ] + }, + { + "cell_type": "code", + "execution_count": 190, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Cement': ['Cement_global_warming_potential',\n", + " 'Cement_cumulative_energy_demand'],\n", + " 'Electricity': ['Electricity_global_warming_pote',\n", + " 'Electricity_cumulative_energy_d']}" + ] + }, + "execution_count": 190, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "worksheet_dict" + ] + }, + { + "cell_type": "code", + "execution_count": 207, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "8\n", + "'Cement_global_warming_potential'!$I$1:$I$8\n", + "'Cement_global_warming_potential'!$J$1:$J$8\n", + "8\n", + "'Cement_cumulative_energy_demand'!$I$1:$I$8\n", + "'Cement_cumulative_energy_demand'!$J$1:$J$8\n", + "11\n", + "'Electricity_global_warming_pote'!$I$1:$I$11\n", + "'Electricity_global_warming_pote'!$J$1:$J$11\n", + "11\n", + "'Electricity_cumulative_energy_d'!$I$1:$I$11\n", + "'Electricity_cumulative_energy_d'!$J$1:$J$11\n" + ] + } + ], + "source": [ + "dot_plots('output_v51_2.xlsx', worksheet_dict=worksheet_dict)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Setup Stacked Plots" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from openpyxl import Workbook\n", + "from openpyxl.chart import BarChart, Series, Reference\n", + "from openpyxl.chart.text import RichText\n", + "from openpyxl.drawing.text import CharacterProperties\n", + "\n", + "def stacked_bars(filepath_workbook, worksheet_dict):\n", + "\n", + " # Load the workbook\n", + " wb = load_workbook(filepath_workbook)\n", + " \n", + " # Iterate over each sector and its associated worksheets\n", + " for sector, worksheet_names in worksheet_dict.items():\n", + " \n", + " # Create or get the chart sheet for the current sector\n", + " chart_sheet_name = f\"{sector}_stacked_bars\"\n", + " if chart_sheet_name in wb.sheetnames:\n", + " ws_charts = wb[chart_sheet_name]\n", + " else:\n", + " ws_charts = wb.create_sheet(chart_sheet_name)\n", + " \n", + " # Initial position for the first chart\n", + " current_row = 1 # Start placing charts from row 1\n", + " chart_height = 21 # Number of rows a chart occupies, adjust as needed\n", + " \n", + " # Iterate over each worksheet name in the current sector\n", + " for worksheet_name in worksheet_names:\n", + " ws = wb[worksheet_name]\n", + "\n", + " # Find min_row, max_row and max_column\n", + " max_row = ws.max_row\n", + " max_column = ws.max_column\n", + " input_min_col=15\n", + "\n", + " chart = BarChart()\n", + " chart.type = \"col\"\n", + " chart.style = 2\n", + " chart.grouping = \"stacked\"\n", + " chart.overlap = 100\n", + "\n", + " # Chart titles\n", + " chart.title = f\"{ws['F2'].value} input contributions to LCA scores for {sector} sector\"\n", + " chart.y_axis.title = f\"{ws['G2'].value}\"\n", + " chart.x_axis.title = 'activity index'\n", + "\n", + " # Avoid overlap\n", + " chart.title.overlay = False\n", + " chart.x_axis.title.overlay = False\n", + " chart.y_axis.title.overlay = False \n", + " chart.legend.overlay = False\n", + "\n", + " # define data\n", + " data = Reference(ws, min_col=input_min_col, min_row=1, max_row=max_row, max_col = max_column)\n", + " cats = Reference(ws, min_col=1, min_row=2, max_row=max_row)\n", + "\n", + " chart.add_data(data, titles_from_data=True)\n", + " chart.set_categories(cats)\n", + " chart.shape = 4\n", + "\n", + " # Modify each series in the chart to disable the inversion of negative values \n", + " for series in chart.series:\n", + " series.invertIfNegative = False\n", + "\n", + " # y-axis ticks\n", + " chart.y_axis.tickLblPos = \"nextTo\"\n", + " chart.y_axis.delete = False # Ensure axis is not deleted\n", + " chart.y_axis.number_format = '0.000'\n", + "\n", + " # Set legend position to the right of the plot area\n", + " chart.legend.position = 'r' # 'r' for right\n", + " chart.legend.overlay= False\n", + "\n", + " # Adjust chart dimensions and position to make space for the legend\n", + " chart.width = 15 # Example width, adjust as needed\n", + " chart.height = 25 # Example height, adjust as needed\n", + "\n", + " # Add the chart to the chart worksheet\n", + " # Calculate the position for this chart\n", + " position = f\"A{current_row}\"\n", + " ws_charts.add_chart(chart, position)\n", + " \n", + " # Update current_row to position the next chart below the current one\n", + " current_row += chart_height+4\n", + "\n", + " wb.save(filepath_workbook)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# stacked_bars('output_v4_2.xlsx', worksheet_dict=worksheet_dict)" + ] + }, + { + "cell_type": "code", + "execution_count": 202, + "metadata": {}, + "outputs": [], + "source": [ + "from openpyxl import load_workbook\n", + "from openpyxl.chart import BarChart, Reference\n", + "\n", + "def stacked_bars(filepath_workbook, worksheet_dict):\n", + " # Load the workbook\n", + " wb = load_workbook(filepath_workbook)\n", + " \n", + " # Iterate over each sector and its associated worksheets\n", + " for sector, worksheet_names in worksheet_dict.items():\n", + " \n", + " # Create or get the chart sheet for the current sector\n", + " chart_sheet_name = f\"{sector}_stacked_bars\"\n", + " if chart_sheet_name in wb.sheetnames:\n", + " ws_charts = wb[chart_sheet_name]\n", + " else:\n", + " ws_charts = wb.create_sheet(chart_sheet_name)\n", + " \n", + " # Initial position for the first chart\n", + " current_row = 1 # Start placing charts from row 1\n", + " current_col = 1 # Start placing charts from column 1\n", + " chart_height = 18 # Number of rows a chart occupies\n", + " chart_width = 15 # Number of columns a chart occupies\n", + " charts_per_row = 3 # Number of charts per row\n", + " \n", + " # Iterate over each worksheet name in the current sector\n", + " for i, worksheet_name in enumerate(worksheet_names):\n", + " ws = wb[worksheet_name]\n", + "\n", + " # Find min_row, max_row and max_column\n", + " max_row = ws.max_row\n", + " max_column = ws.max_column\n", + " input_min_col = 16\n", + "\n", + " chart = BarChart()\n", + " chart.type = \"col\"\n", + " chart.style = 2\n", + " chart.grouping = \"stacked\"\n", + " chart.overlap = 100\n", + "\n", + " # Chart titles\n", + " chart.title = f\"{ws['F2'].value} input contributions to LCA scores for {sector} sector\"\n", + " chart.y_axis.title = f\"{ws['G2'].value}\"\n", + " chart.x_axis.title = 'activity index'\n", + "\n", + " # Avoid overlap\n", + " chart.title.overlay = False\n", + " chart.x_axis.title.overlay = False\n", + " chart.y_axis.title.overlay = False \n", + " chart.legend.overlay = False\n", + "\n", + " # Define data\n", + " data = Reference(ws, min_col=input_min_col, min_row=1, max_row=max_row, max_col=max_column)\n", + " cats = Reference(ws, min_col=1, min_row=2, max_row=max_row)\n", + "\n", + " chart.add_data(data, titles_from_data=True)\n", + " chart.set_categories(cats)\n", + " chart.shape = 4\n", + "\n", + " # Modify each series in the chart to disable the inversion of negative values \n", + " for series in chart.series:\n", + " series.invertIfNegative = False\n", + "\n", + " # y-axis ticks\n", + " chart.y_axis.tickLblPos = \"nextTo\"\n", + " chart.y_axis.delete = False # Ensure axis is not deleted\n", + " chart.y_axis.number_format = '0.000'\n", + "\n", + " # Set legend position to the right of the plot area\n", + " chart.legend.position = 'r' # 'r' for right\n", + " chart.legend.overlay = False\n", + "\n", + " # Adjust chart dimensions\n", + " chart.width = 15 # Example width, adjust as needed\n", + " chart.height = 25 # Example height, adjust as needed\n", + "\n", + " # Add the chart to the chart worksheet\n", + " # Calculate the position for this chart\n", + " position = ws_charts.cell(row=current_row, column=current_col).coordinate\n", + " ws_charts.add_chart(chart, position)\n", + " \n", + " # Update position for the next chart\n", + " current_col += chart_width -4\n", + " if (i + 1) % charts_per_row == 0: # Move to the next row after placing `charts_per_row` charts\n", + " current_row += chart_height +2\n", + " current_col = 1 # Reset to the first column\n", + " \n", + " wb.save(filepath_workbook)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 208, + "metadata": {}, + "outputs": [], + "source": [ + "stacked_bars('output_v51_2.xlsx', worksheet_dict=worksheet_dict)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from openpyxl import Workbook\n", + "from openpyxl.chart import BarChart, Series, Reference\n", + "from openpyxl.chart.text import RichText\n", + "from openpyxl.drawing.text import CharacterProperties\n", + "\n", + "wb = load_workbook('output_v45_2.xlsx')\n", + "ws = wb['Electricity_Electricity_cumulat']\n", + "ws_charts=wb.create_sheet('stacked_bar_charts_cumulat_10')\n", + "\n", + "chart = BarChart()\n", + "chart.type = \"col\"\n", + "chart.style = 2\n", + "chart.grouping = \"stacked\"\n", + "chart.overlap = 100\n", + "\n", + "chart.title = \"Stacked bar chart\"\n", + "chart.y_axis.title = \"method unit\"\n", + "chart.x_axis.title = \"activity indexes\"\n", + "\n", + "# Avoid overlap\n", + "chart.title.overlay = False\n", + "chart.x_axis.title.overlay = False\n", + "chart.y_axis.title.overlay = False \n", + "chart.legend.overlay = False\n", + "\n", + "# define data\n", + "data = Reference(ws, min_col=15, min_row=1, max_row=11, max_col = 19)\n", + "cats = Reference(ws, min_col=1, min_row=2, max_row=11)\n", + "\n", + "chart.add_data(data, titles_from_data=True)\n", + "chart.set_categories(cats)\n", + "chart.shape = 7 #what is this for?\n", + "\n", + "# Modify each series in the chart to disable the inversion of negative values \n", + "for series in chart.series:\n", + " series.invertIfNegative = False\n", + "\n", + "# y-axis ticks\n", + "chart.y_axis.tickLblPos = \"nextTo\"\n", + "chart.y_axis.delete = False # Ensure axis is not deleted\n", + "chart.y_axis.number_format = '0.000'\n", + "\n", + "ws_charts.add_chart(chart, \"B1\")\n", + "wb.save('output_v47_2.xlsx')\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "premise", + "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.11.9" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}