Conditions | 6 |
Total Lines | 88 |
Code Lines | 45 |
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 | import h5py |
||
9 | def __init__(self, |
||
10 | counts, |
||
11 | scattering_bins, |
||
12 | exposures, |
||
13 | count_errors=None, |
||
14 | sys_errors=None, |
||
15 | scale_factor=1., |
||
16 | mission=None, |
||
17 | instrument=None, |
||
18 | tstart=None, |
||
19 | tstop=None): |
||
20 | """ |
||
21 | |||
22 | A interface for the modulation curve to facilitate the reading and writing of files |
||
23 | |||
24 | :param counts: a matrix of counts where the rows are time intervals and the columns are scattering bins |
||
25 | :param scattering_bins: and array of scattering bin edges |
||
26 | :param exposures: and array of exposures for each time bin |
||
27 | :param count_errors: a matrix of counts where the rows are time intervals and the columns are scattering bins |
||
28 | :param sys_errors: a matrix of counts where the rows are time intervals and the columns are scattering bins |
||
29 | :param scale_factor: the scale factor of the background |
||
30 | :param mission: the space mission |
||
31 | :param instrument: the instrument on the mission |
||
32 | :param tstart: an array of start times for the intervals |
||
33 | :param tstop: an array of stop times for the intervals |
||
34 | """ |
||
35 | |||
36 | # make sure that all the arrays are the correct shape |
||
37 | |||
38 | counts = np.atleast_2d(counts) |
||
39 | exposures = np.atleast_1d(exposures) |
||
40 | |||
41 | assert len(exposures.shape) == 1 |
||
42 | |||
43 | assert len(counts.shape) == 2 |
||
44 | |||
45 | # extract the shape so we know the interval size |
||
46 | # as well as the number of bins |
||
47 | |||
48 | n_intervals, n_bins = counts.shape |
||
49 | |||
50 | assert len(exposures) == n_intervals |
||
51 | |||
52 | assert len(scattering_bins) == n_bins + 1, 'The shape of the counts is incorrect' |
||
53 | |||
54 | assert np.all(exposures >= 0.), 'exposures are not positive' |
||
55 | |||
56 | if count_errors is not None: |
||
57 | |||
58 | self._is_poisson = False |
||
59 | count_errors = np.atleast_2d(count_errors) |
||
60 | |||
61 | assert np.all(count_errors.shape == counts.shape) |
||
62 | |||
63 | else: |
||
64 | |||
65 | self._is_poisson = True |
||
66 | |||
67 | self._count_errors = count_errors |
||
68 | |||
69 | # correct start and stops |
||
70 | tmp = [tstart, tstop] |
||
71 | |||
72 | for i, _ in enumerate(tmp): |
||
73 | |||
74 | if tmp[i] is not None: |
||
75 | tmp[i] = np.atleast_1d(tmp[i]) |
||
76 | assert len(tmp[i].shape) == 1 |
||
77 | assert len(tmp[i]) == n_intervals |
||
78 | |||
79 | ## fix this later |
||
80 | self._sys_errors = sys_errors |
||
81 | |||
82 | if instrument is None: |
||
83 | instrument = 'Unknown' |
||
84 | |||
85 | if mission is None: |
||
86 | mission = 'Unknown' |
||
87 | |||
88 | self._counts = counts |
||
89 | self._scattering_bins = scattering_bins |
||
90 | self._exposures = exposures |
||
91 | self._n_intervals = n_intervals |
||
92 | self._scale_factor = scale_factor |
||
93 | self._tstart = tstart |
||
94 | self._tstop = tstop |
||
95 | self._instrument = instrument |
||
96 | self._mission = mission |
||
97 | |||
305 |