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

1import numpy as np 

2from lsst.afw.table import GroupView 

3 

4__all__ = ("filterMatches",) 

5 

6 

7def filterMatches( 

8 matchedCatalog, 

9 snrMin=None, 

10 snrMax=None, 

11 extended=None, 

12 doFlags=None, 

13 isPrimary=None, 

14 psfStars=None, 

15 photoCalibStars=None, 

16 astromCalibStars=None, 

17): 

18 

19 if snrMin is None: 

20 snrMin = 50.0 

21 if snrMax is None: 

22 snrMax = np.Inf 

23 if extended is None: 

24 extended = False 

25 if doFlags is None: 

26 doFlags = True 

27 nMatchesRequired = 2 

28 if isPrimary is None: 

29 isPrimary = True 

30 if psfStars is None: 

31 psfStars = False 

32 if photoCalibStars is None: 

33 photoCalibStars = False 

34 if astromCalibStars is None: 

35 astromCalibStars = False 

36 

37 matchedCat = GroupView.build(matchedCatalog) 

38 magKey = matchedCat.schema.find("slot_PsfFlux_mag").key 

39 

40 def nMatchFilter(cat): 

41 if len(cat) < nMatchesRequired: 

42 return False 

43 return np.isfinite(cat.get(magKey)).all() 

44 

45 def snrFilter(cat): 

46 # Note that this also implicitly checks for psfSnr being non-nan. 

47 snr = cat.get("base_PsfFlux_snr") 

48 (ok0,) = np.where(np.isfinite(snr)) 

49 medianSnr = np.median(snr[ok0]) 

50 return snrMin <= medianSnr and medianSnr <= snrMax 

51 

52 def ptsrcFilter(cat): 

53 ext = cat.get("base_ClassificationExtendedness_value") 

54 # Keep only objects that are flagged as "not extended" in *ALL* visits, 

55 # (base_ClassificationExtendedness_value = 1 for extended, 0 for point-like) 

56 if extended: 

57 return np.min(ext) > 0.9 

58 else: 

59 return np.max(ext) < 0.9 

60 

61 def flagFilter(cat): 

62 if doFlags: 

63 flag_sat = cat.get("base_PixelFlags_flag_saturated") 

64 flag_cr = cat.get("base_PixelFlags_flag_cr") 

65 flag_bad = cat.get("base_PixelFlags_flag_bad") 

66 flag_edge = cat.get("base_PixelFlags_flag_edge") 

67 return np.logical_not(np.any([flag_sat, flag_cr, flag_bad, flag_edge])) 

68 else: 

69 return True 

70 

71 def isPrimaryFilter(cat): 

72 if isPrimary: 

73 flag_isPrimary = cat.get("detect_isPrimary") 

74 return np.all(flag_isPrimary) 

75 else: 

76 return True 

77 

78 def fullFilter(cat): 

79 return ( 

80 nMatchFilter(cat) 

81 and snrFilter(cat) 

82 and ptsrcFilter(cat) 

83 and flagFilter(cat) 

84 and isPrimaryFilter(cat) 

85 ) 

86 

87 return matchedCat.where(fullFilter)