jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def _residual(self, aE_vec):
        """Given a policy function, solves the model's optimality condition and returns the error

        An optimal policy function will return an error close to zero. This method will typically 
        be called within a numerical solver to find the optimal policy function. 

        Args:
            aE_vec (np.array): 1 dimensional array with as many elements as the policy function(s)

        Returns:
            numpy array of 1 dimension representing optimality equation errors
        """
        # reshape input (candidate solution) to shape of policy function, assign to self.aE
        self._unpack_residual_inputs(aE_vec)
        self._get_splines()

        # back out current model variables based on policy function
        q = self._get_q(self.aE)
        theta = self._get_theta(q)
        V = self._get_V(theta, self.N)

        # employment tomorrow
        N_p = self._get_N_tplusone(self.N, q, V)

        # interpolate for tomorrow's policy function (will need to check this to see if it holds w/ log consumption)
        func = sp.interpolate.bisplev(self.X, self.N, self.tck)
        E_p = np.array([func(self.X[i_n], N_p[:, i_n]) for i_n in range(self.nx)])
        E_p = E_p[:, :, 0].T

        # back out future model variables based on policy function
        q_p = self._get_q(E_p)
        theta_p = self._get_theta(q_p)
        lambda_p = self._get_lambda(E_p, q_p)
        gamma_p = self.gamma + self.c*q_p
        w_p = self._get_W(self.X, theta_p, q_p)

        # error in the optimal job creation condition (inside the expectation operator)
        inside_p = self.beta*(self.X - w_p + (1 - self.s)*(gamma_p/q_p - lambda_p))

        # expectations over future states
        rhs = np.dot(inside_p, self.P)
        err = self.aE - rhs
        return err.flatten()
//  [python]
def test_Approximate_Background():
    """
    test_Approximate_Background
    """
    X, Y = np.mgrid[0:10, 0:20]
    import pylab as p

    Z = fit2d.gaussian(100, 2, 13, 3, 5)(X, Y)
    tck, _, _, _ = sci.bisplrep(np.ravel(X), np.ravel(Y), np.ravel(Z), kx=4, ky=4, full_output=1)
    print(Z.shape)
    print(X.shape)
    print(Y.shape)

    Zfit = sci.bisplev(np.arange(10), np.arange(20), tck)

    # for k in np.arange(9):
    # p.subplot(3,3,k+1)
    # p.plot(Z[k],'bo - ')
    # p.plot(Zfit[k],'ro - ')

    p.imshow(Z)
    p.contour(Zfit, cmap=GT.COPPER)

    p.show()
//  [python]
def ipspline(ipparams, position, etc = []):
    """
  This function fits the intra-pixel sensitivity effect using a bicubic spline.

  Parameters
  ----------
    k#:   Knot coefficient
    x,y:  Array of x,y pixel positions and quadrant locations
    etx:  Knot locations

  Returns
  -------
    This function returns an array of y values...

  Revisions
  ---------
  2010-06-08	Kevin Stevenson, UCF  
			kevin218@knights.ucf.edu
		Original Version
    """
    y, x, q = position
    yknots, xknots = etc
    
    tck = spi.bisplrep(xknots.flatten(), yknots.flatten(), ipparams, kx=3, ky=3)
    #print(tck)
    #tck = [yknots, xknots, ipparams, 3, 3]
    #func = spi.interp2d(xknots, yknots, ipparams, kind='cubic'
    output = np.ones(y.size)
    for i in range(y.size):
        output[i] = spi.bisplev(x[i], y[i], tck)
    
    return output
//  [python]
def ipspline(ipparams, position, etc = []):
    """
  This function fits the intra-pixel sensitivity effect using a bicubic spline.

  Parameters
  ----------
    k#:   Knot coefficient
    x,y:  Array of x,y pixel positions and quadrant locations
    etx:  Knot locations

  Returns
  -------
    This function returns an array of y values...

  Revisions
  ---------
  2010-06-08	Kevin Stevenson, UCF  
			kevin218@knights.ucf.edu
		Original Version
    """
    y, x, q = position
    yknots, xknots = etc
    
    tck = spi.bisplrep(xknots.flatten(), yknots.flatten(), ipparams, kx=3, ky=3)
    #print(tck)
    #tck = [yknots, xknots, ipparams, 3, 3]
    #func = spi.interp2d(xknots, yknots, ipparams, kind='cubic'
    output = np.ones(y.size)
    for i in range(y.size):
        output[i] = spi.bisplev(x[i], y[i], tck, dx=0, dy=0)
    
    return output
//  [python]
def interpolate_simple(var, points, z, f):
    X, Y = np.meshgrid(z,f)
    temp = interpolate.bisplrep(X,Y,var) 
    inter_value = interpolate.bisplev(points[0], points[1], temp)
    del temp
    return inter_value
//  [python]
def ipspline(ipparams, position, etc = []):
    """
  This function fits the intra-pixel sensitivity effect using a bicubic spline.

  Parameters
  ----------
    k#:   Knot coefficient
    x,y:  Array of x,y pixel positions and quadrant locations
    etx:  Knot locations

  Returns
  -------
    This function returns an array of y values...

  Revisions
  ---------
  2010-06-08    Kevin Stevenson, UCF
            kevin218@knights.ucf.edu
        Original Version
    """
    y, x, q = position
    yknots, xknots = etc

    tck = spi.bisplrep(xknots.flatten(), yknots.flatten(), ipparams, kx=3, ky=3)
    #print(tck)
    #tck = [yknots, xknots, ipparams, 3, 3]
    #func = spi.interp2d(xknots, yknots, ipparams, kind='cubic'
    output = np.ones(y.size)
    for i in range(y.size):
        output[i] = spi.bisplev(x[i], y[i], tck)

    return output
