2

Sorry if this has already been asked, I'm not sure exactly how to describe my question in a sentence anyway. I'm doing some work for my bioinformatics course, and I've begun to see variables declared when functions are called, instead of just passing the argument (see line 5 below).

def sobelFilter(pixmap):
  matrix = array([[-1, 0, 1],
                  [-2, 0, 2],
                  [-1, 0, 1]])
  grey = pixmap.mean(axis=2) #<---here
  edgeX = convolveMatrix2D(grey, matrix)
  edgeY = convolveMatrix2D(grey, matrix.T)
  pixmap2 = sqrt(edgeX * edgeX + edgeY * edgeY)
  normalisePixmap(pixmap2)
  return pixmap2

What is the purpose of grey = pixmap.mean(axis=2) when axis is never used again? Why not just say grey = pixmap.mean(2)?

If it's necessary for my question, this is just code we were given to use, not written by myself. Pixmap refers to this code:

def imageToPixmapRGB(img):
    img2 = img.convert("RGB")
    w,h = img2.size
    data = img2.getdata()
    pixmap = array(data, float)
    pixmap = pixmap.reshape((h,w,3))
    return pixmap

3 Answers 3

2

There is not a local variable called axis being initialized, you are just explicitly stating which parameter of the function you are passing.

You might use this if there are multiple optional parameters in a function:

Example:

In [9]: def foo(x=2, y=2):
   ...:     print(x, y)
   ...:

In [10]: foo(y=3)
2 3

If I want to change y, but leave x as default, I must specify when I call the function.

Even when you aren't setting a particular optional parameter however, there is nothing wrong with passing an argument by its keyword.

Sign up to request clarification or add additional context in comments.

1 Comment

Minor note: PEP8 spacing rules calls for no spaces around equals signs used when defining default values or passing arguments by keyword, partially to disambiguate from variable assignment (which does use spaces).
1

pixmap is a numpy array with dimension >= 2. You can test this by printing pixmap.ndim.

The line you refer to is calculating the mean of an array along an axis. In other words, axis is an argument of the mean method. See numpy.mean documentation for more details.

You are right in that the axis parameter may be omitted, i.e. arr.mean(2) is equivalent to arr.mean(axis=2). Most likely it was explicitly stated for clarity.

Comments

1

This looks a bit like the variable assignment syntax because of the =, but it is actually a very different feature:

When you call functions, you can specify which variables to set to what using the "keyword argument" syntax. This allows you to set specific parameters by name:

some_func(arg1, arg2, arg5=None)

This is very useful with default arguments:

def x(a, b, c=None, d=None):
    pass

If you wanted to set d with the normal syntax, you would have to specify an argument for c too.

x(1, 2, None, "hi")

Using the keyword argument syntax, you can omit this:

x(1, 2, d="hi")

Keyword arguments are often, like in this case, also used to make the purpose of a parameter clear to the reader. If you see:

pixmap.mean(2)

It is not immediately clear what the 2 meas. However, if you see

pixmap.mean(axis=2)

you immediately know what 2 refers to and why it's there

Also see this section of the python docs

1 Comment

Hadn't thought about its purpose in making the parameter clear, good observation to keep in mind.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.