def createSignalModelExponential(data): """ Toy model that treats the first ~10% of the waveform as an exponential. Does a good job of finding the start time (t_0) Since I made this as a toy, its super brittle. Waveform must be normalized """ print "Creating model" switchpoint = DiscreteUniform('switchpoint', lower=0, upper=len(data)) noise_sigma = HalfNormal('noise_sigma', tau=sigToTau(.01)) exp_sigma = HalfNormal('exp_sigma', tau=sigToTau(.05)) #Modeling these parameters this way is why wf needs to be normalized exp_rate = Uniform('exp_rate', lower=0, upper=.1) exp_scale = Uniform('exp_scale', lower=0, upper=.1) timestamp = np.arange(0, len(data), dtype=np.float) @deterministic(plot=False, name="test") def uncertainty_model(s=switchpoint, n=noise_sigma, e=exp_sigma): ''' Concatenate Poisson means ''' out = np.empty(len(data)) out[:s] = n out[s:] = e return out @deterministic def tau(eps=uncertainty_model): return np.power(eps, -2) ## @deterministic(plot=False, name="test2") ## def adjusted_scale(s=switchpoint, s1=exp_scale): ## out = np.empty(len(data)) ## out[:s] = s1 ## out[s:] = s1 ## return out # # scale_param = adjusted_scale(switchpoint, exp_scale) @deterministic(plot=False) def baseline_model(s=switchpoint, r=exp_rate, scale=exp_scale): out = np.zeros(len(data)) out[s:] = scale * (np.exp(r * (timestamp[s:] - s)) - 1.) # plt.figure(fig.number) # plt.clf() # plt.plot(out ,color="blue" ) # plt.plot(data ,color="red" ) # value = raw_input(' --> Press q to quit, any other key to continue\n') return out baseline_observed = Normal("baseline_observed", mu=baseline_model, tau=tau, value=data, observed=True) return locals()
def createSignalModel(data): #set up your model parameters switchpoint = DiscreteUniform('switchpoint', lower=0, upper=len(data)) early_sigma = HalfNormal('early_sigma', tau=sigToTau(1)) late_sigma = HalfNormal('late_sigma', tau=sigToTau(1)) early_mu = Normal('early_mu', mu=.5, tau=sigToTau(1)) late_mu = Normal('late_mu', mu=.5, tau=sigToTau(1)) #set up the model for uncertainty (ie, the noise) and the signal (ie, the step function) ############################ @deterministic(plot=False, name="test") def uncertainty_model(s=switchpoint, n=early_sigma, e=late_sigma): #Concatenate Uncertainty sigmas (or taus or whatever) around t0 s = np.around(s) out = np.empty(len(data)) out[:s] = n out[s:] = e return out ############################ @deterministic def tau(eps=uncertainty_model): #pymc uses this tau parameter instead of sigma to model a gaussian. its annoying. return np.power(eps, -2) ############################ @deterministic(plot=False, name="siggenmodel") def signal_model(s=switchpoint, e=early_mu, l=late_mu): #makes the step function using the means out = np.zeros(len(data)) out[:s] = e out[s:] = l return out ############################ #Full model: normally distributed noise around a step function baseline_observed = Normal("baseline_observed", mu=signal_model, tau=tau, value=data, observed=True) return locals()
def createSignalModelSiggen(data, t0_guess, energy_guess, noise_sigma_guess, baseline_guess): verbose = 0 slowness_sigma = HalfNormal('slowness_sigma', tau=.01) switchpoint = Normal('switchpoint', mu=t0_guess, tau=.001) wfScale = Normal('wfScale', mu=energy_guess, tau=sigToTau(.25 * energy_guess)) # baselineB = Normal('baselineB', mu=baseline_guess, tau=10) # baselineM = Normal('baselineM', mu=0, tau=1000000) #### Baseline noise Model # noise_sigma = HalfNormal('noise_sigma', tau=.01) # @deterministic # def tau(eps=noise_sigma): # return np.power(eps, -2) noise_tau = np.power(noise_sigma_guess, -2) if verbose: print "t0 guess is %d" % t0_guess print "noise sigma guess is %0.2f" % noise_sigma_guess print "t0 init is %d" % switchpoint print "wfScale init is %f" % wfScale print "noise tau is %0.2e" % noise_tau ############################ @deterministic(plot=False, name="siggenmodel") def siggen_model(s=switchpoint, e=wfScale, sig=slowness_sigma): out = np.zeros(len(data)) # out = np.multiply(baselineM, out) # out = np.add(baselineB, out) siggen_data = siggen_wf #findSiggenWaveform(rad,phi,z) if e < 0: e = 0 #siggen_data *= e if s < 0: s = 0 if s > len(data): s = len(data) s = np.around(s) out[s:] += siggen_data[0:(len(data) - s)] * e out = ndimage.filters.gaussian_filter1d(out, sig) if doPlots: plt.figure(fig.number) plt.clf() plt.plot(data, color="red") plt.plot(out, color="blue") value = raw_input( ' --> Press q to quit, any other key to continue\n') if value == 'q': exit(1) return out ############################ baseline_observed = Normal("baseline_observed", mu=siggen_model, tau=noise_tau, value=data, observed=True) return locals()
def CreateFullDetectorModel(detector, data, t0_guess, energy_guess, rcint_guess, rcdiff_guess): z_min = 0 #temporary hack to keep you off the taper # switchpoint = t0_gues #This is friggin ridiculous #noise_sigma = HalfNormal('baseline_sigma', tau=sigToTau(.01)) siggen_sigma = HalfNormal('siggen_sigma', tau=sigToTau(.01)) siggen_tau = np.power(siggen_sigma, -2) radEst = Uniform('radEst', lower=0, upper=np.floor(detector.radius)) zEst = Uniform('zEst', lower=z_min, upper=np.floor(detector.length)) phiEst = Uniform('phiEst', lower=0, upper=np.pi / 4) # print "rc int guess is %f" % rcint_guess # print "rc diff guess is %f" % rcdiff_guess rc_int = Normal('rc_int', mu=rcint_guess, tau=sigToTau(1.)) #should be in ns rc_diff = Normal('rc_diff', mu=rcdiff_guess, tau=sigToTau(100.)) #should print "z value is %f" % zEst.value if zEst.value < 5: "setting initial z value guess safely above 5 mm" zEst.value = 5 switchpoint = Normal('switchpoint', mu=t0_guess, tau=sigToTau(1)) wfScale = Normal('wfScale', mu=energy_guess, tau=sigToTau(.01 * energy_guess)) print "switchpoint is %d" % switchpoint print "wfScale is %f" % wfScale ############################ @deterministic(plot=False, name="siggenmodel") def siggen_model(s=switchpoint, rad=radEst, phi=phiEst, z=zEst, e=wfScale, rise_time=rc_int, fall_time=rc_diff): # print "rc diff is %0.3f" % fall_time out = np.zeros(len(data)) #Let the rounding happen organically in the detector model... # siggen_data = detector.GetWaveformByPosition(rad,phi,z) detector.preampRiseTime = rise_time detector.preampFallTime = fall_time siggen_wf = detector.GetSiggenWaveform(rad, phi, z, energy=2600) # print siggen_wf if siggen_wf is None: return -np.inf if np.amax(siggen_wf) == 0: print "wtf is even happening here?" return -np.inf siggen_data = detector.ProcessWaveform(siggen_wf) siggen_data = siggen_data[500::] siggen_data *= e s = np.around(s) if s < 0: #print "S is zero dude." s = 0 out[s:] = siggen_data[0:(len(data) - s)] if doPlots: if np.isnan(siggen_data[0]): return out plt.figure(11) plt.clf() plt.plot(data, color="red") plt.plot(out, color="blue") print "r: %0.1f, z: %0.1f" % (rad, z) value = raw_input( ' --> Press q to quit, any other key to continue\n') if value == 'q': exit(1) return out ############################ baseline_observed = Normal("baseline_observed", mu=siggen_model, tau=siggen_tau, value=data, observed=True) return locals()
T = get_data('T') I = get_data('I') Rf = get_data('Rf') D = get_data('D') vn = get_data(dictsp.get(species)) ''' Priors ''' alpha = Uniform('alpha', lower=0.001, upper=0.2, value=0.02) c = Uniform('c', lower=2, upper=20, value=16) g1 = Uniform('g1', lower=1, upper=100, value=50) kxmax = Uniform('kxmax', lower=0.5, upper=10, value=4.5) Lamp = Uniform('Lamp', lower=0, upper=1, value=0.5) Lave = Uniform('Lave', lower=1, upper=3, value=2) LTf = Uniform('LTf', lower=1 / 3650, upper=1 / 365, value=1 / 1000) p50 = Uniform('p50', lower=-10, upper=-0.1, value=-5) Z = Uniform('Z', lower=0.5, upper=5, value=3) sigma = HalfNormal('sigma', tau=1) ''' deterministic model ''' @deterministic def muf( alpha=alpha, c=c, g1=g1, kxmax=kxmax, Lamp=Lamp, Lave=Lave, LTf=LTf, p50=p50, Z=Z, ca=400,
def createSignalModelSiggen(data, t0_guess, energy_guess, baseline_guess): rAvg = detRad zAvg = detZ / 2 phiAvg = np.pi / 8 print "t0 guess is %d" % t0_guess noise_sigma = HalfNormal('noise_sigma', tau=.01) slowness_sigma = HalfNormal('slowness_sigma', tau=.01) switchpoint = Normal('switchpoint', mu=t0_guess, tau=.005) wfScale = Normal('wfScale', mu=energy_guess, tau=sigToTau(.05 * energy_guess)) # baselineB = Normal('baselineB', mu=baseline_guess, tau=10) # baselineM = Normal('baselineM', mu=0, tau=1000000) print "switchpoint is %d" % switchpoint print "wfScale is %f" % wfScale ############################ @deterministic def tau(eps=noise_sigma): return np.power(eps, -2) ############################ @deterministic(plot=False, name="siggenmodel") def siggen_model(s=switchpoint, rad=rAvg, phi=phiAvg, z=zAvg, e=wfScale, sig=slowness_sigma): out = np.zeros(len(data)) # out = np.multiply(baselineM, out) # out = np.add(baselineB, out) siggen_data = findSiggenWaveform(rad, phi, z) if e < 0: e = 0 siggen_data *= e if s < 0: s = 0 if s > len(data): s = len(data) s = np.around(s) out[s:] += siggen_data[0:(len(data) - s)] out = ndimage.filters.gaussian_filter1d(out, sig) if doPlots: plt.figure(fig.number) plt.clf() plt.plot(data, color="red") plt.plot(out, color="blue") value = raw_input( ' --> Press q to quit, any other key to continue\n') if value == 'q': exit(1) return out ############################ baseline_observed = Normal("baseline_observed", mu=siggen_model, tau=tau, value=data, observed=True) return locals()
def createSignalModelSiggen(data, t0_guess, energy_guess): # switchpoint = t0_gues noise_sigma = HalfNormal('noise_sigma', tau=sigToTau(.01)) exp_sigma = HalfNormal('exp_sigma', tau=sigToTau(.05)) radEst = Uniform('radEst', lower=0, upper=detRad) zEst = Uniform('zEst', lower=5, upper=detZ) phiEst = Uniform('phiEst', lower=0, upper=np.pi / 4) switchpoint = Normal('switchpoint', mu=t0_guess, tau=sigToTau(1)) wfScale = Normal('wfScale', mu=energy_guess, tau=sigToTau(.01 * energy_guess)) print "switchpoint is %d" % switchpoint print "wfScale is %f" % wfScale ############################ @deterministic(plot=False, name="test") def uncertainty_model(s=switchpoint, n=noise_sigma, e=exp_sigma): ''' Concatenate Uncertainty sigmas (or taus or whatever) ''' s = np.around(s) out = np.empty(len(data)) out[:s] = n out[s:] = e return out ############################ @deterministic def tau(eps=uncertainty_model): return np.power(eps, -2) ############################ @deterministic(plot=False, name="siggenmodel") def siggen_model(s=switchpoint, rad=radEst, phi=phiEst, z=zEst, e=wfScale): out = np.zeros(len(data)) siggen_data = findSiggenWaveform(rad, phi, z) siggen_data *= e s = np.around(s) out[s:] = siggen_data[0:(len(data) - s)] if doPlots: plt.figure(fig.number) plt.clf() plt.plot(data, color="red") plt.plot(out, color="blue") value = raw_input( ' --> Press q to quit, any other key to continue\n') if value == 'q': exit(1) return out ############################ baseline_observed = Normal("baseline_observed", mu=siggen_model, tau=tau, value=data, observed=True) return locals()