Conditions | 12 |
Total Lines | 88 |
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:
Complex classes like polarpy.polar2hdf5.polar_polarization_to_hdf5() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | import h5py |
||
20 | def polar_polarization_to_hdf5(polarization_root_file, hdf5_out_file): |
||
21 | """ |
||
22 | |||
23 | :param polarization_root_file: The ROOT file from which to build the response |
||
24 | :param hdf5_out_file: The output HDF5 file name |
||
25 | """ |
||
26 | energy = [] |
||
27 | degree = [] |
||
28 | angle = [] |
||
29 | |||
30 | energy_str = [] |
||
31 | degree_str = [] |
||
32 | angle_str = [] |
||
33 | |||
34 | |||
35 | with open_ROOT_file(polarization_root_file) as f: |
||
36 | |||
37 | tmp = [key.GetName() for key in f.GetListOfKeys()] |
||
38 | tmp = filter(lambda x: 'sim' in x, tmp) |
||
39 | for tmp2 in tmp: |
||
40 | _, x, y, z = tmp2.split('_') |
||
41 | |||
42 | energy.append(float(x)) |
||
43 | degree.append(float(y)) |
||
44 | angle.append(float(z)) |
||
45 | |||
46 | energy_str.append(x) |
||
47 | degree_str.append(y) |
||
48 | angle_str.append(z) |
||
49 | |||
50 | |||
51 | |||
52 | |||
53 | energy = np.array(np.unique(energy)) |
||
54 | degree = np.array(np.unique(degree)) |
||
55 | angle = np.array(np.unique(angle)) |
||
56 | |||
57 | energy_str = np.array(np.unique(energy_str)) |
||
58 | degree_str = np.array(np.unique(degree_str)) |
||
59 | angle_str = np.array(np.unique(angle_str)) |
||
60 | |||
61 | # just to get the bins |
||
62 | # must change this from ints later |
||
63 | |||
64 | file_string = 'sim_%s_%s_%s' % (energy_str[1], degree_str[1], angle_str[1]) |
||
65 | |||
66 | bins, _, hist = th2_to_arrays(f.Get(file_string)) |
||
67 | |||
68 | out_matrix = np.zeros((len(energy), len(angle), len(degree), len(hist))) |
||
69 | |||
70 | with h5py.File(hdf5_out_file, 'w', libver='latest') as database: |
||
71 | |||
72 | for i,x in enumerate(energy_str): |
||
73 | |||
74 | |||
75 | |||
76 | for j, y in enumerate(angle_str): |
||
77 | |||
78 | |||
79 | |||
80 | for k, z in enumerate(degree_str): |
||
81 | |||
82 | file_string = 'sim_%s_%s_%s' % (x, z, y) |
||
83 | |||
84 | _ , _, hist = th2_to_arrays(f.Get(file_string)) |
||
85 | |||
86 | out_matrix[i,j,k,:] = hist |
||
87 | |||
88 | |||
89 | |||
90 | database.create_dataset('matrix',data=out_matrix,compression='lzf') |
||
91 | |||
92 | |||
93 | if np.min(bins) < 0: |
||
94 | # we will try to automatically correct for the badly specified bins |
||
95 | bins = np.array(bins) |
||
96 | |||
97 | bins += -np.min(bins) |
||
98 | |||
99 | assert np.min(bins) >=0, 'The scattering bins have egdes less than zero' |
||
100 | assert np.max(bins) <=360, 'The scattering bins have egdes greater than 360' |
||
101 | |||
102 | |||
103 | |||
104 | database.create_dataset('bins', data=bins, compression='lzf') |
||
105 | database.create_dataset('pol_ang', data=angle, compression='lzf') |
||
106 | database.create_dataset('pol_deg', data=degree, compression='lzf') |
||
107 | database.create_dataset('energy',data=energy,compression='lzf') |
||
108 | |||
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.