//  [python]
def ipspline(ipparams, position, etc = []):
    """
  This function fits the intra-pixel sensitivity effect using a bicubic spline.

  Parameters
  ----------
    k#:   Knot coefficient
    x,y:  Array of x,y pixel positions and quadrant locations
    etx:  Knot locations

  Returns
  -------
    This function returns an array of y values...

  Revisions
  ---------
  2010-06-08    Kevin Stevenson, UCF
            kevin218@knights.ucf.edu
        Original Version
    """
    y, x, q = position
    yknots, xknots = etc

    tck = spi.bisplrep(xknots.flatten(), yknots.flatten(), ipparams, kx=3, ky=3)
    #print(tck)
    #tck = [yknots, xknots, ipparams, 3, 3]
    #func = spi.interp2d(xknots, yknots, ipparams, kind='cubic'
    output = np.ones(y.size)
    for i in range(y.size):
        output[i] = spi.bisplev(x[i], y[i], tck, dx=0, dy=0)

    return output
//  [python]
def _interpolator2d_linear(x_new, y_new, _tck) -> ndarray:
    '''
    Z values (e.g., colours) produced by interpolating from the passed source
    linear B-spline representation describing the source mesh defined on the
    set of X and Y coordinates and Z values (e.g., colours) previously passed to
    the :func:`.interp2d_linear` function onto a target mesh defined on the
    passed X and Y coordinates.

    Parameters
    ----------
    x: ndarray
        Target X coordinates to interpolate onto.
    y: ndarray
        Target Y coordinates to interpolate onto.
    _tck : ndarray
        List ``[tx, ty, c, kx, ky]`` containing the knots ``(tx, ty)`` and
        coefficients ``(c)`` of the bivariate B-spline representation of the
        surface along with the degree of the spline, typically obtained by a
        prior call to the :func:`.bisplrep` function.

    Returns
    ----------
    ndarray
        Z values (e.g., colours) produced by this interpolation.
    '''

    # Perform this interpolation. Note that the transpose (i.e., ".T") mimics
    # the behaviour of the original scipy.interpolate.interp2d() function.
    return splprep(x_new, y_new, _tck).T
//  [python]
def _interpolator2d_linear(x_new, y_new, _tck) -> ndarray:
    '''
    Z values (e.g., colours) produced by interpolating from the passed source
    linear B-spline representation describing the source mesh defined on the
    set of X and Y coordinates and Z values (e.g., colours) previously passed to
    the :func:`.interp2d_linear` function onto a target mesh defined on the
    passed X and Y coordinates.

    Parameters
    ----------
    x: ndarray
        Target X coordinates to interpolate onto.
    y: ndarray
        Target Y coordinates to interpolate onto.
    _tck : ndarray
        List ``[tx, ty, c, kx, ky]`` containing the knots ``(tx, ty)`` and
        coefficients ``(c)`` of the bivariate B-spline representation of the
        surface along with the degree of the spline, typically obtained by a
        prior call to the :func:`.bisplrep` function.

    Returns
    ----------
    ndarray
        Z values (e.g., colours) produced by this interpolation.
    '''

    # Perform this interpolation. Note that the transpose (i.e., ".T") mimics
    # the behaviour of the original scipy.interpolate.interp2d() function.
    return bisplev(x_new, y_new, _tck).T
//  [python]
def test_Approximate_Background_mccdimage_spline():
    """
    with spline: does not work

    """
    filename = "CdTe_I999_03Jul06_0200.mccd"

    _, dataimage = IOimage.readoneimage_full(filename)

    mini = np.amin(dataimage[dataimage > 0])

    sampling = 50

    Xin, Yin = np.mgrid[0:2048:sampling, 0:2048:sampling]
    # data_gauss = fit2d.gaussian(100, 1024, 1024, 500, 500)(Xin, Yin)

    dataimage = dataimage[::sampling, ::sampling]

    cond_circle = (Xin - 1023) ** 2 + (Yin - 1023) ** 2 <= 1024 ** 2

    dataimage_bis = np.where(cond_circle, dataimage, mini)

    #    Z=fit2d.gaussian(100,25,50,30,30)(Xin,Yin)

    print(dataimage_bis.shape)
    print(Xin.shape)
    print(Yin.shape)
    tck, _, _, msg = sci.bisplrep(np.ravel(Xin),
                                    np.ravel(Yin),
                                    np.ravel(dataimage_bis),
                                    kx=3,
                                    ky=3,
                                    nxest=100,
                                    nyest=100,
                                    s=10000,
                                    full_output=1)

    # print "tck",tck
    print("msg", msg)

    Zfit = sci.bisplev(np.arange(0, 2048, sampling), np.arange(0, 2048, sampling), tck)

    # for k in np.arange(9):
    # p.subplot(3,3,k+1)
    # p.plot(Z[k],'bo - ')
    # p.plot(Zfit[k],'ro - ')

    p.imshow(dataimage)

    p.contour(Zfit, cmap=GT.COPPER)

    p.show()
//  [python]
def bivariate_Interpolation(self, point, disp, *args, **kwargs):
        """Interpolates the displacement field. Bivariate B-spline interpolation algorithm from scipy is used here.
        for no interpolation option this can use raw method"""

        dx = np.array([d[0] for d in disp])
        dy = np.array([d[1] for d in disp])
        method = 'raw' if 'method' not in kwargs else kwargs['method']
        if method == 'raw':
            self.disp_x = self.Points_X.copy()
            self.disp_y = self.Points_Y.copy()
            count = 0
            for i in range(self.disp_x.shape[0]):
                for j in range(self.disp_x.shape[1]):
                    self.disp_x[i, j] = dx[count]
                    self.disp_y[i, j] = dy[count]
                    count = count + 1

        elif method == 'bivar_Spline':
            tck_x = scipy.interpolate.bisplrep(self.Points_X,
                                               self.Points_Y,
                                               dx,
                                               kx=5,
                                               ky=5)
            self.disp_x = scipy.interpolate.bisplev(self.Points_X[:, 0],
                                                    self.Points_Y[0, :], tck_x)
            tck_y = scipy.interpolate.bisplrep(self.Points_X,
                                               self.Points_Y,
                                               dy,
                                               kx=5,
                                               ky=5)
            self.disp_y = scipy.interpolate.bisplev(self.Points_X[:, 0],
                                                    self.Points_Y[0, :], tck_y)
