193 ) -> CorrelationMatrix:
194 """Determine correlation coefficients between pixels
195
196 This is the concrete routine that does the computation.
197
198 Parameters
199 ----------
200 maskedImage : `~lsst.afw.image.MaskedImage`
201 Image for which to determine the variance rescaling factor.
202 refMaskedImage : `~lsst.afw.image.MaskedImage`
203 Image from which to determine which pixels to mask.
204
205 Returns
206 -------
207 corr_matrix : `CorrelationMatrix`
208 Correlation matrix of the maskedImage.
209 """
210 maskVal = refMaskedImage.mask.getPlaneBitMask(self.config.maskPlanes)
211 isGood = (
212 ((refMaskedImage.mask.array & maskVal) == 0)
213 & np.isfinite(refMaskedImage.image.array)
214 & np.isfinite(refMaskedImage.variance.array)
215 & (refMaskedImage.variance.array > 0)
216 )
217
218 nGood = np.sum(isGood)
219 self.log.debug(
220 "Number of selected background pixels: %d of %d.", nGood, isGood.size
221 )
222
223
224
225 with warnings.catch_warnings():
226 warnings.simplefilter("ignore", category=RuntimeWarning)
227 normalized_arr = maskedImage.image.array / np.sqrt(
228 maskedImage.variance.array
229 )
230 normalized_arr[~isGood] = np.nan
231
232 corr_matrix = np.empty(
233 (self.config.size + 1, self.config.size + 1), dtype=np.float32
234 )
235
236 for dx, dy in itertools.product(
237 range(self.config.size + 1), range(self.config.size + 1)
238 ):
239 sliceX = slice(None, -dx) if dx != 0 else slice(None, None)
240 sliceY = slice(None, -dy) if dy != 0 else slice(None, None)
241 arr1 = normalized_arr[sliceX, sliceY]
242
243 sliceX = slice(dx, None) if dx != 0 else slice(None, None)
244 sliceY = slice(dy, None) if dy != 0 else slice(None, None)
245 arr2 = normalized_arr[sliceX, sliceY]
246
247 if self.config.subtractEmpiricalMean:
248 arr1 -= np.nanmean(arr1)
249 arr2 -= np.nanmean(arr2)
250 if self.config.scaleEmpiricalVariance:
251
252
253 arr1 /= np.nanmean(arr1**2) ** 0.5
254 arr2 /= np.nanmean(arr2**2) ** 0.5
255
256 cov = np.nanmean(arr1 * arr2)
257
258
259
260 cov *= 1.0 + 0.5 * (1 - cov**2) / (~np.isnan(arr1 * arr2)).sum()
261
262 corr_matrix[dx, dy] = cov
263
264 return CorrelationMatrix(corr_matrix)