| 1 |  |  | import os | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | import pytest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | from gammapy.utils.check import check_tutorials_setup | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | # add a marker for the tests that need private data and don't run them | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | # by default | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | def pytest_configure(config): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     if "test_data" not in config.option.markexpr:  # pragma: no cover | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |         if config.option.markexpr: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |             config.option.markexpr += " and " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |         config.option.markexpr += "not test_data" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | # Check if gammapy-data is downloaded and if not, then download it. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | check_tutorials_setup(download_datasets_path="./gammapy-data") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | @pytest.fixture  # (scope="session") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | def base_config_path(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     """Get the base config path for basic tests.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     return "src/asgardpy/tests/config_test_base.yaml" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | # @pytest.mark.test_data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | @pytest.fixture  # (scope="session") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | def mwl_config_path(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     """Get the Gammapy MWL tutorial config path.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     return "src/asgardpy/tests/config_gpy_mwl.yaml" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | @pytest.fixture  # (scope="session") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | def hess_magic_config_path(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     """Get the config path for HESS (3D) + MAGIC (1D).""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     return "src/asgardpy/tests/config_test_gadf.yaml" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | @pytest.fixture  # (scope="session") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | def hawc_config_path(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     """Get the config path for HAWC (3D).""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     return "src/asgardpy/tests/config_hawc.yaml" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | @pytest.mark.test_data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | @pytest.fixture  # (scope="session") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | def ebl_deabs_path(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     """Get the base config path for basic tests.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     return "src/asgardpy/tests/config_test_ebl.yaml" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | @pytest.fixture  # (scope="session") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | def gammapy_data_path(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     """Save a copy of path of gammapy-data for easy and general use.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     # Check first for the path used in CI test | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     if os.path.exists("./gammapy-datasets/1.3/"): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         GAMMAPY_DATA = "./gammapy-datasets/1.3/" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         # Update the environ for builtin EBL models | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         os.environ["GAMMAPY_DATA"] = GAMMAPY_DATA | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         # Using the saved path in the environ for users | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         GAMMAPY_DATA = os.environ.get("GAMMAPY_DATA", "not set") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     return GAMMAPY_DATA | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | @pytest.fixture  # (scope="session") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | def base_config(base_config_path, gammapy_data_path): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     """Define the base config for basic tests.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     from asgardpy.config import AsgardpyConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     config = AsgardpyConfig().read(base_config_path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     # Update DL3 file paths | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     config.dataset3d.instruments[0].input_dl3[0].input_dir = f"{gammapy_data_path}fermipy-crab/" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     config.dataset3d.instruments[0].input_dl3[1].input_dir = f"{gammapy_data_path}fermipy-crab/" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     config.dataset1d.instruments[0].input_dl3[0].input_dir = f"{gammapy_data_path}hess-dl3-dr1/" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     config.dataset1d.instruments[1].input_dl3[0].input_dir = f"{gammapy_data_path}magic/rad_max/data/" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     return config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  | @pytest.fixture  # (scope="session") | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  | def base_config_1d(base_config): | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |     """Define base config for only 1D analysis.""" | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |     base_config_1d = base_config | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |     base_config_1d.target.source_name = "Crab Nebula" | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |     # Update model parameters | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |     base_config_1d.target.components[0].spectral.parameters[0].value = 1.0e-9 | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |     base_config_1d.target.components[0].spectral.parameters[1].value = 0.4 | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |     base_config_1d.target.components[0].spectral.parameters[2].value = 2.0 | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |     base_config_1d.fit_params.fit_range.min = "100 GeV" | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |     return base_config_1d | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | @pytest.fixture  # (scope="session") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | def gpy_mwl_config(mwl_config_path, gammapy_data_path): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     """Define the Gammapy MWL Tutorial config.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     from asgardpy.config import AsgardpyConfig, gammapy_model_to_asgardpy_model_config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     config = AsgardpyConfig().read(mwl_config_path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     # Update DL4 file paths and models file path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     config.target.models_file = f"{gammapy_data_path}fermi-3fhl-crab/Fermi-LAT-3FHL_models.yaml" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     config.dataset3d.instruments[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     ].dl4_dataset_info.dl4_dataset.input_dir = f"{gammapy_data_path}fermi-3fhl-crab/Fermi-LAT-3FHL_datasets.yaml" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     config.dataset1d.instruments[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     ].dl4_dataset_info.dl4_dataset.input_dir = f"{gammapy_data_path}joint-crab/spectra/hess/" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     other_config = gammapy_model_to_asgardpy_model_config(config.target.models_file, recursive_merge=False) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     config = config.update(other_config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     return config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | @pytest.fixture  # (scope="session") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | def gpy_hess_magic(hess_magic_config_path, gammapy_data_path): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     """Define the config for HESS (3D) + MAGIC (1D).""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     from asgardpy.config import AsgardpyConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     config = AsgardpyConfig().read(hess_magic_config_path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     # Update DL3 file paths | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     config.dataset3d.instruments[0].input_dl3[0].input_dir = f"{gammapy_data_path}hess-dl3-dr1/" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     config.dataset3d.instruments[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     ].dataset_info.background.exclusion.exclusion_file = ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         f"{gammapy_data_path}joint-crab/exclusion/exclusion_mask_crab.fits.gz" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     config.dataset1d.instruments[0].input_dl3[0].input_dir = f"{gammapy_data_path}magic/rad_max/data/" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     return config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | @pytest.fixture  # (scope="session") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | def ebl_hess_pks(ebl_deabs_path, gammapy_data_path): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     """Define the config for HESS PKS 2155-304 data.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     from asgardpy.config import AsgardpyConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     config = AsgardpyConfig().read(ebl_deabs_path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     # Update DL4 file path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     config.dataset1d.instruments[0].dl4_dataset_info.dl4_dataset.input_dir = f"{gammapy_data_path}PKS2155-steady/" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     return config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  | @pytest.fixture  # (scope="session") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | def hawc_dl3_config(hawc_config_path, gammapy_data_path): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |     """Define the config for HAWC (3D).""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     from asgardpy.config import AsgardpyConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     config = AsgardpyConfig().read(hawc_config_path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |     # Update DL3 file path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |     config.dataset3d.instruments[0].input_dl3[0].input_dir = f"{gammapy_data_path}hawc/crab_events_pass4/" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 181 |  |  |     return config | 
            
                                                        
            
                                    
            
            
                | 182 |  |  |  |