Coverage for pesummary/core/file/_lazy/hdf5.py: 0.0%

39 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2025-11-05 13:38 +0000

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

2 

3import h5py 

4import numpy as np 

5import copy 

6from ..formats.base_read import Read 

7from .base import _LazyRead 

8 

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

10 

11 

12class LazyHDF5(_LazyRead, h5py.File): 

13 """Class to a lazily read a hdf5 file 

14 

15 Parameters 

16 ---------- 

17 drop_non_numeric: bool, optional 

18 if True, remove all non_numeric values from the posterior samples 

19 table 

20 remove_nan_likelihood_samples: bool, optional 

21 if True. remove all rows in the posterior samples table that have 'nan' 

22 likelihood 

23 path_to_samples: str, optional 

24 if provided, this path is used to extract the posterior samples. If 

25 'None', the "guess_path_to_samples" method is used. 

26 remove_params: list, optional 

27 list of parameters you wish to remove from the posterior samples 

28 table. Default None. 

29 

30 Attributes 

31 ---------- 

32 parameters: pesummary.utils.parameters.Parameters 

33 list of parameters in the posterior samples table 

34 samples: np.ndarray 

35 2D array of samples in the posterior samples table 

36 samples_dict: pesummary.utils.samples_dict.SamplesDict 

37 dictionary displaying the posterior samples table. 

38 """ 

39 def __init__(self, *args, path_to_samples=None, remove_params=None, **kwargs): 

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

41 self._path_to_samples = path_to_samples 

42 self._remove_params = remove_params 

43 

44 def grab_samples_from_file(self): 

45 self.guess_path_to_samples() 

46 c1 = isinstance(self[self._path_to_samples], h5py._hl.group.Group) 

47 if c1 and "parameter_names" not in self[self._path_to_samples].keys(): 

48 original_parameters = [i for i in self[self._path_to_samples].keys()] 

49 original_samples = np.array( 

50 [ 

51 self[self._path_to_samples][i][:] for i in 

52 original_parameters 

53 ] 

54 ).T 

55 elif c1: 

56 original_parameters = [ 

57 i.decode("utf-8") if isinstance(i, bytes) else i for i in 

58 self[self._path_to_samples]["parameter_names"] 

59 ] 

60 samples = np.array(f[path_to_samples]["samples"]) 

61 elif isinstance(self[self._path_to_samples], h5py._hl.dataset.Dataset): 

62 original_parameters = self[self._path_to_samples].dtype.names 

63 original_samples = np.array(self[self._path_to_samples]).view( 

64 (float, len(original_parameters)) 

65 ) 

66 if self._remove_params is not None: 

67 inds_to_keep = np.array( 

68 [ 

69 num for num, i in enumerate(original_parameters) if i 

70 not in self._remove_params 

71 ] 

72 ) 

73 else: 

74 inds_to_keep = np.arange(len(original_parameters)) 

75 self._parameters = np.array(original_parameters)[inds_to_keep] 

76 self._samples = (original_samples[:, inds_to_keep]) 

77 for num, par in enumerate(self._parameters): 

78 if par == "logL": 

79 self._parameters[num] = "log_likelihood" 

80 return self._samples 

81 

82 def guess_path_to_samples(self): 

83 if self._path_to_samples is None: 

84 try: 

85 self._path_to_samples = Read.guess_path_to_samples(self.filename) 

86 except ValueError: 

87 # raised if more than one set of samples found in the file. 

88 raise 

89 return self._path_to_samples