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

26 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-13 03:24 +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 

7import os 

8import sys 

9 

10from cosmic_toolbox import arraytools as at 

11from cosmo_torrent import data_path 

12 

13from .models import ALL_MODELS 

14 

15 

16def load_abc_posterior(name): 

17 """ 

18 Loads the ABC posterior for the given model. 

19 

20 :param name: name of the model 

21 :return: ABC posterior as a structured numpy array 

22 """ 

23 if name == "Moser+24": 

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

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

26 elif name == "Fischbacher+24": 

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

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

29 else: 

30 raise ValueError( 

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

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

33 ) 

34 

35 return at.load_hdf_cols(path) 

36 

37 

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

39 """ 

40 Loads the correct configuration for the given model and mode. 

41 If mode is "config_file", the configuration is loaded from the given file. 

42 Otherwise, the configuration is loaded based on the model name and mode. 

43 

44 :param name: name of the model 

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

46 "image", "config_file") 

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

48 :return: module name of the configuration to use 

49 """ 

50 if mode == "config_file": 

51 return _check_custom_config_file(config_file) 

52 

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

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

55 

56 

57def _check_custom_config_file(config_file): 

58 """ 

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

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

61 If it is a module name, the module name is returned. 

62 """ 

63 if os.path.isfile(config_file): 

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

65 sys.path.append(file_dir) 

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

67 return module_name 

68 return config_file