2 from root_numpy
import hist2array
6 from os.path
import isfile, join
11 dlist = [f
for f
in listdir(folder)
if key
in f]
14 flist = [f
for f
in listdir(folder +
'/' + dirname)
if '_y.npy' in f]
16 d = np.load(folder +
'/' + dirname +
'/' + fname)
21 dlist = [f
for f
in listdir(folder)
if 'training' in f]
22 flist = [f
for f
in listdir(folder +
'/' + dlist[0])
if '_x.npy' in f]
23 d = np.load(folder +
'/' + dlist[0] +
'/' + flist[0])
24 return d.shape[1], d.shape[2]
28 cum = np.cumsum(np.sum(A, axis=0))
29 start_ind = np.max([0, np.where(cum > cum[-1]*0.005)[0][0] - drift_margin])
30 end_ind = np.min([A.shape[1], np.where(cum > cum[-1]*0.995)[0][0] + drift_margin])
31 return start_ind, end_ind
33 def get_data(folder, fname, drift_margin = 0, crop = True, blur = None, white_noise = 0, coherent_noise = 0):
34 print 'Reading', fname
36 if isinstance(folder, TFile):
37 A_raw = hist2array(folder.Get(fname +
'_raw'))
38 A_deposit = hist2array(folder.Get(fname +
'_deposit'))
39 A_pdg = hist2array(folder.Get(fname +
'_pdg'))
41 A_raw = np.genfromtxt(folder+
'/'+fname +
'.raw', delimiter=
' ', dtype=np.float32)
42 A_deposit = np.genfromtxt(folder+
'/'+fname +
'.deposit', delimiter=
' ', dtype=np.float32)
43 A_pdg = np.genfromtxt(folder+
'/'+fname +
'.pdg', delimiter=
' ', dtype=np.int32)
45 print 'Bad event, return empty arrays' 46 return None,
None,
None,
None,
None 48 if A_raw.shape[0] < 8
or A_raw.shape[1] < 8:
return None,
None,
None,
None,
None 50 test_pdg = np.sum(A_pdg)
51 test_dep = np.sum(A_deposit)
52 test_raw = np.sum(A_raw)
53 if test_raw == 0.0
or test_dep == 0.0
or test_pdg == 0:
return None,
None,
None,
None,
None 55 print test_raw, test_dep, test_pdg
60 A_raw = A_raw[:,evt_start_ind:evt_stop_ind]
61 A_deposit = A_deposit[:,evt_start_ind:evt_stop_ind]
62 A_pdg = A_pdg[:,evt_start_ind:evt_stop_ind]
65 evt_stop_ind = A_raw.shape[1]
66 print evt_start_ind, evt_stop_ind
72 deposit_th_ind = A_deposit < 2.0e-5
73 A_pdg[deposit_th_ind] = 0
75 showers = A_pdg.copy()
76 tracks[(A_pdg & 0x0FFF) == 11] = 0
77 tracks[tracks > 0] = 1
78 showers[(A_pdg & 0x0FFF) != 11] = 0
79 showers[showers > 0] = 1
80 return A_raw, A_deposit, A_pdg, tracks, showers
85 if kernel
is None or kernel.shape[0] < 2:
return a
87 margin_left = kernel.shape[0] >> 1
88 margin_right = kernel.shape[0] - margin_left - 1
90 for w
in range(margin_left, a.shape[0] - margin_right):
91 for d
in range(a.shape[1]):
93 for i
in range(kernel.shape[0]):
94 s += kernel[i] * src[w + i - margin_left, d]
101 if sigma
is None or sigma == 0:
return a
103 a += np.random.normal(0, sigma, a.shape)
110 if sigma
is None or sigma == 0:
return a
112 a += np.random.normal(0, sigma, a.shape)
114 amps1 = np.random.normal(1, 0.1, a.shape[0]);
115 amps2 = np.random.normal(1, 0.1, 1 + (a.shape[0] >> 5));
118 for w
in range(a.shape[0]):
120 noise = np.random.normal(0, sigma, a.shape[1])
121 group_amp = amps2[w >> 5]
122 a[w] += group_amp * amps1[w] * noise
128 halfSizeW = wsize / 2;
129 halfSizeD = dsize / 2;
131 w0 = wire - halfSizeW;
132 w1 = wire + halfSizeW;
134 d0 = drift - halfSizeD;
135 d1 = drift + halfSizeD;
137 patch = np.zeros((wsize, dsize), dtype=np.float32)
140 for w
in range(w0, w1):
141 if w >= 0
and w < a.shape[0]:
143 for d
in range(d0, d1):
144 if d >= 0
and d < a.shape[1]:
145 patch[wpatch,dpatch] = a[w,d];
152 max_count = A.shape[0]*A.shape[1] / 4
153 vtx = np.zeros((max_count, 3), dtype=np.int32)
155 for i
in range(A.shape[0]):
156 for j
in range(A.shape[1]):
157 if nvtx >= max_count:
break 158 if (A[i,j] & 0xFF000000) > 0:
170 vtx = np.zeros((max_count, 3), dtype=np.int32)
172 for i
in range(A.shape[0]):
173 for j
in range(A.shape[1]):
174 if nvtx >= max_count:
break 175 if (A[i,j] & 0xFF0000) > 0:
176 t = (A[i,j] >> 16) & 0xFF
186 assert len(a) == len(b)
187 rng_state = np.random.get_state()
189 np.random.set_state(rng_state)
194 with
open(cfgname,
'r') as fin: 195 config = json.loads(fin.read()); 197 print 'This script requires configuration file: config.json' def get_event_bounds(A, drift_margin=0)
int open(const char *, int)
Opens a file descriptor.
def addWhiteNoise(a, sigma)
def count_events(folder, key)
def get_patch(a, wire, drift, wsize, dsize)
def addCoherentNoise(a, sigma)
def shuffle_in_place(a, b)
def get_patch_size(folder)
def get_data(folder, fname, drift_margin=0, crop=True, blur=None, white_noise=0, coherent_noise=0)