<jupyter_start><jupyter_text>Use examples of [premise](https://github.com/romainsacchi/premise)Author: [romainsacchi](https://github.com/romainsacchi)This notebook shows examples on how to use `premise` to create life cycle invneotries databases for prospective environmental impact assessment.This library extract useful information from IAM model output files (such as those of REMIND or IMAGE) and aligns inventories in the ecoinvent database accordingly.With version 0.2.0, the following transformation are available:* `update_electricity()`: create regional electricity markets and adjust efficiency of power plants* `update_cement()`: creates regional markets for clinker production and adjust clinker production efficiency* `update_steel()`: creates regional markets for steel and adjust steel production efficiency and the supply of secondary steel* `update_cars()`: produces fleet average cars and relinks to activities consuming pasenger car trnasport* `update_trucks()`: produces fleet average trucks and relinks to activities consuming lorry trnasport* `update_solar_PV()`: updates efficiency of solar PV modulesAdditional documentation on the methodology is available [here](https://premise.readthedocs.io/en/latest/introduction.html). Requirements* Pyhton 3.7 or above* a user license for ecoinvent v.3  Use case with [brightway2](https://brightway.dev/)`brightway2` is an open source LCA framework for Python.To use `premise` from `brightway2`, it requires that you have a open `brightway2` project with abiosphere as well as anecoinvent v.3 database registered in that project.<jupyter_code>from premise import *
import brightway2 as bw
bw.projects.set_current("your_bw2_project")<jupyter_output><empty_output><jupyter_text>List of available scenariosSome scenarios come installed with the library.They are stored in `data/iam_ouput_files` from the root directory.They are all within the SSP2 pathway (middle of the road).With REMIND:* "SSP2-Base"* "SSP2-NPi"* "SSP2-NDC"* "SSP2-PkBudg1300"* "SSP2-PkBudg1100"* "SSP2-PkBudg900"With IMAGE:* "SSP2-Base"  Database creation from default scenariosTo create a scenario using REMIND's SSP2 Base pathway, from ecoinvent 3.6 for the year 2028, one would execute the following cell. This leads to the extraction of the database, some cleanup as well as importing a few additional inventories.<jupyter_code>ndb = NewDatabase(
            scenarios=[
                {"model":"remind", "pathway":"SSP2-Base", "year":2028}
            ],
            source_db="ecoinvent 3.6 cutoff", # <-- name of the database in the BW2 project. Must be a string.
            source_version="3.6", # <-- version of ecoinvent. Can be "3.5", "3.6", "3.7" or "3.7.1". Must be a string.
    )<jupyter_output><empty_output><jupyter_text>or here with ecoinvent 3.7.1<jupyter_code>ndb = NewDatabase(
        scenarios=[
                {"model":"remind", "pathway":"SSP2-Base", "year":2028}
            ],
        source_db="ecoinvent 3.7 cutoff", # <-- this is NEW.
        source_version="3.7.1", # <-- this is NEW
    )<jupyter_output><empty_output><jupyter_text>If you want to create multiple databases at once, just populate the `scenarios` list.<jupyter_code>ndb = NewDatabase(
            scenarios=[
                {"model":"remind", "pathway":"SSP2-Base", "year":2028},
                {"model":"remind", "pathway":"SSP2-NPi", "year":2032}
            ],
            source_db="ecoinvent 3.5 cutoff", # <-- name of the database. Must be a string.
            source_version="3.5", # <-- version of ecoinvent. Can be "3.5", "3.6", "3.7" or "3.7.1"
                 )<jupyter_output><empty_output><jupyter_text>When the database is loaded and the additional inventories imported, you can apply a transformation function.For example here, we adjust the efficiency of the solar PVs to the two scenarios we have loaded.We go more in details later.<jupyter_code>ndb.update_solar_PV()<jupyter_output><empty_output><jupyter_text>And then, we register these two databases back into brightway2.<jupyter_code>ndb.write_db_to_brightway()<jupyter_output><empty_output><jupyter_text>Database creation from non-default scenariosIf 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.**Important remark**: your scenario file must begin with "remind_" or "image_".<jupyter_code>ndb = NewDatabase(
    scenarios = [{"model":"remind", "pathway":"my_special_scenario", "year":2028, "filepath":r"C:\Users\sacchi_r\Downloads\REMIND"}],        
    source_db="ecoinvent 3.6 cutoff", # <-- name of the database
    source_version=3.6, # <-- version of ecoinvent
                 )<jupyter_output><empty_output><jupyter_text>Adding inventoriesUpon the database extraction, you can import some of your Brightway2-compatible inventories like so:<jupyter_code>ndb = NewDatabase(
            scenarios=[
                {"model":"remind", "pathway":"SSP2-Base", "year":2030},
                {"model":"remind", "pathway":"SSP2-Base", "year":2030},
            ],
            source_db="ecoinvent 3.7 cutoff", 
            source_version="3.7.1",
            additional_inventories= [ # <-- this is NEW
                {"filepath": r"filepath\to\excel_file.xlsx", "ecoinvent version": "3.7"}, # <-- this is NEW
                {"filepath": r"filepath\to\another_excel_file.xlsx", "ecoinvent version": "3.7"}, # <-- this is NEW
            ] # <-- this is NEW
                 )<jupyter_output><empty_output><jupyter_text>Use case with ecospold2The source database does not have to be from a brightway2 project.It can be directly extracted from the bunch of ecospold2 files one gets when downloaded from the [ecoinvent website](https://ecoinvent.org).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.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).<jupyter_code>ndb = NewDatabase(
        scenarios = [
            {"model":"remind", "pathway":"my_special_scenario", "year":2028, "filepath":r"C:\Users\sacchi_r\Downloads\REMIND"}
        ],        
        source_type="ecospold", # <--- this is NEW
        source_file_path=r"C:\Users\sacchi_r\Dropbox\Public\ecoinvent 3.5_cutoff_ecoSpold02\datasets", # <-- this is NEW
        source_version="3.5",
    )<jupyter_output><empty_output><jupyter_text>Transformation functionsThese functions modify the extracted database:* **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.* **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) and clinker-to-cement ratio.* **update_steel()**: adjustment of process efficiency, fuel mix and share of secondary steel in steel markets.* **update_solar_PV()**: adjustment of solar PV panels efficiency to the year considered.They can be applied *separately*, *consecutively* or *altogether* (using instead **.update_all()**).They will apply to all the scenario-specific databases listed in `scenarios`.<jupyter_code>ndb.update_all()<jupyter_output><empty_output><jupyter_text>Additional options for database creation with cars and truck inventoriesTwo additional functions are available:* **update_cars()**: creates updated inventories for fleet average passenger cars and links back to activities that consumetransport.* **update_trucks()**: creates updated inventories for fleet average lorry trucks and links back to activities that consumetransport.By default, inventories for passenger cars and trucks are not added upon the creation of the database.But you can add them by adding the following keys to the scenario dictionary: `passenger cars` and `trucks`.This will create inventories for passenger cars and trucks for each region of the IAM model.For example, here we create car inventories in the first scenario (but only for the region *EUR*), and both cars and trucks inventories in the second scenario (for all regions). Leaving an empty dictionary indicates that we do not apply any filter in terms of regions and that we do not provide a fleet composition file (a default one is then used).<jupyter_code>from premise import *
