jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def sindy(X, Y, psi, eps=0.001, iterations=10):
    '''
    Sparse indentification of nonlinear dynamics for the data given by X and Y.

    :param psi:        set of basis functions, see d3s.observables
    :param eps:        cutoff threshold
    :param iterations: number of sparsification steps
    :return:           coefficient matrix Xi
    '''
    PsiX = psi(X)
    Xi = Y @ _sp.linalg.pinv(PsiX) # least-squares initial guess

    for k in range(iterations):
        s = abs(Xi) < eps # find coefficients less than eps ...
        Xi[s] = 0         # ... and set them to zero
        for ind in range(X.shape[0]):
            b = ~s[ind, :] # consider only functions corresponding to coefficients greater than eps
            Xi[ind, b] = Y[ind, :] @ _sp.linalg.pinv(PsiX[b, :])
    return Xi
//  [python]
def _predict(cls, model, xs_2d):
        pop_mu = model['mu'].astype(float)
        pop_cov = model['cov'].astype(float)
        feats = xs_2d
        sample_mu = np.mean(feats, axis=0)
        sample_cov = np.cov(feats.T)
        X = sample_mu - pop_mu
        covmat = ((pop_cov+sample_cov)/2.0)
        pinvmat = np.linalg.pinv(covmat)
        d1 = np.sqrt(np.dot(np.dot(X, pinvmat), X))
        return d1
//  [python]
def _predict(cls, model, xs_2d):
        pop_mu = model['mu'].astype(float)
        pop_cov = model['cov'].astype(float)
        feats = xs_2d
        sample_mu = np.mean(feats, axis=0)
        sample_cov = np.cov(feats.T)
        X = sample_mu - pop_mu
        covmat = ((pop_cov+sample_cov)/2.0)
        pinvmat = scipy.linalg.pinv(covmat)
        d1 = np.sqrt(np.dot(np.dot(X, pinvmat), X))
        return d1
//  [python]
def niqe(inputImgData):

    patch_size = 96
    module_path = dirname(__file__)

    # TODO: memoize
    params = scipy.io.loadmat(join(module_path, 'data', 'niqe_image_params.mat'))
    pop_mu = np.ravel(params["pop_mu"])
    pop_cov = params["pop_cov"]


    M, N = inputImgData.shape

    # assert C == 1, "niqe called with videos containing %d channels. Please supply only the luminance channel" % (C,)
    assert M > (patch_size*2+1), "niqe called with small frame size, requires > 192x192 resolution video using current training parameters"
    assert N > (patch_size*2+1), "niqe called with small frame size, requires > 192x192 resolution video using current training parameters"


    feats = get_patches_test_features(inputImgData, patch_size)
    sample_mu = np.mean(feats, axis=0)
    sample_cov = np.cov(feats.T)

    X = sample_mu - pop_mu
    covmat = ((pop_cov+sample_cov)/2.0)
    pinvmat = scipy.linalg.pinv2(covmat)
    niqe_score = np.sqrt(np.dot(np.dot(X, pinvmat), X))

    return niqe_score
//  [python]
def niqe(inputImgData):

    patch_size = 96
    module_path = dirname(__file__)

    # TODO: memoize
    params = scipy.io.loadmat(join(module_path, 'data', 'niqe_image_params.mat'))
    pop_mu = np.ravel(params["pop_mu"])
    pop_cov = params["pop_cov"]


    M, N = inputImgData.shape

    # assert C == 1, "niqe called with videos containing %d channels. Please supply only the luminance channel" % (C,)
    assert M > (patch_size*2+1), "niqe called with small frame size, requires > 192x192 resolution video using current training parameters"
    assert N > (patch_size*2+1), "niqe called with small frame size, requires > 192x192 resolution video using current training parameters"


    feats = get_patches_test_features(inputImgData, patch_size)
    sample_mu = np.mean(feats, axis=0)
    sample_cov = np.cov(feats.T)

    X = sample_mu - pop_mu
    covmat = ((pop_cov+sample_cov)/2.0)
    pinvmat = scipy.linalg.pinv(covmat)
    niqe_score = np.sqrt(np.dot(np.dot(X, pinvmat), X))

    return niqe_score
//  [python]
def get_pinvs(adj_matrix, vals_org, dim):
    """ Precomputes the pseudo-inverse matrices for every dimension.

    :param adj_matrix: sp.spmatrix
        The graph represented as a sparse scipy matrix
    :param vals_org: np.ndarray, shape [n]
        The generalized eigenvalues of the clean graph
    :param dim: int
        Embedding dimension
    :return:  np.ndarray, shape [k, n, n]
        Pseudo-inverse matrices for every dimension
    """
    deg_matrix = sp.diags(adj_matrix.sum(0).A1)
    pinvs = []
    for k in range(dim):
        print(k)
        try:
            pinvs.append(-np.linalg.pinv((adj_matrix - vals_org[k] * deg_matrix).toarray()))
        except np.linalg.LinAlgError:
            print('error')
            pinvs.append(-sp.linalg.pinv((adj_matrix - vals_org[k] * deg_matrix).toarray()))

    return np.stack(pinvs)

    sum_of_powers = transition_matrix
    last = transition_matrix
    for i in range(1, pow):
        last = last.dot(transition_matrix)
        sum_of_powers += last
//  [python]
def get_pinvs(adj_matrix, vals_org, dim):
    """ Precomputes the pseudo-inverse matrices for every dimension.

    :param adj_matrix: sp.spmatrix
        The graph represented as a sparse scipy matrix
    :param vals_org: np.ndarray, shape [n]
        The generalized eigenvalues of the clean graph
    :param dim: int
        Embedding dimension
    :return:  np.ndarray, shape [k, n, n]
        Pseudo-inverse matrices for every dimension
    """
    deg_matrix = sp.diags(adj_matrix.sum(0).A1)
    pinvs = []
    for k in range(dim):
        print(k)
        try:
            pinvs.append(-np.linalg.pinv((adj_matrix - vals_org[k] * deg_matrix).toarray()))
        except np.linalg.LinAlgError:
            print('error')
            pinvs.append(-spl.pinv((adj_matrix - vals_org[k] * deg_matrix).toarray()))

    return np.stack(pinvs)

    sum_of_powers = transition_matrix
    last = transition_matrix
    for i in range(1, pow):
        last = last.dot(transition_matrix)
        sum_of_powers += last
//  [python]
def multivariateGaussian(x, mu, sigma2):
    k = np.size(mu, 0)
    sigma2 = np.diag(sigma2)
    x = x-mu
    p = (2*math.pi)**(-k/2)*la.det(sigma2)**(-0.5)*np.exp(-0.5*np.sum(x.dot(s.pinv(sigma2))*x, 1))
    return p
//  [python]
def multivariateGaussian(x, mu, sigma2):
    k = np.size(mu, 0)
    sigma2 = np.diag(sigma2)
    x = x-mu
    p = (2*math.pi)**(-k/2)*la.det(sigma2)**(-0.5)*np.exp(-0.5*np.sum(x.dot(la.pinv(sigma2))*x, 1))
    return p
