class_docstr: Total least squares estimator for 2D ellipses.
  
  The functional model of the ellipse is::
  
      xt = xc + a*cos(theta)*cos(t) - b*sin(theta)*sin(t)
      yt = yc + a*sin(theta)*cos(t) + b*cos(theta)*sin(t)
      d = sqrt((x - xt)**2 + (y - yt)**2)
  
  where ``(xt, yt)`` is the closest point on the ellipse to ``(x, y)``. Thus
  d is the shortest distance from the point to the ellipse.
  
  The estimator is based on a least squares minimization. The optimal
  solution is computed directly, no iterations are required. This leads
  to a simple, stable and robust fitting method.
  
  The ``params`` attribute contains the parameters in the following order::
  
      xc, yc, a, b, theta
  
  Attributes
  ----------
  params : tuple
      Ellipse model parameters in the following order `xc`, `yc`, `a`, `b`,
      `theta`.
  
  Examples
  --------
  
  >>> xy = EllipseModel().predict_xy(np.linspace(0, 2 * np.pi, 25),
  ...                                params=(10, 15, 8, 4, np.deg2rad(30)))
  >>> ellipse = EllipseModel()
  >>> ellipse.estimate(xy)
  True
  >>> np.round(ellipse.params, 2)
  array([10.  , 15.  ,  8.  ,  4.  ,  0.52])
  >>> np.round(abs(ellipse.residuals(xy)), 5)
  array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0., 0., 0., 0., 0., 0.])
class_docstr_tok: ["Total","least","squares","estimator","for","2D","ellipses",".","The","functional","model","of","the","ellipse","is",":",":","xt","=","xc","+","a","*","cos","(","theta",")","*","cos","(","t",")","-","b","*","sin","(","theta",")","*","sin","(","t",")","yt","=","yc","+","a","*","sin","(","theta",")","*","cos","(","t",")","+","b","*","cos","(","theta",")","*","sin","(","t",")","d","=","sqrt","(","(","x","-","xt",")","*","*","2","+","(","y","-","yt",")","*","*","2",")","where","``","(","xt",",","yt",")","``","is","the","closest","point","on","the","ellipse","to","``","(","x",",","y",")","``",".","Thus","d","is","the","shortest","distance","from","the","point","to","the","ellipse",".","The","estimator","is","based","on","a","least","squares","minimization",".","The","optimal","solution","is","computed","directly",",","no","iterations","are","required",".","This","leads","to","a","simple",",","stable","and","robust","fitting","method",".","The","``","params","``","attribute","contains","the","parameters","in","the","following","order",":",":","xc",",","yc",",","a",",","b",",","theta","Attributes","--","--","--","--","--","params",":","tuple","Ellipse","model","parameters","in","the","following","order","`","xc","`",",","`","yc","`",",","`","a","`",",","`","b","`",",","`","theta","`",".","Examples","--","--","--","--",">",">",">","xy","=","EllipseModel","(",")",".predict_xy","(","np.linspace","(","0",",","2","*","np.pi",",","25",")",",","...","params=","(","10",",","15",",","8",",","4",",","np.deg2rad","(","30",")",")",")",">",">",">","ellipse","=","EllipseModel","(",")",">",">",">","ellipse.estimate","(","xy",")","True",">",">",">","np.round","(","ellipse.params",",","2",")","array","(","[","10.",",","15.",",","8.",",","4.",",","0.52","]",")",">",">",">","np.round","(","abs","(","ellipse.residuals","(","xy",")",")",",","5",")","array","(","[","0.",",","0.",",","0.",",","0.",",","0.",",","0.",",","0.",",","0.",",","0.",",","0.",",","0.",",","0.",",","0.",",","0.",",","0.",",","0.",",","0.",",","0.",",","0.",",","0.",",","0.",",","0.",",","0.",",","0.",",","0",".","]",")"]
docstr: Determine residuals of data to model.
  
  For each point the shortest distance to the ellipse is returned.
  
  Parameters
  ----------
  data : (N, 2) array
      N points with ``(x, y)`` coordinates, respectively.
  
  Returns
  -------
  residuals : (N,) array
      Residual for each data point.
docstr_tok: ["Determine","residuals","of","data","to","model",".","For","each","point","the","shortest","distance","to","the","ellipse","is","returned",".","Parameters","--","--","--","--","--","data",":","(","N",",","2",")","array","N","points","with","``","(","x",",","y",")","``","coordinates",",","respectively",".","Returns","--","--","--","-","residuals",":","(","N",",",")","array","Residual","for","each","data","point","."]
code: def residuals(self, data):
      """Determine residuals of data to model.
  
      For each point the shortest distance to the ellipse is returned.
  
      Parameters
      ----------
      data : (N, 2) array
          N points with ``(x, y)`` coordinates, respectively.
  
      Returns
      -------
      residuals : (N,) array
          Residual for each data point.
  
      """
  
      _check_data_dim(data, dim=2)
  
      xc, yc, a, b, theta = self.params
  
      ctheta = math.cos(theta)
      stheta = math.sin(theta)
  
      x = data[:, 0]
      y = data[:, 1]
  
      N = data.shape[0]
  
      def fun(t, xi, yi):
          ct = math.cos(np.squeeze(t))
          st = math.sin(np.squeeze(t))
          xt = xc + a * ctheta * ct - b * stheta * st
          yt = yc + a * stheta * ct + b * ctheta * st
          return (xi - xt) ** 2 + (yi - yt) ** 2
  
      # def Dfun(t, xi, yi):
      #     ct = math.cos(t)
      #     st = math.sin(t)
      #     xt = xc + a * ctheta * ct - b * stheta * st
      #     yt = yc + a * stheta * ct + b * ctheta * st
      #     dfx_t = - 2 * (xi - xt) * (- a * ctheta * st
      #                                - b * stheta * ct)
      #     dfy_t = - 2 * (yi - yt) * (- a * stheta * st
      #                                + b * ctheta * ct)
      #     return [dfx_t + dfy_t]
  
      residuals = np.empty((N,), dtype=np.float64)
  
      # initial guess for parameter t of closest point on ellipse
      t0 = np.arctan2(y - yc, x - xc) - theta
  
      # determine shortest distance to ellipse for each point
      for i in range(N):
          xi = x[i]
          yi = y[i]
          # faster without Dfun, because of the python overhead
          t, _ = optimize.leastsq(fun, t0[i], args=(xi, yi))
          residuals[i] = np.sqrt(fun(t, xi, yi))
  
      return residuals
  
imports: import math
  from warnings import warn
  import numpy
  from numpy.linalg import inv
  from scipy import optimize, spatial
label_desc: Use image node_id 2 for calling the EllipseModel obj's underlying member method code with example usage: obj.residuals(data) and returns: residuals, unknown