import brightway2 as bw
bw.projects.set_current("your_bw2_project")
ndb = NewDatabase(
            scenarios=[
                {"model":"remind", "pathway":"SSP2-Base", "year":2030,"passenger cars": {"regions":["EUR"]}},
                {"model":"remind", "pathway":"SSP2-Base", "year":2030, "passenger cars":{}, "trucks":{}},
            ],
            source_db="ecoinvent 3.7 cutoff",
            source_version="3.7", 
            
                 )<jupyter_output>No fleet composition file is provided for trucks.Fleet average vehicles will be built using default fleet projection.

////////////////////// EXTRACTING SOURCE DATABASE ///////////////////////
Getting activity data<jupyter_text>Then we need to explicitly run `update_cars()` and/or `update_trucks()` (or `update_all()`).<jupyter_code>ndb.update_cars()
ndb.update_trucks()
ndb.write_db_to_brightway()<jupyter_output>Write new database(s) to Brightway2.
19631 datasets
631583 exchanges
0 unlinked exchanges<jupyter_text>Fleet filesA last word about passenger cars and trucks: it is possible to pass a fleet composition file to generate fleet average inventories and limit those to specific regions (here, the European region).<jupyter_code>ndb = NewDatabase(
            scenarios=[
                {"model":"remind", "pathway":"SSP2-Base", "year":2030,
                 "passenger cars": {"regions":["EUR", "NEU"],
                                    "fleet file":r"filepath/to/fleet_file.csv.csv"},
                 "trucks": {"regions":["EUR"]}
                },
                {"model":"image", "pathway":"SSP2-Base", "year":2030,},
            ],
            source_db="ecoinvent 3.7 cutoff",
            source_version="3.7"
    )<jupyter_output><empty_output><jupyter_text>Exclude specific functionsFinally, we can exclude some transformation functions when executing `update_all()` like so:<jupyter_code>ndb = NewDatabase(
            scenarios=[
                {"model":"remind", "pathway":"SSP2-Base", "year":2030,
                 "exclude": ["update_steel"], # <-- do not execute update_seel()
                 "passenger cars": {"regions":["EUR"]},"trucks": {"regions":["EUR"]}
                },
                {"model":"remind", "pathway":"SSP2-Base", "year":2030,},
            ],
            source_db="ecoinvent 3.7 cutoff",
            source_version="3.7", 
)<jupyter_output><empty_output><jupyter_text>Export  As a Brightway2 database Export the modified database to brightway2<jupyter_code>ndb.write_db_to_brightway()<jupyter_output><empty_output><jupyter_text>As a sparse matrix representation Or export it as a sparse matrix representation.This will export four files:* "A_matrix.csv": matrix coordinates and values of shape (index of activity; index of product; value) for the technosphere* "A_matrix_index.csv": labels for indices for A matrix of shape (name of activity, reference product, unit, location, index)* "B_matrix.csv": matrix coordinates and values of shape (index of activity; index of biosphere flow; value) for the biosphere* "B_matrix_index.csv": labels for indices for B matrix of shape (name of biosphere flow, main compartment, sub-compartmnet, unit, index)As a convenience, you can specifiy a directory where to store the exported matrices.If the directory does not exist, it will be created.If you leave it unspecified, they will be stored in **data/matrices** in the root folder of the library.<jupyter_code>ndb.write_db_to_matrices(filepath=r"C:/Users/sacchi_r/Downloads/exported_matrices")<jupyter_output><empty_output><jupyter_text>As a SimaPro CSV file<jupyter_code>ndb.write_db_to_simapro(filepath=r"C:/Users/sacchi_r/Downloads/exported_simapro_file")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10859986945169713, 'fraction_numerical': 0.016155352480417755, 'mean_word_length': 4.088003320880033, 'pattern_counts': {'":': 66, '<': 82, '<?xml version=': 0, '>': 67, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '75216', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 3686, 'n_tokens_neox': 3488, 'n_words': 1182, 'path': 'examples/.ipynb_checkpoints/examples-checkpoint.ipynb', 'repo_name': 'kais-siala/rmnd-lca', 'size': '12256'}