| 1 |  |  | import pytest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | from asgardpy.config import AsgardpyConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | def test_config_basic(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |     """Test on basic Config features.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     config = AsgardpyConfig() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     assert "AsgardpyConfig" in str(config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     config_str_0 = """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     general: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |       n_jobs: 100 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     config_100 = AsgardpyConfig.from_yaml(config_str_0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     assert config_100.general.n_jobs == 100 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     with pytest.raises(ValueError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         config_str_1 = """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         fit_params: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |           fit_range: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |             min: 10 s | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             max: 10 TeV | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         AsgardpyConfig.from_yaml(config_str_1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     with pytest.raises(ValueError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         config_str_2 = """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         general: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |           outdir: ./bla/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         AsgardpyConfig.from_yaml(config_str_2) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  | def test_config_time(): | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     """Test for reading Time inputs.""" | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     from pydantic import ValidationError | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     config = AsgardpyConfig() | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     with pytest.raises(ValidationError): | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         config.dataset1d.instruments[0].dataset_info.observation.obs_time = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |             {"format": "abc", "start": "2000-01-01", "stop": "2001-01-01"} | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         ] | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     with pytest.raises(ValueError): | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         config.dataset1d.instruments[0].dataset_info.observation.obs_time = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |             {"format": "iso", "start": "60000", "stop": "2001-01-01"} | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         ] | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |     with pytest.raises(ValueError): | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         config.dataset1d.instruments[0].dataset_info.observation.obs_time = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             {"format": "iso", "start": "2001-01-01", "stop": "60000"} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | def test_get_model_template(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     """Test for reading a model template by a given tag.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     from asgardpy.config.generator import get_model_template | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     new_model = get_model_template("eclp") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     new_config = AsgardpyConfig.read(new_model) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     assert new_config.target.components[0].spectral.type == "ExpCutoffLogParabolaSpectralModel" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     with pytest.raises(IOError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         new_config.write(new_model, overwrite=False) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | def test_create_config_from_dict(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     """Test to create AsgardpyConfig from a simple dict.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     gen_dict = {"general": {"log": {"level": "warning"}}} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     config = AsgardpyConfig(**gen_dict) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     assert config.general.log.level == "warning" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | @pytest.mark.test_data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | def test_config_update_gammapy(gammapy_data_path, base_config_1d): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     """Tests to update target model config from Gammapy-based YAML files.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     import os | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     from asgardpy.config.generator import gammapy_model_to_asgardpy_model_config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     main_config = AsgardpyConfig() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     other_config_path = f"{gammapy_data_path}fermi-3fhl-crab/Fermi-LAT-3FHL_models.yaml" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     other_config_path_2 = f"{gammapy_data_path}estimators/pks2155_hess_lc/models.yaml" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     other_config_1 = gammapy_model_to_asgardpy_model_config( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         other_config_path, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         recursive_merge=False, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     other_config_2 = gammapy_model_to_asgardpy_model_config( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         other_config_path, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         base_config_1d, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         recursive_merge=False, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     main_config.write("test_base_config.yaml", overwrite=True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     other_config_3 = gammapy_model_to_asgardpy_model_config( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         other_config_path_2, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         "test_base_config.yaml", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         recursive_merge=False, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     main_config_1 = main_config.update(other_config_1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     main_config_2 = base_config_1d.update(other_config_2) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     main_config_3 = main_config.update(other_config_3) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     new_spectral_model_name = main_config_1.target.components[0].spectral.type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     new_spectral_model_name_2 = main_config_2.target.components[0].spectral.type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     new_spectral_model_name_3 = main_config_3.target.components[0].spectral.type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     index_max = main_config_3.target.components[0].spectral.parameters[0].max | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     assert new_spectral_model_name == "LogParabolaSpectralModel" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     assert new_spectral_model_name_2 == "LogParabolaSpectralModel" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     assert new_spectral_model_name_3 == "PowerLawSpectralModel" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     assert index_max == 10.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     os.remove("test_base_config.yaml") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | def test_config_update(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     """Tests to update target model config from other AsgardpyConfig file.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     from asgardpy.config.generator import get_model_template | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     main_config = AsgardpyConfig() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     spec_model_template_file_1 = get_model_template("bpl") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     spec_model_template_file_2 = get_model_template("sbpl") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     other_config_1 = AsgardpyConfig.read(spec_model_template_file_1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     other_config_2 = AsgardpyConfig.read(spec_model_template_file_2) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     main_config = main_config.update(other_config_1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     new_spectral_model_name_1 = main_config.target.components[0].spectral.type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     new_config_str = """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     general: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |       n_jobs: 100 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     main_config_2 = main_config.update(new_config_str) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     main_config = main_config.update(other_config_2, merge_recursive=True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     new_spectral_model_name_2 = main_config.target.components[0].spectral.type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     spectral_model_params = main_config.target.components[0].spectral.parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     assert new_spectral_model_name_1 == "BrokenPowerLawSpectralModel" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     assert main_config_2.general.n_jobs == 100 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     with pytest.raises(TypeError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         main_config.update(5) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     assert new_spectral_model_name_2 == "SmoothBrokenPowerLawSpectralModel" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     assert len(spectral_model_params) == 6 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | def test_write_model_config(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     """From a Gammapy Models object, write it as an AsgardpyConfig file.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     from gammapy.modeling.models import ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         ExpCutoffPowerLaw3FGLSpectralModel, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         Models, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         SkyModel, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |     from asgardpy.analysis import AsgardpyAnalysis | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     from asgardpy.config.generator import ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         CONFIG_PATH, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         AsgardpyConfig, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |         get_model_template, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |         write_asgardpy_model_to_file, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     config_ = AsgardpyConfig() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |     analysis_ = AsgardpyAnalysis(config_) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |     model_ = SkyModel(name="Template", spectral_model=ExpCutoffPowerLaw3FGLSpectralModel()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |     analysis_.final_model = Models(model_) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |     assert get_model_template("ecpl-3fgl") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |     write_asgardpy_model_to_file( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |         gammapy_model=model_, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |         output_file=str(CONFIG_PATH) + "/model_templates/model_template_ecpl-3fgl.yaml", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |     with pytest.raises(TypeError): | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 194 |  |  |         write_asgardpy_model_to_file("None") | 
            
                                                        
            
                                    
            
            
                | 195 |  |  |  |