Visual logging

trunk/src/visual_logging.py

(Requires: matplotlib)

The visual_logging module of NeuroTools works like the standard library's  `logging` module, but instead of writing strings to a file, it plots data.

I find logging very useful for debugging/verifying when running simulations or analyzing the results, but often the variables I am interested in are large arrays, too large to fill a log file with, and it is not always easy to tell if the array values are reasonable just by looking at a long list of numbers.

In this situation, I really want to plot the array, and look at the resulting graph, but I don't want to clutter up my code with lots of pylab calls.

visual_logging lets you plot data with a single line of code, and gathers all the plots from a single run in a zip file.

As with the logging module, the graphs are only created if the global logging level is set low enough.

A simple example:

  >>> import numpy
  >>> import NeuroTools.visual_logging as vl
  >>> vl.basicConfig("vl_test.zip", level=vl.DEBUG)
  >>> xdata = numpy.arange(0, 2*numpy.pi, 0.02*numpy.pi)
  >>> vl.debug(numpy.sin(xdata), xdata, 'x', 'sin(x)', 'visual_logging test 1')
  >>> vl.debug(0.5*numpy.sin(2*xdata-0.3), xdata, 'x', 'sin(2x-0.3)/2')
  >>> vl.debug(numpy.sqrt(xdata), xdata, 'x', 'sqrt(x)')

This will create a file "vl_test.zip" in your working directory, containing three PNG files whose names are timestamps (arguably the filenames should also include the log level. This needs to be added).

If you used level=vl.INFO in the basicConfig() call, no graphs would be created, making it easy to turn debugging on/off without having to go through your code and comment-out a lot of lines.

As with the standard logging module, the main functions in visual_logging are debug(), info(), warning() and error(). The latter is useful for giving more information when an Exception is raised.