.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/pipeline/plot_balanced_scaling_food_balance_sheet_pipeline.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_pipeline_plot_balanced_scaling_food_balance_sheet_pipeline.py: ====================================== Building a Food Balance Sheet Pipeline ====================================== This example demonstrates the use of the pipeline manager to create a simple pipeline of modules. In this particular example, we will load a food balance sheet dataset, compute prelimimary Self-Sufficiency and Import Dependency Ratios (SSR, IDR) and add new items and years to the dataset. We will also print the SSR and IDR values to the console. Finally, we will scale the food balance sheet to reduce animal products and plot the results. .. GENERATED FROM PYTHON SOURCE LINES 18-20 We start by creating a pipeline object, which will manage the flow of data through the different modules. .. GENERATED FROM PYTHON SOURCE LINES 20-33 .. code-block:: Python import numpy as np from matplotlib import pyplot as plt from agrifoodpy.pipeline.pipeline import Pipeline from agrifoodpy.food import model from agrifoodpy.utils import nodes from agrifoodpy.utils.scaling import linear_scale # Create a pipeline object pipeline = Pipeline() .. GENERATED FROM PYTHON SOURCE LINES 34-35 We add a node to the pipeline to load a food balance sheet dataset. .. GENERATED FROM PYTHON SOURCE LINES 35-51 .. code-block:: Python # Load a dataset pipeline.add_node( nodes.load_dataset, name="Load Dataset", params={ "datablock_path": "food", "module": "agrifoodpy_data.food", "data_attr": "FAOSTAT", "coords": { "Item": [2731, 2511], "Year": [2019, 2020], "Region": 229}, } ) .. GENERATED FROM PYTHON SOURCE LINES 52-55 We add a node to the pipeline to store a conversion factor in the datablock. This conversion factor will be used to convert the food balance sheet data from 1000 tonnes to kgs. .. GENERATED FROM PYTHON SOURCE LINES 55-76 .. code-block:: Python # Add convertion factors to the datablock pipeline.add_node( nodes.write_to_datablock, name="Write to datablock", params={ "key": "tonnes_to_kgs", "value": 1e6, } ) # Convert food data from 1000 tonnes to kgs pipeline.add_node( model.fbs_convert, name="Convert from 1000 tonnes to kgs", params={ "fbs": "food", "convertion_arr": "tonnes_to_kgs", } ) .. GENERATED FROM PYTHON SOURCE LINES 77-79 Compute preliminary Self-Sufficiency Ratio (SSR) and Import Dependency Ratio (IDR) .. GENERATED FROM PYTHON SOURCE LINES 79-101 .. code-block:: Python # Compute IDR and SSR for food pipeline.add_node( model.SSR, name="Compute SSR for food", params={ "fbs": "food", "out_key": "SSR" } ) # Compute IDR and SSR for food pipeline.add_node( model.IDR, name="Compute IDR for food", params={ "fbs": "food", "out_key": "IDR" } ) .. GENERATED FROM PYTHON SOURCE LINES 102-103 Print the SSR and IDR values to the console .. GENERATED FROM PYTHON SOURCE LINES 103-115 .. code-block:: Python # Add a print node to display the SSR pipeline.add_node( nodes.print_datablock, name="Print SSR", params={ "key": "SSR", "method": "to_numpy", "preffix": "SSR values: ", } ) .. GENERATED FROM PYTHON SOURCE LINES 116-117 Now we can add new items to the food balance sheet dataset. .. GENERATED FROM PYTHON SOURCE LINES 117-134 .. code-block:: Python # Add an item to the food dataset pipeline.add_node( nodes.add_items, name="Add item to food", params={ "dataset": "food", "items": { "Item": 5000, "Item_name": "Cultured meat", "Item_group": "Cultured products", "Item_origin": "Synthetic origin", }, "copy_from": 2731 } ) .. GENERATED FROM PYTHON SOURCE LINES 135-136 We can also add new years to the food balance sheet dataset. .. GENERATED FROM PYTHON SOURCE LINES 136-151 .. code-block:: Python projection = np.linspace(1.1, 2.0, 10) new_years = np.arange(2021, 2031) # Extend the year range of the food dataset pipeline.add_node( nodes.add_years, name="Add years to food", params={ "dataset": "food", "years": new_years, "projection": projection, } ) .. GENERATED FROM PYTHON SOURCE LINES 152-153 We execute the pipeline to run all the nodes in order. .. GENERATED FROM PYTHON SOURCE LINES 153-156 .. code-block:: Python pipeline.run(timing=True) .. rst-class:: sphx-glr-script-out .. code-block:: none Node 1: Load Dataset, executed in 0.3422 seconds. Node 2: Write to datablock, executed in 0.0000 seconds. Node 3: Convert from 1000 tonnes to kgs, executed in 0.0003 seconds. Node 4: Compute SSR for food, executed in 0.0020 seconds. Node 5: Compute IDR for food, executed in 0.0016 seconds. SSR values: [0.9440375 0.79882324] Node 6: Print SSR, executed in 0.0002 seconds. Node 7: Add item to food, executed in 0.0053 seconds. Node 8: Add years to food, executed in 0.0037 seconds. Pipeline executed in 0.3557 seconds. .. GENERATED FROM PYTHON SOURCE LINES 157-165 .. code-block:: Python # Get the food results from the pipeline and plot using the fbs accessor food_results = pipeline.datablock["food"]["food"] f, ax = plt.subplots(figsize=(10, 6)) food_results.fbs.plot_years(show="Item_name", labels="show", ax=ax) plt.show() .. image-sg:: /examples/pipeline/images/sphx_glr_plot_balanced_scaling_food_balance_sheet_pipeline_001.png :alt: plot balanced scaling food balance sheet pipeline :srcset: /examples/pipeline/images/sphx_glr_plot_balanced_scaling_food_balance_sheet_pipeline_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 166-169 We can continue adding nodes to the pipeline, even after being executed once. To pick up where we left, we indicate which node to start execution from .. GENERATED FROM PYTHON SOURCE LINES 169-205 .. code-block:: Python # Define a year dependent linear scale starting decreasing at 2021 from 1 to # 0.5 scaling = linear_scale( 2019, 2021, 2030, 2030, 1, 0.5 ) # We will add a node to scale consumption pipeline.add_node( model.balanced_scaling, name="Balanced scaling of items", params={ "fbs": "food", "scale": scaling, "element": "food", "items": ("Item_name", "Bovine Meat"), "constant": True, "out_key": "food_scaled" } ) # Execute the recently added node pipeline.run(from_node=8, timing=True) # Get the food results from the pipeline and plot using the fbs accessor scaled_food_results = pipeline.datablock["food_scaled"]["food"] f, ax = plt.subplots(figsize=(10, 6)) scaled_food_results.fbs.plot_years(show="Item_name", labels="show", ax=ax) plt.show() .. image-sg:: /examples/pipeline/images/sphx_glr_plot_balanced_scaling_food_balance_sheet_pipeline_002.png :alt: plot balanced scaling food balance sheet pipeline :srcset: /examples/pipeline/images/sphx_glr_plot_balanced_scaling_food_balance_sheet_pipeline_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Node 9: Balanced scaling of items, executed in 0.0101 seconds. Pipeline executed in 0.0102 seconds. .. GENERATED FROM PYTHON SOURCE LINES 206-209 We can see in the scaled Food Balance Sheet that Bovine Meat consumption is reduced by half by 2030, while the total sum across all items remains constant. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.499 seconds) .. _sphx_glr_download_examples_pipeline_plot_balanced_scaling_food_balance_sheet_pipeline.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_balanced_scaling_food_balance_sheet_pipeline.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_balanced_scaling_food_balance_sheet_pipeline.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_balanced_scaling_food_balance_sheet_pipeline.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_