17 import matplotlib.pyplot
as plt
19 import tensorflow.keras
20 from tensorflow.keras.models
import Sequential
21 from tensorflow.keras.layers
import Dense
22 from tensorflow.keras.layers
import Dropout
23 from tensorflow.keras.utils
import to_categorical
24 from tensorflow.keras.utils
import plot_model
25 from tensorflow.keras
import regularizers
26 from tensorflow.keras.wrappers.scikit_learn
import KerasRegressor
27 from sklearn.model_selection
import GridSearchCV, RandomizedSearchCV,train_test_split, cross_val_score
28 from sklearn.metrics
import roc_curve, auc
29 from tensorflow.keras.layers
import Activation
30 from tensorflow.keras
import backend
as K
31 from tensorflow.keras.models
import save_model
32 import tensorflow
as tf
33 from tensorflow
import keras
39 parser1 = argparse.ArgumentParser()
40 parser1.add_argument(
"-I",
"--input",help=
"input file",default=
"isotk_relval_lowinter1.pkl")
41 parser1.add_argument(
"-V",
"--modelv",help=
"model version (any number) ",default=
"2")
43 args = parser1.parse_args(args=[])
52 print (
"file Name:", fName)
53 print (
"modv :", modv)
59 df = pd.read_pickle(fName)
60 print (
"vars in file:",df.keys())
61 print(
"events in df original:",df.shape[0])
62 df = df.loc[df[
't_eHcal_y'] > 20]
63 print(
"events in df after energy cut:",df.shape[0])
64 df[
't_eHcal_xun'] = df[
't_eHcal_x']
65 df[
't_delta_un'] = df[
't_delta']
66 df[
't_ieta_un'] = df[
't_ieta']
79 mina.append(df[i].
min())
80 maxa.append(df[i].
max())
90 cols_to_minmax = [
't_delta',
't_hmaxNearP',
't_emaxNearP',
't_hAnnular',
't_eAnnular',
't_pt',
't_nVtx',
't_ieta',
't_eHcal10',
't_eHcal30',
't_rhoh',
't_eHcal_x']
94 df[cols_to_minmax] = df[cols_to_minmax].
apply(
lambda x: (x - x.min()) / (x.max() - x.min()))
97 print (
'data shape:',data.shape)
100 print (
'targets shape:', targets.shape)
101 print (
"vars in file:",df.keys())
109 testindx = data.shape[0] - ntest
113 print (
'data shape:',data.shape[0])
114 print (
'testindx: ' ,testindx)
116 X_train = data[:testindx,0:12]
117 Y_train = data[:testindx,12]
118 X_test = data[testindx:,:]
119 print (
"shape of X_train:",X_train.shape)
120 print (
"shape of Y_train:",Y_train.shape)
121 print (
"shape of X_test:",X_test.shape)
122 meany = np.mean(Y_train)
123 print (
"mean y:",meany)
124 stdy = np.std(Y_train)
125 print (
"std y:", stdy)
132 a0 = [0.973, 0.998, 0.992, 0.965 ]
133 a1 = [0, -0.318, -0.261, -0.406]
134 a2 = [0, 0, 0.047, 0.089]
139 idx = (
int(jeta >= PU_IETA_1) +
int(jeta >= PU_IETA_2) +
int(jeta >= PU_IETA_3))
145 idx = (
int(jeta >= PU_IETA_1) +
int(jeta >= PU_IETA_2) +
int(jeta >= PU_IETA_3))
151 idx = (
int(jeta >= PU_IETA_1) +
int(jeta >= PU_IETA_2) +
int(jeta >= PU_IETA_3))
158 vec0 = np.vectorize(fac0)
159 vec1 = np.vectorize(fac1)
160 vec2 = np.vectorize(fac2)
163 eop = (X_test[:,15]/X_test[:,13])
164 dop = (X_test[:,16]/X_test[:,13])
168 mcorrval = vec0(
abs(X_test[:,17])) + vec1(
abs(X_test[:,17]))*eop*dop*( 1 + vec2(
abs(X_test[:,17]))*dop)
175 weight = np.copy(y_true)
176 weight[
abs(y_true - meany) > 0] = 1.90*
abs(y_true - meany)/stdy
178 weight[
abs(y_true - meany) < stdy] = 1
179 print (
"wieght : ", weight)
186 from keras
import optimizers
187 print (
"creating model=========>")
189 model.add(Dense(128, input_shape=(X_train.shape[1],), activation=
'relu'))
191 model.add(Dense(271, activation=
'relu',kernel_regularizer=regularizers.l2(0.01)))
192 model.add(Dense(301, activation=
'relu',kernel_regularizer=regularizers.l2(0.01)))
193 model.add(Dense(241, activation=
'relu',kernel_regularizer=regularizers.l2(0.01)))
200 print (
"compilation up next=======>")
201 model.compile(loss=
'logcosh',optimizer=
'adam')
205 print (
"fitting now=========>")
206 history = model.fit(X_train,Y_train , batch_size=5000, epochs=50, validation_split=0.2, verbose=1,sample_weight=
propweights(Y_train))
212 plt.plot(history.history[
'loss'])
213 plt.plot(history.history[
'val_loss'])
214 plt.title(
'model loss')
217 plt.legend([
'train',
'validation'], loc=
'upper left')
218 plt.savefig(modv+
'_loss_distributions_lowWinter.png')
226 preds = model.predict(X_test[:,0:12])
227 targets = X_test[:,12]
228 uncorrected = X_test[:,15]
229 marinascorr = X_test[:,15]*mcorrval
230 plt.hist(preds, bins =100, range=(0,200),label=
'PU regression',alpha=0.6)
231 plt.hist(targets, bins =100, range=(0,200),label=
'no PU',alpha=0.6)
232 plt.hist(uncorrected, bins =100, range=(0,200),label=
'uncorrected',alpha=0.6)
235 plt.title(
"Energy distribution")
236 plt.legend(loc=
'upper right')
237 plt.savefig(modv+
'_energy_distributions_lowWinter.png')
241 preds = preds.reshape(preds.shape[0])
248 plt.hist(
abs(uncorrected -targets)/targets*100, bins =100, range=(0,100),label=
'uncorrected',alpha=0.6)
250 plt.hist(100*
abs(preds -targets)/targets, bins =100, range=(0,100),label=
'PU correction',alpha=0.6)
252 plt.title(
"error distribution")
253 plt.legend(loc=
'upper right')
254 plt.savefig(modv+
'_errors_low.png')
262 plt.scatter(targets, uncorrected,alpha=0.3,label=
'uncorr')
263 plt.scatter(targets, preds,alpha=0.3,label=
'PUcorr')
264 plt.xlabel(
'True Values ')
265 plt.ylabel(
'Predictions ')
266 plt.legend(loc=
'upper right')
270 _ = plt.plot(lims, lims)
271 plt.savefig(modv+
'_energyscatt_lowWinter.png')
281 plt.hist(targets/pmom, bins =100, range=(0,5),label=
'E/p noPU',alpha=0.6)
284 plt.hist(marinascorr/pmom, bins =100, range=(0,5),label=
'E/p marina corr',alpha=0.6)
287 plt.legend(loc=
'upper right')
289 plt.title(
"E/p distributions")
290 plt.savefig(modv+
'_eopdist_lowWinter.png')
300 if not os.path.exists(
'models'):
301 os.makedirs(
'models')
302 model.save(
'models/model_BarrelWinter'+modv+
'.h5')
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Abs< T >::type abs(const T &t)
def fac0(jeta)
marinas correction form