pynkowski.data.healpix
This submodule contains the class for scalar fields in HEALPix format and an easy interface for $P^2$ maps in this format.
1'''This submodule contains the class for scalar fields in HEALPix format and an easy interface for $P^2$ maps in this format.''' 2import numpy as np 3import healpy as hp 4from .base_da import DataField, _masked_variance, _prepare_pixel_weights 5from .utils_da import get_theta, healpix_derivatives, healpix_second_derivatives 6 7def _hotspot(healpix_map): 8 """Find the local maxima of the input HEALPix map. 9 10 Returns 11 ------- 12 pixels : np.array 13 Indices of the pixels which are local maxima. 14 15 values : np.array 16 Values of input map which are local maxima. 17 18 """ 19 nside = hp.npix2nside(healpix_map.shape[0]) 20 neigh = hp.get_all_neighbours(nside, np.arange(12*nside**2)) 21 22 extT = np.concatenate([healpix_map, [np.min(healpix_map)-1.]]) 23 neigh_matrix = extT[neigh] 24 25 mask = np.all(healpix_map > neigh_matrix, axis=0) 26 pixels = np.argwhere(mask).flatten() 27 values = healpix_map[pixels] 28 return(pixels, values) 29 30 31class Healpix(DataField): 32 """Class for spherical scalar fields in HEALPix format. 33 34 Parameters 35 ---------- 36 field : np.array 37 Values of the scalar field in HEALPix format in RING scheme. The field must be finite: the pixels outside of the region of interest can be set to any finite value and excluded with `mask`. 38 39 normalise : bool, optional 40 If `True`, the map is normalised to have unit variance. Note: the mean of the map is not forced to be 0. 41 Default: `True`. 42 43 mask : np.array or None, optional 44 Mask where the field is considered. It is a bool array of the same shape that `field`. 45 Default: all data is included. 46 47 first_der : np.array or None, optional 48 First **covariant** derivatives of the field in an orthonormal basis of the space. Same structure as `field`, and shape `(dim, field.shape)`, in the order of `get_first_der` (θ, ϕ). 49 If given, they are used instead of computing them from the map. They must be given together with `second_der`, and they are normalised together with the field if `normalise=True`. 50 Default: computed when needed. 51 52 second_der : np.array or None, optional 53 Second **covariant** derivatives of the field in an orthonormal basis of the space. Same structure as `field`, and shape `(dim*(dim+1)/2, field.shape)`, in the order of `get_second_der` (θθ, ϕϕ, θϕ). 54 They must be given together with `first_der`. 55 Default: computed when needed. 56 57 pixel_weights : np.array or None, optional 58 Relative weight of each pixel in the averages over the mask. HEALPix pixels have equal areas, so this is only needed to weight the pixels differently. 59 It is an array of finite and non-negative values of the same shape that `field`. It is used by the Minkowski Functionals and by the normalisation of the field (weighted variance). 60 Default: all pixels have the same weight. 61 62 Attributes 63 ---------- 64 field : np.array 65 Data of the field as a HEALPix map in RING scheme. 66 67 nside : int 68 Parameter `nside` of the map. The number of pixels is `12*nside**2`. 69 70 dim : int 71 Dimension of the space where the field is defined. In this case, the space is the sphere and this is 2. 72 73 name : str 74 Name of the field. In this case, "HEALPix map" 75 76 first_der : np.array or None 77 First **covariant** derivatives of the field in an orthonormal basis of the space. Same structure as `field`, and shape `(dim, field.shape)`. 78 79 second_der : np.array or None 80 Second **covariant** derivatives of the field in an orthonormal basis of the space. Same structure as `field`, and shape `(dim*(dim+1)/2, field.shape)`. 81 The order of the derivatives is diagonal first, e.g. in `dim=3`: `11`, `22`, `33`, `12`, `13`, `23`. 82 83 mask : np.array 84 Mask where the field is considered. It is a bool array of the same shape that `field`. 85 86 pixel_weights : np.array or None 87 Relative weight of each pixel in the averages over the mask, or `None` if all pixels have the same weight. 88 89 """ 90 def __init__(self, field, normalise=True, mask=None, first_der=None, second_der=None, pixel_weights=None): 91 super().__init__(field, 2, name="HEALPix map", mask=mask, pixel_weights=pixel_weights) 92 self.nside = hp.get_nside(self.field) 93 if hp.get_nside(self.mask) != self.nside: 94 raise ValueError('The map and the mask have different nside') 95 self._check_finite() 96 sigma = 1. 97 if normalise: 98 # self.field -= self.field.mean() 99 sigma = np.sqrt(self.get_variance()) 100 self.field /= sigma 101 self._set_derivatives(first_der, second_der, scale=sigma) 102 103 def get_variance(self): 104 """Compute the variance of the Healpix scalar map within the mask, weighted by `pixel_weights` if given. 105 106 Returns 107 ------- 108 var : float 109 The variance of the Healpix map within the mask. 110 111 """ 112 return _masked_variance(self.field, self.mask, self.pixel_weights) 113 114 def get_first_der(self, lmax=None): 115 """Compute the covariant first derivatives of the input Healpix scalar map. 116 It stores: 117 118 - first covariant derivative wrt θ in self.first_der[0] 119 - first covariant derivative wrt ϕ in self.first_der[1] 120 121 Parameters 122 ---------- 123 lmax : int or None, optional 124 Maximum multipole used in the computation of the derivatives. 125 Default: 3*nside - 1 126 """ 127 self.first_der = np.array(healpix_derivatives(self.field, gradient=True, lmax=lmax)) # order: θ, ϕ 128 129 def get_second_der(self, lmax=None): 130 """Compute the covariant second derivatives of the input Healpix scalar map. 131 It stores: 132 133 - second covariant derivative wrt θθ in self.second_der[0] 134 - second covariant derivative wrt ϕϕ in self.second_der[1] 135 - second covariant derivative wrt θϕ in self.second_der[2] 136 137 Parameters 138 ---------- 139 lmax : int or None, optional 140 Maximum multipole used in the computation of the derivatives. 141 Default: 3*nside - 1 142 """ 143 if self.first_der is None: 144 self.get_first_der() 145 theta = get_theta(self.nside) 146 147 second_partial = healpix_second_derivatives(self.first_der[0], np.cos(theta) * self.first_der[1], lmax=lmax) #order θθ, ϕϕ, θϕ 148 149 self.second_der = np.array([second_partial[0], 150 second_partial[1]/np.cos(theta)**2. + self.first_der[0]*np.tan(theta), 151 (second_partial[2]/np.cos(theta) - self.first_der[1] * np.tan(theta))]) #order θθ, ϕϕ, θϕ 152 153 def maxima_list(self): 154 """Compute the values of the local maxima of the HEALPix map. 155 156 Returns 157 ------- 158 values : np.array 159 Values of the map which are local maxima. 160 161 """ 162 pixels, values = _hotspot(self.field) 163 return values[self.mask[pixels]] 164 165 def minima_list(self): 166 """Compute the values of the local minima of the HEALPix map. 167 168 Returns 169 ------- 170 values : np.array 171 Values of the map which are local minima. 172 173 """ 174 pixels, values = _hotspot(-self.field) 175 return -values[self.mask[pixels]] 176 177 178class HealpixP2(Healpix): 179 """Class for spherical $P^2 = Q^2 + U^2$ fields in HEALPix format, from the $Q$ and $U$ maps. 180 181 Parameters 182 ---------- 183 Q : np.array 184 Values of the Q component in HEALPix format in RING scheme. 185 186 U : np.array 187 Values of the U component in HEALPix format in RING scheme. 188 189 normalise : bool, optional 190 If `True`, Q and U are normalised to unit variance. Note: the normalisation is computed as the average of both variances, the mean of both maps are not forced to be 0. 191 192 mask : np.array or None, optional 193 Mask where the field if considered. It is a bool array of the same shape that `field`. 194 Default: all data is included. 195 196 first_der : np.array or None, optional 197 First **covariant** derivatives of $P^2$, computed from the input `Q` and `U` (before the normalisation). Same structure as `field`, and shape `(dim, field.shape)`, in the order of `get_first_der` (θ, ϕ). 198 They must be given together with `second_der`. If `normalise=True`, they are normalised together with $P^2$, i.e., divided by the average of the variances of `Q` and `U`. 199 Default: computed when needed. 200 201 second_der : np.array or None, optional 202 Second **covariant** derivatives of $P^2$, computed from the input `Q` and `U` (before the normalisation). Same structure as `field`, and shape `(dim*(dim+1)/2, field.shape)`, in the order of `get_second_der` (θθ, ϕϕ, θϕ). 203 They must be given together with `first_der`. 204 Default: computed when needed. 205 206 pixel_weights : np.array or None, optional 207 Relative weight of each pixel in the averages over the mask. HEALPix pixels have equal areas, so this is only needed to weight the pixels differently. 208 It is an array of finite and non-negative values of the same shape that `field`. It is used by the Minkowski Functionals and by the normalisation of the field (weighted variance). 209 Default: all pixels have the same weight. 210 211 Attributes 212 ---------- 213 Q : np.array 214 Data of the $Q$ component as a HEALPix map in RING scheme. 215 216 U : np.array 217 Data of the $U$ component as a HEALPix map in RING scheme. 218 219 field : np.array 220 Data of the $P^2$ field as a HEALPix map in RING scheme. 221 222 nside : int 223 Parameter `nside` of the map. The number of pixels is `12*nside**2`. 224 225 dim : int 226 Dimension of the space where the field is defined. In this case, the space is the sphere and this is 2. 227 228 name : str 229 Name of the field. In this case, "HEALPix map" 230 231 first_der : np.array or None 232 First **covariant** derivatives of the field in an orthonormal basis of the space. Same structure as `field`, and shape `(dim, field.shape)`. 233 234 second_der : np.array or None 235 Second **covariant** derivatives of the field in an orthonormal basis of the space. Same structure as `field`, and shape `(dim*(dim+1)/2, field.shape)`. 236 The order of the derivatives is diagonal first, e.g. in `dim=3`: `11`, `22`, `33`, `12`, `13`, `23`. 237 238 mask : np.array 239 Mask where the field if considered. It is a bool array of the same shape that `field`. 240 241 pixel_weights : np.array or None 242 Relative weight of each pixel in the averages over the mask, or `None` if all pixels have the same weight. 243 244 """ 245 def __init__(self, Q, U, normalise=True, mask=None, first_der=None, second_der=None, pixel_weights=None): 246 self.Q = Q 247 self.U = U 248 self.mask = mask 249 self.pixel_weights = _prepare_pixel_weights(pixel_weights, np.shape(Q)) 250 σ2 = 1. 251 if normalise: 252 # self.Q -= self.Q.mean() 253 # self.U -= self.U.mean() 254 σ2 = self.get_variance() 255 self.Q = self.Q / np.sqrt(σ2) # Not in place, so that the input maps are not modified 256 self.U = self.U / np.sqrt(σ2) 257 # P² scales as σ² (not σ), so the given derivatives are normalised here instead of in `Healpix` 258 if first_der is not None: 259 first_der = np.asarray(first_der) / σ2 260 if second_der is not None: 261 second_der = np.asarray(second_der) / σ2 262 263 super().__init__(self.Q**2. + self.U**2., normalise=False, mask=mask, first_der=first_der, second_der=second_der, pixel_weights=pixel_weights) 264 265 def get_variance(self): 266 """Compute the variance of the input $Q$ and $U$ maps within the input mask (as the average of both), weighted by `pixel_weights` if given. 267 268 Returns 269 ------- 270 var : float 271 The variance of the input $Q$ and $U$ maps within the input mask. 272 273 """ 274 return (_masked_variance(self.Q, self.mask, self.pixel_weights) + _masked_variance(self.U, self.mask, self.pixel_weights))/2. 275 276 277__all__ = ["Healpix", "HealpixP2"] 278__docformat__ = "numpy"
32class Healpix(DataField): 33 """Class for spherical scalar fields in HEALPix format. 34 35 Parameters 36 ---------- 37 field : np.array 38 Values of the scalar field in HEALPix format in RING scheme. The field must be finite: the pixels outside of the region of interest can be set to any finite value and excluded with `mask`. 39 40 normalise : bool, optional 41 If `True`, the map is normalised to have unit variance. Note: the mean of the map is not forced to be 0. 42 Default: `True`. 43 44 mask : np.array or None, optional 45 Mask where the field is considered. It is a bool array of the same shape that `field`. 46 Default: all data is included. 47 48 first_der : np.array or None, optional 49 First **covariant** derivatives of the field in an orthonormal basis of the space. Same structure as `field`, and shape `(dim, field.shape)`, in the order of `get_first_der` (θ, ϕ). 50 If given, they are used instead of computing them from the map. They must be given together with `second_der`, and they are normalised together with the field if `normalise=True`. 51 Default: computed when needed. 52 53 second_der : np.array or None, optional 54 Second **covariant** derivatives of the field in an orthonormal basis of the space. Same structure as `field`, and shape `(dim*(dim+1)/2, field.shape)`, in the order of `get_second_der` (θθ, ϕϕ, θϕ). 55 They must be given together with `first_der`. 56 Default: computed when needed. 57 58 pixel_weights : np.array or None, optional 59 Relative weight of each pixel in the averages over the mask. HEALPix pixels have equal areas, so this is only needed to weight the pixels differently. 60 It is an array of finite and non-negative values of the same shape that `field`. It is used by the Minkowski Functionals and by the normalisation of the field (weighted variance). 61 Default: all pixels have the same weight. 62 63 Attributes 64 ---------- 65 field : np.array 66 Data of the field as a HEALPix map in RING scheme. 67 68 nside : int 69 Parameter `nside` of the map. The number of pixels is `12*nside**2`. 70 71 dim : int 72 Dimension of the space where the field is defined. In this case, the space is the sphere and this is 2. 73 74 name : str 75 Name of the field. In this case, "HEALPix map" 76 77 first_der : np.array or None 78 First **covariant** derivatives of the field in an orthonormal basis of the space. Same structure as `field`, and shape `(dim, field.shape)`. 79 80 second_der : np.array or None 81 Second **covariant** derivatives of the field in an orthonormal basis of the space. Same structure as `field`, and shape `(dim*(dim+1)/2, field.shape)`. 82 The order of the derivatives is diagonal first, e.g. in `dim=3`: `11`, `22`, `33`, `12`, `13`, `23`. 83 84 mask : np.array 85 Mask where the field is considered. It is a bool array of the same shape that `field`. 86 87 pixel_weights : np.array or None 88 Relative weight of each pixel in the averages over the mask, or `None` if all pixels have the same weight. 89 90 """ 91 def __init__(self, field, normalise=True, mask=None, first_der=None, second_der=None, pixel_weights=None): 92 super().__init__(field, 2, name="HEALPix map", mask=mask, pixel_weights=pixel_weights) 93 self.nside = hp.get_nside(self.field) 94 if hp.get_nside(self.mask) != self.nside: 95 raise ValueError('The map and the mask have different nside') 96 self._check_finite() 97 sigma = 1. 98 if normalise: 99 # self.field -= self.field.mean() 100 sigma = np.sqrt(self.get_variance()) 101 self.field /= sigma 102 self._set_derivatives(first_der, second_der, scale=sigma) 103 104 def get_variance(self): 105 """Compute the variance of the Healpix scalar map within the mask, weighted by `pixel_weights` if given. 106 107 Returns 108 ------- 109 var : float 110 The variance of the Healpix map within the mask. 111 112 """ 113 return _masked_variance(self.field, self.mask, self.pixel_weights) 114 115 def get_first_der(self, lmax=None): 116 """Compute the covariant first derivatives of the input Healpix scalar map. 117 It stores: 118 119 - first covariant derivative wrt θ in self.first_der[0] 120 - first covariant derivative wrt ϕ in self.first_der[1] 121 122 Parameters 123 ---------- 124 lmax : int or None, optional 125 Maximum multipole used in the computation of the derivatives. 126 Default: 3*nside - 1 127 """ 128 self.first_der = np.array(healpix_derivatives(self.field, gradient=True, lmax=lmax)) # order: θ, ϕ 129 130 def get_second_der(self, lmax=None): 131 """Compute the covariant second derivatives of the input Healpix scalar map. 132 It stores: 133 134 - second covariant derivative wrt θθ in self.second_der[0] 135 - second covariant derivative wrt ϕϕ in self.second_der[1] 136 - second covariant derivative wrt θϕ in self.second_der[2] 137 138 Parameters 139 ---------- 140 lmax : int or None, optional 141 Maximum multipole used in the computation of the derivatives. 142 Default: 3*nside - 1 143 """ 144 if self.first_der is None: 145 self.get_first_der() 146 theta = get_theta(self.nside) 147 148 second_partial = healpix_second_derivatives(self.first_der[0], np.cos(theta) * self.first_der[1], lmax=lmax) #order θθ, ϕϕ, θϕ 149 150 self.second_der = np.array([second_partial[0], 151 second_partial[1]/np.cos(theta)**2. + self.first_der[0]*np.tan(theta), 152 (second_partial[2]/np.cos(theta) - self.first_der[1] * np.tan(theta))]) #order θθ, ϕϕ, θϕ 153 154 def maxima_list(self): 155 """Compute the values of the local maxima of the HEALPix map. 156 157 Returns 158 ------- 159 values : np.array 160 Values of the map which are local maxima. 161 162 """ 163 pixels, values = _hotspot(self.field) 164 return values[self.mask[pixels]] 165 166 def minima_list(self): 167 """Compute the values of the local minima of the HEALPix map. 168 169 Returns 170 ------- 171 values : np.array 172 Values of the map which are local minima. 173 174 """ 175 pixels, values = _hotspot(-self.field) 176 return -values[self.mask[pixels]]
Class for spherical scalar fields in HEALPix format.
Parameters
- field (np.array):
Values of the scalar field in HEALPix format in RING scheme. The field must be finite: the pixels outside of the region of interest can be set to any finite value and excluded with
mask. - normalise (bool, optional):
If
True, the map is normalised to have unit variance. Note: the mean of the map is not forced to be 0. Default:True. - mask (np.array or None, optional):
Mask where the field is considered. It is a bool array of the same shape that
field. Default: all data is included. - first_der (np.array or None, optional):
First covariant derivatives of the field in an orthonormal basis of the space. Same structure as
field, and shape(dim, field.shape), in the order ofget_first_der(θ, ϕ). If given, they are used instead of computing them from the map. They must be given together withsecond_der, and they are normalised together with the field ifnormalise=True. Default: computed when needed. - second_der (np.array or None, optional):
Second covariant derivatives of the field in an orthonormal basis of the space. Same structure as
field, and shape(dim*(dim+1)/2, field.shape), in the order ofget_second_der(θθ, ϕϕ, θϕ). They must be given together withfirst_der. Default: computed when needed. - pixel_weights (np.array or None, optional):
Relative weight of each pixel in the averages over the mask. HEALPix pixels have equal areas, so this is only needed to weight the pixels differently.
It is an array of finite and non-negative values of the same shape that
field. It is used by the Minkowski Functionals and by the normalisation of the field (weighted variance). Default: all pixels have the same weight.
Attributes
- field (np.array): Data of the field as a HEALPix map in RING scheme.
- nside (int):
Parameter
nsideof the map. The number of pixels is12*nside**2. - dim (int): Dimension of the space where the field is defined. In this case, the space is the sphere and this is 2.
- name (str): Name of the field. In this case, "HEALPix map"
- first_der (np.array or None):
First covariant derivatives of the field in an orthonormal basis of the space. Same structure as
field, and shape(dim, field.shape). - second_der (np.array or None):
Second covariant derivatives of the field in an orthonormal basis of the space. Same structure as
field, and shape(dim*(dim+1)/2, field.shape). The order of the derivatives is diagonal first, e.g. indim=3:11,22,33,12,13,23. - mask (np.array):
Mask where the field is considered. It is a bool array of the same shape that
field. - pixel_weights (np.array or None):
Relative weight of each pixel in the averages over the mask, or
Noneif all pixels have the same weight.
91 def __init__(self, field, normalise=True, mask=None, first_der=None, second_der=None, pixel_weights=None): 92 super().__init__(field, 2, name="HEALPix map", mask=mask, pixel_weights=pixel_weights) 93 self.nside = hp.get_nside(self.field) 94 if hp.get_nside(self.mask) != self.nside: 95 raise ValueError('The map and the mask have different nside') 96 self._check_finite() 97 sigma = 1. 98 if normalise: 99 # self.field -= self.field.mean() 100 sigma = np.sqrt(self.get_variance()) 101 self.field /= sigma 102 self._set_derivatives(first_der, second_der, scale=sigma)
104 def get_variance(self): 105 """Compute the variance of the Healpix scalar map within the mask, weighted by `pixel_weights` if given. 106 107 Returns 108 ------- 109 var : float 110 The variance of the Healpix map within the mask. 111 112 """ 113 return _masked_variance(self.field, self.mask, self.pixel_weights)
Compute the variance of the Healpix scalar map within the mask, weighted by pixel_weights if given.
Returns
- var (float): The variance of the Healpix map within the mask.
115 def get_first_der(self, lmax=None): 116 """Compute the covariant first derivatives of the input Healpix scalar map. 117 It stores: 118 119 - first covariant derivative wrt θ in self.first_der[0] 120 - first covariant derivative wrt ϕ in self.first_der[1] 121 122 Parameters 123 ---------- 124 lmax : int or None, optional 125 Maximum multipole used in the computation of the derivatives. 126 Default: 3*nside - 1 127 """ 128 self.first_der = np.array(healpix_derivatives(self.field, gradient=True, lmax=lmax)) # order: θ, ϕ
Compute the covariant first derivatives of the input Healpix scalar map. It stores:
- first covariant derivative wrt θ in self.first_der[0]
- first covariant derivative wrt ϕ in self.first_der[1]
Parameters
- lmax (int or None, optional): Maximum multipole used in the computation of the derivatives. Default: 3*nside - 1
130 def get_second_der(self, lmax=None): 131 """Compute the covariant second derivatives of the input Healpix scalar map. 132 It stores: 133 134 - second covariant derivative wrt θθ in self.second_der[0] 135 - second covariant derivative wrt ϕϕ in self.second_der[1] 136 - second covariant derivative wrt θϕ in self.second_der[2] 137 138 Parameters 139 ---------- 140 lmax : int or None, optional 141 Maximum multipole used in the computation of the derivatives. 142 Default: 3*nside - 1 143 """ 144 if self.first_der is None: 145 self.get_first_der() 146 theta = get_theta(self.nside) 147 148 second_partial = healpix_second_derivatives(self.first_der[0], np.cos(theta) * self.first_der[1], lmax=lmax) #order θθ, ϕϕ, θϕ 149 150 self.second_der = np.array([second_partial[0], 151 second_partial[1]/np.cos(theta)**2. + self.first_der[0]*np.tan(theta), 152 (second_partial[2]/np.cos(theta) - self.first_der[1] * np.tan(theta))]) #order θθ, ϕϕ, θϕ
Compute the covariant second derivatives of the input Healpix scalar map. It stores:
- second covariant derivative wrt θθ in self.second_der[0]
- second covariant derivative wrt ϕϕ in self.second_der[1]
- second covariant derivative wrt θϕ in self.second_der[2]
Parameters
- lmax (int or None, optional): Maximum multipole used in the computation of the derivatives. Default: 3*nside - 1
154 def maxima_list(self): 155 """Compute the values of the local maxima of the HEALPix map. 156 157 Returns 158 ------- 159 values : np.array 160 Values of the map which are local maxima. 161 162 """ 163 pixels, values = _hotspot(self.field) 164 return values[self.mask[pixels]]
Compute the values of the local maxima of the HEALPix map.
Returns
- values (np.array): Values of the map which are local maxima.
166 def minima_list(self): 167 """Compute the values of the local minima of the HEALPix map. 168 169 Returns 170 ------- 171 values : np.array 172 Values of the map which are local minima. 173 174 """ 175 pixels, values = _hotspot(-self.field) 176 return -values[self.mask[pixels]]
Compute the values of the local minima of the HEALPix map.
Returns
- values (np.array): Values of the map which are local minima.
Inherited Members
179class HealpixP2(Healpix): 180 """Class for spherical $P^2 = Q^2 + U^2$ fields in HEALPix format, from the $Q$ and $U$ maps. 181 182 Parameters 183 ---------- 184 Q : np.array 185 Values of the Q component in HEALPix format in RING scheme. 186 187 U : np.array 188 Values of the U component in HEALPix format in RING scheme. 189 190 normalise : bool, optional 191 If `True`, Q and U are normalised to unit variance. Note: the normalisation is computed as the average of both variances, the mean of both maps are not forced to be 0. 192 193 mask : np.array or None, optional 194 Mask where the field if considered. It is a bool array of the same shape that `field`. 195 Default: all data is included. 196 197 first_der : np.array or None, optional 198 First **covariant** derivatives of $P^2$, computed from the input `Q` and `U` (before the normalisation). Same structure as `field`, and shape `(dim, field.shape)`, in the order of `get_first_der` (θ, ϕ). 199 They must be given together with `second_der`. If `normalise=True`, they are normalised together with $P^2$, i.e., divided by the average of the variances of `Q` and `U`. 200 Default: computed when needed. 201 202 second_der : np.array or None, optional 203 Second **covariant** derivatives of $P^2$, computed from the input `Q` and `U` (before the normalisation). Same structure as `field`, and shape `(dim*(dim+1)/2, field.shape)`, in the order of `get_second_der` (θθ, ϕϕ, θϕ). 204 They must be given together with `first_der`. 205 Default: computed when needed. 206 207 pixel_weights : np.array or None, optional 208 Relative weight of each pixel in the averages over the mask. HEALPix pixels have equal areas, so this is only needed to weight the pixels differently. 209 It is an array of finite and non-negative values of the same shape that `field`. It is used by the Minkowski Functionals and by the normalisation of the field (weighted variance). 210 Default: all pixels have the same weight. 211 212 Attributes 213 ---------- 214 Q : np.array 215 Data of the $Q$ component as a HEALPix map in RING scheme. 216 217 U : np.array 218 Data of the $U$ component as a HEALPix map in RING scheme. 219 220 field : np.array 221 Data of the $P^2$ field as a HEALPix map in RING scheme. 222 223 nside : int 224 Parameter `nside` of the map. The number of pixels is `12*nside**2`. 225 226 dim : int 227 Dimension of the space where the field is defined. In this case, the space is the sphere and this is 2. 228 229 name : str 230 Name of the field. In this case, "HEALPix map" 231 232 first_der : np.array or None 233 First **covariant** derivatives of the field in an orthonormal basis of the space. Same structure as `field`, and shape `(dim, field.shape)`. 234 235 second_der : np.array or None 236 Second **covariant** derivatives of the field in an orthonormal basis of the space. Same structure as `field`, and shape `(dim*(dim+1)/2, field.shape)`. 237 The order of the derivatives is diagonal first, e.g. in `dim=3`: `11`, `22`, `33`, `12`, `13`, `23`. 238 239 mask : np.array 240 Mask where the field if considered. It is a bool array of the same shape that `field`. 241 242 pixel_weights : np.array or None 243 Relative weight of each pixel in the averages over the mask, or `None` if all pixels have the same weight. 244 245 """ 246 def __init__(self, Q, U, normalise=True, mask=None, first_der=None, second_der=None, pixel_weights=None): 247 self.Q = Q 248 self.U = U 249 self.mask = mask 250 self.pixel_weights = _prepare_pixel_weights(pixel_weights, np.shape(Q)) 251 σ2 = 1. 252 if normalise: 253 # self.Q -= self.Q.mean() 254 # self.U -= self.U.mean() 255 σ2 = self.get_variance() 256 self.Q = self.Q / np.sqrt(σ2) # Not in place, so that the input maps are not modified 257 self.U = self.U / np.sqrt(σ2) 258 # P² scales as σ² (not σ), so the given derivatives are normalised here instead of in `Healpix` 259 if first_der is not None: 260 first_der = np.asarray(first_der) / σ2 261 if second_der is not None: 262 second_der = np.asarray(second_der) / σ2 263 264 super().__init__(self.Q**2. + self.U**2., normalise=False, mask=mask, first_der=first_der, second_der=second_der, pixel_weights=pixel_weights) 265 266 def get_variance(self): 267 """Compute the variance of the input $Q$ and $U$ maps within the input mask (as the average of both), weighted by `pixel_weights` if given. 268 269 Returns 270 ------- 271 var : float 272 The variance of the input $Q$ and $U$ maps within the input mask. 273 274 """ 275 return (_masked_variance(self.Q, self.mask, self.pixel_weights) + _masked_variance(self.U, self.mask, self.pixel_weights))/2.
Class for spherical $P^2 = Q^2 + U^2$ fields in HEALPix format, from the $Q$ and $U$ maps.
Parameters
- Q (np.array): Values of the Q component in HEALPix format in RING scheme.
- U (np.array): Values of the U component in HEALPix format in RING scheme.
- normalise (bool, optional):
If
True, Q and U are normalised to unit variance. Note: the normalisation is computed as the average of both variances, the mean of both maps are not forced to be 0. - mask (np.array or None, optional):
Mask where the field if considered. It is a bool array of the same shape that
field. Default: all data is included. - first_der (np.array or None, optional):
First covariant derivatives of $P^2$, computed from the input
QandU(before the normalisation). Same structure asfield, and shape(dim, field.shape), in the order ofget_first_der(θ, ϕ). They must be given together withsecond_der. Ifnormalise=True, they are normalised together with $P^2$, i.e., divided by the average of the variances ofQandU. Default: computed when needed. - second_der (np.array or None, optional):
Second covariant derivatives of $P^2$, computed from the input
QandU(before the normalisation). Same structure asfield, and shape(dim*(dim+1)/2, field.shape), in the order ofget_second_der(θθ, ϕϕ, θϕ). They must be given together withfirst_der. Default: computed when needed. - pixel_weights (np.array or None, optional):
Relative weight of each pixel in the averages over the mask. HEALPix pixels have equal areas, so this is only needed to weight the pixels differently.
It is an array of finite and non-negative values of the same shape that
field. It is used by the Minkowski Functionals and by the normalisation of the field (weighted variance). Default: all pixels have the same weight.
Attributes
- Q (np.array): Data of the $Q$ component as a HEALPix map in RING scheme.
- U (np.array): Data of the $U$ component as a HEALPix map in RING scheme.
- field (np.array): Data of the $P^2$ field as a HEALPix map in RING scheme.
- nside (int):
Parameter
nsideof the map. The number of pixels is12*nside**2. - dim (int): Dimension of the space where the field is defined. In this case, the space is the sphere and this is 2.
- name (str): Name of the field. In this case, "HEALPix map"
- first_der (np.array or None):
First covariant derivatives of the field in an orthonormal basis of the space. Same structure as
field, and shape(dim, field.shape). - second_der (np.array or None):
Second covariant derivatives of the field in an orthonormal basis of the space. Same structure as
field, and shape(dim*(dim+1)/2, field.shape). The order of the derivatives is diagonal first, e.g. indim=3:11,22,33,12,13,23. - mask (np.array):
Mask where the field if considered. It is a bool array of the same shape that
field. - pixel_weights (np.array or None):
Relative weight of each pixel in the averages over the mask, or
Noneif all pixels have the same weight.
246 def __init__(self, Q, U, normalise=True, mask=None, first_der=None, second_der=None, pixel_weights=None): 247 self.Q = Q 248 self.U = U 249 self.mask = mask 250 self.pixel_weights = _prepare_pixel_weights(pixel_weights, np.shape(Q)) 251 σ2 = 1. 252 if normalise: 253 # self.Q -= self.Q.mean() 254 # self.U -= self.U.mean() 255 σ2 = self.get_variance() 256 self.Q = self.Q / np.sqrt(σ2) # Not in place, so that the input maps are not modified 257 self.U = self.U / np.sqrt(σ2) 258 # P² scales as σ² (not σ), so the given derivatives are normalised here instead of in `Healpix` 259 if first_der is not None: 260 first_der = np.asarray(first_der) / σ2 261 if second_der is not None: 262 second_der = np.asarray(second_der) / σ2 263 264 super().__init__(self.Q**2. + self.U**2., normalise=False, mask=mask, first_der=first_der, second_der=second_der, pixel_weights=pixel_weights)
266 def get_variance(self): 267 """Compute the variance of the input $Q$ and $U$ maps within the input mask (as the average of both), weighted by `pixel_weights` if given. 268 269 Returns 270 ------- 271 var : float 272 The variance of the input $Q$ and $U$ maps within the input mask. 273 274 """ 275 return (_masked_variance(self.Q, self.mask, self.pixel_weights) + _masked_variance(self.U, self.mask, self.pixel_weights))/2.
Compute the variance of the input $Q$ and $U$ maps within the input mask (as the average of both), weighted by pixel_weights if given.
Returns
- var (float): The variance of the input $Q$ and $U$ maps within the input mask.