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

29 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2025-12-17 09:47 +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 else: 

37 raise ValueError( 

38 f"Model {name} not found, only the following" 

39 f" models are available: [{ALL_MODELS}]" 

40 ) 

41 

42 return at.load_hdf_cols(path) 

43 

44 

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

46 """ 

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

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

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

50 

51 :param name: name of the model 

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

53 "image", "config_file") 

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

55 :return: module name of the configuration to use 

56 """ 

57 if mode == "config_file": 

58 return _check_custom_config_file(config_file) 

59 

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

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

62 

63 

64def _check_custom_config_file(config_file): 

65 """ 

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

67 

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

69 name, the module name is returned. 

70 """ 

71 if os.path.isfile(config_file): 

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

73 sys.path.append(file_dir) 

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

75 return module_name 

76 return config_file