//  [python]
def get_zdata(self, xydata):
        return_value = []
        for each_xy in xydata:
            each_z = self._tck(each_xy[0], each_xy[1])
            return_value.append(each_z)
        return np.array(return_value)
//  [python]
def get_zdata(self, xydata):
        return_value = []
        for each_xy in xydata:
            each_z = si.bisplev(each_xy[0], each_xy[1], self._tck)
            return_value.append(each_z)
        return np.array(return_value)
//  [python]
def correct_background_bisplrep(
    image: np.ndarray, kx: int = 3, ky: int = 3, s: float = 1e20, sample_step: int = 1
) -> np.ndarray:
    """Corrects the background of an image using 2D B-spline.

    Args:
        image (np.ndarray): Input image as a numpy array.
        kx (int): Degree of the B-spline in the x-direction.
        ky (int): Degree of the B-spline in the y-direction.
        s (float): Smoothing factor for the B-spline.
        sample_step (int): Step size for subsampling the image.

    Returns:
        np.ndarray: Background-corrected image as a numpy array.

    """
    # Define grid of coordinates
    ygrid, xgrid = np.indices(image.shape)

    # Subsample the image
    ygrid = ygrid[::sample_step, ::sample_step]
    xgrid = xgrid[::sample_step, ::sample_step]
    image_sub = image[::sample_step, ::sample_step]

    # Fit B-spline to subsampled image
    tck = interp.bisplrep(xgrid, ygrid, image_sub, kx=kx, ky=ky, s=s)

    # Evaluate B-spline on grid
    tck = scipy.interpolate.bisplrep(xgrid, ygrid, image_sub, s=s)  # xgrid and y grid are auto raveled
    nx, ny = image.shape[0], image.shape[1]
    lx = np.linspace(0, nx, nx)
    ly = np.linspace(0, ny, ny)
    image_corrected = interp.bisplev(lx, ly, tck)
    image_corrected = image - image_corrected
    image_corrected[image_corrected < 0] = 0

    return image_corrected
//  [python]
def correct_background_bisplrep(
    image: np.ndarray, kx: int = 3, ky: int = 3, s: float = 1e20, sample_step: int = 1
) -> np.ndarray:
    """Corrects the background of an image using 2D B-spline.

    Args:
        image (np.ndarray): Input image as a numpy array.
        kx (int): Degree of the B-spline in the x-direction.
        ky (int): Degree of the B-spline in the y-direction.
        s (float): Smoothing factor for the B-spline.
        sample_step (int): Step size for subsampling the image.

    Returns:
        np.ndarray: Background-corrected image as a numpy array.

    """
    # Define grid of coordinates
    ygrid, xgrid = np.indices(image.shape)

    # Subsample the image
    ygrid = ygrid[::sample_step, ::sample_step]
    xgrid = xgrid[::sample_step, ::sample_step]
    image_sub = image[::sample_step, ::sample_step]

    # Fit B-spline to subsampled image
    tck = interp.bisplrep(xgrid, ygrid, image_sub, kx=kx, ky=ky, s=s)

    # Evaluate B-spline on grid
    tck = scipy.interpolate.bisplrep(xgrid, ygrid, image_sub, s=s)  # xgrid and y grid are auto raveled
    nx, ny = image.shape[0], image.shape[1]
    lx = np.linspace(0, nx, nx)
    ly = np.linspace(0, ny, ny)
    image_corrected = scipy.interpolate.bisplev(lx, ly, tck)
    image_corrected = image - image_corrected
    image_corrected[image_corrected < 0] = 0

    return image_corrected
//  [python]
def unwhiten(data, comp):
    """
    Inverse process of whitening.

    _data_ is in principle subspace
    coordinates, rowwise. _comp_ (column wise)
    span this principle subspace. 
    Unwhitening needs to be done with
    the pseudo inverse, as comp usually
    is not a full rotation matrix.
    """
    uw = la.pinv2(comp)
    return np.dot(data, uw)
//  [python]
def unwhiten(data, comp):
    """
    Inverse process of whitening.

    _data_ is in principle subspace
    coordinates, rowwise. _comp_ (column wise)
    span this principle subspace. 
    Unwhitening needs to be done with
    the pseudo inverse, as comp usually
    is not a full rotation matrix.
    """
    uw = la.pinv(comp)
    return np.dot(data, uw)
//  [python]
def _interp(self, grid, spline=False):
        """Return the interpolated values corresponding to the grid points.

        Parameters
        ----------
        grid :
            pixel values
        spline : bool
            If False, linear interpolation (uses
            `scipy.interpolate.griddata`), or if True: spline
            interpolation (uses `scipy.interpolate.bisplrep` and
            `scipy.interpolate.bisplev`).

        """
        if self.mask is np.ma.nomask:
            x, y = np.mgrid[:self.shape[0], :self.shape[1]].reshape(2, -1)
            data = self._data
        else:
            x, y = np.where(~self._mask)
            data = self._data[x, y]

        grid = np.array(grid)

        if spline:
            if self.var is not None:
                var = self.var.filled(np.inf)
                weight = 1 / np.sqrt(np.abs(var[x, y]))
            else:
                weight = None

            tck = interpolate.bisplrep(x, y, data, w=weight)
            res = interpolate.bisplev(grid[0], grid[1], tck)
            return res
        else:
            # FIXME - check if this is still needed :
            # scipy 0.9 griddata - interpolate.interp2d segfaults when there
            # are too many data points
            # f = interpolate.interp2d(x, y, data)
            res = interpolate.griddata((x, y), data, grid.T, method='linear')
            return res
//  [python]
def find_lpc_qr(D, d):
    """
    Find linear prediction filter using QR decomposition.
    """
    q, r = scipy.linalg.qr(D)
    q, r = np.mat(q), np.mat(r)

    # SPEED
    # the next line is slow and the use of pinv2 should be avoided as
    # pseudo inversion of r involves a computationally expensive SVD
    # decomposition which is not needed.  Rather r*x = q.H*d should be
    # solved for x using LAPACK's ZTRTRS function (or similar function with
    # different prefix).  This is not currently available in scipy/numpy and
    # therefore is not used here.
    return scipy.linalg.pinv2(r) * q.H * d
