Demonstrate usage of logging

Note

You can download this example as a Jupyter notebook or start it in interactive mode.

Demonstrate usage of logging#

PyPSA uses the Python standard library logging. This notebook shows how to use it and control the logging messages from different modules.

One can set the logging level to different values like ERROR, WARNING, INFO, DEBUG. This works independently for separate module.

We start by setting the basic logging level to ERROR.

[1]:
import pypsa, os
import logging

logging.basicConfig(level=logging.ERROR)
ERROR 1: PROJ: proj_create_from_database: Open of /home/docs/checkouts/readthedocs.org/user_builds/pypsa/conda/latest/share/proj failed
[2]:
network = pypsa.examples.ac_dc_meshed(from_master=True)
[3]:
out = network.optimize()
/home/docs/checkouts/readthedocs.org/user_builds/pypsa/conda/latest/lib/python3.11/site-packages/pypsa/optimization/optimize.py:357: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.
The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.

For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.


  n.df(c)[attr + "_opt"].update(df)

Now turn on infos just for OPF module.

[4]:
pypsa.opf.logger.setLevel(logging.INFO)
[5]:
out = network.optimize()
/home/docs/checkouts/readthedocs.org/user_builds/pypsa/conda/latest/lib/python3.11/site-packages/pypsa/optimization/optimize.py:357: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.
The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.

For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.


  n.df(c)[attr + "_opt"].update(df)

Now turn on warnings just for OPF module

[6]:
pypsa.opf.logger.setLevel(logging.WARNING)
[7]:
out = network.optimize()
/home/docs/checkouts/readthedocs.org/user_builds/pypsa/conda/latest/lib/python3.11/site-packages/pypsa/optimization/optimize.py:357: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.
The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.

For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.


  n.df(c)[attr + "_opt"].update(df)

Now turn on all messages for the PF module

[8]:
pypsa.pf.logger.setLevel(logging.DEBUG)
[9]:
out = network.lpf()
DEBUG:pypsa.pf:Slack bus for sub-network 0 is Manchester
DEBUG:pypsa.pf:Slack bus for sub-network 1 is Norwich DC
DEBUG:pypsa.pf:Slack bus for sub-network 2 is Frankfurt
DEBUG:pypsa.pf:No slack generator found in sub-network 3, using Norway Wind as the slack generator
DEBUG:pypsa.pf:Slack bus for sub-network 3 is Norway
DEBUG:pypsa.pf:Slack bus for sub-network 0 is Manchester
INFO:pypsa.pf:Performing linear load-flow on AC sub-network SubNetwork 0 for snapshot(s) DatetimeIndex(['2015-01-01 00:00:00', '2015-01-01 01:00:00',
               '2015-01-01 02:00:00', '2015-01-01 03:00:00',
               '2015-01-01 04:00:00', '2015-01-01 05:00:00',
               '2015-01-01 06:00:00', '2015-01-01 07:00:00',
               '2015-01-01 08:00:00', '2015-01-01 09:00:00'],
              dtype='datetime64[ns]', name='snapshot', freq=None)
DEBUG:pypsa.pf:Slack bus for sub-network 1 is Norwich DC
INFO:pypsa.pf:Performing linear load-flow on DC sub-network SubNetwork 1 for snapshot(s) DatetimeIndex(['2015-01-01 00:00:00', '2015-01-01 01:00:00',
               '2015-01-01 02:00:00', '2015-01-01 03:00:00',
               '2015-01-01 04:00:00', '2015-01-01 05:00:00',
               '2015-01-01 06:00:00', '2015-01-01 07:00:00',
               '2015-01-01 08:00:00', '2015-01-01 09:00:00'],
              dtype='datetime64[ns]', name='snapshot', freq=None)
DEBUG:pypsa.pf:Slack bus for sub-network 2 is Frankfurt
INFO:pypsa.pf:Performing linear load-flow on AC sub-network SubNetwork 2 for snapshot(s) DatetimeIndex(['2015-01-01 00:00:00', '2015-01-01 01:00:00',
               '2015-01-01 02:00:00', '2015-01-01 03:00:00',
               '2015-01-01 04:00:00', '2015-01-01 05:00:00',
               '2015-01-01 06:00:00', '2015-01-01 07:00:00',
               '2015-01-01 08:00:00', '2015-01-01 09:00:00'],
              dtype='datetime64[ns]', name='snapshot', freq=None)
DEBUG:pypsa.pf:Slack bus for sub-network 3 is Norway
INFO:pypsa.pf:Performing linear load-flow on AC sub-network SubNetwork 3 for snapshot(s) DatetimeIndex(['2015-01-01 00:00:00', '2015-01-01 01:00:00',
               '2015-01-01 02:00:00', '2015-01-01 03:00:00',
               '2015-01-01 04:00:00', '2015-01-01 05:00:00',
               '2015-01-01 06:00:00', '2015-01-01 07:00:00',
               '2015-01-01 08:00:00', '2015-01-01 09:00:00'],
              dtype='datetime64[ns]', name='snapshot', freq=None)

Now turn off all messages for the PF module again

[10]:
pypsa.pf.logger.setLevel(logging.ERROR)
[11]:
out = network.lpf()