pynkowski.data.utils_da

This submodule contains some utilities for data fields.

  1'''This submodule contains some utilities for data fields.'''
  2import numpy as np
  3import healpy as hp
  4
  5
  6def get_theta(nside):
  7    """Define a HEALPix map with the value of θ in each pixel at the input `nside`
  8    
  9    Parameters
 10    ----------
 11    nside : int
 12        The `nside` of the map
 13        
 14    Returns
 15    -------
 16    theta : np.array
 17        A healpix map with the value of theta in each pixel, in radians.
 18        
 19    """
 20    theta, _ = hp.pix2ang(nside, np.arange(12 * nside ** 2))
 21    return np.pi/2. - theta
 22
 23def healpix_derivatives(mapp, lmax=None, gradient=False, **kwargs):
 24    """Find the derivatives d_theta, d_phi of a Healpix map. It uses the healpy alm2map_der1 function.
 25
 26    Parameters
 27    ----------
 28    mapp : np.array
 29        The Healpix map to find its derivatives.
 30    
 31    lmax : int, optional
 32        Maximum multipole to get the alm and the derivatives. It can create numerical errors if it is too high. Default: 3*nside-1.
 33        
 34    gradient : bool, optional
 35        If True, return the covariant derivatives. If False, return the partial derivatives. Default: False.
 36        
 37    **kwargs :
 38        Extra keywords to pass to the map2alm function.
 39        
 40    Returns
 41    -------
 42    d_theta : np.array
 43        A healpix map with the derivative with respect to theta.
 44    
 45    d_phi : np.array
 46        A healpix map with the derivatives with respect to phi, without normalizing.
 47        
 48    """
 49    nside = hp.get_nside(mapp)
 50    if lmax is None:
 51        lmax = 3 * nside - 1
 52
 53    alm = hp.map2alm(mapp, lmax=lmax, **kwargs)
 54    [_, d_theta, d_phiosin] = hp.alm2map_der1(alm, nside, lmax=lmax)
 55    d_theta = -d_theta
 56
 57    if gradient:
 58        return (d_theta, d_phiosin)
 59    
 60    theta = get_theta(nside)
 61
 62    d_phi = d_phiosin * np.cos(theta)
 63    return (d_theta, d_phi)
 64
 65
 66
 67def healpix_second_derivatives(d_theta, d_phi, lmax=None, **kwargs):
 68    """Find the second partial derivatives for every pixel of a Healpix map given the first partial derivatives.
 69
 70    Parameters
 71    ----------
 72    d_theta : np.array
 73        The partial theta derivative of the Healpix map.
 74    
 75    d_phi : np.array
 76        The partial phi derivative of the Healpix map.
 77    
 78    lmax : int, optional
 79        Maximum multipole to get the alm and the derivatives. It can create numerical errors if it is too high. Default: 3*nside-1.
 80        
 81    Returns
 82    -------
 83    d_thetatheta : np.array
 84        A Healpix map of the second partial derivative wrt theta.
 85    
 86    d_phiphi : np.array
 87        A Healpix map of the second partial derivative wrt phi.
 88        
 89    d_phitheta : np.array
 90        A Healpix map of the second partial derivative wrt theta and phi.
 91        
 92    """
 93    nside = hp.get_nside(d_theta)
 94
 95    theta = get_theta(nside)
 96
 97    if lmax is None:
 98        lmax = 3 * nside - 1
 99
100    d_phitheta, d_phiphi = healpix_derivatives(d_phi, lmax=lmax, **kwargs)
101
102    alm_theta = hp.map2alm(d_theta, lmax=lmax, **kwargs)
103    [_, d_thetatheta, _] = hp.alm2map_der1(alm_theta, nside, lmax=lmax)
104    d_thetatheta = -d_thetatheta
105
106    
107    return (d_thetatheta, d_phiphi, d_phitheta)
108
109__all__ = ["get_theta", "healpix_derivatives", "healpix_second_derivatives"]
110__docformat__ = "numpy"
def get_theta(nside):
 7def get_theta(nside):
 8    """Define a HEALPix map with the value of θ in each pixel at the input `nside`
 9    
10    Parameters
11    ----------
12    nside : int
13        The `nside` of the map
14        
15    Returns
16    -------
17    theta : np.array
18        A healpix map with the value of theta in each pixel, in radians.
19        
20    """
21    theta, _ = hp.pix2ang(nside, np.arange(12 * nside ** 2))
22    return np.pi/2. - theta

Define a HEALPix map with the value of θ in each pixel at the input nside

Parameters
  • nside (int): The nside of the map
