flip.covariance.cov_utils
=========================

.. py:module:: flip.covariance.cov_utils


Attributes
----------

.. autoapisummary::

   flip.covariance.cov_utils.log


Functions
---------

.. autoapisummary::

   flip.covariance.cov_utils.compute_sep
   flip.covariance.cov_utils.compute_i_j
   flip.covariance.cov_utils.flatshape_to_fullshape
   flip.covariance.cov_utils.compute_i_j_cross_matrix
   flip.covariance.cov_utils.compute_phi
   flip.covariance.cov_utils.angle_separation
   flip.covariance.cov_utils.compute_phi_bisector_theorem
   flip.covariance.cov_utils.return_matrix_covariance
   flip.covariance.cov_utils.return_flat_covariance
   flip.covariance.cov_utils.return_flat_cross_cov
   flip.covariance.cov_utils.return_matrix_covariance_cross
   flip.covariance.cov_utils.return_correlation_matrix
   flip.covariance.cov_utils.save_matrix
   flip.covariance.cov_utils.open_matrix
   flip.covariance.cov_utils.generator_need
   flip.covariance.cov_utils.check_generator_need
   flip.covariance.cov_utils.generate_redshift_dict


Module Contents
---------------

.. py:data:: log

.. py:function:: compute_sep(ra, dec, comoving_distance, los_definition='bisector', size_batch=10000)

   The compute_sep function computes the separation between all pairs of objects in a catalog.

   :param ra: Store the right ascension of each object
   :param dec: Compute the angular separation between two objects
   :param comoving_distance: Calculate the separation between two objects
   :param size_batch: Control the number of objects that are processed at a time
   :param : Set the number of objects in a batch

   :returns: The separation, the perpendicular component of the separation, and


.. py:function:: compute_i_j(N, seq)

   The compute_i_j function takes in a number of nodes N and a sequence number seq.
   It returns the i, j indices for the upper triangular matrix that correspond to
   the given sequence number. The function is used to convert between an indexing scheme
   that uses integers from 0 to (N^2 - N)/2 - 1 and one that uses i,j indices where
   i = 0,...,N-2 and j = i+ 1,...,N-2.

   :param N: Determine the size of the matrix
   :param seq: Find the row and column of the element in a matrix

   :returns: The row and column of the lower triangle matrix


.. py:function:: flatshape_to_fullshape(flat_shape_non_diagonal)

.. py:function:: compute_i_j_cross_matrix(Nv, seq)

   The compute_i_j_cross_matrix function takes in the number of vertices, Nv, and a sequence of numbers
   and returns two arrays. The first array is an array containing the row indices for each element in the
   sequence. The second array contains column indices for each element in the sequence.

   :param Nv: Determine the number of vertices in a given face
   :param seq: Create the i and j arrays

   :returns: The indices of the cross-terms in the matrix


.. py:function:: compute_phi(ra_0, ra_1, dec_0, dec_1, r_0, r_1, los_definition)

   The compute_phi function computes the angle between the line of sight and
   the separation vector. The line of sight is defined as either:
       - mean: (r_0 + r_2) / 2, where r_0 and r_2 are the radial coordinates at each end of a pair.
       - bisector: (r_0 / |r| + r_2 / |r|), where |r| is the distance between two points in a pair.
       - endpoint: only use one point in a pair to define the line-of-sight direction, i.e., use only one galaxy

   :param ra_0: Compute the x_0, y_0 and z_0 coordinates of a galaxy
   :param ra_1: Compute the phi angle
   :param dec_0: Compute the z_0 parameter in radec2cart function
   :param dec_1: Compute the phi angle
   :param r_0: Compute the distance between two points
   :param r_1: Compute the distance between two points in the sky
   :param los_definition: Define the line of sight

   :returns: The angle between the line of sight and the separation vector