//  [python]
def linalg_bench():
    if np.__config__.get_info("lapack_mkl_info"):
        print("MKL version", get_mkl_version())
    else:
        print("not using MKL")

    print("PyTorch version", torch.version.__version__)

    print("Scipy version: ", scipy.version.full_version)
    print("Numpy version: ", np.version.full_version)

    for d in [1024]:
        print(f"{d}-by-{d} matrix")
        n = 10000
        assert n > 2*d   # to prevent singularity
        X = np.random.random((d, 10000))
        Y = np.random.random((d, 10000))
        H = X @ X.T
        S = Y @ Y.T

        with timeit(f"linalg.solve_lyapunov"):
            result = scipy.linalg.solve_lyapunov(H, S)
            #print(result[0,0])

        with timeit(f"linalg.pinvh"):
            result = scipy.linalg.pinvh(H)
            #print(result[0, 0])

        with timeit(f"linalg.pinv"):
            result = np.linalg.pinv(H)
            #print(result[0, 0])


        with timeit(f"linalg.inv"):
            result = scipy.linalg.inv(H)
            #print(result[0, 0])

        with timeit(f"qr"):
            result = scipy.linalg.qr(H)
            #print(result[0, 0])

        with timeit(f"qr-pivoting"):
            result = scipy.linalg.qr(H, pivoting=True)
            #print(result[0, 0])

        with timeit(f"svd"):
            result = scipy.linalg.svd(H)
//  [python]
def linalg_bench():
    if np.__config__.get_info("lapack_mkl_info"):
        print("MKL version", get_mkl_version())
    else:
        print("not using MKL")

    print("PyTorch version", torch.version.__version__)

    print("Scipy version: ", scipy.version.full_version)
    print("Numpy version: ", np.version.full_version)

    for d in [1024]:
        print(f"{d}-by-{d} matrix")
        n = 10000
        assert n > 2*d   # to prevent singularity
        X = np.random.random((d, 10000))
        Y = np.random.random((d, 10000))
        H = X @ X.T
        S = Y @ Y.T

        with timeit(f"linalg.solve_lyapunov"):
            result = scipy.linalg.solve_lyapunov(H, S)
            #print(result[0,0])

        with timeit(f"linalg.pinvh"):
            result = scipy.linalg.pinvh(H)
            #print(result[0, 0])

        with timeit(f"linalg.pinv"):
            result = scipy.linalg.pinv(H)
            #print(result[0, 0])


        with timeit(f"linalg.inv"):
            result = scipy.linalg.inv(H)
            #print(result[0, 0])

        with timeit(f"qr"):
            result = scipy.linalg.qr(H)
            #print(result[0, 0])

        with timeit(f"qr-pivoting"):
            result = scipy.linalg.qr(H, pivoting=True)
            #print(result[0, 0])

        with timeit(f"svd"):
            result = scipy.linalg.svd(H)
//  [python]
def niqe(inputImgData, patch_size=96):
   
    if isinstance(inputImgData, str):
        #inputImgData = np.array(Image.open(inputImgData).convert('LA'))[:,:,0] #[:,512:1024,0]
        try:
          
            inputImgData = cv2.cvtColor(cv2.imread(inputImgData), cv2.COLOR_RGB2LAB)[:,512:1024,0]#[:,:,0]

        except:
            return None
    # TODO: memoize

    M, N = inputImgData.shape

    # assert C == 1, "niqe called with videos containing %d channels. Please supply only the luminance channel" % (C,)
    assert M > (patch_size*2+1), "niqe called with small frame size, requires > 192x192 resolution video using current training parameters"
    assert N > (patch_size*2+1), "niqe called with small frame size, requires > 192x192 resolution video using current training parameters"


    feats = get_patches_test_features(inputImgData, patch_size)
    sample_mu = np.mean(feats, axis=0)
    sample_cov = np.cov(feats.T)

    X = sample_mu - pop_mu
    covmat = ((pop_cov+sample_cov)/2.0)
    pinvmat = np.linalg.pinv(covmat)
    niqe_score = np.sqrt(np.dot(np.dot(X, pinvmat), X))
    print(niqe_score)
    return niqe_score
//  [python]
def niqe(inputImgData, patch_size=96):
   
    if isinstance(inputImgData, str):
        #inputImgData = np.array(Image.open(inputImgData).convert('LA'))[:,:,0] #[:,512:1024,0]
        try:
          
            inputImgData = cv2.cvtColor(cv2.imread(inputImgData), cv2.COLOR_RGB2LAB)[:,512:1024,0]#[:,:,0]

        except:
            return None
    # TODO: memoize

    M, N = inputImgData.shape

    # assert C == 1, "niqe called with videos containing %d channels. Please supply only the luminance channel" % (C,)
    assert M > (patch_size*2+1), "niqe called with small frame size, requires > 192x192 resolution video using current training parameters"
    assert N > (patch_size*2+1), "niqe called with small frame size, requires > 192x192 resolution video using current training parameters"


    feats = get_patches_test_features(inputImgData, patch_size)
    sample_mu = np.mean(feats, axis=0)
    sample_cov = np.cov(feats.T)

    X = sample_mu - pop_mu
    covmat = ((pop_cov+sample_cov)/2.0)
    pinvmat = scipy.linalg.pinv(covmat)
    niqe_score = np.sqrt(np.dot(np.dot(X, pinvmat), X))
    print(niqe_score)
    return niqe_score
//  [python]
def ens_update0(ens, obs_ens, observations, perturbs, obs_err_cov):
    """Compute the ensemble analysis (conditioning/Bayes) update."""
    X, _        = center(ens)
    Y, _        = center(obs_ens)
    perturbs, _ = center(perturbs, rescale=True)
    obs_cov     = obs_err_cov*(len(Y)-1) + Y.T@Y
    obs_pert    = perturbs @ sqrt(obs_err_cov)  # TODO: sqrtm if R non-diag
    innovations = observations - (obs_ens + obs_pert)
    KG          = s.pinv(obs_cov) @ Y.T @ X
    return ens + innovations @ KG
//  [python]
def ens_update0(ens, obs_ens, observations, perturbs, obs_err_cov):
    """Compute the ensemble analysis (conditioning/Bayes) update."""
    X, _        = center(ens)
    Y, _        = center(obs_ens)
    perturbs, _ = center(perturbs, rescale=True)
    obs_cov     = obs_err_cov*(len(Y)-1) + Y.T@Y
    obs_pert    = perturbs @ sqrt(obs_err_cov)  # TODO: sqrtm if R non-diag
    innovations = observations - (obs_ens + obs_pert)
    KG          = sla.pinv(obs_cov) @ Y.T @ X
    return ens + innovations @ KG