//  [python]
def find_lpc_qr(D, d):
    """
    Find linear prediction filter using QR decomposition.
    """
    q, r = scipy.linalg.qr(D)
    q, r = np.mat(q), np.mat(r)
    return scipy.linalg.pinv(r) * q.H * d
//  [python]
def baseline(y, deg=None, max_it=None, tol=None):
    """
    Computes the baseline of a given data.

    Iteratively performs a polynomial fitting in the data to detect its
    baseline. At every iteration, the fitting weights on the regions with
    peaks are reduced to identify the baseline only.

    Parameters
    ----------
    y : ndarray
        Data to detect the baseline.
    deg : int
        Degree of the polynomial that will estimate the data baseline. A low
        degree may fail to detect all the baseline present, while a high
        degree may make the data too oscillatory, especially at the edges.
    max_it : int
        Maximum number of iterations to perform.
    tol : float
        Tolerance to use when comparing the difference between the current
        fit coefficients and the ones from the last iteration. The iteration
        procedure will stop when the difference between them is lower than
        *tol*.

    Returns
    -------
    baseline : ndarray
        Array with the baseline amplitude for every original point in *y*
    """

    # for not repeating ourselves in `envelope`
    if deg is None:
        deg = 3
    if max_it is None:
        max_it = 100
    if tol is None:
        tol = 1e-3

    order = deg + 1
    coeffs = np.ones(order)

    # try to avoid numerical issues
    cond = math.pow(y.max(), 1.0 / order)
    x = np.linspace(0.0, cond, y.size)
    base = y.copy()

    vander = np.vander(x, order)
    vander_pinv = la.pinv2(vander)

    for _ in range(max_it):
        coeffs_new = np.dot(vander_pinv, y)

        if la.norm(coeffs_new - coeffs) / la.norm(coeffs) < tol:
            break

        coeffs = coeffs_new
        base = np.dot(vander, coeffs)
        y = np.minimum(y, base)

    return base
//  [python]
def baseline(y, deg=None, max_it=None, tol=None):
    """
    Computes the baseline of a given data.

    Iteratively performs a polynomial fitting in the data to detect its
    baseline. At every iteration, the fitting weights on the regions with
    peaks are reduced to identify the baseline only.

    Parameters
    ----------
    y : ndarray
        Data to detect the baseline.
    deg : int
        Degree of the polynomial that will estimate the data baseline. A low
        degree may fail to detect all the baseline present, while a high
        degree may make the data too oscillatory, especially at the edges.
    max_it : int
        Maximum number of iterations to perform.
    tol : float
        Tolerance to use when comparing the difference between the current
        fit coefficients and the ones from the last iteration. The iteration
        procedure will stop when the difference between them is lower than
        *tol*.

    Returns
    -------
    baseline : ndarray
        Array with the baseline amplitude for every original point in *y*
    """

    # for not repeating ourselves in `envelope`
    if deg is None:
        deg = 3
    if max_it is None:
        max_it = 100
    if tol is None:
        tol = 1e-3

    order = deg + 1
    coeffs = np.ones(order)

    # try to avoid numerical issues
    cond = math.pow(y.max(), 1.0 / order)
    x = np.linspace(0.0, cond, y.size)
    base = y.copy()

    vander = np.vander(x, order)
    vander_pinv = la.pinv(vander)

    for _ in range(max_it):
        coeffs_new = np.dot(vander_pinv, y)

        if la.norm(coeffs_new - coeffs) / la.norm(coeffs) < tol:
            break

        coeffs = coeffs_new
        base = np.dot(vander, coeffs)
        y = np.minimum(y, base)

    return base
//  [python]
def baseline(y, deg=None, max_it=None, tol=None):
    """
    Computes the baseline of a given data.

    Iteratively performs a polynomial fitting in the data to detect its
    baseline. At every iteration, the fitting weights on the regions with
    peaks are reduced to identify the baseline only.

    Parameters
    ----------
    y : ndarray
        Data to detect the baseline.
    deg : int (default: 3)
        Degree of the polynomial that will estimate the data baseline. A low
        degree may fail to detect all the baseline present, while a high
        degree may make the data too oscillatory, especially at the edges.
    max_it : int (default: 100)
        Maximum number of iterations to perform.
    tol : float (default: 1e-3)
        Tolerance to use when comparing the difference between the current
        fit coefficients and the ones from the last iteration. The iteration
        procedure will stop when the difference between them is lower than
        *tol*.

    Returns
    -------
    ndarray
        Array with the baseline amplitude for every original point in *y*
    """
    # for not repeating ourselves in `envelope`
    if deg is None: deg = 3
    if max_it is None: max_it = 100
    if tol is None: tol = 1e-3
    
    order = deg + 1
    coeffs = np.ones(order)

    # try to avoid numerical issues
    cond = math.pow(abs(y).max(), 1. / order)
    x = np.linspace(0., cond, y.size)
    base = y.copy()

    vander = np.vander(x, order)
    vander_pinv = LA.pinv2(vander)

    for _ in range(max_it):
        coeffs_new = np.dot(vander_pinv, y)

        if LA.norm(coeffs_new - coeffs) / LA.norm(coeffs) < tol:
            break

        coeffs = coeffs_new
        base = np.dot(vander, coeffs)
        y = np.minimum(y, base)

    return base
