tjpcov.covariance_builder
=========================

.. py:module:: tjpcov.covariance_builder


Classes
-------

.. autoapisummary::

   tjpcov.covariance_builder.CovarianceBuilder
   tjpcov.covariance_builder.CovarianceFourier
   tjpcov.covariance_builder.CovarianceReal
   tjpcov.covariance_builder.CovarianceProjectedReal


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

.. py:class:: CovarianceBuilder(config)

   Bases: :py:obj:`abc.ABC`


   Base class in charge of building the full covariance.

   Class in charge of building the full covariance needed for the sacc
   file from individual covariance blocks. This is meant to be used as a
   parent class and the child classes would actually implement the actual
   computation of the blocks.

   Init the base class with a config file or dictionary.

   :param config: If dict, it returns the configuration
                  dictionary directly. If string, it asumes a YAML file and
                  parses it.
   :type config: dict or str


   .. py:property:: covariance_block_data_type
      :type: str


      The covariance block data type for your builder.

      :returns: Covariance block sacc data type
      :rtype: str


   .. py:attribute:: io


   .. py:attribute:: cosmo
      :value: None



   .. py:attribute:: bias_lens


   .. py:attribute:: IA


   .. py:attribute:: Ngal


   .. py:attribute:: sigma_e


   .. py:attribute:: cov
      :value: None



   .. py:attribute:: nbpw
      :value: None



   .. py:attribute:: _block_data_type
      :value: None



   .. py:attribute:: mask_files


   .. py:attribute:: mask_names


   .. py:attribute:: nside


   .. py:method:: _split_tasks_by_rank(tasks)

      Yield the tasks corresponding to the given process.

      Iterate through a list of items, yielding the ones this process is
      responsible for. The tasks are allocated in a round-robin way.

      :param tasks: Tasks to split up
      :type tasks: iterable

      :returns: Tasks associated to this process
      :rtype: :obj:`generator`



   .. py:property:: _tracer_types

      Tuple with the tracer types (e.g. ("cl", "cl")).

      This is used to decide if the block covariance should be computed or is
      zero. For instance, if the class is meant to produce the covariance for
      Cells and the tracer types are clusters, the class should return 0.


   .. py:method:: _build_matrix_from_blocks(blocks, tracers_cov)

      Build full matrix from blocks.

      :param blocks: List of blocks
      :type blocks: list
      :param tracers_cov: List of tracer combinations corresponding to
                          each block in blocks. They must have the same order
      :type tracers_cov: list

      :returns: Covariance matrix for all combinations in the sacc file.
      :rtype: array



   .. py:method:: _compute_all_blocks(**kwargs)

      Compute all the independent covariance blocks.

      :param \*\*kwargs: Arguments to pass to the get_covariance_block method.
                         These will depend on the covariance type requested.

      :returns:

                    - blocks (list): List of all the independent super sample
                      covariance blocks.
                    - tracer_blocks (list): List of all tracer combinations in
                      order as the blocks.
      :rtype: tuple



   .. py:method:: get_cosmology()

      Return a CCL Cosmology instance.

      The Cosmology is generated with the information passed in the
      configuration file in the "cosmo" section of "tjpcov". This can be a
      file path to a yaml, pickle object or "set" to read the parameters from
      the "parameters" section.

      :returns: :obj:`pyccl.Cosmology` instance



   .. py:method:: get_covariance_block(tracer_comb1, tracer_comb2, **kwargs)
      :abstractmethod:


      Return the covariance block for the two pair of tracers.

      This is what you would get from an external code and could contain
      data types not present in the sacc file. For Fourier space covariances,
      we assume the same order as in NaMaster.

      :param tracer_comb1: List of the pair of tracer names of C_ell^1
      :type tracer_comb1: list
      :param tracer_comb2: List of the pair of tracer names of C_ell^2
      :type tracer_comb2: list
      :param \*\*kwargs: extra possible arguments

      :returns: Covariance block
      :rtype: array



   .. py:method:: _get_covariance_block_for_sacc(tracer_comb1, tracer_comb2, **kwargs)
      :abstractmethod:


      Return the covariance block as needed for the sacc file.

      For instance, if the sacc file does not contain B-modes, the covariance
      block would only have the EE-EE component). Furthermore, the ordering
      must be the same as in the sacc file.

      :param tracer_comb1: List of the pair of tracer names of C_ell^1
      :type tracer_comb1: list
      :param tracer_comb2: List of the pair of tracer names of C_ell^2
      :type tracer_comb2: list
      :param \*\*kwargs: Arguments accepted by get_covariance_block

      :returns: Covariance block
      :rtype: array



   .. py:method:: get_covariance_block_for_sacc(tracer_comb1, tracer_comb2, **kwargs)

      Return the covariance block as needed for the sacc file.

      For instance, if the sacc file does not contain B-modes, the covariance
      block would only have the EE-EE component). Furthermore, the ordering
      must be the same as in the sacc file.

      This function returns 0 if the covariance class is not meant to produce
      covariances for the given tracer combination.

      :param tracer_comb1: List of the pair of tracer names of C_ell^1
      :type tracer_comb1: list
      :param tracer_comb2: List of the pair of tracer names of C_ell^2
      :type tracer_comb2: list
      :param \*\*kwargs: Arguments accepted by get_covariance_block

      :returns: Covariance block
      :rtype: array



   .. py:method:: get_covariance(**kwargs)

      Return the full covariance.

      :param \*\*kwargs: Arguments to pass to _compute_all_blocks.

      :returns: Full covariance
      :rtype: array



   .. py:method:: get_list_of_tracers_for_cov()

      Return the covariance independent tracers combinations.

      :returns: List of independent tracers combinations.
      :rtype: list of str



   .. py:method:: get_mask_names_dict(tracer_names)

      Return a dictionary with the mask names for the given tracers.

      :param tracer_names: Dictionary of the tracer names of the same
                           form as mask_name. It has to be given as {1: name1, 2: name2,
                           3: name3, 4: name4}, where 12 and 34 are the pair of tracers
                           that go into the first and second Cell you are computing the
                           covariance for; i.e. <Cell^12 Cell^34>.
      :type tracer_names: dict

      :returns: Dictionary with the mask names assotiated to the fields to
                be correlated.
      :rtype: dict



   .. py:method:: get_masks_dict(tracer_names, cache=None)

      Return a dictionary with the masks assotiated to the given tracers.

      :param tracer_names: Dictionary of the tracer names of the same
                           form as mask_name. It has to be given as {1: name1, 2: name2,
                           3: name3, 4: name4}, where 12 and 34 are the pair of tracers
                           that go into the first and second Cell you are computing the
                           covariance for; i.e. <Cell^12 Cell^34>.
      :type tracer_names: dict
      :param cache: Dictionary with cached variables. It will
                    use the cached masks if found. The keys must be 'm1', 'm2',
                    'm3' or 'm4' and the values the loaded maps.
      :type cache: dict, optional

      :returns: Dictionary with the masks assotiated to the fields to be
                correlated.
      :rtype: dict



   .. py:method:: get_nbpw()

      Return the number of bandpowers in which the data has been binned.

      :returns: Number of bandpowers; i.e. ell_effective.size
      :rtype: int



   .. py:method:: get_tracers_spin_dict(tracer_names)

      Return a dictionary with the spins assotiated to the given tracers.

      :param tracer_names: Dictionary of the tracer names of the same
                           form as mask_name. It has to be given as {1: name1, 2: name2,
                           3: name3, 4: name4}, where 12 and 34 are the pair of tracers
                           that go into the first and second Cell you are computing the
                           covariance for; i.e. <Cell^12 Cell^34>.
      :type tracer_names: dict

      :returns: Dictionary with the spins assotiated to the fields to be
                correlated.
      :rtype: dict



   .. py:method:: get_tracer_comb_spin(tracer_comb)

      Return the spins of a pair of tracers.

      :param tracer_comb: List or tuple of a pair of tracer names
      :type tracer_comb: tuple

      :returns:     - s1 (int):  Spin of the first tracer
                    - s2 (int):  Spin of the second tracer
      :rtype: tuple



   .. py:method:: get_tracer_spin(tracer)

      Return the spin of a given tracer.

      :param sacc_data: Data Sacc instance
      :type sacc_data: Sacc
      :param tracer: Tracer name
      :type tracer: str

      :returns: Spin of the given tracer
      :rtype: int



   .. py:method:: get_tracer_nmaps(tracer)

      Return the number of maps assotiated to the given tracer.

      :param sacc_data: Data Sacc instance
      :type sacc_data: Sacc
      :param tracer: Tracer name
      :type tracer: str

      :returns: Number of maps assotiated to the tracer.
      :rtype: int



   .. py:method:: get_tracer_comb_data_types(tracer_comb)

      Return the tracer data types associated to the given tracers.

      :param tracer_comb: List of a pair of tracer names in the sacc file
      :type tracer_comb: list

      :returns: List of data types associated to the given tracer pair.
      :rtype: list