.. py:function:: angle_separation(ra_0, ra_1, dec_0, dec_1, r_0, r_1, los_definition='bisector')

   The angle_separation function computes the angle between two points on a sphere.

   :param ra_0: Set the right ascension of the first galaxy
   :param ra_1: Calculate the angle between two points in the sky
   :param dec_0: Define the declination of the first galaxy
   :param dec_1: Calculate the cosine of theta
   :param r_0: Compute the distance between two points
   :param r_1: Compute the distance between two points in space
   :param los_definition: Define the line of sight
   :param : Define the line of sight

   :returns: The separation between two points in


.. py:function:: compute_phi_bisector_theorem(r, theta, r_0, r_1)

.. py:function:: return_matrix_covariance(flat_covariance)

   Reconstructs a full covariance matrix from a flattened representation.

   The input `flat_covariance` is expected to have the diagonal value as its first element,
   followed by the upper-triangular (excluding diagonal) elements of the covariance matrix.

   :param flat_covariance: 1D array containing the diagonal value followed by
                           the upper-triangular elements of the covariance matrix.
   :type flat_covariance: np.ndarray

   :returns: The reconstructed full covariance matrix.
   :rtype: np.ndarray

   .. rubric:: Notes

   - The function uses `flatshape_to_fullshape` to determine the size of the full matrix.
   - The diagonal is set to the first value in `flat_covariance_matrix`.
   - The off-diagonal elements are filled symmetrically.


.. py:function:: return_flat_covariance(matrix_covariance)

   Flattens a covariance matrix into a one-dimensional array.

   The returned array starts with the variance value (element at position [0, 0]),
   followed by the upper triangular elements of the covariance matrix (excluding the diagonal).

   :param cov: A square covariance matrix.
   :type cov: numpy.ndarray

   :returns: A one-dimensional array containing the variance and upper triangular covariance values.
   :rtype: numpy.ndarray


.. py:function:: return_flat_cross_cov(cov)

.. py:function:: return_matrix_covariance_cross(cov, number_objects_g, number_objects_v)

   The return_matrix_covariance_cross function takes in a covariance matrix and the number of objects in each band.
   It then reshapes the covariance matrix into a full cross-covariance matrix, which is returned.

   :param cov: Reshape the covariance matrix
   :param number_objects_g: Reshape the covariance matrix into a full covariance matrix
   :param number_objects_v: Reshape the covariance matrix into a full covariance matrix

   :returns: The full covariance matrix


.. py:function:: return_correlation_matrix(cov)

   The return_correlation_matrix function takes a covariance matrix as input and returns the correlation matrix.
   The correlation matrix is calculated by dividing each element of the covariance matrix by the product of its row's standard deviation and column's standard deviation.

   :param cov: Calculate the correlation matrix

   :returns: The correlation matrix


.. py:function:: save_matrix(matrix, name)

   The save_matrix function takes a matrix and saves it to the current directory as a .npy file.

   :param matrix: Save the matrix to a file
   :param name: Save the matrix with a name

   :returns: The name of the file that was saved


.. py:function:: open_matrix(name)

   The open_matrix function takes in a string as an argument and returns the matrix that is saved under that name.

   :param name: Specify the name of the matrix to be loaded

   :returns: A matrix


.. py:function:: generator_need(coordinates_density=None, coordinates_velocity=None)

   The generator_need function checks if the coordinates_density and coordinates_velocity inputs are provided.
   If they are not, it raises a ValueError exception.


   :param coordinates_density: Generate the density covariance matrix
   :param coordinates_velocity: Generate the covariance matrix of the velocity field
   :param : Check if the coordinates are provided or not

   :returns: A list of the coordinates that are needed to proceed with covariance generation


.. py:function:: check_generator_need(model_kind, coordinates_density, coordinates_velocity)

   The check_generator_need function is used to check if the generator_need function
   is called with the correct arguments. The model type determines which coordinates are needed,
   and these are passed as arguments to generator_need.

   :param model_kind: Determine if the density, velocity or full model is being used
   :param coordinates_density: Check if the density coordinates are needed
   :param coordinates_velocity: Determine whether the velocity model is needed

   :returns: A boolean


.. py:function:: generate_redshift_dict(redshift_dependent_model, model_kind, redshift_velocity=None, redshift_density=None, coordinates_velocity=None, coordinates_density=None)