//  [python]
def baseline(y, deg=None, max_it=None, tol=None):
    """
    Computes the baseline of a given data.

    Iteratively performs a polynomial fitting in the data to detect its
    baseline. At every iteration, the fitting weights on the regions with
    peaks are reduced to identify the baseline only.

    Parameters
    ----------
    y : ndarray
        Data to detect the baseline.
    deg : int (default: 3)
        Degree of the polynomial that will estimate the data baseline. A low
        degree may fail to detect all the baseline present, while a high
        degree may make the data too oscillatory, especially at the edges.
    max_it : int (default: 100)
        Maximum number of iterations to perform.
    tol : float (default: 1e-3)
        Tolerance to use when comparing the difference between the current
        fit coefficients and the ones from the last iteration. The iteration
        procedure will stop when the difference between them is lower than
        *tol*.

    Returns
    -------
    ndarray
        Array with the baseline amplitude for every original point in *y*
    """
    # for not repeating ourselves in `envelope`
    if deg is None: deg = 3
    if max_it is None: max_it = 100
    if tol is None: tol = 1e-3
    
    order = deg + 1
    coeffs = np.ones(order)

    # try to avoid numerical issues
    cond = math.pow(abs(y).max(), 1. / order)
    x = np.linspace(0., cond, y.size)
    base = y.copy()

    vander = np.vander(x, order)
    vander_pinv = LA.pinv(vander)

    for _ in range(max_it):
        coeffs_new = np.dot(vander_pinv, y)

        if LA.norm(coeffs_new - coeffs) / LA.norm(coeffs) < tol:
            break

        coeffs = coeffs_new
        base = np.dot(vander, coeffs)
        y = np.minimum(y, base)

    return base
//  [python]
def bg_correction(image):
    # adapt code from weikang
    sample_step = 5
    I = image
    n_row, n_col = I.shape
    ctrl_x = []
    ctrl_y = []
    ctrl_z = []

    for i in np.arange(0, n_row, sample_step):
        for j in np.arange(0, n_col, sample_step):
            ctrl_x.append(i)
            ctrl_y.append(j)
            ctrl_z.append(I[i, j])
    ctrl_x = np.array(ctrl_x)
    ctrl_y = np.array(ctrl_y)
    ctrl_z = np.array(ctrl_z)

    nx, ny = I.shape[0], I.shape[1]
    lx = np.linspace(0, n_row, nx)
    ly = np.linspace(0, n_col, ny)

    # s value is important for smoothing
    tck = scipy.interpolate.bisplrep(ctrl_x, ctrl_y, ctrl_z, s=1e20)
    znew = scipy.interpolate.bisplev(lx, ly, tck)
    # func = scipy.interpolate.interp2d(ctrl_x, ctrl_y, ctrl_z, kind='quintic')
    # znew = func(lx, ly).T
    res = I - znew
    res[res < 0] = 0
    # plt.imshow(I - znew * 2)
    # fig, axes = plt.subplots(2,2)
    # axes[0, 0].imshow(I)
    # axes[0, 1].imshow(znew)
    # axes[1, 0].imshow(I-znew)
    # axes[1, 1].imshow(I-znew * 2)

    # axes[0, 0].set_title('original')
    # axes[0, 1].set_title('fitted surface')
    # axes[1, 0].set_title('image-surface')
    # axes[1, 1].set_title('image-surfaceXfactor')

    # plt.show()

    # fig = plt.figure()
    # ax = Axes3D(fig)
    # # ax = fig.gca(projection='3d')

    # fig = plt.figure()
    # ax = fig.gca(projection='3d')
    # x, y = np.meshgrid(lx, ly)
    # surf = ax.plot_surface(x, y, znew, cmap=matplotlib.cm.coolwarm,
    #                        linewidth=0)
    # ax.scatter(x, y, I - znew)
    # plt.show()
    return res
//  [python]
def baseline(y, deg=3, max_it=100, tol=1e-3):
    """Computes the baseline of a given data.

    Iteratively performs a polynomial fitting in the data to detect its
    baseline. At every iteration, the fitting weights on the regions with
    peaks are reduced to identify the baseline only.

    Parameters
    ----------
    y : ndarray
        Data to detect the baseline.
    deg : int
        Degree of the polynomial that will estimate the data baseline. A low
        degree may fail to detect all the baseline present, while a high
        degree may make the data too oscillatory, especially at the edges.
    max_it : int
        Maximum number of iterations to perform.
    tol : float
        Tolerance to use when comparing the difference between the current
        fit coefficient and the ones from the last iteration. The iteration
        procedure will stop when the difference between them is lower than
        *tol*.

    Returns
    -------
    ndarray
        Array with the baseline amplitude for every original point in *y*
    """
    order = deg + 1
    coeffs = np.ones(order)

    # try to avoid numerical issues
    cond = math.pow(y.max(), 1. / order)
    x = np.linspace(0., cond, y.size)
    base = y.copy()

    vander = np.vander(x, order)
    vander_pinv = LA.pinv2(vander)

    for _ in range(max_it):
        coeffs_new = np.dot(vander_pinv, y)

        if LA.norm(coeffs_new - coeffs) / LA.norm(coeffs) < tol:
            break

        coeffs = coeffs_new
        base = np.dot(vander, coeffs)
        y = np.minimum(y, base)

    return base
//  [python]
def baseline(y, deg=3, max_it=100, tol=1e-3):
    """Computes the baseline of a given data.

    Iteratively performs a polynomial fitting in the data to detect its
    baseline. At every iteration, the fitting weights on the regions with
    peaks are reduced to identify the baseline only.

    Parameters
    ----------
    y : ndarray
        Data to detect the baseline.
    deg : int
        Degree of the polynomial that will estimate the data baseline. A low
        degree may fail to detect all the baseline present, while a high
        degree may make the data too oscillatory, especially at the edges.
    max_it : int
        Maximum number of iterations to perform.
    tol : float
        Tolerance to use when comparing the difference between the current
        fit coefficient and the ones from the last iteration. The iteration
        procedure will stop when the difference between them is lower than
        *tol*.

    Returns
    -------
    ndarray
        Array with the baseline amplitude for every original point in *y*
    """
    order = deg + 1
    coeffs = np.ones(order)

    # try to avoid numerical issues
    cond = math.pow(y.max(), 1. / order)
    x = np.linspace(0., cond, y.size)
    base = y.copy()

    vander = np.vander(x, order)
    vander_pinv = LA.pinv(vander)

    for _ in range(max_it):
        coeffs_new = np.dot(vander_pinv, y)

        if LA.norm(coeffs_new - coeffs) / LA.norm(coeffs) < tol:
            break

        coeffs = coeffs_new
        base = np.dot(vander, coeffs)
        y = np.minimum(y, base)

    return base
