# Collection of Standard Python Functions for Shock and Vibration

### From Python Wiki

Most of the functions required for shock and vibration are available in numpy, scipy and matplotlib.

The following methods are particularly relevant to shock and vibration applications. They are organized by package. Please identify the package that's required:

## Contents |

## Enthought Tool Suite (ETS)

Enthought is the company that develops and maintains scipy. I (Paul) am not sure what their business model is, but they provide a whole suite of open source Python software tools called the Enthought Tool Suite (ETS), that would appear to be very powerful if you're developing any significant scientific application based on Python. The primary packages in ETS are Traits, which offers a more structured object than the standard Python version; Traits GUI, which gives you a GUI for Traits objects; Chaco, which provides a way of developing a GUI based on 2D plots; and Mayavi, which provides a 3D plotting toolkit. I (Paul) am not enough of a serious programmer to comment on these, but if I were to develop any kind of application based on Python I would seriously consider using the suite. The Traits package, in particular, would seem to address many of the issues associated with loose typing of object attributes in Python, which I could definitely see causing problems downstream.

Note that ETS is not the same as the Enthought Python Distribution (EPD), which is a full Python distribution that contains ETS among many other tools. EPD has a number of different license options, but it is not free. The basic single user license is $199. ETS is free, and it comes with the python(x,y) distribution or can be downloaded directly from Enthought.

I did find the that the Traits GUI package was not compatible with the default setup in Spyder, which is the IDE that comes with python(x,y). When importing the traits GUI package as "from enthought.traits.ui import *" I would get the following warning:

Warning/PyQt4-Spyder (API 'QString' has already been set to version 1)

There's a discussion of this issue on the spyderlib google group. The answer was as follows:

*This behavior is due to the fact that the Enthought Tool Suite is (trying) to use PyQt API #2 but, before that, Spyder's machinery has already imported PyQt with the default API (which is API #1 with Python 2.x). To avoid this behavior, you just have to prevent Spyder from importing PyQt. Depending on the Spyder's version that you are using, it means that you have to disable the "Remove PyQt Input Hook" option and the "Matplotlib patch" option - and eventually the "Monitor" features (all *
these options are available from the preferences panel).* *

I found that by turning off "Remove PyQt Input Hook" on the Preferences/Console/External Modules form worked for me. The "Matplotlib Patch" is also there, and the "Enable monitor" feature is on the Preferences/Console/Introspection form. This seemed to work, but now I'm back to the same error again. There appears to be a fundamental incompatibility between Spyder and ETS.

## scitools

scitools sits on top of numpy, scipy and matplotlib, and includes easyviz, a unified interface to multiple Python plotting packages to provide a simple Matlab like plotting capability. Surprisingly enough this does not come with the python(x,y) distribution, but is easily downloaded and installed. I (Paul) had no problem installing it on top of my python(x,y) distribution, but haven't used it much. It was recommended in a book I have called A Primer on Scientific Programming with Python, and appears that it might be very convenient.

## matplotlib.mlab

As well as all the plotting routines that come with matplotlib, the matplotlib.mlab package offered a number of signal processing tools, which are very similar to Matlab tools. These include:

**cohere()**-Coherence (normalized cross spectral density)**csd()**- Cross spectral density uing Welch’s average periodogram**detrend(**) -Remove the mean or best fit line from an array**find()**- Return the indices where some condition is true; numpy.nonzero is similar but more general.**griddata(**) - interpolate irregularly distributed data to a regular grid.**prctile()**- find the percentiles of a sequence**prepca()**- Principal Component Analysis**psd()**- Power spectral density uing Welch’s average periodogram**rk4()**- A 4th order runge kutta integrator for 1D or ND systems**specgram()**- Spectrogram (power spectral density over segments of time)**cohere_pairs()**- Coherence over all pairs. This function is optimized to do this efficiently by caching the direct FFTs.**contiguous_regions()**- return the indices of the regions spanned by some logical mask**cross_from_below()**- return the indices where a 1D array crosses a threshold from below**cross_from_above()**- return the indices where a 1D array crosses a threshold from above

Note that matplotlib.mlab has many other functions that might prove useful in writing Python scripts.

Also note that many of the matplotlib.mlab functions also have matplotlib.pyplot equivalents. The pyplot versions plot the results.

There's a very nice collection of examples of using the various matplotlib routines.

## Relevant scipy Packages

- The scipy.signal package has lots of basic signal processing functions including convolution, B-splines, filtering, filter design, linear system simulation, waveforms, windowing, and limited wavelets.
- The scipy.stats package has tons of statistics functions. This includes data statistics such as skewness, kurtosis, etc., as well as random distributions and lots of other stuff.
- The scipy.fftpack package has functions for calculating and working with FFTs.
- The scipy.integrate package has functions for integrating and solving ordinary differential equations.
- The scipy.linalg package has most of the linear algebra routines that you might want, including eigenvalues, SVD, etc.
- The scipy.optimize package has a number of functions for numerical optimization. Most are gradient based methods, but a simulated annealing function is included
- Scipy has a number of sparse matrix formats available

There is an annual scipy conference, with both speakers and tutorials. The most recent one was SciPy 2011, held in July, 2011 in Austin Texas. There are some very good tutorials available.

## Reading and Writing Data to Excel

Python-Excel.org has a couple of packages for reading, writing and processing Excel data. There's a tutorial document on the web page. I believe that these are included with most Python distributions. They are definitely in python(x,y).

Return to: Main Page | Vibrationdata Python Scripts