Skip to content

Commit

Permalink
Align wrt master
Browse files Browse the repository at this point in the history
  • Loading branch information
romainsacchi authored and romainsacchi committed Aug 13, 2024
1 parent bb2a3fe commit db1c201
Showing 1 changed file with 76 additions and 126 deletions.
202 changes: 76 additions & 126 deletions examples/examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -209,32 +209,30 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ndb.write_db_to_brightway()"
]
"source": "ndb.write_db_to_brightway(name=\"my database\") # name is optional"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "However, if you wish first to proceed with the IAM integration, you need to use the `update()` method, like so for the electricity sector:"
"source": [
"However, if you wish first to proceed with the IAM integration, you need to use the `update()` method, like so for the electricity sector:\n",
"\n",
"Note: if you have run ``write_db_to_brightway()`` already, you will eed to re-instantiate the `NewDatabase` object."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ndb.update(\"electricity\")"
]
"source": "ndb.update(\"electricity\") # <- updates the electricity sector only"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ndb.write_db_to_brightway()"
]
"source": "ndb.write_db_to_brightway(name=\"my database with electricity changes\")"
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -265,43 +263,36 @@
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When the database is loaded and the additional inventories imported, you can apply a transformation function.\n",
"For example here, we adjust the efficiency of the power plants to the two scenarios we have loaded.\n",
"We go more in details later."
]
"source": "When the database is loaded and the additional inventories imported, you can apply `.update()`."
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ndb.update(\"electricity\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Or you can proceed instead to doing all the sectoral transformations available, like so:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ndb.update() # <- updates all sectors"
]
"metadata": {
"ExecuteTime": {
"end_time": "2024-08-13T11:00:52.481723Z",
"start_time": "2024-08-13T11:00:52.241966Z"
}
},
"source": "ndb.update() # <- updates all sectors, unless you provide a list of sectors to update",
"outputs": [
{
"ename": "NameError",
"evalue": "name 'ndb' is not defined",
"output_type": "error",
"traceback": [
"\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
"\u001B[0;31mNameError\u001B[0m Traceback (most recent call last)",
"Cell \u001B[0;32mIn[4], line 1\u001B[0m\n\u001B[0;32m----> 1\u001B[0m \u001B[43mndb\u001B[49m\u001B[38;5;241m.\u001B[39mupdate() \u001B[38;5;66;03m# <- updates all sectors, unless you provide a list of sectors to update\u001B[39;00m\n",
"\u001B[0;31mNameError\u001B[0m: name 'ndb' is not defined"
]
}
],
"execution_count": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And then, we register these two databases back into brightway2."
]
"source": "And then, we register these four databases back into brightway2."
},
{
"cell_type": "code",
Expand All @@ -318,10 +309,10 @@
"source": [
"### Consequential\n",
"\n",
"`premise` can read in the consequential version of ecoinvent (v.3.8 and 3.9 only).\n",
"Based on the publication of Maes et al. 2023 (https://doi.org/10.1016/j.rser.2023.113830), `premise` builds marginal market mixes for electricity and fuels.\n",
"`premise` can read in the consequential version of ecoinvent. Based on the publication of Maes et al. 2023 (https://doi.org/10.1016/j.rser.2023.113830), `premise` builds marginal market mixes for electricity, fuel and steel markets. For steel markets, this is rather simple: the secondary steel providers (who use scrap steel in an electric arc furnace) are removed from regional the market mix.\n",
"The identification of marginal suppliers can be influenced by passing a series of arguments to `NewDatabase()`.\n",
"Additionally, `premise` removes secondary steel technologies from steel markets."
"\n",
"Refer to the [documentation](https://premise.readthedocs.io/en/latest/consequential.html) for more info."
]
},
{
Expand All @@ -348,10 +339,15 @@
" source_version=\"3.8\", # <-- Can only be 3.8.\n",
" key='xxxxxxxxxxxxxxxxxxxxxxxxx',\n",
" system_model=\"consequential\", # <-- Must specify \"consequential\"\n",
" system_model_args=args # Optional. Arguments.\n",
" system_model_args=args # Optional. Arguments. If not provided, default values are used. See premise documentation for more info.\n",
")"
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Than proceed as usual, with the update and write functions."
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -360,7 +356,7 @@
"\n",
"If you have some specific IAM scenarios (one that is not included in `premise`) you would like to build a database from, you can specify the directory to those.\n",
"\n",
"**Important remark**: your scenario file must begin with \"remind_\" or \"image_\". When using a non-default scenario that you provide yourself, you do not have to provide a decryption key."
"**Important remark**: your scenario file must begin with the model name (e.g., \"remind_xxx.csv\" when using `remind`). When using a non-default scenario that you provide yourself, you do not have to provide a decryption key."
]
},
{
Expand Down Expand Up @@ -404,17 +400,17 @@
"outputs": [],
"source": [
"ndb = NewDatabase(\n",
" scenarios=[\n",
" {\"model\":\"remind\", \"pathway\":\"SSP2-Base\", \"year\":2030},\n",
" ],\n",
" source_db=\"ecoinvent 3.7 cutoff\", \n",
" source_version=\"3.7.1\",\n",
" key='xxxxxxxxxxxxxxxxxxxxxxxxx'\n",
" additional_inventories= [ # <-- this is NEW\n",
" {\"filepath\": r\"filepath\\to\\excel_file.xlsx\", \"ecoinvent version\": \"3.7\"}, # <-- this is NEW\n",
" {\"filepath\": r\"filepath\\to\\another_excel_file.xlsx\", \"ecoinvent version\": \"3.7\"}, # <-- this is NEW\n",
" ] # <-- this is NEW\n",
" )"
" scenarios=[\n",
" {\"model\":\"remind\", \"pathway\":\"SSP2-Base\", \"year\":2030},\n",
" ],\n",
" source_db=\"ecoinvent 3.7 cutoff\", \n",
" source_version=\"3.7.1\",\n",
" key='xxxxxxxxxxxxxxxxxxxxxxxxx'\n",
" additional_inventories= [ # <-- this is NEW\n",
" {\"filepath\": r\"filepath\\to\\excel_file.xlsx\", \"ecoinvent version\": \"3.7\"}, # <-- this is NEW\n",
" {\"filepath\": r\"filepath\\to\\another_excel_file.xlsx\", \"ecoinvent version\": \"3.7\"}, # <-- this is NEW\n",
" ]\n",
")"
]
},
{
Expand All @@ -424,11 +420,11 @@
"# Use case with ecospold2\n",
"\n",
"The source database does not have to be from a brightway2 project.\n",
"It can be directly extracted from the bunch of ecospold2 files one gets when downloaded from the [ecoinvent website](https://ecoinvent.org).\n",
"It can be directly extracted from the ecospold2 files one gets when downloaded from the [ecoinvent website](https://ecoinvent.org).\n",
"\n",
"For this, one needs to specify the argument `source_db = \"ecospold\"` as well as `source_file_path`, which is the directory leading to the ecospold files.\n",
"For this, one needs to specify the argument `source_db = \"ecospold\"` as well as `source_file_path`, which is the `datasets` folder containing the ecospold files.\n",
"\n",
"For example, here we combine the use of a specific (non-default) IAM scenario file with the use of ecospold2 files as data source (ecoinvent 3.5 in this case)."
"For example, here we combine the use of a specific (non-default) IAM scenario file with the use of ecospold2 files as data source (ecoinvent 3.8 in this case)."
]
},
{
Expand All @@ -444,7 +440,7 @@
" ], \n",
" source_type=\"ecospold\", # <--- this is NEW\n",
" source_file_path=r\"C:\\filepath\\to\\your\\ecosposld\\folder\\datasets\", # <-- this is NEW\n",
" source_version=\"3.5\",\n",
" source_version=\"3.8\",\n",
" )"
]
},
Expand All @@ -458,6 +454,10 @@
"\n",
"* **update(\"electricity\")**: alignment of regional electricity production mixes as well as efficiencies for a number of electricity production technologies, including Carbon Capture and Storage technologies and photovoltaic panels. Also updated the natural gas extraction datasets.\n",
"\n",
"* **update(\"biomass\")**: adjustment of the share of residual vs. purpose-grown biomass for use in heat and power generation.\n",
"\n",
"* **update(\"battery\")**: adjustment of battery technologies for stationary and mobile applications.\n",
"\n",
"* **update(\"cement\")**: adjustment of technologies for cement production (dry, semi-dry, wet, with pre-heater or not), fuel efficiency of kilns, fuel mix of kilns (including biomass and waste fuels).\n",
"\n",
"* **update(\"steel\")**: adjustment of process efficiency, fuel mix and share of secondary steel in steel markets.\n",
Expand All @@ -478,81 +478,23 @@
"\n",
"* **update(\"buses\")**: create inventories for buses.\n",
"\n",
"A look at the documentation is advised.\n",
"* **update(\"trains\")**: create inventories for trains.\n",
"\n",
"A look at the [documentation](https://premise.readthedocs.io/en/latest/transform.html) is advised.\n",
"\n",
"\n",
"These functions can be applied *separately*, *consecutively* or *altogether* (using instead **.update()** without arguments).\n",
"\n",
"They will apply to all the scenario-specific databases listed in `scenarios`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from premise import *\n",
"import bw2data\n",
"bw2data.projects.set_current(\"some project\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ndb = NewDatabase(\n",
" scenarios=[\n",
" {'model':'remind','pathway':'SSP2-Base','year':'2020'},\n",
" {\"model\":\"image\", \"pathway\":\"SSP2-Base\", \"year\":2034},\n",
" ],\n",
" key='xxxxxxxxxxxxxxxxxxxxxxxxx',\n",
" source_db=\"ecoinvent 3.7 cutoff\",\n",
" source_version=\"3.7\", \n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ndb.update()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ndb.write_db_to_brightway()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also give your datababases a custom name."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ndb.write_db_to_brightway(name=[\"my_custom_name_1\", \"my_custom_name_2\"])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Export"
"# Export\n",
"\n",
"Note: `.update()` methods must be applied before exporting the databases, otherwise they will not contain any changes (only additional inventories)."
]
},
{
Expand Down Expand Up @@ -593,9 +535,9 @@
"\n",
"This will export four files:\n",
"\n",
"* \"A_matrix.csv\": matrix coordinates and values of shape (index of activity; index of product; value) for the technosphere\n",
"* \"A_matrix.csv\": matrix coordinates and values of shape (index of activity; index of product; `amount`, `uncertainty type`, `loc`, `scale`, `shape`, `minimum`, `maximum` and `negative`) for the technosphere matrix. Information on the meaning of these parameters cna be found in the `stats_array` [documentation](https://stats-arrays.readthedocs.io/en/latest/).\n",
"* \"A_matrix_index.csv\": labels for indices for A matrix of shape (name of activity, reference product, unit, location, index)\n",
"* \"B_matrix.csv\": matrix coordinates and values of shape (index of activity; index of biosphere flow; value) for the biosphere\n",
"* \"B_matrix.csv\": matrix coordinates and values of shape (index of activity; index of biosphere flow; `amount`, `uncertainty type`, `loc`, `scale`, `shape`, `minimum`, `maximum` and `negative`) for the biosphere matrix. Information on the meaning of these parameters cna be found in the `stats_array` [documentation](https://stats-arrays.readthedocs.io/en/latest/).\n",
"* \"B_matrix_index.csv\": labels for indices for B matrix of shape (name of biosphere flow, main compartment, sub-compartmnet, unit, index)\n",
"\n",
"As a convenience, you can specifiy a directory where to store the exported matrices.\n",
Expand Down Expand Up @@ -712,6 +654,14 @@
"[(k, v) for k, v in zip(l_res, list(A_inds_rev.values())[:10])]"
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"Perhaps more efficient, you can instead load these arrays into a data package using `bw_processing` and calculate the GWP scores using `bw2calc`.\n",
"See examples [here](https://github.com/Depart-de-Sentier/Spring-School-2024/blob/main/class-materials/brightway-basics/2%20-%20Building%20and%20using%20matrices%20in%20bw2calc.ipynb)."
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down

0 comments on commit db1c201

Please sign in to comment.