1
我有一個數字代碼的拼圖,可能由於各種原因失敗,我想用numpy.NAN替換輸出值,當它這樣做的時候,事情繼續前進。我嘗試在brentq中嘗試打包調用:except:語句,但它仍然崩潰我的代碼在同一個異常。嘗試除了沒有捕捉異常
def someotherfunction(stuff):
self.energy2ph = lambda e: self.brentq_fails_to_NAN(ph2offset_energy, 0., max_ph, args=(e,))
def brentq_fails_to_NAN(self, *args):
''' this simply calls scipy.optimize.brentq with the exact same arguments,
but in the case of an error it returns numpy.NAN instead of throwing an exception '''
try:
return scipy.optimize.brentq(*args)
except:
print 'returning numpy.NAN instead of throwing an exception for energy2ph'
return numpy.NAN
我得到一個回溯到我的try語句的異常。我的理解是應該運行下,除了代碼:在這種情況下
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mass/calibration/energy_calibration.pyc in name2ph(self, name)
257
258 def brentq_fails_to_NAN(self, *args):
--> 259 try:
260 return scipy.optimize.brentq(*args)
261 except:
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mass/calibration/energy_calibration.pyc in <lambda>(e)
252 max_ph = 1.3*self._ph[-1]
253 ph2offset_energy = lambda ph, eoffset: self.ph2energy(ph)-eoffset
--> 254 # self.energy2ph = lambda e: scipy.optimize.brentq(ph2offset_energy, 0., max_ph, args=(e,))
255 self.energy2ph = lambda e: self.brentq_fails_to_NAN(ph2offset_energy, 0., max_ph, args=(e,))
256
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/optimize/zeros.pyc in brentq(f, a, b, args, xtol, rtol, maxiter, full_output, disp)
387 if type(args) != type(()) :
388 args = (args,)
--> 389 r = _zeros._brentq(f,a,b,xtol,maxiter,args,full_output,disp)
390 return results_c(full_output, r)
391
ValueError: f(a) and f(b) must have different signs