.. py:class:: CovarianceFourier(config)

   Bases: :py:obj:`CovarianceBuilder`


   Parent class for Cell x Cell covariances in Fourier space.

   This has all the methods common to all the Fourier covariance calculations.
   The child classes will actually implement the actual computation of the
   blocks.

   Init the base class with a config file or dictionary.

   :param config: If dict, it returns the configuration
                  dictionary directly. If string, it asumes a YAML file and
                  parses it.
   :type config: dict or str


   .. py:attribute:: space_type
      :value: 'Fourier'



   .. py:attribute:: _tracer_types
      :value: ('cl', 'cl')


      Tuple with the tracer types (e.g. ("cl", "cl")).

      This is used to decide if the block covariance should be computed or is
      zero. For instance, if the class is meant to produce the covariance for
      Cells and the tracer types are clusters, the class should return 0.


   .. py:attribute:: ccl_tracers
      :value: None



   .. py:attribute:: tracer_Noise
      :value: None



   .. py:attribute:: tracer_Noise_coupled
      :value: None



   .. py:method:: _get_covariance_block_for_sacc(tracer_comb1, tracer_comb2, **kwargs)

      Return the covariance block as needed for the sacc file.

      For instance, if the sacc file does not contain B-modes, the covariance
      block would only have the EE-EE component). Furthermore, the ordering
      must be the same as in the sacc file.

      :param tracer_comb 1: List of the pair of tracer names of C_ell^1
      :type tracer_comb 1: list
      :param tracer_comb 2: List of the pair of tracer names of C_ell^2
      :type tracer_comb 2: list
      :param \*\*kwargs: Arguments accepted by get_covariance_block

      :returns: Covariance matrix for a pair of C_ell for the data
                types considered in the sacc file.
      :rtype: array



   .. py:method:: get_datatypes_from_ncell(ncell)

      Return the datatypes (e.g cl_00) for a the number of cells.

      :param ncell: Number of Cell for a pair of tracers
      :type ncell: int

      :returns: List of data types assotiated to the given degrees of freedom
      :rtype: list



   .. py:method:: get_ell_eff()

      Return the effective ell in the sacc file.

      It assume that all of them have the same effective ell (true with
      current TXPipe implementation).

      :param sacc_data: Data Sacc instance
      :type sacc_data: :obj:`sacc.sacc.Sacc`

      :returns: Array with the effective ell in the sacc file.
      :rtype: array



   .. py:method:: get_binning_info()

      Return all ells used for the different bandpowers, their effective
      ell and bandpower edges based on the SACC information.

      It assume that all tracers have the same bandpower windows.

      :param sacc_data: Data Sacc instance
      :type sacc_data: :obj:`sacc.sacc.Sacc`

      :returns: Array with the ells to pass to theory prediction.
                ell_eff: Array with effective ells from sacc
                ell_edges: Array with bandpower edges (assumed to be contiguous).
      :rtype: ells



   .. py:method:: get_sacc_with_concise_dtypes()

      Return a copy of the sacc file with concise data types.

      :returns: Data Sacc instance with concise data types
      :rtype: :obj:`sacc.sacc.Sacc`



   .. py:method:: get_tracer_comb_ncell(tracer_comb, independent=False)

      Return the number of Cell for a pair of tracers.

      For instance, for shear-shear, ncell = 4: EE, EB, BE, BB.

      :param sacc_data: Data Sacc instance
      :type sacc_data: Sacc
      :param tracer_comb: List or tuple of a pair of tracer names
      :type tracer_comb: tuple
      :param independent: If True, just return the number of
                          independent Cell.
      :type independent: bool, optional

      :returns: Number of Cell for the pair of tracers given
      :rtype: int



   .. py:method:: get_tracer_info(return_noise_coupled=False)

      Returns CCL tracer objects and the noise for all the tracers.

      This is done based on the specifications given in the configuration
      file.

      :param return_noise_coupled: If True, also return
                                   tracers_Noise_coupled. Default False.
      :type return_noise_coupled: bool, optional

      :returns:

                    - ccl_tracers (:obj:`pyccl.tracers.Tracer`): CCL tracers used
                      to compute the theory vector
                    - tracer_Noise (:obj:`dict`): shot (shape) noise for lens
                      (sources) tracers
                    - tracer_Noise_coupled (:obj:`dict`): coupled shot (shape)
                      noise for lens (sources). Returned if retrun_noise_coupled is
                      True.
      :rtype: tuple



