| 1 |  |  | import pytest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | from asgardpy.analysis import AsgardpyAnalysis | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | def test_dataset3d(base_config, caplog): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |     """Test for creating 3D DL4 dataset.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     from gammapy.datasets import MapDataset | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     base_config.dataset3d.instruments[0].input_dl3[1].glob_pattern["iso_diffuse"] = "" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     base_config.dataset3d.instruments[0].input_dl3[1].glob_pattern["gal_diffuse"] = "" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     base_config.dataset3d.instruments[0].dataset_info.background.exclusion.regions = [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     analysis = AsgardpyAnalysis(base_config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     analysis.get_3d_datasets() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     assert len(analysis.datasets) == 2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     assert isinstance(analysis.datasets[0], MapDataset) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     assert analysis.final_model[0].spectral_model.parameters[1].value == 0.015 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     assert analysis.datasets[0].counts.geom.npix == (222, 222) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     assert caplog.record_tuples[-3][2] == "Using counts_map to create safe mask" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  | def test_dataset3d_different_config(base_config, caplog): | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     """Test for creating 3D DL4 dataset with target model info from DL3 files.""" | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     analysis_0 = AsgardpyAnalysis(base_config) | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     analysis_0.config.target.from_3d = True | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     analysis_0.config.dataset3d.instruments[0].dataset_info.geom.from_events_file = False | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     analysis_0.get_3d_datasets() | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     assert analysis_0.final_model[0].spectral_model.parameters[1].value == 0.01 | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     assert analysis_0.datasets[0].counts.geom.npix == (40, 40) | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     # Use coordinates for the central region of the exclusion mask | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     base_config.dataset3d.instruments[0].dataset_info.background.exclusion.regions[0].name = "" | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     analysis_1 = AsgardpyAnalysis(base_config) | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     analysis_1.config.dataset3d.instruments[0].dataset_info.key = [] | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     analysis_1.get_3d_datasets() | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     print(caplog.record_tuples[-4][2]) | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     assert caplog.record_tuples[-4][2][:15] == "No distinct key" | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |     with pytest.raises(ValueError): | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         analysis_2 = AsgardpyAnalysis(base_config) | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         analysis_2.config.dataset3d.instruments[0].dataset_info.key = ["12"] | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         analysis_2.get_3d_datasets() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | def test_fermi_fits_file(gammapy_data_path): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     """Basic test on I/O of Fermi-LAT Fits files.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     from astropy.io import fits | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     from asgardpy.base.geom import get_source_position | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     from asgardpy.config import AsgardpyConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     config = AsgardpyConfig() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     fits_file = f"{gammapy_data_path}fermipy-crab/ft1_test.fits" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     fits_header = fits.open(fits_file)[1].header | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     source_pos = get_source_position(config.target.sky_position, fits_header) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     assert source_pos["center"].ra.deg == 83.633 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | @pytest.mark.test_data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | def test_hawc_analysis(hawc_dl3_config): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     """Basic test on running analysis of HAWC DL3 data.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     from asgardpy.analysis import AsgardpyAnalysis | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     analysis = AsgardpyAnalysis(hawc_dl3_config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     analysis.run() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     flux_table = analysis.flux_points[0].to_table(sed_type="e2dnde", formatted=True, format="gadf-sed") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 84 |  |  |     assert flux_table["counts"][3] == 4388.0 | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |  |