//  [python]
def integrate(y, x):
    return np.hstack((0., cumtrapz(y, x)))
//  [python]
def integrate(y, x):
    return np.hstack((0., cumulative_trapezoid(y, x)))
//  [python]
def estimateMarkovParameters(U,Y,past):
    import numpy as np
    import scipy 
    from scipy import linalg
    
    timeSteps=U.shape[1]
    m=U.shape[0]
    r=Y.shape[0]
    l=timeSteps-past-1
    
    # data matrices for estimating the Markov parameters
    Y_p_p_l=np.zeros(shape=(r,l+1))
    Z_0_pm1_l=np.zeros(shape=((m+r)*past,l+1))  # - returned
    # the estimated matrix that is returned as the output of the function
    M_pm1=np.zeros(shape=(r,(r+m)*past))   # -returned
    
    
    # form the matrices "Y_p_p_l" and "Z_0_pm1_l"
    # iterate through columns
    for j in range(l+1):
        # iterate through rows
        for i in range(past):
            Z_0_pm1_l[i*(m+r):i*(m+r)+m,j]=U[:,i+j]
            Z_0_pm1_l[i*(m+r)+m:i*(m+r)+m+r,j]=Y[:,i+j]
        Y_p_p_l[:,j]=Y[:,j+past]
        # numpy.linalg.lstsq
        #M_pm1=scipy.linalg.lstsq(Z_0_pm1_l.T,Y_p_p_l.T)
        M_pm1=np.matmul(Y_p_p_l,linalg.pinv(Z_0_pm1_l))
    
    return M_pm1, Z_0_pm1_l, Y_p_p_l
//  [python]
def iterate_gf(E, h_0, h_l, h_r, se, num_iter):
    """

    Parameters
    ----------
    E : numpy.ndarray (dtype=numpy.float)
        Energy array
    h_l : numpy.ndarray (dtype=numpy.float)
        Left-side coupling Hamiltonian
    h_0 : numpy.ndarray (dtype=numpy.float)
        Hamiltonian of the device region
    h_r : numpy.ndarray (dtype=numpy.float)
        Right-side coupling Hamiltonian
    se : numpy.ndarray (dtype=numpy.complex)
        Self-energy
    num_iter : int
        Number of iterations

    Returns
    -------
    se : numpy.ndarray (dtype=numpy.complex)
        Self-energy
    """

    for _ in range(num_iter):
        se = h_r.dot(linalg.pinv2(E * np.identity(h_0.shape[0]) - h_0 - se)).dot(h_l)
    return se
//  [python]
def iterate_gf(E, h_0, h_l, h_r, se, num_iter):
    """

    Parameters
    ----------
    E : numpy.ndarray (dtype=numpy.float)
        Energy array
    h_l : numpy.ndarray (dtype=numpy.float)
        Left-side coupling Hamiltonian
    h_0 : numpy.ndarray (dtype=numpy.float)
        Hamiltonian of the device region
    h_r : numpy.ndarray (dtype=numpy.float)
        Right-side coupling Hamiltonian
    se : numpy.ndarray (dtype=numpy.complex)
        Self-energy
    num_iter : int
        Number of iterations

    Returns
    -------
    se : numpy.ndarray (dtype=numpy.complex)
        Self-energy
    """

    for _ in range(num_iter):
        se = h_r.dot(linalg.pinv(E * np.identity(h_0.shape[0]) - h_0 - se)).dot(h_l)
    return se
//  [python]
def IES_analysis(w, T, Y, dy):
    """Compute the ensemble analysis."""
    Y0       = s.pinv(T) @ Y                       # "De-condition"
    nExs     = Y0.shape[0] - Y0.shape[1]             # nEns - len(y), i.e. "Excess N"
    V, s, UT = sla.svd(Y0, full_matrices=(nExs>0))   # Decompose
    cow1s    = N-1 + np.pad(s**2, (0, max(0, nExs))) # Postr. cov_w^{-1} spectrum
    cowp     = lambda p: (V * cow1s**p) @ V.T        # Postr. cov_w^{-p}
    grad     = Y0@dy - w*(N-1)                       # Cost function gradient
    dw       = grad@cowp(-1.0)                       # Gauss-Newton step
    T        = cowp(-.5) * sqrt(N-1)                 # Transform matrix
    return dw, T
//  [python]
def IES_analysis(w, T, Y, dy):
    """Compute the ensemble analysis."""
    Y0       = sla.pinv(T) @ Y                       # "De-condition"
    nExs     = Y0.shape[0] - Y0.shape[1]             # nEns - len(y), i.e. "Excess N"
    V, s, UT = sla.svd(Y0, full_matrices=(nExs>0))   # Decompose
    cow1s    = N-1 + np.pad(s**2, (0, max(0, nExs))) # Postr. cov_w^{-1} spectrum
    cowp     = lambda p: (V * cow1s**p) @ V.T        # Postr. cov_w^{-p}
    grad     = Y0@dy - w*(N-1)                       # Cost function gradient
    dw       = grad@cowp(-1.0)                       # Gauss-Newton step
    T        = cowp(-.5) * sqrt(N-1)                 # Transform matrix
    return dw, T
//  [python]
def step(self, q, dq, com_ref, orien_ref, mom_ref, dmom_ref,
             endeff_pos_ref, endeff_vel_ref, endeff_acc_ref,
             endeff_contact, joint_regularization_ref):
        '''
        Arguments:
            q: Current robot state
            dq: Current robot velocity
            com_ref: Reference com position in global coordinates
            lmom_ref: Reference linear momentum in global coordinates
            amom_ref: Reference angular momentum in global coordinates
            endeff_pos_ref: [N_endeff x 3] Reference endeffectors position in global coordinates
            endeff_vel_ref: [N_endeff x 3] Reference endeffectors velocity in global coordinates
        '''
        self.update_kinematics(q, dq)

        self.update_des_acc(q, dq, com_ref, orien_ref, mom_ref, dmom_ref,
                            endeff_pos_ref, endeff_vel_ref, endeff_acc_ref,
                            joint_regularization_ref)
        self.fill_weights(endeff_contact)

        if self.use_hierarchy:
            J_feet = self.J[6:6+12, :]
            J_feet_pinv = np.linalg.pinv(J_feet)
            ddq_feet = J_feet_pinv @ (self.desired_acceleration[6:6+12] - self.drift_terms[6:6+12])
            N_feet = np.eye(self.nv) - J_feet_pinv @ J_feet
            J_rest = self.J[:6,:]
            J_rest_pinv = np.linalg.pinv(J_rest @ N_feet)
            rest_acc = self.desired_acceleration[:6]
            rest_acc = rest_acc - self.drift_terms[:6]
            rest_acc = rest_acc - J_rest @ ddq_feet
            return ddq_feet + J_rest_pinv @ rest_acc

        else:
            hessian = self.J.T @ self.w @ self.J
            hessian += 1e-6 * np.identity(len(hessian))
            gradient = - self.J.T.dot(self.w).dot(self.desired_acceleration - self.drift_terms).reshape(-1)
            return self.qp_solver.quadprog_solve_qp(hessian, gradient)
