Conditions | 1 |
Total Lines | 92 |
Code Lines | 47 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | from regions import PointSkyRegion |
||
6 | def test_gpy_mwl(gpy_mwl_config, gammapy_data_path): |
||
7 | """ |
||
8 | Test for running the 3D+1D joint analysis tutorial example from Gammapy. |
||
9 | """ |
||
10 | |||
11 | from gammapy.datasets import FluxPointsDataset |
||
12 | from gammapy.estimators import FluxPoints |
||
13 | from gammapy.modeling.models import create_crab_spectral_model |
||
14 | |||
15 | from asgardpy.data.target import set_models |
||
16 | |||
17 | analysis = AsgardpyAnalysis(gpy_mwl_config) |
||
18 | |||
19 | # Update model parameters |
||
20 | # LP-amplitude |
||
21 | analysis.config.target.components[0].spectral.parameters[0].value /= 1e4 |
||
22 | analysis.config.target.components[0].spectral.parameters[0].min = 1.0e-13 |
||
23 | analysis.config.target.components[0].spectral.parameters[0].max = 0.01 |
||
24 | analysis.config.target.components[0].spectral.parameters[0].frozen = False |
||
25 | |||
26 | # LP-reference |
||
27 | analysis.config.target.components[0].spectral.parameters[1].value *= 1e3 |
||
28 | analysis.config.target.components[0].spectral.parameters[1].min = 0.001 |
||
29 | analysis.config.target.components[0].spectral.parameters[1].max = 100 |
||
30 | |||
31 | # LP-alpha |
||
32 | analysis.config.target.components[0].spectral.parameters[2].min = 0.5 |
||
33 | analysis.config.target.components[0].spectral.parameters[2].max = 5.0 |
||
34 | analysis.config.target.components[0].spectral.parameters[2].frozen = False |
||
35 | |||
36 | # LP-beta |
||
37 | analysis.config.target.components[0].spectral.parameters[3].min = 0.001 |
||
38 | analysis.config.target.components[0].spectral.parameters[3].max = 1.0 |
||
39 | analysis.config.target.components[0].spectral.parameters[3].frozen = False |
||
40 | |||
41 | # Spatial-lon |
||
42 | analysis.config.target.components[0].spatial.parameters[0].error = 1.0e-6 |
||
43 | analysis.config.target.components[0].spatial.parameters[0].min = 83.0 |
||
44 | analysis.config.target.components[0].spatial.parameters[0].max = 84.0 |
||
45 | |||
46 | # Spatial-lat |
||
47 | analysis.config.target.components[0].spatial.parameters[1].error = 1.0e-6 |
||
48 | analysis.config.target.components[0].spatial.parameters[1].min = -90 |
||
49 | analysis.config.target.components[0].spatial.parameters[1].max = +90 |
||
50 | |||
51 | # FoV-bkg-Norm - Not being read exactly |
||
52 | analysis.config.target.components[1].spectral.parameters[0].min = 0.0 |
||
53 | analysis.config.target.components[1].spectral.parameters[0].max = 10.0 |
||
54 | analysis.config.target.components[1].spectral.parameters[0].frozen = False |
||
55 | |||
56 | analysis.run(["datasets-3d"]) |
||
57 | analysis.run(["datasets-1d"]) |
||
58 | |||
59 | # Include HAWC Flux Points |
||
60 | # Read to Gammapy objects |
||
61 | filename = f"{gammapy_data_path}hawc_crab/HAWC19_flux_points.fits" |
||
62 | fp_hawc = FluxPoints.read(filename, reference_model=create_crab_spectral_model("meyer")) |
||
63 | fpd_hawc = FluxPointsDataset(data=fp_hawc, name="HAWC") |
||
64 | |||
65 | analysis.datasets.append(fpd_hawc) |
||
66 | |||
67 | # Update other dataset info |
||
68 | analysis.dataset_name_list.append("HAWC") |
||
69 | |||
70 | """ |
||
71 | # FPE to only run for Fermi and HESS datasets, as HAWC is already estimated. |
||
72 | analysis.instrument_spectral_info["name"].append("HAWC") |
||
73 | |||
74 | hawc_en = np.array([1, 1.78, 3.16, 5.62, 10.0, 17.8, 31.6, 56.2, 100, 177, 316]) * u.TeV |
||
75 | analysis.instrument_spectral_info["spectral_energy_ranges"].append(hawc_en) |
||
76 | analysis.instrument_spectral_info["en_bins"] += 10 |
||
77 | analysis.instrument_spectral_info["DoF"] += 10 |
||
78 | """ |
||
79 | |||
80 | # Reset models to the updated dataset |
||
81 | analysis.datasets, analysis.final_model = set_models( |
||
82 | analysis.config.target, |
||
83 | analysis.datasets, |
||
84 | analysis.dataset_name_list, |
||
85 | models=analysis.final_model, |
||
86 | ) |
||
87 | |||
88 | # Update Fit energy range |
||
89 | analysis.config.fit_params.fit_range.max = "300 TeV" |
||
90 | |||
91 | analysis.run(["fit"]) |
||
92 | analysis.get_flux_points() |
||
93 | |||
94 | assert analysis.fit_result.success is True |
||
95 | assert len(analysis.datasets) == 3 |
||
96 | assert len(analysis.flux_points) == 2 |
||
97 | assert analysis.datasets[1].counts.geom.region is None |
||
98 | |||
139 |