2013-03-12 2 views
1

여러 가지 이유로 실패 할 수있는 숫자 코드가 있습니다. 결과가 계속 이동하면 numpy.NAN으로 출력 값을 바꾸고 싶습니다. try : except : 문에서 brentq에 대한 호출을 감싸려고했지만 동일한 예외에서 코드가 여전히 손상되었습니다.예외 잡기를 제외하고 시도하십시오

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 

답변

0

어쩌면 몇 가지 예외가이 경우에 삼켜되고있는 경우,이

except Exception: 
     print 'returning numpy.NAN instead of throwing an exception for energy2ph' 
     return numpy.NAN 
을 시도하십시오
관련 문제