//  [python]
def step(self, q, dq, com_ref, orien_ref, mom_ref, dmom_ref,
             endeff_pos_ref, endeff_vel_ref, endeff_acc_ref,
             endeff_contact, joint_regularization_ref):
        '''
        Arguments:
            q: Current robot state
            dq: Current robot velocity
            com_ref: Reference com position in global coordinates
            lmom_ref: Reference linear momentum in global coordinates
            amom_ref: Reference angular momentum in global coordinates
            endeff_pos_ref: [N_endeff x 3] Reference endeffectors position in global coordinates
            endeff_vel_ref: [N_endeff x 3] Reference endeffectors velocity in global coordinates
        '''
        self.update_kinematics(q, dq)

        self.update_des_acc(q, dq, com_ref, orien_ref, mom_ref, dmom_ref,
                            endeff_pos_ref, endeff_vel_ref, endeff_acc_ref,
                            joint_regularization_ref)
        self.fill_weights(endeff_contact)

        if self.use_hierarchy:
            J_feet = self.J[6:6+12, :]
            J_feet_pinv = scipy.linalg.pinv(J_feet, cond=0.00001)
            ddq_feet = J_feet_pinv @ (self.desired_acceleration[6:6+12] - self.drift_terms[6:6+12])
            N_feet = np.eye(self.nv) - J_feet_pinv @ J_feet
            J_rest = self.J[:6,:]
            J_rest_pinv = scipy.linalg.pinv(J_rest @ N_feet, cond=0.00001)
            rest_acc = self.desired_acceleration[:6]
            rest_acc = rest_acc - self.drift_terms[:6]
            rest_acc = rest_acc - J_rest @ ddq_feet
            return ddq_feet + J_rest_pinv @ rest_acc

        else:
            hessian = self.J.T @ self.w @ self.J
            hessian += 1e-6 * np.identity(len(hessian))
            gradient = - self.J.T.dot(self.w).dot(self.desired_acceleration - self.drift_terms).reshape(-1)
            return self.qp_solver.quadprog_solve_qp(hessian, gradient)
//  [python]
def calculate_tm_via_trapz_height(VP, T, H):
    from scipy.integrate import cumtrapz
    import numpy as np
    # change T units to K:
    T_copy = T.copy(deep=True) + 273.15
    num = cumtrapz(VP / T_copy, H, initial=np.nan)
    denom = cumtrapz(VP / T_copy**2, H, initial=np.nan)
    tm = num / denom
    return tm
//  [python]
def calculate_tm_via_trapz_height(VP, T, H):
    from scipy.integrate import cumulative_trapezoid
    import numpy as np
    # change T units to K:
    T_copy = T.copy(deep=True) + 273.15
    num = cumulative_trapezoid(VP / T_copy, H, initial=np.nan)
    denom = cumulative_trapezoid(VP / T_copy**2, H, initial=np.nan)
    tm = num / denom
    return tm
//  [python]
def _get_eeos(self):
        """get_eeos.

        This method uses scipy.integrate to integrate
        pressure to obtain energy.


        """

        v = self.volume
        P_birch_murnaghan = self.eos_birch_murnaghan_pressure(
            self.eos_birch_murnaghan_pressure_fitted.x, v
        )

        self.E_birch_murnaghan = (
            integrate.cumtrapz(v, P_birch_murnaghan, initial=0) * 6.2415e-03
        )
//  [python]
def _get_eeos(self):
        """get_eeos.

        This method uses scipy.integrate to integrate
        pressure to obtain energy.


        """

        v = self.volume
        P_birch_murnaghan = self.eos_birch_murnaghan_pressure(
            self.eos_birch_murnaghan_pressure_fitted.x, v
        )

        self.E_birch_murnaghan = (
            integrate.cumulative_trapezoid(v, P_birch_murnaghan, initial=0) * 6.2415e-03
        )
//  [python]
def integrate_grid_cumulative(integrand, kgrid, ugrid):
    """
    Integrate over a 2D grid of sample points using the Simpson rule.
    """
    Ik = [scipy.integrate.simps(integrand.T[i], ugrid) for i in range(kgrid.size)]
    
    # Debugging plot of integrand I(k)
    if DBG_PLOT_CUMUL_INTEGRAND:
        P.subplot(111)
        P.plot(kgrid, Ik)
        P.xscale('log')
        P.show()
    return scipy.integrate.cumtrapz(Ik, kgrid, initial=0.)
//  [python]
def integrate_grid_cumulative(integrand, kgrid, ugrid):
    """
    Integrate over a 2D grid of sample points using the Simpson rule.
    """
    Ik = [scipy.integrate.simps(integrand.T[i], ugrid) for i in range(kgrid.size)]
    
    # Debugging plot of integrand I(k)
    if DBG_PLOT_CUMUL_INTEGRAND:
        P.subplot(111)
        P.plot(kgrid, Ik)
        P.xscale('log')
        P.show()
    return scipy.integrate.cumulative_trapezoid(Ik, kgrid, initial=0.)
//  [python]
def integrate(self):
        """
        Integrate trace.
        """
        self.data = np.concatenate((self.data[:1], scipy.integrate.cumtrapz(self.data, dx=1. / self.stats.sampling_rate)))
        self.stats.filter += 'Int'
//  [python]
def integrate(self):
        """
        Integrate trace.
        """
        self.data = np.concatenate((self.data[:1], scipy.integrate.cumulative_trapezoid(self.data, dx=1. / self.stats.sampling_rate)))
        self.stats.filter += 'Int'
//  [python]
def fsigma8_for_params(z, cosmo, zmax=10., usegamma=False):
    """
    Calculate f(z)*sigma_8(z) for a set of cosmological parameters.
    """
    _z = np.linspace(0., zmax, 400)
    a = 1. / (1. + _z)
    
    # Integrate linear growth rate to find linear growth factor, D(z)
    # N.B. D(z=0) = 1.
    _f = fgrowth(cosmo, _z, usegamma=usegamma)
    _D = np.concatenate( ([0.,], scipy.integrate.cumtrapz(_f, np.log(a))) )
    _D = np.exp(_D)
    
    # Construct interpolating functions and return
    fs8 = scipy.interpolate.interp1d(_z, _f*_D*cosmo['sigma_8'], 
                                     kind='linear', bounds_error=False)
    return fs8(z)
//  [python]
def fsigma8_for_params(z, cosmo, zmax=10., usegamma=False):
    """
    Calculate f(z)*sigma_8(z) for a set of cosmological parameters.
    """
    _z = np.linspace(0., zmax, 400)
    a = 1. / (1. + _z)
    
    # Integrate linear growth rate to find linear growth factor, D(z)
    # N.B. D(z=0) = 1.
    _f = fgrowth(cosmo, _z, usegamma=usegamma)
    _D = np.concatenate( ([0.,], scipy.integrate.cumulative_trapezoid(_f, np.log(a))) )
    _D = np.exp(_D)
    
    # Construct interpolating functions and return
    fs8 = scipy.interpolate.interp1d(_z, _f*_D*cosmo['sigma_8'], 
                                     kind='linear', bounds_error=False)
    return fs8(z)
