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 astropy.units as u 

2import numpy as np 

3from lsst.pipe.base import Struct, Task 

4from lsst.pex.config import Config, Field 

5from lsst.verify import Measurement 

6 

7from lsst.faro.utils.matcher import mergeCatalogs 

8 

9__all__ = ('NumSourcesTask', 'NumSourcesMergeTask', 

10 'NumpySummaryTaskConfig', 'NumpySummaryTask') 

11 

12 

13class NumSourcesTaskConfig(Config): 

14 doPrimary = Field(doc="Only count sources where detect_isPrimary is True.", 

15 dtype=bool, default=False) 

16 

17 

18class NumSourcesTask(Task): 

19 """Simple default task count the number of sources/objects in catalog.""" 

20 

21 ConfigClass = NumSourcesTaskConfig 

22 _DefaultName = "numSourcesTask" 

23 

24 def run(self, metricName, catalog, **kwargs): 

25 self.log.info("Measuring %s", metricName) 

26 if self.config.doPrimary: 

27 nSources = np.sum(catalog['detect_isPrimary'] is True) 

28 else: 

29 nSources = len(catalog) 

30 self.log.info("Number of sources (nSources) = %i" % nSources) 

31 meas = Measurement("nsrcMeas", nSources * u.count) 

32 return Struct(measurement=meas) 

33 

34 

35class NumSourcesMergeTask(Task): 

36 

37 ConfigClass = Config 

38 _DefaultName = "numSourcesMergeTask" 

39 

40 def run(self, metricName, catalogs, photoCalibs, astromCalibs, **kwargs): 

41 self.log.info("Measuring %s", metricName) 

42 catalog = mergeCatalogs(catalogs, photoCalibs, astromCalibs) 

43 nSources = len(catalog) 

44 meas = Measurement("nsrcMeas", nSources * u.count) 

45 return Struct(measurement=meas) 

46 

47 

48class NumpySummaryTaskConfig(Config): 

49 summary = Field(dtype=str, default="median", 

50 doc="Aggregation to use for summary metrics") 

51 

52 

53class NumpySummaryTask(Task): 

54 

55 ConfigClass = NumpySummaryTaskConfig 

56 _DefaultName = "numpySummaryTask" 

57 

58 def run(self, measurements, agg_name, package, metric): 

59 agg = agg_name.lower() 

60 if agg == "summary": 

61 agg = self.config.summary 

62 self.log.info("Computing the %s of %s_%s values", agg, package, metric) 

63 

64 if len(measurements) == 0: 

65 self.log.info('Received zero length measurements list. Returning NaN.') 

66 # In the case of an empty list, there is nothing we can do other than 

67 # to return a NaN 

68 value = u.Quantity(np.nan) 

69 else: 

70 unit = measurements[0].quantity.unit 

71 value = getattr(np, agg)(u.Quantity([x.quantity for x in measurements 

72 if np.isfinite(x.quantity)])) 

73 # Make sure return has same unit as inputs 

74 # In some cases numpy can return a NaN and the unit gets dropped 

75 value = value.value*unit 

76 return Struct(measurement=Measurement(f"metricvalue_{agg_name.lower()}_{package}_{metric}", value))