python modul for spice data and conversion utility

Abstract

This page is about how to read spice simulation data with a python modul. The data can be plotted, analysed or being converted to the hdf5 file format.

Reading spice data

The import filter spice_read reads all kinds of spice files and returns a list of spice plots. Commandline execution of the python script prints a summary of the data in the file:
werner@werner-amd64:~/oss/python_spice/test> ../src/spice_read.py complicated_binary.raw
The file: "complicated_binary.raw" contains the following plots:
  Plot 0 with the attributes
    Title:  * simulation de RC2
    Date:  Mon Sep  3 19:53:05  2007
    Plotname:  AC Analysis
    Plottype:  plottype undefined
    The Scale vector has the following properties:
      Name:  frequency
      Type:  frequency
      Vector-Length:  201
      Vector-Type:  float64
    Data vector 0 has the following properties:
      Name:  ac.v(n2)
      Type:  voltage
      Vector-Length:  201
      Vector-Type:  complex128
    Data vector 1 has the following properties:
      Name:  ac.v(n1)
      Type:  voltage
      Vector-Length:  201
      Vector-Type:  complex128
    Data vector 2 has the following properties:
      Name:  ac.v(n2)
      Type:  voltage
      Vector-Length:  201
      Vector-Type:  complex128
  Plot 1 with the attributes
    Title:  * simulation de RC2
    Date:  Mon Sep  3 19:53:05  2007
    Plotname:  DC transfer characteristic
    Plottype:  plottype undefined
    The Scale vector has the following properties:
      Name:  v-sweep
      Type:  voltage
      Vector-Length:  501
      Vector-Type:  float64
    Data vector 0 has the following properties:
      Name:  dc.v(n1)
      Type:  voltage
      Vector-Length:  501
      Vector-Type:  float64
    Data vector 1 has the following properties:
      Name:  dc.v(n2)
      Type:  voltage
      Vector-Length:  501
      Vector-Type:  float64
  Plot 2 with the attributes
    Title:  * simulation de RC2
    Date:  Mon Sep  3 19:53:05  2007
    Plotname:  Transient Analysis
    Plottype:  plottype undefined
    The Scale vector has the following properties:
      Name:  time
      Type:  time
      Vector-Length:  1069
      Vector-Type:  float64
    Data vector 0 has the following properties:
      Name:  tran.v(n1)
      Type:  voltage
      Vector-Length:  1069
      Vector-Type:  float64 

Plotting spice data

You can plot the data using pylab from the python module matplotlib. E.g. the third plot of the file can be plotted with a short script using the spice_read class:
#!/usr/bin/python

from pylab import *
sys.path.append("../src/")
import spice_read

plotlist = spice_read.spice_read("../test/complicated_binary.raw").get_plots()

plot2 = plotlist[2]
x = plot2.get_scalevector().get_data()

for v in plot2.get_datavectors():
    plot(x, v.get_data(), label=v.name)

title("Plottest")
grid()
legend()
savefig("short_test.png")
close()
    

The script results in a simple plot written into a bitmap file:

Storing the data in hdf5 format

If you have lots of simulations and you want to store and analyse the data, hdf5 is a great data format. You can store large arrays of data in a hierarchical structure or store data in large data tables. This format can be read by many programs.

The script spice2hdf5 reads spice files and writes them into a hdf5 file. The script can create tables of each simulation or store each vector of the simulation as an array. The command line interface offers some options to store the spice plots at different places in the hdf5 file.

werner@linux-m82i:~/oss/python_spice/src> ./spice2hdf5.py -h
spice2hdf5 version 0.0.2   (C) Werner Hoch
usage: ./spice2hdf5.py [options], spicefile, [spicefile2, ..]
  -h --help: print help information
  -v --verbose: print debug messages to stdout
  -o --outfile: specify the hdf5 output filename (default: out.hdf5)
  -p --pathprefix: location to store the spice data
  -f --format: whether to store the data as single vectors or table
               (default: table)

You can now store data in a hdf5 file

> spice2hdf5.py --pathprefix=/vectors -f vectors *raw
> spice2hdf5.py --pathprefix=/tables -f table -o out.hdf5 *raw
> spice2hdf5.py --pathprefix=/ac/myname ac_binary.raw
> spice2hdf5.py -f vectors -o out.hdf5 dc_binary.raw

You can use the program ViTables to view the data stored in the hdf5 file and read it using the python tables module. Here is a screenshot from ViTables with some spice data:

Download

This is a combined package that contains the spice import filter spice_read, the spice2hdf5 conversion utility and the differential evolution optimizer.

programming language: python
natural language: english
licence: the DE-solver has a BSD-like licence, please read the head of the src/diffev.py file. The spice import filter src/spice_read.py and the conversion utility src/spice2hdf5.py uses GPL2 all other files and examples are released into public domain.
download: python_spice-0.0.3.tar.gz

Links

The simulator homepage: ngspice
The scientific python page: scipy / numpy
The plotting package: matplotlib
The hdf5 file format: HDF5 Group
The python module to read hdf5 data: pytables
The programm to view hdf5 data files: vitables

Werner Hoch
Last modified: Sun Aug 21 10:19:17 CEST 2011