//  [python]
def integrate(curvelist, low=None, high=None):
    """
     Take the integral of the curve or curves in curvelist.

    :param curvelist: A curve or list of curves
    :type curvelist: curve or list
    :param low: The lower limit
    :type low: float
    :param high: The maximum limit
    :type high: float
    :return: list -- the list of integrated curves
    """
    curves = list()
    ncurves = list()

    if isinstance(curvelist, list):
        curves.extend(curvelist)
    else:
        curves.append(curvelist)

    for c in curves:
        nc = c.copy()
        nc.plotname = ''
        nc.color = ''

        if low is None:
            nc.name = 'Integrate ' + c.plotname
        else:
            nc.name = 'Integrate %s [%.1f,%.1f]' % (c.plotname, low, high)

        if low is None:
            low = min(nc.x)

        if high is None:
            high = max(nc.x)

        r = __get_sub_range(nc.x, low, high)
        nc.x = nc.x[r[0]:r[1]+1]
        nc.y = nc.y[r[0]:r[1]+1]
        nc.y = np.array(scipy.integrate.cumtrapz(nc.y, nc.x, initial=0.0))

        ncurves.append(nc)

    return ncurves
//  [python]
def integrate(curvelist, low=None, high=None):
    """
     Take the integral of the curve or curves in curvelist.

    :param curvelist: A curve or list of curves
    :type curvelist: curve or list
    :param low: The lower limit
    :type low: float
    :param high: The maximum limit
    :type high: float
    :return: list -- the list of integrated curves
    """
    curves = list()
    ncurves = list()

    if isinstance(curvelist, list):
        curves.extend(curvelist)
    else:
        curves.append(curvelist)

    for c in curves:
        nc = c.copy()
        nc.plotname = ''
        nc.color = ''

        if low is None:
            nc.name = 'Integrate ' + c.plotname
        else:
            nc.name = 'Integrate %s [%.1f,%.1f]' % (c.plotname, low, high)

        if low is None:
            low = min(nc.x)

        if high is None:
            high = max(nc.x)

        r = __get_sub_range(nc.x, low, high)
        nc.x = nc.x[r[0]:r[1]+1]
        nc.y = nc.y[r[0]:r[1]+1]
        nc.y = np.array(scipy.integrate.cumulative_trapezoid(nc.y, nc.x, initial=0.0))

        ncurves.append(nc)

    return ncurves
//  [python]
def _logC(self, m, beta, deltam, ml, res = 200, exact_th=0.):
        '''
        Gives inverse log integral of  p(m1, m2) dm2 (i.e. log C(m1) in the LVC notation )
        '''
        xlow=np.linspace(ml, ml+deltam+deltam/10, 200)
        xup=np.linspace(ml+deltam+deltam/10+deltam/100, m[~np.isnan(m)].max(), res)
        xx=np.sort(np.concatenate([xlow,xup], ))
  
        p2 = np.exp(self._logpdfm2( xx , beta, deltam, ml))
        cdf = cumtrapz(p2, xx)
        
        where_compute = ~np.isnan(m)
        where_exact = m <exact_th*ml
        
        where_approx = (~where_exact) & (where_compute)
        
        
        result = np.empty_like(m)
        
        result[~where_compute]=np.NINF
        result[where_exact]=self._logCexact(m[where_exact], beta, deltam, ml,) #np.NINF
        
        result[where_approx] = -np.log( np.interp(m[where_approx], xx[1:], cdf) )
        
        return result
//  [python]
def _logC(self, m, beta, deltam, ml, res = 200, exact_th=0.):
        '''
        Gives inverse log integral of  p(m1, m2) dm2 (i.e. log C(m1) in the LVC notation )
        '''
        xlow=np.linspace(ml, ml+deltam+deltam/10, 200)
        xup=np.linspace(ml+deltam+deltam/10+deltam/100, m[~np.isnan(m)].max(), res)
        xx=np.sort(np.concatenate([xlow,xup], ))
  
        p2 = np.exp(self._logpdfm2( xx , beta, deltam, ml))
        cdf = cumulative_trapezoid(p2, xx)
        
        where_compute = ~np.isnan(m)
        where_exact = m <exact_th*ml
        
        where_approx = (~where_exact) & (where_compute)
        
        
        result = np.empty_like(m)
        
        result[~where_compute]=np.NINF
        result[where_exact]=self._logCexact(m[where_exact], beta, deltam, ml,) #np.NINF
        
        result[where_approx] = -np.log( np.interp(m[where_approx], xx[1:], cdf) )
        
        return result
//  [python]
def background_evolution_splines(cosmo, zmax=10., nsamples=500):
    """
    Get interpolation functions for background functions of redshift:
      * H(z), Hubble rate in km/s/Mpc
      * r(z), comoving distance in Mpc
      * D(z), linear growth factor
      * f(z), linear growth rate
    """
    _z = np.linspace(0., zmax, nsamples)
    a = 1. / (1. + _z)
    H0 = (100.*cosmo['h']); w0 = cosmo['w0']; wa = cosmo['wa']
    om = cosmo['omega_M_0']; ol = cosmo['omega_lambda_0']
    ok = 1. - om - ol
    
    # Sample Hubble rate H(z) and comoving dist. r(z) at discrete points
    omegaDE = ol * np.exp(3.*wa*(a - 1.)) / a**(3.*(1. + w0 + wa))
    E = np.sqrt( om * a**(-3.) + ok * a**(-2.) + omegaDE )
    _H = H0 * E
    
    r_c = np.concatenate( ([0.], scipy.integrate.cumtrapz(1./E, _z)) )
    if ok > 0.:
        _r = C/(H0*np.sqrt(ok)) * np.sinh(r_c * np.sqrt(ok))
    elif ok < 0.:
        _r = C/(H0*np.sqrt(-ok)) * np.sin(r_c * np.sqrt(-ok))
    else:
        _r = (C/H0) * r_c
    
    # Integrate linear growth rate to find linear growth factor, D(z)
    # N.B. D(z=0) = 1.
    a = 1. / (1. + _z)
    _f = fgrowth(cosmo, _z)
    _D = np.concatenate( ([0.,], scipy.integrate.cumtrapz(_f, np.log(a))) )
    _D = np.exp(_D)
    
    # Construct interpolating functions and return
    r = scipy.interpolate.interp1d(_z, _r, kind='linear', bounds_error=False)
    H = scipy.interpolate.interp1d(_z, _H, kind='linear', bounds_error=False)
    D = scipy.interpolate.interp1d(_z, _D, kind='linear', bounds_error=False)
    f = scipy.interpolate.interp1d(_z, _f, kind='linear', bounds_error=False)
    return H, r, D, f
