Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

# This file is part of meas_extensions_scarlet. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (https://www.lsst.org). 

# See the COPYRIGHT file at the top-level directory of this distribution 

# for details of code ownership. 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the GNU General Public License 

# along with this program. If not, see <https://www.gnu.org/licenses/>. 

 

import numpy as np 

import scipy.signal 

 

from lsst.geom import Box2I, Point2I, Extent2I 

from lsst.afw.geom import SpanSet 

from lsst.afw.detection import Footprint, GaussianPsf 

 

 

def numpyToStack(images, center, offset): 

"""Convert numpy and python objects to stack objects 

""" 

cy, cx = center 

bands, height, width = images.shape 

x0, y0 = offset 

bbox = Box2I(Point2I(x0, y0), Extent2I(width, height)) 

spanset = SpanSet(bbox) 

foot = Footprint(spanset) 

foot.addPeak(cx+x0, cy+y0, images[:, cy, cx].max()) 

peak = foot.getPeaks()[0] 

return foot, peak, bbox 

 

 

def initData(shape, coords, amplitudes=None, convolve=True): 

"""Initialize data for the tests 

""" 

 

B, Ny, Nx = shape 

K = len(coords) 

 

if amplitudes is None: 

amplitudes = np.ones((K,)) 

assert K == len(amplitudes) 

 

_seds = [ 

np.arange(B, dtype=float), 

np.arange(B, dtype=float)[::-1], 

np.ones((B,), dtype=float) 

] 

seds = np.array([_seds[n % 3]*amplitudes[n] for n in range(K)]) 

 

morphs = np.zeros((K, Ny, Nx)) 

for k, coord in enumerate(coords): 

morphs[k, coord[0], coord[1]] = 1 

images = seds.T.dot(morphs.reshape(K, -1)).reshape(shape) 

 

if convolve: 

psfRadius = 20 

psfShape = (2*psfRadius+1, 2*psfRadius+1) 

 

targetPsf = GaussianPsf(psfShape[1], psfShape[0], .9) 

targetPsfImage = targetPsf.computeImage().array 

 

psfs = [GaussianPsf(psfShape[1], psfShape[0], 1+.2*b) for b in range(B)] 

psfImages = np.array([psf.computeImage().array for psf in psfs]) 

psfImages /= psfImages.max(axis=(1, 2))[:, None, None] 

 

# Convolve the image with the psf in each channel 

# Use scipy.signal.convolve without using FFTs as a sanity check 

images = np.array([scipy.signal.convolve(img, psf, method="direct", mode="same") 

for img, psf in zip(images, psfImages)]) 

# Convolve the true morphology with the target PSF, 

# also using scipy.signal.convolve as a sanity check 

morphs = np.array([scipy.signal.convolve(m, targetPsfImage, method="direct", mode="same") 

for m in morphs]) 

morphs /= morphs.max() 

psfImages /= psfImages.sum(axis=(1, 2))[:, None, None] 

 

channels = range(len(images)) 

return targetPsfImage, psfImages, images, channels, seds, morphs, targetPsf, psfs