//  [python]
def predict_view(self, *args):
        # Regress original given views onto target
        transformed_views = self.transform_view(*args)

        # Get the regression from the training data with available views
        predicted_target = np.mean([transformed_views[i] for i in range(len(args)) if args[i] is not None], axis=0)

        predicted_views = []
        for i, view in enumerate(args):
            if view is None:
                predicted_views.append(predicted_target @ pinv2(self.weights_list[i]))
            else:
                predicted_views.append(view)
        for i, predicted_view in enumerate(predicted_views):
            predicted_views[i] += self.dataset_means[i]
        return predicted_views
//  [python]
def predict_view(self, *args):
        # Regress original given views onto target
        transformed_views = self.transform_view(*args)

        # Get the regression from the training data with available views
        predicted_target = np.mean([transformed_views[i] for i in range(len(args)) if args[i] is not None], axis=0)

        predicted_views = []
        for i, view in enumerate(args):
            if view is None:
                predicted_views.append(predicted_target @ np.linalg.pinv(self.weights_list[i]))
            else:
                predicted_views.append(view)
        for i, predicted_view in enumerate(predicted_views):
            predicted_views[i] += self.dataset_means[i]
        return predicted_views
//  [python]
def compute_pseudoinverse(A):
    return scipy.linalg.pinv2(A)
//  [python]
def compute_pseudoinverse(A):
    return scipy.linalg.pinv(A)
//  [python]
def _newton_update(weights_0, X, XX_T, target, k, method_, maxiter=int(1024),
                   ftol=1e-12, gtol=1e-8, reg_lambda=0.0, reg_mu=None,
                   ref_row=True, initializer=None, reg_format=None):

    L_list = [float(_objective(weights_0, X, XX_T, target, k, method_,
                                      reg_lambda, reg_mu, ref_row, initializer,
                                      reg_format))]

    weights = weights_0.copy()

    # TODO move this to the initialization
    if method_ is None:
        weights = np.zeros_like(weights)

    for i in range(0, maxiter):

        gradient = _gradient(weights, X, XX_T, target, k, method_, reg_lambda,
                             reg_mu, ref_row, initializer, reg_format)

        if np.abs(gradient).sum() < gtol:
            break

        # FIXME hessian is ocasionally NaN
        hessian = _hessian(weights, X, XX_T, target, k, method_, reg_lambda,
                           reg_mu, ref_row, initializer, reg_format)

        if method_ == 'FixDiag':
            updates = gradient / hessian
        else:
            try:
                inverse = scipy.linalg.pinv2(hessian)
                updates = np.matmul(inverse, gradient)
            except (raw_np.linalg.LinAlgError, ValueError) as err:
                logging.error(err)
                updates = gradient

        for step_size in np.hstack((np.linspace(1, 0.1, 10),
                                    np.logspace(-2, -32, 31))):

            tmp_w = weights - (updates * step_size).ravel()

            if np.any(np.isnan(tmp_w)):
                logging.debug("{}: There are NaNs in tmp_w".format(method_))

            L = _objective(tmp_w, X, XX_T, target, k, method_, reg_lambda,
                           reg_mu, ref_row, initializer, reg_format)

            if (L - L_list[-1]) < 0:
                break

        L_list.append(float(L))

        logging.debug("{}: after {} iterations log-loss = {:.7e}, sum_grad = {:.7e}".format(
            method_, i, L, np.abs(gradient).sum()))

        if np.isnan(L):
            logging.error("{}: log-loss is NaN".format(method_))
            break

        if i >= 5:
            if (float(raw_np.min(raw_np.diff(L_list[-5:]))) > -ftol) & \
               (float(raw_np.sum(raw_np.diff(L_list[-5:])) > 0) == 0):
                weights = tmp_w.copy()
                logging.debug('{}: Terminate as there is not enough changes on loss.'.format(
                    method_))
                break

        if (L_list[-1] - L_list[-2]) > 0:
            logging.debug('{}: Terminate as the loss increased {}.'.format(
                method_, np.diff(L_list[-2:])))
            break
        else:
            weights = tmp_w.copy()

    L = _objective(weights, X, XX_T, target, k, method_,
                   reg_lambda, reg_mu, ref_row, initializer, reg_format)

    logging.debug("{}: after {} iterations final log-loss = {:.7e}, sum_grad = {:.7e}".format(
        method_, i, L, np.abs(gradient).sum()))

    return weights