Returns
  • theta (np.array): A healpix map with the value of theta in each pixel, in radians.
def healpix_derivatives(mapp, lmax=None, gradient=False, **kwargs):
24def healpix_derivatives(mapp, lmax=None, gradient=False, **kwargs):
25    """Find the derivatives d_theta, d_phi of a Healpix map. It uses the healpy alm2map_der1 function.
26
27    Parameters
28    ----------
29    mapp : np.array
30        The Healpix map to find its derivatives.
31    
32    lmax : int, optional
33        Maximum multipole to get the alm and the derivatives. It can create numerical errors if it is too high. Default: 3*nside-1.
34        
35    gradient : bool, optional
36        If True, return the covariant derivatives. If False, return the partial derivatives. Default: False.
37        
38    **kwargs :
39        Extra keywords to pass to the map2alm function.
40        
41    Returns
42    -------
43    d_theta : np.array
44        A healpix map with the derivative with respect to theta.
45    
46    d_phi : np.array
47        A healpix map with the derivatives with respect to phi, without normalizing.
48        
49    """
50    nside = hp.get_nside(mapp)
51    if lmax is None:
52        lmax = 3 * nside - 1
53
54    alm = hp.map2alm(mapp, lmax=lmax, **kwargs)
55    [_, d_theta, d_phiosin] = hp.alm2map_der1(alm, nside, lmax=lmax)
56    d_theta = -d_theta
57
58    if gradient:
59        return (d_theta, d_phiosin)
60    
61    theta = get_theta(nside)
62
63    d_phi = d_phiosin * np.cos(theta)
64    return (d_theta, d_phi)

Find the derivatives d_theta, d_phi of a Healpix map. It uses the healpy alm2map_der1 function.

Parameters
  • mapp (np.array): The Healpix map to find its derivatives.
  • lmax (int, optional): Maximum multipole to get the alm and the derivatives. It can create numerical errors if it is too high. Default: 3*nside-1.
  • gradient (bool, optional): If True, return the covariant derivatives. If False, return the partial derivatives. Default: False.
  • **kwargs :: Extra keywords to pass to the map2alm function.
Returns
  • d_theta (np.array): A healpix map with the derivative with respect to theta.
  • d_phi (np.array): A healpix map with the derivatives with respect to phi, without normalizing.
def healpix_second_derivatives(d_theta, d_phi, lmax=None, **kwargs):
 68def healpix_second_derivatives(d_theta, d_phi, lmax=None, **kwargs):
 69    """Find the second partial derivatives for every pixel of a Healpix map given the first partial derivatives.
 70
 71    Parameters
 72    ----------
 73    d_theta : np.array
 74        The partial theta derivative of the Healpix map.
 75    
 76    d_phi : np.array
 77        The partial phi derivative of the Healpix map.
 78    
 79    lmax : int, optional
 80        Maximum multipole to get the alm and the derivatives. It can create numerical errors if it is too high. Default: 3*nside-1.
 81        
 82    Returns
 83    -------
 84    d_thetatheta : np.array
 85        A Healpix map of the second partial derivative wrt theta.
 86    
 87    d_phiphi : np.array
 88        A Healpix map of the second partial derivative wrt phi.
 89        
 90    d_phitheta : np.array
 91        A Healpix map of the second partial derivative wrt theta and phi.
 92        
 93    """
 94    nside = hp.get_nside(d_theta)
 95
 96    theta = get_theta(nside)
 97
 98    if lmax is None:
 99        lmax = 3 * nside - 1
100
101    d_phitheta, d_phiphi = healpix_derivatives(d_phi, lmax=lmax, **kwargs)
102
103    alm_theta = hp.map2alm(d_theta, lmax=lmax, **kwargs)
104    [_, d_thetatheta, _] = hp.alm2map_der1(alm_theta, nside, lmax=lmax)
105    d_thetatheta = -d_thetatheta
106
107    
108    return (d_thetatheta, d_phiphi, d_phitheta)

Find the second partial derivatives for every pixel of a Healpix map given the first partial derivatives.

Parameters
  • d_theta (np.array): The partial theta derivative of the Healpix map.
  • d_phi (np.array): The partial phi derivative of the Healpix map.
  • lmax (int, optional): Maximum multipole to get the alm and the derivatives. It can create numerical errors if it is too high. Default: 3*nside-1.
Returns
  • d_thetatheta (np.array): A Healpix map of the second partial derivative wrt theta.
  • d_phiphi (np.array): A Healpix map of the second partial derivative wrt phi.
  • d_phitheta (np.array): A Healpix map of the second partial derivative wrt theta and phi.