Coverage for python/lsst/faro/measurement/GeneralMeasurementTasks.py: 39%

21 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-02-08 10:43 +0000

1# This file is part of faro. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

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

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

7# for details of code ownership. 

8# 

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

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

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

12# (at your option) any later version. 

13# 

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

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

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

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

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

21 

22import numpy as np 

23from lsst.pipe.base import Struct, Task 

24from lsst.pex.config import Config 

25from lsst.verify import Measurement 

26 

27__all__ = ("HistMedianTask",) 

28 

29 

30class HistMedianTask(Task): 

31 

32 ConfigClass = Config 

33 _DefaultName = "histMedianTask" 

34 

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

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

37 values = measurements[0].extras["values"].quantity 

38 bins = measurements[0].extras["bins"].quantity 

39 for m in measurements[1:]: 

40 values += m.extras["values"].quantity 

41 c = np.cumsum(values) 

42 idx = np.searchsorted(c, c[-1] / 2.0) 

43 # This is the bin lower bound 

44 lower = bins[idx] 

45 # This is the bin upper bound 

46 upper = bins[idx + 1] 

47 

48 # Linear interpolation of median value within the bin 

49 frac = (c[-1] / 2.0 - c[idx - 1]) / (c[idx] - c[idx - 1]) 

50 interp = lower + (upper - lower) * frac 

51 

52 return Struct( 

53 measurement=Measurement( 

54 f"metricvalue_{agg_name}_{package}_{metric}", interp 

55 ) 

56 )