//  [python]
def _newton_update(weights_0, X, XX_T, target, k, method_, maxiter=int(1024),
                   ftol=1e-12, gtol=1e-8, reg_lambda=0.0, reg_mu=None,
                   ref_row=True, initializer=None, reg_format=None):

    L_list = [float(_objective(weights_0, X, XX_T, target, k, method_,
                                      reg_lambda, reg_mu, ref_row, initializer,
                                      reg_format))]

    weights = weights_0.copy()

    # TODO move this to the initialization
    if method_ is None:
        weights = np.zeros_like(weights)

    for i in range(0, maxiter):

        gradient = _gradient(weights, X, XX_T, target, k, method_, reg_lambda,
                             reg_mu, ref_row, initializer, reg_format)

        if np.abs(gradient).sum() < gtol:
            break

        # FIXME hessian is ocasionally NaN
        hessian = _hessian(weights, X, XX_T, target, k, method_, reg_lambda,
                           reg_mu, ref_row, initializer, reg_format)

        if method_ == 'FixDiag':
            updates = gradient / hessian
        else:
            try:
                inverse = scipy.linalg.pinv(hessian)
                updates = np.matmul(inverse, gradient)
            except (raw_np.linalg.LinAlgError, ValueError) as err:
                logging.error(err)
                updates = gradient

        for step_size in np.hstack((np.linspace(1, 0.1, 10),
                                    np.logspace(-2, -32, 31))):

            tmp_w = weights - (updates * step_size).ravel()

            if np.any(np.isnan(tmp_w)):
                logging.debug("{}: There are NaNs in tmp_w".format(method_))

            L = _objective(tmp_w, X, XX_T, target, k, method_, reg_lambda,
                           reg_mu, ref_row, initializer, reg_format)

            if (L - L_list[-1]) < 0:
                break

        L_list.append(float(L))

        logging.debug("{}: after {} iterations log-loss = {:.7e}, sum_grad = {:.7e}".format(
            method_, i, L, np.abs(gradient).sum()))

        if np.isnan(L):
            logging.error("{}: log-loss is NaN".format(method_))
            break

        if i >= 5:
            if (float(raw_np.min(raw_np.diff(L_list[-5:]))) > -ftol) & \
               (float(raw_np.sum(raw_np.diff(L_list[-5:])) > 0) == 0):
                weights = tmp_w.copy()
                logging.debug('{}: Terminate as there is not enough changes on loss.'.format(
                    method_))
                break

        if (L_list[-1] - L_list[-2]) > 0:
            logging.debug('{}: Terminate as the loss increased {}.'.format(
                method_, np.diff(L_list[-2:])))
            break
        else:
            weights = tmp_w.copy()

    L = _objective(weights, X, XX_T, target, k, method_,
                   reg_lambda, reg_mu, ref_row, initializer, reg_format)

    logging.debug("{}: after {} iterations final log-loss = {:.7e}, sum_grad = {:.7e}".format(
        method_, i, L, np.abs(gradient).sum()))

    return weights
//  [python]
def polynomial_baseline_correction(x, order=3, n_iter=100):
    '''
    Polynomial baseline correction.

    Reference:
        https://github.com/lucashn/peakutils/blob/master/peakutils/baseline.py
    '''
    base = x.copy()
    coeffs = np.ones(order)

    cond = math.pow(abs(x).max(), 1. / order)
    y = np.linspace(0., cond, base.size)
    z = x.copy()

    vander = np.vander(y, order)
    vander_pinv = scipy.linalg.pinv2(vander)

    for _ in range(n_iter):
        coeffs = np.dot(vander_pinv, base)
        z = np.dot(vander, coeffs)
        base = np.minimum(base, z)
    return x - z
//  [python]
def polynomial_baseline_correction(x, order=3, n_iter=100):
    '''
    Polynomial baseline correction.

    Reference:
        https://github.com/lucashn/peakutils/blob/master/peakutils/baseline.py
    '''
    base = x.copy()
    coeffs = np.ones(order)

    cond = math.pow(abs(x).max(), 1. / order)
    y = np.linspace(0., cond, base.size)
    z = x.copy()

    vander = np.vander(y, order)
    vander_pinv = scipy.linalg.pinv(vander)

    for _ in range(n_iter):
        coeffs = np.dot(vander_pinv, base)
        z = np.dot(vander, coeffs)
        base = np.minimum(base, z)
    return x - z
//  [python]
def ols_svd(x: np.ndarray, y: np.ndarray) -> np.ndarray:
    u, s, v = scl.svd(x)
    return v.T @ np.diag(1/s) @ u.T @ y
//  [python]
def ols_svd(x: np.ndarray, y: np.ndarray) -> np.ndarray:
    u, s, v = scl.svd(x)
    return v.T @ scl.pinv(scl.diagsvd(s, u.shape[0], v.shape[0])) @ u.T @ y
//  [python]
def train(self, trajs, silent=False):
        trans_obs = [self._transform_observations(traj.obs[:]) for traj in trajs]
        X = np.concatenate([obs[:-1,:] for obs in trans_obs]).T
        Y = np.concatenate([obs[1:,:] for obs in trans_obs]).T
        U = np.concatenate([traj.ctrls[:-1,:] for traj in trajs]).T
        
        n = X.shape[0] # state dimension
        m = U.shape[0] # control dimension    
        
        XU = np.concatenate((X, U), axis = 0) # stack X and U together
        if self.method == "lstsq": # Least Squares Solution
            AB = np.dot(Y, sla.pinv2(XU))
            A = AB[:n, :n]
            B = AB[:n, n:]
        elif self.method == "lasso":  # Call lasso regression on coefficients
            print("Call Lasso")
            clf = Lasso(alpha=self.lasso_alpha)
            clf.fit(XU.T, Y.T)
            AB = clf.coef_
            A = AB[:n, :n]
            B = AB[:n, n:]
        elif self.method == "stable": # Compute stable A, and B
            print("Compute Stable Koopman")
            # call function
            A, _, _, _, B, _ = stabilize_discrete(X, U, Y)
            A = np.real(A)
            B = np.real(B)

        self.A, self.B = A, B
//  [python]
def train(self, trajs, silent=False):
        trans_obs = [self._transform_observations(traj.obs[:]) for traj in trajs]
        X = np.concatenate([obs[:-1,:] for obs in trans_obs]).T
        Y = np.concatenate([obs[1:,:] for obs in trans_obs]).T
        U = np.concatenate([traj.ctrls[:-1,:] for traj in trajs]).T
        
        n = X.shape[0] # state dimension
        m = U.shape[0] # control dimension    
        
        XU = np.concatenate((X, U), axis = 0) # stack X and U together
        if self.method == "lstsq": # Least Squares Solution
            AB = np.dot(Y, sla.pinv(XU))
            A = AB[:n, :n]
            B = AB[:n, n:]
        elif self.method == "lasso":  # Call lasso regression on coefficients
            print("Call Lasso")
            clf = Lasso(alpha=self.lasso_alpha)
            clf.fit(XU.T, Y.T)
            AB = clf.coef_
            A = AB[:n, :n]
            B = AB[:n, n:]
        elif self.method == "stable": # Compute stable A, and B
            print("Compute Stable Koopman")
            # call function
            A, _, _, _, B, _ = stabilize_discrete(X, U, Y)
            A = np.real(A)
            B = np.real(B)

        self.A, self.B = A, B
