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 

7__all__ = ('NumSourcesTask', 'NumpySummaryTaskConfig', 'NumpySummaryTask') 

8 

9 

10class NumSourcesTask(Task): 

11 

12 ConfigClass = Config 

13 _DefaultName = "numSourcesTask" 

14 

15 def run(self, catalog, metric_name, vIds=None): 

16 self.log.info(f"Measuring {metric_name}") 

17 nSources = len(catalog) 

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

19 return Struct(measurement=meas) 

20 

21 

22class NumpySummaryTaskConfig(Config): 

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

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

25 

26 

27class NumpySummaryTask(Task): 

28 

29 ConfigClass = NumpySummaryTaskConfig 

30 _DefaultName = "numpySummaryTask" 

31 

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

33 agg = agg_name.lower() 

34 if agg == "summary": 

35 agg = self.config.summary 

36 self.log.info(f"Computing the {agg} of {package}_{metric} values") 

37 

38 if len(measurements) == 0: 

39 self.log.info('Recieved zero length measurments list. Returning NaN.') 

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

41 # to return a NaN 

42 value = u.Quantity(np.nan) 

43 else: 

44 unit = measurements[0].quantity.unit 

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

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

47 # Make sure return has same unit as inputs 

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

49 value = value.value*unit 

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