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

32 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-08 07:15 -0700

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 

26from collections import UserDict 

27from typing import Any 

28 

29from lsst.verify import Measurement 

30 

31 

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

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

34 actions. 

35 

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

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

38 objects produced by that `AnalysisMetric` action. 

39 

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

41 from json compatible objects. 

42 """ 

43 

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

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

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

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

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

49 

50 def json(self) -> str: 

51 result = {} 

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

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

54 if self.reference_package is not None: 

55 result["__reference_package"] = self.reference_package 

56 if self.timestamp_version is not None: 

57 result["__timestamp_version"] = self.timestamp_version 

58 if self.dataset_identifier is not None: 

59 result["__dataset_identifier"] = self.dataset_identifier 

60 return json.dumps(result) 

61 

62 @classmethod 

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

64 inst = cls() 

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

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

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

68 

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

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

71 return inst