//  [python]
def background_evolution_splines(cosmo, zmax=10., nsamples=500):
    """
    Get interpolation functions for background functions of redshift:
      * H(z), Hubble rate in km/s/Mpc
      * r(z), comoving distance in Mpc
      * D(z), linear growth factor
      * f(z), linear growth rate
    """
    _z = np.linspace(0., zmax, nsamples)
    a = 1. / (1. + _z)
    H0 = (100.*cosmo['h']); w0 = cosmo['w0']; wa = cosmo['wa']
    om = cosmo['omega_M_0']; ol = cosmo['omega_lambda_0']
    ok = 1. - om - ol
    
    # Sample Hubble rate H(z) and comoving dist. r(z) at discrete points
    omegaDE = ol * np.exp(3.*wa*(a - 1.)) / a**(3.*(1. + w0 + wa))
    E = np.sqrt( om * a**(-3.) + ok * a**(-2.) + omegaDE )
    _H = H0 * E
    
    r_c = np.concatenate( ([0.], scipy.integrate.cumulative_trapezoid(1./E, _z)) )
    if ok > 0.:
        _r = C/(H0*np.sqrt(ok)) * np.sinh(r_c * np.sqrt(ok))
    elif ok < 0.:
        _r = C/(H0*np.sqrt(-ok)) * np.sin(r_c * np.sqrt(-ok))
    else:
        _r = (C/H0) * r_c
    
    # Integrate linear growth rate to find linear growth factor, D(z)
    # N.B. D(z=0) = 1.
    a = 1. / (1. + _z)
    _f = fgrowth(cosmo, _z)
    _D = np.concatenate( ([0.,], scipy.integrate.cumulative_trapezoid(_f, np.log(a))) )
    _D = np.exp(_D)
    
    # Construct interpolating functions and return
    r = scipy.interpolate.interp1d(_z, _r, kind='linear', bounds_error=False)
    H = scipy.interpolate.interp1d(_z, _H, kind='linear', bounds_error=False)
    D = scipy.interpolate.interp1d(_z, _D, kind='linear', bounds_error=False)
    f = scipy.interpolate.interp1d(_z, _f, kind='linear', bounds_error=False)
    return H, r, D, f
//  [python]
def diffMeasure(c1, c2, tol=1e-8):
    """
    Compare two curves. For the given curves a fractional difference measure and its average are computed.

    >>> curves = pydvif.read('testData.txt')

    >>> c1, c2  = pydvif.diffMeasure(curves[0], curves[1])

    >>> curves.append(c1)

    >>> curves.append(c2)

    >>> pydvif.create_plot(curves, legend=True)

    :param c1: The first curve
    :type c1: Curve
    :param c2: The second curve
    :type c2: Curve
    :param tol: The tolerance
    :type tol: float
    :return: tuple -- Two curves representing the fractional difference measure and its average
    """
    cdiff = curve.Curve('', 'FD = $|$' + __toCurveString(c1) + ' - ' + __toCurveString(c2) + '$|$/($|$' + __toCurveString(c1) + '$|$ + $|$' + __toCurveString(c2) + '$|$)')
    ic1, ic2 = curve.getinterp(c1, c2)
    f1 = tol * (np.max(ic1.y) - np.min(ic1.y))
    f2 = tol * (np.max(ic2.y) - np.min(ic2.y))
    ydiff = np.abs(ic1.y - ic2.y)
    yden = (np.abs(ic1.y)+f1) + (np.abs(ic2.y)+f2)
    dx = np.max(ic1.x) - np.min(ic1.x)
    cdiff.x = np.array(ic1.x)
    cdiff.y = np.array(ydiff/yden)

    cint = curve.Curve('', 'Integral(FD)/dX')
    yint = scipy.integrate.cumtrapz(cdiff.y, cdiff.x, initial=0.0)
    cint.x = np.array(ic1.x)
    cint.y = np.array(yint/dx)

    return cdiff, cint
//  [python]
def diffMeasure(c1, c2, tol=1e-8):
    """
    Compare two curves. For the given curves a fractional difference measure and its average are computed.

    >>> curves = pydvif.read('testData.txt')

    >>> c1, c2  = pydvif.diffMeasure(curves[0], curves[1])

    >>> curves.append(c1)

    >>> curves.append(c2)

    >>> pydvif.create_plot(curves, legend=True)

    :param c1: The first curve
    :type c1: Curve
    :param c2: The second curve
    :type c2: Curve
    :param tol: The tolerance
    :type tol: float
    :return: tuple -- Two curves representing the fractional difference measure and its average
    """
    cdiff = curve.Curve('', 'FD = $|$' + __toCurveString(c1) + ' - ' + __toCurveString(c2) + '$|$/($|$' + __toCurveString(c1) + '$|$ + $|$' + __toCurveString(c2) + '$|$)')
    ic1, ic2 = curve.getinterp(c1, c2)
    f1 = tol * (np.max(ic1.y) - np.min(ic1.y))
    f2 = tol * (np.max(ic2.y) - np.min(ic2.y))
    ydiff = np.abs(ic1.y - ic2.y)
    yden = (np.abs(ic1.y)+f1) + (np.abs(ic2.y)+f2)
    dx = np.max(ic1.x) - np.min(ic1.x)
    cdiff.x = np.array(ic1.x)
    cdiff.y = np.array(ydiff/yden)

    cint = curve.Curve('', 'Integral(FD)/dX')
    yint = scipy.integrate.cumulative_trapezoid(cdiff.y, cdiff.x, initial=0.0)
    cint.x = np.array(ic1.x)
    cint.y = np.array(yint/dx)

    return cdiff, cint
