Conditions | 5 |
Total Lines | 67 |
Code Lines | 39 |
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | from ..HAL import HAL |
||
6 | def fit_point_source(roi, |
||
7 | maptree, |
||
8 | response, |
||
9 | point_source_model, |
||
10 | bin_list, |
||
11 | confidence_intervals=False, |
||
12 | liff=False, |
||
13 | pixel_size=0.17, |
||
14 | verbose=False): |
||
15 | |||
16 | data_radius = roi.data_radius.to("deg").value |
||
17 | |||
18 | if not liff: |
||
19 | |||
20 | # This is a 3ML plugin |
||
21 | hawc = HAL("HAWC", |
||
22 | maptree, |
||
23 | response, |
||
24 | roi, |
||
25 | flat_sky_pixels_size=pixel_size) |
||
26 | |||
27 | hawc.set_active_measurements(bin_list=bin_list) |
||
28 | |||
29 | else: |
||
30 | |||
31 | from threeML import HAWCLike |
||
32 | |||
33 | hawc = HAWCLike("HAWC", |
||
34 | maptree, |
||
35 | response, |
||
36 | fullsky=True) |
||
37 | |||
38 | hawc.set_bin_list(bin_list) |
||
39 | |||
40 | ra_roi, dec_roi = roi.ra_dec_center |
||
41 | |||
42 | hawc.set_ROI(ra_roi, dec_roi, data_radius) |
||
43 | |||
44 | if not liff: |
||
45 | |||
46 | hawc.display() |
||
47 | |||
48 | data = DataList(hawc) |
||
49 | |||
50 | jl = JointLikelihood(point_source_model, data, verbose=verbose) |
||
51 | |||
52 | point_source_model.display(complete=True) |
||
53 | |||
54 | try: |
||
55 | |||
56 | jl.set_minimizer("minuit") |
||
57 | |||
58 | except: |
||
59 | |||
60 | jl.set_minimizer("minuit") |
||
61 | |||
62 | param_df, like_df = jl.fit() |
||
63 | |||
64 | if confidence_intervals: |
||
65 | |||
66 | ci = jl.get_errors() |
||
67 | |||
68 | else: |
||
69 | |||
70 | ci = None |
||
71 | |||
72 | return param_df, like_df, ci, jl.results |
||
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.