def main(argv): timeStepSize = 1 wfFileName = "most_recent_wf_pt.npz" numWaveforms = 30 #wfFileName = "P42574A_512waveforms_%drisetimeculled.npz" % numWaveforms if os.path.isfile(wfFileName): data = np.load(wfFileName) samples = data['samples'] else: print "No saved waveform available." tempGuess = 79.204603 gradGuess = 0.05 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. / timeStepSize, ) det.LoadFields("P42574A_fields_v3.npz") det.SetFields(pcRadGuess, pcLenGuess, gradGuess) b_over_a = 0.107077 c = -0.817381 d = 0.825026 rc = 76.551780 det.SetTransferFunction(b_over_a, c, d, rc) positionFig = plt.figure(5) plt.clf() xedges = np.linspace(0, np.around(det.detector_radius, 1), np.around(det.detector_radius, 1) * 50 + 1) yedges = np.linspace(0, np.around(det.detector_length, 1), np.around(det.detector_length, 1) * 50 + 1) z, xe, ye = np.histogram2d(samples[:, 0], samples[:, 2], bins=[xedges, yedges]) z /= z.sum() n = 100 t = np.linspace(0, z.max(), n) integral = ((z >= t[:, None, None]) * z).sum(axis=(1, 2)) from scipy import interpolate f = interpolate.interp1d(integral, t) t_contours = f(np.array([0.95, .68, .5, .2])) cs = plt.contourf(z.T, t_contours, extent=[0, det.detector_radius, 0, det.detector_length], alpha=0.7, colors=("green", "r", "c", "b"), extend='max') cs.cmap.set_over('red') proxy = [ plt.Rectangle((0, 0), 1, 1, fc=pc.get_facecolor()[0]) for pc in cs.collections ] plt.legend(proxy, ["95% C.I.", "68% C.I.", "50% C.I.", "20% C.I."], loc=3) plt.xlabel("r from Point Contact (mm)") plt.ylabel("z from Point Contact (mm)") plt.xlim(8, 14) plt.ylim(14, 21) plt.savefig("pt_credible_intervals.pdf") print len(np.where(z >= t_contours[-1])[0]) * .02 * .02 plt.figure() plt.imshow(z.T, origin='lower', extent=[0, det.detector_radius, 0, det.detector_length], norm=LogNorm()) plt.xlabel("r from Point Contact (mm)") plt.ylabel("z from Point Contact (mm)") plt.savefig("pt_heat_map.pdf") plt.figure() wfPlotNumber = 100 simWfs = np.empty((wfPlotNumber, fitSamples)) for idx, ( r, phi, z, scale, t0, smooth, ) in enumerate(samples[np.random.randint(len(samples), size=wfPlotNumber)]): simWfs[idx, :] = det.MakeSimWaveform( r, phi, z, scale, t0, fitSamples, h_smoothing=smooth, ) residFig = plt.figure(3) helpers.plotResidual(simWfs, wf.windowedWf, figure=residFig) plt.show()
def main(argv): plt.ion() numSamples = 4000000 burnin = 0.9 * numSamples doInitPlot = False fitSamples = 175 timeStepSize = 1. #ns #Prepare detector tempGuess = 79.071172 gradGuess = 0.05 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 = 3 c = -0.809211 d = 0.816583 rc1 = 74.4 rc2 = 1.79 rcfrac = 0.992 det.SetTransferFunction(b_over_a, c, d, rc1, rc2, rcfrac) initializeDetector(det, ) # wfFileName = "P42574A_512waveforms_30risetimeculled.npz" wfFileName = "P42574A_12_fastandslow_oldwfs.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.ones(numWaveforms) simWfArr = np.empty((1, numWaveforms, fitSamples)) nll = lambda *args: -lnlike_waveform(*args) # for (idx, wf) in enumerate(wfs): # wf.WindowWaveformTimepoint(fallPercentage=0.999, rmsMult=2) # # initializeWaveform(wf) # # r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx]= results[idx]['x'] # startGuess = [r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], 10] # 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'] # 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(11) # 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, >>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) for (wf_idx, wf) in enumerate(wfs): if wf_idx <= 0: continue wf.WindowWaveformTimepoint(fallPercentage=0.98, rmsMult=2) initializeWaveform(wf) 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): 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[wf_idx], phi_arr[wf_idx], z_arr[wf_idx], scale_arr[ wf_idx], t0_arr[wf_idx], smooth_arr[wf_idx], = minresult["x"] fig = plt.figure(11) plt.clf() ml_wf = 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]) 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) new_r = np.sqrt(r_arr[wf_idx]**2 + z_arr[wf_idx]**2) new_theta = np.arctan(z_arr[wf_idx] / r_arr[wf_idx]) print new_theta startGuess = { 'radEst': new_r, 'thetaEst': new_theta, 'phiEst': phi_arr[wf_idx], 'wfScale': scale_arr[wf_idx], 'switchpoint': t0_arr[wf_idx], 'smooth': smooth_arr[wf_idx], 'b_over_a': b_over_a, 'c': c, 'd': d, 'rc1': rc1, 'rc2': rc2, 'rcfrac': rcfrac, 'temp': tempGuess } # for k, v in startGuess.iteritems(): # print k, v model_locals = sm.CreateTFModel(det, wf, startGuess) M = pymc.MCMC(model_locals, ) M.use_step_method( pymc.AdaptiveMetropolis, [M.radEst, M.phiEst, M.t0Est, M.thetaEst, M.b_over_a, M.tempEst], scales={ M.radEst: 0.1, M.t0Est: 0.1, M.phiEst: np.pi / 10, M.thetaEst: 0.01, M.b_over_a: 0.5, M.tempEst: 0.5 }, delay=10000, interval=10000, shrink_if_necessary=True, ) M.use_step_method( pymc.AdaptiveMetropolis, [ M.c, M.d, ], scales={ M.c: 0.1, M.d: 0.1, }, delay=10000, interval=10000, shrink_if_necessary=True, ) # M.use_step_method(pymc.Metropolis, M.radEst, proposal_sd=0.5, proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.thetaEst, proposal_sd=np.pi/5, proposal_distribution='Normal', ) # M.use_step_method(pymc.Metropolis, M.phiEst, proposal_sd=np.pi/10, proposal_distribution='Normal') M.use_step_method(pymc.Metropolis, M.scaleEst, proposal_sd=0.01 * startGuess['wfScale'], proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.t0Est, proposal_sd=0.05, proposal_distribution='Normal') M.use_step_method(pymc.Metropolis, M.sigEst, proposal_sd=01, proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.tempEst, proposal_sd=1, proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.b_over_a, proposal_sd=0.5, proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.c, proposal_sd=0.1, proposal_distribution='Normal') # M.use_step_method(pymc.Metropolis, M.d, proposal_sd=0.1, proposal_distribution='Normal') M.use_step_method(pymc.Metropolis, M.rc1, proposal_sd=0.5, proposal_distribution='Normal') M.use_step_method(pymc.Metropolis, M.rc2, proposal_sd=0.1, proposal_distribution='Normal') M.use_step_method(pymc.Metropolis, M.rcfrac, proposal_sd=0.01, proposal_distribution='Normal') # # M.use_step_method(pymc.AdaptiveMetropolis, [M.c, M.d,], # scales={M.c: 0.1, # M.d: 0.1, # }, delay=1000, interval=1000,shrink_if_necessary=True, ) # # M.use_step_method(pymc.AdaptiveMetropolis, [M.radEst, M.t0Est,], # scales={M.radEst: 0.1, # M.t0Est: 0.1, # }, delay=1000, interval=1000,shrink_if_necessary=True, ) # M.use_step_method(pymc.Slicer, M.radEst) # M.use_step_method(pymc.Slicer, M.thetaEst, ) # M.use_step_method(pymc.Slicer, M.phiEst, ) # M.use_step_method(pymc.Slicer, M.scaleEst, ) # M.use_step_method(pymc.Slicer, M.t0Est,)# proposal_sd=0.05, proposal_distribution='Normal') # M.use_step_method(pymc.Slicer, M.sigEst,)# proposal_sd=01, proposal_distribution='Normal') # # M.use_step_method(pymc.Slicer, M.tempEst, )# proposal_sd=1, proposal_distribution='Normal') # M.use_step_method(pymc.Slicer, M.b_over_a,)# proposal_sd=0.5, proposal_distribution='Normal') # M.use_step_method(pymc.Slicer, M.c, )# proposal_sd=0.1, proposal_distribution='Normal') # M.use_step_method(pymc.Slicer, M.d, )# proposal_sd=0.1, proposal_distribution='Normal') # # M.use_step_method(pymc.Slicer, M.rc1, )# proposal_sd=0.5, proposal_distribution='Normal') # M.use_step_method(pymc.Slicer, M.rc2, )# proposal_sd=0.1, proposal_distribution='Normal') # M.use_step_method(pymc.Slicer, M.rcfrac, )# proposal_sd=0.01, proposal_distribution='Normal') print M.step_method_dict[M.radEst][0].C print M.step_method_dict[M.c][0].C print M.step_method_dict[M.radEst][0].stochastics print M.step_method_dict[M.c][0].stochastics M.sample(iter=numSamples, burn=0, tune_interval=1000) M.db.close() print M.step_method_dict[M.radEst][0].C # print M.step_method_dict[M.c][0].C # 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(1, 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('theta') ax2.set_ylabel('phi') ax3.set_ylabel('e') ax4.set_ylabel('t0') ax5.set_ylabel('sig') ax0.plot(M.trace('radEst')[:]) ax1.plot(M.trace('thetaEst')[:]) ax2.plot(M.trace('phiEst')[:]) ax3.plot(M.trace('wfScale')[:]) ax4.plot(M.trace('switchpoint')[:]) ax5.plot(M.trace('sigma')[:]) plt.savefig("pymc_wf_params.png") stepsFig = plt.figure(2, figsize=(20, 10)) plt.clf() ax0 = stepsFig.add_subplot(711) ax1 = stepsFig.add_subplot(712, sharex=ax0) ax2 = stepsFig.add_subplot(713, sharex=ax0) ax3 = stepsFig.add_subplot(714, sharex=ax0) ax4 = stepsFig.add_subplot(715, sharex=ax0) ax5 = stepsFig.add_subplot(716, sharex=ax0) ax6 = stepsFig.add_subplot(717, sharex=ax0) ax0.set_ylabel('temp') ax1.set_ylabel('b_over_a') ax2.set_ylabel('c') ax3.set_ylabel('d') ax4.set_ylabel('rc1') ax5.set_ylabel('rc2') ax5.set_ylabel('rcfrac') ax0.plot(M.trace('temp')[:]) ax1.plot(M.trace('b_over_a')[:]) ax2.plot(M.trace('c')[:]) ax3.plot(M.trace('d')[:]) ax4.plot(M.trace('rc1')[:]) ax5.plot(M.trace('rc2')[:]) ax6.plot(M.trace('rcfrac')[:]) plt.savefig("pymc_tf_params.png") stepsFig = plt.figure(3, figsize=(20, 10)) plt.clf() ax0 = stepsFig.add_subplot(321) ax1 = stepsFig.add_subplot(322, ) ax2 = stepsFig.add_subplot(323, ) ax3 = stepsFig.add_subplot(324, ) ax4 = stepsFig.add_subplot(325, ) ax5 = stepsFig.add_subplot(326, ) ax0.set_ylabel('r') ax1.set_ylabel('theta') ax2.set_ylabel('phi') ax3.set_ylabel('e') ax4.set_ylabel('t0') ax5.set_ylabel('sig') hist, bins = np.histogram(M.trace('radEst')[burnin:]) ax0.plot(bins[:-1], hist, ls="steps-post") hist, bins = np.histogram(M.trace('thetaEst')[burnin:]) ax1.plot(bins[:-1], hist, ls="steps-post") hist, bins = np.histogram(M.trace('phiEst')[burnin:]) ax2.plot(bins[:-1], hist, ls="steps-post") hist, bins = np.histogram(M.trace('wfScale')[burnin:]) ax3.plot(bins[:-1], hist, ls="steps-post") hist, bins = np.histogram(M.trace('switchpoint')[burnin:]) ax4.plot(bins[:-1], hist, ls="steps-post") hist, bins = np.histogram(M.trace('sigma')[burnin:]) ax5.plot(bins[:-1], hist, ls="steps-post") plt.savefig("pymc_wf_params_hist.png") wfPlotNumber = 5000 simWfs = np.empty((wfPlotNumber, fitSamples)) if burnin > len(M.trace('radEst')[:]): burnin = len(M.trace('radEst')[:]) - wfPlotNumber numSamples = len(M.trace('radEst')[:]) for (sim_idx, chain_idx) in enumerate( np.random.randint(low=burnin, high=numSamples, size=wfPlotNumber)): t0 = M.trace('switchpoint')[chain_idx] rad = M.trace('radEst')[chain_idx] theta = M.trace('thetaEst')[chain_idx] phi = M.trace('phiEst')[chain_idx] scale = M.trace('wfScale')[chain_idx] sigma = M.trace('sigma')[chain_idx] r = np.cos(theta) * rad z = np.sin(theta) * rad simWfs[sim_idx, :] = det.MakeSimWaveform( r, phi, z, scale, t0, fitSamples, h_smoothing=sigma, ) residFig = plt.figure(4, figsize=(20, 10)) helpers.plotResidual(simWfs, wf.windowedWf, figure=residFig) plt.savefig("pymc_waveforms.png") value = raw_input(' --> Press q to quit, any other key to continue\n') if value == 'q': exit(0)
def main(argv): plt.ion() runRange = (13420,13429) channel = 626 aeCutVal = 0.01425 fitSamples = 150 numWaveforms = 2 #get waveforms cut = "trapECal>%f && trapECal<%f && TSCurrent100nsMax/trapECal > %f" % (1588,1594, aeCutVal) wfs = helpers.GetWaveforms(runRange, channel, numWaveforms, cut) #init wf sim grad = 0.08 pcRad = 2.75#this is the actual starret value ish detName = "conf/P42574A_grad%0.2f_pcrad%0.2f.conf" % (grad,pcRad) 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) det = Detector(detName, temperature=83., timeStep=1., numSteps=fitSamples*10, tfSystem=system) # #plot to make sure you like what you see # plt.figure() # for wf in wfs: # wf.WindowWaveform(fitSamples) # plt.plot(wf.windowedWf, color="r") # # sim_wf = det.GetSimWaveform(10, 0, 10, wfs[0].wfMax, wfs[0].t0Guess, fitSamples) # plt.plot(sim_wf, color="b") # # plt.show() #ML as a start nll = lambda *args: -lnlike(*args) for (idx,wf) in enumerate(wfs): # if idx == 0: continue plt.figure(1) wf.WindowWaveformTimepoint() startGuess = [15., np.pi/8, 15., wf.wfMax, wf.t0Guess] init_wf = det.GetSimWaveform(15, np.pi/8, 15, wf.wfMax, wf.t0Guess, fitSamples) plt.plot(init_wf, color="g") result = op.minimize(nll, startGuess, args=(wf.windowedWf, det, wf.baselineRMS), method="Powell") r, phi, z, scale, t0= result["x"] plt.plot(wf.windowedWf, color="r") ml_wf = det.GetSimWaveform(r, phi, z, scale, t0, fitSamples) ml_wf_inv = det.GetSimWaveform(z, phi, r, scale, t0, fitSamples) plt.plot(ml_wf, color="b") plt.plot(ml_wf_inv, "b:") # print result["x"] # plt.show() # plt.figure(2) # plt.scatter(r, z) #Do the MCMC ndim, nwalkers = 5, 20 mcmc_startguess = np.array([r, phi, z, scale, t0]) pos0 = [mcmc_startguess + 1e-2*np.random.randn(ndim) for i in range(nwalkers)] sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob, args=(wf.windowedWf, det, wf.baselineRMS, wf.wfMax, wf.t0Guess)) # f = open("chain.dat", "w") # f.close() iter, burnIn = 1000, 500 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) # position = result[0] # f = open("chain.dat", "a") # for k in range(position.shape[0]): # print " ".join(str(position[k])) ## f.write( "{0:4d} {1:s}\n".format(k, " ".join(position[k]))) # f.close() # sampler.run_mcmc(pos, 500) end = timer() bar.finish() print "Elapsed time: " + str(end-start) #samples = sampler.chain[:, 50:, :].reshape((-1, ndim)) ######### Plots for MC Steps stepsFig = plt.figure(2) 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): ax0.plot(sampler.chain[i,:,0], "b", alpha=0.3) ax1.plot(sampler.chain[i,:,1], "b", alpha=0.3) ax2.plot(sampler.chain[i,:,2], "b", alpha=0.3) ax3.plot(sampler.chain[i,:,3], "b", alpha=0.3) ax4.plot(sampler.chain[i,:,4], "b", alpha=0.3) #pull the samples after burn-in samples = sampler.chain[:, 50:, :].reshape((-1, ndim)) wfPlotNumber = 100 simWfs = np.empty(wfPlotNumber, dtype=object) for idx, (r, phi, z, scale, t0) in enumerate(samples[np.random.randint(len(samples), size=wfPlotNumber)]): simWfs[idx] = det.GetSimWaveform(r, phi, z, scale, t0, fitSamples) residFig = plt.figure(3) helpers.plotResidual(simWfs, wf.windowedWf, figure=residFig) # plt.show() value = raw_input(' --> Press q to quit, any other key to continue\n') if value == 'q': exit(0)
def main(argv): plt.ion() runRange = (13420,13429) channel = 626 aeCutVal = 0.01425 numThreads = 8 fitSamples = 250 timeStepSize = 1 # wfFileName = "fep_old_postpsa.npz" wfFileName = "ms_event_set_runs11510-11530_mcmcfit.npz" numWaveforms = 30 #wfFileName = "P42574A_512waveforms_%drisetimeculled.npz" % numWaveforms if os.path.isfile(wfFileName): data = np.load(wfFileName) wfs = data['wfs'] numWaveforms = wfs.size else: print "No saved waveforms available. Loading from Data" exit(0) #Prepare detector tempGuess = 79.204603 gradGuess = 0.05 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./timeStepSize,) det.LoadFields("P42574A_fields_v3.npz") det.SetFields(pcRadGuess, pcLenGuess, gradGuess, "full") b_over_a = 0.107077 c = -0.817381 d = 0.825026 rc = 76.551780 det.SetTransferFunction(b_over_a, c, d, rc) initializeDetector(det, ) #ML as a start nll = lambda *args: -lnlike_waveform(*args) for (idx,wf) in enumerate(wfs): print "wf number %d" % idx if idx < 130: continue if wf.energy > 1800: continue lnprob = -1*wf.lnprob/wf.wfLength if lnprob < 5: continue print " ln prob is %f" % lnprob if wf.wfLength > fitSamples: print "wtf?" continue 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") wf.WindowWaveformTimepoint(fallPercentage=.999, rmsMult=2) initializeWaveform(wf) dataLen = wf.wfLength t_data = np.arange(dataLen) * 10 ax0.plot(t_data, wf.windowedWf, color="r") value = raw_input(' --> Press q to quit, s to skip, any other key to continue\n') if value == 'q': exit(0) if value == 's': continue 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 try: ax0.plot(t_data, ml_wf[:dataLen], color="b") ax1.plot(t_data, ml_wf[:dataLen] - wf.windowedWf, color="b") except ValueError: print r, z print ml_wf if result['fun'] < minlike: minlike = result['fun'] minresult = result ax1.set_ylim(-20,20) r, phi, z, scale, t0, smooth, = minresult["x"] print r, phi, z, scale, t0, smooth ml_wf = np.copy(det.MakeSimWaveform(r, phi, z, scale, t0, fitSamples, h_smoothing=smooth, )) ax0.plot(t_data, ml_wf[:dataLen], color="g") ax1.plot(t_data, ml_wf[:dataLen] - wf.windowedWf, color="g", lw=5) mcmc_startguess = minresult["x"] value = raw_input(' --> Press q to quit, s to skip, any other key to continue\n') if value == 'q': exit(0) if value == 's': continue # # startGuess = [det.detector_radius/2, np.pi/8, det.detector_length/2, wf.wfMax, wf.t0Guess-5, 10] # #startGuess = [0.2, np.pi/8, det.detector_length/2, wf.wfMax, wf.t0Guess-5, 10] # # result = op.minimize(nll, startGuess, method="Nelder-Mead") # #result = op.basinhopping(nll, startGuess, niter=100, T=10000, stepsize=10, minimizer_kwargs= {"method": "Nelder-Mead"}) # r, phi, z, scale, t0, smooth, = result["x"] # # r_new, z_new = det.ReflectPoint(r,z) # # print r, z # print r_new, z_new # # r_new = np.amin( [z, np.floor(det.detector_radius)] ) # z_new = np.amin( [r, np.floor(det.detector_length)] ) # print "Initial LN like is %f" % (result['fun']/wf.wfLength) # ## print r, phi, z, scale, t0, smooth # # ml_wf = np.copy(det.MakeSimWaveform(r, phi, z, scale, t0, fitSamples, h_smoothing=smooth, )) # ax0.plot(t_data, ml_wf[:dataLen], color="b") # ax1.plot(t_data, ml_wf[:dataLen] - wf.windowedWf, color="b") # # result2 = op.minimize(nll, [det.detector_radius/2, phi, 0.2, scale, wf.t0Guess-5,10], method="Powell") # r, phi, z, scale, t0, smooth, = result2["x"] # inv_ml_wf = det.MakeSimWaveform(r, phi, z, scale, t0, fitSamples, h_smoothing=smooth, ) # ax0.plot(t_data,inv_ml_wf[:dataLen], color="g") ## print r, phi, z, scale, t0, smooth # ax1.plot(t_data,inv_ml_wf[:dataLen] - wf.windowedWf, color="g") # # # ax1.set_ylim(-20,20) # # # if result2['fun'] < result['fun']: # mcmc_startguess = result2["x"] # else: # mcmc_startguess = result["x"] # # wf.prior = mcmc_startguess ## mcmc_startguess = startGuess # print "Inverted LN like is %f" % (result2['fun']/wf.wfLength) # # value = raw_input(' --> Press q to quit, s to skip, any other key to continue\n') # if value == 'q': # exit(0) # if value == 's': # continue #Do the MCMC ndim, nwalkers = 6, 100 # mcmc_startguess = startGuess#np.array([r, phi, z, scale, t0, smooth]) pos0 = [mcmc_startguess + 1e-1*np.random.randn(ndim)*mcmc_startguess for i in range(nwalkers)] p = Pool(numThreads, initializer=initializeWaveform, initargs=[ wf]) sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob_waveform, ) iter, burnIn = 1000, 900 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() p.close() #samples = sampler.chain[:, 50:, :].reshape((-1, ndim)) ######### Plots for MC Steps stepsFig = plt.figure(2) 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) # ax6 = stepsFig.add_subplot(717, 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('smooth') # ax6.set_ylabel('esmooth') for i in range(nwalkers): ax0.plot(sampler.chain[i,:,0], "b", alpha=0.3) ax1.plot(sampler.chain[i,:,1], "b", alpha=0.3) ax2.plot(sampler.chain[i,:,2], "b", alpha=0.3) ax3.plot(sampler.chain[i,:,3], "b", alpha=0.3) ax4.plot(sampler.chain[i,:,4], "b", alpha=0.3) ax5.plot(sampler.chain[i,:,5], "b", alpha=0.3) # ax6.plot(sampler.chain[i,:,6], "b", alpha=0.3) #pull the samples after burn-in samples = sampler.chain[:, burnIn:, :].reshape((-1, ndim)) wfPlotNumber = 100 simWfs = np.empty((wfPlotNumber, fitSamples) ) for idx, (r, phi, z, scale, t0, smooth, ) in enumerate(samples[np.random.randint(len(samples), size=wfPlotNumber)]): simWfs[idx,:] = det.MakeSimWaveform(r, phi, z, scale, t0, fitSamples, h_smoothing = smooth,) residFig = plt.figure(3) helpers.plotResidual(simWfs, wf.windowedWf, figure=residFig) lnprobFig = plt.figure(4) plt.clf() lnprobs = sampler.lnprobability[:, burnIn:].reshape((-1)) median_prob = np.median(lnprobs) print "median lnprob is %f (length normalized: %f)" % (median_prob, median_prob/wf.wfLength) # print lnprobs # lnprobs[np.where(np.isfinite(lnprobs) == 0)] = np.nan # # hist, bins = np.histogram(lnprobs/wf.wfLength, bins=np.linspace(-10,0,100)) # plt.plot(bins[:-1], hist, ls="steps-post") positionFig = plt.figure(5) plt.clf() xedges = np.linspace(0, np.around(det.detector_radius,1), np.around(det.detector_radius,1)*10+1) yedges = np.linspace(0, np.around(det.detector_length,1), np.around(det.detector_length,1)*10+1) plt.hist2d(samples[:,0], samples[:,2], norm=LogNorm(), bins=[ xedges,yedges ]) plt.colorbar() plt.xlabel("r from Point Contact (mm)") plt.ylabel("z from Point Contact (mm)") phiFig = plt.figure(6) plt.clf() plt.hist(samples[:,1], bins=np.linspace(0, np.pi/4, 100), ) plt.xlabel("Phi (0 to pi/4)") plt.ylabel("Marginalized Posterior Probability") # plt.xlim(0, det.detector_radius) # plt.ylim(0, det.detector_length) phiFig = plt.figure(7) plt.clf() plt.hist(samples[:,4]*10, bins=np.linspace(100, 299, 2000),) plt.xlim(np.amin(samples[:,4]*10), np.amax(samples[:,4]*10)) plt.xlabel("t0 [ns]" ) plt.ylabel("Marginalized Posterior Probability") print "1 sigma percentiles: " print " r = %f (%f,%f)" % (np.median(samples[:,0]), np.percentile(samples[:,0], 50-34), np.percentile(samples[:,0], 50+34)) print " phi = %f (%f,%f)" % (np.median(samples[:,1]), np.percentile(samples[:,1], 50-34), np.percentile(samples[:,1], 50+34)) print " z = %f (%f,%f)" % (np.median(samples[:,2]), np.percentile(samples[:,2], 50-34), np.percentile(samples[:,2], 50+34)) print " t0 = %f (%f,%f)" % (np.median(samples[:,4]), np.percentile(samples[:,4], 50-34), np.percentile(samples[:,4], 50+34)) print np.median(samples[:,:6], axis=0) # plt.show() value = raw_input(' --> Press q to quit, any other key to continue\n') if value == 'q': exit(0)
def main(argv): plt.ion() runRange = (13420, 13429) channel = 626 aeCutVal = 0.01425 fitSamples = 150 numWaveforms = 2 #get waveforms wfFileName = "P42574A_32waveforms_risetimeculled.npz" if os.path.isfile(wfFileName): data = np.load(wfFileName) wfs = data['wfs'] else: exit(0) # cut = "trapECal>%f && trapECal<%f && TSCurrent100nsMax/trapECal > %f" % (1588,1594, aeCutVal) # wfs = helpers.GetWaveforms(runRange, channel, numWaveforms, cut) num = [8772059139.7583485, 1.7795141663115218e+18, 17696700392300130.0] den = [1, 50813678.171704151, 708456403501206.5, 1.4152530477835272e+19] system = signal.lti(num, den) tempGuess = 77 gradGuess = 0.0487 pcRadGuess = 2.535691 pcLenGuess = 1.655159 #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) # #plot to make sure you like what you see # plt.figure() # for wf in wfs: # wf.WindowWaveform(fitSamples) # plt.plot(wf.windowedWf, color="r") # # sim_wf = det.GetSimWaveform(10, 0, 10, wfs[0].wfMax, wfs[0].t0Guess, fitSamples) # plt.plot(sim_wf, color="b") # # plt.show() #ML as a start nll = lambda *args: -lnlike(*args) for (idx, wf) in enumerate(wfs): # if idx == 0: continue plt.figure(1) wf.WindowWaveformTimepoint() startGuess = [15., np.pi / 8, 15., wf.wfMax, wf.t0Guess, 3., 1.] init_wf = det.GetSimWaveform(15, np.pi / 8, 15, wf.wfMax, wf.t0Guess, fitSamples) plt.plot(init_wf, color="g") result = op.minimize(nll, startGuess, args=(wf.windowedWf, det, wf.baselineRMS), method="Powell") r, phi, z, scale, t0, smooth, esmooth = result["x"] plt.plot(wf.windowedWf, color="r") ml_wf = det.GetSimWaveform(r, phi, z, scale, t0, fitSamples, smoothing=smooth, electron_smoothing=esmooth) ml_wf_inv = det.GetSimWaveform(z, phi, r, scale, t0, fitSamples, smoothing=smooth, electron_smoothing=esmooth) plt.plot(ml_wf, color="b") plt.plot(ml_wf_inv, "b:") # print result["x"] # plt.show() # plt.figure(2) # plt.scatter(r, z) #Do the MCMC ndim, nwalkers = 7, 100 mcmc_startguess = np.array([r, phi, z, scale, t0, smooth, esmooth]) pos0 = [ mcmc_startguess + 1e-1 * np.random.randn(ndim) * mcmc_startguess for i in range(nwalkers) ] sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob, args=(wf.windowedWf, det, wf.baselineRMS, wf.wfMax, wf.t0Guess)) # f = open("chain.dat", "w") # f.close() iter, burnIn = 2000, 1500 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) # position = result[0] # f = open("chain.dat", "a") # for k in range(position.shape[0]): # print " ".join(str(position[k])) ## f.write( "{0:4d} {1:s}\n".format(k, " ".join(position[k]))) # f.close() # sampler.run_mcmc(pos, 500) end = timer() bar.finish() print "Elapsed time: " + str(end - start) #samples = sampler.chain[:, 50:, :].reshape((-1, ndim)) ######### Plots for MC Steps stepsFig = plt.figure(2) plt.clf() ax0 = stepsFig.add_subplot(711) ax1 = stepsFig.add_subplot(712, sharex=ax0) ax2 = stepsFig.add_subplot(713, sharex=ax0) ax3 = stepsFig.add_subplot(714, sharex=ax0) ax4 = stepsFig.add_subplot(715, sharex=ax0) ax5 = stepsFig.add_subplot(716, sharex=ax0) ax6 = stepsFig.add_subplot(717, 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('smooth') ax6.set_ylabel('esmooth') for i in range(nwalkers): ax0.plot(sampler.chain[i, :, 0], "b", alpha=0.3) ax1.plot(sampler.chain[i, :, 1], "b", alpha=0.3) ax2.plot(sampler.chain[i, :, 2], "b", alpha=0.3) ax3.plot(sampler.chain[i, :, 3], "b", alpha=0.3) ax4.plot(sampler.chain[i, :, 4], "b", alpha=0.3) ax5.plot(sampler.chain[i, :, 5], "b", alpha=0.3) ax6.plot(sampler.chain[i, :, 6], "b", alpha=0.3) #pull the samples after burn-in samples = sampler.chain[:, burnIn:, :].reshape((-1, ndim)) wfPlotNumber = 100 simWfs = np.empty(wfPlotNumber, dtype=object) for idx, (r, phi, z, scale, t0, smooth, e_smooth) in enumerate( samples[np.random.randint(len(samples), size=wfPlotNumber)]): simWfs[idx] = det.GetSimWaveform(r, phi, z, scale, t0, fitSamples, smoothing=smooth, electron_smoothing=e_smooth) residFig = plt.figure(3) helpers.plotResidual(simWfs, wf.windowedWf, figure=residFig) # plt.show() value = raw_input(' --> Press q to quit, any other key to continue\n') if value == 'q': exit(0)
def main(argv): plt.ion() runRange = (13420,13429) channel = 626 aeCutVal = 0.01425 fitSamples = 150 numWaveforms = 2 #get waveforms wfFileName = "P42574A_32waveforms_risetimeculled.npz" if os.path.isfile(wfFileName): data = np.load(wfFileName) wfs = data['wfs'] else: exit(0) # cut = "trapECal>%f && trapECal<%f && TSCurrent100nsMax/trapECal > %f" % (1588,1594, aeCutVal) # wfs = helpers.GetWaveforms(runRange, channel, numWaveforms, cut) num = [8772059139.7583485, 1.7795141663115218e+18, 17696700392300130.0] den = [1, 50813678.171704151, 708456403501206.5, 1.4152530477835272e+19] system = signal.lti(num, den) tempGuess = 77 gradGuess = 0.0487 pcRadGuess = 2.535691 pcLenGuess = 1.655159 #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) # #plot to make sure you like what you see # plt.figure() # for wf in wfs: # wf.WindowWaveform(fitSamples) # plt.plot(wf.windowedWf, color="r") # # sim_wf = det.GetSimWaveform(10, 0, 10, wfs[0].wfMax, wfs[0].t0Guess, fitSamples) # plt.plot(sim_wf, color="b") # # plt.show() #ML as a start nll = lambda *args: -lnlike(*args) for (idx,wf) in enumerate(wfs): # if idx == 0: continue plt.figure(1) wf.WindowWaveformTimepoint() startGuess = [15., np.pi/8, 15., wf.wfMax, wf.t0Guess, 3., 1.] init_wf = det.GetSimWaveform(15, np.pi/8, 15, wf.wfMax, wf.t0Guess, fitSamples) plt.plot(init_wf, color="g") result = op.minimize(nll, startGuess, args=(wf.windowedWf, det, wf.baselineRMS), method="Powell") r, phi, z, scale, t0, smooth, esmooth= result["x"] plt.plot(wf.windowedWf, color="r") ml_wf = det.GetSimWaveform(r, phi, z, scale, t0, fitSamples, smoothing=smooth, electron_smoothing = esmooth) ml_wf_inv = det.GetSimWaveform(z, phi, r, scale, t0, fitSamples, smoothing=smooth, electron_smoothing = esmooth) plt.plot(ml_wf, color="b") plt.plot(ml_wf_inv, "b:") # print result["x"] # plt.show() # plt.figure(2) # plt.scatter(r, z) #Do the MCMC ndim, nwalkers = 7, 100 mcmc_startguess = np.array([r, phi, z, scale, t0, smooth, esmooth]) pos0 = [mcmc_startguess + 1e-1*np.random.randn(ndim)*mcmc_startguess for i in range(nwalkers)] sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob, args=(wf.windowedWf, det, wf.baselineRMS, wf.wfMax, wf.t0Guess)) # f = open("chain.dat", "w") # f.close() iter, burnIn = 2000, 1500 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) # position = result[0] # f = open("chain.dat", "a") # for k in range(position.shape[0]): # print " ".join(str(position[k])) ## f.write( "{0:4d} {1:s}\n".format(k, " ".join(position[k]))) # f.close() # sampler.run_mcmc(pos, 500) end = timer() bar.finish() print "Elapsed time: " + str(end-start) #samples = sampler.chain[:, 50:, :].reshape((-1, ndim)) ######### Plots for MC Steps stepsFig = plt.figure(2) plt.clf() ax0 = stepsFig.add_subplot(711) ax1 = stepsFig.add_subplot(712, sharex=ax0) ax2 = stepsFig.add_subplot(713, sharex=ax0) ax3 = stepsFig.add_subplot(714, sharex=ax0) ax4 = stepsFig.add_subplot(715, sharex=ax0) ax5 = stepsFig.add_subplot(716, sharex=ax0) ax6 = stepsFig.add_subplot(717, 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('smooth') ax6.set_ylabel('esmooth') for i in range(nwalkers): ax0.plot(sampler.chain[i,:,0], "b", alpha=0.3) ax1.plot(sampler.chain[i,:,1], "b", alpha=0.3) ax2.plot(sampler.chain[i,:,2], "b", alpha=0.3) ax3.plot(sampler.chain[i,:,3], "b", alpha=0.3) ax4.plot(sampler.chain[i,:,4], "b", alpha=0.3) ax5.plot(sampler.chain[i,:,5], "b", alpha=0.3) ax6.plot(sampler.chain[i,:,6], "b", alpha=0.3) #pull the samples after burn-in samples = sampler.chain[:, burnIn:, :].reshape((-1, ndim)) wfPlotNumber = 100 simWfs = np.empty(wfPlotNumber, dtype=object) for idx, (r, phi, z, scale, t0, smooth, e_smooth) in enumerate(samples[np.random.randint(len(samples), size=wfPlotNumber)]): simWfs[idx] = det.GetSimWaveform(r, phi, z, scale, t0, fitSamples, smoothing = smooth, electron_smoothing = e_smooth) residFig = plt.figure(3) helpers.plotResidual(simWfs, wf.windowedWf, figure=residFig) # plt.show() value = raw_input(' --> Press q to quit, any other key to continue\n') if value == 'q': exit(0)
def main(argv): plt.ion() numThreads = 8 fitSamples = 130 timeStepSize = 1 wfFileName = "P42574A_12_fastandslow_oldwfs.npz" # wfFileName = "P42574A_pc_highe.npz" #wfFileName = "fep_event_set_runs11531-11539.npz" # numWaveforms = 16 # wfFileName = "P42574A_512waveforms_%drisetimeculled.npz" % numWaveforms if os.path.isfile(wfFileName): data = np.load(wfFileName) wfs = data['wfs'] results = data['results'] numWaveforms = wfs.size else: print "No saved waveforms available. Loading from Data" exit(0) tempGuess = 79.071172 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.LoadFieldsGrad("fields_impgrad.npz", pcLen=pcLenGuess, pcRad=pcRadGuess) det.SetFieldsGradInterp(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 #us det.trapping_rc = trapping_rc #do my own hole mobility model based on bruy 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. # trapping_rc = 4 # det.trapping_rc = trapping_rc initializeDetector(det, ) pmw.initializeDetector(det, ) #ML as a start nll = lambda *args: -lnlike_waveform(*args) nll_wf = lambda *args: -pmw.lnlike_waveform(*args) # plt.ioff() for (wf_idx,wf) in enumerate(wfs): if wf_idx <= 2: continue if wf.energy < 1700: continue wf.WindowWaveformTimepoint(fallPercentage=.995, rmsMult=2) initializeWaveform(wf) pmw.initializeWaveform(wf) dataLen = wf.wfLength t_data = np.arange(dataLen) * 10 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") 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, 3): for z in np.linspace(4, np.floor(det.detector_length)-3, 3): # 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_wf, 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 ax1.set_ylim(-20,20) r, phi, z, scale, t0, smooth, = minresult["x"] print r, phi, z, scale, t0, smooth # r, phi, z, scale, t0, smooth = results[wf_idx]['x'] # startGuess = [r, phi, z, scale, t0, smooth] # result = op.minimize(nll_wf, startGuess, method="Powell") # r, phi, z, scale, t0, smooth = result['x'] ml_wf = np.copy(det.MakeSimWaveform(r, phi, z, scale, t0, fitSamples, h_smoothing=smooth, )) 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, s to skip, any other key to continue\n') if value == 'q': exit(0) if value == 's': continue mcmc_startguess = [r, phi, z, scale, t0, smooth, b_over_a, c, d, h_100_mu0, h_100_beta, h_100_e0, h_111_mu0, h_111_beta, h_111_e0,]# trapping_rc, ]#gradGuess, pcRadGuess, pcLenGuess ] # mcmc_startguess = [r, phi, z, scale, t0, smooth, tempGuess, omega, decay, rc1, rc2, rcfrac, ] #Do the MCMC ndim = 15 nwalkers = ndim*10 # mcmc_startguess = startGuess#np.array([r, phi, z, scale, t0, smooth]) pos0 = [mcmc_startguess + 1e-2*np.random.randn(ndim)*mcmc_startguess for i in range(nwalkers)] gradIdx = -1 mobIdx = 9 # pcRadIdx = -2 # pcLenIdx = -1 # rc1idx = -4 # rc2idx = -3 # rcfracidx = -2 # for pos in pos0: ## 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) # # 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 = lnprob_waveform(pos,) # if not np.isfinite(prior) : # print "BAD PRIOR WITH START GUESS YOURE KILLING ME SMALLS" # print pos # exit(0) p = Pool(numThreads, initializer=initializeWaveform, initargs=[ wf]) sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob_waveform, ) iter, burnIn = 10000, 9900 wfPlotNumber = 100 bar = ProgressBar(widgets=[Percentage(), Bar(), ETA()], maxval=iter).start() for (i,result) in enumerate(sampler.sample(pos0, iterations=iter, storechain=True)): bar.update(i+1) bar.finish() p.close() #samples = sampler.chain[:, 50:, :].reshape((-1, ndim)) ######### Plots for MC Steps stepsFig = plt.figure(2, figsize= (20,10)) plt.clf() plotnum = 600 ax0 = stepsFig.add_subplot(plotnum+11) ax1 = stepsFig.add_subplot(plotnum+12, sharex=ax0) ax2 = stepsFig.add_subplot(plotnum+13, sharex=ax0) ax3 = stepsFig.add_subplot(plotnum+14, sharex=ax0) ax4 = stepsFig.add_subplot(plotnum+15, sharex=ax0) ax5 = stepsFig.add_subplot(plotnum+16, 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('smooth') # ax6.set_ylabel('temp') for i in range(nwalkers): ax0.plot(sampler.chain[i,:,0], "b", alpha=0.3) ax1.plot(sampler.chain[i,:,1], "b", alpha=0.3) ax2.plot(sampler.chain[i,:,2], "b", alpha=0.3) ax3.plot(sampler.chain[i,:,3], "b", alpha=0.3) ax4.plot(sampler.chain[i,:,4], "b", alpha=0.3) ax5.plot(sampler.chain[i,:,5], "b", alpha=0.3) plt.savefig("hdxwf%d_wf_params.png"%wf_idx) stepsFig = plt.figure(3, figsize= (20,10)) plt.clf() plotnum = 600 ax0 = stepsFig.add_subplot(plotnum+11) ax1 = stepsFig.add_subplot(plotnum+12, sharex=ax0) ax2 = stepsFig.add_subplot(plotnum+13, sharex=ax0) ax3 = stepsFig.add_subplot(plotnum+14, sharex=ax0) ax4 = stepsFig.add_subplot(plotnum+15, sharex=ax0) ax5 = stepsFig.add_subplot(plotnum+16, sharex=ax0) ax0.set_ylabel('h_100_mu0') ax1.set_ylabel('h_100_beta') ax2.set_ylabel('h_100_e0') ax3.set_ylabel('h_111_mu0') ax4.set_ylabel('h_111_beta') ax5.set_ylabel('h_111_e0') # ax6.set_ylabel('temp') for i in range(nwalkers): ax0.plot(sampler.chain[i,:,mobIdx+0], "b", alpha=0.3) ax1.plot(sampler.chain[i,:,mobIdx+1], "b", alpha=0.3) ax2.plot(sampler.chain[i,:,mobIdx+2], "b", alpha=0.3) ax3.plot(sampler.chain[i,:,mobIdx+3], "b", alpha=0.3) ax4.plot(sampler.chain[i,:,mobIdx+4], "b", alpha=0.3) ax5.plot(sampler.chain[i,:,mobIdx+5], "b", alpha=0.3) plt.savefig("hdxwf%d_mobility_params.png"%wf_idx) stepsFig2 = plt.figure(4, figsize = (20,10)) plotnum = 400 ax6 = stepsFig2.add_subplot(plotnum+11) ax7 = stepsFig2.add_subplot(plotnum+12, sharex=ax6) ax8 = stepsFig2.add_subplot(plotnum+13, sharex=ax6) ax9 = stepsFig2.add_subplot(plotnum+14, sharex=ax6) # ax10 = stepsFig2.add_subplot(plotnum+15, sharex=ax6) # ax11 = stepsFig2.add_subplot(plotnum+16, sharex=ax6) # ax12 = stepsFig2.add_subplot(plotnum+17, sharex=ax6) # ax13 = stepsFig2.add_subplot(plotnum+18, sharex=ax6) ax6.set_ylabel('b_ov_a') ax7.set_ylabel('c') ax8.set_ylabel('d') ax9.set_ylabel('trapping') # ax10.set_ylabel('rc1') # ax11.set_ylabel('rc2') # ax12.set_ylabel('rcfrac') # ax8.set_ylabel('trapping') # ax9.set_ylabel('grad') for i in range(nwalkers): ax6.plot(sampler.chain[i,:,6], "b", alpha=0.3) ax7.plot(sampler.chain[i,:,7], "b", alpha=0.3) ax8.plot(sampler.chain[i,:,8], "b", alpha=0.3) # ax9.plot(sampler.chain[i,:,9], "b", alpha=0.3) # ax10.plot(sampler.chain[i,:,10], "b", alpha=0.3) # ax11.plot(sampler.chain[i,:,11], "b", alpha=0.3) # ax12.plot(sampler.chain[i,:,12], "b", alpha=0.3) # ax13.plot(sampler.chain[i,:,-1], "b", alpha=0.3) plt.savefig("hdxwf%d_tf_params.png"%wf_idx) samples = sampler.chain[:, burnIn:, :].reshape((-1, ndim)) # stepsFigTF = plt.figure(5, figsize = (20,10)) # # 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(815, ) ## tf5 = stepsFigTF.add_subplot(816, ) ## tf6 = stepsFigTF.add_subplot(817,) ## tf7 = stepsFigTF.add_subplot(818,) # # tf0.set_ylabel('temp') ## tf1.set_ylabel('c') ## tf2.set_ylabel('d') ## tf3.set_ylabel('rc1') ## tf4.set_ylabel('rc2') ## tf5.set_ylabel('rcfrac') # tf1.set_ylabel('b_ov_a') # tf2.set_ylabel('trapping') ## tf3.set_ylabel('grad') # # num_bins = 300 # [n, b, p] = tf0.hist(samples[:,6], bins=num_bins) # print "b_over_a mode is %f" % b[np.argmax(n)] # # [n, b, p] = tf1.hist(samples[:,7],bins=num_bins) # print "c mode is %f" % b[np.argmax(n)] # # [n, b, p] = tf2.hist(samples[:,8],bins=num_bins) # print "d mode is %f" % b[np.argmax(n)] # [n, b, p] = tf3.hist(samples[:,-1],bins=num_bins) # print "rc_decay1 mode is %f" % b[np.argmax(n)] # # [n, b, p] = tf4.hist(samples[:,-3],bins=num_bins) # print "rc_decay2 mode is %f" % b[np.argmax(n)] # # [n, b, p] = tf5.hist(samples[:,-2],bins=num_bins) # print "rc_frac mode is %f" % b[np.argmax(n)] # # [n, b, p] = tf6.hist(samples[:,-8],bins=num_bins) # print "temp mode is %f" % b[np.argmax(n)] # # [n, b, p] = tf6.hist(samples[:,-1],bins=num_bins) # print "grad mode is %f" % b[np.argmax(n)] plt.savefig("hdxwf%d_tf_hist.png"%wf_idx) # stepsFig3 = plt.figure(13) # plotnum = 300 # ax13 = stepsFig3.add_subplot(plotnum+11) # ax14 = stepsFig3.add_subplot(plotnum+12, sharex=ax13) # ax15 = stepsFig3.add_subplot(plotnum+13, sharex=ax13) # ax13.set_ylabel('grad') # ax14.set_ylabel('pcrad') # ax15.set_ylabel('pclem') # for i in range(nwalkers): # ax13.plot(sampler.chain[i,:,13], "b", alpha=0.3) # ax14.plot(sampler.chain[i,:,14], "b", alpha=0.3) # ax15.plot(sampler.chain[i,:,15], "b", alpha=0.3) # # plt.savefig("hdxwf%d_det_params.png"%wf_idx) #pull the samples after burn-in simWfs = np.empty((wfPlotNumber, fitSamples) ) for idx, (r, phi, z, scale, t0, smooth, b_over_a, c, d, h_100_mu0, h_100_beta, h_100_e0, h_111_mu0, h_111_beta, h_111_e0, ) in enumerate(samples[np.random.randint(len(samples), size=wfPlotNumber)]): # for idx, (r, phi, z, scale, t0, smooth, temp, omega,decay, rc1, rc2, rcfrac, ) in enumerate(samples[np.random.randint(len(samples), size=wfPlotNumber)]): det.SetTransferFunction(b_over_a, c, d, rc1, rc2, rcfrac) 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.SetFieldsGradInterp(grad) # det.SetFields(pcRad, pcLen, impGrad) # det.trapping_rc = trapping_rc simWfs[idx,:] = det.MakeSimWaveform(r, phi, z, scale, t0, fitSamples, h_smoothing=smooth) residFig = plt.figure(6) helpers.plotResidual(simWfs, wf.windowedWf, figure=residFig) print "Median values wf %d: " % wf_idx for idx in range(ndim): print " param %d: %f" % (idx, np.median(samples[:,idx])) plt.savefig("hdxwf%d_resid.png"%wf_idx) # plt.show() value = raw_input(' --> Press q to quit, any other key to continue\n') if value == 'q': exit(0)
def main(argv): plt.ion() numThreads = 8 fitSamples = 130 timeStepSize = 1 wfFileName = "P42574A_12_fastandslow_oldwfs.npz" # wfFileName = "P42574A_pc_highe.npz" #wfFileName = "fep_event_set_runs11531-11539.npz" # numWaveforms = 16 # wfFileName = "P42574A_512waveforms_%drisetimeculled.npz" % numWaveforms if os.path.isfile(wfFileName): data = np.load(wfFileName) wfs = data['wfs'] results = data['results'] numWaveforms = wfs.size else: print "No saved waveforms available. Loading from Data" exit(0) tempGuess = 79.071172 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.LoadFieldsGrad("fields_impgrad.npz", pcLen=pcLenGuess, pcRad=pcRadGuess) det.SetFieldsGradInterp(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 #us det.trapping_rc = trapping_rc #do my own hole mobility model based on bruy 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. # trapping_rc = 4 # det.trapping_rc = trapping_rc initializeDetector(det, ) pmw.initializeDetector(det, ) #ML as a start nll = lambda *args: -lnlike_waveform(*args) nll_wf = lambda *args: -pmw.lnlike_waveform(*args) # plt.ioff() for (wf_idx, wf) in enumerate(wfs): if wf_idx <= 2: continue if wf.energy < 1700: continue wf.WindowWaveformTimepoint(fallPercentage=.995, rmsMult=2) initializeWaveform(wf) pmw.initializeWaveform(wf) dataLen = wf.wfLength t_data = np.arange(dataLen) * 10 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") 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, 3): for z in np.linspace(4, np.floor(det.detector_length) - 3, 3): # 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_wf, 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 ax1.set_ylim(-20, 20) r, phi, z, scale, t0, smooth, = minresult["x"] print r, phi, z, scale, t0, smooth # r, phi, z, scale, t0, smooth = results[wf_idx]['x'] # startGuess = [r, phi, z, scale, t0, smooth] # result = op.minimize(nll_wf, startGuess, method="Powell") # r, phi, z, scale, t0, smooth = result['x'] ml_wf = np.copy( det.MakeSimWaveform( r, phi, z, scale, t0, fitSamples, h_smoothing=smooth, )) 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, s to skip, any other key to continue\n') if value == 'q': exit(0) if value == 's': continue mcmc_startguess = [ r, phi, z, scale, t0, smooth, b_over_a, c, d, h_100_mu0, h_100_beta, h_100_e0, h_111_mu0, h_111_beta, h_111_e0, ] # trapping_rc, ]#gradGuess, pcRadGuess, pcLenGuess ] # mcmc_startguess = [r, phi, z, scale, t0, smooth, tempGuess, omega, decay, rc1, rc2, rcfrac, ] #Do the MCMC ndim = 15 nwalkers = ndim * 10 # mcmc_startguess = startGuess#np.array([r, phi, z, scale, t0, smooth]) pos0 = [ mcmc_startguess + 1e-2 * np.random.randn(ndim) * mcmc_startguess for i in range(nwalkers) ] gradIdx = -1 mobIdx = 9 # pcRadIdx = -2 # pcLenIdx = -1 # rc1idx = -4 # rc2idx = -3 # rcfracidx = -2 # for pos in pos0: ## 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) # # 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 = lnprob_waveform(pos,) # if not np.isfinite(prior) : # print "BAD PRIOR WITH START GUESS YOURE KILLING ME SMALLS" # print pos # exit(0) p = Pool(numThreads, initializer=initializeWaveform, initargs=[wf]) sampler = emcee.EnsembleSampler( nwalkers, ndim, lnprob_waveform, ) iter, burnIn = 10000, 9900 wfPlotNumber = 100 bar = ProgressBar(widgets=[Percentage(), Bar(), ETA()], maxval=iter).start() for (i, result) in enumerate( sampler.sample(pos0, iterations=iter, storechain=True)): bar.update(i + 1) bar.finish() p.close() #samples = sampler.chain[:, 50:, :].reshape((-1, ndim)) ######### Plots for MC Steps stepsFig = plt.figure(2, figsize=(20, 10)) plt.clf() plotnum = 600 ax0 = stepsFig.add_subplot(plotnum + 11) ax1 = stepsFig.add_subplot(plotnum + 12, sharex=ax0) ax2 = stepsFig.add_subplot(plotnum + 13, sharex=ax0) ax3 = stepsFig.add_subplot(plotnum + 14, sharex=ax0) ax4 = stepsFig.add_subplot(plotnum + 15, sharex=ax0) ax5 = stepsFig.add_subplot(plotnum + 16, 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('smooth') # ax6.set_ylabel('temp') for i in range(nwalkers): ax0.plot(sampler.chain[i, :, 0], "b", alpha=0.3) ax1.plot(sampler.chain[i, :, 1], "b", alpha=0.3) ax2.plot(sampler.chain[i, :, 2], "b", alpha=0.3) ax3.plot(sampler.chain[i, :, 3], "b", alpha=0.3) ax4.plot(sampler.chain[i, :, 4], "b", alpha=0.3) ax5.plot(sampler.chain[i, :, 5], "b", alpha=0.3) plt.savefig("hdxwf%d_wf_params.png" % wf_idx) stepsFig = plt.figure(3, figsize=(20, 10)) plt.clf() plotnum = 600 ax0 = stepsFig.add_subplot(plotnum + 11) ax1 = stepsFig.add_subplot(plotnum + 12, sharex=ax0) ax2 = stepsFig.add_subplot(plotnum + 13, sharex=ax0) ax3 = stepsFig.add_subplot(plotnum + 14, sharex=ax0) ax4 = stepsFig.add_subplot(plotnum + 15, sharex=ax0) ax5 = stepsFig.add_subplot(plotnum + 16, sharex=ax0) ax0.set_ylabel('h_100_mu0') ax1.set_ylabel('h_100_beta') ax2.set_ylabel('h_100_e0') ax3.set_ylabel('h_111_mu0') ax4.set_ylabel('h_111_beta') ax5.set_ylabel('h_111_e0') # ax6.set_ylabel('temp') for i in range(nwalkers): ax0.plot(sampler.chain[i, :, mobIdx + 0], "b", alpha=0.3) ax1.plot(sampler.chain[i, :, mobIdx + 1], "b", alpha=0.3) ax2.plot(sampler.chain[i, :, mobIdx + 2], "b", alpha=0.3) ax3.plot(sampler.chain[i, :, mobIdx + 3], "b", alpha=0.3) ax4.plot(sampler.chain[i, :, mobIdx + 4], "b", alpha=0.3) ax5.plot(sampler.chain[i, :, mobIdx + 5], "b", alpha=0.3) plt.savefig("hdxwf%d_mobility_params.png" % wf_idx) stepsFig2 = plt.figure(4, figsize=(20, 10)) plotnum = 400 ax6 = stepsFig2.add_subplot(plotnum + 11) ax7 = stepsFig2.add_subplot(plotnum + 12, sharex=ax6) ax8 = stepsFig2.add_subplot(plotnum + 13, sharex=ax6) ax9 = stepsFig2.add_subplot(plotnum + 14, sharex=ax6) # ax10 = stepsFig2.add_subplot(plotnum+15, sharex=ax6) # ax11 = stepsFig2.add_subplot(plotnum+16, sharex=ax6) # ax12 = stepsFig2.add_subplot(plotnum+17, sharex=ax6) # ax13 = stepsFig2.add_subplot(plotnum+18, sharex=ax6) ax6.set_ylabel('b_ov_a') ax7.set_ylabel('c') ax8.set_ylabel('d') ax9.set_ylabel('trapping') # ax10.set_ylabel('rc1') # ax11.set_ylabel('rc2') # ax12.set_ylabel('rcfrac') # ax8.set_ylabel('trapping') # ax9.set_ylabel('grad') for i in range(nwalkers): ax6.plot(sampler.chain[i, :, 6], "b", alpha=0.3) ax7.plot(sampler.chain[i, :, 7], "b", alpha=0.3) ax8.plot(sampler.chain[i, :, 8], "b", alpha=0.3) # ax9.plot(sampler.chain[i,:,9], "b", alpha=0.3) # ax10.plot(sampler.chain[i,:,10], "b", alpha=0.3) # ax11.plot(sampler.chain[i,:,11], "b", alpha=0.3) # ax12.plot(sampler.chain[i,:,12], "b", alpha=0.3) # ax13.plot(sampler.chain[i,:,-1], "b", alpha=0.3) plt.savefig("hdxwf%d_tf_params.png" % wf_idx) samples = sampler.chain[:, burnIn:, :].reshape((-1, ndim)) # stepsFigTF = plt.figure(5, figsize = (20,10)) # # 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(815, ) ## tf5 = stepsFigTF.add_subplot(816, ) ## tf6 = stepsFigTF.add_subplot(817,) ## tf7 = stepsFigTF.add_subplot(818,) # # tf0.set_ylabel('temp') ## tf1.set_ylabel('c') ## tf2.set_ylabel('d') ## tf3.set_ylabel('rc1') ## tf4.set_ylabel('rc2') ## tf5.set_ylabel('rcfrac') # tf1.set_ylabel('b_ov_a') # tf2.set_ylabel('trapping') ## tf3.set_ylabel('grad') # # num_bins = 300 # [n, b, p] = tf0.hist(samples[:,6], bins=num_bins) # print "b_over_a mode is %f" % b[np.argmax(n)] # # [n, b, p] = tf1.hist(samples[:,7],bins=num_bins) # print "c mode is %f" % b[np.argmax(n)] # # [n, b, p] = tf2.hist(samples[:,8],bins=num_bins) # print "d mode is %f" % b[np.argmax(n)] # [n, b, p] = tf3.hist(samples[:,-1],bins=num_bins) # print "rc_decay1 mode is %f" % b[np.argmax(n)] # # [n, b, p] = tf4.hist(samples[:,-3],bins=num_bins) # print "rc_decay2 mode is %f" % b[np.argmax(n)] # # [n, b, p] = tf5.hist(samples[:,-2],bins=num_bins) # print "rc_frac mode is %f" % b[np.argmax(n)] # # [n, b, p] = tf6.hist(samples[:,-8],bins=num_bins) # print "temp mode is %f" % b[np.argmax(n)] # # [n, b, p] = tf6.hist(samples[:,-1],bins=num_bins) # print "grad mode is %f" % b[np.argmax(n)] plt.savefig("hdxwf%d_tf_hist.png" % wf_idx) # stepsFig3 = plt.figure(13) # plotnum = 300 # ax13 = stepsFig3.add_subplot(plotnum+11) # ax14 = stepsFig3.add_subplot(plotnum+12, sharex=ax13) # ax15 = stepsFig3.add_subplot(plotnum+13, sharex=ax13) # ax13.set_ylabel('grad') # ax14.set_ylabel('pcrad') # ax15.set_ylabel('pclem') # for i in range(nwalkers): # ax13.plot(sampler.chain[i,:,13], "b", alpha=0.3) # ax14.plot(sampler.chain[i,:,14], "b", alpha=0.3) # ax15.plot(sampler.chain[i,:,15], "b", alpha=0.3) # # plt.savefig("hdxwf%d_det_params.png"%wf_idx) #pull the samples after burn-in simWfs = np.empty((wfPlotNumber, fitSamples)) for idx, ( r, phi, z, scale, t0, smooth, b_over_a, c, d, h_100_mu0, h_100_beta, h_100_e0, h_111_mu0, h_111_beta, h_111_e0, ) in enumerate(samples[np.random.randint(len(samples), size=wfPlotNumber)]): # for idx, (r, phi, z, scale, t0, smooth, temp, omega,decay, rc1, rc2, rcfrac, ) in enumerate(samples[np.random.randint(len(samples), size=wfPlotNumber)]): det.SetTransferFunction(b_over_a, c, d, rc1, rc2, rcfrac) 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.SetFieldsGradInterp(grad) # det.SetFields(pcRad, pcLen, impGrad) # det.trapping_rc = trapping_rc simWfs[idx, :] = det.MakeSimWaveform(r, phi, z, scale, t0, fitSamples, h_smoothing=smooth) residFig = plt.figure(6) helpers.plotResidual(simWfs, wf.windowedWf, figure=residFig) print "Median values wf %d: " % wf_idx for idx in range(ndim): print " param %d: %f" % (idx, np.median(samples[:, idx])) plt.savefig("hdxwf%d_resid.png" % wf_idx) # plt.show() value = raw_input(' --> Press q to quit, any other key to continue\n') if value == 'q': exit(0)
def main(argv): plt.ion() runRange = (13420,13429) channel = 626 aeCutVal = 0.01425 numThreads = 8 fitSamples = 200 timeStepSize = 1 wfFileName = "fep_old_postpsa.npz" numWaveforms = 30 #wfFileName = "P42574A_512waveforms_%drisetimeculled.npz" % numWaveforms if os.path.isfile(wfFileName): data = np.load(wfFileName) wfs = data['wfs'] numWaveforms = wfs.size else: print "No saved waveforms available. Loading from Data" exit(0) #Prepare detector tempGuess = 79.310080 gradGuess = 0.051005 pcRadGuess = 2.499387 pcLenGuess = 1.553464 #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,) det.LoadFields("P42574A_fields_v3.npz") det.SetFields(pcRadGuess, pcLenGuess, gradGuess) b_over_a = 0.107213 c = -0.821158 d = 0.828957 rc = 76.710043 det.SetTransferFunction(b_over_a, c, d, rc) initializeDetector(det, ) #ML as a start nll = lambda *args: -lnlike_waveform(*args) for (idx,wf) in enumerate(wfs): print "Waveform number %d" % idx if idx < 22: continue 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") wf.WindowWaveformTimepoint(fallPercentage=.999, rmsMult=2) initializeWaveform(wf) dataLen = wf.wfLength t_data = np.arange(dataLen) * 10 ax0.plot(t_data, wf.windowedWf, color="r") startGuess = [15., np.pi/8, 15., wf.wfMax, wf.t0Guess-5, 10] init_wf = det.MakeSimWaveform(15, np.pi/8, 15, wf.wfMax, wf.t0Guess, fitSamples, h_smoothing=10) result = op.minimize(nll, startGuess, method="Powell") #result = op.basinhopping(nll, startGuess, T=1000,stepsize=15, minimizer_kwargs= {"method": "Nelder-Mead", "args":(wf)}) r, phi, z, scale, t0, smooth, = result["x"] r_new, z_new = det.ReflectPoint(r,z) print "Initial LN like is %f" % (result['fun']) # print r, phi, z, scale, t0, smooth ml_wf = np.copy(det.MakeSimWaveform(r, phi, z, scale, t0, fitSamples, h_smoothing=smooth, )) ax0.plot(t_data, ml_wf[:dataLen], color="b") ax1.plot(t_data, ml_wf[:dataLen] - wf.windowedWf, color="b") result2 = op.minimize(nll, [r_new, phi, z_new, scale, wf.t0Guess-5,10], method="Powell") r, phi, z, scale, t0, smooth, = result2["x"] inv_ml_wf = det.MakeSimWaveform(r, phi, z, scale, t0, fitSamples, h_smoothing=smooth, ) ax0.plot(t_data,inv_ml_wf[:dataLen], color="g") # print r, phi, z, scale, t0, smooth ax1.plot(t_data,inv_ml_wf[:dataLen] - wf.windowedWf, color="g") ax1.set_ylim(-20,20) if result2['fun'] < result['fun']: mcmc_startguess = result2["x"] else: mcmc_startguess = result["x"] wf.prior = mcmc_startguess print "Inverted LN like is %f" % (result2['fun']) value = raw_input(' --> Press q to quit, s to skip, any other key to continue\n') if value == 'q': exit(0) if value == 's': continue #Do the MCMC ndim, nwalkers = 6, 12 mcmc_startguess = startGuess#np.array([r, phi, z, scale, t0, smooth]) ntemps = 512 pos0 = np.random.uniform(low=0.99, high=1.01, size=(ntemps, nwalkers, ndim)) for tidx in range(ntemps): for widx in range(nwalkers): pos0[tidx, widx, :] = pos0[tidx, widx, :] * mcmc_startguess #sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob_waveform, args=([wf])) p = Pool(numThreads, initializer=initializeWaveform, initargs=[ wf]) sampler = emcee.PTSampler(ntemps, nwalkers, ndim, lnlike_waveform, lnprob_waveform, pool=p, ) iter, burnIn = 1000, 900 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() p.close() #samples = sampler.chain[:, 50:, :].reshape((-1, ndim)) # ######### Plots for MC Steps # stepsFig = plt.figure(2) # 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) ## ax6 = stepsFig.add_subplot(717, 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('smooth') ## ax6.set_ylabel('esmooth') # # for k in range(ntemps): # for i in range(nwalkers): # ax0.plot(sampler.chain[k,i,:,0], "b", alpha=0.3) # ax1.plot(sampler.chain[k,i,:,1], "b", alpha=0.3) # ax2.plot(sampler.chain[k,i,:,2], "b", alpha=0.3) # ax3.plot(sampler.chain[k,i,:,3], "b", alpha=0.3) # ax4.plot(sampler.chain[k,i,:,4], "b", alpha=0.3) # ax5.plot(sampler.chain[k,i,:,5], "b", alpha=0.3) ## ax6.plot(sampler.chain[i,:,6], "b", alpha=0.3) # # #pull the samples after burn-in samples = sampler.chain[:,:, burnIn:, :].reshape((-1, ndim)) wfPlotNumber = 100 simWfs = np.empty((wfPlotNumber, fitSamples) ) for idx, (r, phi, z, scale, t0, smooth, ) in enumerate(samples[np.random.randint(len(samples), size=wfPlotNumber)]): simWfs[idx,:] = det.MakeSimWaveform(r, phi, z, scale, t0, fitSamples, h_smoothing = smooth,) # plt.ioff() plt.ion() residFig = plt.figure(3) helpers.plotResidual(simWfs, wf.windowedWf, figure=residFig) plt.savefig("waveform_fig.png") plt.figure(4) plt.hist(samples[:,0], bins=40) plt.xlabel("radius [mm]") plt.figure(5) plt.hist(samples[:,2], bins=40) plt.ylabel("height [mm]") # lnprobFig = plt.figure(4) # plt.clf() # lnprobs = sampler.lnprobability[:, burnIn:].reshape((-1)) # median_prob = np.median(lnprobs) # print "median lnprob is %f (length normalized: %f)" % (median_prob, median_prob/wf.wfLength) # plt.hist(lnprobs) # positionFig = plt.figure(6) plt.hist2d(samples[:,0], samples[:,2], norm=LogNorm(), bins=[ np.around(det.detector_radius,1)*10,np.around(det.detector_length,1)*10 ]) plt.xlim(0, det.detector_radius) plt.ylim(0, det.detector_length) plt.colorbar() lnprobFig = plt.figure(7) plt.clf() lnprobs = sampler.lnprobability[:, :, burnIn:].reshape((-1)) median_prob = np.median(lnprobs) print "median lnprob is %f (length normalized: %f)" % (median_prob, median_prob/wf.wfLength) hist, bins = np.histogram(lnprobs/wf.wfLength, bins=np.linspace(-10,0,1000)) plt.plot(bins[:-1], hist, ls="steps-post") # plt.show() value = raw_input(' --> Press q to quit, any other key to continue\n') if value == 'q': exit(0)
def main(argv): plt.ion() runRange = (13420, 13429) channel = 626 aeCutVal = 0.01425 numThreads = 8 fitSamples = 200 timeStepSize = 1 wfFileName = "fep_old_postpsa.npz" numWaveforms = 30 #wfFileName = "P42574A_512waveforms_%drisetimeculled.npz" % numWaveforms if os.path.isfile(wfFileName): data = np.load(wfFileName) wfs = data['wfs'] numWaveforms = wfs.size else: print "No saved waveforms available. Loading from Data" exit(0) #Prepare detector tempGuess = 79.310080 gradGuess = 0.051005 pcRadGuess = 2.499387 pcLenGuess = 1.553464 #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, ) det.LoadFields("P42574A_fields_v3.npz") det.SetFields(pcRadGuess, pcLenGuess, gradGuess) b_over_a = 0.107213 c = -0.821158 d = 0.828957 rc = 76.710043 det.SetTransferFunction(b_over_a, c, d, rc) initializeDetector(det, ) #ML as a start nll = lambda *args: -lnlike_waveform(*args) for (idx, wf) in enumerate(wfs): print "Waveform number %d" % idx if idx < 22: continue 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") wf.WindowWaveformTimepoint(fallPercentage=.999, rmsMult=2) initializeWaveform(wf) dataLen = wf.wfLength t_data = np.arange(dataLen) * 10 ax0.plot(t_data, wf.windowedWf, color="r") startGuess = [15., np.pi / 8, 15., wf.wfMax, wf.t0Guess - 5, 10] init_wf = det.MakeSimWaveform(15, np.pi / 8, 15, wf.wfMax, wf.t0Guess, fitSamples, h_smoothing=10) result = op.minimize(nll, startGuess, method="Powell") #result = op.basinhopping(nll, startGuess, T=1000,stepsize=15, minimizer_kwargs= {"method": "Nelder-Mead", "args":(wf)}) r, phi, z, scale, t0, smooth, = result["x"] r_new, z_new = det.ReflectPoint(r, z) print "Initial LN like is %f" % (result['fun']) # print r, phi, z, scale, t0, smooth ml_wf = np.copy( det.MakeSimWaveform( r, phi, z, scale, t0, fitSamples, h_smoothing=smooth, )) ax0.plot(t_data, ml_wf[:dataLen], color="b") ax1.plot(t_data, ml_wf[:dataLen] - wf.windowedWf, color="b") result2 = op.minimize(nll, [r_new, phi, z_new, scale, wf.t0Guess - 5, 10], method="Powell") r, phi, z, scale, t0, smooth, = result2["x"] inv_ml_wf = det.MakeSimWaveform( r, phi, z, scale, t0, fitSamples, h_smoothing=smooth, ) ax0.plot(t_data, inv_ml_wf[:dataLen], color="g") # print r, phi, z, scale, t0, smooth ax1.plot(t_data, inv_ml_wf[:dataLen] - wf.windowedWf, color="g") ax1.set_ylim(-20, 20) if result2['fun'] < result['fun']: mcmc_startguess = result2["x"] else: mcmc_startguess = result["x"] wf.prior = mcmc_startguess print "Inverted LN like is %f" % (result2['fun']) value = raw_input( ' --> Press q to quit, s to skip, any other key to continue\n') if value == 'q': exit(0) if value == 's': continue #Do the MCMC ndim, nwalkers = 6, 12 mcmc_startguess = startGuess #np.array([r, phi, z, scale, t0, smooth]) ntemps = 512 pos0 = np.random.uniform(low=0.99, high=1.01, size=(ntemps, nwalkers, ndim)) for tidx in range(ntemps): for widx in range(nwalkers): pos0[tidx, widx, :] = pos0[tidx, widx, :] * mcmc_startguess #sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob_waveform, args=([wf])) p = Pool(numThreads, initializer=initializeWaveform, initargs=[wf]) sampler = emcee.PTSampler( ntemps, nwalkers, ndim, lnlike_waveform, lnprob_waveform, pool=p, ) iter, burnIn = 1000, 900 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() p.close() #samples = sampler.chain[:, 50:, :].reshape((-1, ndim)) # ######### Plots for MC Steps # stepsFig = plt.figure(2) # 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) ## ax6 = stepsFig.add_subplot(717, 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('smooth') ## ax6.set_ylabel('esmooth') # # for k in range(ntemps): # for i in range(nwalkers): # ax0.plot(sampler.chain[k,i,:,0], "b", alpha=0.3) # ax1.plot(sampler.chain[k,i,:,1], "b", alpha=0.3) # ax2.plot(sampler.chain[k,i,:,2], "b", alpha=0.3) # ax3.plot(sampler.chain[k,i,:,3], "b", alpha=0.3) # ax4.plot(sampler.chain[k,i,:,4], "b", alpha=0.3) # ax5.plot(sampler.chain[k,i,:,5], "b", alpha=0.3) ## ax6.plot(sampler.chain[i,:,6], "b", alpha=0.3) # # #pull the samples after burn-in samples = sampler.chain[:, :, burnIn:, :].reshape((-1, ndim)) wfPlotNumber = 100 simWfs = np.empty((wfPlotNumber, fitSamples)) for idx, ( r, phi, z, scale, t0, smooth, ) in enumerate(samples[np.random.randint(len(samples), size=wfPlotNumber)]): simWfs[idx, :] = det.MakeSimWaveform( r, phi, z, scale, t0, fitSamples, h_smoothing=smooth, ) # plt.ioff() plt.ion() residFig = plt.figure(3) helpers.plotResidual(simWfs, wf.windowedWf, figure=residFig) plt.savefig("waveform_fig.png") plt.figure(4) plt.hist(samples[:, 0], bins=40) plt.xlabel("radius [mm]") plt.figure(5) plt.hist(samples[:, 2], bins=40) plt.ylabel("height [mm]") # lnprobFig = plt.figure(4) # plt.clf() # lnprobs = sampler.lnprobability[:, burnIn:].reshape((-1)) # median_prob = np.median(lnprobs) # print "median lnprob is %f (length normalized: %f)" % (median_prob, median_prob/wf.wfLength) # plt.hist(lnprobs) # positionFig = plt.figure(6) plt.hist2d(samples[:, 0], samples[:, 2], norm=LogNorm(), bins=[ np.around(det.detector_radius, 1) * 10, np.around(det.detector_length, 1) * 10 ]) plt.xlim(0, det.detector_radius) plt.ylim(0, det.detector_length) plt.colorbar() lnprobFig = plt.figure(7) plt.clf() lnprobs = sampler.lnprobability[:, :, burnIn:].reshape((-1)) median_prob = np.median(lnprobs) print "median lnprob is %f (length normalized: %f)" % ( median_prob, median_prob / wf.wfLength) hist, bins = np.histogram(lnprobs / wf.wfLength, bins=np.linspace(-10, 0, 1000)) plt.plot(bins[:-1], hist, ls="steps-post") # plt.show() value = raw_input(' --> Press q to quit, any other key to continue\n') if value == 'q': exit(0)