title: Numba jit with scipy
body: <p>So I wanted to speed up a program I wrote with the help of numba <code>jit</code>. However <code>jit</code> seems to be not compatible with many scipy functions because they use <code>try</code> ... <code>except</code> ... structures that <code>jit</code> cannot handle (Am I right with this point?)</p>
  
  <p>A relatively simple solution I came up with is to copy the scipy source code I need and delete the <code>try</code> <code>except</code> parts (I already know that it will not run into errors so the <code>try</code> part will always work anyways)</p>
  
  <p>However I do not like this solution and I am not sure if it will work.</p>
  
  <p>My code structure looks like the following</p>
  
  <pre class="lang-py prettyprint-override"><code>import scipy.integrate as integrate
  from scipy optimize import curve_fit
  from numba import jit
  
  def fitfunction():
      ...
  
  @jit
  def function(x):
      # do some stuff
      try:
          fit_param, fit_cov = curve_fit(fitfunction, x, y, p0=(0,0,0), maxfev=500)
          for idx in some_list:
              integrated = integrate.quad(lambda x: fitfunction(fit_param), lower, upper)
      except:
          fit_param=(0,0,0)
          ...
  </code></pre>
  
  <p>Now this results in the following error:</p>
  
  <blockquote>
    <p>LoweringError: Failed at object (object mode backend)</p>
  </blockquote>
  
  <p>I assume this is due to <code>jit</code> not being able to handle <code>try</code> <code>except</code> (it also does not work if I only put <code>jit</code> on the <code>curve_fit</code> and <code>integrate.quad</code> parts and work around my own <code>try</code> <code>except</code> structure)</p>
  
  <pre class="lang-py prettyprint-override"><code>import scipy.integrate as integrate
  from scipy optimize import curve_fit
  from numba import jit
  
  def fitfunction():
      ...
  
  @jit
  def integral(lower, upper):
      return integrate.quad(lambda x: fitfunction(fit_param), lower, upper)
  
  @jit
  def fitting(x, y, pzero, max_fev)
      return curve_fit(fitfunction, x, y, p0=pzero, maxfev=max_fev)
  
  
  def function(x):
      # do some stuff
      try:
          fit_param, fit_cov = fitting(x, y, (0,0,0), 500)
          for idx in some_list:
              integrated = integral(lower, upper)
      except:
          fit_param=(0,0,0)
          ...
  </code></pre>
  
  <p>Is there a way to use <code>jit</code> with <code>scipy.integrate.quad</code> and <code>curve_fit</code> without manually deleting all <code>try</code> <code>except</code> structures from the scipy code?</p>
  
  <p>And would it even speed up the code?</p>