##### Document Text Contents

Page 2

Python Programming for Biology

Bioinformatics and Beyond

Page 410

grey = pixmap.mean(axis=2)

pixmapEdge = convolveMatrix2D(grey, matrix)

normalisePixmap(pixmapEdge)

The grey pixmap with the enhanced edges has its values centred on the average

brightness. So, for example, if the average brightness of pixmapEdge is 127, the range of

values changes from 0…255 to −127…128. These centred values, either side of zero,

represent how much adjustment we will apply to sharpen the original image. Before

making the adjustment pixmapEdge is stacked so that it is three layers deep, and thus will

operate on red, green and blue.

pixmapEdge -= pixmapEdge.mean()

pixmapEdge = dstack([pixmapEdge, pixmapEdge, pixmapEdge])

The new, sharpened image is created by adding the pixmap edge adjustment to the

original pixmap. With the pixels adjusted the clip function (inbuilt into NumPy arrays) is

used to make sure that adding the pixmaps does not exceed the limits of 0 and 255.

pixmapSharp = pixmap + pixmapEdge

pixmapSharp = pixmapSharp.clip(0, 255)

return pixmapSharp

The next example is the Gaussian filter, which blurs pixels with a weighting that has a

normal (‘bell curve’) distribution (see Figure 22.4 for an illustration). For this, two values

are passed in: r is the half-width of the filter excluding the centre and sigma is the amount

of spread in the distribution. These parameters respectively control the size and strength of

the blur. Larger filters with wider distributions (i.e. influence away from the centre) will

give more blurring. It is notable that the mgrid object is used to give a range of initial grid

values for the filter, specifying the separation of each point from the centre in terms of

rows and columns; this is similar to using range() to generate a list.

def gaussFilter(pixmap, r=2, sigma=1.4):

x, y = mgrid[-r:r+1, -r:r+1]

The Gaussian function is applied by taking the row and column values (x and y),

squaring them, scaling by two times sigma squared and finally taking the negative

exponent of the sum. The exact centre row and column will be zero and so the exponent

will be at a maximum here, but the further x and y row and column values are from the

centre the smaller the value is.

s2 = 2.0 * sigma * sigma

x2 = x * x / s2

y2 = y * y / s2

matrix = exp( -(x2 + y2))

matrix /= matrix.sum()

Once the filter matrix is defined it is applied to the pixmap using convolution, to each

Page 411

of the colour components.

pixmap2 = convolveMatrix2D(pixmap, matrix)

return pixmap2

The final filter example is for edge detection and uses what is known as the Sobel

operator. In essence this is a filter that detects the intensity gradient between nearby pixels

(see Figure 18.4f). It is applied horizontally, vertically or in both directions and gives

bright pixels at those edges. As can be seen in the Python code the filter is a 3×3 matrix

where there is a line of negative numbers, then zeros, then positive numbers. This matrix

is transposed to switch between horizontal and vertical operations. The matrix means that,

for a given orientation, a transformed pixel has none of its original value, but rather a

value which represents the difference between values on either side.

def sobelFilter(pixmap):

matrix = array([[-1, 0, 1],

[-2, 0, 2],

[-1, 0, 1]])

The Sobel filter matrix is applied to the grey average of the input pixmap. This is done

twice for both orientations so we get two edge maps.

grey = pixmap.mean(axis=2)

edgeX = convolveMatrix2D(grey, matrix)

edgeY = convolveMatrix2D(grey, matrix.T)

The final pixmap of edges is then a combination of horizontal and vertical edge maps.

Taking the square root of the sum of the squares of the two edge maps means the values

will always be positive; it won’t make a difference between an edge going from light to

dark or dark to light in an image. The edge-detected pixmap is also normalised so we can

see the full range of values and finally it is returned from the function.

pixmap2 = sqrt(edgeX * edgeX + edgeY * edgeY)

normalisePixmap(pixmap2) # Put min, max at 0, 255

return pixmap2

The filter functions can all be tested with the example image, using Image.show() to see

the results, after the appropriate array conversions. Note that for the sobelFilter() output

we pass the ‘L’ mode to the PIL conversion function because it is a greyscale image, not

RGB.

from PIL import Image

img = Image.open('examples/Cells.jpg')

pixmap = imageToPixmapRGB(img)

pixmap = sharpenPixmap(pixmap)

pixmapToImage(pixmap).show()

pixmap = gaussFilter(pixmap)

Page 820

substitution matrices 215–216

calculating 258

substitution rates 272

substrings 28

support vector machines 534, 541

sys module 640

sys.argv attribute 84

text functions 622

textual data see Strings: data type

thumbnail images 366

time module 641

timing performance 163

tkinter module 570

torsion angle 157

transcription 185

translate() function 70, 167

translation, of codons 185, 190

travelling salesman problem 552

trigonometric functions 139

triple-quoted string 25

True value 47

try statement 58

T-statistic 472

T-tests 472

tuples 32

manipulation 34

membership 35

operations 621

two-tailed test 463

type checking 177

type classes 115

type() function 25, 115

Page 821

types module 177

undefined value see None value

urllib module 287, 345

urlopen() function 288, 345

variables 23

as attributes 105

naming 23

naming convention 160

scope 72

self 106

swapping 165

vector distance 516

vector space 144

Viterbi algorithm 443

while loops 52

whitespace

in Python code 22

removal 29

splitting on 84

with statement 83

XML files 90, 229

zip() function 164, 169