//  [python]
def zbins_const_dr(expt, cosmo, bins=None, nsamples=500, initial_dz=None):
    """
    Return redshift bin edges and centroids for bins that are equally-spaced 
    in r(z). Will either split the full range into some number of bins, or else 
    fill the range using bins of const. dr. set from an initial bin delta_z.
    """
    # Get redshift range
    zmin = expt['nu_line'] / expt['survey_numax'] - 1.
    zmax = expt['nu_line'] / (expt['survey_numax'] - expt['survey_dnutot']) - 1.
    
    # Fiducial cosmological values
    _z = np.linspace(0., zmax, nsamples)
    a = 1. / (1. + _z)
    H0 = (100.*cosmo['h']); w0 = cosmo['w0']; wa = cosmo['wa']
    om = cosmo['omega_M_0']; ol = cosmo['omega_lambda_0']
    ok = 1. - om - ol
    
    # Sample comoving dist. r(z) at discrete points (assumes ok~0 in last step)
    omegaDE = ol * np.exp(3.*wa*(a - 1.)) / a**(3.*(1. + w0 + wa))
    E = np.sqrt( om * a**(-3.) + ok * a**(-2.) + omegaDE )
    _r = (C/H0) * np.concatenate( ([0.], scipy.integrate.cumtrapz(1./E, _z)) )
    
    # Interpolate to get z(r) and r(z)
    r_z = scipy.interpolate.interp1d(_z, _r, kind='linear')
    z_r = scipy.interpolate.interp1d(_r, _z, kind='linear')
    
    # Return bin edges and centroids
    if bins is not None:
        # Get certain no. of bins
        rbins = np.linspace(r_z(zmin), r_z(zmax), bins+1)
        zbins = z_r(rbins)
    else:
        # Get bins with const. dr from initial delta_z
        rmin = r_z(zmin)
        rmax = r_z(zmax)
        dr = r_z(zmin + initial_dz) - rmin
        print("Const. dr =", dr, "Mpc")
        
        # Loop through range, filling with const. dr bins as far as possible
        rtot = rmin
        zbins = []
        while rtot < rmax:
            zbins.append(z_r(rtot))
            rtot += dr
        zbins.append(z_r(rmax))
        zbins = np.array(zbins)
        
    zc = [0.5*(zbins[i+1] + zbins[i]) for i in range(zbins.size - 1)]
    return zbins, np.array(zc)
//  [python]
def zbins_const_dr(expt, cosmo, bins=None, nsamples=500, initial_dz=None):
    """
    Return redshift bin edges and centroids for bins that are equally-spaced 
    in r(z). Will either split the full range into some number of bins, or else 
    fill the range using bins of const. dr. set from an initial bin delta_z.
    """
    # Get redshift range
    zmin = expt['nu_line'] / expt['survey_numax'] - 1.
    zmax = expt['nu_line'] / (expt['survey_numax'] - expt['survey_dnutot']) - 1.
    
    # Fiducial cosmological values
    _z = np.linspace(0., zmax, nsamples)
    a = 1. / (1. + _z)
    H0 = (100.*cosmo['h']); w0 = cosmo['w0']; wa = cosmo['wa']
    om = cosmo['omega_M_0']; ol = cosmo['omega_lambda_0']
    ok = 1. - om - ol
    
    # Sample comoving dist. r(z) at discrete points (assumes ok~0 in last step)
    omegaDE = ol * np.exp(3.*wa*(a - 1.)) / a**(3.*(1. + w0 + wa))
    E = np.sqrt( om * a**(-3.) + ok * a**(-2.) + omegaDE )
    _r = (C/H0) * np.concatenate( ([0.], scipy.integrate.cumulative_trapezoid(1./E, _z)) )
    
    # Interpolate to get z(r) and r(z)
    r_z = scipy.interpolate.interp1d(_z, _r, kind='linear')
    z_r = scipy.interpolate.interp1d(_r, _z, kind='linear')
    
    # Return bin edges and centroids
    if bins is not None:
        # Get certain no. of bins
        rbins = np.linspace(r_z(zmin), r_z(zmax), bins+1)
        zbins = z_r(rbins)
    else:
        # Get bins with const. dr from initial delta_z
        rmin = r_z(zmin)
        rmax = r_z(zmax)
        dr = r_z(zmin + initial_dz) - rmin
        print("Const. dr =", dr, "Mpc")
        
        # Loop through range, filling with const. dr bins as far as possible
        rtot = rmin
        zbins = []
        while rtot < rmax:
            zbins.append(z_r(rtot))
            rtot += dr
        zbins.append(z_r(rmax))
        zbins = np.array(zbins)
        
    zc = [0.5*(zbins[i+1] + zbins[i]) for i in range(zbins.size - 1)]
    return zbins, np.array(zc)
//  [python]
def _logC(self, m, beta, deltam, ml):
        '''
        Gives inverse log integral of  p(m1, m2) dm2 (i.e. log C(m1) in the LVC notation )
        '''
        res = 200
        exact_th=0.
        
        xlow=np.linspace(ml, ml+deltam+deltam/10, 200)
        xup=np.linspace(ml+deltam+deltam/10+1e-01, m[~np.isnan(m)].max(), res)
        xx=np.sort(np.concatenate([xlow,xup], ))
  
        p2 = np.exp(self._logpdfm2( xx , beta, deltam, ml))
        cdf = cumtrapz(p2, xx)
        
        where_compute = ~np.isnan(m)
        #where_exact = m <exact_th*ml
        
        #where_approx = (~where_exact) & (where_compute)
        
        result = np.empty_like(m)
        
        result[~where_compute]=np.NINF
        #result[where_exact]=self._logCexact(m[where_exact], beta, deltam, ml,) #np.NINF
        
        result[where_compute] = -np.log( np.interp(m[where_compute], xx[1:], cdf) )
        
        return result
//  [python]
def _logC(self, m, beta, deltam, ml):
        '''
        Gives inverse log integral of  p(m1, m2) dm2 (i.e. log C(m1) in the LVC notation )
        '''
        res = 200
        exact_th=0.
        
        xlow=np.linspace(ml, ml+deltam+deltam/10, 200)
        xup=np.linspace(ml+deltam+deltam/10+1e-01, m[~np.isnan(m)].max(), res)
        xx=np.sort(np.concatenate([xlow,xup], ))
  
        p2 = np.exp(self._logpdfm2( xx , beta, deltam, ml))
        cdf = cumulative_trapezoid(p2, xx)
        
        where_compute = ~np.isnan(m)
        #where_exact = m <exact_th*ml
        
        #where_approx = (~where_exact) & (where_compute)
        
        result = np.empty_like(m)
        
        result[~where_compute]=np.NINF
        #result[where_exact]=self._logCexact(m[where_exact], beta, deltam, ml,) #np.NINF
        
        result[where_compute] = -np.log( np.interp(m[where_compute], xx[1:], cdf) )
        
        return result
//  [python]
def _logC(self, m, beta, deltam, ml, res = 200, exact_th=0.):
        '''
        Gives inverse log integral of  p(m1, m2) dm2 (i.e. log C(m1) in the LVC notation )
        '''
        
        #print('_logC call')
        #print('Input m1 shape: %s' %str(m.shape))
        
        #print(m)
        #print(ml+deltam+deltam/10+1e-01)
        #print(m[~np.isnan(m)].max())
        
        
        xlow=np.linspace(ml, ml+deltam+deltam/10, 200)
        xup=np.linspace(ml+deltam+deltam/10+1e-01, m[~np.isnan(m)].max(), res)
        xx=np.sort(np.concatenate([xlow,xup], ))
  
        p2 = np.exp(self._logpdfm2( xx , beta, deltam, ml))
        cdf = cumtrapz(p2, xx)
        
        where_compute = ~np.isnan(m)
        #where_exact = m <exact_th*ml
        
        #where_approx = (~where_exact) & (where_compute)
        
        
        result = np.zeros(m.shape)
        
        result[~where_compute]=np.NINF
        result[where_compute] = -np.log( np.interp(m[where_compute], xx[1:], cdf) )
        
        #result = np.where(where_compute, -np.log( np.interp(m, xx[1:], cdf) ) , np.NINF)
        #print('Out res shape: %s' %str(result.shape))
        return result
