Conditions | 6 |
Total Lines | 105 |
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 | import os |
||
40 | def from_root_file(map_tree_file, roi): |
||
41 | """ |
||
42 | Create a MapTree object from a ROOT file and a ROI. Do not use this directly, use map_tree_factory instead. |
||
43 | |||
44 | :param map_tree_file: |
||
45 | :param roi: |
||
46 | :return: |
||
47 | """ |
||
48 | |||
49 | from ..root_handler import open_ROOT_file, root_numpy, tree_to_ndarray |
||
50 | |||
51 | map_tree_file = sanitize_filename(map_tree_file) |
||
52 | |||
53 | # Check that they exists and can be read |
||
54 | |||
55 | if not file_existing_and_readable(map_tree_file): # pragma: no cover |
||
56 | raise IOError("MapTree %s does not exist or is not readable" % map_tree_file) |
||
57 | |||
58 | # Make sure we have a proper ROI (or None) |
||
59 | |||
60 | assert isinstance(roi, HealpixROIBase) or roi is None, "You have to provide an ROI choosing from the " \ |
||
61 | "available ROIs in the region_of_interest module" |
||
62 | |||
63 | if roi is None: |
||
64 | custom_warnings.warn("You have set roi=None, so you are reading the entire sky") |
||
65 | |||
66 | # Read map tree |
||
67 | |||
68 | with open_ROOT_file(map_tree_file) as f: |
||
69 | |||
70 | data_bins_labels = list(root_numpy.tree2array(f.Get("BinInfo"), "name")) |
||
71 | |||
72 | # A transit is defined as 1 day, and totalDuration is in hours |
||
73 | # Get the number of transit from bin 0 (as LiFF does) |
||
74 | |||
75 | n_transits = root_numpy.tree2array(f.Get("BinInfo"), "totalDuration") / 24.0 |
||
76 | |||
77 | # The map-maker underestimate the livetime of bins with low statistic by removing time intervals with |
||
78 | # zero events. Therefore, the best estimate of the livetime is the maximum of n_transits, which normally |
||
79 | # happen in the bins with high statistic |
||
80 | n_transits = max(n_transits) |
||
81 | |||
82 | n_bins = len(data_bins_labels) |
||
83 | |||
84 | # These are going to be Healpix maps, one for each data analysis bin_name |
||
85 | |||
86 | data_analysis_bins = collections.OrderedDict() |
||
87 | |||
88 | for i in range(n_bins): |
||
89 | |||
90 | name = data_bins_labels[i] |
||
91 | |||
92 | data_tobject = _get_bin_object(f, name, "data") |
||
93 | |||
94 | bkg_tobject = _get_bin_object(f, name, "bkg") |
||
95 | |||
96 | # Get ordering scheme |
||
97 | nside = data_tobject.GetUserInfo().FindObject("Nside").GetVal() |
||
98 | nside_bkg = bkg_tobject.GetUserInfo().FindObject("Nside").GetVal() |
||
99 | |||
100 | assert nside == nside_bkg |
||
101 | |||
102 | scheme = data_tobject.GetUserInfo().FindObject("Scheme").GetVal() |
||
103 | scheme_bkg = bkg_tobject.GetUserInfo().FindObject("Scheme").GetVal() |
||
104 | |||
105 | assert scheme == scheme_bkg |
||
106 | |||
107 | assert scheme == 0, "NESTED scheme is not supported yet" |
||
108 | |||
109 | if roi is not None: |
||
110 | |||
111 | # Only read the elements in the ROI |
||
112 | |||
113 | active_pixels = roi.active_pixels(nside, system='equatorial', ordering='RING') |
||
114 | |||
115 | counts = _read_partial_tree(data_tobject, active_pixels) |
||
116 | bkg = _read_partial_tree(bkg_tobject, active_pixels) |
||
117 | |||
118 | counts_hpx = SparseHealpix(counts, active_pixels, nside) |
||
119 | bkg_hpx = SparseHealpix(bkg, active_pixels, nside) |
||
120 | |||
121 | this_data_analysis_bin = DataAnalysisBin(name, |
||
122 | counts_hpx, |
||
123 | bkg_hpx, |
||
124 | active_pixels_ids=active_pixels, |
||
125 | n_transits=n_transits, |
||
126 | scheme='RING') |
||
127 | |||
128 | else: |
||
129 | |||
130 | # Read the entire sky. |
||
131 | |||
132 | counts = tree_to_ndarray(data_tobject, "count").astype(np.float64) |
||
133 | bkg = tree_to_ndarray(bkg_tobject, "count").astype(np.float64) |
||
134 | |||
135 | this_data_analysis_bin = DataAnalysisBin(name, |
||
136 | DenseHealpix(counts), |
||
137 | DenseHealpix(bkg), |
||
138 | active_pixels_ids=None, |
||
139 | n_transits=n_transits, |
||
140 | scheme='RING') |
||
141 | |||
142 | data_analysis_bins[name] = this_data_analysis_bin |
||
143 | |||
144 | return data_analysis_bins |
||
145 | |||
201 |
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.