1

I have written a simple function to do a baseline substraction of my spectrum using numpy. The code looks like this:

import numpy as np
def bas_sub(baseline_y, spectrum_y):
    try:
        len(baseline_y)==len(spectrum_y)
        spectrum_new =  np.copy(spectrum_y)-baseline_y
        return spectrum_new
    except:
        print 'Baseline and spectrum shoud have the same length.'

Where baseline and spectrum are two 1D numpy arrays. What I want my function to do is a simple length check, i.e. if the baseline and spectrum have different length the function should print the message: 'Baseline and spectrum should have the same length'. The function works well with input of equal length but it fails in printing the message with input of different length. In the last case the function output is a NoneType object. What do I do wrong? Thanks

diegus
  • 1,168
  • 2
  • 26
  • 57
  • 1
    Perhaps you want something like `assert len(baseline_y)==len(spectrum_y)`? – Warren Weckesser Nov 02 '15 at 13:27
  • 3
    Bare `except` is an extremely bad idea - see e.g. http://blog.codekills.net/2011/09/29/the-evils-of--except--/ – jonrsharpe Nov 02 '15 at 13:27
  • Please add enough code that demonstrates the unexpected behavior (i.e. something we can just copy and run). When I pass two 1-d arrays with different lengths to that function, the message is printed (and None is returned, of course). It is the line `spectrum_new = np.copy(spectrum_y)-baseline_y` that raises a `ValueError` when the lengths are different. – Warren Weckesser Nov 02 '15 at 13:32
  • Catching Asserts with a general except or even like this seems unadvisable: http://stackoverflow.com/a/11587247/102315 If you're going to go this route raise your own Exception and catch that specifically: https://docs.python.org/2/tutorial/errors.html – Alper Nov 02 '15 at 13:33

2 Answers2

1

There's nothing there which would throw an exception to be caught by that except block.

Exception handling is not the right pattern here. You should just use an if/else statement:

def bas_sub(baseline_y, spectrum_y):
    if len(baseline_y) == len(spectrum_y):
        spectrum_new = np.copy(spectrum_y) - baseline_y
        return spectrum_new
    else:
        print 'Baseline and spectrum shoud have the same length.'
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • If `baseline_y` and `spectrum_y` are 1-d arrays with different lengths, a `ValueError` exception is raised at the line `spectrum_new = np.copy(spectrum_y)-baseline_y`. (Still, I agree with the your pattern change.) – Warren Weckesser Nov 02 '15 at 13:33
  • Thank you that works as well but I still get an Nonetype object. For example if I do: a= bas_sub(bas, spec) with bas and spec having different length than the message is printed but I get a = None and that is not what I wanted. – diegus Nov 02 '15 at 16:31
  • Well, you need to return something else then, I presumed there was more code you're not showing. What do you want to happen in this case? – Daniel Roseman Nov 02 '15 at 16:31
  • Maybe that isn't really clear in my question. However, when baseline_y and spectrum_y have different length i want the funtion to print the message and exit, maybe a break steatment would do the trick in the code you used – diegus Nov 02 '15 at 19:24
  • Exit what? Currently it exits the function, which is the same as doing `return None`; so if you are assigning the result of calling the function to a variable, that variable will be assigned None. But I still can't work out what you would want to assign in that case. Or are you wanting to stop the script altogether? – Daniel Roseman Nov 02 '15 at 19:29
  • I know that it exits the function, but the problem is that the function returns None, again i don't want that. The assert in the accepted answer is what i was looking for – diegus Nov 02 '15 at 20:11
1

Another way is to use assertions:

import numpy as np
def bas_sub(baseline_y, spectrum_y):
    assert len(baseline_y)==len(spectrum_y), "Baseline and spectrum shoud have the same length."
    spectrum_new =  np.copy(spectrum_y)-baseline_y
    return spectrum_new
kurtosis
  • 1,365
  • 2
  • 12
  • 27