|
| 1 | +########### |
| 2 | +Statistics |
| 3 | +########### |
| 4 | + |
| 5 | +.. seealso:: |
| 6 | + |
| 7 | + See :doc:`/api/statistics` for the API reference. |
| 8 | + |
| 9 | +Overview |
| 10 | +========= |
| 11 | + |
| 12 | +The ``pypsa.statistics`` module provides an accessor to the ``pypsa.Network`` object |
| 13 | +via ``n.statistics``, enabling users to efficiently calculate key network metrics. |
| 14 | +This module is designed to simplify the analysis of network data by abstracting complex |
| 15 | +calculations into more accessible methods. It is particularly useful for users who |
| 16 | +prefer to avoid manual calculations and directly obtain relevant statistical data |
| 17 | +about the network. |
| 18 | + |
| 19 | + |
| 20 | +Available Metrics |
| 21 | +================== |
| 22 | + |
| 23 | +.. autosummary:: |
| 24 | + |
| 25 | + ~pypsa.statistics.StatisticsAccessor.capex |
| 26 | + ~pypsa.statistics.StatisticsAccessor.installed_capex |
| 27 | + ~pypsa.statistics.StatisticsAccessor.expanded_capex |
| 28 | + ~pypsa.statistics.StatisticsAccessor.optimal_capacity |
| 29 | + ~pypsa.statistics.StatisticsAccessor.installed_capacity |
| 30 | + ~pypsa.statistics.StatisticsAccessor.expanded_capacity |
| 31 | + ~pypsa.statistics.StatisticsAccessor.opex |
| 32 | + ~pypsa.statistics.StatisticsAccessor.supply |
| 33 | + ~pypsa.statistics.StatisticsAccessor.withdrawal |
| 34 | + ~pypsa.statistics.StatisticsAccessor.transmission |
| 35 | + ~pypsa.statistics.StatisticsAccessor.energy_balance |
| 36 | + ~pypsa.statistics.StatisticsAccessor.curtailment |
| 37 | + ~pypsa.statistics.StatisticsAccessor.capacity_factor |
| 38 | + ~pypsa.statistics.StatisticsAccessor.revenue |
| 39 | + ~pypsa.statistics.StatisticsAccessor.market_value |
| 40 | + |
| 41 | +Usage |
| 42 | +----------------- |
| 43 | + |
| 44 | +To utilize the statistics module, instantiate the accessor from a PyPSA network object |
| 45 | +as follows: |
| 46 | + |
| 47 | +.. doctest:: |
| 48 | + |
| 49 | + >>> n = pypsa.Network() |
| 50 | + |
| 51 | +You can then call specific methods from the ``statistics`` property to calculate various |
| 52 | +metrics, such as installed capacity or operational expenditure. For example: |
| 53 | + |
| 54 | +.. doctest:: |
| 55 | + |
| 56 | + >>> installed_capacity = n.statistics.installed_capacity() |
| 57 | + >>> installed_capacity |
| 58 | + Component carrier |
| 59 | + Generator gas 100.0 |
| 60 | + wind 50.0 |
| 61 | + solar 30.0 |
| 62 | + Name: optimal_capacity, dtype: float64 |
| 63 | + |
| 64 | + >>> opex = n.statistics.opex() |
| 65 | + >>> opex |
| 66 | + Component carrier |
| 67 | + Generator gas 1000.0 |
| 68 | + wind 0.0 |
| 69 | + solar 0.0 |
| 70 | + Name: opex, dtype: float64 |
| 71 | + |
| 72 | + |
| 73 | +Have a look at the method's docstring for more details on the arguments. |
| 74 | + |
| 75 | +For example, to calculate the capital expenditure (capex) as a sum for all components, |
| 76 | +you can use: |
| 77 | + |
| 78 | +.. doctest:: |
| 79 | + |
| 80 | + >>> n.statistics.capex(aggregate_groups='sum') |
| 81 | + |
| 82 | +Similarly, to calculate the operational expenditure for all Link components, which |
| 83 | +connect to hydrogen (H2) buses: |
| 84 | + |
| 85 | +.. doctest:: |
| 86 | + |
| 87 | + >>> n.statistics.opex(comps=["Link"], bus_carrier="H2") |
| 88 | + |
| 89 | +Statistic groupers |
| 90 | +=================== |
| 91 | + |
| 92 | +Groupers can be used via the ``groupby`` argument in the statistic methods. |
| 93 | + |
| 94 | +All default groupers are defined in the :class:`pypsa.statistics.grouping.Groupers` |
| 95 | +class and currently included are, grouping by .. |
| 96 | + |
| 97 | +* :meth:`carrier <pypsa.statistics.grouping.Groupers.carrier>` |
| 98 | +* :meth:`bus_carrier <pypsa.statistics.grouping.Groupers.bus_carrier>` |
| 99 | +* :meth:`name <pypsa.statistics.grouping.Groupers.name>` |
| 100 | +* :meth:`bus <pypsa.statistics.grouping.Groupers.bus>` |
| 101 | +* :meth:`country <pypsa.statistics.grouping.Groupers.country>` |
| 102 | +* :meth:`unit <pypsa.statistics.grouping.Groupers.unit>` |
| 103 | +* A list of registered groupers can be accessed via |
| 104 | + :meth:`pypsa.statistics.groupers.list_groupers <pypsa.statistics.grouping.Groupers.list_groupers>` |
| 105 | + |
| 106 | +Custom groupers can be registered on module level via |
| 107 | +:meth:`pypsa.statistics.groupers.add_grouper <pypsa.statistics.grouping.Groupers.add_grouper>`. |
| 108 | +The key will be used as identifier in the ``groupby`` argument. |
| 109 | + |
| 110 | +Usage |
| 111 | +----------------- |
| 112 | + |
| 113 | +.. doctest:: |
| 114 | + |
| 115 | + >>> groupers = n.statistics.groupers |
| 116 | + >>> n.statistics.capex(groupby=groupers.carrier) |
| 117 | + carrier |
| 118 | + gas 10000.0 |
| 119 | + wind 5000.0 |
| 120 | + solar 3000.0 |
| 121 | + Name: capex, dtype: float64 |
| 122 | + |
| 123 | + >>> # or simply |
| 124 | + >>> n.statistics.capex(groupby='carrier') |
| 125 | + carrier |
| 126 | + gas 10000.0 |
| 127 | + wind 5000.0 |
| 128 | + solar 3000.0 |
| 129 | + Name: capex, dtype: float64 |
| 130 | + |
| 131 | + |
| 132 | +Groupers can also be used to create multiindexed groupers. For example, to group by |
| 133 | +bus and carrier: |
| 134 | + |
| 135 | +.. code-block:: python |
| 136 | + |
| 137 | + groupers = n.statistics.groupers |
| 138 | + n.statistics.capex(groupby=groupers['bus', 'carrier']) |
| 139 | + # or simply |
| 140 | + n.statistics.capex(groupby=['bus', 'carrier']) |
| 141 | +
|
| 142 | +.. autosummary:: |
| 143 | + |
| 144 | + ~pypsa.statistics.grouping.Groupers.add_grouper |
| 145 | + ~pypsa.statistics.grouping.Groupers.list_groupers |
| 146 | + ~pypsa.statistics.grouping.Groupers.carrier |
| 147 | + ~pypsa.statistics.grouping.Groupers.bus_carrier |
| 148 | + ~pypsa.statistics.grouping.Groupers.name |
| 149 | + ~pypsa.statistics.grouping.Groupers.bus |
| 150 | + ~pypsa.statistics.grouping.Groupers.country |
| 151 | + ~pypsa.statistics.grouping.Groupers.unit |
| 152 | + |
| 153 | + |
| 154 | +Advanced Examples and Visualization |
| 155 | +======================================= |
| 156 | + |
| 157 | +In addition to basic usage, the statistics module offers advanced functionality for |
| 158 | +in-depth analysis and visualization of network metrics. Here are some advanced examples |
| 159 | +and visualization techniques: |
| 160 | + |
| 161 | +1. **Comparative Analysis**: Users can compare different scenarios or network |
| 162 | +configurations by calculating metrics for each scenario and visualizing the results |
| 163 | +side by side. For example, compare the installed capacity of renewable energy sources |
| 164 | +in two different network models. |
| 165 | + |
| 166 | +2. **Temporal Analysis**: Utilize the aggregate_time parameter to analyze temporal |
| 167 | +variations in network metrics. Plotting time series data can reveal patterns |
| 168 | +and trends over time, such as seasonal variations in energy supply or demand. |
| 169 | + |
| 170 | +3. **Geospatial Visualization**: If the network includes geospatial data, users can |
| 171 | +create maps to visualize the distribution of network components and metrics |
| 172 | +geographically. This can be particularly useful for understanding spatial dependencies |
| 173 | +and identifying areas with high or low capacity utilization. |
| 174 | + |
| 175 | +4. **Scenario Planning**: Explore different scenarios or what-if analyses by adjusting |
| 176 | +input parameters and observing the impact on network metrics. For example, |
| 177 | +simulate the effect of increasing renewable energy penetration on curtailment and |
| 178 | +market value. |
| 179 | + |
| 180 | +5. **Interactive Dashboards**: Develop interactive dashboards using visualization |
| 181 | +libraries like Plotly or Bokeh to allow users to dynamically explore network |
| 182 | +metrics and drill down into specific details. Dashboards can provide a user-friendly |
| 183 | +interface for exploring complex network data. |
| 184 | + |
| 185 | +Example Code Snippet: |
| 186 | + |
| 187 | +.. doctest:: |
| 188 | + |
| 189 | + >>> import matplotlib.pyplot as plt |
| 190 | + >>> # Calculate installed capacity |
| 191 | + >>> installed_capacity = n.statistics.installed_capacity().droplevel(0) |
| 192 | + >>> # Plot installed capacity by component type |
| 193 | + >>> installed_capacity.plot(kind='bar', figsize=(10, 6)) |
| 194 | + >>> plt.title('Installed Capacity by Component Type') |
| 195 | + >>> plt.xlabel('Component Type') |
| 196 | + >>> plt.ylabel('Installed Capacity (MW)') |
| 197 | + >>> plt.xticks(rotation=45) |
| 198 | + >>> plt.grid(axis='y') |
| 199 | + >>> plt.tight_layout() |
| 200 | + >>> plt.savefig('statistics_advanced_usage.png') |
| 201 | + >>> plt.close() |
| 202 | + |
| 203 | +.. figure:: ../img/statistics_advanced_usage.png |
| 204 | + :alt: Installed Capacity by Component Type |
| 205 | + :width: 100% |
| 206 | + |
| 207 | + Installed capacity by component type. |
| 208 | + |
| 209 | +This code snippet calculates the installed capacity for each component type in the |
| 210 | +network and visualizes the results using a bar plot. Similar visualizations can |
| 211 | +be created for other metrics, providing valuable insights into the network's composition |
| 212 | +and characteristics. |
0 commit comments