Completed
Push — master ( fd2cae...1dc21f )
by J. Michael
08:43 queued 07:25
created

polarpy.polar2hdf5.polar_polarization_to_hdf5()   D

Complexity

Conditions 12

Size

Total Lines 88
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 44
nop 2
dl 0
loc 88
rs 4.8
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

Complexity

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
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
introduced by
Unable to import 'h5py'
Loading history...
2
import numpy as np
0 ignored issues
show
introduced by
Unable to import 'numpy'
Loading history...
3
4
5
try:
6
    import ROOT
7
    from threeML.io.cern_root_utils.io_utils import open_ROOT_file
8
    from threeML.io.cern_root_utils.tobject_to_numpy import tree_to_ndarray, th2_to_arrays
9
10
    has_root = True
0 ignored issues
show
Coding Style Naming introduced by
The name has_root does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
11
12
except(ImportError):
0 ignored issues
show
Unused Code Coding Style introduced by
There is an unnecessary parenthesis after except.
Loading history...
13
14
    has_root = False
0 ignored issues
show
Coding Style Naming introduced by
The name has_root does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
15
16
    
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
17
18
if has_root:
19
20
    def polar_polarization_to_hdf5(polarization_root_file, hdf5_out_file):
0 ignored issues
show
Comprehensibility introduced by
This function exceeds the maximum number of variables (23/15).
Loading history...
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
        
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
35
        with open_ROOT_file(polarization_root_file) as f:
0 ignored issues
show
Coding Style Naming introduced by
The name f does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
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('_')
0 ignored issues
show
Coding Style Naming introduced by
The name x does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
The name y does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
The name z does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
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
                
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
52
53
            energy = np.array(np.unique(energy))
54
            degree = np.array(np.unique(degree))
55
            angle  = np.array(np.unique(angle))
0 ignored issues
show
Coding Style introduced by
Exactly one space required before assignment
Loading history...
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))
0 ignored issues
show
Coding Style introduced by
Exactly one space required before assignment
Loading history...
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):
0 ignored issues
show
Coding Style introduced by
Exactly one space required after comma
Loading history...
Coding Style Naming introduced by
The name x does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
73
74
75
76
                    for j, y in enumerate(angle_str):
0 ignored issues
show
Coding Style Naming introduced by
The name y does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
77
78
79
80
                        for k, z in enumerate(degree_str):
0 ignored issues
show
Coding Style Naming introduced by
The name z does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
81
82
                            file_string = 'sim_%s_%s_%s' % (x, z, y)
83
84
                            _ , _, hist = th2_to_arrays(f.Get(file_string))
0 ignored issues
show
Coding Style introduced by
No space allowed before comma
Loading history...
85
86
                            out_matrix[i,j,k,:] = hist
0 ignored issues
show
Coding Style introduced by
Exactly one space required after comma
Loading history...
87
88
89
90
                database.create_dataset('matrix',data=out_matrix,compression='lzf')
0 ignored issues
show
Coding Style introduced by
Exactly one space required after comma
Loading history...
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'
0 ignored issues
show
Coding Style introduced by
Exactly one space required after comparison
Loading history...
100
                    assert np.max(bins) <=360, 'The scattering bins have egdes greater than 360'
0 ignored issues
show
Coding Style introduced by
Exactly one space required after comparison
Loading history...
101
102
                    
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
103
                
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
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')
0 ignored issues
show
Coding Style introduced by
Exactly one space required after comma
Loading history...
108
109
    def polar_spectra_to_hdf5(polar_root_file, polar_rsp_root_file, hdf5_out_file):
0 ignored issues
show
Comprehensibility introduced by
This function exceeds the maximum number of variables (19/15).
Loading history...
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:
0 ignored issues
show
Coding Style Naming introduced by
The name f does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
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)
0 ignored issues
show
Coding Style Naming introduced by
The name f does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
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:
0 ignored issues
show
Coding Style Best Practice introduced by
General except handlers without types should be used sparingly.

Typically, you would use general except handlers when you intend to specifically handle all types of errors, f.e. when logging. Otherwise, such general error handlers can mask errors in your application that you want to know of.

Loading history...
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
            
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
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
            
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
188
            
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
189
            f.Close()
190