Coverage for src/galsbi/load.py: 100%

34 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-09 18:05 +0000

1# Copyright (C) 2024 ETH Zurich 

2# Institute for Particle Physics and Astrophysics 

3# Author: Silvan Fischbacher 

4# created: Wed Aug 07 2024 

5 

6# Copyright (C) 2025 LMU Munich 

7# Main contributor: Luca Tortorelli 

8# updated: Apr 2025 

9 

10 

11import os 

12import sys 

13 

14from cosmic_toolbox import arraytools as at 

15from cosmo_torrent import data_path 

16 

17from .models import ALL_MODELS 

18 

19 

20def load_abc_posterior(name): 

21 """ 

22 Loads the ABC posterior for the given model. 

23 

24 :param name: name of the model 

25 :return: ABC posterior as a structured numpy array 

26 """ 

27 if name == "Moser+24": 

28 path = data_path("Moser+24_abc_posterior") 

29 path = os.path.join(path, "abc_posterior.h5") 

30 elif name == "Fischbacher+24": 

31 path = data_path("Fischbacher+24_abc_posterior") 

32 path = os.path.join(path, "abc_posterior.h5") 

33 elif name == "Tortorelli+25": 

34 path = data_path("Tortorelli+25_fiducial_values") 

35 path = os.path.join(path, "galsbi_sps_fiducial_values.h5") 

36 elif name == "Fischbacher+26": 

37 path = data_path("Fischbacher+26_abc_posterior") 

38 path = os.path.join(path, "abc_posterior.h5") 

39 elif os.path.exists(name): 

40 path = name 

41 else: 

42 raise ValueError( 

43 f"Unknown model name '{name}'. Valid options are: {ALL_MODELS} " 

44 "or a path to an ABC posterior file." 

45 ) 

46 

47 return at.load_hdf_cols(path) 

48 

49 

50def load_config(name, mode, config_file=None): 

51 """ 

52 Loads the correct configuration for the given model and mode. If mode is 

53 "config_file", the configuration is loaded from the given file. Otherwise, the 

54 configuration is loaded based on the model name and mode. 

55 

56 :param name: name of the model 

57 :param mode: mode for which to load the configuration (either "intrinsic", "emu", 

58 "image", "config_file") 

59 :param config_file: path to a configuration file to use 

60 :return: module name of the configuration to use 

61 """ 

62 if mode == "config_file": 

63 return _check_custom_config_file(config_file) 

64 

65 module_name = f"config_{name}_{mode}" 

66 return f"galsbi.configs.{module_name}" 

67 

68 

69def _check_custom_config_file(config_file): 

70 """ 

71 Checks if the given configuration file is a path or a module name. 

72 

73 If it is a path, the module name is generated based on the path. If it is a module 

74 name, the module name is returned. 

75 """ 

76 if os.path.isfile(config_file): 

77 file_dir = os.path.abspath(os.path.dirname(config_file)) 

78 sys.path.append(file_dir) 

79 module_name = os.path.splitext(os.path.basename(config_file))[0] 

80 return module_name 

81 return config_file