Coverage for src/shephex/where.py: 100%

18 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-03-29 18:45 +0100

1""" 

2This module provides functions to find experiments that match certain conditions. 

3""" 

4from pathlib import Path 

5from typing import Any, List, Tuple, Union 

6 

7from shephex import Study 

8from shephex.experiment import Experiment, ExperimentResult, Options 

9 

10 

11def id_where(*args, directory: Union[Path, str], **kwargs) -> Tuple[List[int], List[Options]]: 

12 """ 

13 Get the ids of the experiments that match the conditions. 

14 

15 Parameters 

16 ---------- 

17 directory : Union[Path, str] 

18 The directory where the experiments are stored. 

19 args : Any 

20 Positional arguments for the experiments to find.  

21 kwargs : Any 

22 Keyword arguments for the experiments to find. 

23  

24 Returns 

25 ------- 

26 Tuple[List[int], List[Options]] 

27 The ids and options of the experiments that match the conditions. 

28 """ 

29 study = Study(directory) 

30 ids, options = study.where(*args, **kwargs, load_shephex_options=True) 

31 return ids, options 

32 

33def path_where(*args, directory: Union[Path, str], **kwargs) -> Tuple[List[Path], List[Options]]: 

34 """ 

35 Get the paths of the experiments that match the conditions. 

36 

37 Parameters 

38 ---------- 

39 directory : Union[Path, str] 

40 The directory where the experiments are stored. 

41 args : Any 

42 Positional arguments for the experiments to find. 

43 kwargs : Any 

44 Keyword arguments for the experiments to find. 

45 

46 Returns 

47 ------- 

48 Tuple[List[Path], List[Options]] 

49 The paths and options of the experiments that match the conditions. 

50 """ 

51 ids, options = id_where(*args, directory=directory, **kwargs) 

52 paths = [Path(directory) / f"{id_}-{Experiment.extension}" for id_ in ids] 

53 return paths, options 

54 

55def result_where(*args, directory: Union[Path, str], result_object: bool = False, **kwargs) -> Tuple[List[Any], List[Options]]: 

56 """ 

57 Get the results of the experiments that match the conditions. 

58 

59 Parameters 

60 ---------- 

61 directory : Union[Path, str] 

62 The directory where the experiments are stored. 

63 result_object : bool 

64 Whether to return the ExperimentResult object or the result object. 

65 args : Any 

66 Positional arguments for the experiments to find. 

67 

68 Returns 

69 ------- 

70 Tuple[List[Any], List[Options]] 

71 The results and options of the experiments that match the conditions 

72 """ 

73 paths, options = path_where(*args, directory=directory, **kwargs) 

74 results = [ExperimentResult.load(path / "shephex") for path in paths] 

75 if not result_object: 

76 results = [result.result for result in results] 

77 return results, options 

78 

79 

80 

81 

82