| Conditions | 9 |
| Total Lines | 98 |
| Code Lines | 44 |
| 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 |
||
| 14 | def polar_polarization_to_hdf5(polarization_root_file, hdf5_out_file): |
||
| 15 | """ |
||
| 16 | Converts the ROOT POLAR response into an HDF5 file so that users are not |
||
| 17 | dependent on ROOT. |
||
| 18 | |||
| 19 | :param polarization_root_file: The ROOT file from which to build the response |
||
| 20 | :param hdf5_out_file: The output HDF5 file name |
||
| 21 | """ |
||
| 22 | |||
| 23 | # create a few lists so that we can hold the values |
||
| 24 | |||
| 25 | energy = [] |
||
| 26 | degree = [] |
||
| 27 | angle = [] |
||
| 28 | |||
| 29 | energy_str = [] |
||
| 30 | degree_str = [] |
||
| 31 | angle_str = [] |
||
| 32 | |||
| 33 | # open the ROOT file |
||
| 34 | |||
| 35 | with open_ROOT_file(polarization_root_file) as f: |
||
| 36 | |||
| 37 | # This looks at all the info in the ROOT file |
||
| 38 | # It is gross because ROOT is gross. |
||
| 39 | |||
| 40 | tmp = [key.GetName() for key in f.GetListOfKeys()] |
||
| 41 | tmp = filter(lambda x: 'sim' in x, tmp) |
||
| 42 | for tmp2 in tmp: |
||
| 43 | _, x, y, z = tmp2.split('_') |
||
| 44 | |||
| 45 | energy.append(float(x)) |
||
| 46 | degree.append(float(y)) |
||
| 47 | angle.append(float(z)) |
||
| 48 | |||
| 49 | energy_str.append(x) |
||
| 50 | degree_str.append(y) |
||
| 51 | angle_str.append(z) |
||
| 52 | |||
| 53 | # There are duplicates everywhere. |
||
| 54 | # This makes sure we only grab what we need. |
||
| 55 | |||
| 56 | energy = np.array(np.unique(energy)) |
||
| 57 | degree = np.array(np.unique(degree)) |
||
| 58 | angle = np.array(np.unique(angle)) |
||
| 59 | |||
| 60 | energy_str = np.array(np.unique(energy_str)) |
||
| 61 | degree_str = np.array(np.unique(degree_str)) |
||
| 62 | angle_str = np.array(np.unique(angle_str)) |
||
| 63 | |||
| 64 | # just to get the bins |
||
| 65 | # must change this from ints later |
||
| 66 | |||
| 67 | file_string = 'sim_%s_%s_%s' % (energy_str[1], degree_str[1], angle_str[1]) |
||
| 68 | |||
| 69 | bins, _, hist = th2_to_arrays(f.Get(file_string)) |
||
| 70 | |||
| 71 | out_matrix = np.zeros((len(energy), len(angle), len(degree), len(hist))) |
||
| 72 | |||
| 73 | # Now we will build the HDF5 file. Much eaasier because the format is |
||
| 74 | # beautiful. |
||
| 75 | |||
| 76 | with h5py.File(hdf5_out_file, 'w', libver='latest') as database: |
||
| 77 | |||
| 78 | for i, x in enumerate(energy_str): |
||
| 79 | |||
| 80 | for j, y in enumerate(angle_str): |
||
| 81 | |||
| 82 | for k, z in enumerate(degree_str): |
||
| 83 | |||
| 84 | file_string = 'sim_%s_%s_%s' % (x, z, y) |
||
| 85 | |||
| 86 | _, _, hist = th2_to_arrays(f.Get(file_string)) |
||
| 87 | |||
| 88 | # Some beautiful matrix math |
||
| 89 | |||
| 90 | out_matrix[i, j, k, :] = hist |
||
| 91 | |||
| 92 | # write to the matrix extension |
||
| 93 | |||
| 94 | database.create_dataset('matrix', data=out_matrix, compression='lzf') |
||
| 95 | |||
| 96 | if np.min(bins) < 0: |
||
| 97 | # we will try to automatically correct for the |
||
| 98 | # badly specified bins |
||
| 99 | bins = np.array(bins) |
||
| 100 | |||
| 101 | bins += -np.min(bins) |
||
| 102 | |||
| 103 | assert np.min(bins) >= 0, 'The scattering bins have egdes less than zero' |
||
| 104 | assert np.max(bins) <= 360, 'The scattering bins have egdes greater than 360' |
||
| 105 | |||
| 106 | # Save all this out. We MUST write some docs describing the format at some point |
||
| 107 | |||
| 108 | database.create_dataset('bins', data=bins, compression='lzf') |
||
| 109 | database.create_dataset('pol_ang', data=angle, compression='lzf') |
||
| 110 | database.create_dataset('pol_deg', data=degree, compression='lzf') |
||
| 111 | database.create_dataset('energy', data=energy, compression='lzf') |
||
| 112 | |||
| 194 |