Coverage for pesummary/core/file/injection.py: 95.2%

21 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-05-02 08:42 +0000

1# Licensed under an MIT style license -- see LICENSE.md 

2 

3from pesummary.utils.samples_dict import SamplesDict 

4 

5__author__ = ["Charlie Hoy <charlie.hoy@ligo.org>"] 

6 

7 

8class Injection(SamplesDict): 

9 """Class to handle injection information 

10 

11 Parameters 

12 ---------- 

13 parameters: list 

14 list of parameters 

15 samples: nd list 

16 list of samples for each parameter 

17 """ 

18 def __init__(self, *args, mapping={}, conversion_kwargs={}, **kwargs): 

19 super(Injection, self).__init__(*args, **kwargs) 

20 self.update(self.standardize_parameter_names(mapping=mapping)) 

21 

22 @classmethod 

23 def read(cls, path, mapping={}, num=0, read_kwargs={}, **kwargs): 

24 """Read an injection file and initalize the Injection class 

25 

26 Parameters 

27 ---------- 

28 path: str 

29 Path to the injection file you wish to read 

30 num: int, optional 

31 The row you wish to load. Default is 0 

32 """ 

33 from pesummary.io import read 

34 original = read(path, **read_kwargs).samples_dict 

35 if num is not None: 

36 import numpy as np 

37 for key, item in original.items(): 

38 item = np.atleast_1d(item) 

39 if num <= len(item): 

40 original[key] = [item[num]] 

41 else: 

42 raise ValueError( 

43 "Unable to extract row {} because the file only has {} " 

44 "rows".format(num, len(item)) 

45 ) 

46 return cls(original, mapping=mapping, **kwargs) 

47 

48 @property 

49 def samples_dict(self): 

50 return self