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 logging

import pypsa

logging.basicConfig(level=logging.ERROR)
[2]:
network = pypsa.examples.ac_dc_meshed(from_master=True)
[3]:
out = network.optimize()
Running HiGHS 1.7.2 (git hash: 184e327): Copyright (c) 2024 HiGHS under MIT licence terms
Coefficient ranges:
  Matrix [1e-02, 1e+00]
  Cost   [9e-03, 3e+03]
  Bound  [2e+07, 2e+07]
  RHS    [9e-01, 1e+03]
Presolving model
391 rows, 187 cols, 930 nonzeros  0s
305 rows, 101 cols, 1042 nonzeros  0s
303 rows, 99 cols, 1058 nonzeros  0s
Presolve : Reductions: rows 303(-165); columns 99(-89); elements 1058(+51)
Solving the presolved LP
Using EKK dual simplex solver - serial
  Iteration        Objective     Infeasibilities num(sum)
          0    -2.1204510016e+07 Pr: 102(98953); Du: 0(4.73182e-11) 0s
        126    -3.4742560406e+06 Pr: 0(0) 0s
Solving the original LP from the solution after postsolve
Model   status      : Optimal
Simplex   iterations: 126
Objective value     : -3.4742560406e+06
HiGHS run time      :          0.00
Writing the solution to /tmp/linopy-solve-1u8yf0i3.sol

Now turn on infos just for optimization module.

[4]:
pypsa.optimization.optimize.logger.setLevel(logging.INFO)
[5]:
out = network.optimize()
INFO:pypsa.optimization.optimize:The shadow-prices of the constraints Generator-ext-p-lower, Generator-ext-p-upper, Line-ext-s-lower, Line-ext-s-upper, Link-ext-p-lower, Link-ext-p-upper, Kirchhoff-Voltage-Law were not assigned to the network.
Running HiGHS 1.7.2 (git hash: 184e327): Copyright (c) 2024 HiGHS under MIT licence terms
Coefficient ranges:
  Matrix [1e-02, 1e+00]
  Cost   [9e-03, 3e+03]
  Bound  [2e+07, 2e+07]
  RHS    [9e-01, 1e+03]
Presolving model
391 rows, 187 cols, 930 nonzeros  0s
305 rows, 101 cols, 1042 nonzeros  0s
303 rows, 99 cols, 1058 nonzeros  0s
Presolve : Reductions: rows 303(-165); columns 99(-89); elements 1058(+51)
Solving the presolved LP
Using EKK dual simplex solver - serial
  Iteration        Objective     Infeasibilities num(sum)
          0    -2.1204510016e+07 Pr: 102(98953); Du: 0(4.73182e-11) 0s
        126    -3.4742560406e+06 Pr: 0(0) 0s
Solving the original LP from the solution after postsolve
Model   status      : Optimal
Simplex   iterations: 126
Objective value     : -3.4742560406e+06
HiGHS run time      :          0.00
Writing the solution to /tmp/linopy-solve-8iyp27vj.sol

Now turn on warnings just for optimization module

[6]:
pypsa.optimization.optimize.logger.setLevel(logging.WARNING)
[7]:
out = network.optimize()
Running HiGHS 1.7.2 (git hash: 184e327): Copyright (c) 2024 HiGHS under MIT licence terms
Coefficient ranges:
  Matrix [1e-02, 1e+00]
  Cost   [9e-03, 3e+03]
  Bound  [2e+07, 2e+07]
  RHS    [9e-01, 1e+03]
Presolving model
391 rows, 187 cols, 930 nonzeros  0s
305 rows, 101 cols, 1042 nonzeros  0s
303 rows, 99 cols, 1058 nonzeros  0s
Presolve : Reductions: rows 303(-165); columns 99(-89); elements 1058(+51)
Solving the presolved LP
Using EKK dual simplex solver - serial
  Iteration        Objective     Infeasibilities num(sum)
          0    -2.1204510016e+07 Pr: 102(98953); Du: 0(4.73182e-11) 0s
        126    -3.4742560406e+06 Pr: 0(0) 0s
Solving the original LP from the solution after postsolve
Model   status      : Optimal
Simplex   iterations: 126
Objective value     : -3.4742560406e+06
HiGHS run time      :          0.00
Writing the solution to /tmp/linopy-solve-e0d9q8lp.sol

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()