def main(argv): plt.ion() runRange = (13420,13429) channel = 626 aeCutVal = 0.01425 tempGuess = 81 fitSamples = 200 numWaveforms = 10 #Prepare detector num = [3.64e+09, 1.88e+17, 6.05e+15] den = [1, 4.03e+07, 5.14e+14, 7.15e+18] system = signal.lti(num, den) gradGuess = 0.04 pcRadGuess = 2.25 #Create a detector model detName = "conf/P42574A_grad%0.2f_pcrad%0.2f.conf" % (gradGuess,pcRadGuess) det = Detector(detName, temperature=tempGuess, timeStep=1., numSteps=fitSamples*10, tfSystem=system) det.LoadFields("P42574A_fields.npz") wfFileName = "P42574A_%dwaveforms.npz" % numWaveforms if os.path.isfile(wfFileName): data = np.load(wfFileName) r_arr = data['r_arr'] phi_arr = data['phi_arr'] z_arr = data['z_arr'] scale_arr = data['scale_arr'] t0_arr = data['t0_arr'] wfs = data['wfs'] else: print "No saved waveforms available. Loading from Data" #get waveforms cut = "trapECal>%f && trapECal<%f && TSCurrent100nsMax/trapECal > %f" % (1588,1594, aeCutVal) wfs = helpers.GetWaveforms(runRange, channel, numWaveforms, cut) #prep holders for each wf-specific param r_arr = np.empty(numWaveforms) phi_arr = np.empty(numWaveforms) z_arr = np.empty(numWaveforms) scale_arr = np.empty(numWaveforms) t0_arr = np.empty(numWaveforms) #ML as a start, using each individual wf nll_wf = lambda *args: -lnlike_waveform(*args) for (idx,wf) in enumerate(wfs): print "Doing MLE for waveform %d" % idx wf.WindowWaveformTimepoint(fallPercentage=.99) startGuess = [15., np.pi/8, 15., wf.wfMax, wf.t0Guess] result = op.minimize(nll_wf, startGuess, args=(wf, det), method="Powell") r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx] = result["x"] np.savez(wfFileName, wfs = wfs, r_arr=r_arr, phi_arr = phi_arr, z_arr = z_arr, scale_arr = scale_arr, t0_arr=t0_arr, ) if True: fig = plt.figure() for (idx,wf) in enumerate(wfs): print "r: %f\nphi %f\nz %f\n e %f\nt0 %f" % (r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx]) ml_wf = det.GetSimWaveform(r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], fitSamples) plt.plot(ml_wf, color="b") plt.plot(wf.windowedWf, color="r") value = raw_input(' --> Press q to quit, any other key to continue\n') # if False: # print "Starting detector MLE..." # detmleFileName = "P42574A_%dwaveforms_detectormle.npz" % numWaveforms # nll_det = lambda *args: -lnlike_detector(*args) # detector_startguess = np.hstack((r_arr[:], phi_arr[:], z_arr[:], scale_arr[:], t0_arr[:], tempGuess, gradGuess,pcRadGuess)) # result = op.minimize(nll_det, detector_startguess, args=(wfs, det), method="Powell") # temp, impGrad, pcRad = result["x"][-3:] # r_arr, phi_arr, z_arr, scale_arr, t0_arr = result["x"][:-3].reshape((5, numWaveforms)) # # print "MLE temp is %f" % temp # print "MLE grad is %f" % impGrad # print "MLE pc rad is %f" % pcRad # # fig = plt.figure() # det.SetTemperature(temp) # det.SetFields(pcRad, impGrad) # for (idx,wf) in enumerate(wfs): # ml_wf = det.GetSimWaveform(r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], fitSamples) # # plt.plot(ml_wf, color="b") # plt.plot(wf.windowedWf, color="r") # # np.savez(detmleFileName, wfs = wfs, r_arr=r_arr, phi_arr = phi_arr, z_arr = z_arr, scale_arr = scale_arr, t0_arr=t0_arr, temp=temp, impGrad=impGrad, pcRad=pcRad) # plt.show() # value = raw_input(' --> Press q to quit, any other key to continue\n') # exit(0) #Do the MCMC ndim = 5*numWaveforms + 3 + 6 nwalkers = ndim * 2 mcmc_startguess = np.hstack((r_arr[:], phi_arr[:], z_arr[:], scale_arr[:], t0_arr[:], tempGuess, gradGuess,pcRadGuess, num[:], den[1:])) tempIdx = -9 gradIdx = -8 pcRadIdx = -7 pos0 = [mcmc_startguess + 1e-2*np.random.randn(ndim)*mcmc_startguess for i in range(nwalkers)] for pos in pos0: # print "radius is %f" % det.detector_radius # print "length is %f" % det.detector_length # pos[:numWaveforms] = np.clip( pos[:numWaveforms], 0, np.floor(det.detector_radius)) pos[numWaveforms:2*numWaveforms] = np.clip(pos[numWaveforms:2*numWaveforms], 0, np.pi/4) pos[2*numWaveforms:3*numWaveforms] = np.clip(pos[2*numWaveforms:3*numWaveforms], 0, det.detector_length) pos[4*numWaveforms:5*numWaveforms] = np.clip(pos[4*numWaveforms:5*numWaveforms], 0, fitSamples) pos[tempIdx] = np.clip(pos[tempIdx], 40, 120) pos[gradIdx] = np.clip(pos[gradIdx], det.gradList[0], det.gradList[-1]) pos[pcRadIdx] = np.clip(pos[pcRadIdx], det.pcRadList[0], det.pcRadList[-1]) # print pos[0:30] prior = lnprior(pos, wfs, det) if not np.isfinite(prior) : print "BAD PRIOR WITH START GUESS YOURE KILLING ME SMALLS" exit(0) sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob, args=(wfs, det), threads=1) # f = open("chain.dat", "w") # f.close() iter, burnIn = 1000, 800 wfPlotNumber = 10 start = timer() #w/ progress bar bar = ProgressBar(widgets=[Percentage(), Bar()], maxval=iter).start() for (idx,result) in enumerate(sampler.sample(pos0, iterations=iter, storechain=True)): bar.update(idx+1) end = timer() bar.finish() print "Elapsed time: " + str(end-start) print "Dumpimng chain to file..." np.save("sampler.npy", sampler.chain) # print "Making MCMC steps figure..." ######### Plots for MC Steps stepsFig = plt.figure(2, figsize=(20, 15)) plt.clf() ax0 = stepsFig.add_subplot(511) ax1 = stepsFig.add_subplot(512, sharex=ax0) ax2 = stepsFig.add_subplot(513, sharex=ax0) ax3 = stepsFig.add_subplot(514, sharex=ax0) ax4 = stepsFig.add_subplot(515, sharex=ax0) ax0.set_ylabel('r') ax1.set_ylabel('phi') ax2.set_ylabel('z') ax3.set_ylabel('scale') ax4.set_ylabel('t0') for i in range(nwalkers): for j in range(wfs.size): ax0.plot(sampler.chain[i,:,0+j], alpha=0.3) # r ax1.plot(sampler.chain[i,:,numWaveforms + j], alpha=0.3) # phi ax2.plot(sampler.chain[i,:,2*numWaveforms + j], alpha=0.3) #z ax3.plot(sampler.chain[i,:,3*numWaveforms + j], alpha=0.3) #energy ax4.plot(sampler.chain[i,:,4*numWaveforms + j], alpha=0.3) #t0 plt.savefig("emcee_wfchain_%dwfs.png" % numWaveforms) stepsFigDet = plt.figure(3, figsize=(20, 15)) plt.clf() ax0 = stepsFigDet.add_subplot(911) ax1 = stepsFigDet.add_subplot(912, sharex=ax0) ax2 = stepsFigDet.add_subplot(913, sharex=ax0) ax3 = stepsFigDet.add_subplot(914, sharex=ax0) ax4 = stepsFigDet.add_subplot(915, sharex=ax0) ax5 = stepsFigDet.add_subplot(916, sharex=ax0) ax6 = stepsFigDet.add_subplot(917, sharex=ax0) ax7 = stepsFigDet.add_subplot(918, sharex=ax0) ax8 = stepsFigDet.add_subplot(919, sharex=ax0) ax0.set_ylabel('temp') ax1.set_ylabel('grad') ax2.set_ylabel('pcRad') ax3.set_ylabel('num1') ax4.set_ylabel('num2') ax5.set_ylabel('num3') ax6.set_ylabel('den1') ax7.set_ylabel('den2') ax8.set_ylabel('den3') for i in range(nwalkers): ax0.plot(sampler.chain[i,:,tempIdx], "b", alpha=0.3) #temp ax1.plot(sampler.chain[i,:,gradIdx], "b", alpha=0.3) #grad ax2.plot(sampler.chain[i,:,pcRadIdx], "b", alpha=0.3) #pcrad ax3.plot(sampler.chain[i,:,-6], "b", alpha=0.3) #temp ax4.plot(sampler.chain[i,:,-5], "b", alpha=0.3) #grad ax5.plot(sampler.chain[i,:,-4], "b", alpha=0.3) #pcrad ax6.plot(sampler.chain[i,:,-3], "b", alpha=0.3) #temp ax7.plot(sampler.chain[i,:,-2], "b", alpha=0.3) #grad ax8.plot(sampler.chain[i,:,-1], "b", alpha=0.3) #pcrad plt.savefig("emcee_detchain_%dwfs.png" % numWaveforms) print "making waveforms figure..." det2 = Detector(detName, temperature=tempGuess, timeStep=1., numSteps=fitSamples*10, tfSystem=system) det2.LoadFields("P42574A_fields.npz") #pull the samples after burn-in samples = sampler.chain[:, burnIn:, :].reshape((-1, ndim)) simWfs = np.empty((wfPlotNumber, numWaveforms, fitSamples)) print "temp is %f" % np.median(samples[:,tempIdx]) print "grad is %f" % np.median(samples[:,gradIdx]) print "pcrad is %f" % np.median(samples[:,pcRadIdx]) print "num1 is %f" % np.median(samples[:,-6]) print "num2 is %f" % np.median(samples[:,-5]) print "num3 is %f" % np.median(samples[:,-4]) print "den1 is %f" % np.median(samples[:,-3]) print "den2 is %f" % np.median(samples[:,-2]) print "den3 is %f" % np.median(samples[:,-1]) for idx, (theta) in enumerate(samples[np.random.randint(len(samples), size=wfPlotNumber)]): temp, impGrad, pcRad = theta[tempIdx], theta[gradIdx], theta[pcRadIdx] r_arr, phi_arr, z_arr, scale_arr, t0_arr = theta[:-9].reshape((5, numWaveforms)) det2.SetTemperature(temp) det2.SetFields(pcRad, impGrad) num = [theta[-6], theta[-5], theta[-4]] den = [1, theta[-3], theta[-2], theta[-1]] det2.SetTransferFunction(num, den) for wf_idx in range(wfs.size): wf_i = det2.GetSimWaveform(r_arr[wf_idx], phi_arr[wf_idx], z_arr[wf_idx], scale_arr[wf_idx], t0_arr[wf_idx], fitSamples) simWfs[idx, wf_idx, :] = wf_i if wf_i is None: print "Waveform %d, %d is None" % (idx, wf_idx) residFig = plt.figure(4, figsize=(20, 15)) helpers.plotManyResidual(simWfs, wfs, figure=residFig) plt.savefig("emcee_waveforms_%dwfs.png" % numWaveforms) plt.show() value = raw_input(' --> Press q to quit, any other key to continue\n') if value == 'q': exit(0)
def main(argv): fitSamples = 200 plt.ion() pzCorrTimeConstant = 72 * 1000. pz = MGWFPoleZeroCorrection() pz.SetDecayConstant(pzCorrTimeConstant) wfFileName = "P42574A_512waveforms_fit_smooth_de.npz" if os.path.isfile(wfFileName): data = np.load(wfFileName) results = data['result_arr'] wfs = data['wfs'] else: print "No saved waveforms available." exit(0) wfFileName = "P42574A_512waveforms_raw.npz" if os.path.isfile(wfFileName): data = np.load(wfFileName) wfs_raw = data['wfs'] wfs_no_bl_sub = data['raw_wfs'] else: print "No raw waveforms available." exit(0) print "There are %d waveforms fit" % wfs.size print "...And i pulled out %d raw ones" % wfs_raw.size det = prepdetector(fitSamples) simWfArr = np.empty((1, len(wfs), fitSamples)) likes = np.empty(len(wfs)) risetimes = np.empty(len(wfs)) r_arr = np.empty(len(wfs)) z_arr = np.empty(len(wfs)) t0_arr = np.empty(len(wfs)) time_since_last_arr = np.empty(len(wfs)) last_energy_arr = np.empty(len(wfs)) f1 = plt.figure(1, figsize=(15, 8)) good_risetimes = [] likeCutoff = 20 simWfArr = np.empty((1, len(wfs), fitSamples)) nll = lambda *args: -lnlike_diffusion(*args) for (idx, wf) in enumerate(wfs): r, phi, z, scale, t0, smooth = results[idx]['x'] simWfArr[0, idx, :] = det.GetSimWaveform(r, phi, z, scale * 100, t0, 200, smoothing=smooth) r_arr[idx], z_arr[idx], t0_arr[idx] = r, z, t0 # simWfArr[0,idx,:] = det.GetSimWaveform(r, phi, z, scale*100., t0, fitSamples, smoothing=smooth, electron_smoothing=esmooth) likes[idx] = results[idx]['fun'] / wf.wfLength risetimes[idx] = findTimePointBeforeMax(wf.windowedWf, 0.99) - wf.t0Guess time_since_last_arr[idx] = wfs_raw[idx].timeSinceLast last_energy_arr[idx] = wfs_raw[idx].energyLast # #cheap PZ correction # mgwf = MGWaveform() # mgwf.SetSamplingPeriod(10*1) # mgwf.SetLength(len(wf.waveformData)) # for i, wfVal in enumerate(wf.waveformData): # mgwf.SetValue(i, wfVal) # mgwfpz = MGWaveform() # pz.TransformOutOfPlace(mgwf, mgwfpz) # waveform = mgwfpz.GetVectorData() # waveform = np.multiply(waveform, 1) # wfToPlot = waveform # alignPoint = findTimePointBeforeMax(wfToPlot, 0.99) wfToPlot = wf.windowedWf if likes[idx] > 60: continue plt.plot(wfToPlot, color="orange") continue if likes[idx] > likeCutoff: continue plt.plot(wfToPlot, color="r") print "run number is wf %d" % wf.runNumber print ">>fit t0 is %f" % t0 simWf = det.GetSimWaveform(r, phi, z, scale * 100, t0, fitSamples, smoothing=smooth) plt.plot(simWf, color="b") # # new_result = op.minimize(nll, [r, phi, z, scale, t0, smooth], args=(det, wf.windowedWf, wf.baselineRMS), method="Powell") # r, phi, z, scale, t0, smooth = new_result["x"] # new_simWf = det.GetSimWaveform(r, phi, z, scale*100, t0, fitSamples, smoothing=smooth) # print ">>new like is %f" % (new_result['fun'] / wf.windowedWf.size) # # plt.plot(new_simWf, color="g") else: if idx < 10: continue # print ">>old like is %f" % (results[idx]['fun'] / wf.windowedWf.size) # # new_result = op.minimize(nll, [r, phi, z, scale, t0, 2.], args=(det, wf.windowedWf, wf.baselineRMS), method="Powell") # r, phi, z, scale, t0, charge_cloud = new_result["x"] # new_simWf = det.GetSimWaveform(r, phi, z, scale*100, t0, fitSamples, energy=1592., charge_cloud_size=charge_cloud) # # # print ">>new like is %f" % (new_result['fun'] / wf.windowedWf.size) # # # gs = gridspec.GridSpec(2, 1, height_ratios=[4, 1]) # ax0 = plt.subplot(gs[0]) # ax1 = plt.subplot(gs[1], sharex=ax0) # ax1.set_xlabel("Digitizer Time [ns]") # ax0.set_ylabel("Voltage [Arb.]") # ax1.set_ylabel("Residual") # # ax0.plot(simWfArr[0,idx,:] ,color="blue", label="sim" ) # ax0.plot( wf.windowedWf ,color="red", label="data" ) # ax1.plot(wf.windowedWf-simWfArr[0,idx,:wf.windowedWf.size], color="b") # ax0.legend(loc=4) # # # break # plt.plot(wfToPlot , "g", alpha=0.1) # # if likes[idx] > likeCutoff: # plt.plot(np.arange(wf.windowedWf.size)*10, wf.windowedWf, color="r", ) # else: # plt.plot(np.arange(wf.windowedWf.size)*10, wf.windowedWf, color="g", alpha=0.05) plt.xlabel("time [ns]") plt.ylabel("energy [arb]") plt.plot(np.nan, color="r", label="bad fit :(") plt.plot(np.nan, color="g", label="good fit :)") plt.legend(loc=4) plt.savefig("512_all_waveforms.png") f2 = plt.figure(2, figsize=(15, 8)) helpers.plotManyResidual(simWfArr[:15], wfs[:15], f2, residAlpha=1) plt.savefig("512_residuals.png") value = raw_input(' --> Press q to quit, any other key to continue\n') if value == 'q': exit(0) ''' plt.close(f1) plt.close(f2) good_idxs = np.where(likes <= likeCutoff) bad_idxs = np.where(likes > likeCutoff) f3 = plt.figure(3, figsize=(15,8)) helpers.plotManyResidual(simWfArr[0,bad_idxs], wfs[bad_idxs], f3, residAlpha=0.5) plt.savefig("512_badwfs.png") fig_timesincelast = plt.figure() plt.scatter(time_since_last_arr[good_idxs], last_energy_arr[good_idxs], color="g") plt.scatter(time_since_last_arr[bad_idxs], last_energy_arr[bad_idxs] , color="r") plt.xlabel("Time since last event") plt.ylabel("Energy [keV]") # fig_like = plt.figure() # plt.scatter(risetimes*10, likes) # plt.xlabel("Risetime [ns]") # plt.ylabel("NLL [normalized by wf length]") # plt.savefig("512_liklihoods.png") fig_pos = plt.figure() plt.scatter(r_arr[good_idxs], z_arr[good_idxs], color="g") plt.scatter(r_arr[bad_idxs], z_arr[bad_idxs], color="r") plt.xlim(0, det.detector_radius) plt.ylim(0, det.detector_length) plt.xlabel("Radial posion [mm from point contact]") plt.ylabel("Axial posion [mm above point contact]") plt.savefig("512_positions.png") fig = plt.figure() plt.hist(likes, bins="auto") plt.xlabel("NLL [normalized by wf length]") plt.savefig("512_likes.png") plt.xlim(0,50) plt.savefig("512_likes_zoom.png") figt0 = plt.figure() plt.hist(t0_arr*10, bins=20) plt.xlabel("Start Time [ns]") plt.savefig("512_times.png") value = raw_input(' --> Press q to quit, any other key to continue\n') if value == 'q': exit(0) ''' # numWfsToSave = 30 print "minimum risetime: %f" % np.min(risetimes) print "maximum risetime: %f" % np.max(risetimes) hist, bin_edges = np.histogram(risetimes, range=(30, 100), bins=numWfsToSave) print bin_edges #bin the rise-times in 8 bins, from 30-100. Then, take enough from each to hit our waveform number goal. Try to maximize the number of low risetime events # So, say we want 32... equal would be 4 from each bin. Let's try that. # print np.where(risetimes < bin_edges[1]) idxlist_raw = [] for bin_idx in np.arange(len(bin_edges) - 1): indices = np.nonzero( np.logical_and(np.greater(risetimes, bin_edges[bin_idx]), np.less_equal(risetimes, bin_edges[bin_idx + 1])))[0] for idx in indices: if likes[idx] < 5: idxlist_raw.append(idx) break else: print "Couldn't find any wfs with like <5 in bin %d" % bin_idx for idx in indices: if likes[idx] < 10: idxlist_raw.append(idx) break else: print "...Couldn't find any wfs with like <10 in bin %d either" % bin_idx idxlist = idxlist_raw plt.figure(figsize=(20, 10)) wfsto_save = np.empty(len(bin_edges) - 1, dtype=np.object) for idx in idxlist: plt.plot(wfs[idx].windowedWf, color="r") wfFileName = "P42574A_512waveforms_%drisetimeculled.npz" % numWfsToSave np.savez(wfFileName, wfs=wfs[idxlist], results=results[idxlist]) value = raw_input(' --> Press q to quit, any other key to continue\n') if value == 'q': exit(0)
def main(argv): ################## #These change a lot numWaveforms = 16 numThreads = 4 ###################### plt.ion() channel = 626 aeCutVal = 0.01425 runRanges = [(13385, 13392), (13420,13429)] #Good runs: #13385-92 #13420-8 #13551-7 #13690-7 (not yet downloaded anything past this) #13740-7 #13905-12 #13954-61 r_mult = 1. z_mult = 1. scale_mult = 100. fitSamples = 200 #Prepare detector num = [8685207069.0676746, 1.7618952141698222e+18, 17521485536930826.0] den = [1, 50310572.447231829, 701441983664560.88, 1.4012406413698292e+19] system = signal.lti(num, den) tempGuess = 82.48 gradGuess = 0.0482 pcRadGuess = 2.563885 pcLenGuess = 1.440751 #Create a detector model detName = "conf/P42574A_grad%0.2f_pcrad%0.2f_pclen%0.2f.conf" % (0.04,2.5, 1.6) det = Detector(detName, temperature=tempGuess, timeStep=1., numSteps=fitSamples*10, tfSystem=system) det.LoadFields("P42574A_fields_len.npz") det.SetFields(pcRadGuess, pcLenGuess, gradGuess) initializeDetector(det) p = Pool(numThreads, initializer=initializeDetector, initargs=[det]) wfFileName = "P42574A_%dwaveforms.npz" % numWaveforms if os.path.isfile(wfFileName): data = np.load(wfFileName) r_arr = data['r_arr'] phi_arr = data['phi_arr'] z_arr = data['z_arr'] scale_arr = data['scale_arr'] t0_arr = data['t0_arr'] smooth_arr = data['smooth_arr'] wfs = data['wfs'] else: print "No saved waveforms available. Loading from Data" #get waveforms cut = "trapECal>%f && trapECal<%f && TSCurrent100nsMax/trapECal > %f" % (1588,1594, aeCutVal) wfs = helpers.GetWaveforms(runRanges, channel, numWaveforms, cut) for (idx, wf) in enumerate(wfs): wf.WindowWaveformTimepoint(fallPercentage=.99) #prep holders for each wf-specific param r_arr = np.empty(numWaveforms) phi_arr = np.empty(numWaveforms) z_arr = np.empty(numWaveforms) scale_arr = np.empty(numWaveforms) t0_arr = np.empty(numWaveforms) smooth_arr = np.empty(numWaveforms) simWfArr = np.empty((1,numWaveforms, fitSamples)) #parallel fit the waveforms with the initial detector params args = [] for (idx, wf) in enumerate(wfs): wf.WindowWaveformTimepoint(fallPercentage=.99) args.append( [15./r_mult, np.pi/8., 15./z_mult, wf.wfMax/scale_mult, wf.t0Guess, 10., wfs[idx] ] ) # result = op.minimize(neg_lnlike_wf, [15./10., np.pi/8., 15./10., wf.wfMax/1000., wf.t0Guess, 10.], args=(wf, det) ,method="Nelder-Mead", tol=1.) # r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx] = result["x"] # print " >> wf %d (normalized likelihood %0.2f):" % (idx, result["fun"]/wfs[idx].wfLength) # print " r: %0.2f, phi: %0.3f, z: %0.2f, e: %0.2f, t0: %0.2f, smooth:%0.2f" % (r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx]) # # simWfArr[0,idx,:] = det.GetSimWaveform(r_arr[idx]*10, phi_arr[idx], z_arr[idx]*10, scale_arr[idx]*1000, t0_arr[idx], fitSamples, smoothing=smooth_arr[idx],) print "performing parallelized initial fit..." start = timer() results = p.map(minimize_waveform_only_star, args) end = timer() print "Initial fit time: %f" % (end-start) for (idx,result) in enumerate(results): r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx] = result["x"] print " >> wf %d (normalized likelihood %0.2f):" % (idx, result["fun"]/wfs[idx].wfLength) print " r: %0.2f, phi: %0.3f, z: %0.2f, e: %0.2f, t0: %0.2f, smooth:%0.2f" % (r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx]) simWfArr[0,idx,:] = det.GetSimWaveform(r_arr[idx]*r_mult, phi_arr[idx], z_arr[idx]*z_mult, scale_arr[idx]*scale_mult, t0_arr[idx], fitSamples, smoothing=smooth_arr[idx],) fig1 = plt.figure(figsize=(20,15)) helpers.plotManyResidual(simWfArr, wfs, fig1, residAlpha=1) np.savez(wfFileName, wfs = wfs, r_arr=r_arr, phi_arr = phi_arr, z_arr = z_arr, scale_arr = scale_arr, t0_arr=t0_arr, smooth_arr=smooth_arr ) value = raw_input(' --> Press q to quit, any other key to continue\n') if value == 'q': exit(0) init_wfs(wfs) #do the detector-wide fit if True: print "Starting detector MLE..." detmleFileName = "P42574A_%dwaveforms_detectormle.npz" % numWaveforms nll_det = lambda *args: -lnlike_detector(*args) num = [num[0] / 1E9 , num[1]/1E17, num[2]/1E15] den = [ 1., den[1]/1E7, den[2]/1E14, den[3]/1E18] tf_bound = 2. tempMult = 10. gradMult = 100. mcmc_startguess = np.hstack((tempGuess/tempMult, gradGuess*gradMult, pcRadGuess, pcLenGuess, num[:], den[1:])) wfParams = np.hstack((r_arr[:], phi_arr[:], z_arr[:], scale_arr[:], t0_arr[:],smooth_arr[:],) ) bounds = [ (78./tempMult, 85./tempMult), (det.gradList[0]*gradMult, det.gradList[-1]*gradMult), (det.pcRadList[0], det.pcRadList[-1]), (det.pcLenList[0], det.pcLenList[-1]), (num[0]/tf_bound, num[0]*tf_bound),(num[1]/tf_bound, num[1]*tf_bound),(num[2]/tf_bound, num[2]*tf_bound), (den[1]/tf_bound, den[1]*tf_bound),(den[2]/tf_bound, den[2]*tf_bound),(den[3]/tf_bound, den[3]*tf_bound) ] #fitting only the 3 detector params start = timer() #result = op.basinhopping(nll_det, mcmc_startguess, accept_test=IsAllowableStep, T=20., stepsize=0.5, minimizer_kwargs={ "method":"Nelder-Mead", "tol":5., "args": (p, wfParams)}) #result = op.minimize(nll_det, mcmc_startguess, method="Nelder-Mead", tol=1., args=(p, wfParams)) #result = op.minimize(nll_det, mcmc_startguess, method="L-BFGS-B", tol=5, bounds=bounds, args=(p, wfParams)) result = op.differential_evolution(nll_det, bounds, args=(p, wfParams), polish=False) end = timer() print "Elapsed time: " + str(end-start) temp, impGrad, pcRad, pcLen = result["x"][0], result["x"][1], result["x"][2], result["x"][3] temp *= tempMult impGrad /= 100. tfStartIdx = 4 num = [result["x"][tfStartIdx] *1E9 , result["x"][tfStartIdx+1] *1E17, result["x"][tfStartIdx+2]*1E15 ] den = [1, result["x"][tfStartIdx+3] *1E7 , result["x"][tfStartIdx+4] *1E14, result["x"][tfStartIdx+5]*1E18 ] print "MLE Values..." print " >> temp is %f" % temp print " >> grad is %f" % impGrad print " >> pc rad is %f" % pcRad print " >> pc len is %f" % pcLen print " >> num = [%e, %e, %e]" % (num[0], num[1], num[2]) print " >> den = [1., %e, %e, %e]" % (den[1], den[2], den[3]) print "Plotting best fit..." fig2 = plt.figure(figsize=(20,10)) det.SetTemperature(temp) det.SetFields(pcRad, pcLen, impGrad) det.SetTransferFunction(num, den) simWfArr = np.empty((1,numWaveforms, fitSamples)) #one last minimization for the plotzzz args = [] for idx in np.arange(numWaveforms): args.append( [r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx], wfs[idx] ] ) results = p.map(minimize_waveform_only_star, args) for (idx,result) in enumerate(results): r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx] = result["x"] print " >> wf %d (normalized likelihood %0.2f):" % (idx, result["fun"]/wfs[idx].wfLength) print " r: %0.2f, phi: %0.3f, z: %0.2f, e: %0.2f, t0: %0.2f, smooth:%0.2f" % (r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx]) simWfArr[0,idx,:] = det.GetSimWaveform(r_arr[idx]*10, phi_arr[idx], z_arr[idx]*10, scale_arr[idx]*1000, t0_arr[idx], fitSamples, smoothing=smooth_arr[idx]) helpers.plotManyResidual(simWfArr, wfs, fig2, residAlpha=1) plt.savefig("mle_waveforms.pdf") np.savez(detmleFileName, wfs = wfs, r_arr=r_arr, phi_arr = phi_arr, z_arr = z_arr, scale_arr = scale_arr, t0_arr=t0_arr, smooth_arr=smooth_arr, temp=temp, impGrad=impGrad, pcRad=pcRad) value = raw_input(' --> Press q to quit, any other key to continue\n') exit(0)
def main(argv): plt.ion() fitSamples = 210 timeStepSize = 10. #ns numSamples = 20000 burnin = 0.9 * numSamples doInitPlot = False #Prepare detector zero_1 = 0.470677 pole_1 = 0.999857 pole_real = 0.807248 pole_imag = 0.085347 zeros = [zero_1, -1., 1.] poles = [ pole_1, pole_real + pole_imag * 1j, pole_real - pole_imag * 1j, ] tempGuess = 78.474793 gradGuess = 0.045049 pcRadGuess = 2.574859 pcLenGuess = 1.524812 #Create a detector model detName = "conf/P42574A_grad%0.2f_pcrad%0.2f_pclen%0.2f.conf" % (0.05, 2.5, 1.65) det = Detector(detName, temperature=tempGuess, timeStep=timeStepSize, numSteps=fitSamples * 10. / timeStepSize, poles=poles, zeros=zeros) det.LoadFields("P42574A_fields_v3.npz") det.SetFields(pcRadGuess, pcLenGuess, gradGuess) wfFileName = "P42574A_512waveforms_30risetimeculled.npz" if os.path.isfile(wfFileName): data = np.load(wfFileName) results = data['results'] wfs = data['wfs'] numWaveforms = wfs.size else: print "No saved waveforms available. Go hard or go home." exit(0) #prep holders for each wf-specific param r_arr = np.empty(numWaveforms) phi_arr = np.empty(numWaveforms) z_arr = np.empty(numWaveforms) scale_arr = np.empty(numWaveforms) t0_arr = np.empty(numWaveforms) smooth_arr = np.empty(numWaveforms) simWfArr = np.empty((1, numWaveforms, fitSamples)) for (idx, wf) in enumerate(wfs): wf.WindowWaveform(200) r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[ idx], smooth_arr[idx] = results[idx]['x'] t0_arr[ idx] += 10 #because i had a different windowing offset back in the day smooth_arr[idx] /= 10. scale_arr[idx] *= 100 #Plot the waveforms to take a look at the initial guesses if doInitPlot: plt.ion() fig = plt.figure() for (idx, wf) in enumerate(wfs): print "WF number %d:" % idx print " >>r: %f\n >>phi %f\n >>z %f\n >>e %f\n >>t0 %f\n >>smooth %f" % ( r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx]) ml_wf = det.GetSimWaveform(r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], fitSamples, smoothing=smooth_arr[idx]) plt.plot(ml_wf, color="b") plt.plot(wf.windowedWf, color="r") value = raw_input(' --> Press q to quit, any other key to continue\n') plt.ioff() if value == 'q': exit(0) startGuess = { 'radEst': r_arr, 'zEst': z_arr, 'phiEst': phi_arr, 'wfScale': scale_arr, 'switchpoint': t0_arr, 'smooth': smooth_arr, 'temp': tempGuess, 'grad': gradGuess, 'pcRad': pcRadGuess, 'pcLen': pcLenGuess } model_locals = sm.CreateFullDetectorModel(det, wfs, startGuess, zero_1, pole_1, pole_real, pole_imag) M = pymc.MCMC(model_locals, db='pickle', dbname='Detector.pickle') # M.sample(iter=100, burn=90) #12:30 pm 9/24 M.use_step_method(pymc.Slicer, M.grad, w=0.03) M.use_step_method(pymc.Slicer, M.pcRad, w=0.2) M.use_step_method(pymc.Slicer, M.pcLen, w=0.2) M.use_step_method(pymc.Slicer, M.tempEst, w=8) M.use_step_method(pymc.Slicer, M.zero_1, w=0.2) M.use_step_method(pymc.Slicer, M.pole_1, w=0.1) M.use_step_method(pymc.Slicer, M.pole_real, w=0.1) M.use_step_method(pymc.Slicer, M.pole_imag, w=0.01) # M.use_step_method(pymc.Metropolis, M.grad, proposal_sd=0.01, proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.pcRad, proposal_sd=0.05, proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.pcLen, proposal_sd=0.05, proposal_distribution='Normal') # # M.use_step_method(pymc.Metropolis, M.tempEst, proposal_sd=3., proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.zero_1, proposal_sd=0.5, proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.pole_1, proposal_sd=0.1, proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.pole_real, proposal_sd=0.5, proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.pole_imag, proposal_sd=0.1, proposal_distribution='Normal') for idx in range(numWaveforms): M.use_step_method(pymc.AdaptiveMetropolis, [M.radiusArray[idx], M.zArray[idx]], scales={ M.radiusArray[idx]: 10, M.zArray[idx]: 10 }, delay=100, interval=100, shrink_if_necessary=True) # M.use_step_method(pymc.Metropolis, M.radiusArray[idx], proposal_sd=10., proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.zArray[idx], proposal_sd=10., proposal_distribution='Normal') M.use_step_method(pymc.Metropolis, M.phiArray[idx], proposal_sd=0.3, proposal_distribution='Normal') M.use_step_method(pymc.Metropolis, M.scaleArray[idx], proposal_sd=0.01 * startGuess['wfScale'][idx], proposal_distribution='Normal') M.use_step_method(pymc.Metropolis, M.t0Array[idx], proposal_sd=5, proposal_distribution='Normal') M.use_step_method(pymc.Metropolis, M.sigArray[idx], proposal_sd=0.5, proposal_distribution='Normal') # morning 9/24 # M.use_step_method(pymc.Metropolis, M.tempEst, proposal_sd=3., proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.grad, proposal_sd=0.005, proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.pcRad, proposal_sd=0.05, proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.pcLen, proposal_sd=0.05, proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.zero_1, proposal_sd=0.01, proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.pole_1, proposal_sd=0.001, proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.pole_real, proposal_sd=0.1, proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.pole_imag, proposal_sd=0.01, proposal_distribution='Normal') # # for idx in range(numWaveforms): # M.use_step_method(pymc.AdaptiveMetropolis, [M.radiusArray[idx], M.zArray[idx]], # scales={M.radiusArray[idx]: 10, # M.zArray[idx]: 10}, delay=100, interval=100,shrink_if_necessary=True) # # ## M.use_step_method(pymc.Metropolis, M.radiusArray[idx], proposal_sd=10., proposal_distribution='Normal') ## M.use_step_method(pymc.Metropolis, M.zArray[idx], proposal_sd=10., proposal_distribution='Normal') # # M.use_step_method(pymc.Metropolis, M.phiArray[idx], proposal_sd=0.3, proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.scaleArray[idx], proposal_sd=0.01*startGuess['wfScale'][idx], proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.t0Array[idx], proposal_sd=5, proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.sigArray[idx], proposal_sd=0.5, proposal_distribution='Normal') # # M.use_step_method(pymc.AdaptiveMetropolis, [M.tempEst, M.grad, M.pcRad, M.pcLen,M.zero_1,M.pole_1,M.pole_real,M.pole_imag], # scales={M.tempEst: .3, # M.grad: 0.001, # M.pcRad: 0.01, # M.pcLen: 0.01, # M.zero_1:0.01, # M.pole_1:0.001, # M.pole_real:0.01, # M.pole_imag:0.001 # }, delay=100, interval=100,shrink_if_necessary=True) # ## zero_1 = 0.474472 ## pole_1 = 0.999845 ## pole_real = 0.807801 ## pole_imag = 0.081791 ## # for idx in range(numWaveforms): # M.use_step_method(pymc.AdaptiveMetropolis, [M.radiusArray[idx],M.phiArray[idx],M.zArray[idx], # M.scaleArray[idx], M.t0Array[idx], M.sigArray[idx],], # scales={M.radiusArray[idx]: 10, # M.phiArray[idx]: np.pi/4/4, # M.zArray[idx]: 10, # M.scaleArray[idx]: 0.01*startGuess['wfScale'][idx], # M.t0Array[idx]: 5, # M.sigArray[idx]: 0.5 # }, delay=100, interval=100,shrink_if_necessary=True) M.sample(iter=numSamples, burn=0, tune_interval=100) M.db.close() # totalIter = 0 # while totalIter < this_sample: # M.sample(iter=10, verbose=0) # totalIter += 10 # #pymc.Matplot.plot(M) # # ######### Plots for MC Steps stepsFig = plt.figure(figsize=(20, 10)) plt.clf() ax0 = stepsFig.add_subplot(611) ax1 = stepsFig.add_subplot(612, sharex=ax0) ax2 = stepsFig.add_subplot(613, sharex=ax0) ax3 = stepsFig.add_subplot(614, sharex=ax0) ax4 = stepsFig.add_subplot(615, sharex=ax0) ax5 = stepsFig.add_subplot(616, sharex=ax0) ax0.set_ylabel('r') ax1.set_ylabel('z') ax2.set_ylabel('phi') ax3.set_ylabel('e') ax4.set_ylabel('t0') ax5.set_ylabel('sig') for i in range(len(wfs)): ax0.plot(M.trace('radEst_%d' % i)[:]) ax1.plot(M.trace('zEst_%d' % i)[:]) ax2.plot(M.trace('phiEst_%d' % i)[:]) ax3.plot(M.trace('wfScale_%d' % i)[:]) ax4.plot(M.trace('switchpoint_%d' % i)[:]) ax5.plot(M.trace('sigma_%d' % i)[:]) plt.savefig("pymc_wf_params.png") # stepsFig2 = plt.figure(figsize=(20, 10)) plt.clf() ax0 = stepsFig2.add_subplot(811) ax1 = stepsFig2.add_subplot(812, sharex=ax0) ax2 = stepsFig2.add_subplot(813, sharex=ax0) ax3 = stepsFig2.add_subplot(814, sharex=ax0) ax4 = stepsFig2.add_subplot(815, sharex=ax0) ax5 = stepsFig2.add_subplot(816, sharex=ax0) ax6 = stepsFig2.add_subplot(817, sharex=ax0) ax7 = stepsFig2.add_subplot(818, sharex=ax0) ax0.set_ylabel('zero_1') ax1.set_ylabel('pole_1') ax2.set_ylabel('pole_real') ax3.set_ylabel('pole_imag') ax4.set_ylabel('temp') ax5.set_ylabel('grad') ax6.set_ylabel('pcRad') ax7.set_ylabel('pcLen') ax0.plot(M.trace('zero_1')[:]) ax1.plot(M.trace('pole_1')[:]) ax2.plot(M.trace('pole_real')[:]) ax3.plot(M.trace('pole_imag')[:]) ax4.plot(M.trace('temp')[:]) ax5.plot(M.trace('grad')[:]) ax6.plot(M.trace('pcRad')[:]) ax7.plot(M.trace('pcLen')[:]) plt.savefig("pymc_detector.png") fig3 = plt.figure(3, figsize=(20, 10)) plt.clf() plt.title("Charge waveform") plt.xlabel("Sample number [10s of ns]") plt.ylabel("Raw ADC Value [Arb]") wfPlotNumber = 10 simWfArr = np.empty((wfPlotNumber, numWaveforms, fitSamples)) if burnin > len(M.trace('temp')[:]): burnin = len(M.trace('temp')[:]) - wfPlotNumber numSamples = len(M.trace('temp')[:]) for (sim_idx, chain_idx) in enumerate( np.random.randint(low=burnin, high=numSamples, size=wfPlotNumber)): temp = M.trace('temp')[chain_idx] grad = M.trace('grad')[chain_idx] pcRad = M.trace('pcRad')[chain_idx] pcLen = M.trace('pcLen')[chain_idx] zero_1 = M.trace('zero_1')[chain_idx] pole_1 = M.trace('pole_1')[chain_idx] pole_real = M.trace('pole_real')[chain_idx] pole_imag = M.trace('pole_imag')[chain_idx] zeros = [zero_1, -1., 1.] poles = [ pole_1, pole_real + pole_imag * 1j, pole_real - pole_imag * 1j, ] det.SetTransferFunction(zeros, poles) det.SetTemperature(temp) det.SetFields(pcRad, pcLen, grad) for (wf_idx, wf) in enumerate(wfs): t0 = M.trace('switchpoint_%d' % wf_idx)[chain_idx] r = M.trace('radEst_%d' % wf_idx)[chain_idx] z = M.trace('zEst_%d' % wf_idx)[chain_idx] phi = M.trace('phiEst_%d' % wf_idx)[chain_idx] scale = M.trace('wfScale_%d' % wf_idx)[chain_idx] sigma = M.trace('sigma_%d' % wf_idx)[chain_idx] simWfArr[sim_idx, wf_idx, :] = det.GetSimWaveform(r, phi, z, scale, t0, fitSamples, smoothing=sigma) helpers.plotManyResidual(simWfArr, wfs, fig3, residAlpha=1) plt.savefig("pymc_waveforms.png") value = raw_input(' --> Press q to quit, any other key to continue\n')
def main(argv): plt.ion() fitSamples = 200 zero_1 = -5.56351644e+07 pole_1 = -1.38796386e+04 pole_real = -2.02559385e+07 pole_imag = 9885315.37450211 zeros = [zero_1, 0] poles = [pole_real + pole_imag * 1j, pole_real - pole_imag * 1j, pole_1] system = signal.lti(zeros, poles, 1E7) tempGuess = 77.757659 gradGuess = 0.0401 pcRadGuess = 2.5551 pcLenGuess = 1.5169 numSamples = 1000 #Create a detector model detName = "conf/P42574A_grad%0.2f_pcrad%0.2f_pclen%0.2f.conf" % (0.04, 2.5, 1.6) det = Detector(detName, temperature=tempGuess, timeStep=1., numSteps=fitSamples * 10, tfSystem=system) det.LoadFields("P42574A_fields_v3.npz") det.SetFields(pcRadGuess, pcLenGuess, gradGuess) wfFileName = "P42574A_512waveforms_8risetimeculled.npz" if os.path.isfile(wfFileName): data = np.load(wfFileName) results = data['results'] wfs = data['wfs'] numWaveforms = wfs.size else: print "No saved waveforms available. Loading from Data" exit(0) #prep holders for each wf-specific param r_arr = np.empty(numWaveforms) phi_arr = np.empty(numWaveforms) z_arr = np.empty(numWaveforms) scale_arr = np.empty(numWaveforms) t0_arr = np.empty(numWaveforms) smooth_arr = np.empty(numWaveforms) simWfArr = np.empty((1, numWaveforms, fitSamples)) args = [] for (idx, wf) in enumerate(wfs): wf.WindowWaveform(200) r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[ idx], smooth_arr[idx] = results[idx]['x'] t0_arr[ idx] += 10 #because i had a different windowing offset back in the day args.append([ r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], wf.t0Guess, 5., wfs[idx] ]) if True: # p = Pool(8, initializer=initializeDetector, initargs=[det]) # print "performing parallelized initial fit..." # results = p.map(minimize_waveform_only_star, args) # np.savez(wfFileName, wfs = wfs, results=results ) fig = plt.figure() for (idx, wf) in enumerate(wfs): print "WF number %d:" % idx print " >>r: %f\n >>phi %f\n >>z %f\n >>e %f\n >>t0 %f\n >>smooth %f" % ( r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx]) ml_wf = det.GetSimWaveform(r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx] * 100, t0_arr[idx], fitSamples, smoothing=smooth_arr[idx]) plt.plot(ml_wf, color="b") plt.plot(wf.windowedWf, color="r") value = raw_input(' --> Press q to quit, any other key to continue\n') if value == 'q': exit(0) startGuess = { 'radEst': r_arr, 'zEst': z_arr, 'phiEst': phi_arr, 'wfScale': scale_arr * 100, 'switchpoint': t0_arr, 'smooth': smooth_arr, 'temp': tempGuess, 'grad': gradGuess, 'pcRad': pcRadGuess, 'pcLen': pcLenGuess } siggen_model = sm3.CreateFullDetectorModel(det, wfs, startGuess, zero_1, pole_1, pole_real, pole_imag) with siggen_model: step = Metropolis() trace = sample(numSamples, step=step) burnin = np.int(len(trace['temp'][:]) - 0.25 * len(trace['temp'][:]) ) #no clue what to choose, for now, just make it 75% temp = np.median(trace['temp'][burnin:]) print "<<<detector temperature is %f" % temp grad = np.median(trace['grad'][burnin:]) pcRad = np.median(trace['pcRad'][burnin:]) pcLen = np.median(trace['pcLen'][burnin:]) print "<<<detector gradient is %0.4f " % (grad) print "<<<PC Radius is %0.4f, Length is %0.4f " % (pcRad, pcLen) zero_1 = np.median(trace['zero_1'][burnin:]) pole_1 = np.median(trace['pole_1'][burnin:]) pole_real = np.median(trace['pole_real'][burnin:]) pole_imag = np.median(trace['pole_imag'][burnin:]) print "<<< zero_1=%e" % zero_1 print "<<< pole_1=%e" % pole_1 print "<<< pole_real=%e" % pole_real print "<<< pole_imag=%e" % pole_imag plt.ioff() traceplot(trace) plt.savefig("hierarchical_full_%dchain.png" % len(wfs)) plt.ion() zeros = [zero_1, 0] poles = [ pole_real + pole_imag * 1j, pole_real - pole_imag * 1j, pole_1 ] system = signal.lti(zeros, poles, 1E7) det.tfSystem = system det.SetTemperature(temp) det.SetFields(pcRad, pcLen, grad) fig3 = plt.figure(3, figsize=(20, 10)) plt.clf() plt.title("Charge waveform") plt.xlabel("Sample number [10s of ns]") plt.ylabel("Raw ADC Value [Arb]") wfPlotNumber = 10 simWfArr = np.empty((wfPlotNumber, numWaveforms, fitSamples)) for (sim_idx, chain_idx) in enumerate( np.random.randint(low=burnin, high=numSamples, size=wfPlotNumber)): temp = trace['temp'][chain_idx] grad = trace['grad'][chain_idx] pcRad = trace['pcRad'][chain_idx] pcLen = trace['pcLen'][chain_idx] zero_1 = trace['zero_1'][chain_idx] pole_1 = trace['pole_1'][chain_idx] pole_real = trace['pole_real'][chain_idx] pole_imag = trace['pole_imag'][chain_idx] zeros = [zero_1, 0] poles = [ pole_real + pole_imag * 1j, pole_real - pole_imag * 1j, pole_1 ] system = signal.lti(zeros, poles, 1E7) det.tfSystem = system det.SetTemperature(temp) det.SetFields(pcRad, pcLen, grad) for (wf_idx, wf) in enumerate(wfs): t0 = trace['switchpoint'][chain_idx, wf_idx] r = trace['radEst'][chain_idx, wf_idx] z = trace['zEst'][chain_idx, wf_idx] phi = trace['phiEst'][chain_idx, wf_idx] scale = trace['wfScale'][chain_idx, wf_idx] sigma = trace['sigma'][chain_idx, wf_idx] simWfArr[sim_idx, wf_idx, :] = det.GetSimWaveform(r, phi, z, scale, t0, fitSamples, smoothing=sigma) helpers.plotManyResidual(simWfArr, wfs, fig3, residAlpha=1) # plt.savefig("hierarchical_full_%dwaveforms.png" % len(wfs)) summary(trace) value = raw_input(' --> Press q to quit, any other key to continue\n')
def main(): ################## #These change a lot numWaveforms = 2 numThreads = 8 ndim = 6 * numWaveforms + 13 nwalkers = 100 * ndim iter = 50000 burnIn = 49000 wfPlotNumber = 100 ###################### doPlots = 1 # plt.ion() fitSamples = 200 timeStepSize = 1. #ns #Prepare detector tempGuess = 79.310080 gradGuess = 0.04 pcRadGuess = 2.5 pcLenGuess = 1.6 #Create a detector model detName = "conf/P42574A_grad%0.2f_pcrad%0.2f_pclen%0.2f.conf" % (0.05, 2.5, 1.65) det = Detector(detName, temperature=tempGuess, timeStep=timeStepSize, numSteps=fitSamples * 10) det.LoadFields("P42574A_fields_v3.npz") det.SetFields(pcRadGuess, pcLenGuess, gradGuess) b_over_a = 0.107213 c = -0.815152 d = 0.822696 rc1 = 74.4 rc2 = 1.79 rcfrac = 0.992 trapping_rc = 120 #us det.SetTransferFunction(b_over_a, c, d, rc1, rc2, rcfrac) det.trapping_rc = trapping_rc det.siggenInst.set_velocity_type(1) h_100_mu0, h_100_beta, h_100_e0, h_111_mu0, h_111_beta, h_111_e0 = 66333., 0.744, 181., 107270., 0.580, 100. mlw.initializeDetector(det, ) #and the remaining 6 are for the transfer function fig_size = (20, 10) #Create a decent start guess by fitting waveform-by-waveform wfFileName = "P42574A_12_fastandslow_oldwfs.npz" # wfFileName = "P42574A_24_spread.npz" # wfFileName = "P42574A_5_fast.npz" if os.path.isfile(wfFileName): data = np.load(wfFileName) results = data['results'] wfs = data['wfs'] # wfs = np.delete(wfs, [2]) # results = np.delete(results, [2]) # results = results[::3] idxs = [4, 5] wfs = wfs[idxs] results = results[idxs] numWaveforms = wfs.size else: print "No saved waveforms available. Exiting." exit(0) #prep holders for each wf-specific param r_arr = np.empty(numWaveforms) phi_arr = np.empty(numWaveforms) z_arr = np.empty(numWaveforms) scale_arr = np.empty(numWaveforms) t0_arr = np.empty(numWaveforms) smooth_arr = np.ones(numWaveforms) * 7. simWfArr = np.empty((1, numWaveforms, fitSamples)) #Prepare the initial value arrays # plt.ion() # fig = plt.figure() for (idx, wf) in enumerate(wfs): wf.WindowWaveformTimepoint( fallPercentage=.99, rmsMult=2, ) r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[ idx], smooth_arr[idx] = results[idx]['x'] # plt.plot(wf.windowedWf) # value = raw_input(' --> Press q to quit, any other key to continue\n') # t0_arr[idx] -= 15 #Plot the waveforms to take a look at the initial guesses if True: plt.ion() fig1 = plt.figure(1) plt.clf() gs = gridspec.GridSpec(2, 1, height_ratios=[4, 1]) ax0 = plt.subplot(gs[0]) ax1 = plt.subplot(gs[1], sharex=ax0) ax1.set_xlabel("Digitizer Time [ns]") ax0.set_ylabel("Voltage [Arb.]") ax1.set_ylabel("Residual") for (idx, wf) in enumerate(wfs): print "WF number %d:" % idx mlw.initializeWaveform(wf) dataLen = wf.wfLength t_data = np.arange(dataLen) * 10 ax0.plot(t_data, wf.windowedWf, color="r") # minresult = None # minlike = np.inf # # for r in np.linspace(4, np.floor(det.detector_radius)-3, 6): # for z in np.linspace(4, np.floor(det.detector_length)-3, 6): # # for t0_guess in np.linspace(wf.t0Guess-10, wf.t0Guess+5, 3): # if not det.IsInDetector(r,0,z): continue # startGuess = [r, np.pi/8, z, wf.wfMax, wf.t0Guess-5, 10] # result = op.minimize(nll, startGuess, method="Nelder-Mead") # r, phi, z, scale, t0, smooth, = result["x"] # ml_wf = np.copy(det.MakeSimWaveform(r, phi, z, scale, t0, fitSamples, h_smoothing=smooth, )) # if ml_wf is None: # print r, z # continue # if result['fun'] < minlike: # minlike = result['fun'] # minresult = result # r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx] = minresult['x'] r, phi, z, scale, t0, smooth = results[idx]['x'] startGuess = [r, phi, z, scale, t0, smooth] result = op.minimize(nll, startGuess, method="Powell") r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[ idx], smooth_arr[idx] = result['x'] print " >>r: %f\n >>phi %f\n >>z %f\n >>e %f\n >>t0 %f\n >>smooth %f" % ( r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx]) ml_wf = det.MakeSimWaveform(r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], fitSamples, h_smoothing=smooth_arr[idx]) ax0.plot(t_data, ml_wf[:dataLen], color="g") ax1.plot( t_data, ml_wf[:dataLen] - wf.windowedWf, color="g", ) value = raw_input(' --> Press q to quit, any other key to continue\n') plt.ioff() if value == 'q': exit(0) #Initialize the multithreading p = Pool(numThreads, initializer=initializeDetectorAndWaveforms, initargs=[det, wfs]) initializeDetectorAndWaveforms(det, wfs) #Do the MCMC mcmc_startguess = np.hstack(( r_arr[:], phi_arr[:], z_arr[:], scale_arr[:], t0_arr[:], smooth_arr[:], # waveform-specific params trapping_rc, b_over_a, c, d, rc1, rc2, rcfrac, h_100_mu0, h_100_beta, h_100_e0, h_111_mu0, h_111_beta, h_111_e0)) # detector-specific #number of walkers _must_ be even if nwalkers % 2: nwalkers += 1 pos0 = [ mcmc_startguess + 1e-2 * np.random.randn(ndim) * mcmc_startguess for i in range(nwalkers) ] trapIdx = -13 rc1idx = -9 rc2idx = -8 rcfracidx = -7 #Make sure everything in the initial guess is within bounds for pos in pos0: pos[:numWaveforms] = np.clip(pos[:numWaveforms], 0, np.floor(det.detector_radius * 10.) / 10.) pos[numWaveforms:2 * numWaveforms] = np.clip( pos[numWaveforms:2 * numWaveforms], 0, np.pi / 4) pos[2 * numWaveforms:3 * numWaveforms] = np.clip( pos[2 * numWaveforms:3 * numWaveforms], 0, np.floor(det.detector_length * 10.) / 10.) pos[4 * numWaveforms:5 * numWaveforms] = np.clip( pos[4 * numWaveforms:5 * numWaveforms], 0, fitSamples) pos[5 * numWaveforms:6 * numWaveforms] = np.clip( pos[5 * numWaveforms:6 * numWaveforms], 0, 20.) # pos[tempIdx] = np.clip(pos[tempIdx], 40, 120) pos[trapIdx] = np.clip(pos[trapIdx], 0, np.inf) pos[rcfracidx] = np.clip(pos[rcfracidx], 0, 1) pos[rc2idx] = np.clip(pos[rc2idx], 0, np.inf) pos[rc1idx] = np.clip(pos[rc1idx], 0, np.inf) prior = lnprior(pos, ) if not np.isfinite(prior): print "BAD PRIOR WITH START GUESS YOURE KILLING ME SMALLS" print pos exit(0) #Initialize, run the MCMC sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob, pool=p) #w/ progress bar, & time the thing bar = ProgressBar(widgets=[Percentage(), Bar(), ETA()], maxval=iter).start() for (idx, result) in enumerate( sampler.sample(pos0, iterations=iter, storechain=True)): bar.update(idx + 1) bar.finish() if not doPlots: exit(0) print "Dumping chain to file..." np.save("sampler_%dwfs.npy" % numWaveforms, sampler.chain) print "Making MCMC steps figure..." # ######### Plots for Waveform params # stepsFig = plt.figure(2, figsize=fig_size) # plt.clf() # ax0 = stepsFig.add_subplot(611) # ax1 = stepsFig.add_subplot(612, sharex=ax0) # ax2 = stepsFig.add_subplot(613, sharex=ax0) # ax3 = stepsFig.add_subplot(614, sharex=ax0) # ax4 = stepsFig.add_subplot(615, sharex=ax0) # ax5 = stepsFig.add_subplot(616, sharex=ax0) # # ax0.set_ylabel('r') # ax1.set_ylabel('phi') # ax2.set_ylabel('z') # ax3.set_ylabel('scale') # ax4.set_ylabel('t0') # ax5.set_ylabel('smoothing') # # for i in range(nwalkers): # for j in range(wfs.size): # ax0.plot(sampler.chain[i,:,0+j], alpha=0.3) # r # ax1.plot(sampler.chain[i,:,numWaveforms + j], alpha=0.3) # phi # ax2.plot(sampler.chain[i,:,2*numWaveforms + j], alpha=0.3) #z # ax3.plot(sampler.chain[i,:,3*numWaveforms + j], alpha=0.3) #energy # ax4.plot(sampler.chain[i,:,4*numWaveforms + j], alpha=0.3) #t0 # ax5.plot(sampler.chain[i,:,5*numWaveforms + j], alpha=0.3) #smoothing # # plt.savefig("emcee_wfchain_%dwfs.png" % numWaveforms) # # # #and for the transfer function stepsFigTF = plt.figure(7, figsize=fig_size) plt.clf() tf0 = stepsFigTF.add_subplot(711) tf1 = stepsFigTF.add_subplot(712, sharex=tf0) tf2 = stepsFigTF.add_subplot(713, sharex=tf0) tf3 = stepsFigTF.add_subplot(714, sharex=tf0) tf4 = stepsFigTF.add_subplot(715, sharex=tf0) tf5 = stepsFigTF.add_subplot(716, sharex=tf0) tf6 = stepsFigTF.add_subplot(717, sharex=tf0) # tf7 = stepsFigTF.add_subplot(818, sharex=tf0) tf0.set_ylabel('b_over_a') tf1.set_ylabel('c') tf2.set_ylabel('d') tf3.set_ylabel('rc1') tf4.set_ylabel('rc2') tf5.set_ylabel('rcfrac') tf6.set_ylabel('temp') # tf7.set_ylabel('trapping') for i in range(nwalkers): tf0.plot(sampler.chain[i, :, trapIdx + 1], "b", alpha=0.3) #2 tf1.plot(sampler.chain[i, :, trapIdx + 2], "b", alpha=0.3) #den1 tf2.plot(sampler.chain[i, :, trapIdx + 3], "b", alpha=0.3) #2 tf3.plot(sampler.chain[i, :, trapIdx + 4], "b", alpha=0.3) #3 tf4.plot(sampler.chain[i, :, trapIdx + 5], "b", alpha=0.3) #3 tf5.plot(sampler.chain[i, :, trapIdx + 6], "b", alpha=0.3) #3 tf6.plot(sampler.chain[i, :, trapIdx], "b", alpha=0.3) #3 # tf6.plot(sampler.chain[i,:,trapIdx], "b", alpha=0.3) #3 plt.savefig("emcee_tfchain_%dwfs.png" % numWaveforms) stepsFigTF = plt.figure(6, figsize=fig_size) plt.clf() tf0 = stepsFigTF.add_subplot(611) tf1 = stepsFigTF.add_subplot(612, sharex=tf0) tf2 = stepsFigTF.add_subplot(613, sharex=tf0) tf3 = stepsFigTF.add_subplot(614, sharex=tf0) tf4 = stepsFigTF.add_subplot(615, sharex=tf0) tf5 = stepsFigTF.add_subplot(616, sharex=tf0) # tf6 = stepsFigTF.add_subplot(717, sharex=tf0) # tf7 = stepsFigTF.add_subplot(818, sharex=tf0) tf0.set_ylabel('1') tf1.set_ylabel('2') tf2.set_ylabel('3') tf3.set_ylabel('4') tf4.set_ylabel('5') tf5.set_ylabel('6') # tf6.set_ylabel('temp') # tf7.set_ylabel('trapping') for i in range(nwalkers): tf0.plot(sampler.chain[i, :, trapIdx + 7], "b", alpha=0.3) #2 tf1.plot(sampler.chain[i, :, trapIdx + 8], "b", alpha=0.3) #den1 tf2.plot(sampler.chain[i, :, trapIdx + 9], "b", alpha=0.3) #2 tf3.plot(sampler.chain[i, :, trapIdx + 10], "b", alpha=0.3) #3 tf4.plot(sampler.chain[i, :, trapIdx + 11], "b", alpha=0.3) #3 tf5.plot(sampler.chain[i, :, trapIdx + 12], "b", alpha=0.3) #3 # tf6.plot(sampler.chain[i,:,trapIdx], "b", alpha=0.3) #3 # tf6.plot(sampler.chain[i,:,trapIdx], "b", alpha=0.3) #3 plt.savefig("emcee_velochain_%dwfs.png" % numWaveforms) samples = sampler.chain[:, burnIn:, :].reshape((-1, ndim)) stepsFigTF = plt.figure(5, figsize=(20, 10)) plotnum = 600 tf0 = stepsFigTF.add_subplot(plotnum + 11) tf1 = stepsFigTF.add_subplot(plotnum + 12, ) tf2 = stepsFigTF.add_subplot(plotnum + 13, ) tf3 = stepsFigTF.add_subplot(plotnum + 14, ) tf4 = stepsFigTF.add_subplot(plotnum + 15, ) tf5 = stepsFigTF.add_subplot(plotnum + 16, ) tf0.set_ylabel('h_100_mu0') tf1.set_ylabel('h_100_beta') tf2.set_ylabel('h_100_e0') tf3.set_ylabel('h_111_mu0') tf4.set_ylabel('h_111_beta') tf5.set_ylabel('h_111_e0') num_bins = 300 [n, b, p] = tf0.hist(samples[:, -6], bins=num_bins) print "h_100_mu0 mode is %f" % b[np.argmax(n)] [n, b, p] = tf1.hist(samples[:, -5], bins=num_bins) print "h_100_beta mode is %f" % b[np.argmax(n)] [n, b, p] = tf2.hist(samples[:, -4], bins=num_bins) print "h_100_e0 mode is %f" % b[np.argmax(n)] [n, b, p] = tf3.hist(samples[:, -3], bins=num_bins) print "h_111_mu0 mode is %f" % b[np.argmax(n)] [n, b, p] = tf4.hist(samples[:, -2], bins=num_bins) print "h_111_beta mode is %f" % b[np.argmax(n)] [n, b, p] = tf5.hist(samples[:, -1], bins=num_bins) print "h_111_e0 mode is %f" % b[np.argmax(n)] # print "temp is %f" % np.median(samples[:,tempIdx]) print "trapping is %f" % np.median(samples[:, trapIdx]) print "b_over_a is %f" % np.median(samples[:, -6]) print "c is %f" % np.median(samples[:, -5]) print "d is %f" % np.median(samples[:, -4]) print "rc_decay1 is %f" % np.median(samples[:, -3]) print "rc_decay2 is %f" % np.median(samples[:, -2]) print "rc_frac is %f" % np.median(samples[:, -1]) #TODO: Aaaaaaand plot some waveforms.. simWfs = np.empty((wfPlotNumber, numWaveforms, fitSamples)) for idx, (theta) in enumerate(samples[np.random.randint( len(samples), size=wfPlotNumber)]): # temp = theta[tempIdx] # trapping_rc = theta[trapIdx] trapping_rc, b_over_a, c, d, rc1, rc2, rcfrac, h_100_mu0, h_100_beta, h_100_e0, h_111_mu0, h_111_beta, h_111_e0, = theta[ -13:] r_arr, phi_arr, z_arr, scale_arr, t0_arr, smooth_arr = theta[:-13].reshape( (6, numWaveforms)) det.siggenInst.set_hole_params(h_100_mu0, h_100_beta, h_100_e0, h_111_mu0, h_111_beta, h_111_e0) # det.SetTemperature(temp) det.trapping_rc = trapping_rc det.SetTransferFunction(b_over_a, c, d, rc1, rc2, rcfrac) for wf_idx in range(numWaveforms): wf_i = det.MakeSimWaveform(r_arr[wf_idx], phi_arr[wf_idx], z_arr[wf_idx], scale_arr[wf_idx], t0_arr[wf_idx], fitSamples, h_smoothing=smooth_arr[wf_idx]) simWfs[idx, wf_idx, :] = wf_i if wf_i is None: print "Waveform %d, %d is None" % (idx, wf_idx) residFig = plt.figure(4, figsize=(20, 15)) helpers.plotManyResidual(simWfs, wfs, figure=residFig) plt.savefig("emcee_waveforms_%dwfs.png" % numWaveforms) value = raw_input(' --> Press q to quit, any other key to continue\n')
def main(argv): ################## #These change a lot numWaveforms = 3 numThreads = 8 ndim = 6*numWaveforms + 8 nwalkers = 10*ndim iter=1000 burnIn = 800 wfPlotNumber = 20 ###################### # plt.ion() fitSamples = 140 timeStepSize = 1. #ns #Prepare detector tempGuess = 79.194609 gradGuess = 0.048953 pcRadGuess = 2.498492 pcLenGuess = 1.574738 #Create a detector model detName = "conf/P42574A_grad%0.2f_pcrad%0.2f_pclen%0.2f.conf" % (0.05,2.5, 1.65) det = Detector(detName, temperature=tempGuess, timeStep=timeStepSize, numSteps=fitSamples*10 ) det.LoadFields("P42574A_fields_v3.npz") det.SetFields(pcRadGuess, pcLenGuess, gradGuess) b_over_a = 0.119558 c = -0.804569 d = 0.809473 rc = 74.235542 det.SetTransferFunction(b_over_a, c, d, rc) tempIdx = -8 gradIdx = -7 pcRadIdx = -6 pcLenIdx = -5 #and the remaining 4 are for the transfer function fig_size = (20,10) #Create a decent start guess by fitting waveform-by-waveform # wfFileName = "P42574A_512waveforms_%drisetimeculled.npz" % numWaveforms wfFileName = "P42574A_3_fastandslow_oldwfs.npz" if os.path.isfile(wfFileName): data = np.load(wfFileName) results = data['results'] wfs = data['wfs'] # results = np.array([results[0], results[2], results[4]]) # wfs = np.array([wfs[0], wfs[2], wfs[4]]) numWaveforms = wfs.size else: print "No saved waveforms available. Loading from Data" exit(0) #prep holders for each wf-specific param r_arr = np.empty(numWaveforms) phi_arr = np.empty(numWaveforms) z_arr = np.empty(numWaveforms) scale_arr = np.empty(numWaveforms) t0_arr = np.empty(numWaveforms) smooth_arr = np.ones(numWaveforms)*10. simWfArr = np.empty((1,numWaveforms, fitSamples)) #Prepare the initial value arrays for (idx, wf) in enumerate(wfs): wf.WindowWaveformTimepoint(fallPercentage=.999, rmsMult=2) r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], __ = results[idx]['x'] #Plot the waveforms to take a look at the initial guesses if True: plt.ion() fig = plt.figure() for (idx,wf) in enumerate(wfs): print "WF number %d:" % idx print " >>r: %f\n >>phi %f\n >>z %f\n >>e %f\n >>t0 %f\n >>smooth %f" % (r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx]) ml_wf = det.MakeSimWaveform(r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], fitSamples, h_smoothing = smooth_arr[idx]) plt.plot(ml_wf, color="b") plt.plot(wf.windowedWf, color="r") value = raw_input(' --> Press q to quit, any other key to continue\n') plt.ioff() if value == 'q': exit(0) #Initialize the multithreading p = Pool(numThreads, initializer=initializeDetectorAndWaveforms, initargs=[det, wfs]) initializeDetectorAndWaveforms(det, wfs) #Do the MCMC mcmc_startguess = np.hstack((r_arr[:], phi_arr[:], z_arr[:], scale_arr[:], t0_arr[:],smooth_arr[:], # waveform-specific params tempGuess, gradGuess,pcRadGuess, pcLenGuess, b_over_a, c, d, rc)) # detector-specific #number of walkers _must_ be even if nwalkers % 2: nwalkers +=1 #Initialize walkers with a random, narrow ball around the start guess pos0 = [mcmc_startguess + 1e-2*np.random.randn(ndim)*mcmc_startguess for i in range(nwalkers)] #Make sure everything in the initial guess is within bounds for pos in pos0: pos[:numWaveforms] = np.clip( pos[:numWaveforms], 0, np.floor(det.detector_radius*10.)/10.) pos[numWaveforms:2*numWaveforms] = np.clip(pos[numWaveforms:2*numWaveforms], 0, np.pi/4) pos[2*numWaveforms:3*numWaveforms] = np.clip(pos[2*numWaveforms:3*numWaveforms], 0, np.floor(det.detector_length*10.)/10.) pos[4*numWaveforms:5*numWaveforms] = np.clip(pos[4*numWaveforms:5*numWaveforms], 0, fitSamples) pos[5*numWaveforms:6*numWaveforms] = np.clip(pos[5*numWaveforms:6*numWaveforms], 0, 20.) pos[tempIdx] = np.clip(pos[tempIdx], 40, 120) pos[gradIdx] = np.clip(pos[gradIdx], det.gradList[0], det.gradList[-1]) pos[pcRadIdx] = np.clip(pos[pcRadIdx], det.pcRadList[0], det.pcRadList[-1]) pos[pcLenIdx] = np.clip(pos[pcLenIdx], det.pcLenList[0], det.pcLenList[-1]) prior = lnprior(pos,) if not np.isfinite(prior) : print "BAD PRIOR WITH START GUESS YOURE KILLING ME SMALLS" print pos exit(0) #Initialize, run the MCMC sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob, pool=p) #w/ progress bar, & time the thing bar = ProgressBar(widgets=[Percentage(), Bar(), ETA()], maxval=iter).start() for (idx,result) in enumerate(sampler.sample(pos0, iterations=iter, storechain=True)): bar.update(idx+1) bar.finish() print "Dumping chain to file..." np.save("sampler_%dwfs.npy" % numWaveforms, sampler.chain) print "Making MCMC steps figure..." ######### Plots for Waveform params stepsFig = plt.figure(2, figsize=fig_size) plt.clf() ax0 = stepsFig.add_subplot(611) ax1 = stepsFig.add_subplot(612, sharex=ax0) ax2 = stepsFig.add_subplot(613, sharex=ax0) ax3 = stepsFig.add_subplot(614, sharex=ax0) ax4 = stepsFig.add_subplot(615, sharex=ax0) ax5 = stepsFig.add_subplot(616, sharex=ax0) ax0.set_ylabel('r') ax1.set_ylabel('phi') ax2.set_ylabel('z') ax3.set_ylabel('scale') ax4.set_ylabel('t0') ax5.set_ylabel('smoothing') for i in range(nwalkers): for j in range(wfs.size): ax0.plot(sampler.chain[i,:,0+j], alpha=0.3) # r ax1.plot(sampler.chain[i,:,numWaveforms + j], alpha=0.3) # phi ax2.plot(sampler.chain[i,:,2*numWaveforms + j], alpha=0.3) #z ax3.plot(sampler.chain[i,:,3*numWaveforms + j], alpha=0.3) #energy ax4.plot(sampler.chain[i,:,4*numWaveforms + j], alpha=0.3) #t0 ax5.plot(sampler.chain[i,:,5*numWaveforms + j], alpha=0.3) #smoothing plt.savefig("emcee_wfchain_%dwfs.png" % numWaveforms) ######### Plots for Detector params stepsFigDet = plt.figure(3, figsize=fig_size) plt.clf() ax0 = stepsFigDet.add_subplot(411) ax1 = stepsFigDet.add_subplot(412, sharex=ax0) ax2 = stepsFigDet.add_subplot(413, sharex=ax0) ax3 = stepsFigDet.add_subplot(414, sharex=ax0) ax0.set_ylabel('temp') ax1.set_ylabel('grad') ax2.set_ylabel('pcRad') ax3.set_ylabel('pcLen') for i in range(nwalkers): ax0.plot(sampler.chain[i,:,tempIdx], "b", alpha=0.3) #temp ax1.plot(sampler.chain[i,:,gradIdx], "b", alpha=0.3) #grad ax2.plot(sampler.chain[i,:,pcRadIdx], "b", alpha=0.3) #pcrad ax3.plot(sampler.chain[i,:,pcLenIdx], "b", alpha=0.3) #pclen plt.savefig("emcee_detchain_%dwfs.png" % numWaveforms) #and for the transfer function stepsFigTF = plt.figure(4, figsize=fig_size) plt.clf() tf0 = stepsFigTF.add_subplot(411) tf1 = stepsFigTF.add_subplot(412, sharex=ax0) tf2 = stepsFigTF.add_subplot(413, sharex=ax0) tf3 = stepsFigTF.add_subplot(414, sharex=ax0) # tf3 = stepsFigTF.add_subplot(515, sharex=ax0) tf0.set_ylabel('b_over_a') tf1.set_ylabel('c') tf2.set_ylabel('d') tf3.set_ylabel('rc_decay') for i in range(nwalkers): tf0.plot(sampler.chain[i,:,-4], "b", alpha=0.3) #2 tf1.plot(sampler.chain[i,:,-3], "b", alpha=0.3) #den1 tf2.plot(sampler.chain[i,:,-2], "b", alpha=0.3) #2 tf3.plot(sampler.chain[i,:,-1], "b", alpha=0.3) #3 # tf3.plot(sampler.chain[i,:,-2], "b", alpha=0.3) #3 plt.savefig("emcee_tfchain_%dwfs.png" % numWaveforms) samples = sampler.chain[:, burnIn:, :].reshape((-1, ndim)) print "temp is %f" % np.median(samples[:,tempIdx]) print "grad is %f" % np.median(samples[:,gradIdx]) print "pcrad is %f" % np.median(samples[:,pcRadIdx]) print "pclen is %f" % np.median(samples[:,pcLenIdx]) print "b_over_a is %f" % np.median(samples[:,-4]) print "c is %f" % np.median(samples[:,-3]) print "d is %f" % np.median(samples[:,-2]) print "rc_decay is %f" % np.median(samples[:,-1]) #TODO: Aaaaaaand plot some waveforms.. simWfs = np.empty((wfPlotNumber,numWaveforms, fitSamples)) for idx, (theta) in enumerate(samples[np.random.randint(len(samples), size=wfPlotNumber)]): temp, impGrad, pcRad, pcLen = theta[tempIdx], theta[gradIdx], theta[pcRadIdx], theta[pcLenIdx] b_over_a, c, d, rc = theta[-4:] r_arr, phi_arr, z_arr, scale_arr, t0_arr, smooth_arr = theta[:-8].reshape((6, numWaveforms)) det.SetTemperature(temp) det.SetFields(pcRad, pcLen, impGrad) det.SetTransferFunction(b_over_a, c, d, rc) for wf_idx in range(wfs.size): wf_i = det.MakeSimWaveform(r_arr[wf_idx], phi_arr[wf_idx], z_arr[wf_idx], scale_arr[wf_idx], t0_arr[wf_idx], fitSamples, h_smoothing = smooth_arr[wf_idx]) simWfs[idx, wf_idx, :] = wf_i if wf_i is None: print "Waveform %d, %d is None" % (idx, wf_idx) residFig = plt.figure(4, figsize=(20, 15)) helpers.plotManyResidual(simWfs, wfs, figure=residFig) plt.savefig("emcee_waveforms_%dwfs.png" % numWaveforms)
def main(argv): plt.ion() fitSamples = 210 timeStepSize = 10. #ns numSamples = 10000 doInitPlot = False #Prepare detector zero_1 = 0.474472 pole_1 = 0.999845 pole_real = 0.807801 pole_imag = 0.081791 zeros = [zero_1, -1., 1.] poles = [ pole_1, pole_real + pole_imag * 1j, pole_real - pole_imag * 1j, ] tempGuess = 77.747244 gradGuess = 0.046950 pcRadGuess = 2.547418 pcLenGuess = 1.569172 #Create a detector model detName = "conf/P42574A_grad%0.2f_pcrad%0.2f_pclen%0.2f.conf" % (0.05, 2.5, 1.65) det = Detector(detName, temperature=tempGuess, timeStep=timeStepSize, numSteps=fitSamples * 10. / timeStepSize, poles=poles, zeros=zeros) det.LoadFields("P42574A_fields_v3.npz") det.SetFields(pcRadGuess, pcLenGuess, gradGuess) wfFileName = "P42574A_512waveforms_8risetimeculled.npz" if os.path.isfile(wfFileName): data = np.load(wfFileName) results = data['results'] wfs = data['wfs'] numWaveforms = wfs.size else: print "No saved waveforms available. Loading from Data" exit(0) #prep holders for each wf-specific param r_arr = np.empty(numWaveforms) phi_arr = np.empty(numWaveforms) z_arr = np.empty(numWaveforms) scale_arr = np.empty(numWaveforms) t0_arr = np.empty(numWaveforms) smooth_arr = np.empty(numWaveforms) simWfArr = np.empty((1, numWaveforms, fitSamples)) for (idx, wf) in enumerate(wfs): wf.WindowWaveform(200) r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[ idx], smooth_arr[idx] = results[idx]['x'] t0_arr[ idx] += 10 #because i had a different windowing offset back in the day smooth_arr[idx] /= 10. #Plot the waveforms to take a look at the initial guesses if doInitPlot: plt.ion() fig = plt.figure() for (idx, wf) in enumerate(wfs): print "WF number %d:" % idx print " >>r: %f\n >>phi %f\n >>z %f\n >>e %f\n >>t0 %f\n >>smooth %f" % ( r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx]) ml_wf = det.GetSimWaveform(r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx] * 100, t0_arr[idx], fitSamples, smoothing=smooth_arr[idx]) plt.plot(ml_wf, color="b") plt.plot(wf.windowedWf, color="r") value = raw_input(' --> Press q to quit, any other key to continue\n') plt.ioff() if value == 'q': exit(0) startGuess = { 'radEst': r_arr, 'zEst': z_arr, 'phiEst': phi_arr, 'wfScale': scale_arr * 100, 'switchpoint': t0_arr, 'smooth': smooth_arr, 'temp': tempGuess, 'grad': gradGuess, 'pcRad': pcRadGuess, 'pcLen': pcLenGuess } siggen_model = sm3.CreateFullDetectorModel(det, wfs, startGuess, zero_1, pole_1, pole_real, pole_imag) with siggen_model: step = Metropolis(scaling=[ np.ones(1) * 1, np.ones(1) * 5, np.ones(1) * np.pi / 4 / 10, np.ones(1) * 5, np.ones(1) * 100, np.ones(1) * 0.1, 3, 0.01, 0.0001, 0.01, 0.001, 0.005, 0.05, 0.05 ]) #step = Metropolis(scaling=0.001) trace = sample( numSamples, step=step, ) burnin = np.int(len(trace['temp'][:]) - 0.25 * len(trace['temp'][:]) ) #no clue what to choose, for now, just make it 75% temp = np.median(trace['temp'][burnin:]) print "<<<detector temperature is %f" % temp grad = np.median(trace['grad'][burnin:]) pcRad = np.median(trace['pcRad'][burnin:]) pcLen = np.median(trace['pcLen'][burnin:]) print "<<<detector gradient is %0.4f " % (grad) print "<<<PC Radius is %0.4f, Length is %0.4f " % (pcRad, pcLen) zero_1 = np.median(trace['zero_1'][burnin:]) pole_1 = np.median(trace['pole_1'][burnin:]) pole_real = np.median(trace['pole_real'][burnin:]) pole_imag = np.median(trace['pole_imag'][burnin:]) print "<<< zero_1=%e" % zero_1 print "<<< pole_1=%e" % pole_1 print "<<< pole_real=%e" % pole_real print "<<< pole_imag=%e" % pole_imag plt.ioff() traceplot(trace) plt.savefig("hierarchical_full_%dchain.png" % len(wfs)) plt.ion() fig3 = plt.figure(3, figsize=(20, 10)) plt.clf() plt.title("Charge waveform") plt.xlabel("Sample number [10s of ns]") plt.ylabel("Raw ADC Value [Arb]") wfPlotNumber = 10 simWfArr = np.empty((wfPlotNumber, numWaveforms, fitSamples)) for (sim_idx, chain_idx) in enumerate( np.random.randint(low=burnin, high=numSamples, size=wfPlotNumber)): temp = trace['temp'][chain_idx] grad = trace['grad'][chain_idx] pcRad = trace['pcRad'][chain_idx] pcLen = trace['pcLen'][chain_idx] zero_1 = trace['zero_1'][chain_idx] pole_1 = trace['pole_1'][chain_idx] pole_real = trace['pole_real'][chain_idx] pole_imag = trace['pole_imag'][chain_idx] zeros = [zero_1, -1., 1.] poles = [ pole_1, pole_real + pole_imag * 1j, pole_real - pole_imag * 1j, ] det.SetTransferFunction(zeros, poles) det.SetTemperature(temp) det.SetFields(pcRad, pcLen, grad) for (wf_idx, wf) in enumerate(wfs): t0 = trace['switchpoint'][chain_idx, wf_idx] r = trace['radEst'][chain_idx, wf_idx] z = trace['zEst'][chain_idx, wf_idx] phi = trace['phiEst'][chain_idx, wf_idx] scale = trace['wfScale'][chain_idx, wf_idx] sigma = trace['sigma'][chain_idx, wf_idx] simWfArr[sim_idx, wf_idx, :] = det.GetSimWaveform(r, phi, z, scale, t0, fitSamples, smoothing=sigma) helpers.plotManyResidual(simWfArr, wfs, fig3, residAlpha=1) # plt.savefig("hierarchical_full_%dwaveforms.png" % len(wfs)) summary(trace) value = raw_input(' --> Press q to quit, any other key to continue\n')
def main(argv): numThreads = 4 numWfs = 4 plt.ion() r_mult = 1. z_mult = 1. scale_mult = 100. fitSamples = 200 #Prepare detector num = [8685207069.0676746, 1.7618952141698222e+18, 17521485536930826.0] den = [1, 50310572.447231829, 701441983664560.88, 1.4012406413698292e+19] system = signal.lti(num, den) tempGuess = 82.48 gradGuess = 0.0482 pcRadGuess = 2.563885 pcLenGuess = 1.440751 #Create a detector model detName = "conf/P42574A_grad%0.2f_pcrad%0.2f_pclen%0.2f.conf" % (0.04,2.5, 1.6) det = Detector(detName, temperature=tempGuess, timeStep=1., numSteps=fitSamples*10, tfSystem=system) det.LoadFields("P42574A_fields_len.npz") det.SetFields(pcRadGuess, pcLenGuess, gradGuess) simWfArr = np.empty((1,numWfs, fitSamples)) wfFileName = "P42574A_32waveforms_risetimeculled.npz" if os.path.isfile(wfFileName): data = np.load(wfFileName) r_arr = data['r_arr'] phi_arr = data['phi_arr'] z_arr = data['z_arr'] scale_arr = data['scale_arr'] t0_arr = data['t0_arr'] smooth_arr = data['smooth_arr'] wfs = data['wfs'] else: exit(0) initializeDetectorAndWfs(det, wfs[:numWfs]) p = Pool(numThreads, initializer=initializeDetectorAndWfs, initargs=[det, wfs[:numWfs]]) args = [] for (idx, wf) in enumerate(wfs[:numWfs]): args.append( [15./r_mult, np.pi/8., 15./z_mult, wf.wfMax/scale_mult, wf.t0Guess, 10., wfs[idx] ] ) print "performing parallelized initial fit..." start = timer() results = p.map(minimize_waveform_only_star, args) end = timer() print "Initial fit time: %f" % (end-start) for (idx,result) in enumerate(results): r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx] = result["x"] print " >> wf %d (normalized likelihood %0.2f):" % (idx, result["fun"]/wfs[idx].wfLength) print " r: %0.2f, phi: %0.3f, z: %0.2f, e: %0.2f, t0: %0.2f, smooth:%0.2f" % (r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx]) simWfArr[0,idx,:] = det.GetSimWaveform(r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx]*100., t0_arr[idx], fitSamples, smoothing=smooth_arr[idx],) fig1 = plt.figure(figsize=(20,15)) helpers.plotManyResidual(simWfArr, wfs[:numWfs], fig1, residAlpha=1) value = raw_input(' --> Press q to quit, any other key to continue\n') if value == 'q': exit(0) tempMult = 10. gradMult = 100. mcmc_startguess = np.hstack((tempGuess/tempMult, gradGuess*gradMult, pcRadGuess, pcLenGuess, num[:], den[1:])) wfParams = np.hstack((np.ones(numWfs)*3., phi_arr[:numWfs], np.ones(numWfs)*3., scale_arr[:numWfs], t0_arr[:numWfs],smooth_arr[:numWfs],) ) nll_det = lambda *args: -lnlike_detector(*args) result = op.minimize(nll_det, mcmc_startguess, method="Nelder-Mead", args=(p, wfParams)) temp, impGrad, pcRad, pcLen = result["x"][0], result["x"][1], result["x"][2], result["x"][3] temp *= tempMult impGrad /= 100. tfStartIdx = 4 num = [result["x"][tfStartIdx] *1E9 , result["x"][tfStartIdx+1] *1E17, result["x"][tfStartIdx+2]*1E15 ] den = [1, result["x"][tfStartIdx+3] *1E7 , result["x"][tfStartIdx+4] *1E14, result["x"][tfStartIdx+5]*1E18 ] print "MLE Values..." print " >> temp is %f" % temp print " >> grad is %f" % impGrad print " >> pc rad is %f" % pcRad print " >> pc len is %f" % pcLen print " >> num = [%e, %e, %e]" % (num[0], num[1], num[2]) print " >> den = [1., %e, %e, %e]" % (den[1], den[2], den[3]) print "Plotting best fit..." fig2 = plt.figure(figsize=(20,10)) det.SetTemperature(temp) det.SetFields(pcRad, pcLen, impGrad) det.SetTransferFunction(num, den) simWfArr = np.empty((1,numWfs, fitSamples)) #one last minimization for the plotzzz args = [] for idx in np.arange(numWfs): args.append( [r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx], wfs[idx] ] ) results = p.map(minimize_waveform_only_star, args) for (idx,result) in enumerate(results): r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx] = result["x"] print " >> wf %d (normalized likelihood %0.2f):" % (idx, result["fun"]/wfs[idx].wfLength) print " r: %0.2f, phi: %0.3f, z: %0.2f, e: %0.2f, t0: %0.2f, smooth:%0.2f" % (r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx]) simWfArr[0,idx,:] = det.GetSimWaveform(r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx]*100, t0_arr[idx], fitSamples, smoothing=smooth_arr[idx]) helpers.plotManyResidual(simWfArr, wfs[:numWfs], fig2, residAlpha=1) plt.savefig("mle_waveforms_fast.pdf") value = raw_input(' --> Press q to quit, any other key to continue\n') exit(0)
def main(argv): ################## #These change a lot numWaveforms = 30 numThreads = 12 ndim = 6*numWaveforms + 8 nwalkers = 4*ndim iter=5000 burnIn = 4000 wfPlotNumber = 100 ###################### # plt.ion() fitSamples = 200 #Prepare detector zero_1 = -5.56351644e+07 pole_1 = -1.38796386e+04 pole_real = -2.02559385e+07 pole_imag = 9885315.37450211 zeros = [zero_1,0 ] poles = [ pole_real+pole_imag*1j, pole_real-pole_imag*1j, pole_1] system = signal.lti(zeros, poles, 1E7 ) tempGuess = 77.89 gradGuess = 0.0483 pcRadGuess = 2.591182 pcLenGuess = 1.613357 #Create a detector model detName = "conf/P42574A_grad%0.2f_pcrad%0.2f_pclen%0.2f.conf" % (0.05,2.5, 1.65) det = Detector(detName, temperature=tempGuess, timeStep=1., numSteps=fitSamples*10, tfSystem=system) det.LoadFields("P42574A_fields_v3.npz") det.SetFields(pcRadGuess, pcLenGuess, gradGuess) tempIdx = -8 gradIdx = -7 pcRadIdx = -6 pcLenIdx = -5 #and the remaining 4 are for the transfer function fig_size = (20,10) #Create a decent start guess by fitting waveform-by-waveform wfFileName = "P42574A_512waveforms_%drisetimeculled.npz" % numWaveforms if os.path.isfile(wfFileName): data = np.load(wfFileName) results = data['results'] wfs = data['wfs'] numWaveforms = wfs.size else: print "No saved waveforms available. Loading from Data" exit(0) #prep holders for each wf-specific param r_arr = np.empty(numWaveforms) phi_arr = np.empty(numWaveforms) z_arr = np.empty(numWaveforms) scale_arr = np.empty(numWaveforms) t0_arr = np.empty(numWaveforms) smooth_arr = np.ones(numWaveforms)*7. simWfArr = np.empty((1,numWaveforms, fitSamples)) #Prepare the initial value arrays for (idx, wf) in enumerate(wfs): wf.WindowWaveformTimepoint(fallPercentage=.99) r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx] = results[idx]['x'] t0_arr[idx] += 10 #because i had a different windowing offset back in the day #Plot the waveforms to take a look at the initial guesses if False: fig = plt.figure() for (idx,wf) in enumerate(wfs): print "WF number %d:" % idx print " >>r: %f\n >>phi %f\n >>z %f\n >>e %f\n >>t0 %f\n >>smooth %f" % (r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx]) ml_wf = det.GetSimWaveform(r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx]*100, t0_arr[idx], fitSamples, smoothing = smooth_arr[idx]) plt.plot(ml_wf, color="b") plt.plot(wf.windowedWf, color="r") value = raw_input(' --> Press q to quit, any other key to continue\n') if value == 'q': exit(0) #Initialize the multithreading p = Pool(numThreads, initializer=initializeDetectorAndWaveforms, initargs=[det, wfs]) initializeDetectorAndWaveforms(det, wfs) #Do the MCMC mcmc_startguess = np.hstack((r_arr[:], phi_arr[:], z_arr[:], scale_arr[:]*100., t0_arr[:],smooth_arr[:], # waveform-specific params tempGuess, gradGuess,pcRadGuess, pcLenGuess, zero_1, pole_1, pole_real, pole_imag)) # detector-specific #number of walkers _must_ be even if nwalkers % 2: nwalkers +=1 #Initialize walkers with a random, narrow ball around the start guess pos0 = [mcmc_startguess + 1e-2*np.random.randn(ndim)*mcmc_startguess for i in range(nwalkers)] #Make sure everything in the initial guess is within bounds for pos in pos0: pos[:numWaveforms] = np.clip( pos[:numWaveforms], 0, np.floor(det.detector_radius*10.)/10.) pos[numWaveforms:2*numWaveforms] = np.clip(pos[numWaveforms:2*numWaveforms], 0, np.pi/4) pos[2*numWaveforms:3*numWaveforms] = np.clip(pos[2*numWaveforms:3*numWaveforms], 0, np.floor(det.detector_length*10.)/10.) pos[4*numWaveforms:5*numWaveforms] = np.clip(pos[4*numWaveforms:5*numWaveforms], 0, fitSamples) pos[5*numWaveforms:6*numWaveforms] = np.clip(pos[5*numWaveforms:6*numWaveforms], 0, 20.) pos[tempIdx] = np.clip(pos[tempIdx], 40, 120) pos[gradIdx] = np.clip(pos[gradIdx], det.gradList[0], det.gradList[-1]) pos[pcRadIdx] = np.clip(pos[pcRadIdx], det.pcRadList[0], det.pcRadList[-1]) pos[pcLenIdx] = np.clip(pos[pcLenIdx], det.pcLenList[0], det.pcLenList[-1]) prior = lnprior(pos,) if not np.isfinite(prior) : print "BAD PRIOR WITH START GUESS YOURE KILLING ME SMALLS" print pos exit(0) #Initialize, run the MCMC sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob, pool=p) #w/ progress bar, & time the thing bar = ProgressBar(widgets=[Percentage(), Bar()], maxval=iter).start() start = timer() for (idx,result) in enumerate(sampler.sample(pos0, iterations=iter, storechain=True)): bar.update(idx+1) end = timer() bar.finish() print "Elapsed time: " + str(end-start) print "Dumping chain to file..." np.save("sampler_%dwfs.npy" % numWaveforms, sampler.chain) print "Making MCMC steps figure..." ######### Plots for Waveform params stepsFig = plt.figure(2, figsize=fig_size) plt.clf() ax0 = stepsFig.add_subplot(611) ax1 = stepsFig.add_subplot(612, sharex=ax0) ax2 = stepsFig.add_subplot(613, sharex=ax0) ax3 = stepsFig.add_subplot(614, sharex=ax0) ax4 = stepsFig.add_subplot(615, sharex=ax0) ax5 = stepsFig.add_subplot(616, sharex=ax0) ax0.set_ylabel('r') ax1.set_ylabel('phi') ax2.set_ylabel('z') ax3.set_ylabel('scale') ax4.set_ylabel('t0') ax5.set_ylabel('smoothing') for i in range(nwalkers): for j in range(wfs.size): ax0.plot(sampler.chain[i,:,0+j], alpha=0.3) # r ax1.plot(sampler.chain[i,:,numWaveforms + j], alpha=0.3) # phi ax2.plot(sampler.chain[i,:,2*numWaveforms + j], alpha=0.3) #z ax3.plot(sampler.chain[i,:,3*numWaveforms + j], alpha=0.3) #energy ax4.plot(sampler.chain[i,:,4*numWaveforms + j], alpha=0.3) #t0 ax5.plot(sampler.chain[i,:,5*numWaveforms + j], alpha=0.3) #smoothing plt.savefig("emcee_wfchain_%dwfs.png" % numWaveforms) ######### Plots for Detector params stepsFigDet = plt.figure(3, figsize=fig_size) plt.clf() ax0 = stepsFigDet.add_subplot(411) ax1 = stepsFigDet.add_subplot(412, sharex=ax0) ax2 = stepsFigDet.add_subplot(413, sharex=ax0) ax3 = stepsFigDet.add_subplot(414, sharex=ax0) ax0.set_ylabel('temp') ax1.set_ylabel('grad') ax2.set_ylabel('pcRad') ax3.set_ylabel('pcLen') for i in range(nwalkers): ax0.plot(sampler.chain[i,:,tempIdx], "b", alpha=0.3) #temp ax1.plot(sampler.chain[i,:,gradIdx], "b", alpha=0.3) #grad ax2.plot(sampler.chain[i,:,pcRadIdx], "b", alpha=0.3) #pcrad ax3.plot(sampler.chain[i,:,pcLenIdx], "b", alpha=0.3) #pclen plt.savefig("emcee_detchain_%dwfs.png" % numWaveforms) #and for the transfer function stepsFigTF = plt.figure(4, figsize=fig_size) plt.clf() tf0 = stepsFigTF.add_subplot(411) tf1 = stepsFigTF.add_subplot(412, sharex=ax0) tf2 = stepsFigTF.add_subplot(413, sharex=ax0) tf3 = stepsFigTF.add_subplot(414, sharex=ax0) tf0.set_ylabel('zero_1') tf1.set_ylabel('pole_1') tf2.set_ylabel('pole_real') tf3.set_ylabel('pole_imag') for i in range(nwalkers): tf0.plot(sampler.chain[i,:,-4], "b", alpha=0.3) #2 tf1.plot(sampler.chain[i,:,-3], "b", alpha=0.3) #den1 tf2.plot(sampler.chain[i,:,-2], "b", alpha=0.3) #2 tf3.plot(sampler.chain[i,:,-1], "b", alpha=0.3) #3 plt.savefig("emcee_tfchain_%dwfs.png" % numWaveforms) samples = sampler.chain[:, burnIn:, :].reshape((-1, ndim)) print "temp is %f" % np.median(samples[:,tempIdx]) print "grad is %f" % np.median(samples[:,gradIdx]) print "pcrad is %f" % np.median(samples[:,pcRadIdx]) print "pclen is %f" % np.median(samples[:,pcLenIdx]) print "zero_1 is %f" % np.median(samples[:,-4]) print "pole_1 is %f" % np.median(samples[:,-3]) print "pole_real is %f" % np.median(samples[:,-2]) print "pole_imag is %f" % np.median(samples[:,-1]) #TODO: Aaaaaaand plot some waveforms.. simWfs = np.empty((wfPlotNumber,numWaveforms, fitSamples)) for idx, (theta) in enumerate(samples[np.random.randint(len(samples), size=wfPlotNumber)]): temp, impGrad, pcRad, pcLen = theta[tempIdx], theta[gradIdx], theta[pcRadIdx], theta[pcLenIdx] zero_1, pole_1, pole_real, pole_imag = theta[-4:] r_arr, phi_arr, z_arr, scale_arr, t0_arr, smooth_arr = theta[:-8].reshape((6, numWaveforms)) det.SetTemperature(temp) det.SetFields(pcRad, pcLen, impGrad) zeros = [zero_1,0 ] poles = [ pole_real+pole_imag*1j, pole_real-pole_imag*1j, pole_1] det.SetTransferFunction(zeros, poles, 1E7) for wf_idx in range(wfs.size): wf_i = det.GetSimWaveform(r_arr[wf_idx], phi_arr[wf_idx], z_arr[wf_idx], scale_arr[wf_idx], t0_arr[wf_idx], fitSamples) simWfs[idx, wf_idx, :] = wf_i if wf_i is None: print "Waveform %d, %d is None" % (idx, wf_idx) residFig = plt.figure(4, figsize=(20, 15)) helpers.plotManyResidual(simWfs, wfs, figure=residFig) plt.savefig("emcee_waveforms_%dwfs.png" % numWaveforms)
def main(argv): numThreads = 4 numWfs = 4 plt.ion() r_mult = 1. z_mult = 1. scale_mult = 100. fitSamples = 200 #Prepare detector num = [8685207069.0676746, 1.7618952141698222e+18, 17521485536930826.0] den = [1, 50310572.447231829, 701441983664560.88, 1.4012406413698292e+19] system = signal.lti(num, den) tempGuess = 82.48 gradGuess = 0.0482 pcRadGuess = 2.563885 pcLenGuess = 1.440751 #Create a detector model detName = "conf/P42574A_grad%0.2f_pcrad%0.2f_pclen%0.2f.conf" % (0.04, 2.5, 1.6) det = Detector(detName, temperature=tempGuess, timeStep=1., numSteps=fitSamples * 10, tfSystem=system) det.LoadFields("P42574A_fields_len.npz") det.SetFields(pcRadGuess, pcLenGuess, gradGuess) simWfArr = np.empty((1, numWfs, fitSamples)) wfFileName = "P42574A_32waveforms_risetimeculled.npz" if os.path.isfile(wfFileName): data = np.load(wfFileName) r_arr = data['r_arr'] phi_arr = data['phi_arr'] z_arr = data['z_arr'] scale_arr = data['scale_arr'] t0_arr = data['t0_arr'] smooth_arr = data['smooth_arr'] wfs = data['wfs'] else: exit(0) initializeDetectorAndWfs(det, wfs[:numWfs]) p = Pool(numThreads, initializer=initializeDetectorAndWfs, initargs=[det, wfs[:numWfs]]) args = [] for (idx, wf) in enumerate(wfs[:numWfs]): args.append([ 15. / r_mult, np.pi / 8., 15. / z_mult, wf.wfMax / scale_mult, wf.t0Guess, 10., wfs[idx] ]) print "performing parallelized initial fit..." start = timer() results = p.map(minimize_waveform_only_star, args) end = timer() print "Initial fit time: %f" % (end - start) for (idx, result) in enumerate(results): r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[ idx], smooth_arr[idx] = result["x"] print " >> wf %d (normalized likelihood %0.2f):" % ( idx, result["fun"] / wfs[idx].wfLength) print " r: %0.2f, phi: %0.3f, z: %0.2f, e: %0.2f, t0: %0.2f, smooth:%0.2f" % ( r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx]) simWfArr[0, idx, :] = det.GetSimWaveform( r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx] * 100., t0_arr[idx], fitSamples, smoothing=smooth_arr[idx], ) fig1 = plt.figure(figsize=(20, 15)) helpers.plotManyResidual(simWfArr, wfs[:numWfs], fig1, residAlpha=1) value = raw_input(' --> Press q to quit, any other key to continue\n') if value == 'q': exit(0) tempMult = 10. gradMult = 100. mcmc_startguess = np.hstack((tempGuess / tempMult, gradGuess * gradMult, pcRadGuess, pcLenGuess, num[:], den[1:])) wfParams = np.hstack(( np.ones(numWfs) * 3., phi_arr[:numWfs], np.ones(numWfs) * 3., scale_arr[:numWfs], t0_arr[:numWfs], smooth_arr[:numWfs], )) nll_det = lambda *args: -lnlike_detector(*args) result = op.minimize(nll_det, mcmc_startguess, method="Nelder-Mead", args=(p, wfParams)) temp, impGrad, pcRad, pcLen = result["x"][0], result["x"][1], result["x"][ 2], result["x"][3] temp *= tempMult impGrad /= 100. tfStartIdx = 4 num = [ result["x"][tfStartIdx] * 1E9, result["x"][tfStartIdx + 1] * 1E17, result["x"][tfStartIdx + 2] * 1E15 ] den = [ 1, result["x"][tfStartIdx + 3] * 1E7, result["x"][tfStartIdx + 4] * 1E14, result["x"][tfStartIdx + 5] * 1E18 ] print "MLE Values..." print " >> temp is %f" % temp print " >> grad is %f" % impGrad print " >> pc rad is %f" % pcRad print " >> pc len is %f" % pcLen print " >> num = [%e, %e, %e]" % (num[0], num[1], num[2]) print " >> den = [1., %e, %e, %e]" % (den[1], den[2], den[3]) print "Plotting best fit..." fig2 = plt.figure(figsize=(20, 10)) det.SetTemperature(temp) det.SetFields(pcRad, pcLen, impGrad) det.SetTransferFunction(num, den) simWfArr = np.empty((1, numWfs, fitSamples)) #one last minimization for the plotzzz args = [] for idx in np.arange(numWfs): args.append([ r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx], wfs[idx] ]) results = p.map(minimize_waveform_only_star, args) for (idx, result) in enumerate(results): r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[ idx], smooth_arr[idx] = result["x"] print " >> wf %d (normalized likelihood %0.2f):" % ( idx, result["fun"] / wfs[idx].wfLength) print " r: %0.2f, phi: %0.3f, z: %0.2f, e: %0.2f, t0: %0.2f, smooth:%0.2f" % ( r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx]) simWfArr[0, idx, :] = det.GetSimWaveform(r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx] * 100, t0_arr[idx], fitSamples, smoothing=smooth_arr[idx]) helpers.plotManyResidual(simWfArr, wfs[:numWfs], fig2, residAlpha=1) plt.savefig("mle_waveforms_fast.pdf") value = raw_input(' --> Press q to quit, any other key to continue\n') exit(0)
def main(argv): plt.ion() fitSamples = 200 zero_1 = -5.56351644e+07 pole_1 = -1.38796386e+04 pole_real = -2.02559385e+07 pole_imag = 9885315.37450211 zeros = [zero_1,0 ] poles = [ pole_real+pole_imag*1j, pole_real-pole_imag*1j, pole_1] system = signal.lti(zeros, poles, 1E7 ) tempGuess = 77.757659 gradGuess = 0.0401 pcRadGuess = 2.5551 pcLenGuess = 1.5169 numSamples = 1000 #Create a detector model detName = "conf/P42574A_grad%0.2f_pcrad%0.2f_pclen%0.2f.conf" % (0.04,2.5, 1.6) det = Detector(detName, temperature=tempGuess, timeStep=1., numSteps=fitSamples*10, tfSystem=system) det.LoadFields("P42574A_fields_v3.npz") det.SetFields(pcRadGuess, pcLenGuess, gradGuess) wfFileName = "P42574A_512waveforms_8risetimeculled.npz" if os.path.isfile(wfFileName): data = np.load(wfFileName) results = data['results'] wfs = data['wfs'] numWaveforms = wfs.size else: print "No saved waveforms available. Loading from Data" exit(0) #prep holders for each wf-specific param r_arr = np.empty(numWaveforms) phi_arr = np.empty(numWaveforms) z_arr = np.empty(numWaveforms) scale_arr = np.empty(numWaveforms) t0_arr = np.empty(numWaveforms) smooth_arr = np.empty(numWaveforms) simWfArr = np.empty((1,numWaveforms, fitSamples)) args = [] for (idx, wf) in enumerate(wfs): wf.WindowWaveform(200) r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx] = results[idx]['x'] t0_arr[idx] += 10 #because i had a different windowing offset back in the day args.append( [r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], wf.t0Guess, 5., wfs[idx] ] ) if True: # p = Pool(8, initializer=initializeDetector, initargs=[det]) # print "performing parallelized initial fit..." # results = p.map(minimize_waveform_only_star, args) # np.savez(wfFileName, wfs = wfs, results=results ) fig = plt.figure() for (idx,wf) in enumerate(wfs): print "WF number %d:" % idx print " >>r: %f\n >>phi %f\n >>z %f\n >>e %f\n >>t0 %f\n >>smooth %f" % (r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx]) ml_wf = det.GetSimWaveform(r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx]*100, t0_arr[idx], fitSamples, smoothing = smooth_arr[idx]) plt.plot(ml_wf, color="b") plt.plot(wf.windowedWf, color="r") value = raw_input(' --> Press q to quit, any other key to continue\n') if value == 'q': exit(0) startGuess = {'radEst': r_arr, 'zEst': z_arr, 'phiEst': phi_arr, 'wfScale': scale_arr*100, 'switchpoint': t0_arr, 'smooth': smooth_arr, 'temp': tempGuess, 'grad': gradGuess, 'pcRad': pcRadGuess, 'pcLen':pcLenGuess} siggen_model = sm3.CreateFullDetectorModel(det, wfs, startGuess, zero_1, pole_1, pole_real, pole_imag) with siggen_model: step = Metropolis() trace = sample(numSamples, step = step) burnin = np.int(len(trace['temp'][:]) - 0.25*len(trace['temp'][:]))#no clue what to choose, for now, just make it 75% temp = np.median( trace['temp'][burnin:]) print "<<<detector temperature is %f" % temp grad = np.median( trace['grad'][burnin:]) pcRad= np.median( trace['pcRad'][burnin:]) pcLen= np.median( trace['pcLen'][burnin:]) print "<<<detector gradient is %0.4f " % (grad) print "<<<PC Radius is %0.4f, Length is %0.4f " % (pcRad, pcLen) zero_1 = np.median( trace['zero_1'][burnin:]) pole_1 = np.median( trace['pole_1'][burnin:]) pole_real = np.median( trace['pole_real'][burnin:]) pole_imag = np.median( trace['pole_imag'][burnin:]) print "<<< zero_1=%e" % zero_1 print "<<< pole_1=%e" % pole_1 print "<<< pole_real=%e"% pole_real print "<<< pole_imag=%e"% pole_imag plt.ioff() traceplot(trace) plt.savefig("hierarchical_full_%dchain.png" % len(wfs)) plt.ion() zeros = [zero_1,0 ] poles = [ pole_real+pole_imag*1j, pole_real-pole_imag*1j, pole_1] system = signal.lti(zeros, poles, 1E7 ) det.tfSystem = system det.SetTemperature(temp) det.SetFields(pcRad, pcLen, grad) fig3 = plt.figure(3, figsize = (20,10)) plt.clf() plt.title("Charge waveform") plt.xlabel("Sample number [10s of ns]") plt.ylabel("Raw ADC Value [Arb]") wfPlotNumber = 10 simWfArr = np.empty((wfPlotNumber,numWaveforms, fitSamples)) for (sim_idx, chain_idx) in enumerate(np.random.randint(low=burnin, high=numSamples, size=wfPlotNumber)): temp = trace['temp'][chain_idx] grad = trace['grad'][chain_idx] pcRad= trace['pcRad'][chain_idx] pcLen= trace['pcLen'][chain_idx] zero_1 = trace['zero_1'][chain_idx] pole_1 = trace['pole_1'][chain_idx] pole_real = trace['pole_real'][chain_idx] pole_imag = trace['pole_imag'][chain_idx] zeros = [zero_1,0 ] poles = [ pole_real+pole_imag*1j, pole_real-pole_imag*1j, pole_1] system = signal.lti(zeros, poles, 1E7 ) det.tfSystem = system det.SetTemperature(temp) det.SetFields(pcRad, pcLen, grad) for (wf_idx, wf) in enumerate(wfs): t0 = trace['switchpoint'][chain_idx,wf_idx] r = trace['radEst'][chain_idx,wf_idx] z = trace['zEst'][chain_idx,wf_idx] phi = trace['phiEst'][chain_idx,wf_idx] scale = trace['wfScale'][chain_idx,wf_idx] sigma = trace['sigma'][chain_idx,wf_idx] simWfArr[sim_idx,wf_idx,:] = det.GetSimWaveform(r, phi, z, scale, t0, fitSamples, smoothing=sigma) helpers.plotManyResidual(simWfArr, wfs, fig3, residAlpha=1) # plt.savefig("hierarchical_full_%dwaveforms.png" % len(wfs)) summary(trace) value = raw_input(' --> Press q to quit, any other key to continue\n')
def main(argv): fitSamples = 200 plt.ion() pzCorrTimeConstant = 72*1000. pz = MGWFPoleZeroCorrection() pz.SetDecayConstant(pzCorrTimeConstant) wfFileName = "P42574A_512waveforms_fit_smooth_de.npz" if os.path.isfile(wfFileName): data = np.load(wfFileName) results = data['result_arr'] wfs = data['wfs'] else: print "No saved waveforms available." exit(0) wfFileName = "P42574A_512waveforms_raw.npz" if os.path.isfile(wfFileName): data = np.load(wfFileName) wfs_raw = data['wfs'] wfs_no_bl_sub = data['raw_wfs'] else: print "No raw waveforms available." exit(0) print "There are %d waveforms fit" % wfs.size print "...And i pulled out %d raw ones" % wfs_raw.size det = prepdetector(fitSamples) simWfArr = np.empty((1,len(wfs), fitSamples)) likes = np.empty(len(wfs)) risetimes = np.empty(len(wfs)) r_arr = np.empty(len(wfs)) z_arr = np.empty(len(wfs)) t0_arr = np.empty(len(wfs)) time_since_last_arr = np.empty(len(wfs)) last_energy_arr = np.empty(len(wfs)) f1 = plt.figure(1, figsize=(15,8)) good_risetimes = [] likeCutoff = 20 simWfArr = np.empty((1,len(wfs), fitSamples)) nll = lambda *args: -lnlike_diffusion(*args) for (idx, wf) in enumerate(wfs): r, phi, z, scale, t0, smooth = results[idx]['x'] simWfArr[0,idx,:] = det.GetSimWaveform(r, phi, z, scale*100, t0, 200, smoothing=smooth) r_arr[idx], z_arr[idx], t0_arr[idx] = r,z,t0 # simWfArr[0,idx,:] = det.GetSimWaveform(r, phi, z, scale*100., t0, fitSamples, smoothing=smooth, electron_smoothing=esmooth) likes[idx] = results[idx]['fun'] / wf.wfLength risetimes[idx] = findTimePointBeforeMax(wf.windowedWf, 0.99) - wf.t0Guess time_since_last_arr[idx] = wfs_raw[idx].timeSinceLast last_energy_arr[idx] = wfs_raw[idx].energyLast # #cheap PZ correction # mgwf = MGWaveform() # mgwf.SetSamplingPeriod(10*1) # mgwf.SetLength(len(wf.waveformData)) # for i, wfVal in enumerate(wf.waveformData): # mgwf.SetValue(i, wfVal) # mgwfpz = MGWaveform() # pz.TransformOutOfPlace(mgwf, mgwfpz) # waveform = mgwfpz.GetVectorData() # waveform = np.multiply(waveform, 1) # wfToPlot = waveform # alignPoint = findTimePointBeforeMax(wfToPlot, 0.99) wfToPlot = wf.windowedWf if likes[idx] > 60: continue plt.plot(wfToPlot, color="orange") continue if likes[idx] > likeCutoff: continue plt.plot(wfToPlot, color="r") print "run number is wf %d" % wf.runNumber print ">>fit t0 is %f" % t0 simWf = det.GetSimWaveform(r, phi, z, scale*100, t0, fitSamples, smoothing=smooth) plt.plot(simWf, color="b") # # new_result = op.minimize(nll, [r, phi, z, scale, t0, smooth], args=(det, wf.windowedWf, wf.baselineRMS), method="Powell") # r, phi, z, scale, t0, smooth = new_result["x"] # new_simWf = det.GetSimWaveform(r, phi, z, scale*100, t0, fitSamples, smoothing=smooth) # print ">>new like is %f" % (new_result['fun'] / wf.windowedWf.size) # # plt.plot(new_simWf, color="g") else: if idx < 10: continue # print ">>old like is %f" % (results[idx]['fun'] / wf.windowedWf.size) # # new_result = op.minimize(nll, [r, phi, z, scale, t0, 2.], args=(det, wf.windowedWf, wf.baselineRMS), method="Powell") # r, phi, z, scale, t0, charge_cloud = new_result["x"] # new_simWf = det.GetSimWaveform(r, phi, z, scale*100, t0, fitSamples, energy=1592., charge_cloud_size=charge_cloud) # # # print ">>new like is %f" % (new_result['fun'] / wf.windowedWf.size) # # # gs = gridspec.GridSpec(2, 1, height_ratios=[4, 1]) # ax0 = plt.subplot(gs[0]) # ax1 = plt.subplot(gs[1], sharex=ax0) # ax1.set_xlabel("Digitizer Time [ns]") # ax0.set_ylabel("Voltage [Arb.]") # ax1.set_ylabel("Residual") # # ax0.plot(simWfArr[0,idx,:] ,color="blue", label="sim" ) # ax0.plot( wf.windowedWf ,color="red", label="data" ) # ax1.plot(wf.windowedWf-simWfArr[0,idx,:wf.windowedWf.size], color="b") # ax0.legend(loc=4) # # # break # plt.plot(wfToPlot , "g", alpha=0.1) # # if likes[idx] > likeCutoff: # plt.plot(np.arange(wf.windowedWf.size)*10, wf.windowedWf, color="r", ) # else: # plt.plot(np.arange(wf.windowedWf.size)*10, wf.windowedWf, color="g", alpha=0.05) plt.xlabel("time [ns]") plt.ylabel("energy [arb]") plt.plot(np.nan, color="r", label="bad fit :(") plt.plot(np.nan, color="g", label="good fit :)") plt.legend(loc=4) plt.savefig("512_all_waveforms.png") f2 = plt.figure(2, figsize=(15,8)) helpers.plotManyResidual(simWfArr[:15], wfs[:15], f2, residAlpha=1) plt.savefig("512_residuals.png") value = raw_input(' --> Press q to quit, any other key to continue\n') if value == 'q': exit(0) ''' plt.close(f1) plt.close(f2) good_idxs = np.where(likes <= likeCutoff) bad_idxs = np.where(likes > likeCutoff) f3 = plt.figure(3, figsize=(15,8)) helpers.plotManyResidual(simWfArr[0,bad_idxs], wfs[bad_idxs], f3, residAlpha=0.5) plt.savefig("512_badwfs.png") fig_timesincelast = plt.figure() plt.scatter(time_since_last_arr[good_idxs], last_energy_arr[good_idxs], color="g") plt.scatter(time_since_last_arr[bad_idxs], last_energy_arr[bad_idxs] , color="r") plt.xlabel("Time since last event") plt.ylabel("Energy [keV]") # fig_like = plt.figure() # plt.scatter(risetimes*10, likes) # plt.xlabel("Risetime [ns]") # plt.ylabel("NLL [normalized by wf length]") # plt.savefig("512_liklihoods.png") fig_pos = plt.figure() plt.scatter(r_arr[good_idxs], z_arr[good_idxs], color="g") plt.scatter(r_arr[bad_idxs], z_arr[bad_idxs], color="r") plt.xlim(0, det.detector_radius) plt.ylim(0, det.detector_length) plt.xlabel("Radial posion [mm from point contact]") plt.ylabel("Axial posion [mm above point contact]") plt.savefig("512_positions.png") fig = plt.figure() plt.hist(likes, bins="auto") plt.xlabel("NLL [normalized by wf length]") plt.savefig("512_likes.png") plt.xlim(0,50) plt.savefig("512_likes_zoom.png") figt0 = plt.figure() plt.hist(t0_arr*10, bins=20) plt.xlabel("Start Time [ns]") plt.savefig("512_times.png") value = raw_input(' --> Press q to quit, any other key to continue\n') if value == 'q': exit(0) ''' # numWfsToSave = 30 print "minimum risetime: %f" % np.min(risetimes) print "maximum risetime: %f" % np.max(risetimes) hist, bin_edges = np.histogram(risetimes, range=(30,100), bins=numWfsToSave) print bin_edges #bin the rise-times in 8 bins, from 30-100. Then, take enough from each to hit our waveform number goal. Try to maximize the number of low risetime events # So, say we want 32... equal would be 4 from each bin. Let's try that. # print np.where(risetimes < bin_edges[1]) idxlist_raw = [] for bin_idx in np.arange(len(bin_edges)-1): indices = np.nonzero( np.logical_and( np.greater(risetimes, bin_edges[bin_idx]), np.less_equal(risetimes, bin_edges[bin_idx+1]) ) )[0] for idx in indices: if likes[idx] < 5: idxlist_raw.append(idx) break else: print "Couldn't find any wfs with like <5 in bin %d" % bin_idx for idx in indices: if likes[idx] < 10: idxlist_raw.append(idx) break else: print "...Couldn't find any wfs with like <10 in bin %d either" % bin_idx idxlist = idxlist_raw plt.figure(figsize=(20,10)) wfsto_save = np.empty(len(bin_edges)-1, dtype=np.object) for idx in idxlist: plt.plot(wfs[idx].windowedWf, color="r") wfFileName = "P42574A_512waveforms_%drisetimeculled.npz" % numWfsToSave np.savez(wfFileName, wfs = wfs[idxlist], results=results[idxlist] ) value = raw_input(' --> Press q to quit, any other key to continue\n') if value == 'q': exit(0)
def main(argv): ################## #These change a lot numWaveforms = 16 numThreads = 4 ###################### plt.ion() channel = 626 aeCutVal = 0.01425 runRanges = [(13385, 13392), (13420, 13429)] #Good runs: #13385-92 #13420-8 #13551-7 #13690-7 (not yet downloaded anything past this) #13740-7 #13905-12 #13954-61 r_mult = 1. z_mult = 1. scale_mult = 100. fitSamples = 200 #Prepare detector num = [8685207069.0676746, 1.7618952141698222e+18, 17521485536930826.0] den = [1, 50310572.447231829, 701441983664560.88, 1.4012406413698292e+19] system = signal.lti(num, den) tempGuess = 82.48 gradGuess = 0.0482 pcRadGuess = 2.563885 pcLenGuess = 1.440751 #Create a detector model detName = "conf/P42574A_grad%0.2f_pcrad%0.2f_pclen%0.2f.conf" % (0.04, 2.5, 1.6) det = Detector(detName, temperature=tempGuess, timeStep=1., numSteps=fitSamples * 10, tfSystem=system) det.LoadFields("P42574A_fields_len.npz") det.SetFields(pcRadGuess, pcLenGuess, gradGuess) initializeDetector(det) p = Pool(numThreads, initializer=initializeDetector, initargs=[det]) wfFileName = "P42574A_%dwaveforms.npz" % numWaveforms if os.path.isfile(wfFileName): data = np.load(wfFileName) r_arr = data['r_arr'] phi_arr = data['phi_arr'] z_arr = data['z_arr'] scale_arr = data['scale_arr'] t0_arr = data['t0_arr'] smooth_arr = data['smooth_arr'] wfs = data['wfs'] else: print "No saved waveforms available. Loading from Data" #get waveforms cut = "trapECal>%f && trapECal<%f && TSCurrent100nsMax/trapECal > %f" % ( 1588, 1594, aeCutVal) wfs = helpers.GetWaveforms(runRanges, channel, numWaveforms, cut) for (idx, wf) in enumerate(wfs): wf.WindowWaveformTimepoint(fallPercentage=.99) #prep holders for each wf-specific param r_arr = np.empty(numWaveforms) phi_arr = np.empty(numWaveforms) z_arr = np.empty(numWaveforms) scale_arr = np.empty(numWaveforms) t0_arr = np.empty(numWaveforms) smooth_arr = np.empty(numWaveforms) simWfArr = np.empty((1, numWaveforms, fitSamples)) #parallel fit the waveforms with the initial detector params args = [] for (idx, wf) in enumerate(wfs): wf.WindowWaveformTimepoint(fallPercentage=.99) args.append([ 15. / r_mult, np.pi / 8., 15. / z_mult, wf.wfMax / scale_mult, wf.t0Guess, 10., wfs[idx] ]) # result = op.minimize(neg_lnlike_wf, [15./10., np.pi/8., 15./10., wf.wfMax/1000., wf.t0Guess, 10.], args=(wf, det) ,method="Nelder-Mead", tol=1.) # r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx] = result["x"] # print " >> wf %d (normalized likelihood %0.2f):" % (idx, result["fun"]/wfs[idx].wfLength) # print " r: %0.2f, phi: %0.3f, z: %0.2f, e: %0.2f, t0: %0.2f, smooth:%0.2f" % (r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx]) # # simWfArr[0,idx,:] = det.GetSimWaveform(r_arr[idx]*10, phi_arr[idx], z_arr[idx]*10, scale_arr[idx]*1000, t0_arr[idx], fitSamples, smoothing=smooth_arr[idx],) print "performing parallelized initial fit..." start = timer() results = p.map(minimize_waveform_only_star, args) end = timer() print "Initial fit time: %f" % (end - start) for (idx, result) in enumerate(results): r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[ idx], smooth_arr[idx] = result["x"] print " >> wf %d (normalized likelihood %0.2f):" % ( idx, result["fun"] / wfs[idx].wfLength) print " r: %0.2f, phi: %0.3f, z: %0.2f, e: %0.2f, t0: %0.2f, smooth:%0.2f" % ( r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx]) simWfArr[0, idx, :] = det.GetSimWaveform( r_arr[idx] * r_mult, phi_arr[idx], z_arr[idx] * z_mult, scale_arr[idx] * scale_mult, t0_arr[idx], fitSamples, smoothing=smooth_arr[idx], ) fig1 = plt.figure(figsize=(20, 15)) helpers.plotManyResidual(simWfArr, wfs, fig1, residAlpha=1) np.savez(wfFileName, wfs=wfs, r_arr=r_arr, phi_arr=phi_arr, z_arr=z_arr, scale_arr=scale_arr, t0_arr=t0_arr, smooth_arr=smooth_arr) value = raw_input(' --> Press q to quit, any other key to continue\n') if value == 'q': exit(0) init_wfs(wfs) #do the detector-wide fit if True: print "Starting detector MLE..." detmleFileName = "P42574A_%dwaveforms_detectormle.npz" % numWaveforms nll_det = lambda *args: -lnlike_detector(*args) num = [num[0] / 1E9, num[1] / 1E17, num[2] / 1E15] den = [1., den[1] / 1E7, den[2] / 1E14, den[3] / 1E18] tf_bound = 2. tempMult = 10. gradMult = 100. mcmc_startguess = np.hstack( (tempGuess / tempMult, gradGuess * gradMult, pcRadGuess, pcLenGuess, num[:], den[1:])) wfParams = np.hstack(( r_arr[:], phi_arr[:], z_arr[:], scale_arr[:], t0_arr[:], smooth_arr[:], )) bounds = [(78. / tempMult, 85. / tempMult), (det.gradList[0] * gradMult, det.gradList[-1] * gradMult), (det.pcRadList[0], det.pcRadList[-1]), (det.pcLenList[0], det.pcLenList[-1]), (num[0] / tf_bound, num[0] * tf_bound), (num[1] / tf_bound, num[1] * tf_bound), (num[2] / tf_bound, num[2] * tf_bound), (den[1] / tf_bound, den[1] * tf_bound), (den[2] / tf_bound, den[2] * tf_bound), (den[3] / tf_bound, den[3] * tf_bound)] #fitting only the 3 detector params start = timer() #result = op.basinhopping(nll_det, mcmc_startguess, accept_test=IsAllowableStep, T=20., stepsize=0.5, minimizer_kwargs={ "method":"Nelder-Mead", "tol":5., "args": (p, wfParams)}) #result = op.minimize(nll_det, mcmc_startguess, method="Nelder-Mead", tol=1., args=(p, wfParams)) #result = op.minimize(nll_det, mcmc_startguess, method="L-BFGS-B", tol=5, bounds=bounds, args=(p, wfParams)) result = op.differential_evolution(nll_det, bounds, args=(p, wfParams), polish=False) end = timer() print "Elapsed time: " + str(end - start) temp, impGrad, pcRad, pcLen = result["x"][0], result["x"][1], result[ "x"][2], result["x"][3] temp *= tempMult impGrad /= 100. tfStartIdx = 4 num = [ result["x"][tfStartIdx] * 1E9, result["x"][tfStartIdx + 1] * 1E17, result["x"][tfStartIdx + 2] * 1E15 ] den = [ 1, result["x"][tfStartIdx + 3] * 1E7, result["x"][tfStartIdx + 4] * 1E14, result["x"][tfStartIdx + 5] * 1E18 ] print "MLE Values..." print " >> temp is %f" % temp print " >> grad is %f" % impGrad print " >> pc rad is %f" % pcRad print " >> pc len is %f" % pcLen print " >> num = [%e, %e, %e]" % (num[0], num[1], num[2]) print " >> den = [1., %e, %e, %e]" % (den[1], den[2], den[3]) print "Plotting best fit..." fig2 = plt.figure(figsize=(20, 10)) det.SetTemperature(temp) det.SetFields(pcRad, pcLen, impGrad) det.SetTransferFunction(num, den) simWfArr = np.empty((1, numWaveforms, fitSamples)) #one last minimization for the plotzzz args = [] for idx in np.arange(numWaveforms): args.append([ r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx], wfs[idx] ]) results = p.map(minimize_waveform_only_star, args) for (idx, result) in enumerate(results): r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[ idx], smooth_arr[idx] = result["x"] print " >> wf %d (normalized likelihood %0.2f):" % ( idx, result["fun"] / wfs[idx].wfLength) print " r: %0.2f, phi: %0.3f, z: %0.2f, e: %0.2f, t0: %0.2f, smooth:%0.2f" % ( r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx]) simWfArr[0, idx, :] = det.GetSimWaveform(r_arr[idx] * 10, phi_arr[idx], z_arr[idx] * 10, scale_arr[idx] * 1000, t0_arr[idx], fitSamples, smoothing=smooth_arr[idx]) helpers.plotManyResidual(simWfArr, wfs, fig2, residAlpha=1) plt.savefig("mle_waveforms.pdf") np.savez(detmleFileName, wfs=wfs, r_arr=r_arr, phi_arr=phi_arr, z_arr=z_arr, scale_arr=scale_arr, t0_arr=t0_arr, smooth_arr=smooth_arr, temp=temp, impGrad=impGrad, pcRad=pcRad) value = raw_input(' --> Press q to quit, any other key to continue\n') exit(0)
def main(argv): plt.ion() fitSamples = 210 timeStepSize = 10. #ns numSamples = 10000 doInitPlot = False #Prepare detector zero_1 = 0.474472 pole_1 = 0.999845 pole_real = 0.807801 pole_imag = 0.081791 zeros = [zero_1, -1., 1. ] poles = [pole_1, pole_real+pole_imag*1j, pole_real-pole_imag*1j, ] tempGuess = 77.747244 gradGuess = 0.046950 pcRadGuess = 2.547418 pcLenGuess = 1.569172 #Create a detector model detName = "conf/P42574A_grad%0.2f_pcrad%0.2f_pclen%0.2f.conf" % (0.05,2.5, 1.65) det = Detector(detName, temperature=tempGuess, timeStep=timeStepSize, numSteps=fitSamples*10./timeStepSize, poles=poles, zeros=zeros) det.LoadFields("P42574A_fields_v3.npz") det.SetFields(pcRadGuess, pcLenGuess, gradGuess) wfFileName = "P42574A_512waveforms_8risetimeculled.npz" if os.path.isfile(wfFileName): data = np.load(wfFileName) results = data['results'] wfs = data['wfs'] numWaveforms = wfs.size else: print "No saved waveforms available. Loading from Data" exit(0) #prep holders for each wf-specific param r_arr = np.empty(numWaveforms) phi_arr = np.empty(numWaveforms) z_arr = np.empty(numWaveforms) scale_arr = np.empty(numWaveforms) t0_arr = np.empty(numWaveforms) smooth_arr = np.empty(numWaveforms) simWfArr = np.empty((1,numWaveforms, fitSamples)) for (idx, wf) in enumerate(wfs): wf.WindowWaveform(200) r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx] = results[idx]['x'] t0_arr[idx] += 10 #because i had a different windowing offset back in the day smooth_arr[idx] /= 10. #Plot the waveforms to take a look at the initial guesses if doInitPlot: plt.ion() fig = plt.figure() for (idx,wf) in enumerate(wfs): print "WF number %d:" % idx print " >>r: %f\n >>phi %f\n >>z %f\n >>e %f\n >>t0 %f\n >>smooth %f" % (r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx]) ml_wf = det.GetSimWaveform(r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx]*100, t0_arr[idx], fitSamples, smoothing = smooth_arr[idx]) plt.plot(ml_wf, color="b") plt.plot(wf.windowedWf, color="r") value = raw_input(' --> Press q to quit, any other key to continue\n') plt.ioff() if value == 'q': exit(0) startGuess = {'radEst': r_arr, 'zEst': z_arr, 'phiEst': phi_arr, 'wfScale': scale_arr*100, 'switchpoint': t0_arr, 'smooth': smooth_arr, 'temp': tempGuess, 'grad': gradGuess, 'pcRad': pcRadGuess, 'pcLen':pcLenGuess} siggen_model = sm3.CreateFullDetectorModel(det, wfs, startGuess, zero_1, pole_1, pole_real, pole_imag) with siggen_model: step = Metropolis(scaling = [np.ones(1)*1, np.ones(1)*5, np.ones(1)*np.pi/4/10, np.ones(1)*5, np.ones(1)*100, np.ones(1)*0.1, 3, 0.01, 0.0001, 0.01, 0.001, 0.005, 0.05, 0.05]) #step = Metropolis(scaling=0.001) trace = sample(numSamples, step = step, ) burnin = np.int(len(trace['temp'][:]) - 0.25*len(trace['temp'][:]))#no clue what to choose, for now, just make it 75% temp = np.median( trace['temp'][burnin:]) print "<<<detector temperature is %f" % temp grad = np.median( trace['grad'][burnin:]) pcRad= np.median( trace['pcRad'][burnin:]) pcLen= np.median( trace['pcLen'][burnin:]) print "<<<detector gradient is %0.4f " % (grad) print "<<<PC Radius is %0.4f, Length is %0.4f " % (pcRad, pcLen) zero_1 = np.median( trace['zero_1'][burnin:]) pole_1 = np.median( trace['pole_1'][burnin:]) pole_real = np.median( trace['pole_real'][burnin:]) pole_imag = np.median( trace['pole_imag'][burnin:]) print "<<< zero_1=%e" % zero_1 print "<<< pole_1=%e" % pole_1 print "<<< pole_real=%e"% pole_real print "<<< pole_imag=%e"% pole_imag plt.ioff() traceplot(trace) plt.savefig("hierarchical_full_%dchain.png" % len(wfs)) plt.ion() fig3 = plt.figure(3, figsize = (20,10)) plt.clf() plt.title("Charge waveform") plt.xlabel("Sample number [10s of ns]") plt.ylabel("Raw ADC Value [Arb]") wfPlotNumber = 10 simWfArr = np.empty((wfPlotNumber,numWaveforms, fitSamples)) for (sim_idx, chain_idx) in enumerate(np.random.randint(low=burnin, high=numSamples, size=wfPlotNumber)): temp = trace['temp'][chain_idx] grad = trace['grad'][chain_idx] pcRad= trace['pcRad'][chain_idx] pcLen= trace['pcLen'][chain_idx] zero_1 = trace['zero_1'][chain_idx] pole_1 = trace['pole_1'][chain_idx] pole_real = trace['pole_real'][chain_idx] pole_imag = trace['pole_imag'][chain_idx] zeros = [zero_1, -1., 1. ] poles = [pole_1, pole_real+pole_imag*1j, pole_real-pole_imag*1j, ] det.SetTransferFunction(zeros, poles) det.SetTemperature(temp) det.SetFields(pcRad, pcLen, grad) for (wf_idx, wf) in enumerate(wfs): t0 = trace['switchpoint'][chain_idx,wf_idx] r = trace['radEst'][chain_idx,wf_idx] z = trace['zEst'][chain_idx,wf_idx] phi = trace['phiEst'][chain_idx,wf_idx] scale = trace['wfScale'][chain_idx,wf_idx] sigma = trace['sigma'][chain_idx,wf_idx] simWfArr[sim_idx,wf_idx,:] = det.GetSimWaveform(r, phi, z, scale, t0, fitSamples, smoothing=sigma) helpers.plotManyResidual(simWfArr, wfs, fig3, residAlpha=1) # plt.savefig("hierarchical_full_%dwaveforms.png" % len(wfs)) summary(trace) value = raw_input(' --> Press q to quit, any other key to continue\n')
def main(argv): plt.ion() fitSamples = 210 timeStepSize = 10. #ns numSamples = 20000 burnin = 0.9*numSamples doInitPlot = False #Prepare detector zero_1 = 0.470677 pole_1 = 0.999857 pole_real = 0.807248 pole_imag = 0.085347 zeros = [zero_1, -1., 1. ] poles = [pole_1, pole_real+pole_imag*1j, pole_real-pole_imag*1j, ] tempGuess = 78.474793 gradGuess = 0.045049 pcRadGuess = 2.574859 pcLenGuess = 1.524812 #Create a detector model detName = "conf/P42574A_grad%0.2f_pcrad%0.2f_pclen%0.2f.conf" % (0.05,2.5, 1.65) det = Detector(detName, temperature=tempGuess, timeStep=timeStepSize, numSteps=fitSamples*10./timeStepSize, poles=poles, zeros=zeros) det.LoadFields("P42574A_fields_v3.npz") det.SetFields(pcRadGuess, pcLenGuess, gradGuess) wfFileName = "P42574A_512waveforms_30risetimeculled.npz" if os.path.isfile(wfFileName): data = np.load(wfFileName) results = data['results'] wfs = data['wfs'] numWaveforms = wfs.size else: print "No saved waveforms available. Go hard or go home." exit(0) #prep holders for each wf-specific param r_arr = np.empty(numWaveforms) phi_arr = np.empty(numWaveforms) z_arr = np.empty(numWaveforms) scale_arr = np.empty(numWaveforms) t0_arr = np.empty(numWaveforms) smooth_arr = np.empty(numWaveforms) simWfArr = np.empty((1,numWaveforms, fitSamples)) for (idx, wf) in enumerate(wfs): wf.WindowWaveform(200) r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx] = results[idx]['x'] t0_arr[idx] += 10 #because i had a different windowing offset back in the day smooth_arr[idx] /= 10. scale_arr[idx]*=100 #Plot the waveforms to take a look at the initial guesses if doInitPlot: plt.ion() fig = plt.figure() for (idx,wf) in enumerate(wfs): print "WF number %d:" % idx print " >>r: %f\n >>phi %f\n >>z %f\n >>e %f\n >>t0 %f\n >>smooth %f" % (r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx]) ml_wf = det.GetSimWaveform(r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], fitSamples, smoothing = smooth_arr[idx]) plt.plot(ml_wf, color="b") plt.plot(wf.windowedWf, color="r") value = raw_input(' --> Press q to quit, any other key to continue\n') plt.ioff() if value == 'q': exit(0) startGuess = {'radEst': r_arr, 'zEst': z_arr, 'phiEst': phi_arr, 'wfScale': scale_arr, 'switchpoint': t0_arr, 'smooth': smooth_arr, 'temp': tempGuess, 'grad': gradGuess, 'pcRad': pcRadGuess, 'pcLen':pcLenGuess} model_locals = sm.CreateFullDetectorModel(det, wfs, startGuess, zero_1, pole_1, pole_real, pole_imag) M = pymc.MCMC(model_locals, db='pickle', dbname='Detector.pickle') # M.sample(iter=100, burn=90) #12:30 pm 9/24 M.use_step_method(pymc.Slicer, M.grad, w = 0.03) M.use_step_method(pymc.Slicer, M.pcRad, w = 0.2) M.use_step_method(pymc.Slicer, M.pcLen, w=0.2) M.use_step_method(pymc.Slicer, M.tempEst, w=8) M.use_step_method(pymc.Slicer, M.zero_1, w=0.2) M.use_step_method(pymc.Slicer, M.pole_1, w=0.1) M.use_step_method(pymc.Slicer, M.pole_real, w=0.1) M.use_step_method(pymc.Slicer, M.pole_imag, w=0.01) # M.use_step_method(pymc.Metropolis, M.grad, proposal_sd=0.01, proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.pcRad, proposal_sd=0.05, proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.pcLen, proposal_sd=0.05, proposal_distribution='Normal') # # M.use_step_method(pymc.Metropolis, M.tempEst, proposal_sd=3., proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.zero_1, proposal_sd=0.5, proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.pole_1, proposal_sd=0.1, proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.pole_real, proposal_sd=0.5, proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.pole_imag, proposal_sd=0.1, proposal_distribution='Normal') for idx in range(numWaveforms): M.use_step_method(pymc.AdaptiveMetropolis, [M.radiusArray[idx], M.zArray[idx]], scales={M.radiusArray[idx]: 10, M.zArray[idx]: 10}, delay=100, interval=100,shrink_if_necessary=True) # M.use_step_method(pymc.Metropolis, M.radiusArray[idx], proposal_sd=10., proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.zArray[idx], proposal_sd=10., proposal_distribution='Normal') M.use_step_method(pymc.Metropolis, M.phiArray[idx], proposal_sd=0.3, proposal_distribution='Normal') M.use_step_method(pymc.Metropolis, M.scaleArray[idx], proposal_sd=0.01*startGuess['wfScale'][idx], proposal_distribution='Normal') M.use_step_method(pymc.Metropolis, M.t0Array[idx], proposal_sd=5, proposal_distribution='Normal') M.use_step_method(pymc.Metropolis, M.sigArray[idx], proposal_sd=0.5, proposal_distribution='Normal') # morning 9/24 # M.use_step_method(pymc.Metropolis, M.tempEst, proposal_sd=3., proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.grad, proposal_sd=0.005, proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.pcRad, proposal_sd=0.05, proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.pcLen, proposal_sd=0.05, proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.zero_1, proposal_sd=0.01, proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.pole_1, proposal_sd=0.001, proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.pole_real, proposal_sd=0.1, proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.pole_imag, proposal_sd=0.01, proposal_distribution='Normal') # # for idx in range(numWaveforms): # M.use_step_method(pymc.AdaptiveMetropolis, [M.radiusArray[idx], M.zArray[idx]], # scales={M.radiusArray[idx]: 10, # M.zArray[idx]: 10}, delay=100, interval=100,shrink_if_necessary=True) # # ## M.use_step_method(pymc.Metropolis, M.radiusArray[idx], proposal_sd=10., proposal_distribution='Normal') ## M.use_step_method(pymc.Metropolis, M.zArray[idx], proposal_sd=10., proposal_distribution='Normal') # # M.use_step_method(pymc.Metropolis, M.phiArray[idx], proposal_sd=0.3, proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.scaleArray[idx], proposal_sd=0.01*startGuess['wfScale'][idx], proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.t0Array[idx], proposal_sd=5, proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.sigArray[idx], proposal_sd=0.5, proposal_distribution='Normal') # # M.use_step_method(pymc.AdaptiveMetropolis, [M.tempEst, M.grad, M.pcRad, M.pcLen,M.zero_1,M.pole_1,M.pole_real,M.pole_imag], # scales={M.tempEst: .3, # M.grad: 0.001, # M.pcRad: 0.01, # M.pcLen: 0.01, # M.zero_1:0.01, # M.pole_1:0.001, # M.pole_real:0.01, # M.pole_imag:0.001 # }, delay=100, interval=100,shrink_if_necessary=True) # ## zero_1 = 0.474472 ## pole_1 = 0.999845 ## pole_real = 0.807801 ## pole_imag = 0.081791 ## # for idx in range(numWaveforms): # M.use_step_method(pymc.AdaptiveMetropolis, [M.radiusArray[idx],M.phiArray[idx],M.zArray[idx], # M.scaleArray[idx], M.t0Array[idx], M.sigArray[idx],], # scales={M.radiusArray[idx]: 10, # M.phiArray[idx]: np.pi/4/4, # M.zArray[idx]: 10, # M.scaleArray[idx]: 0.01*startGuess['wfScale'][idx], # M.t0Array[idx]: 5, # M.sigArray[idx]: 0.5 # }, delay=100, interval=100,shrink_if_necessary=True) M.sample(iter=numSamples, burn=0, tune_interval=100) M.db.close() # totalIter = 0 # while totalIter < this_sample: # M.sample(iter=10, verbose=0) # totalIter += 10 # #pymc.Matplot.plot(M) # # ######### Plots for MC Steps stepsFig = plt.figure(figsize=(20,10)) plt.clf() ax0 = stepsFig.add_subplot(611) ax1 = stepsFig.add_subplot(612, sharex=ax0) ax2 = stepsFig.add_subplot(613, sharex=ax0) ax3 = stepsFig.add_subplot(614, sharex=ax0) ax4 = stepsFig.add_subplot(615, sharex=ax0) ax5 = stepsFig.add_subplot(616, sharex=ax0) ax0.set_ylabel('r') ax1.set_ylabel('z') ax2.set_ylabel('phi') ax3.set_ylabel('e') ax4.set_ylabel('t0') ax5.set_ylabel('sig') for i in range(len(wfs)): ax0.plot(M.trace('radEst_%d'%i)[:]) ax1.plot(M.trace('zEst_%d'%i)[:]) ax2.plot(M.trace('phiEst_%d'%i)[:]) ax3.plot(M.trace('wfScale_%d'%i)[:]) ax4.plot(M.trace('switchpoint_%d'%i)[:]) ax5.plot(M.trace('sigma_%d'%i)[:]) plt.savefig("pymc_wf_params.png") # stepsFig2 = plt.figure(figsize=(20,10)) plt.clf() ax0 = stepsFig2.add_subplot(811) ax1 = stepsFig2.add_subplot(812, sharex=ax0) ax2 = stepsFig2.add_subplot(813, sharex=ax0) ax3 = stepsFig2.add_subplot(814, sharex=ax0) ax4 = stepsFig2.add_subplot(815, sharex=ax0) ax5 = stepsFig2.add_subplot(816, sharex=ax0) ax6 = stepsFig2.add_subplot(817, sharex=ax0) ax7 = stepsFig2.add_subplot(818, sharex=ax0) ax0.set_ylabel('zero_1') ax1.set_ylabel('pole_1') ax2.set_ylabel('pole_real') ax3.set_ylabel('pole_imag') ax4.set_ylabel('temp') ax5.set_ylabel('grad') ax6.set_ylabel('pcRad') ax7.set_ylabel('pcLen') ax0.plot(M.trace('zero_1')[:]) ax1.plot(M.trace('pole_1')[:]) ax2.plot(M.trace('pole_real')[:]) ax3.plot(M.trace('pole_imag')[:]) ax4.plot(M.trace('temp')[:]) ax5.plot(M.trace('grad')[:]) ax6.plot(M.trace('pcRad')[:]) ax7.plot(M.trace('pcLen')[:]) plt.savefig("pymc_detector.png") fig3 = plt.figure(3, figsize = (20,10)) plt.clf() plt.title("Charge waveform") plt.xlabel("Sample number [10s of ns]") plt.ylabel("Raw ADC Value [Arb]") wfPlotNumber = 10 simWfArr = np.empty((wfPlotNumber,numWaveforms, fitSamples)) if burnin > len(M.trace('temp')[:]): burnin = len(M.trace('temp')[:]) - wfPlotNumber numSamples = len(M.trace('temp')[:]) for (sim_idx, chain_idx) in enumerate(np.random.randint(low=burnin, high=numSamples, size=wfPlotNumber)): temp = M.trace('temp')[chain_idx] grad = M.trace('grad')[chain_idx] pcRad= M.trace('pcRad')[chain_idx] pcLen= M.trace('pcLen')[chain_idx] zero_1 = M.trace('zero_1')[chain_idx] pole_1 = M.trace('pole_1')[chain_idx] pole_real = M.trace('pole_real')[chain_idx] pole_imag = M.trace('pole_imag')[chain_idx] zeros = [zero_1, -1., 1. ] poles = [pole_1, pole_real+pole_imag*1j, pole_real-pole_imag*1j, ] det.SetTransferFunction(zeros, poles) det.SetTemperature(temp) det.SetFields(pcRad, pcLen, grad) for (wf_idx, wf) in enumerate(wfs): t0 = M.trace('switchpoint_%d' % wf_idx)[chain_idx] r = M.trace('radEst_%d' % wf_idx)[chain_idx] z = M.trace('zEst_%d' % wf_idx)[chain_idx] phi = M.trace('phiEst_%d' % wf_idx)[chain_idx] scale = M.trace('wfScale_%d' % wf_idx)[chain_idx] sigma = M.trace('sigma_%d' % wf_idx)[chain_idx] simWfArr[sim_idx,wf_idx,:] = det.GetSimWaveform(r, phi, z, scale, t0, fitSamples, smoothing=sigma) helpers.plotManyResidual(simWfArr, wfs, fig3, residAlpha=1) plt.savefig("pymc_waveforms.png") value = raw_input(' --> Press q to quit, any other key to continue\n')
def main(argv): ################## #These change a lot numWaveforms = 16 numThreads = 12 ndim = 6 * numWaveforms + 8 nwalkers = 2 * ndim iter = 50 burnIn = 40 wfPlotNumber = 10 ###################### # plt.ion() fitSamples = 200 #Prepare detector zero_1 = -5.56351644e+07 pole_1 = -1.38796386e+04 pole_real = -2.02559385e+07 pole_imag = 9885315.37450211 zeros = [zero_1, 0] poles = [pole_real + pole_imag * 1j, pole_real - pole_imag * 1j, pole_1] system = signal.lti(zeros, poles, 1E7) tempGuess = 77.89 gradGuess = 0.0483 pcRadGuess = 2.591182 pcLenGuess = 1.613357 #Create a detector model detName = "conf/P42574A_grad%0.2f_pcrad%0.2f_pclen%0.2f.conf" % (0.05, 2.5, 1.65) det = Detector(detName, temperature=tempGuess, timeStep=1., numSteps=fitSamples * 10, tfSystem=system) det.LoadFields("P42574A_fields_v3.npz") det.SetFields(pcRadGuess, pcLenGuess, gradGuess) tempIdx = -8 gradIdx = -7 pcRadIdx = -6 pcLenIdx = -5 #and the remaining 4 are for the transfer function fig_size = (20, 10) #Create a decent start guess by fitting waveform-by-waveform wfFileName = "P42574A_512waveforms_%drisetimeculled.npz" % numWaveforms if os.path.isfile(wfFileName): data = np.load(wfFileName) results = data['results'] wfs = data['wfs'] numWaveforms = wfs.size else: print "No saved waveforms available. Loading from Data" exit(0) #prep holders for each wf-specific param r_arr = np.empty(numWaveforms) phi_arr = np.empty(numWaveforms) z_arr = np.empty(numWaveforms) scale_arr = np.empty(numWaveforms) t0_arr = np.empty(numWaveforms) smooth_arr = np.ones(numWaveforms) * 7. simWfArr = np.empty((1, numWaveforms, fitSamples)) #Prepare the initial value arrays for (idx, wf) in enumerate(wfs): wf.WindowWaveformTimepoint(fallPercentage=.99) r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[ idx], smooth_arr[idx] = results[idx]['x'] t0_arr[ idx] += 10 #because i had a different windowing offset back in the day #Plot the waveforms to take a look at the initial guesses if False: fig = plt.figure() for (idx, wf) in enumerate(wfs): print "WF number %d:" % idx print " >>r: %f\n >>phi %f\n >>z %f\n >>e %f\n >>t0 %f\n >>smooth %f" % ( r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx]) ml_wf = det.GetSimWaveform(r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx] * 100, t0_arr[idx], fitSamples, smoothing=smooth_arr[idx]) plt.plot(ml_wf, color="b") plt.plot(wf.windowedWf, color="r") value = raw_input(' --> Press q to quit, any other key to continue\n') if value == 'q': exit(0) #Initialize this thread's globals initializeDetectorAndWaveforms(det, wfs) #Initialize the multithreading pool = MPIPool() if not pool.is_master(): pool.wait() sys.exit(0) #Do the MCMC mcmc_startguess = np.hstack(( r_arr[:], phi_arr[:], z_arr[:], scale_arr[:] * 100., t0_arr[:], smooth_arr[:], # waveform-specific params tempGuess, gradGuess, pcRadGuess, pcLenGuess, zero_1, pole_1, pole_real, pole_imag)) # detector-specific #number of walkers _must_ be even if nwalkers % 2: nwalkers += 1 #Initialize walkers with a random, narrow ball around the start guess pos0 = [ mcmc_startguess + 1e-2 * np.random.randn(ndim) * mcmc_startguess for i in range(nwalkers) ] #Make sure everything in the initial guess is within bounds for pos in pos0: pos[:numWaveforms] = np.clip(pos[:numWaveforms], 0, np.floor(det.detector_radius * 10.) / 10.) pos[numWaveforms:2 * numWaveforms] = np.clip( pos[numWaveforms:2 * numWaveforms], 0, np.pi / 4) pos[2 * numWaveforms:3 * numWaveforms] = np.clip( pos[2 * numWaveforms:3 * numWaveforms], 0, np.floor(det.detector_length * 10.) / 10.) pos[4 * numWaveforms:5 * numWaveforms] = np.clip( pos[4 * numWaveforms:5 * numWaveforms], 0, fitSamples) pos[5 * numWaveforms:6 * numWaveforms] = np.clip( pos[5 * numWaveforms:6 * numWaveforms], 0, 20.) pos[tempIdx] = np.clip(pos[tempIdx], 40, 120) pos[gradIdx] = np.clip(pos[gradIdx], det.gradList[0], det.gradList[-1]) pos[pcRadIdx] = np.clip(pos[pcRadIdx], det.pcRadList[0], det.pcRadList[-1]) pos[pcLenIdx] = np.clip(pos[pcLenIdx], det.pcLenList[0], det.pcLenList[-1]) prior = lnprior(pos, ) if not np.isfinite(prior): print "BAD PRIOR WITH START GUESS YOURE KILLING ME SMALLS" print pos exit(0) #Initialize, run the MCMC sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob, pool=p) #w/ progress bar, & time the thing start = timer() for (idx, result) in enumerate( sampler.sample(pos0, iterations=iter, storechain=True)): continue end = timer() pool.close() print "Elapsed time: " + str(end - start) print "Dumping chain to file..." np.save("sampler_mpi_%dwfs.npy" % numWaveforms, sampler.chain) print "Making MCMC steps figure..." ######### Plots for Waveform params stepsFig = plt.figure(2, figsize=fig_size) plt.clf() ax0 = stepsFig.add_subplot(611) ax1 = stepsFig.add_subplot(612, sharex=ax0) ax2 = stepsFig.add_subplot(613, sharex=ax0) ax3 = stepsFig.add_subplot(614, sharex=ax0) ax4 = stepsFig.add_subplot(615, sharex=ax0) ax5 = stepsFig.add_subplot(616, sharex=ax0) ax0.set_ylabel('r') ax1.set_ylabel('phi') ax2.set_ylabel('z') ax3.set_ylabel('scale') ax4.set_ylabel('t0') ax5.set_ylabel('smoothing') for i in range(nwalkers): for j in range(wfs.size): ax0.plot(sampler.chain[i, :, 0 + j], alpha=0.3) # r ax1.plot(sampler.chain[i, :, numWaveforms + j], alpha=0.3) # phi ax2.plot(sampler.chain[i, :, 2 * numWaveforms + j], alpha=0.3) #z ax3.plot(sampler.chain[i, :, 3 * numWaveforms + j], alpha=0.3) #energy ax4.plot(sampler.chain[i, :, 4 * numWaveforms + j], alpha=0.3) #t0 ax5.plot(sampler.chain[i, :, 5 * numWaveforms + j], alpha=0.3) #smoothing plt.savefig("emcee_mpi_wfchain_%dwfs.png" % numWaveforms) ######### Plots for Detector params stepsFigDet = plt.figure(3, figsize=fig_size) plt.clf() ax0 = stepsFigDet.add_subplot(411) ax1 = stepsFigDet.add_subplot(412, sharex=ax0) ax2 = stepsFigDet.add_subplot(413, sharex=ax0) ax3 = stepsFigDet.add_subplot(414, sharex=ax0) ax0.set_ylabel('temp') ax1.set_ylabel('grad') ax2.set_ylabel('pcRad') ax3.set_ylabel('pcLen') for i in range(nwalkers): ax0.plot(sampler.chain[i, :, tempIdx], "b", alpha=0.3) #temp ax1.plot(sampler.chain[i, :, gradIdx], "b", alpha=0.3) #grad ax2.plot(sampler.chain[i, :, pcRadIdx], "b", alpha=0.3) #pcrad ax3.plot(sampler.chain[i, :, pcLenIdx], "b", alpha=0.3) #pclen plt.savefig("emcee_mpi_detchain_%dwfs.png" % numWaveforms) #and for the transfer function stepsFigTF = plt.figure(4, figsize=fig_size) plt.clf() tf0 = stepsFigTF.add_subplot(411) tf1 = stepsFigTF.add_subplot(412, sharex=ax0) tf2 = stepsFigTF.add_subplot(413, sharex=ax0) tf3 = stepsFigTF.add_subplot(414, sharex=ax0) tf0.set_ylabel('zero_1') tf1.set_ylabel('pole_1') tf2.set_ylabel('pole_real') tf3.set_ylabel('pole_imag') for i in range(nwalkers): tf0.plot(sampler.chain[i, :, -4], "b", alpha=0.3) #2 tf1.plot(sampler.chain[i, :, -3], "b", alpha=0.3) #den1 tf2.plot(sampler.chain[i, :, -2], "b", alpha=0.3) #2 tf3.plot(sampler.chain[i, :, -1], "b", alpha=0.3) #3 plt.savefig("emcee_mpi_tfchain_%dwfs.png" % numWaveforms) samples = sampler.chain[:, burnIn:, :].reshape((-1, ndim)) print "temp is %f" % np.median(samples[:, tempIdx]) print "grad is %f" % np.median(samples[:, gradIdx]) print "pcrad is %f" % np.median(samples[:, pcRadIdx]) print "pclen is %f" % np.median(samples[:, pcLenIdx]) print "zero_1 is %f" % np.median(samples[:, -4]) print "pole_1 is %f" % np.median(samples[:, -3]) print "pole_real is %f" % np.median(samples[:, -2]) print "pole_imag is %f" % np.median(samples[:, -1]) #TODO: Aaaaaaand plot some waveforms.. simWfs = np.empty((wfPlotNumber, numWaveforms, fitSamples)) for idx, (theta) in enumerate(samples[np.random.randint( len(samples), size=wfPlotNumber)]): temp, impGrad, pcRad, pcLen = theta[tempIdx], theta[gradIdx], theta[ pcRadIdx], theta[pcLenIdx] zero_1, pole_1, pole_real, pole_imag = theta[-4:] r_arr, phi_arr, z_arr, scale_arr, t0_arr, smooth_arr = theta[:-8].reshape( (6, numWaveforms)) det.SetTemperature(temp) det.SetFields(pcRad, pcLen, impGrad) zeros = [zero_1, 0] poles = [ pole_real + pole_imag * 1j, pole_real - pole_imag * 1j, pole_1 ] det.SetTransferFunction(zeros, poles, 1E7) for wf_idx in range(wfs.size): wf_i = det.GetSimWaveform(r_arr[wf_idx], phi_arr[wf_idx], z_arr[wf_idx], scale_arr[wf_idx], t0_arr[wf_idx], fitSamples) simWfs[idx, wf_idx, :] = wf_i if wf_i is None: print "Waveform %d, %d is None" % (idx, wf_idx) residFig = plt.figure(4, figsize=(20, 15)) helpers.plotManyResidual(simWfs, wfs, figure=residFig) plt.savefig("emcee_mpi_waveforms_%dwfs.png" % numWaveforms)
def main(): ################## #These change a lot numWaveforms = 2 numThreads = 8 ndim = 6*numWaveforms + 13 nwalkers = 100*ndim iter=50000 burnIn = 49000 wfPlotNumber = 100 ###################### doPlots = 1 # plt.ion() fitSamples = 200 timeStepSize = 1. #ns #Prepare detector tempGuess = 79.310080 gradGuess = 0.04 pcRadGuess = 2.5 pcLenGuess = 1.6 #Create a detector model detName = "conf/P42574A_grad%0.2f_pcrad%0.2f_pclen%0.2f.conf" % (0.05,2.5, 1.65) det = Detector(detName, temperature=tempGuess, timeStep=timeStepSize, numSteps=fitSamples*10 ) det.LoadFields("P42574A_fields_v3.npz") det.SetFields(pcRadGuess, pcLenGuess, gradGuess) b_over_a = 0.107213 c = -0.815152 d = 0.822696 rc1 = 74.4 rc2 = 1.79 rcfrac = 0.992 trapping_rc = 120#us det.SetTransferFunction(b_over_a, c, d, rc1, rc2, rcfrac) det.trapping_rc = trapping_rc det.siggenInst.set_velocity_type(1) h_100_mu0, h_100_beta, h_100_e0, h_111_mu0, h_111_beta, h_111_e0 = 66333., 0.744, 181., 107270., 0.580, 100. mlw.initializeDetector(det, ) #and the remaining 6 are for the transfer function fig_size = (20,10) #Create a decent start guess by fitting waveform-by-waveform wfFileName = "P42574A_12_fastandslow_oldwfs.npz" # wfFileName = "P42574A_24_spread.npz" # wfFileName = "P42574A_5_fast.npz" if os.path.isfile(wfFileName): data = np.load(wfFileName) results = data['results'] wfs = data['wfs'] # wfs = np.delete(wfs, [2]) # results = np.delete(results, [2]) # results = results[::3] idxs = [4,5] wfs = wfs[idxs] results = results[idxs] numWaveforms = wfs.size else: print "No saved waveforms available. Exiting." exit(0) #prep holders for each wf-specific param r_arr = np.empty(numWaveforms) phi_arr = np.empty(numWaveforms) z_arr = np.empty(numWaveforms) scale_arr = np.empty(numWaveforms) t0_arr = np.empty(numWaveforms) smooth_arr = np.ones(numWaveforms)*7. simWfArr = np.empty((1,numWaveforms, fitSamples)) #Prepare the initial value arrays # plt.ion() # fig = plt.figure() for (idx, wf) in enumerate(wfs): wf.WindowWaveformTimepoint(fallPercentage=.99, rmsMult=2,) r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx] = results[idx]['x'] # plt.plot(wf.windowedWf) # value = raw_input(' --> Press q to quit, any other key to continue\n') # t0_arr[idx] -= 15 #Plot the waveforms to take a look at the initial guesses if True: plt.ion() fig1 = plt.figure(1) plt.clf() gs = gridspec.GridSpec(2, 1, height_ratios=[4, 1]) ax0 = plt.subplot(gs[0]) ax1 = plt.subplot(gs[1], sharex=ax0) ax1.set_xlabel("Digitizer Time [ns]") ax0.set_ylabel("Voltage [Arb.]") ax1.set_ylabel("Residual") for (idx,wf) in enumerate(wfs): print "WF number %d:" % idx mlw.initializeWaveform(wf) dataLen = wf.wfLength t_data = np.arange(dataLen) * 10 ax0.plot(t_data, wf.windowedWf, color="r") # minresult = None # minlike = np.inf # # for r in np.linspace(4, np.floor(det.detector_radius)-3, 6): # for z in np.linspace(4, np.floor(det.detector_length)-3, 6): # # for t0_guess in np.linspace(wf.t0Guess-10, wf.t0Guess+5, 3): # if not det.IsInDetector(r,0,z): continue # startGuess = [r, np.pi/8, z, wf.wfMax, wf.t0Guess-5, 10] # result = op.minimize(nll, startGuess, method="Nelder-Mead") # r, phi, z, scale, t0, smooth, = result["x"] # ml_wf = np.copy(det.MakeSimWaveform(r, phi, z, scale, t0, fitSamples, h_smoothing=smooth, )) # if ml_wf is None: # print r, z # continue # if result['fun'] < minlike: # minlike = result['fun'] # minresult = result # r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx] = minresult['x'] r, phi, z, scale, t0, smooth = results[idx]['x'] startGuess = [r, phi, z, scale, t0, smooth] result = op.minimize(nll, startGuess, method="Powell") r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx] = result['x'] print " >>r: %f\n >>phi %f\n >>z %f\n >>e %f\n >>t0 %f\n >>smooth %f" % (r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx]) ml_wf = det.MakeSimWaveform(r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], fitSamples, h_smoothing = smooth_arr[idx]) ax0.plot(t_data, ml_wf[:dataLen], color="g") ax1.plot(t_data, ml_wf[:dataLen] - wf.windowedWf, color="g",) value = raw_input(' --> Press q to quit, any other key to continue\n') plt.ioff() if value == 'q': exit(0) #Initialize the multithreading p = Pool(numThreads, initializer=initializeDetectorAndWaveforms, initargs=[det, wfs]) initializeDetectorAndWaveforms(det, wfs) #Do the MCMC mcmc_startguess = np.hstack((r_arr[:], phi_arr[:], z_arr[:], scale_arr[:], t0_arr[:], smooth_arr[:], # waveform-specific params trapping_rc, b_over_a, c, d, rc1, rc2, rcfrac, h_100_mu0, h_100_beta, h_100_e0, h_111_mu0, h_111_beta, h_111_e0)) # detector-specific #number of walkers _must_ be even if nwalkers % 2: nwalkers +=1 pos0 = [mcmc_startguess + 1e-2*np.random.randn(ndim)*mcmc_startguess for i in range(nwalkers)] trapIdx = -13 rc1idx = -9 rc2idx = -8 rcfracidx = -7 #Make sure everything in the initial guess is within bounds for pos in pos0: pos[:numWaveforms] = np.clip( pos[:numWaveforms], 0, np.floor(det.detector_radius*10.)/10.) pos[numWaveforms:2*numWaveforms] = np.clip(pos[numWaveforms:2*numWaveforms], 0, np.pi/4) pos[2*numWaveforms:3*numWaveforms] = np.clip(pos[2*numWaveforms:3*numWaveforms], 0, np.floor(det.detector_length*10.)/10.) pos[4*numWaveforms:5*numWaveforms] = np.clip(pos[4*numWaveforms:5*numWaveforms], 0, fitSamples) pos[5*numWaveforms:6*numWaveforms] = np.clip(pos[5*numWaveforms:6*numWaveforms], 0, 20.) # pos[tempIdx] = np.clip(pos[tempIdx], 40, 120) pos[trapIdx] = np.clip(pos[trapIdx], 0, np.inf) pos[rcfracidx] = np.clip(pos[rcfracidx], 0, 1) pos[rc2idx] = np.clip(pos[rc2idx], 0, np.inf) pos[rc1idx] = np.clip(pos[rc1idx], 0, np.inf) prior = lnprior(pos,) if not np.isfinite(prior) : print "BAD PRIOR WITH START GUESS YOURE KILLING ME SMALLS" print pos exit(0) #Initialize, run the MCMC sampler = emcee.EnsembleSampler( nwalkers, ndim, lnprob, pool=p) #w/ progress bar, & time the thing bar = ProgressBar(widgets=[Percentage(), Bar(), ETA()], maxval=iter).start() for (idx,result) in enumerate(sampler.sample(pos0, iterations=iter, storechain=True)): bar.update(idx+1) bar.finish() if not doPlots: exit(0) print "Dumping chain to file..." np.save("sampler_%dwfs.npy" % numWaveforms, sampler.chain) print "Making MCMC steps figure..." # ######### Plots for Waveform params # stepsFig = plt.figure(2, figsize=fig_size) # plt.clf() # ax0 = stepsFig.add_subplot(611) # ax1 = stepsFig.add_subplot(612, sharex=ax0) # ax2 = stepsFig.add_subplot(613, sharex=ax0) # ax3 = stepsFig.add_subplot(614, sharex=ax0) # ax4 = stepsFig.add_subplot(615, sharex=ax0) # ax5 = stepsFig.add_subplot(616, sharex=ax0) # # ax0.set_ylabel('r') # ax1.set_ylabel('phi') # ax2.set_ylabel('z') # ax3.set_ylabel('scale') # ax4.set_ylabel('t0') # ax5.set_ylabel('smoothing') # # for i in range(nwalkers): # for j in range(wfs.size): # ax0.plot(sampler.chain[i,:,0+j], alpha=0.3) # r # ax1.plot(sampler.chain[i,:,numWaveforms + j], alpha=0.3) # phi # ax2.plot(sampler.chain[i,:,2*numWaveforms + j], alpha=0.3) #z # ax3.plot(sampler.chain[i,:,3*numWaveforms + j], alpha=0.3) #energy # ax4.plot(sampler.chain[i,:,4*numWaveforms + j], alpha=0.3) #t0 # ax5.plot(sampler.chain[i,:,5*numWaveforms + j], alpha=0.3) #smoothing # # plt.savefig("emcee_wfchain_%dwfs.png" % numWaveforms) # # # #and for the transfer function stepsFigTF = plt.figure(7, figsize=fig_size) plt.clf() tf0 = stepsFigTF.add_subplot(711) tf1 = stepsFigTF.add_subplot(712, sharex=tf0) tf2 = stepsFigTF.add_subplot(713, sharex=tf0) tf3 = stepsFigTF.add_subplot(714, sharex=tf0) tf4 = stepsFigTF.add_subplot(715, sharex=tf0) tf5 = stepsFigTF.add_subplot(716, sharex=tf0) tf6 = stepsFigTF.add_subplot(717, sharex=tf0) # tf7 = stepsFigTF.add_subplot(818, sharex=tf0) tf0.set_ylabel('b_over_a') tf1.set_ylabel('c') tf2.set_ylabel('d') tf3.set_ylabel('rc1') tf4.set_ylabel('rc2') tf5.set_ylabel('rcfrac') tf6.set_ylabel('temp') # tf7.set_ylabel('trapping') for i in range(nwalkers): tf0.plot(sampler.chain[i,:,trapIdx+1], "b", alpha=0.3) #2 tf1.plot(sampler.chain[i,:,trapIdx+2], "b", alpha=0.3) #den1 tf2.plot(sampler.chain[i,:,trapIdx+3], "b", alpha=0.3) #2 tf3.plot(sampler.chain[i,:,trapIdx+4], "b", alpha=0.3) #3 tf4.plot(sampler.chain[i,:,trapIdx+5], "b", alpha=0.3) #3 tf5.plot(sampler.chain[i,:,trapIdx+6], "b", alpha=0.3) #3 tf6.plot(sampler.chain[i,:,trapIdx], "b", alpha=0.3) #3 # tf6.plot(sampler.chain[i,:,trapIdx], "b", alpha=0.3) #3 plt.savefig("emcee_tfchain_%dwfs.png" % numWaveforms) stepsFigTF = plt.figure(6, figsize=fig_size) plt.clf() tf0 = stepsFigTF.add_subplot(611) tf1 = stepsFigTF.add_subplot(612, sharex=tf0) tf2 = stepsFigTF.add_subplot(613, sharex=tf0) tf3 = stepsFigTF.add_subplot(614, sharex=tf0) tf4 = stepsFigTF.add_subplot(615, sharex=tf0) tf5 = stepsFigTF.add_subplot(616, sharex=tf0) # tf6 = stepsFigTF.add_subplot(717, sharex=tf0) # tf7 = stepsFigTF.add_subplot(818, sharex=tf0) tf0.set_ylabel('1') tf1.set_ylabel('2') tf2.set_ylabel('3') tf3.set_ylabel('4') tf4.set_ylabel('5') tf5.set_ylabel('6') # tf6.set_ylabel('temp') # tf7.set_ylabel('trapping') for i in range(nwalkers): tf0.plot(sampler.chain[i,:,trapIdx+7], "b", alpha=0.3) #2 tf1.plot(sampler.chain[i,:,trapIdx+8], "b", alpha=0.3) #den1 tf2.plot(sampler.chain[i,:,trapIdx+9], "b", alpha=0.3) #2 tf3.plot(sampler.chain[i,:,trapIdx+10], "b", alpha=0.3) #3 tf4.plot(sampler.chain[i,:,trapIdx+11], "b", alpha=0.3) #3 tf5.plot(sampler.chain[i,:,trapIdx+12], "b", alpha=0.3) #3 # tf6.plot(sampler.chain[i,:,trapIdx], "b", alpha=0.3) #3 # tf6.plot(sampler.chain[i,:,trapIdx], "b", alpha=0.3) #3 plt.savefig("emcee_velochain_%dwfs.png" % numWaveforms) samples = sampler.chain[:, burnIn:, :].reshape((-1, ndim)) stepsFigTF = plt.figure(5, figsize = (20,10)) plotnum = 600 tf0 = stepsFigTF.add_subplot(plotnum+11) tf1 = stepsFigTF.add_subplot(plotnum+12, ) tf2 = stepsFigTF.add_subplot(plotnum+13, ) tf3 = stepsFigTF.add_subplot(plotnum+14, ) tf4 = stepsFigTF.add_subplot(plotnum+15, ) tf5 = stepsFigTF.add_subplot(plotnum+16, ) tf0.set_ylabel('h_100_mu0') tf1.set_ylabel('h_100_beta') tf2.set_ylabel('h_100_e0') tf3.set_ylabel('h_111_mu0') tf4.set_ylabel('h_111_beta') tf5.set_ylabel('h_111_e0') num_bins = 300 [n, b, p] = tf0.hist(samples[:,-6], bins=num_bins) print "h_100_mu0 mode is %f" % b[np.argmax(n)] [n, b, p] = tf1.hist(samples[:,-5],bins=num_bins) print "h_100_beta mode is %f" % b[np.argmax(n)] [n, b, p] = tf2.hist(samples[:,-4],bins=num_bins) print "h_100_e0 mode is %f" % b[np.argmax(n)] [n, b, p] = tf3.hist(samples[:,-3],bins=num_bins) print "h_111_mu0 mode is %f" % b[np.argmax(n)] [n, b, p] = tf4.hist(samples[:,-2],bins=num_bins) print "h_111_beta mode is %f" % b[np.argmax(n)] [n, b, p] = tf5.hist(samples[:,-1],bins=num_bins) print "h_111_e0 mode is %f" % b[np.argmax(n)] # print "temp is %f" % np.median(samples[:,tempIdx]) print "trapping is %f" % np.median(samples[:,trapIdx]) print "b_over_a is %f" % np.median(samples[:,-6]) print "c is %f" % np.median(samples[:,-5]) print "d is %f" % np.median(samples[:,-4]) print "rc_decay1 is %f" % np.median(samples[:,-3]) print "rc_decay2 is %f" % np.median(samples[:,-2]) print "rc_frac is %f" % np.median(samples[:,-1]) #TODO: Aaaaaaand plot some waveforms.. simWfs = np.empty((wfPlotNumber,numWaveforms, fitSamples)) for idx, (theta) in enumerate(samples[np.random.randint(len(samples), size=wfPlotNumber)]): # temp = theta[tempIdx] # trapping_rc = theta[trapIdx] trapping_rc, b_over_a, c, d, rc1, rc2, rcfrac, h_100_mu0, h_100_beta, h_100_e0, h_111_mu0, h_111_beta, h_111_e0, = theta[-13:] r_arr, phi_arr, z_arr, scale_arr, t0_arr, smooth_arr = theta[:-13].reshape((6, numWaveforms)) det.siggenInst.set_hole_params(h_100_mu0, h_100_beta, h_100_e0, h_111_mu0, h_111_beta, h_111_e0) # det.SetTemperature(temp) det.trapping_rc = trapping_rc det.SetTransferFunction(b_over_a, c, d, rc1, rc2, rcfrac) for wf_idx in range(numWaveforms): wf_i = det.MakeSimWaveform(r_arr[wf_idx], phi_arr[wf_idx], z_arr[wf_idx], scale_arr[wf_idx], t0_arr[wf_idx], fitSamples, h_smoothing=smooth_arr[wf_idx] ) simWfs[idx, wf_idx, :] = wf_i if wf_i is None: print "Waveform %d, %d is None" % (idx, wf_idx) residFig = plt.figure(4, figsize=(20, 15)) helpers.plotManyResidual(simWfs, wfs, figure=residFig) plt.savefig("emcee_waveforms_%dwfs.png" % numWaveforms) value = raw_input(' --> Press q to quit, any other key to continue\n')
def main(argv): numWaveforms = 512 numThreads = 8 figsize = (20,10) plt.ion() channel = 626 aeCutVal = 0.01425 runRanges = [(13385, 13392), (13420,13429), (13551,13557)] r_mult = 1. z_mult = 1. scale_mult = 100. #Prepare detectoren = [1, 40503831.367655091, 507743886451386.06, 7.0164915381862738e+18] num = den = system = signal.lti(num, den) fitSamples=200 tempGuess = 78 gradGuess = 0.0487 pcRadGuess = 2.495226 pcLenGuess = 1.632869 #Create a detector model detName = "conf/P42574A_grad%0.2f_pcrad%0.2f_pclen%0.2f.conf" % (0.04,2.5, 1.6) det = Detector(detName, temperature=tempGuess, timeStep=1., numSteps=fitSamples*10, tfSystem=system) det.LoadFields("P42574A_fields_len.npz") det.SetFields(pcRadGuess, pcLenGuess, gradGuess) initializeDetector(det) p = Pool(numThreads, initializer=initializeDetector, initargs=[det]) #Crate a decent start guess by fitting waveform-by-waveform wfFileName = "P42574A_%dwaveforms_raw.npz" % numWaveforms wfFileNameProcessed = "P42574A_%dwaveforms_fit_nosmooth_de.npz" % numWaveforms if os.path.isfile(wfFileName): print "Raw File already exists %s" % wfFileName data = np.load(wfFileName) wfs = data['wfs'] print "We actually have %d waveforms here..." % len(wfs) else: print "No saved waveforms available. Loading from Data" #get waveforms cut = "trapECal>%f && trapECal<%f && TSCurrent100nsMax/trapECal > %f" % (1588,1594, aeCutVal) wfs = helpers.GetWaveforms(runRanges, channel, numWaveforms, cut) args = [] fig = plt.figure(figsize=figsize) for (idx, wf) in enumerate(wfs): wf.WindowWaveformTimepoint(fallPercentage=.99) args.append( [15./r_mult, np.pi/8., 15./z_mult, wf.wfMax/scale_mult, wf.t0Guess, wfs[idx] ] ) # args.append( [15./r_mult, np.pi/8., 15./z_mult, wf.wfMax/scale_mult, wf.t0Guess, 10., 5., wfs[idx] ] ) plt.plot(wf.windowedWf, color="r") np.savez(wfFileName, wfs = wfs ) value = raw_input(' --> Press q to quit, any other key to continue\n') if value == 'q': exit(0) print "performing parallelized initial fit..." start = timer() results = p.map(minimize_waveform_only_nosmooth_star, args) end = timer() print "Initial fit time: %f" % (end-start) simWfArr = np.empty((1,len(results), fitSamples)) for (idx,result) in enumerate(results): # r, phi, z, scale, t0, smooth, esmooth = result["x"] r, phi, z, scale, t0, = result["x"] print " >> wf %d (normalized likelihood %0.2f):" % (idx, result["fun"]/wfs[idx].wfLength) print " r: %0.2f, phi: %0.3f, z: %0.2f, e: %0.2f, t0: %0.2f" % (r, phi, z, scale, t0, ) simWfArr[0,idx,:] = det.GetSimWaveform(r*r_mult, phi, z*z_mult, scale*scale_mult, t0, fitSamples, ) # print " r: %0.2f, phi: %0.3f, z: %0.2f, e: %0.2f, t0: %0.2f, smooth:%0.2f, esmooth:%0.2f" % (r, phi, z, scale, t0, smooth, esmooth) # simWfArr[0,idx,:] = det.GetSimWaveform(r*r_mult, phi, z*z_mult, scale*scale_mult, t0, fitSamples, smoothing=smooth, electron_smoothing=esmooth) fig1 = plt.figure(figsize=figsize) helpers.plotManyResidual(simWfArr, wfs, fig1, residAlpha=1) np.savez(wfFileNameProcessed, wfs = wfs, result_arr=results ) value = raw_input(' --> Press q to quit, any other key to continue\n') if value == 'q': exit(0)