Demonstrate usage of logging

PyPSA uses the Python standard library logging.

This script shows how to use it and control the logging messages from different modules.

Available as a Jupyter notebook at http://www.pypsa.org/examples/logging-demo.ipynb.

In [1]:
#logging.basicConfig() needs to be called BEFORE importing PyPSA

#The reason is that logging.basicConfig() can only be called
#once, and it is already called in pypsa.__init__.py; further
#calls are ignored.

#Choices are ERROR, WARNING, INFO, DEBUG

import logging
logging.basicConfig(level=logging.ERROR)

import pypsa, os
In [2]:
csv_folder_name = (os.path.dirname(pypsa.__file__)
                   + "/../examples/ac-dc-meshed/ac-dc-data/")
network = pypsa.Network(csv_folder_name=csv_folder_name)
In [3]:
out = network.lopf()
In [4]:
out = network.lpf()
In [5]:
#now turn on warnings just for OPF module
pypsa.opf.logger.setLevel(logging.WARNING)
In [6]:
out = network.lopf()
# ==========================================================
# = Solver Results                                         =
# ==========================================================
# ----------------------------------------------------------
#   Problem Information
# ----------------------------------------------------------
Problem: 
- Name: unknown
  Lower bound: -3474256.04054994
  Upper bound: -3474256.04054994
  Number of objectives: 1
  Number of constraints: 542
  Number of variables: 278
  Number of nonzeros: 1181
  Sense: minimize
# ----------------------------------------------------------
#   Solver Information
# ----------------------------------------------------------
Solver: 
- Status: ok
  Termination condition: optimal
  Statistics: 
    Branch and bound: 
      Number of bounded subproblems: 0
      Number of created subproblems: 0
  Error rc: 0
  Time: 0.0421445369720459
# ----------------------------------------------------------
#   Solution Information
# ----------------------------------------------------------
Solution: 
- number of solutions: 0
  number of solutions displayed: 0
In [7]:
#now turn on all messages for the PF module
pypsa.pf.logger.setLevel(logging.DEBUG)
In [8]:
out = network.lpf()
INFO: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='name', freq='H')
WARNING:pypsa.pf:No generators in sub-network 1, better hope power is already balanced
INFO: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='name', freq='H')
INFO: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='name', freq='H')
INFO: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='name', freq='H')
In [9]:
#now turn off all messages for the PF module again
pypsa.pf.logger.setLevel(logging.ERROR)
In [10]:
out = network.lpf()