.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gen_tutorials/tutorial_comparison.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_gen_tutorials_tutorial_comparison.py: .. _tutorial_comparison: ================================= Comparison ================================= This tutorial will guide you to creating your own comparison plots. For a more in-depth view, please refer to the :ref:`how_tos` and the API docs :ref:`api-reference-api` .. GENERATED FROM PYTHON SOURCE LINES 13-16 Required Imports ================ First, let's add the required imports to the top of the script. .. GENERATED FROM PYTHON SOURCE LINES 16-22 .. code-block:: Python import pathlib as _pl from pytrnsys_process import api .. GENERATED FROM PYTHON SOURCE LINES 23-37 Preparation ================================================= First, you will need to download the `example data. `_ Extract the data into a project folder you will use for this tutorial. You should end up with a folder structure looking like this. | root | ├─ example_data | ├─ tutorials | ├─ tutorial_comparison.py For the rest of this tutorial, you should work in the last file: "tutorial_comparison.py" .. GENERATED FROM PYTHON SOURCE LINES 40-47 Defining comparison steps ========================= This comparison step will loop through all simulations. It will use the hourly and monthly dataframes to do calculations. The results will be stored in the scalar dataframe that all simulations share. At the end we will return the modified :class:`pytrnsys_process.api.SimulationsData` object. Reason for this is mentioned in the next step. .. GENERATED FROM PYTHON SOURCE LINES 47-96 .. code-block:: Python def do_calc(simulations_data: api.SimulationsData): for sim_name, sim in simulations_data.simulations.items(): hourly_data = sim.hourly max_ice_ratio = hourly_data["VIceRatio"].max() simulations_data.scalar.loc[sim_name, "VIceRatioMax"] = max_ice_ratio demand_columns = [ "qSysOut_QSnk131Demand", "qSysOut_QSnk183Demand", "qSysOut_QSnk191Demand", "qSysOut_QSnk225Demand", "qSysOut_QSnk243Demand", "qSysOut_QSnk266Demand", "qSysOut_QSnk322Demand", "qSysOut_QSnk335Demand", "qSysOut_QSnk358Demand", "qSysOut_QSnk417Demand", "qSysOut_QSnk448Demand", "qSysOut_QSnk469Demand", "qSysOut_QSnk488Demand", "qSysOut_QSnk524Demand", "qSysOut_QSnk539Demand", "qSysOut_QSnk558Demand", "qSysOut_QSnk579Demand", "qSysOut_QSnk60Demand", "qSysOut_QSnk85Demand", ] # Unit conversion factor: kWh to GWh kwh_to_gwh = 1e-6 # Process monthly data monthly_df = sim.monthly monthly_df["total_demand_GWh"] = ( monthly_df[demand_columns].sum(axis=1) * kwh_to_gwh ) # Calculate yearly total (excluding first 2 months) yearly_total = int(monthly_df["total_demand_GWh"].iloc[2::].sum()) simulations_data.scalar.loc[sim_name, "yearly_demand_GWh"] = ( yearly_total ) return simulations_data .. GENERATED FROM PYTHON SOURCE LINES 97-102 Chain multiple comparison steps =============================== By chaining comparison steps together, we ensure that if a dependent step fails, the entire process fails. In this step, we call the previously defined step to validate our data before generating plots. This way, if an error occurs during the calculation, we avoid plotting incorrect data. .. GENERATED FROM PYTHON SOURCE LINES 102-118 .. code-block:: Python def plot_comparison(simulations_data: api.SimulationsData): simulations_data = do_calc(simulations_data) fig, _ = api.scatter_plot( simulations_data.scalar, "VIceSscaled", "VIceRatioMax", group_by_color="yearly_demand_GWh", group_by_marker="ratioDHWtoSH_allSinks", ) fig.show() .. GENERATED FROM PYTHON SOURCE LINES 119-124 Running comparison steps =============================== There are different ways of providing data to the do_comparison function. In this tutorial we will provide the path to our result folder. For other ways checkout see :meth:`pytrnsys_process.api.do_comparison` .. GENERATED FROM PYTHON SOURCE LINES 124-128 .. code-block:: Python if __name__ == "__main__": path_to_sim = _pl.Path("../example_data/ice/") api.do_comparison([plot_comparison], results_folder=path_to_sim) .. image-sg:: /gen_tutorials/images/sphx_glr_tutorial_comparison_001.png :alt: tutorial comparison :srcset: /gen_tutorials/images/sphx_glr_tutorial_comparison_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.213 seconds) .. _sphx_glr_download_gen_tutorials_tutorial_comparison.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: tutorial_comparison.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: tutorial_comparison.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: tutorial_comparison.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_