Z-score 470

Python Programming for Biology

Bioinformatics and Beyond

Page 410

grey = pixmap.mean(axis=2)

pixmapEdge = convolveMatrix2D(grey, matrix)

normalisePixmap(pixmapEdge)

The grey pixmap with the enhanced edges has its values centred on the average

brightness. So, for example, if the average brightness of pixmapEdge is 127, the range of

values changes from 0…255 to −127…128. These centred values, either side of zero,

represent how much adjustment we will apply to sharpen the original image. Before

making the adjustment pixmapEdge is stacked so that it is three layers deep, and thus will

operate on red, green and blue.

pixmapEdge -= pixmapEdge.mean()

pixmapEdge = dstack([pixmapEdge, pixmapEdge, pixmapEdge])

The new, sharpened image is created by adding the pixmap edge adjustment to the

original pixmap. With the pixels adjusted the clip function (inbuilt into NumPy arrays) is

used to make sure that adding the pixmaps does not exceed the limits of 0 and 255.

pixmapSharp = pixmap + pixmapEdge

pixmapSharp = pixmapSharp.clip(0, 255)

return pixmapSharp

The next example is the Gaussian filter, which blurs pixels with a weighting that has a

normal (‘bell curve’) distribution (see Figure 22.4 for an illustration). For this, two values

are passed in: r is the half-width of the filter excluding the centre and sigma is the amount

of spread in the distribution. These parameters respectively control the size and strength of

the blur. Larger filters with wider distributions (i.e. influence away from the centre) will

give more blurring. It is notable that the mgrid object is used to give a range of initial grid

values for the filter, specifying the separation of each point from the centre in terms of

rows and columns; this is similar to using range() to generate a list.

def gaussFilter(pixmap, r=2, sigma=1.4):

x, y = mgrid[-r:r+1, -r:r+1]

The Gaussian function is applied by taking the row and column values (x and y),

squaring them, scaling by two times sigma squared and finally taking the negative

exponent of the sum. The exact centre row and column will be zero and so the exponent

will be at a maximum here, but the further x and y row and column values are from the

centre the smaller the value is.

s2 = 2.0 * sigma * sigma

x2 = x * x / s2

y2 = y * y / s2

matrix = exp( -(x2 + y2))

matrix /= matrix.sum()

Once the filter matrix is defined it is applied to the pixmap using convolution, to each

Page 411

of the colour components.

pixmap2 = convolveMatrix2D(pixmap, matrix)

return pixmap2

The final filter example is for edge detection and uses what is known as the Sobel

operator. In essence this is a filter that detects the intensity gradient between nearby pixels

(see Figure 18.4f). It is applied horizontally, vertically or in both directions and gives

bright pixels at those edges. As can be seen in the Python code the filter is a 3×3 matrix

where there is a line of negative numbers, then zeros, then positive numbers. This matrix

is transposed to switch between horizontal and vertical operations. The matrix means that,

for a given orientation, a transformed pixel has none of its original value, but rather a

value which represents the difference between values on either side.

def sobelFilter(pixmap):

matrix = array([[-1, 0, 1],

[-2, 0, 2],

[-1, 0, 1]])

The Sobel filter matrix is applied to the grey average of the input pixmap. This is done

twice for both orientations so we get two edge maps.

grey = pixmap.mean(axis=2)

edgeX = convolveMatrix2D(grey, matrix)

edgeY = convolveMatrix2D(grey, matrix.T)

The final pixmap of edges is then a combination of horizontal and vertical edge maps.

Taking the square root of the sum of the squares of the two edge maps means the values

will always be positive; it won’t make a difference between an edge going from light to

dark or dark to light in an image. The edge-detected pixmap is also normalised so we can

see the full range of values and finally it is returned from the function.

pixmap2 = sqrt(edgeX * edgeX + edgeY * edgeY)

normalisePixmap(pixmap2) # Put min, max at 0, 255

return pixmap2

The filter functions can all be tested with the example image, using Image.show() to see

the results, after the appropriate array conversions. Note that for the sobelFilter() output

we pass the ‘L’ mode to the PIL conversion function because it is a greyscale image, not

RGB.

from PIL import Image

img = Image.open('examples/Cells.jpg')

pixmap = imageToPixmapRGB(img)

pixmap = sharpenPixmap(pixmap)

pixmapToImage(pixmap).show()

pixmap = gaussFilter(pixmap)

Page 820

substitution matrices 215–216

calculating 258

substitution rates 272

substrings 28

support vector machines 534, 541

sys module 640

sys.argv attribute 84

text functions 622

textual data see Strings: data type

thumbnail images 366

time module 641

timing performance 163

tkinter module 570

torsion angle 157

transcription 185

translate() function 70, 167

translation, of codons 185, 190

travelling salesman problem 552

trigonometric functions 139

triple-quoted string 25

True value 47

try statement 58

T-statistic 472

T-tests 472

tuples 32

manipulation 34

membership 35

operations 621

two-tailed test 463

type checking 177

type classes 115

type() function 25, 115

Page 821

types module 177

undefined value see None value

urllib module 287, 345

urlopen() function 288, 345

variables 23

as attributes 105

naming 23

naming convention 160

scope 72

self 106

swapping 165

vector distance 516

vector space 144

Viterbi algorithm 443

while loops 52

whitespace

in Python code 22

removal 29

splitting on 84

with statement 83

XML files 90, 229

zip() function 164, 169

Z-score 470