.. py:class:: CovarianceReal(config)

   Bases: :py:obj:`CovarianceBuilder`


   Parent class for xi x xi covariances in Real space.

   This has all the methods common to all the Real covariance calculations.
   The child classes will actually implement the actual computation of the
   blocks.

   Init the base class with a config file or dictionary.

   :param config: If dict, it returns the configuration
                  dictionary directly. If string, it asumes a YAML file and
                  parses it.
   :type config: dict or str


   .. py:attribute:: space_type
      :value: 'Real'



   .. py:attribute:: _tracer_types
      :value: ('xi', 'xi')


      Tuple with the tracer types (e.g. ("cl", "cl")).

      This is used to decide if the block covariance should be computed or is
      zero. For instance, if the class is meant to produce the covariance for
      Cells and the tracer types are clusters, the class should return 0.


   .. py:method:: get_theta_eff()

      Return the effective theta in the sacc file.

      It assume that all of them have the same effective theta (true with
      current TXPipe implementation).

      :param sacc_data: Data Sacc instance
      :type sacc_data: :obj:`sacc.sacc.Sacc`

      :returns: Array with the effective theta in the sacc file.
      :rtype: array



.. py:class:: CovarianceProjectedReal(config)

   Bases: :py:obj:`CovarianceReal`


   Parent class for xi x xi covariances in Real space.

   This has all the methods common to all the Real covariance calculations
   that are computed by projecting the Fourier space ones. The child classes
   will only have to call a CovarianceFourier child to get the covariance in
   Fourier space.

   Init the base class with a config file or dictionary.

   :param config: If dict, it returns the configuration
                  dictionary directly. If string, it asumes a YAML file and
                  parses it.
   :type config: dict or str


   .. py:attribute:: WT
      :value: None



   .. py:attribute:: lmax


   .. py:property:: fourier


   .. py:method:: get_binning_info(binning='log', in_radians=True)

      Get the theta for bins given the sacc object.

      :param binning: Binning type.
      :type binning: str
      :param in_radians: If the angles must be given in radians. Needed
                         for the Wigner transforms.
      :type in_radians: bool

      :returns:     - theta (array): All the thetas covered
                    - theta_eff (array): The effective thetas
                    - theta_edges (array): The bandpower edges
      :rtype: tuple



   .. py:method:: get_cov_WT_spin(tracer_comb)

      Get the Wigner transform factors.

      :param tracer_comb: tracer combination in sacc format
      :type tracer_comb: list of two str

      :returns: WT_factors



   .. py:method:: get_Wigner_transform()

      Return an instance of the wigner_transform class.

      :returns: :obj:`~tjpcov.wigner_transform.WignerTransform` instance



   .. py:method:: _get_fourier_block(tracer_comb1, tracer_comb2)
      :abstractmethod:



   .. py:method:: get_covariance_block(tracer_comb1, tracer_comb2, xi_plus_minus1='plus', xi_plus_minus2='plus', binned=True)

      Compute a single covariance matrix for a given pair of xi.

      :param tracer_comb1: List of the pair of tracer names of C_ell^1
      :type tracer_comb1: list
      :param tracer_comb2: List of the pair of tracer names of C_ell^2
      :type tracer_comb2: list
      :param xi_plus_minus1: 'plus' if one wants the covariance for the
                             xi+ component or 'minus' for the xi-. This is ignored if
                             tracer_comb1 is not a spin 2 (e.g. shear) field.
      :type xi_plus_minus1: str
      :param xi_plus_minus2: As xi_plus_minus1 for tracer_comb2.
      :type xi_plus_minus2: str

      :returns: Covariance matrix
      :rtype: array



   .. py:method:: _get_covariance_block_for_sacc(tracer_comb1, tracer_comb2)

      Compute a the covariance matrix for a given pair of C_ell or xi.

      :param tracer_comb1: List of the pair of tracer names of C_ell^1
      :type tracer_comb1: list
      :param tracer_comb2: List of the pair of tracer names of C_ell^2
      :type tracer_comb2: list

      :returns: Covariance matrix
      :rtype: array