//  [python]
def _logC(self, m, beta, deltam, ml, res = 200, exact_th=0.):
        '''
        Gives inverse log integral of  p(m1, m2) dm2 (i.e. log C(m1) in the LVC notation )
        '''
        
        #print('_logC call')
        #print('Input m1 shape: %s' %str(m.shape))
        
        #print(m)
        #print(ml+deltam+deltam/10+1e-01)
        #print(m[~np.isnan(m)].max())
        
        
        xlow=np.linspace(ml, ml+deltam+deltam/10, 200)
        xup=np.linspace(ml+deltam+deltam/10+1e-01, m[~np.isnan(m)].max(), res)
        xx=np.sort(np.concatenate([xlow,xup], ))
  
        p2 = np.exp(self._logpdfm2( xx , beta, deltam, ml))
        cdf = cumulative_trapezoid(p2, xx)
        
        where_compute = ~np.isnan(m)
        #where_exact = m <exact_th*ml
        
        #where_approx = (~where_exact) & (where_compute)
        
        
        result = np.zeros(m.shape)
        
        result[~where_compute]=np.NINF
        result[where_compute] = -np.log( np.interp(m[where_compute], xx[1:], cdf) )
        
        #result = np.where(where_compute, -np.log( np.interp(m, xx[1:], cdf) ) , np.NINF)
        #print('Out res shape: %s' %str(result.shape))
        return result
//  [python]
def integrate(data_dict, key = 'dp'):
    """
    Integrate dps.

    args:
        data_dict (dict): Data. Data must contain key 'time'.

    returns:
        (dict): dict with keys 'int' and 'time'. 'int' is 
            ```
            scipy.integrate.cumtrapz(key, x = 'time')
            ```
    """ 

    assert 'time' in set(data_dict.keys()), "data_dict must contain key 'time'. It does not. Keys are {}".format(data_dict.keys())

    dp_ida = iterable_data_array(data_dict, key)
    time_ida = iterable_data_array(data_dict, 'time')

    out = {'time':data_array_builder(), 'int':data_array_builder()}

    for dp, time in zip(dp_ida, time_ida):
        dp = np.nan_to_num(dp, 0)
        time = np.nan_to_num(time, 0)
        intdp = cumtrapz(dp, x = time)
        intdp = np.concatenate((np.array([0]), intdp))
        out['time'].append(time)
        out['int'].append(intdp)

    return {key: out[key].build() for key in out}
//  [python]
def integrate(data_dict, key = 'dp'):
    """
    Integrate dps.

    args:
        data_dict (dict): Data. Data must contain key 'time'.

    returns:
        (dict): dict with keys 'int' and 'time'. 'int' is 
            
            scipy.integrate.cumulative_trapezoid(key, x = 'time')
            
    """ 

    assert 'time' in set(data_dict.keys()), "data_dict must contain key 'time'. It does not. Keys are {}".format(data_dict.keys())

    dp_ida = iterable_data_array(data_dict, key)
    time_ida = iterable_data_array(data_dict, 'time')

    out = {'time':data_array_builder(), 'int':data_array_builder()}

    for dp, time in zip(dp_ida, time_ida):
        dp = np.nan_to_num(dp, 0)
        time = np.nan_to_num(time, 0)
        intdp = cumulative_trapezoid(dp, x = time)
        intdp = np.concatenate((np.array([0]), intdp))
        out['time'].append(time)
        out['int'].append(intdp)

    return {key: out[key].build() for key in out}
//  [python]
def check_true_derivatives(sol, tol=5e-3):
    d = integrate.cumtrapz(sol.v, sol.t, initial=0, axis=1)
    v = integrate.cumtrapz(sol.a, sol.t, initial=0, axis=1)
    derr = abs((d + sol.d[:, :1]) - sol.d).max() / abs(sol.d).max()
    verr = abs((v + sol.v[:, :1]) - sol.v).max() / abs(sol.v).max()
    assert derr < tol
    assert verr < tol
//  [python]
def check_true_derivatives(sol, tol=5e-3):
    d = integrate.cumulative_trapezoid(sol.v, sol.t, initial=0, axis=1)
    v = integrate.cumulative_trapezoid(sol.a, sol.t, initial=0, axis=1)
    derr = abs((d + sol.d[:, :1]) - sol.d).max() / abs(sol.d).max()
    verr = abs((v + sol.v[:, :1]) - sol.v).max() / abs(sol.v).max()
    assert derr < tol
    assert verr < tol
//  [python]
def GetPlasmaDensity(rate,ngas,dt):
	'''Get plasma density from rate, accounting for gas depletion.
	This is also provided as a method of ``Ionizer``.

	:param numpy.array rate: ionization rate with shape (Nt,Nx,Ny), any units consistent with dt.
	:param numpy.array ngas: gas density with shape (Nx,Ny), any units.
	:param double dt: time step, any units consistent with rate
	:returns: electron density in same units as ngas.'''
	ne_ng = scipy.integrate.cumtrapz(rate[::-1],dx=dt,axis=0,initial=0.0)[::-1]
	return ngas[np.newaxis,...]*(1.0 - np.exp(-ne_ng))
//  [python]
def GetPlasmaDensity(rate,ngas,dt):
	'''Get plasma density from rate, accounting for gas depletion.
	This is also provided as a method of ``Ionizer``.

	:param numpy.array rate: ionization rate with shape (Nt,Nx,Ny), any units consistent with dt.
	:param numpy.array ngas: gas density with shape (Nx,Ny), any units.
	:param double dt: time step, any units consistent with rate
	:returns: electron density in same units as ngas.'''
	ne_ng = scipy.integrate.cumulative_trapezoid(rate[::-1],dx=dt,axis=0,initial=0.0)[::-1]
	return ngas[np.newaxis,...]*(1.0 - np.exp(-ne_ng))
//  [python]
def q_to_curve(q):
    """Include docstring

    Args:
        q:

    """
    #! NEED TO UNDERSTAND WHAT IS GOING ON IN THIS FUNCTION and give appropriate comments
    T = q.shape[1]
    qnorm = np.sqrt(q[0,:]**2 + q[1,:]**2)
 
    p = np.zeros([2,T])
    for i in range(2):
        p[i,:] = scipy.integrate.cumtrapz(q[i,:]*qnorm, initial=0)/T 
    return p
//  [python]
def q_to_curve(q):
    """Include docstring

    Args:
        q:

    """
    T = q.shape[1]
    qnorm = np.sqrt(q[0,:]**2 + q[1,:]**2)
 
    p = np.zeros([2,T])
    for i in range(2):
        p[i,:] = scipy.integrate.cumulative_trapezoid(q[i,:]*qnorm, initial=0)/T 
    return p