Sunday, December 14, 2008

Derivative in Python/Numpy


Though I don't use it very often, the following little snippet for python/numpy can be useful for the determination of an array's derivative. The image shows the use of this function to determine the derivative of a particular array to aid in a classical peak picking scheme using zero-crossings. Oddly enough there doesn't seem to be an explicit function to do this in scipy and/or numpy--or at least not that I'm aware. If anyone has a better solution please share.


def derivative(y_data):
'''calculates the 1st derivative'''
y = (y_data[1:]-y_data[:-1])
dy = y/2 #scaling factor that is not necessary but useful for my application
#one more value is added because the length
# of y and dy are 1 less than y_data
return N.append(dy,dy.mean())

TopHat Filter


I'm always on the lookout for new methods for signal processing, especially related to mass spectrometry and general noise reduction. The tophat filter is a method borrowed from the image processing community that treats a 1D graph as a 2D black and white image. It is primarily used to remove the baseline noise that may be contained in a spectrum. This can be especially important for MALDI spectra that have a high background. An example of this processing may be found in the following document which also contains a number sample figures: Beating the Noise: New Statistical Methods for Detecting Signals in MALDI-TOF Spectra below Noise Level by Tim O.F. Conrad at the Free University of Berlin (pdf). The authors of this pdf are also connected with the OpenMS/TOPP project for proteomics data processing. I've also included a small script that I put together that will perform this function in python.


import numpy as N
from scipy import ndimage#used for tophat filter

def topHat(data, factor):
'''
data -- numpy array
pntFactor determines how finely the filter is applied to data.
A point factor of 0.01 is appropriate for the tophat filter of Bruker MALDI mass spectra.
A smaller number is faster but a trade-off is imposed
'''
pntFactor = factor
struct_pts = int(round(data.size*pntFactor))
str_el = N.repeat([1], struct_pts)
tFil = ndimage.white_tophat(data, None, str_el)

return tFil

Monday, October 6, 2008

File Dialogs in PyQt4

The following code is an example of how to setup a simple open file dialog prompt. Two versions are shown using the following code, the open file dialog and the open directory dialog. More examples may be found in the PyQt4 source code examples and also from the great general tutorial found at zetcode.


#!/usr/bin/python

from PyQt4 import QtCore, QtGui
import os

class OFD_Class(QtGui.QWidget):
def __init__(self, parent = None):
QtGui.QWidget.__init__(self, parent)
self.curDir = os.getcwd()

def PyQt4_OFD(self):
directory= QtGui.QFileDialog.getExistingDirectory(self, "Select Folder", self.curDir)
if directory:
#if user selected a directory...do something
print directory
#remember that PyQt returns QStrings which are unicode object and need to be handled appropriately
print type(directory)

dataFileName = QtGui.QFileDialog.getOpenFileName(self,\
"Select File",\
self.curDir, 'X!Tandem XML (*.xml);; HDF5 File (*.h5);;SQLite Database (*.db);;All Files (*.*)')
if dataFileName:
#if file selected...do something
print dataFileName
print type(dataFileName)

def main():

import sys
app = QtGui.QApplication(sys.argv)

OFD = OFD_Class()
OFD.PyQt4_OFD()

sys.exit(app.exec_())



if __name__ == "__main__":
main()

Saturday, October 4, 2008

What is a Mach disk you ask?

This is fantastic image which illustrates the wide range of gas dynamics that occur during a rapid expansion of a gas. In this particular image the Mach disk is visible as a vertical line located just to the right of center. From the perspective of fluid dynamics, after the Mach disk the behavior of a gas can become highly turbulent (observed as the swirling formations in the image). Also of interest is the relative speed and temperature of the particles immediately preceding and following the Mach disk. Prior to the Mach disk, the Mach number of the system ais much greater than 1 and subsonic following the disk. With respect to temperature, the region following the Mach disk is characterized by a rapid increase in temperature.

http://en.wikipedia.org/wiki/Shock_diamond