| Conditions | 7 |
| Total Lines | 81 |
| Code Lines | 32 |
| 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 h5py |
||
| 109 | def polar_spectra_to_hdf5(polar_root_file, polar_rsp_root_file, hdf5_out_file): |
||
| 110 | """ |
||
| 111 | |||
| 112 | :param polar_root_file: The spectral ROOT file |
||
| 113 | :param polar_rsp_root_file: The response ROOT file |
||
| 114 | :param hdf5_out_file: the name of the output HDF5 file |
||
| 115 | """ |
||
| 116 | |||
| 117 | # extract the info from the crappy root file |
||
| 118 | |||
| 119 | with h5py.File(hdf5_out_file, 'w') as outfile: |
||
| 120 | |||
| 121 | # first we do the RSP |
||
| 122 | |||
| 123 | rsp_grp = outfile.create_group('rsp') |
||
| 124 | |||
| 125 | with open_ROOT_file(polar_rsp_root_file) as f: |
||
| 126 | |||
| 127 | matrix = th2_to_arrays(f.Get('rsp'))[-1] |
||
| 128 | |||
| 129 | rsp_grp.create_dataset('matrix', data=matrix, compression='lzf') |
||
| 130 | |||
| 131 | ebounds = th2_to_arrays(f.Get('EM_bounds'))[-1] |
||
| 132 | |||
| 133 | rsp_grp.create_dataset('ebounds', data=ebounds, compression='lzf') |
||
| 134 | |||
| 135 | mc_low = th2_to_arrays(f.Get('ER_low'))[-1] |
||
| 136 | |||
| 137 | rsp_grp.create_dataset('mc_low', data=mc_low, compression='lzf') |
||
| 138 | |||
| 139 | mc_high = th2_to_arrays(f.Get('ER_high'))[-1] |
||
| 140 | |||
| 141 | rsp_grp.create_dataset('mc_high', data=mc_high, compression='lzf') |
||
| 142 | |||
| 143 | # now we get the spectral informations |
||
| 144 | keys_to_use = ['polar_out'] |
||
| 145 | |||
| 146 | f = ROOT.TFile(polar_root_file) |
||
| 147 | |||
| 148 | extra_grp = outfile.create_group('extras') |
||
| 149 | |||
| 150 | for key in f.GetListOfKeys(): |
||
| 151 | |||
| 152 | name = key.GetName() |
||
| 153 | |||
| 154 | if name not in keys_to_use: |
||
| 155 | try: |
||
| 156 | |||
| 157 | # first we see if it is a TTree and then |
||
| 158 | # add a new group and attach its data |
||
| 159 | |||
| 160 | tree = tree_to_ndarray(f.Get(name)) |
||
| 161 | |||
| 162 | new_grp = extra_grp.create_group(name) |
||
| 163 | |||
| 164 | for new_name in tree.dtype.names: |
||
| 165 | new_grp.create_dataset(new_name, data=tree[new_name], compression='lzf') |
||
| 166 | |||
| 167 | except: |
||
| 168 | |||
| 169 | # in this case we just want the actual data |
||
| 170 | |||
| 171 | data = th2_to_arrays(f.Get(name))[-1] |
||
| 172 | |||
| 173 | extra_grp.create_dataset(name, data=data, compression='lzf') |
||
| 174 | |||
| 175 | # now we will deal with the data that is important |
||
| 176 | |||
| 177 | tmp = tree_to_ndarray(f.Get('polar_out')) |
||
| 178 | |||
| 179 | outfile.create_dataset('energy', data=tmp['Energy'], compression='lzf') |
||
| 180 | |||
| 181 | outfile.create_dataset('scatter_angle', data=tmp['scatter_angle'], compression='lzf') |
||
| 182 | |||
| 183 | outfile.create_dataset('dead_ratio', data=tmp['dead_ratio'], compression='lzf') |
||
| 184 | |||
| 185 | outfile.create_dataset('time', data=tmp['tunix'], compression='lzf') |
||
| 186 | |||
| 187 | |||
| 188 | |||
| 189 | f.Close() |
||
| 190 |
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.