I have a function that fits to a 1D array of data, but this function can fail if the data is inappropriate and will raise a RuntimeError.
As my data is a 2D array I want to try it on both axis of the data if possible, otherwise just run it on the one axis that doesn't throw the error.
If I've understood the notation correctly, I try myfunc on my data in the 1st axis, if this fails go to the second, otherwise go back to the 1st. E.g. for the 1st column or row:
def myfunc(data):
# some code to do a fit, might crash
return fit_params
data = np.random.random((5,5))
try:
results_y = myfunc(data[0,:])
except RuntimeError:
results_x = myfunc(data[:,0])
# some math on the results
else:
results_y = myfunc(data[0,:])
# some math on the results
However, in some scenarios, it would work on both with no error, and it would be useful to see/compare both the results. How do I write this loop such that it will execute both if possible, otherwise will just do the one that worked? I'm thinking of either putting it all in an if loop or having multiple try statements?
Thanks in advance
elseblock here. It's executed if there is no exception, but just repeats what was done in thetryblock anyway; if there was no exception, thenresults_yalready exists.