20
20
global_start = time .time ()
21
21
cpu_global_start = time .process_time ()
22
22
import numpy as np
23
+ from scipy import optimize as opti
23
24
import tables
24
25
import pandas as pd
25
26
from tqdm import tqdm
@@ -78,15 +79,17 @@ def Read_Data(startentry, endentry):
78
79
Device = int (Device )
79
80
device = torch .device (Device )
80
81
nets = dict ([])
81
- alphas = dict ([])
82
82
for channelid in tqdm (channelid_set , desc = 'Loading Nets of each channel' ) :
83
83
nets [channelid ] = torch .load (NetDir + '/Channel{:02d}.torch_net' .format (channelid ), map_location = device )
84
- alphas [channelid ] = torch .load (NetDir + '/alpha_Channel{:02d}.torch_net' .format (channelid ), map_location = device )
85
84
print ('Net Loaded, consuming {0:.02f}s' .format (time .time () - tic ))
86
85
87
86
filter_limit = 0.05
88
87
Timeline = np .arange (WindowSize ).reshape (1 , WindowSize )
89
88
89
+ p = spe_pre [0 ]['parameters' ]
90
+ t_auto = np .arange (WindowSize ).reshape (WindowSize , 1 ) - np .arange (WindowSize ).reshape (1 , WindowSize )
91
+ mnecpu = wff .spe ((t_auto + np .abs (t_auto )) / 2 , p [0 ], p [1 ], p [2 ])
92
+
90
93
def Forward (channelid ):
91
94
Data_of_this_channel = Channel_Grouped_Waveform .get_group (channelid )
92
95
Shifted_Wave = np .vstack (Data_of_this_channel ['Waveform' ])
@@ -97,13 +100,16 @@ def Forward(channelid):
97
100
slices = np .append (np .arange (0 , len (Shifted_Wave ), BATCHSIZE ), len (Shifted_Wave ))
98
101
for i in range (len (slices ) - 1 ):
99
102
inputs = Shifted_Wave [slices [i ]:slices [i + 1 ]]
100
- Total = np .abs (np .sum (inputs , axis = 1 ))
101
- Total = np .clip (Total , 1e-4 , np .inf )
102
- Prediction = nets [channelid ].forward (torch .from_numpy (inputs ).to (device = device )).data
103
- Prediction = alphas [channelid ].forward (Prediction ).data .cpu ().numpy ()
104
- sumPrediction = np .sum (Prediction , axis = 1 )
105
- sumPrediction = np .clip (sumPrediction , 1e-4 , np .inf )
106
- # After alpha implementation, this line will be commented
103
+ Prediction = nets [channelid ].forward (torch .from_numpy (inputs ).to (device = device )).data .cpu ().numpy ()
104
+ # Total = np.clip(np.abs(inputs.sum(axis=1)) / wff.gmu, 1e-6 / wff.gmu, np.inf)
105
+ Total = Prediction .sum (axis = 1 )
106
+
107
+ Alpha = np .empty (slices [i + 1 ] - slices [i ])
108
+ for j in range (slices [i + 1 ] - slices [i ]):
109
+ Alpha [j ] = opti .fmin_l_bfgs_b (lambda alpha : wff .rss_alpha (alpha , Prediction [j ], inputs [j ], mnecpu ), x0 = [0.01 ], approx_grad = True , bounds = [[1e-20 , np .inf ]], maxfun = 50000 )[0 ]
110
+ Total = Total * Alpha
111
+
112
+ sumPrediction = np .clip (Prediction .sum (axis = 1 ), 1e-10 , np .inf )
107
113
Prediction = Prediction / sumPrediction [:, None ] * Total [:, None ]
108
114
HitPosInWindow = Prediction > filter_limit
109
115
pe_numbers = HitPosInWindow .sum (axis = 1 )
@@ -114,9 +120,8 @@ def Forward(channelid):
114
120
HitPosInWindow [no_pe_found , guessed_risetime ] = True
115
121
Prediction [no_pe_found , guessed_risetime ] = 1
116
122
pe_numbers [no_pe_found ] = 1
117
- Prediction = np .where (Prediction > filter_limit , Prediction , 0 )
118
- # After alpha implementation, this line will be commented
119
- Prediction = Prediction / np .sum (Prediction , axis = 1 )[:, None ] * Total [:, None ]
123
+ sumPrediction = Prediction .sum (axis = 1 )
124
+ Prediction = Prediction / sumPrediction [:, None ] * Total [:, None ]
120
125
Prediction = Prediction [HitPosInWindow ] * wff .gmu
121
126
PEmeasure = np .append (PEmeasure , Prediction )
122
127
TimeMatrix = np .repeat (Timeline , len (HitPosInWindow ), axis = 0 )[HitPosInWindow ]
@@ -129,7 +134,7 @@ def Forward(channelid):
129
134
tic = time .time ()
130
135
cpu_tic = time .process_time ()
131
136
Result = []
132
- for ch in tqdm (channelid_set , desc = 'Predict for each channel' ) :
137
+ for ch in tqdm (channelid_set , desc = 'Predict for each channel' ):
133
138
Result .append (Forward (ch ))
134
139
Result = pd .concat (Result )
135
140
Result = Result .sort_values (by = ['TriggerNo' , 'ChannelID' ])
0 commit comments