-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataset_utils.py
301 lines (204 loc) · 8.73 KB
/
dataset_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#
# Utilities for dealing with datasets and adding variables
#
#
import logging
import uproot
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import re
import random
import warnings
FORMAT = '%(asctime)-15s- %(levelname)s - %(name)s -%(message)s'
logging.basicConfig(format=FORMAT, level=logging.DEBUG)
logger = logging.getLogger(__name__)
#if __name__ == '__main__':
# print('Welcome!')
def df_from_root(filename,treename,columnsre=None):
"""
converting from root to a format suitable for manipulation
"""
# prepare data: open file and load tree, then convert to dataframe
logger.info(' Preparing data...')
f_ = uproot.open(filename)
t_ = f_[treename]
logger.info('Entries in file 0: ' + str(t_.num_entries))
logger.info(t_.keys())
df = t_.arrays(library="pd")
return df
def df_gen_col_name(colname, notinlist):
newname = colname
while newname in notinlist: # add random suffix if column with same name exists
newname = ''.join(random.choice(string.ascii_lowercase))
return newname
def df_cleanup(data,ranges=None):
count = 0
df = data.copy()
for i in ranges.keys():
selrange = ranges[i]
thevars = df.filter(regex=i).columns.values
for j in thevars:
count+=len(df.loc[df[j] < selrange[0]])
count+=len(df.loc[df[j] > selrange[1]])
df.loc[df[j] < selrange[0], j] = selrange[2]
df.loc[df[j] > selrange[1], j] = selrange[3]
logger.info(f"Found and replaced {count} out-of-range elements")
return df
def df_category(data,catname,ranges=None):
"""
the ranges are OR'ed
"""
df = data.copy()
df[catname] = 1
count = 0
for i in ranges.keys():
selrange = ranges[i]
thevars = df.filter(regex=i).columns.values
for j in thevars:
count+=len(df.loc[df[j] < selrange[0]])
count+=len(df.loc[df[j] > selrange[1]])
df.loc[df[j] < selrange[0], catname] = 0
df.loc[df[j] > selrange[1], catname] = 0
logger.info(f"Found {count} out-of-range elements")
return df
def df_transform_vars(data,columnsre=None):
"""
juggle features
"""
df = data.copy()
logger.info('Replacing raw features with derived ones ...')
ibar = [ re.sub(r'[a-zA-Z]*', '', s) for s in df.filter(regex="Bar[0-9]+").columns.values ]
for i in ibar:
df['dt'+i] = df['TDCBot'+i]-df['TDCTop'+i]
df['t'+i] = df['TDCBot'+i]+df['TDCTop'+i]
with np.errstate(divide='ignore', invalid='ignore'):
# https://stackoverflow.com/questions/21752989/numpy-efficiently-avoid-0s-when-taking-logmatrix
df['P'+i] = np.sqrt(df['AmpBot'+i]*df['AmpTop'+i])
df['z'+i] = np.log(df['AmpBot'+i]/df['AmpTop'+i])
df['x'+i] = np.cos(int(i) * 2 * np.pi / len(ibar))
df['y'+i] = np.sin(int(i) * 2 * np.pi / len(ibar))
if (columnsre):
savedcols = []
for patt in columnsre:
r = re.compile(patt)
savedcols += list(filter(r.match, df.columns.values))
logger.info(f'These columns will be saved: {savedcols}')
return df.filter(items=savedcols)
else:
return df
def df_extra_vars(data,columnsre=None):
"""
these variables combine the output of several columns:
a cleanup should be applied before calculating them
"""
df = data.copy()
ibar = [ re.sub(r'[a-zA-Z]*', '', s) for s in df.filter(regex="^P[0-9]+").columns.values ]
for i in ibar:
df['w'+i] = df['P'+i]*df['P'+i]
df['iw'+i] = 1./df['P'+i]/df['P'+i]
df['iwz'+i] = df['z'+i]*df['iw'+i]
df['wx'+i] = df['x'+i]*df['w'+i]
df['wy'+i] = df['y'+i]*df['w'+i]
df['wxy'+i] = df['x'+i]*df['y'+i]*df['w'+i]
df['wxx'+i] = df['x'+i]*df['x'+i]*df['w'+i]
df['wyy'+i] = df['y'+i]*df['y'+i]*df['w'+i]
df['N'] = df.filter(regex='^P[0-9]+').count(axis=1)/2
df['logN'] = np.log(df['N'])
df['Eavg'] = df.filter(regex='^P[0-9]+').mean(axis=1,skipna=True)
df['sE'] = df.filter(regex='^P[0-9]+').std(axis=1,skipna=True)/df['Eavg']
df['tavg'] = df.filter(regex='^t[0-9]+').mean(axis=1,skipna=True)
df['st'] = df.filter(regex='^t[0-9]+').std(axis=1,skipna=True)
df['dtavg']= df.filter(regex='^dt[0-9]+').mean(axis=1,skipna=True)
df['sdt'] = df.filter(regex='^dt[0-9]+').std(axis=1,skipna=True) # equivalent to Marta's delta_z
logger.info(str(df['st'].max())+' '+str(df['sdt'].max()))
df['W'] = df.filter(regex='^w[0-9]+').sum(axis=1,skipna=True)
df['IW'] = df.filter(regex='iw[0-9]+').sum(axis=1,skipna=True)
df['ZA'] = df.filter(regex='iwz[0-9]+').sum(axis=1,skipna=True)/df['IW']
df['X'] = df.filter(regex='wx[0-9]+').sum(axis=1,skipna=True)/df['W']
df['Y'] = df.filter(regex='wy[0-9]+').sum(axis=1,skipna=True)/df['W']
df['r'] = np.sqrt(df['X']**2 + df['Y']**2)
df['Sxy'] = df.filter(regex='wxy[0-9]+').sum(axis=1,skipna=True)/df['W'] - df['X']*df['Y']
df['Sxx'] = df.filter(regex='wxx[0-9]+').sum(axis=1,skipna=True)/df['W'] - df['X']*df['X']
df['Syy'] = df.filter(regex='wyy[0-9]+').sum(axis=1,skipna=True)/df['W'] - df['Y']*df['Y']
with np.errstate(divide='ignore', invalid='ignore'):
df['Corr'] = np.abs(df['Sxy']/np.sqrt(df['Sxx']*df['Syy']))
df['Corr'].replace([np.inf], 1, inplace=True)
df['Corr'].replace([-np.inf], 1, inplace=True)
df.loc[df['Corr']>1, 'Corr'] = 1
df.loc[df['Corr']<-1, 'Corr'] = -1
logger.info(columnsre)
logger.info(df)
if (columnsre):
savedcols = []
for patt in columnsre:
r = re.compile(patt)
savedcols += list(filter(r.match, df.columns.values))
logger.info(f'These columns will be saved: {savedcols}')
return df.filter(items=savedcols)
else:
return df
def df_pair_vars(data,columnsre=None):
"""
these variables are calculated from all arrangements
(disposizioni) of the provided columns
"""
df = data.copy()
tofdatain = df.filter(regex='^t[0-9]+$', axis=1).to_numpy()
cols = []
phis_over_two = []
ibar = [ re.sub(r'[a-zA-Z]*', '', s) for s in df.filter(regex="^P[0-9]+").columns.values ]
for i in ibar:
phis_over_two+=[int(i) * np.pi / len(ibar)]
#cols+=['t'+i]
cols+=['P'+i]
phidatain = np.where(~np.isnan(df.filter(cols, axis=1).to_numpy()),phis_over_two,np.nan)
xtof = val_permutedcols(tofdatain,[np.subtract],1)
maxtof= np.nanmax(xtof,1)
stdtof= np.nanstd(xtof,1)
meantof= np.nanmean(xtof,1)
mintof= np.nanmin(xtof,1)
df[['max_tof','std_tof','mean_tof','min_tof']] = np.vstack([maxtof,stdtof,meantof,mintof]).transpose()
xchord=val_permutedcols(phidatain,[np.subtract,np.sin],1)
maxchord= np.nanmax(xchord,1)
stdchord= np.nanstd(xchord,1)
meanchord= np.nanmean(xchord,1)
minchord= np.nanmin(xchord,1)
df[['max_halfchord','std_halfchord','mean_halfchord','min_halfchord']] = np.vstack([maxchord,stdchord,meanchord,minchord]).transpose()
xvel = xtof / xchord
maxvel= np.nanmax(xvel,1)
stdvel= np.nanstd(xvel,1)
meanvel= np.nanmean(xvel,1)
minvel= np.nanmin(xvel,1)
df[['max_vel','std_vel','mean_vel','min_vel']] = np.vstack([maxvel,stdvel,meanvel,minvel]).transpose()
logger.info(columnsre)
logger.info(df)
if (columnsre):
savedcols = []
for patt in columnsre:
r = re.compile(patt)
savedcols += list(filter(r.match, df.columns.values))
logger.info(f'These columns will be saved: {savedcols}')
return df.filter(items=savedcols)
else:
return df
def val_permutedcols(arr,uf_list=[np.subtract], axis=1):
"""
subsequently apply ufuncs from a list to combination of columns using broadcast
https://numpy.org/doc/stable/user/basics.broadcasting.html
https://stackoverflow.com/questions/55353703/how-to-calculate-all-combinations-of-difference-between-array-elements-in-2d
"""
# get shape and obtain broadcastable shapes by introducing singleton dimensions
s = arr.shape
s1=np.insert(s,axis,1)
s2=np.insert(s,axis+1,1)
# apply ufuncs after reshaping input broadcastable arrays against each other
# first ufunc operate on two arrays, the remaining ones operate on the output
uf = uf_list[0]
x=uf(arr.reshape(s1),arr.reshape(s2))
for uf in uf_list[1:]:
x=uf(x)
# set elements on or below diagonal to nan, then reshape
x = np.abs(x) + np.tril(np.ones_like(x)*np.nan)
x = x.reshape(s[axis-1],-1)
return x