26.01514535, 24.01144984, 23.92032753, 25.36948057, 24.36123342, 22.11293807 ], [1.20445654, 1.17307301, 1.21097483, 0.98557953, 1.03284394, 1.16066175], [ 0.06602095, 0.06831482, 0.10287761, 0.09569007, 0.09342742, 0.09662029 ], 1.07043855e-08, 16.1565145, 32.96391684) p = minimize(L, rec_to_p(rec), method='Nelder-Mead', options={ 'disp': True, 'maxfev': 100000 }) rec = p_to_rec(p.x) m = Model(rec['NQ'][0], rec['T'][0], rec['R'][0], rec['F'][0], rec['Tf'][0], rec['Ts'][0], rec['St'][0]) s = Sim(rec['NQ'][0], rec['T'][0], 5, rec['R'][0], rec['F'][0], rec['Tf'][0], rec['Ts'][0], rec['St'][0]) x = np.arange(1000) / 5 fig, ax = plt.subplots(2, 3) for i in range(len(pmts)): np.ravel(ax)[i].plot(x, np.sum(H[:, :, i].T * np.arange(np.shape(H)[0]), axis=1), 'ko', label='Data - PMT{}'.format(pmts[i])) np.ravel(ax)[i].plot( x, np.sum(H[:, 0, i]) * np.sum(m[:, :, i].T * np.arange(np.shape(H)[0]), axis=1),
def L(p): rec = p_to_rec(p) global counter counter += 1 names = ['NQ', 'F', 'Tf', 'Ts', 'T', 'a_spectra', 'R'] for name in names: if np.any(rec[name] < 0): return 1e10 * (1 - np.amin(rec[name])) names = ['F', 'R'] for name in names: if np.any(rec[name] > 1): return 1e10 * (np.amax(rec[name])) if rec['Ts'][0] > 100: return 1e10 * rec['Ts'][0] if rec['Ts'][0] < rec['Tf'][0]: return 1e10 * (rec['Tf'][0] - rec['Ts'][0]) if np.any(rec['St'][0] < 0.5): return 1e10 * (1 + np.abs(np.amin(rec['St'][0]))) # if np.any(rec['Tf'][0]<1): # return 1e10*(1+np.abs(np.amin(rec['Tf'][0]))) l = 0 m = Model(rec['NQ'][0], rec['T'][0], rec['R'][0], rec['F'][0], rec['Tf'][0], rec['Ts'][0], rec['St'][0]) for i in range(len(pmts)): model = np.sum(H[:, 0, i]) * np.ravel(m[:, :500, i]) if np.any(np.isnan(model)) or np.any(np.isinf(model)): print('model is nan or inf') print('NQ=', rec['NQ'][0, i], 'T=', rec['T'][0, i], 'R=', rec['R'][0], 'F=', rec['F'][0], 'Tf=', rec['Tf'][0], 'Ts=', rec['Ts'][0], 'St=', rec['St'][0, i]) plt.figure() plt.plot(np.mean(t.T * np.arange(np.shape(t)[0])), 'k.') plt.show() sys.exit() data = np.ravel(H[:, :500, i]) L = len(model) for j in range(L): if model[j] > 0 and data[j] <= 0: l -= model[j] - data[j] elif model[j] <= 0 and data[j] > 0: return 1e10 * (data[j] - model[j]) elif model[j] == 0 and data[j] == 0: l += 1 else: l += (data[j] * np.log(model[j]) - data[j] * np.log(data[j]) + data[j] - model[j]) spectra_rng = np.nonzero( np.logical_and(PEs > PEs[np.argmax(spectra[i])] - 10, PEs < PEs[np.argmax(spectra[i])] + 10))[0] model = rec['a_spectra'][0, i] * poisson.pmf( PEs, np.sum(m[:, :, i].T * np.arange(np.shape(H)[0])))[spectra_rng] data = spectra[i][spectra_rng] L = len(model) for j in range(L): if model[j] > 0 and data[j] <= 0: l -= model[j] - data[j] elif model[j] <= 0 and data[j] > 0: return 1e10 * (data[j] - model[j]) elif model[j] == 0 and data[j] == 0: l += 1 else: l += (data[j] * np.log(model[j]) - data[j] * np.log(data[j]) + data[j] - model[j]) # for i in range(len(pmts)-1): # for j in range(i+1, len(pmts)): # x=delays[names=='{}_{}'.format(pmts[i], pmts[j])] # rng=np.nonzero(np.logical_and(x>x[np.argmax(data)]-5, x<x[np.argmax(data)]+5))[0] # data=delay_hs[names=='{}_{}'.format(pmts[i], pmts[j])] # model=(x[1]-x[0])*np.sum(data)*np.exp(-0.5*(x[rng]-rec['T'][0,j]+rec['T'][0,i])**2/(rec['St'][0,i]**2+rec['St'][0,j]**2))/np.sqrt(2*np.pi*(rec['St'][0,i]**2+rec['St'][0,j]**2)) # data=data[rng] # L=len(model) # for j in range(L): # if model[j]>0 and data[j]<=0: # l-=model[j]-data[j] # elif model[j]<=0 and data[j]>0: # return 1e10*(data[j]-model[j]) # elif model[j]==0 and data[j]==0: # l+=1 # else: # l+=(data[j]*np.log(model[j])-data[j]*np.log(data[j])+data[j]-model[j]) if counter % (len(p) + 1) == 0: print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!') print('iteration=', int(counter / (len(p) + 1)), 'fanc=', -l) print('--------------------------------') print(rec) return -l
# rec[0]=([44.10656898, 44.16785627], [2.40187361, 2.05004474], [0.88833944, 1.11088245], [0.04048149, 0.04177063], [5.64966695e-05, 2.44157508e-05], 0.66410346, 31.87824534, 367.91330633, 460.48414159) rec[0] = ([34.27791569, 34.18452497], [37.55977339, 37.43415619], [0.60967213, 0.87370292], 0.03910226, 1.59669644, 36.68629443) p = minimize(L, rec_to_p(rec), method='Nelder-Mead', options={ 'disp': True, 'maxfev': 100000 }) rec = p_to_rec(p.x) P = np.dstack((np.identity(100), np.identity(100))) m = Model(rec['NQ'][0], rec['T'][0], [0, 0], rec['F'][0], rec['Tf'][0], rec['Ts'][0], rec['St'][0], [0, 0], P) s = Sim(rec['NQ'][0], rec['T'][0], 5, [0, 0], rec['F'][0], rec['Tf'][0], rec['Ts'][0], rec['St'][0], [0, 0], [0, 0], [1e-6, 1e-6], [1e-6, 1e-6], [0, 0]) x = np.arange(1000) / 5 fig, ((ax1, ax3), (ax2, ax4)) = plt.subplots(2, 2) ax1.plot(x[:30 * 5], np.sum(H[:, :, 0].T * np.arange(np.shape(H)[0]), axis=1)[:30 * 5], 'ko', label='Data - PMT7') ax1.plot(x[:30 * 5], np.sum(H[:, 0, 0]) * np.sum(m[:, :, 0].T * np.arange(np.shape(H)[0]), axis=1)[:30 * 5],
def L(p): rec = p_to_rec(p) global counter counter += 1 names = ['NQ', 'F', 'Tf', 'Ts', 'T'] for name in names: if np.any(rec[name] < 0): return 1e10 * (1 - np.amin(rec[name])) names = ['F'] for name in names: if np.any(rec[name] > 1): return 1e10 * (np.amax(rec[name])) if rec['Ts'][0] > 100: return 1e10 * rec['Ts'][0] if rec['Ts'][0] < rec['Tf'][0]: return 1e10 * (rec['Tf'][0] - rec['Ts'][0]) if np.any(rec['St'][0] < 0.2): return 1e10 * (1 + np.abs(np.amin(rec['St'][0]))) l = 0 # P=make_P(rec['a0'][0], rec['Spad'][0], rec['Spe'][0], rec['m_pad'][0]) P = np.dstack((np.identity(100), np.identity(100))) m = Model(rec['NQ'][0], rec['T'][0], [0, 0], rec['F'][0], rec['Tf'][0], rec['Ts'][0], rec['St'][0], [0, 0], P) #m_area=model_area(areas, rec['m_pad'][0], rec['a_pad'][0], rec['a_spe'][0], rec['a_dpe'][0], rec['a_trpe'][0], rec['Spad'][0], rec['Spe'][0]) for i in range(len(pmts)): model = np.sum(H[:, 0, i]) * np.ravel(m[:, :500, i]) if np.any(np.isnan(model)) or np.any(np.isinf(model)): print('model is nan or inf') print('NQ=', rec['NQ'][0, i], 'T=', rec['T'][0, i], 'F=', rec['F'][0, i], 'Tf=', rec['Tf'][0, i], 'Ts=', rec['Ts'][0, i], 'St=', rec['St'][0, i]) plt.figure() plt.plot(np.mean(t.T * np.arange(np.shape(t)[0])), 'k.') plt.show() sys.exit() data = np.ravel(H[:, :500, i]) L = len(model) for j in range(L): if model[j] > 0 and data[j] <= 0: l -= model[j] - data[j] elif model[j] <= 0 and data[j] > 0: return 1e10 * (data[j] - model[j]) elif model[j] == 0 and data[j] == 0: l += 1 else: l += data[j] * np.log(model[j]) - data[j] * np.log( data[j]) + data[j] - model[j] # model=np.sum(h_q0[i])*q0_model(n_q0, rec['q0'][0,i]) # data=h_q0[i] # L=len(model) # for j in range(L): # if model[j]>0 and data[j]<=0: # l-=model[j]-data[j] # elif model[j]<=0 and data[j]>0: # return 1e10*(data[j]-model[j]) # elif model[j]==0 and data[j]==0: # l+=1 # else: # l+=data[j]*np.log(model[j])-data[j]*np.log(data[j])+data[j]-model[j] # # model=rec['a_spec'][0,i]*poisson.pmf(spec_PEs, np.sum(m[:,:,i].T*np.arange(np.shape(H)[0]))) # data=spectrum[spec_PEs, i] # L=len(model) # for j in range(L): # if model[j]>0 and data[j]<=0: # l-=model[j]-data[j] # elif model[j]<=0 and data[j]>0: # return 1e10*(data[j]-model[j]) # elif model[j]==0 and data[j]==0: # l+=1 # else: # l+=data[j]*np.log(model[j])-data[j]*np.log(data[j])+data[j]-model[j] # # model=m_area[i] # data=H_areas[i] # L=len(model) # for j in range(L): # if model[j]>0 and data[j]<=0: # l-=model[j]-data[j] # elif model[j]<=0 and data[j]>0: # return 1e10*(data[j]-model[j]) # elif model[j]==0 and data[j]==0: # l+=1 # else: # l+=data[j]*np.log(model[j])-data[j]*np.log(data[j])+data[j]-model[j] # # model=rec['a_delay'][0]*np.exp(-0.5*(delays[rng_delay]-rec['T'][0,1]+rec['T'][0,0])**2/(rec['St'][0,0]**2+rec['St'][0,1]**2))/np.sqrt(2*np.pi*(rec['St'][0,0]**2+rec['St'][0,1]**2)) # data=delay_h[rng_delay] # # L=len(model) # for j in range(L): # if model[j]>0 and data[j]<=0: # l-=model[j]-data[j] # elif model[j]<=0 and data[j]>0: # return 1e10*(data[j]-model[j]) # elif model[j]==0 and data[j]==0: # l+=1 # else: # l+=data[j]*np.log(model[j])-data[j]*np.log(data[j])+data[j]-model[j] if counter % (len(p) + 1) == 0: print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!') print('iteration=', int(counter / (len(p) + 1)), 'fanc=', -l) print('--------------------------------') print(rec) return -l
l+=(data[j]*np.log(model[j])-data[j]*np.log(data[j])+data[j]-model[j]) if counter%(len(p)+1)==0: print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!') print('iteration=', int(counter/(len(p)+1)), 'fanc=',-l) print('--------------------------------') print(rec) return -l rec[0]=([36.65361232, 27.0674887 , 21.58765364, 35.26543114, 34.68101963, 56.55811891], [20318.89804123, 29398.22812292, 54050.97911597, 37386.39722335, 16245.51120957, 25676.63138817], [23.90532686, 23.97120507, 23.72761649, 23.46118973, 23.4272549 , 23.73023182], [1.25507068, 1.4145993 , 1.24708723, 0.55778022, 0.86542921, 1.21696164], 0.11440806, 4.51577519, 35.28739035) print(L(rec_to_p(rec))) # p=minimize(L, rec_to_p(rec), method='Nelder-Mead', options={'disp':True, 'maxfev':100000}) # rec=p_to_rec(p.x) m=Model(rec['NQ'][0], rec['T'][0], np.zeros(len(pmts)), rec['F'][0], rec['Tf'][0], rec['Ts'][0], rec['St'][0]) s=Sim(rec['NQ'][0], rec['T'][0], 5, np.zeros(len(pmts)), rec['F'][0], rec['Tf'][0], rec['Ts'][0], rec['St'][0]) x=np.arange(1000)/5 fig, ax=plt.subplots(2,3) for i in range(len(pmts)): np.ravel(ax)[i].plot(x, np.sum(H[:,:,i].T*np.arange(np.shape(H)[0]), axis=1), 'ko', label='Data - PMT{}'.format(pmts[i])) np.ravel(ax)[i].plot(x, np.sum(H[:,0,i])*np.sum(m[:,:,i].T*np.arange(np.shape(H)[0]), axis=1), 'r.-', label='2 exp model', linewidth=3) np.ravel(ax)[i].plot(x, np.sum(H[:,0,i])*np.sum(s[:,:,i].T*np.arange(np.shape(H)[0]), axis=1), 'g.-', label='2 exp model', linewidth=3) np.ravel(ax)[i].legend(fontsize=15) np.ravel(ax)[i].set_xlabel('Time [ns]', fontsize='15') fig.text(0.04, 0.5, r'$N_{events}\sum_n nH_{ni}$', va='center', rotation='vertical', fontsize=15) fig, ax=plt.subplots(2,3) for i in range(len(pmts)): spectra_rng=np.nonzero(np.logical_and(PEs>PEs[np.argmax(spectra[i])]-10, PEs<PEs[np.argmax(spectra[i])]+10))[0] model=rec['a_spectra'][0,i]*poisson.pmf(PEs, np.sum(m[:,:,i].T*np.arange(np.shape(H)[0])))[spectra_rng]
rec[0] = ([ 35.49302268, 26.17497766, 21.34892126, 34.11857516, 34.25089177, 55.35067421 ], [ 24.18066808, 24.14013932, 24.37615609, 24.31612566, 24.51001521, 24.57748248 ], [1.0884017, 0.88510012, 0.99856744, 1.05147662, 1.09080665, 1.14670428 ], [0.06125483, 0.05278206, 0.0909597, 0.09726535, 0.09490963, 0.08954461], 32.79469072) # print(L(rec_to_p(rec))) # p=minimize(L, rec_to_p(rec), method='Nelder-Mead', options={'disp':True, 'maxfev':100000}) # rec=p_to_rec(p.x) m = Model(rec['NQ'][0], rec['T'][0], rec['R'][0], 0, 1000, rec['Ts'][0], rec['St'][0]) s, GS, G_spec = Sim(rec['NQ'][0], rec['T'][0], 1e-6, rec['R'][0], 0, 1000, rec['Ts'][0], rec['St'][0]) x = np.arange(200) fig, ax = plt.subplots(2, 3) for i in range(len(pmts)): np.ravel(ax)[i].plot(x, np.sum(H[:, :, i].T * np.arange(np.shape(H)[0]), axis=1), 'ko', label='Data - PMT{}'.format(pmts[i])) np.ravel(ax)[i].plot( x, np.sum(H[:, 0, i]) * np.sum(m[:, :, i].T * np.arange(np.shape(m)[0]), axis=1),
def L(p): rec = p_to_rec(p) global counter counter += 1 nams = ['NQ', 'Ts', 'T', 'F', 'Tf', 'Ts'] for name in nams: if np.any(rec[name] < 0): return 1e10 * (1 - np.amin(rec[name])) nams = ['F'] for name in nams: if np.any(rec[name] > 1): return 1e10 * (np.amax(rec[name])) if rec['Ts'][0] > 100: return 1e10 * rec['Ts'][0] if np.any(rec['St'][0] < 0.5): return 1e10 * (1 + np.abs(np.amin(rec['St'][0]))) # if np.any(rec['Tf'][0]<1): # return 1e10*(1+np.abs(np.amin(rec['Tf'][0]))) l = 0 m = Model(rec['NQ'][0], rec['T'][0], np.zeros(len(pmts)), rec['F'][0], rec['Tf'][0], rec['Ts'][0], rec['St'][0]) for i in range(len(pmts)): model = np.sum(H[:, 0, i]) * np.ravel(m[:, :100, i]) if np.any(np.isnan(model)) or np.any(np.isinf(model)): print('model is nan or inf') print('NQ=', rec['NQ'][0, i], 'T=', rec['T'][0, i], 'R=', 0, 'Ts=', 'F=', rec['F'][0], 'Tf=', rec['Tf'][0], 'Ts=', rec['Ts'][0], 'St=', rec['St'][0, i]) data = np.ravel(H[:, :100, i]) L = len(model) for j in range(L): if model[j] > 0 and data[j] <= 0: l -= model[j] - data[j] elif model[j] <= 0 and data[j] > 0: return 1e10 * (data[j] - model[j]) elif model[j] == 0 and data[j] == 0: l += 1 else: l += (data[j] * np.log(model[j]) - data[j] * np.log(data[j]) + data[j] - model[j]) spectra_rng = np.nonzero( np.logical_and(PEs > PEs[np.argmax(spectra[i])] - 20, PEs < PEs[np.argmax(spectra[i])] + 20))[0] model = poisson.pmf(PEs, np.sum(m[:, :, i].T * np.arange(np.shape(m)[0])))[spectra_rng] data = spectra[i][spectra_rng] model = model / np.amax(model) * np.amax(data) L = len(model) for j in range(L): if model[j] > 0 and data[j] <= 0: l -= model[j] - data[j] elif model[j] <= 0 and data[j] > 0: return 1e10 * (data[j] - model[j]) elif model[j] == 0 and data[j] == 0: l += 1 else: l += (data[j] * np.log(model[j]) - data[j] * np.log(data[j]) + data[j] - model[j]) for i in range(len(pmts) - 1): for j in range(i + 1, len(pmts)): x = delays[names == '{}_{}'.format(pmts[i], pmts[j])] rng = np.nonzero( np.logical_and(x > x[np.argmax(data)] - 3, x < x[np.argmax(data)] + 3))[0] data = delay_hs[names == '{}_{}'.format(pmts[i], pmts[j])] model = (x[1] - x[0]) * np.exp( -0.5 * (x[rng] - rec['T'][0, j] + rec['T'][0, i])**2 / (rec['St'][0, i]**2 + rec['St'][0, j]**2)) / np.sqrt( 2 * np.pi * (rec['St'][0, i]**2 + rec['St'][0, j]**2)) model = model / np.amax(model) * np.amax(data) data = data[rng] L = len(model) for j in range(L): if model[j] > 0 and data[j] <= 0: l -= model[j] - data[j] elif model[j] <= 0 and data[j] > 0: return 1e10 * (data[j] - model[j]) elif model[j] == 0 and data[j] == 0: l += 1 else: l += (data[j] * np.log(model[j]) - data[j] * np.log(data[j]) + data[j] - model[j]) if counter % (len(p) + 1) == 0: print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!') print('iteration=', int(counter / (len(p) + 1)), 'fanc=', -l) print('--------------------------------') print(rec) return -l
rec[0] = ([33.95208843, 34.56622498], [37.81719022, 37.52927457], [0.41393811, 0.61785414], [4.54280123e-05, 4.63532744e-05], [1.41784125e-03, 2.48391983e-06], [76503.34258707, 80957.52515861], [14998.18651635, 11345.76354315], [1324.48379597, 1032.34423508], [407.41079156, 30.88461516], [-0.06053189, -0.00021648], [0.25647794, 0.22882734], [4.58697010e-03, 2.72860268e-08], 34.04362439, 354.02590438) # p=minimize(L, rec_to_p(rec), method='Nelder-Mead', options={'disp':True, 'maxfev':100000}) # rec=p_to_rec(p.x) print(L(rec_to_p(rec))) P = make_P(rec['a0'][0], rec['Spad'][0], rec['Spe'][0], rec['m_pad'][0]) m = Model(rec['NQ'][0], rec['T'][0], [0, 0], 0, 1, rec['Ts'][0], rec['St'][0], rec['q0'][0], P) m_area = model_area(areas, rec['m_pad'][0], rec['a_pad'][0], rec['a_spe'][0], rec['a_dpe'][0], rec['a_trpe'][0], rec['Spad'][0], rec['Spe'][0]) # s=Sim(rec['NQ'][0], rec['T'][0], 5, [0,0], rec['F'][0], rec['Tf'][0], rec['Ts'][0], rec['St'][0], rec['q0'][0], rec['a0'][0], rec['Spad'][0], rec['Spe'][0], rec['m_pad'][0]) s = m x = np.arange(1000) / 5 fig, ((ax1, ax3), (ax2, ax4)) = plt.subplots(2, 2) ax1.plot(x[:30 * 5], np.sum(H[:, :, 0].T * np.arange(np.shape(H)[0]), axis=1)[:30 * 5], 'ko', label='Data - PMT7') ax1.plot(x[:30 * 5], np.sum(H[:, 0, 0]) *
else: print('f**k') sys.exit() return rec rec1[0] = ([34.17255625, 35.09148282], [39.65578361, 39.30607808], [0.98964095, 1.08379849], [0, 0], 0.07063164, 0.21911959, 36.64258893, 463.23111438) rec2[0] = ([34.1231953, 35.08659966], [38.19114816, 37.94501817], [0.97305945, 1.11166723], [0.0443506, 0.05271929], 0.02677325, 0.21487918, 36.61184292, 466.39540394) P = np.dstack((np.identity(100), np.identity(100))) m1 = Model(rec1['NQ'][0], rec1['T'][0], rec1['R'][0], rec1['F'][0], rec1['Tf'][0], rec1['Ts'][0], rec1['St'][0], [0, 0], P) s1 = Sim(rec1['NQ'][0], rec1['T'][0], 5, rec1['R'][0], rec1['F'][0], rec1['Tf'][0], rec1['Ts'][0], rec1['St'][0], [0, 0], [0, 0], [1e-6, 1e-6], [1e-6, 1e-6], [0, 0]) m2 = Model(rec2['NQ'][0], rec2['T'][0], rec2['R'][0], rec2['F'][0], rec2['Tf'][0], rec2['Ts'][0], rec2['St'][0], [0, 0], P) s2 = Sim(rec2['NQ'][0], rec2['T'][0], 5, rec2['R'][0], rec2['F'][0], rec2['Tf'][0], rec2['Ts'][0], rec2['St'][0], [0, 0], [0, 0], [1e-6, 1e-6], [1e-6, 1e-6], [0, 0]) x = np.arange(200) / 5 fig, ((ax1, ax3), (ax2, ax4)) = plt.subplots(2, 2, sharex=True) ax1.title.set_text('Double Exp')