Coverage for python / lsst / analysis / tools / interfaces / _metricMeasurementBundle.py: 26%

42 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-22 09:09 +0000

1# This file is part of analysis_tools. 

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/>. 

21from __future__ import annotations 

22 

23__all__ = ("MetricMeasurementBundle",) 

24 

25import json 

26import numbers 

27from collections import UserDict 

28from typing import Any 

29 

30from lsst.verify import Measurement 

31 

32 

33class JSONFloatSafeEncoder(json.JSONEncoder): 

34 """JSON encoder that can handle both 32 and 64 bit numpy floats by 

35 explicit casting. Without this cast, numpy 32-bit floats will throw a 

36 serialization error. 

37 """ 

38 

39 def default(self, o): 

40 if isinstance(o, numbers.Real): 

41 return float(o) 

42 else: 

43 return super().default(o) 

44 

45 

46class MetricMeasurementBundle(UserDict[str, list[Measurement]]): 

47 """A specialized dict for storing outputs from multiple `AnalysisMetric` 

48 actions. 

49 

50 Keys correspond to the identifier of the action that produced the 

51 corresponding values. Each value is a list of `~lsst.verift.Measurement` 

52 objects produced by that `AnalysisMetric` action. 

53 

54 This object also supports the pydandic interface for serializing to and 

55 from json compatible objects. 

56 """ 

57 

58 def __init__(self, *args, **kwargs): 

59 self.reference_package: str | None = kwargs.pop("reference_package", None) 

60 self.timestamp_version: str | None = kwargs.pop("timestamp_version", None) 

61 self.dataset_identifier: str | None = kwargs.pop("dataset_identifier", None) 

62 self.metricNamePrefix: str = kwargs.pop("metricNamePrefix", "") 

63 super().__init__(*args, **kwargs) 

64 

65 def json(self) -> str: 

66 result = {} 

67 for key, value in self.items(): 

68 result[key] = [meas.json for meas in value] 

69 if self.reference_package is not None: 

70 result["__reference_package"] = self.reference_package 

71 if self.timestamp_version is not None: 

72 result["__timestamp_version"] = self.timestamp_version 

73 if self.dataset_identifier is not None: 

74 result["__dataset_identifier"] = self.dataset_identifier 

75 result["__metricNamePrefix"] = self.metricNamePrefix 

76 

77 encoder = JSONFloatSafeEncoder() 

78 

79 return encoder.encode(result) 

80 

81 @classmethod 

82 def parse_obj(cls: type[MetricMeasurementBundle], data: dict[str, Any]) -> MetricMeasurementBundle: 

83 inst = cls() 

84 inst.dataset_identifier = data.pop("__dataset_identifier", None) 

85 inst.timestamp_version = data.pop("__timestamp_version", None) 

86 inst.reference_package = data.pop("__reference_package", None) 

87 inst.metricNamePrefix = data.pop("__metricNamePrefix", "") 

88 

89 for key, value in data.items(): 

90 inst[key] = [Measurement.deserialize(**element) for element in value] 

91 return inst