-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpre-processing.py
More file actions
275 lines (237 loc) · 11.3 KB
/
Copy pathpre-processing.py
File metadata and controls
275 lines (237 loc) · 11.3 KB
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
import numpy as np
import pandas as pd
from pandas import read_csv
import math
import matplotlib.pyplot as plt
import glob
import sys
import os
directory = "./debug_plots/"
if not os.path.exists(directory):
os.makedirs(directory)
sensor_geom = '50x12P5_0fb'
output_file = open(directory+"output"+sensor_geom+".txt", "w")
for thresh_iter in [0.1,0.15,0.2,0.3,0.4,0.5]:
threshold = thresh_iter
print("Producing datasets for thresh = ",thresh_iter)
# Global variables
noise_threshold = 600
train_dataset_name = 'dataset_9s' # for train datasets
test_dataset_name = 'dataset_8s' # for location of test (physical pT) datasets
# dataset_savedir = 'dataset_9s'
dataset_savedir = f'dataset_9s_{noise_threshold}NoiseThresh' # for save loc of final datasets
output_file.write("=========="+"\n")
output_file.write("Train dataset prod. beginning: "+str(threshold)+" thresh"+"\n")
dirtrain = '/location/of/parquets/smartpixels/'+train_dataset_name+'/'+train_dataset_name+'_'+sensor_geom+'_parquets/unflipped/'
# /location/of/parquets/smartpixels/dataset_2s/dataset_2s_50x12P5_parquets/unflipped
trainlabels = []
trainrecons = []
iter=0
suffix = 16400
for filepath in glob.iglob(dirtrain+'labels*.parquet'):
iter+=3
output_file.write(str(iter)+" files present in directory."+"\n")
for i in range(int(iter/3)):
trainlabels.append(pd.read_parquet(dirtrain+'labels_d'+str(suffix+i+1)+'.parquet'))
trainrecons.append(pd.read_parquet(dirtrain+'recon2D_d'+str(suffix+i+1)+'.parquet'))
trainlabels_csv = pd.concat(trainlabels, ignore_index=True)
trainrecons_csv = pd.concat(trainrecons, ignore_index=True)
iter_0, iter_1, iter_2 = 0, 0, 0
iter_rem = 0
for iter, row in trainlabels_csv.iterrows():
if(abs(row['pt'])>threshold):
iter_0+=1
elif(-1*threshold<=row['pt']<0):
iter_1+=1
elif(0<row['pt']<=threshold):
iter_2+=1
else:
iter_rem+=1
output_file.write("iter_0: "+str(iter_0)+"\n")
output_file.write("iter_1: "+str(iter_1)+"\n")
output_file.write("iter_2: "+str(iter_2)+"\n")
output_file.write("iter_rem: "+str(iter_rem)+"\n")
plt.hist(trainlabels_csv['pt'], bins=100)
plt.title('pT of all events')
plt.savefig(directory+"train_pt_all_"+sensor_geom+".png")
plt.close()
plt.hist(trainlabels_csv[abs(trainlabels_csv['pt'])>threshold]['pt'], bins=100)
plt.title('pT of Class 0 events')
plt.savefig(directory+"train_pt_cls0_"+sensor_geom+".png")
plt.close()
plt.hist(trainlabels_csv[(0<=trainlabels_csv['pt'])&(trainlabels_csv['pt']<=threshold)]['pt'], bins=50)
plt.hist(trainlabels_csv[(-1*threshold<=trainlabels_csv['pt'])& (trainlabels_csv['pt']<0)]['pt'], bins=50)
plt.title('pT of Class 1+2 events')
plt.savefig(directory+"train_pt_cls12_"+sensor_geom+".png")
plt.close()
number_of_events = (min(iter_1, iter_2)//1000)*1000
if(number_of_events*2>iter_0):
number_of_events = (iter_0//1000)*1000/2
number_of_events = int(number_of_events)
output_file.write("Number of events: "+str(number_of_events)+"\n")
def sumRow(X):
X = np.where(X < noise_threshold, 0, X)
sum1 = 0
sumList = []
for i in X:
sum1 = np.sum(i,axis=0)
sumList.append(sum1)
b = np.array(sumList)
return b
trainlist1, trainlist2 = [], []
hist_temp=[]
for (index1, row1), (index2, row2) in zip(trainrecons_csv.iterrows(), trainlabels_csv.iterrows()):
rowSum = 0.0
X = row1.values
X = np.reshape(X,(13,21))
rowSum = sumRow(X)
hist_temp.append(np.sum(rowSum>0))
trainlist1.append(rowSum)
cls = -1
if(abs(row2['pt'])>threshold):
cls=0
elif(-1*threshold<=row2['pt']<0):
cls=1
elif(0<=row2['pt']<=threshold):
cls=2
trainlist2.append([row2['y-local'], cls, row2['pt']])
plt.hist(hist_temp, bins=14, range=[0, 14], histtype='step', fill=False, density=True)
plt.savefig(directory+"y_profile_afterThreshold_"+sensor_geom+".png")
plt.close()
traindf_all = pd.concat([pd.DataFrame(trainlist1), pd.DataFrame(trainlist2 , columns=['y-local', 'cls', 'pt'])], axis=1)
totalsize = number_of_events
random_seed0 = 10#11
random_seed1 = 13#14
random_seed2 = 19#20
traindf_all = traindf_all.sample(frac=1, random_state=random_seed0).reset_index(drop=True)
# traindf_all.to_csv(dataset_savedir+'/'+'/FullTrainData_'+sensor_geom+'_0P'+str(threshold - int(threshold))[2:]+'thresh.csv', index=False)
traindfcls0 = traindf_all.loc[traindf_all['cls']==0]
traindfcls1 = traindf_all.loc[traindf_all['cls']==1]
traindfcls2 = traindf_all.loc[traindf_all['cls']==2]
output_file.write(str(traindfcls0.shape)+"\n")
output_file.write(str(traindfcls1.shape)+"\n")
output_file.write(str(traindfcls2.shape)+"\n")
traindfcls0 = traindfcls0.iloc[:2*totalsize]
traindfcls1 = traindfcls1.iloc[:totalsize]
traindfcls2 = traindfcls2.iloc[:totalsize]
traincls0 = traindfcls0.sample(frac = 1, random_state=random_seed1)
traincls1 = traindfcls1.sample(frac = 1, random_state=random_seed1)
traincls2 = traindfcls2.sample(frac = 1, random_state=random_seed1)
train = pd.concat([traincls0, traincls1, traincls2], axis=0)
train = train.sample(frac=1, random_state=random_seed2)
output_file.write(str(traincls0.shape)+"\n")
output_file.write(str(traincls1.shape)+"\n")
output_file.write(str(traincls2.shape)+"\n")
output_file.write(str(train.shape)+"\n")
trainlabel = train['cls']
trainpt = train['pt']
train = train.drop(['cls', 'pt'], axis=1)
output_file.write(str(train.shape)+"\n")
output_file.write(str(trainlabel.shape)+"\n")
output_file.write(str(trainpt.shape)+"\n")
train.to_csv(dataset_savedir+'/FullPrecisionInputTrainSet_'+sensor_geom+'_0P'+str(threshold - int(threshold))[2:]+'thresh.csv', index=False)
trainlabel.to_csv(dataset_savedir+'/TrainSetLabel_'+sensor_geom+'_0P'+str(threshold - int(threshold))[2:]+'thresh.csv', index=False)
trainpt.to_csv(dataset_savedir+'/TrainSetPt_'+sensor_geom+'_0P'+str(threshold - int(threshold))[2:]+'thresh.csv', index=False)
# TEST DATASET PROD
output_file.write("=========="+"\n")
output_file.write("Test dataset prod. beginning: "+str(threshold)+" thresh"+"\n")
dirtest = '/location/of/parquets/smartpixels/'+test_dataset_name+'/'+test_dataset_name+'_'+sensor_geom+'_parquets/unflipped/'
# /location/of/parquets/smartpixels/dataset_2s/dataset_2s_50x12P5_parquets/unflipped
testlabels = []
testrecons = []
iter=0
suffix = 16400
for filepath in glob.iglob(dirtest+'labels*.parquet'):
iter+=3
output_file.write(str(iter)+" files present in directory."+"\n")
for i in range(int(iter/3)):
testlabels.append(pd.read_parquet(dirtest+'labels_d'+str(suffix+i+1)+'.parquet'))
testrecons.append(pd.read_parquet(dirtest+'recon2D_d'+str(suffix+i+1)+'.parquet'))
testlabels_csv = pd.concat(testlabels, ignore_index=True)
testrecons_csv = pd.concat(testrecons, ignore_index=True)
iter_0, iter_1, iter_2 = 0, 0, 0
iter_rem = 0
for iter, row in testlabels_csv.iterrows():
if(abs(row['pt'])>threshold):
iter_0+=1
elif(-1*threshold<=row['pt']<0):
iter_1+=1
elif(0<row['pt']<=threshold):
iter_2+=1
else:
iter_rem+=1
output_file.write("iter_0: "+str(iter_0)+"\n")
output_file.write("iter_1: "+str(iter_1)+"\n")
output_file.write("iter_2: "+str(iter_2)+"\n")
output_file.write("iter_rem: "+str(iter_rem)+"\n")
plt.hist(testlabels_csv['pt'], bins=100)
plt.title('pT of all events')
plt.savefig(directory+"test_pt_all_"+sensor_geom+".png")
plt.close()
plt.hist(testlabels_csv[abs(testlabels_csv['pt'])>threshold]['pt'], bins=100)
plt.title('pT of Class 0 events')
plt.savefig(directory+"test_pt_cls0_"+sensor_geom+".png")
plt.close()
plt.hist(testlabels_csv[(0<=testlabels_csv['pt'])&(testlabels_csv['pt']<=threshold)]['pt'], bins=50)
plt.hist(testlabels_csv[(-1*threshold<=testlabels_csv['pt'])& (testlabels_csv['pt']<0)]['pt'], bins=50)
plt.title('pT of Class 1+2 events')
plt.savefig(directory+"test_pt_cls12_"+sensor_geom+".png")
plt.close()
number_of_events = (min(iter_1, iter_2)//1000)*1000
if(number_of_events*2>iter_0):
number_of_events = (iter_0//1000)*1000/2
number_of_events = int(number_of_events)
output_file.write("Number of events: "+str(number_of_events)+"\n")
testlist1, testlist2 = [], []
for (index1, row1), (index2, row2) in zip(testrecons_csv.iterrows(), testlabels_csv.iterrows()):
rowSum = 0.0
X = row1.values
X = np.reshape(X,(13,21))
rowSum = sumRow(X)
testlist1.append(rowSum)
cls = -1
if(abs(row2['pt'])>threshold):
cls=0
elif(-1*threshold<=row2['pt']<0):
cls=1
elif(0<=row2['pt']<=threshold):
cls=2
testlist2.append([row2['y-local'], cls, row2['pt']])
testdf_all = pd.concat([pd.DataFrame(testlist1), pd.DataFrame(testlist2 , columns=['y-local', 'cls', 'pt'])], axis=1)
totalsize = number_of_events#227000
random_seed0 = 10#11
random_seed1 = 13#14
random_seed2 = 19#20
testdf_all = testdf_all.sample(frac=1, random_state=random_seed0).reset_index(drop=True)
testdf_all.to_csv(dataset_savedir+'/'+'/FullTestData_'+sensor_geom+'_0P'+str(threshold - int(threshold))[2:]+'thresh.csv', index=False)
# testdfcls0 = testdf_all.loc[testdf_all['cls']==0]
# testdfcls1 = testdf_all.loc[testdf_all['cls']==1]
# testdfcls2 = testdf_all.loc[testdf_all['cls']==2]
# output_file.write(testdfcls0.shape+"\n")
# output_file.write(testdfcls1.shape+"\n")
# output_file.write(testdfcls2.shape+"\n")
# output_file.write(testdfcls2.head()+"\n")
# testdfcls0 = testdfcls0.iloc[:2*totalsize]
# testdfcls1 = testdfcls1.iloc[:totalsize]
# testdfcls2 = testdfcls2.iloc[:totalsize]
# output_file.write(testdfcls2.head()+"\n")
# testcls0 = testdfcls0.sample(frac = 1, random_state=random_seed1)
# testcls1 = testdfcls1.sample(frac = 1, random_state=random_seed1)
# testcls2 = testdfcls2.sample(frac = 1, random_state=random_seed1)
# test = pd.concat([testcls0, testcls1, testcls2], axis=0)
# test = test.sample(frac=1, random_state=random_seed2)
test=testdf_all
# output_file.write(testcls0.shape+"\n")
# output_file.write(testcls1.shape+"\n")
# output_file.write(testcls2.shape+"\n")
output_file.write(str(test.shape)+"\n")
testlabel = test['cls']
testpt = test['pt']
test = test.drop(['cls', 'pt'], axis=1)
output_file.write(str(test.shape)+"\n")
output_file.write(str(testlabel.shape)+"\n")
output_file.write(str(testpt.shape)+"\n")
test.to_csv(dataset_savedir+'/FullPrecisionInputTestSet_'+sensor_geom+'_0P'+str(threshold - int(threshold))[2:]+'thresh.csv', index=False)
testlabel.to_csv(dataset_savedir+'/TestSetLabel_'+sensor_geom+'_0P'+str(threshold - int(threshold))[2:]+'thresh.csv', index=False)
testpt.to_csv(dataset_savedir+'/TestSetPt_'+sensor_geom+'_0P'+str(threshold - int(threshold))[2:]+'thresh.csv', index=False)
output_file.close()