//  [python]
def baseline(y, deg=None, max_it=None, tol=None):
    """
    Computes the baseline of a given data.

    Iteratively performs a polynomial fitting in the data to detect its
    baseline. At every iteration, the fitting weights on the regions with
    peaks are reduced to identify the baseline only.

    Parameters
    ----------
    y : ndarray
        Data to detect the baseline.
    deg : int (default: 3)
        Degree of the polynomial that will estimate the data baseline. A low
        degree may fail to detect all the baseline present, while a high
        degree may make the data too oscillatory, especially at the edges.
    max_it : int (default: 100)
        Maximum number of iterations to perform.
    tol : float (default: 1e-3)
        Tolerance to use when comparing the difference between the current
        fit coefficients and the ones from the last iteration. The iteration
        procedure will stop when the difference between them is lower than
        *tol*.

    Returns
    -------
    ndarray
        Array with the baseline amplitude for every original point in *y*
    """
    # for not repeating ourselves in `envelope`
    if deg is None: deg = 3
    if max_it is None: max_it = 100
    if tol is None: tol = 1e-3
    
    order = deg + 1
    coeffs = np.ones(order)

    # try to avoid numerical issues
    cond = math.pow(y.max(), 1. / order)
    x = np.linspace(0., cond, y.size)
    base = y.copy()

    vander = np.vander(x, order)
    vander_pinv = LA.pinv2(vander)

    for _ in range(max_it):
        coeffs_new = np.dot(vander_pinv, y)

        if LA.norm(coeffs_new - coeffs) / LA.norm(coeffs) < tol:
            break

        coeffs = coeffs_new
        base = np.dot(vander, coeffs)
        y = np.minimum(y, base)

    return base
//  [python]
def baseline(y, deg=None, max_it=None, tol=None):
    """
    Computes the baseline of a given data.

    Iteratively performs a polynomial fitting in the data to detect its
    baseline. At every iteration, the fitting weights on the regions with
    peaks are reduced to identify the baseline only.

    Parameters
    ----------
    y : ndarray
        Data to detect the baseline.
    deg : int (default: 3)
        Degree of the polynomial that will estimate the data baseline. A low
        degree may fail to detect all the baseline present, while a high
        degree may make the data too oscillatory, especially at the edges.
    max_it : int (default: 100)
        Maximum number of iterations to perform.
    tol : float (default: 1e-3)
        Tolerance to use when comparing the difference between the current
        fit coefficients and the ones from the last iteration. The iteration
        procedure will stop when the difference between them is lower than
        *tol*.

    Returns
    -------
    ndarray
        Array with the baseline amplitude for every original point in *y*
    """
    # for not repeating ourselves in `envelope`
    if deg is None: deg = 3
    if max_it is None: max_it = 100
    if tol is None: tol = 1e-3
    
    order = deg + 1
    coeffs = np.ones(order)

    # try to avoid numerical issues
    cond = math.pow(y.max(), 1. / order)
    x = np.linspace(0., cond, y.size)
    base = y.copy()

    vander = np.vander(x, order)
    vander_pinv = LA.pinv(vander)

    for _ in range(max_it):
        coeffs_new = np.dot(vander_pinv, y)

        if LA.norm(coeffs_new - coeffs) / LA.norm(coeffs) < tol:
            break

        coeffs = coeffs_new
        base = np.dot(vander, coeffs)
        y = np.minimum(y, base)

    return base
//  [python]
def calc_ste(self, verbose=True):
        if verbose:
            print('Standard errors calculation.')
            # logger.info('Standard error calculation.')
        t0 = time.time()
        I_covar = self.covar.getFisherInf()
        I_mean = self.Areml.K()
        self.covar.setFIinv(s.pinv(I_covar))
        if self.mean.n_covs>0:
            self.mean.setFIinv(s.pinv(I_mean))
        t1 = time.time()
        # logger.debug('Time elapsed: %.2fs', t1-t0)
        if verbose:
            print(('Time elapsed: %.2f s' % (t1-t0)))
//  [python]
def calc_ste(self, verbose=True):
        if verbose:
            print('Standard errors calculation.')
            # logger.info('Standard error calculation.')
        t0 = time.time()
        I_covar = self.covar.getFisherInf()
        I_mean = self.Areml.K()
        self.covar.setFIinv(sp.linalg.pinv(I_covar))
        if self.mean.n_covs>0:
            self.mean.setFIinv(sp.linalg.pinv(I_mean))
        t1 = time.time()
        # logger.debug('Time elapsed: %.2fs', t1-t0)
        if verbose:
            print(('Time elapsed: %.2f s' % (t1-t0)))
//  [python]
def compute_projected_matrix(self):
        """
        To pre-compute the matrix for projecting onto subspace

        Uses:
            self.
            P_BC_C: dict for each dimension with the evalualted polynomial values for the boundary conditions (including continuity).
                    np.array([number of boundary conditions, number of coefficients])

        Modifies:
            self.
            proj_mat: matrix used in projecting solutions onto the subspace that adheres to the BCs
                    In each dimension is a 2D np.array
        """

        P_BC_C = self.P_BC_C

        # initialise
        proj_mat = dict()

        # Loop for each dimension
        for key in P_BC_C.keys():
            # number of boundary conditions
            nBCs = int(P_BC_C[key].shape[0])
            # matrix for the boundary condition polynomial matrix
            P_BC_mat = sp.matrix(P_BC_C[key])
            # identity matrix
            eye_mat = sp.matrix(np.identity(nBCs))

            # Compute the projected matrix and convert to a np array
            # proj_mat[key] = np.array(P_BC_mat.T*(np.linalg.inv(P_BC_mat*P_BC_mat.T)*eye_mat))
            # pm_1 = np.array(P_BC_mat.T*(np.linalg.inv(P_BC_mat*P_BC_mat.T)*eye_mat))
            mat = P_BC_mat*P_BC_mat.T
            mat_inv = sp.linalg.pinv2(mat)

            proj_mat[key] = np.array(P_BC_mat.T*(sp.matrix(mat_inv)*eye_mat))

        self.proj_mat = proj_mat