Example #1
0
    def from_prior(self):
        """
         Unlike in C++, this must *return* a numpy array of parameters.
         """
        r_arr = np.empty(num_waveforms)
        z_arr = np.empty(num_waveforms)

        rad_arr = np.empty(num_waveforms)
        phi_arr = np.empty(num_waveforms)
        theta_arr = np.empty(num_waveforms)
        scale_arr = np.empty(num_waveforms)
        t0_arr = np.empty(num_waveforms)
        smooth_arr = np.empty(num_waveforms)
        m_arr = np.empty(num_waveforms)
        b_arr = np.empty(num_waveforms)

        print "\n"
        #draw 8 waveform params for each waveform
        for (wf_idx, wf) in enumerate(wfs):
            (r, z, scale, t0) = draw_position(wf_idx)
            smooth_guess = 10

            r_arr[wf_idx] = r
            z_arr[wf_idx] = z
            # rad_arr[wf_idx] = rad
            phi_arr[wf_idx] = rng.rand() * np.pi / 4
            # theta_arr[wf_idx] = theta
            scale_arr[wf_idx] = 5 * rng.randn() + scale
            t0_arr[wf_idx] = 3 * rng.randn() + maxt_guess
            smooth_arr[wf_idx] = np.clip(rng.randn() + smooth_guess, 0, 20)
            m_arr[wf_idx] = 0.0001 * rng.randn() + 0.
            b_arr[wf_idx] = 0.001 * rng.randn() + 0.

        b = 70 * rng.rand() - 50

        log_gain = 10 * rng.rand() - 10
        gain = np.exp(log_gain)

        d2 = 0.25 * rng.rand() + 0.5

        #limit from 60 to 90
        rc1 = dnest4.wrap(prior_vars[rc1_idx] * rng.randn() + priors[rc1_idx],
                          65, 80)
        rc2 = dnest4.wrap(prior_vars[rc2_idx] * rng.randn() + priors[rc2_idx],
                          0.1, 10)
        rcfrac = dnest4.wrap(
            prior_vars[rcfrac_idx] * rng.randn() + priors[rcfrac_idx], 0.9, 1)

        return np.hstack([
            b, gain, d2, rc1, rc2, rcfrac, r_arr[:], phi_arr[:], z_arr[:],
            scale_arr[:], t0_arr[:], smooth_arr[:], m_arr[:], b_arr[:]
            #   rad_arr[:], phi_arr[:], theta_arr[:], scale_arr[:], t0_arr[:],smooth_arr[:], m_arr[:], b_arr[:]
        ])
Example #2
0
def random_position(r, z):
    r_init, z_init = r, z
    r += dnest4.randh() * 0.1
    z += dnest4.randh() * 0.1

    r = dnest4.wrap(r, 0, detector.detector_radius)
    z = dnest4.wrap(z, 0, detector.detector_length)

    if not detector.IsInDetector(r, 0.1, z):
        #    print "not in detector..."
        return random_position(r_init, z_init)
    else:
        return (r, z)
Example #3
0
def random_position(r, z):
  r_init,z_init = r,z
  r += dnest4.randh()*0.1
  z += dnest4.randh()*0.1

  r = dnest4.wrap(r, 0, detector.detector_radius)
  z = dnest4.wrap(z, 0, detector.detector_length)

  if not detector.IsInDetector(r, 0.1, z):
#    print "not in detector..."
    return random_position(r_init,z_init)
  else:
    return (r,z)
Example #4
0
     def from_prior(self):
         """
         Unlike in C++, this must *return* a numpy array of parameters.
         """
         r_arr      = np.empty(num_waveforms)
         z_arr      = np.empty(num_waveforms)

         rad_arr      = np.empty(num_waveforms)
         phi_arr    = np.empty(num_waveforms)
         theta_arr  = np.empty(num_waveforms)
         scale_arr  = np.empty(num_waveforms)
         t0_arr     = np.empty(num_waveforms)
         smooth_arr = np.empty(num_waveforms)
         m_arr      = np.empty(num_waveforms)
         b_arr      = np.empty(num_waveforms)

         print "\n"
         #draw 8 waveform params for each waveform
         for (wf_idx, wf) in enumerate(wfs):
             (r,z, scale, t0) = draw_position(wf_idx)
             smooth_guess = 10

             r_arr[wf_idx] = r
             z_arr[wf_idx] = z
             # rad_arr[wf_idx] = rad
             phi_arr[wf_idx] = rng.rand() * np.pi/4
             # theta_arr[wf_idx] = theta
             scale_arr[wf_idx] = 5*rng.randn() + scale
             t0_arr[wf_idx] = 3*rng.randn() + maxt_guess
             smooth_arr[wf_idx] = np.clip(rng.randn() + smooth_guess, 0, 20)
             m_arr[wf_idx] =  0.0001*rng.randn() + 0.
             b_arr[wf_idx] =  0.001*rng.randn() + 0.

         b = 70*rng.rand() - 50

         log_gain = 10*rng.rand() - 10
         gain = np.exp(log_gain)

         d2 =  0.25 *rng.rand() + 0.5

         #limit from 60 to 90
         rc1 = dnest4.wrap(prior_vars[rc1_idx]*rng.randn() + priors[rc1_idx], 65, 80)
         rc2 = dnest4.wrap(prior_vars[rc2_idx]*rng.randn() + priors[rc2_idx], 0.1, 10)
         rcfrac = dnest4.wrap(prior_vars[rcfrac_idx]*rng.randn() + priors[rcfrac_idx], 0.9, 1)

         return np.hstack([
               b, gain, d2,
               rc1, rc2, rcfrac,
               r_arr[:], phi_arr[:], z_arr[:], scale_arr[:], t0_arr[:],smooth_arr[:], m_arr[:], b_arr[:]
             #   rad_arr[:], phi_arr[:], theta_arr[:], scale_arr[:], t0_arr[:],smooth_arr[:], m_arr[:], b_arr[:]
             ])
    def perturb_detector(self, params, which):
        logH = 0.0

        if which == ba_idx: #lets call this phi
           params[which] += (tf_phi_max + np.pi/2)*dnest4.randh()
           params[which] = dnest4.wrap(params[which], -np.pi/2, tf_phi_max)
        elif which == c_idx: #call it omega
            params[which] += 0.1*dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0.07, 0.2)
        elif which == dc_idx: #d
            params[which] += 0.01*dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0.7, 0.9)

        elif which == rc1_idx or which == rc2_idx or which == rcfrac_idx:
            #all normally distributed priors
            logH -= -0.5*((params[which] - priors[which])/prior_vars[which])**2
            params[which] += prior_vars[which]*dnest4.randh()
            logH += -0.5*((params[which] - priors[which])/prior_vars[which])**2

        elif which == aliasrc_idx:
          params[which] += 9.9*dnest4.randh()
          params[which] = dnest4.wrap(params[which], 0.1, 10)

        elif which == grad_idx:
          logH -= -0.5*((params[which] - priors[which])/prior_vars[which])**2
          params[which] += prior_vars[which] *dnest4.randh()
          params[which] = dnest4.wrap(params[which], detector.gradList[0], detector.gradList[-1])
          logH += -0.5*((params[which] - priors[which])/prior_vars[which])**2

        elif which == imp_avg_idx:
          logH -= -0.5*((params[which] - priors[which])/prior_vars[which])**2
          params[which] += prior_vars[which] *dnest4.randh()
          params[which] = dnest4.wrap(params[which], detector.impAvgList[0], detector.impAvgList[-1])
          logH += -0.5*((params[which] - priors[which])/prior_vars[which])**2

        elif which == trap_idx:
            params[which] += (1000 - traprc_min)*dnest4.randh()
            params[which] = dnest4.wrap(params[which], traprc_min, 1000)

        elif which >= velo_first_idx and which < velo_first_idx+4:
            logH -= -0.5*((params[which] - priors[which])/prior_vars[which])**2
            params[which] += prior_vars[which]*dnest4.randh()
            params[which] = dnest4.wrap(params[which], 1, priors[which]*10)
            logH += -0.5*((params[which] - priors[which])/prior_vars[which])**2

        elif which == velo_first_idx+4 or which == velo_first_idx+5:
          params[which] += (beta_lims[1] - beta_lims[0])  *dnest4.randh()
          params[which] = dnest4.wrap(params[which], beta_lims[0], beta_lims[1])

        else: #velocity or rc params: cant be below 0, can be arb. large
            print ("which value %d not supported" % which)
            exit(0)

        return logH
Example #6
0
    def get_new_theta(self, rad, theta):
        detector = self.detector
        if rad < np.amin([
                detector.detector_radius - detector.taper_length,
                detector.detector_length
        ]):
            max_val = np.pi / 2
            min_val = 0
        else:
            if rad < detector.detector_radius - detector.taper_length:
                #can't possibly hit the taper
                min_val = 0
            elif rad < np.sqrt(detector.detector_radius**2 +
                               detector.taper_length**2):
                #low enough that it could hit the taper region
                a = detector.detector_radius - detector.taper_length
                z = 0.5 * (np.sqrt(2 * rad**2 - a**2) - a)
                min_val = np.arcsin(z / rad)
            else:
                #longer than could hit the taper
                min_val = np.arccos(detector.detector_radius / rad)

            if rad < detector.detector_length:
                max_val = np.pi / 2
            else:
                max_val = np.pi / 2 - np.arccos(detector.detector_length / rad)

        new_theta = theta + (max_val - min_val) * dnest4.randh()
        new_theta = dnest4.wrap(new_theta, min_val, max_val)
        return new_theta
Example #7
0
    def get_new_theta(self,rad,theta):
        detector = self.detector
        if rad < np.amin([detector.detector_radius - detector.taper_length, detector.detector_length]):
            max_val = np.pi/2
            min_val = 0
        else:
            if rad < detector.detector_radius - detector.taper_length:
                #can't possibly hit the taper
                min_val = 0
            elif rad < np.sqrt(detector.detector_radius**2 + detector.taper_length**2):
                #low enough that it could hit the taper region
                a = detector.detector_radius - detector.taper_length
                z = 0.5 * (np.sqrt(2*rad**2-a**2) - a)
                min_val = np.arcsin(z/rad)
            else:
                #longer than could hit the taper
                min_val = np.arccos(detector.detector_radius/rad)

            if rad < detector.detector_length:
                max_val = np.pi/2
            else:
                max_val = np.pi/2 - np.arccos(detector.detector_length/rad)

        new_theta = theta + (max_val - min_val)*dnest4.randh()
        new_theta = dnest4.wrap(new_theta, min_val, max_val)
        return new_theta
    def perturb(self, params):
        """
        Perturb the current parameters by proposing a new position. This takes a numpy array of
        parameters as input, and modifies it in-place. In just perturbs one parameter at a time.

        Args:
            params (:class:`np.ndarray`): the current parameters (this is modified by the function)

        Returns:
            float: the natural logarithm of the Metropolis-Hastings proposal ratio.
        """
        logH = 0.0 # log of the Metropolis-Hastings prior x proposal ratio
        
        # randomly choose which parameter to perturb 
        which = np.random.randint(len(params))
        mmu = 0.
        msigma = 10.
        if which == 0:
            # update H for Gaussian prior
            logH -= -0.5*((params[which]-mmu)/msigma)**2
        params[which] += 1.*randh() # scale factor of 1. (randh is a heavy-tailed distribution to occasionally sample distant values, although this isn't really required in this case)

        if which == 0:
            # update H for Gaussian prior
            logH += -0.5*((params[which]-mmu)/msigma)**2
        else:
            # wrap c value so that it stays within the prior range
            params[which] = wrap(params[which], cmin, cmax)

        return logH
Example #9
0
    def perturb(self, params):
        """
        Perturb the current parameters by proposing a new position. This takes a numpy array of
        parameters as input, and modifies it in-place. In just perturbs one parameter at a time.

        Args:
            params (:class:`np.ndarray`): the current parameters (this is modified by the function)

        Returns:
            float: the natural logarithm of the Metropolis-Hastings proposal ratio.
        """
        logH = 0.0  # log of the Metropolis-Hastings prior x proposal ratio

        # randomly choose which parameter to perturb
        which = np.random.randint(len(params))
        mmu = 0.
        msigma = 10.
        if which == 0:
            # update H for Gaussian prior
            logH -= -0.5 * ((params[which] - mmu) / msigma)**2
        params[which] += 1. * randh(
        )  # scale factor of 1. (randh is a heavy-tailed distribution to occasionally sample distant values, although this isn't really required in this case)

        if which == 0:
            # update H for Gaussian prior
            logH += -0.5 * ((params[which] - mmu) / msigma)**2
        else:
            # wrap c value so that it stays within the prior range
            params[which] = wrap(params[which], cmin, cmax)

        return logH
Example #10
0
    def get_new_rad(self, rad, theta):
        detector = self.detector
        #FIND THE MAXIMUM RADIUS STILL INSIDE THE DETECTOR
        theta_eq = np.arctan(detector.detector_length /
                             detector.detector_radius)
        theta_taper = np.arctan(detector.taper_length /
                                detector.detector_radius)
        if theta <= theta_taper:
            z = np.tan(theta) * (detector.detector_radius -
                                 detector.taper_length) / (1 - np.tan(theta))
            max_rad = z / np.sin(theta)
        elif theta <= theta_eq:
            max_rad = detector.detector_radius / np.cos(theta)
        else:
            theta_comp = np.pi / 2 - theta
            max_rad = detector.detector_length / np.cos(theta_comp)

        #AND THE MINIMUM (from PC dimple)
        #min_rad  = 1./ ( np.cos(theta)**2/detector.pcRad**2  +  np.sin(theta)**2/detector.pcLen**2 )

        min_rad = np.amax([detector.pcRad, detector.pcLen])

        new_rad = rad + (max_rad - min_rad) * dnest4.randh()
        new_rad = dnest4.wrap(new_rad, min_rad, max_rad)
        return new_rad
Example #11
0
 def perturb(self, coords):
     """The perturb function to perform Monte Carlo trial moves."""
     idx = np.random.randint(self._n_dim)
     coords[idx] += (self._widths[idx] * (np.random.uniform(size=1) - 0.5))
     cw = self._widths[idx]
     cc = self._centers[idx]
     # Note: wrapping like this effectively truncates soft priors, which
     # may or may not be problematic; I'm not entirely sure either way.
     # However, DNest4 seems to have trouble if you don't wrap. -- Blake
     # DNest4 Note: use the return value of wrap, unlike in C++
     coords[idx] = dnest4.wrap(coords[idx], (cc - 0.5 * cw), cc + 0.5 * cw)
     return 0.0
Example #12
0
    def perturb_gaussian_parameter(self, parameter):
        mu = self.mean
        var = self.variance

        logH = 0
        logH -= -0.5*((parameter - mu)/var)**2
        parameter += var*dnest4.randh()
        if (self.lim_lo > -max_float) or (self.lim_hi<np.inf):
            parameter = dnest4.wrap(parameter, self.lim_lo, self.lim_hi)
        logH += -0.5*((parameter - mu)/var)**2

        return (logH, parameter)
Example #13
0
    def perturb(self, params):
        """
        Unlike in C++, this takes a numpy array of parameters as input,
        and modifies it in-place. The return value is still logH.
        """
        logH = 0.0

        which = rng.randint(len(params))

        if which == 0:
            params[which] += (detector.detector_radius) * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0,
                                        detector.detector_radius)
        elif which == 1:
            max_val = np.pi / 4
            params[which] += np.pi / 4 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0, max_val)
            if params[which] < 0 or params[which] > np.pi / 4:
                print "wtf phi"
            #params[which] = np.clip(params[which], 0, max_val)
        elif which == 2:
            params[which] += (detector.detector_length) * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0,
                                        detector.detector_length)

        elif which == 3:  #scale
            min_scale = wf.wfMax - 0.01 * wf.wfMax
            max_scale = wf.wfMax + 0.005 * wf.wfMax
            params[which] += (max_scale - min_scale) * dnest4.randh()
            params[which] = dnest4.wrap(params[which], min_scale, max_scale)
        #   print "  adjusted scale to %f" %  ( params[which])

        elif which == 4:  #t0
            params[which] += 1 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], min_maxt, max_maxt)
        elif which == 5:  #smooth
            params[which] += 0.1 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0, 25)
            #   print "  adjusted smooth to %f" %  ( params[which])

            # elif which == 6: #wf baseline slope (m)
            #     logH -= -0.5*(params[which]/1E-4)**2
            #     params[which] += 1E-4*dnest4.randh()
            #     logH += -0.5*(params[which]/1E-4)**2
            # elif which == 7: #wf baseline incercept (b)
            #     logH -= -0.5*(params[which]/1E-2)**2
            #     params[which] += 1E-2*dnest4.randh()
            #     logH += -0.5*(params[which]/1E-2)**2

            #   params[which] += 0.01*dnest4.randh()
            #   params[which]=dnest4.wrap(params[which], -1, 1)
            #   print "  adjusted b to %f" %  ( params[which])

        else:  #velocity or rc params: cant be below 0, can be arb. large
            print "which value %d not supported" % which
            exit(0)

        return logH
Example #14
0
    def perturb_detector(self, params):
        which = rng.randint(len(priors))

        if which == ba_idx:  #b over a
            params[which] += 0.1 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], -0.9, 15)
        elif which == c_idx:  #b over a
            params[which] += 0.01 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], -0.9, -0.7)
        elif which == dc_idx:  #b over a
            params[which] += 0.01 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], -1.05, -0.975)
        elif which == rc1_idx:
            params[which] += 30 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 60, 90)
        elif which == rc2_idx:
            params[which] += 5 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0, 5)
        elif which == rcfrac_idx:
            params[which] += 0.1 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0.9, 1)
        elif which == grad_idx:
            params[which] += (len(detector.gradList) - 1) * dnest4.randh()
            params[which] = np.int(
                dnest4.wrap(params[which], 0,
                            len(detector.gradList) - 1))

        elif which >= velo_first_idx and which < velo_first_idx + 6:
            params[which] += (velo_width * priors[which] -
                              1 / velo_width * priors[which]) * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0.1 * priors[which],
                                        (10.) * priors[which])
        elif which == trap_idx:
            log_traprc = np.log(params[which])
            log_traprc += 20 * dnest4.randh()
            log_traprc = dnest4.wrap(log_traprc, 0., 20.0)
            params[which] = np.exp(log_traprc)

        else:  #velocity or rc params: cant be below 0, can be arb. large
            print "which value %d not supported" % which
            exit(0)
Example #15
0
    def perturb_detector(self, params):
        which = rng.randint(len(priors))

        if which == ba_idx: #b over a
          params[which] += 0.1*dnest4.randh()
          params[which] = dnest4.wrap(params[which], -0.9, 15)
        elif which == c_idx: #b over a
            params[which] += 0.01*dnest4.randh()
            params[which] = dnest4.wrap(params[which], -0.9, -0.7)
        elif which == dc_idx: #b over a
            params[which] += 0.01*dnest4.randh()
            params[which] = dnest4.wrap(params[which], -1.05, -0.975)
        elif which == rc1_idx:
          params[which] += 30*dnest4.randh()
          params[which] = dnest4.wrap(params[which], 60, 90)
        elif which == rc2_idx:
          params[which] += 5*dnest4.randh()
          params[which] = dnest4.wrap(params[which], 0, 5)
        elif which == rcfrac_idx:
          params[which] += 0.1*dnest4.randh()
          params[which] = dnest4.wrap(params[which], 0.9, 1)
        elif which == grad_idx:
          params[which] += (len(detector.gradList)-1)*dnest4.randh()
          params[which] = np.int(dnest4.wrap(params[which], 0, len(detector.gradList)-1))

        elif which >= velo_first_idx and which < velo_first_idx+6:
            params[which] += (velo_width*priors[which] - 1/velo_width*priors[which])  *dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0.1*priors[which], (10.)*priors[which])
        elif which == trap_idx:
            log_traprc = np.log(params[which])
            log_traprc += 20*dnest4.randh()
            log_traprc = dnest4.wrap(log_traprc, 0., 20.0)
            params[which] = np.exp(log_traprc)

        else: #velocity or rc params: cant be below 0, can be arb. large
            print "which value %d not supported" % which
            exit(0)
Example #16
0
    def perturb(self, params):
        """
        Unlike in C++, this takes a numpy array of parameters as input,
        and modifies it in-place. The return value is still logH.
        """
        logH = 0.0
        which = rng.randint(3)

        if which == 0 or which == 1:
            logH -= -0.5*(params[which]/1E3)**2
            params[which] += 1E3*dnest4.randh()
            logH += -0.5*(params[which]/1E3)**2
        else:
            log_sigma = np.log(params[2])
            log_sigma += 20*dnest4.randh()
            # Note the difference between dnest4.wrap in Python and
            # DNest4::wrap in C++. The former *returns* the wrapped value.
            log_sigma = dnest4.wrap(log_sigma, -10.0, 10.0)
            params[2] = np.exp(log_sigma)

        return logH
Example #17
0
    def perturb(self, params):
        """
        Unlike in C++, this takes a numpy array of parameters as input,
        and modifies it in-place. The return value is still logH.
        """
        logH = 0.0
        which = rng.randint(3)

        if which == 0 or which == 1:
            logH -= -0.5 * (params[which] / 1E3)**2
            params[which] += 1E3 * dnest4.randh()
            logH += -0.5 * (params[which] / 1E3)**2
        else:
            log_sigma = np.log(params[2])
            log_sigma += 20 * dnest4.randh()
            # Note the difference between dnest4.wrap in Python and
            # DNest4::wrap in C++. The former *returns* the wrapped value.
            log_sigma = dnest4.wrap(log_sigma, -10.0, 10.0)
            params[2] = np.exp(log_sigma)

        return logH
Example #18
0
    def get_new_rad(self,rad, theta):
          detector = self.detector
          #FIND THE MAXIMUM RADIUS STILL INSIDE THE DETECTOR
          theta_eq = np.arctan(detector.detector_length/detector.detector_radius)
          theta_taper = np.arctan(detector.taper_length/detector.detector_radius)
          if theta <= theta_taper:
             z = np.tan(theta)*(detector.detector_radius - detector.taper_length) / (1-np.tan(theta))
             max_rad = z / np.sin(theta)
          elif theta <= theta_eq:
              max_rad = detector.detector_radius / np.cos(theta)
          else:
              theta_comp = np.pi/2 - theta
              max_rad = detector.detector_length / np.cos(theta_comp)

          #AND THE MINIMUM (from PC dimple)
          #min_rad  = 1./ ( np.cos(theta)**2/detector.pcRad**2  +  np.sin(theta)**2/detector.pcLen**2 )

          min_rad = np.amax([detector.pcRad, detector.pcLen])

          new_rad = rad + (max_rad - min_rad)*dnest4.randh()
          new_rad = dnest4.wrap(new_rad, min_rad, max_rad)
          return new_rad
Example #19
0
    def from_prior(self):
        """
        Unlike in C++, this must *return* a numpy array of parameters.
        """
        num_waveforms = self.mpi_manager.num_waveforms

        r_arr      = np.empty(num_waveforms)
        z_arr      = np.empty(num_waveforms)

        rad_arr      = np.empty(num_waveforms)
        phi_arr    = np.empty(num_waveforms)
        theta_arr  = np.empty(num_waveforms)
        scale_arr  = np.empty(num_waveforms)
        t0_arr     = np.empty(num_waveforms)
        smooth_arr = np.empty(num_waveforms)
        m_arr      = np.empty(num_waveforms)
        b_arr      = np.empty(num_waveforms)

        #draw 8 waveform params for each waveform
        for (wf_idx) in range(num_waveforms):
            (r,z, scale, t0) = self.draw_position(wf_idx)
            rad = np.sqrt(r**2+z**2)
            theta = np.arctan(z/r)

            smooth_guess = 10

            r_arr[wf_idx] = r
            z_arr[wf_idx] = z
            rad_arr[wf_idx] = rad
            phi_arr[wf_idx] = rng.rand() * np.pi/4
            theta_arr[wf_idx] = theta
            scale_arr[wf_idx] = 5*rng.randn() + scale - .005*scale
            t0_arr[wf_idx] = 3*rng.randn() + self.maxt_guess
            smooth_arr[wf_idx] = np.clip(rng.randn() + smooth_guess, 0, 20)
            # m_arr[wf_idx] =  0.0001*rng.randn() + 0.
            # b_arr[wf_idx] =  0.001*rng.randn() + 0.

        phi = (tf_phi_max + np.pi/2) * rng.rand() - np.pi/2
        omega = np.pi * rng.rand()
        d = rng.rand()

        #limit from 60 to 90
        rc1 = dnest4.wrap(prior_vars[rc1_idx]*rng.randn() + priors[rc1_idx], 65, 80)
        rc2 = dnest4.wrap(prior_vars[rc2_idx]*rng.randn() + priors[rc2_idx], 0.1, 10)
        rcfrac = dnest4.wrap(prior_vars[rcfrac_idx]*rng.randn() + priors[rcfrac_idx], 0.9, 1)

        grad = rng.rand()*( self.mpi_manager.detector.gradList[-1] - self.mpi_manager.detector.gradList[0] ) + self.mpi_manager.detector.gradList[0]
        avgImp = rng.rand()*( self.mpi_manager.detector.impAvgList[-1] - self.mpi_manager.detector.impAvgList[0] ) + self.mpi_manager.detector.impAvgList[0]
        charge_trapping = rng.rand()*(1000 - traprc_min) +  traprc_min

        aliasrc = dnest4.wrap(prior_vars[aliasrc_idx]*rng.randn() + priors[aliasrc_idx], 0.01, 10)

        #6 hole drift params
        h_100_va = (va_lims[1] - va_lims[0]) * rng.rand() + va_lims[0]
        h_100_vmax = (vmax_lims[1] - vmax_lims[0]) * rng.rand() + vmax_lims[0]
        h_100_beta = (beta_lims[1] - beta_lims[0]) * rng.rand() + beta_lims[0]

        h_111_va = (va_lims[1] - va_lims[0]) * rng.rand() + va_lims[0]
        h_111_vmax = (vmax_lims[1] - vmax_lims[0]) * rng.rand() + vmax_lims[0]
        h_111_beta = (beta_lims[1] - beta_lims[0]) * rng.rand() + beta_lims[0]


        return np.hstack([
              phi, omega, d,
              #b, c, dc,
              rc1, rc2, rcfrac,aliasrc,
              h_100_va, h_111_va, h_100_vmax, h_111_vmax, h_100_beta, h_111_beta,
              grad, avgImp, charge_trapping,
              rad_arr[:], phi_arr[:], theta_arr[:], scale_arr[:], t0_arr[:],smooth_arr[:],
            ])
Example #20
0
    def perturb_wf(self, params, wf_idx, ):
    #do both wf and detector params in case theres strong correlation
        logH = 0.0
        num_waveforms = self.mpi_manager.num_waveforms
        detector = self.mpi_manager.detector

        reps = 1
        if rng.rand() < 0.5:
            reps += np.int(np.power(100.0, rng.rand()));

        for i in range(reps):
            wf_which = rng.randint(6)

            # my_which = rng.randint(len(priors) + 8)

            # if my_which < len(priors):
            #     #detector variable
            #     logH += self.perturb_detector(params, my_which)
            #
            # else:
            if wf_which < 6:
                #this is a waveform variable!
                # wf_which =  np.int(my_which - len(priors))

                #which idx of the global params array
                which = len(priors) + wf_which*num_waveforms + wf_idx

                rad_idx = len(priors) + wf_idx
                theta_idx =  len(priors) + 2*num_waveforms+ wf_idx
                self.changed_wfs[wf_idx] = 1

                if wf_which == 0:
                  theta = params[theta_idx]

                  #FIND THE MAXIMUM RADIUS STILL INSIDE THE DETECTOR
                  theta_eq = np.arctan(detector.detector_length/detector.detector_radius)
                  theta_taper = np.arctan(detector.taper_length/detector.detector_radius)
                  if theta <= theta_taper:
                     z = np.tan(theta)*(detector.detector_radius - detector.taper_length) / (1-np.tan(theta))
                     max_rad = z / np.sin(theta)
                  elif theta <= theta_eq:
                      max_rad = detector.detector_radius / np.cos(theta)
                  else:
                      theta_comp = np.pi/2 - theta
                      max_rad = detector.detector_length / np.cos(theta_comp)

                  #AND THE MINIMUM (from PC dimple)
                  #min_rad  = 1./ ( np.cos(theta)**2/detector.pcRad**2  +  np.sin(theta)**2/detector.pcLen**2 )

                  min_rad = np.amax([detector.pcRad, detector.pcLen])

                  total_max_rad = np.sqrt(detector.detector_length**2 + detector.detector_radius**2 )

                  params[which] += total_max_rad*dnest4.randh()
                  params[which] = dnest4.wrap(params[which] , min_rad, max_rad)

                elif wf_which ==2: #theta
                  rad = params[rad_idx]

                  if rad < np.amin([detector.detector_radius - detector.taper_length, detector.detector_length]):
                      max_val = np.pi/2
                      min_val = 0
                  else:
                      if rad < detector.detector_radius - detector.taper_length:
                          #can't possibly hit the taper
                          min_val = 0
                      elif rad < np.sqrt(detector.detector_radius**2 + detector.taper_length**2):
                          #low enough that it could hit the taper region
                          a = detector.detector_radius - detector.taper_length
                          z = 0.5 * (np.sqrt(2*rad**2-a**2) - a)
                          min_val = np.arcsin(z/rad)
                      else:
                          #longer than could hit the taper
                          min_val = np.arccos(detector.detector_radius/rad)

                      if rad < detector.detector_length:
                          max_val = np.pi/2
                      else:
                          max_val = np.pi/2 - np.arccos(detector.detector_length/rad)

                  params[which] += np.pi/2*dnest4.randh()
                  params[which] = dnest4.wrap(params[which], min_val, max_val)

                # if wf_which == 0:
                #     params[which] += (detector.detector_radius)*dnest4.randh()
                #     params[which] = dnest4.wrap(params[which] , 0, detector.detector_radius)
                elif wf_which == 1:
                    max_val = np.pi/4
                    params[which] += np.pi/4*dnest4.randh()
                    params[which] = dnest4.wrap(params[which], 0, max_val)
                    if params[which] < 0 or params[which] > np.pi/4:
                        print ("wtf phi")

                # elif wf_which == 2:
                #     params[which] += (detector.detector_length)*dnest4.randh()
                #     params[which] = dnest4.wrap(params[which] , 0, detector.detector_length)

                elif wf_which == 3: #scale
                    wf = self.mpi_manager.wfs[wf_idx]
                    min_scale = wf.wfMax - 0.01*wf.wfMax
                    max_scale = wf.wfMax + 0.005*wf.wfMax
                    params[which] += (max_scale-min_scale)*dnest4.randh()
                    params[which] = dnest4.wrap(params[which], min_scale, max_scale)

                elif wf_which == 4: #t0
                  params[which] += 1*dnest4.randh()
                  params[which] = dnest4.wrap(params[which], self.min_maxt, self.max_maxt)
                elif wf_which == 5: #smooth
                  params[which] += 0.1*dnest4.randh()
                  params[which] = dnest4.wrap(params[which], 0, 25)

                else:
                    print( "wf which value %d (which value %d) not supported" % (wf_which, which) )
                    exit(0)

        return logH
Example #21
0
    def perturb_detector(self, params, which):
        logH = 0.0
        detector = self.detector
        num_waveforms = self.num_waveforms
        priors = self.priors
        prior_vars = self.prior_vars

        if which == phi_idx:  #lets call this phi
            logH -= -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2
            params[which] += prior_vars[which] * dnest4.randh()
            params[which] = dnest4.wrap(params[which], -np.pi, 0)
            logH += -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2
        elif which == omega_idx:  #call it omega
            logH -= -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2
            params[which] += prior_vars[which] * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0, np.pi)
            logH += -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2
        elif which == d_idx:  #d
            logH -= -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2
            params[which] += prior_vars[which] * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0.5, 0.999)
            # params[which] = dnest4.wrap(params[which], 0, 5000)
            logH += -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2

        elif which == rc1_idx or which == rc2_idx or which == rcfrac_idx:
            #all normally distributed priors
            logH -= -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2
            params[which] += prior_vars[which] * dnest4.randh()
            if which == rcfrac_idx:
                params[which] = dnest4.wrap(params[which], 0, 1)
            logH += -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2
        #
        # elif which == num0_idx:
        #   sig = 3*timeStepMult
        #   logH -= -0.5*((params[which] - 0.)/sig)**2
        #   params[which] += sig*dnest4.randh()
        #   logH += -0.5*((params[which] - 0.)/sig)**2

        # elif which == aliasrc_idx:
        #     params[which] += 19.9*dnest4.randh()
        #     params[which] = dnest4.wrap(params[which], 0.1, 20)

        elif which == grad_idx:
            # logH -= -0.5*((params[which] - priors[which])/prior_vars[which])**2
            # params[which] += prior_vars[which] *dnest4.randh()
            # params[which] = dnest4.wrap(params[which], detector.gradList[0], detector.gradList[-1])
            # logH += -0.5*((params[which] - priors[which])/prior_vars[which])**2

            paramlist = detector.gradList
            params[which] += (paramlist[-1] - paramlist[0]) * dnest4.randh()
            params[which] = dnest4.wrap(params[which], paramlist[0],
                                        paramlist[-1])

        elif which == imp_avg_idx:
            # logH -= -0.5*((params[which] - priors[which])/prior_vars[which])**2
            # params[which] += prior_vars[which] *dnest4.randh()
            # params[which] = dnest4.wrap(params[which], detector.impAvgList[0], detector.impAvgList[-1])
            # logH += -0.5*((params[which] - priors[which])/prior_vars[which])**2
            paramlist = detector.impAvgList
            params[which] += (paramlist[-1] - paramlist[0]) * dnest4.randh()
            params[which] = dnest4.wrap(params[which], paramlist[0],
                                        paramlist[-1])

        elif which == pcrad_idx:
            paramlist = detector.pcRadList
            params[which] += (paramlist[-1] - paramlist[0]) * dnest4.randh()
            params[which] = dnest4.wrap(params[which], paramlist[0],
                                        paramlist[-1])

        elif which == pclen_idx:
            paramlist = detector.pcLenList
            params[which] += (paramlist[-1] - paramlist[0]) * dnest4.randh()
            params[which] = dnest4.wrap(params[which], paramlist[0],
                                        paramlist[-1])

        elif which == trap_idx:
            # let's go with log10-uniform on this
            # log_trap = np.log10(params[which])
            # log_trap += 3*dnest4.randh()
            # log_trap = dnest4.wrap(log_trap, 1, 4)
            # params[which] = 10**(log_trap)

            # p_guess = 350
            # sig = 50
            # logH -= -0.5*((params[which] - p_guess )/sig)**2
            # params[which] += sig*dnest4.randh()
            # params[which] = dnest4.wrap(params[which], 100, 600)
            # logH += -0.5*((params[which] - p_guess)/sig)**2

            traprc_min = 10
            params[which] += (1000 - traprc_min) * dnest4.randh()
            params[which] = dnest4.wrap(params[which], traprc_min, 1000)

        elif which == untrap_idx:
            traprc_min = 1000
            params[which] += (1000000 - traprc_min) * dnest4.randh()
            params[which] = dnest4.wrap(params[which], traprc_min, 1000000)
        elif which == energy_idx:
            wf_guess = self.conf.energy_guess
            sig = 10

            logH -= -0.5 * ((params[which] - wf_guess) / sig)**2
            params[which] += sig * dnest4.randh()
            params[which] = dnest4.wrap(params[which], wf_guess - 30,
                                        wf_guess + 30)
            logH += -0.5 * ((params[which] - wf_guess) / sig)**2

        elif which >= velo_first_idx and which < velo_first_idx + 4:
            logH -= -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2
            params[which] += prior_vars[which] * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 1, priors[which] * 10)
            logH += -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2

        elif which == velo_first_idx + 4 or which == velo_first_idx + 5:
            params[which] += (self.conf.beta_lims[1] -
                              self.conf.beta_lims[0]) * dnest4.randh()
            params[which] = dnest4.wrap(params[which], self.conf.beta_lims[0],
                                        self.conf.beta_lims[1])

        else:  #velocity or rc params: cant be below 0, can be arb. large
            print("which value %d not supported" % which)
            exit(0)

        return logH
    def from_prior(self):
        """
        Unlike in C++, this must *return* a numpy array of parameters.
        """
        r_arr      = np.empty(num_waveforms)
        z_arr      = np.empty(num_waveforms)

        rad_arr      = np.empty(num_waveforms)
        phi_arr    = np.empty(num_waveforms)
        theta_arr  = np.empty(num_waveforms)
        scale_arr  = np.empty(num_waveforms)
        t0_arr     = np.empty(num_waveforms)
        smooth_arr = np.empty(num_waveforms)
        # m_arr      = np.empty(num_waveforms)
        # b_arr      = np.empty(num_waveforms)

        #draw 8 waveform params for each waveform
        for (wf_idx, wf) in enumerate(wfs):
            (r,z, scale, t0) = draw_position(wf_idx)
            smooth_guess = 10
            rad = np.sqrt(r**2+z**2)
            theta = np.arctan(z/r)

            r_arr[wf_idx] = r
            z_arr[wf_idx] = z
            rad_arr[wf_idx] = rad
            phi_arr[wf_idx] = rng.rand() * np.pi/4
            theta_arr[wf_idx] = theta
            scale_arr[wf_idx] = 5*rng.randn() + scale - .005*scale
            t0_arr[wf_idx] = 3*rng.randn() + maxt_guess
            smooth_arr[wf_idx] = np.clip(rng.randn() + smooth_guess, 0, 20)
            # m_arr[wf_idx] =  0.0001*rng.randn() + 0.
            # b_arr[wf_idx] =  0.001*rng.randn() + 0.

        phi = (tf_phi_max + np.pi/2) * rng.rand() - np.pi/2
        omega = np.pi * rng.rand()
        d = rng.rand()

        #limit from 60 to 90
        rc1 = dnest4.wrap(prior_vars[rc1_idx]*rng.randn() + priors[rc1_idx], 65, 80)
        rc2 = dnest4.wrap(prior_vars[rc2_idx]*rng.randn() + priors[rc2_idx], 0.1, 10)
        rcfrac = dnest4.wrap(prior_vars[rcfrac_idx]*rng.randn() + priors[rcfrac_idx], 0.9, 1)

        aliasrc = dnest4.wrap(prior_vars[aliasrc_idx]*rng.randn() + priors[aliasrc_idx], 0.01, 10)

        # grad = rng.randint(len(detector.gradList))
        # avgImp = rng.randint(len(detector.impAvgList))
        charge_trapping = rng.rand()*(1000 - traprc_min) +  traprc_min

        grad = prior_vars[grad_idx]*rng.randn() + priors[grad_idx]
        avgImp = grad = prior_vars[grad_idx+1]*rng.randn() + priors[grad_idx+1]
        grad = dnest4.wrap(grad, detector.gradList[0], detector.gradList[-1])
        avgImp = dnest4.wrap(avgImp, detector.impAvgList[0], detector.impAvgList[-1])


        h_100_beta = (beta_lims[1] - beta_lims[0]) * rng.rand() + beta_lims[0]
        h_111_beta = (beta_lims[1] - beta_lims[0]) * rng.rand() + beta_lims[0]

        h_100_va = dnest4.wrap(prior_vars[velo_first_idx]*rng.randn() + priors[velo_first_idx], 1, 10*priors[velo_first_idx])
        h_111_va = dnest4.wrap(prior_vars[velo_first_idx+1]*rng.randn() + priors[velo_first_idx+1], 1, 10*priors[velo_first_idx])
        h_100_vmax = dnest4.wrap(prior_vars[velo_first_idx+2]*rng.randn() + priors[velo_first_idx+2], 1, 10*priors[velo_first_idx])
        h_111_vmax = dnest4.wrap(prior_vars[velo_first_idx+3]*rng.randn() + priors[velo_first_idx+3], 1, 10*priors[velo_first_idx])

        return np.hstack([
              phi, omega, d,
              #b, c, dc,
              rc1, rc2, rcfrac, aliasrc,
              h_100_va, h_111_va, h_100_vmax, h_111_vmax, h_100_beta, h_111_beta,
            #   k0_0, k0_1, k0_2, k0_3,
              grad, avgImp, charge_trapping,
              rad_arr[:], phi_arr[:], theta_arr[:], scale_arr[:], t0_arr[:],smooth_arr[:],
            ])
Example #23
0
    def perturb_wf(
        self,
        params,
        wf_idx,
    ):
        #do both wf and detector params in case theres strong correlation
        logH = 0.0
        num_waveforms = self.num_waveforms
        detector = self.detector
        priors = self.priors

        wf_params = 6

        reps = 1
        if rng.rand() < 0.5:
            reps += np.int(np.power(100.0, rng.rand()))

        for i in range(reps):
            wf_which = rng.randint(wf_params)

            # my_which = rng.randint(len(priors) + 8)

            # if my_which < len(priors):
            #     #detector variable
            #     logH += self.perturb_detector(params, my_which)
            #
            # else:
            if wf_which < wf_params:
                #this is a waveform variable!
                # wf_which =  np.int(my_which - len(priors))

                #which idx of the global params array
                which = len(priors) + wf_which * num_waveforms + wf_idx

                rad_idx = len(priors) + wf_idx
                theta_idx = len(priors) + 2 * num_waveforms + wf_idx
                self.changed_wfs[wf_idx] = 1

                if wf_which == 0:
                    theta = params[theta_idx]

                    IsInDetector = 0
                    while not (IsInDetector):
                        new_rad = self.get_new_rad(params[which], theta)
                        r = new_rad * np.cos(theta)
                        z = new_rad * np.sin(theta)
                        IsInDetector = detector.IsInDetector(r, 0, z)

                    params[which] = new_rad

                elif wf_which == 2:  #theta
                    rad = params[rad_idx]

                    IsInDetector = 0
                    while not (IsInDetector):
                        new_theta = self.get_new_theta(rad, params[which])
                        r = rad * np.cos(new_theta)
                        z = rad * np.sin(new_theta)
                        IsInDetector = detector.IsInDetector(r, 0, z)
                    params[which] = new_theta

                # if wf_which == 0:
                #     params[which] += (detector.detector_radius)*dnest4.randh()
                #     params[which] = dnest4.wrap(params[which] , 0, detector.detector_radius)
                elif wf_which == 1:
                    max_val = np.pi / 4
                    params[which] += np.pi / 4 * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], 0, max_val)

                # elif wf_which == 3: #scale
                #     wf_guess = self.conf.energy_guess
                #     sig = 10
                #
                #     logH -= -0.5*((params[which] - wf_guess  )/sig)**2
                #     params[which] += sig*dnest4.randh()
                #     params[which] = dnest4.wrap(params[which], wf_guess - 30, wf_guess + 30)
                #     logH += -0.5*((params[which] - wf_guess)/sig)**2

                elif wf_which == 3:  #t0
                    #gaussian around 0, sigma... 5?
                    t0_sig = self.maxt_sigma
                    logH -= -0.5 * (
                        (params[which] - self.alignidx_guess) / t0_sig)**2
                    params[which] += t0_sig * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], self.min_maxt,
                                                self.max_maxt)
                    logH += -0.5 * (
                        (params[which] - self.alignidx_guess) / t0_sig)**2

                elif wf_which == 4:  #smooth
                    #gaussian around 10
                    smooth_guess = 20
                    sig = 20 * timeStepMult
                    logH -= -0.5 * (
                        (params[which] - smooth_guess * timeStepMult) / sig)**2
                    params[which] += sig * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], 1,
                                                40 * timeStepMult)
                    logH += -0.5 * (
                        (params[which] - smooth_guess * timeStepMult) / sig)**2

                elif wf_which == 5:  #p
                    #gaussian around 10

                    if self.conf.smooth_type == "gen_gaus":
                        p_guess = 2
                        sig = 10
                        logH -= -0.5 * ((params[which] - p_guess) / sig)**2
                        params[which] += sig * dnest4.randh()
                        params[which] = dnest4.wrap(params[which], 1, 20)
                        logH += -0.5 * ((params[which] - p_guess) / sig)**2
                    elif self.conf.smooth_type == "skew":
                        params[which] += 20 * dnest4.randh()
                        params[which] = dnest4.wrap(params[which], -20, 0)

            else:
                print("wf which value %d (which value %d) not supported" %
                      (wf_which, which))
                exit(0)

        return logH
    def perturb(self, params):
        """
        Unlike in C++, this takes a numpy array of parameters as input,
        and modifies it in-place. The return value is still logH.
        """
        logH = 0.0
        which = rng.randint(len(params))

        if which >= len(priors):
            #this is a waveform variable!
            wf_which = np.floor((which - len(priors)) / num_waveforms)
            # print "which idx is %d, value is %f" % (which, params[which])
            # print "  wf which is %d" % wf_which

            if wf_which == 0 or wf_which == 4:  #radius and t0
                wf_idx = (which - len(priors)) % num_waveforms
                rad_idx = len(priors) + wf_idx
                theta_idx = len(priors) + 2 * num_waveforms + wf_idx
                t0_idx = len(priors) + 4 * num_waveforms + wf_idx

                theta = params[theta_idx]

                #FIND THE MAXIMUM RADIUS STILL INSIDE THE DETECTOR
                theta_eq = np.arctan(detector.detector_length /
                                     detector.detector_radius)
                theta_taper = np.arctan(detector.taper_length /
                                        detector.detector_radius)
                #   print "theta: %f pi" % (theta / np.pi)
                if theta <= theta_taper:
                    z = np.tan(theta) * (detector.detector_radius -
                                         detector.taper_length) / (
                                             1 - np.tan(theta))
                    max_rad = z / np.sin(theta)
                elif theta <= theta_eq:
                    max_rad = detector.detector_radius / np.cos(theta)
                #   print "max rad radius: %f" %  max_rad
                else:
                    theta_comp = np.pi / 2 - theta
                    max_rad = detector.detector_length / np.cos(theta_comp)
                #   print "max rad length: %f" %  max_rad

                #AND THE MINIMUM (from PC dimple)
                #min_rad  = 1./ ( np.cos(theta)**2/detector.pcRad**2  +  np.sin(theta)**2/detector.pcLen**2 )
                min_rad = np.amax([detector.pcRad, detector.pcLen])

                mean = [0, 0]
                cov = [[1, -0.8], [-0.8, 1]]
                jumps = np.array((0.1 * dnest4.randh(), 0.1 * dnest4.randh()))
                (r_jump, t0_jump) = np.dot(cov, jumps)

                params[rad_idx] = dnest4.wrap(params[rad_idx] + r_jump,
                                              min_rad, max_rad)
                params[t0_idx] = dnest4.wrap(params[t0_idx] + t0_jump, min_t0,
                                             max_t0)

            elif wf_which == 1:
                max_val = np.pi / 4
                params[which] += np.pi / 4 * dnest4.randh()
                params[which] = dnest4.wrap(params[which], 0, max_val)
                if params[which] < 0 or params[which] > np.pi / 4:
                    print "wtf phi"
                #params[which] = np.clip(params[which], 0, max_val)

            elif wf_which == 2:  #theta
                wf_idx = (which - len(priors)) % num_waveforms
                rad_idx = len(priors) + wf_idx
                rad = params[rad_idx]
                #   print "rad: %f" % rad
                if rad < np.amin([
                        detector.detector_radius - detector.taper_length,
                        detector.detector_length
                ]):
                    max_val = np.pi / 2
                    min_val = 0
                #   print "theta: min %f pi, max %f pi" % (min_val, max_val)
                else:
                    if rad < detector.detector_radius - detector.taper_length:
                        #can't possibly hit the taper
                        #   print "less than taper adjustment"
                        min_val = 0
                    elif rad < np.sqrt(detector.detector_radius**2 +
                                       detector.taper_length**2):
                        #low enough that it could hit the taper region
                        #   print "taper adjustment"
                        a = detector.detector_radius - detector.taper_length
                        z = 0.5 * (np.sqrt(2 * rad**2 - a**2) - a)
                        min_val = np.arcsin(z / rad)
                    else:
                        #longer than could hit the taper
                        #   print  " longer thantaper adjustment"
                        min_val = np.arccos(detector.detector_radius / rad)

                    if rad < detector.detector_length:
                        max_val = np.pi / 2
                    else:
                        max_val = np.pi / 2 - np.arccos(
                            detector.detector_length / rad)
                #   print "theta: min %f pi, max %f pi" % (min_val, max_val)

                params[which] += (max_val - min_val) * dnest4.randh()
                params[which] = dnest4.wrap(params[which], min_val, max_val)
                #   params[which] = np.clip(params[which], min_val, max_val)
                if params[which] < min_val or params[which] > max_val:
                    print "wtf theta"

            elif wf_which == 3:  #scale
                wf_idx = (which - len(priors)) % num_waveforms
                wf = wfs[wf_idx]
                params[which] += dnest4.randh()
                params[which] = dnest4.wrap(params[which],
                                            wf.wfMax - 10 * wf.baselineRMS,
                                            wf.wfMax + 10 * wf.baselineRMS)
                params[which] = np.clip(params[which],
                                        wf.wfMax - 50 * wf.baselineRMS,
                                        wf.wfMax + 50 * wf.baselineRMS)
            #   print "  adjusted scale to %f" %  ( params[which])
            elif wf_which == 5:  #smooth
                params[which] += 0.1 * dnest4.randh()
                params[which] = dnest4.wrap(params[which], 0, 25)
            #   print "  adjusted smooth to %f" %  ( params[which])

            elif wf_which == 6:  #wf baseline slope (m)
                params[which] += 0.0001 * dnest4.randh()
                params[which] = dnest4.wrap(params[which], -0.001, 0.001)
            #   print "  adjusted m to %f" %  ( params[which])
            elif wf_which == 7:  #wf baseline incercept (b)
                params[which] += 0.01 * dnest4.randh()
                params[which] = dnest4.wrap(params[which], -1, 1)
            #   print "  adjusted b to %f" %  ( params[which])

        elif which == ba_idx:  #b over a
            params[which] += 0.1 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], -0.9, 15)
        elif which == c_idx:  #b over a
            params[which] += 0.01 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], -0.9, -0.7)
        elif which == dc_idx:  #b over a
            params[which] += 0.01 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], -1.05, -0.975)

        elif which == rc1_idx:
            space = np.exp(-1. / 90) - np.exp(-1. / 60)
            params[which] += space * dnest4.randh()
            params[which] = dnest4.wrap(params[which], np.exp(-1. / 60),
                                        np.exp(-1. / 90))
        elif which == rc2_idx:
            space = np.exp(-1. / 10) - np.exp(-1. / .01)
            params[which] += space * dnest4.randh()
            params[which] = dnest4.wrap(params[which], np.exp(-1. / 0.01),
                                        np.exp(-1. / 10))
        elif which == rcfrac_idx:
            params[which] += 0.1 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0.9, 1)
        elif which == grad_idx:
            params[which] += (len(detector.gradList) - 1) * dnest4.randh()
            params[which] = np.int(
                dnest4.wrap(params[which], 0,
                            len(detector.gradList) - 1))
        elif which >= velo_first_idx and which < velo_first_idx + 6:
            velo_which = (which - velo_first_idx) % 3
            #TODO: consider long transforming priors on two of these
            if velo_which == 0:  #mu0 parameter
                params[which] += (100E3 - 10E3) * dnest4.randh()
                params[which] = dnest4.wrap(params[which], 10E3, 100E3)
            elif velo_which == 1:  #ln(1/beta)
                space = np.log(1 / .1)
                params[which] += space * dnest4.randh()
                params[which] = dnest4.wrap(params[which], 0, np.log(1 / .1))
            elif velo_which == 2:
                space = 10E3 * 100 - 100E3 * 300
                params[which] += space * dnest4.randh()
                params[which] = dnest4.wrap(params[which], 10E3 * 100,
                                            100E3 * 300)

        elif which == trap_idx:
            space = np.exp(-1. / 5000) - np.exp(-1. / 50)
            params[which] += space * dnest4.randh()
            params[which] = dnest4.wrap(params[which], np.exp(-1. / 50),
                                        np.exp(-1. / 5000))

        else:  #velocity or rc params: cant be below 0, can be arb. large
            print "which value %d not supported" % which
            exit(0)

        return logH
Example #25
0
    def perturb_detector(self, params, which):
        logH = 0.0
        detector = self.detector
        num_waveforms =  self.num_waveforms
        priors = self.priors
        prior_vars = self.prior_vars

        if which == phi_idx: #lets call this phi
            logH -= -0.5*((params[which] - priors[which])/prior_vars[which])**2
            params[which] += prior_vars[which]*dnest4.randh()
            params[which] = dnest4.wrap(params[which], -np.pi, 0)
            logH += -0.5*((params[which] - priors[which])/prior_vars[which])**2
        elif which == omega_idx: #call it omega
            logH -= -0.5*((params[which] - priors[which])/prior_vars[which])**2
            params[which] += prior_vars[which]*dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0, np.pi)
            logH += -0.5*((params[which] - priors[which])/prior_vars[which])**2
        elif which == d_idx: #d
            logH -= -0.5*((params[which] - priors[which])/prior_vars[which])**2
            params[which] += prior_vars[which]*dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0.5, 0.999)
            # params[which] = dnest4.wrap(params[which], 0, 5000)
            logH += -0.5*((params[which] - priors[which])/prior_vars[which])**2

        elif which == rc1_idx or which == rc2_idx or which == rcfrac_idx:
            #all normally distributed priors
            logH -= -0.5*((params[which] - priors[which])/prior_vars[which])**2
            params[which] += prior_vars[which]*dnest4.randh()
            if which ==  rcfrac_idx:  params[which] = dnest4.wrap(params[which], 0, 1)
            logH += -0.5*((params[which] - priors[which])/prior_vars[which])**2
        #
        # elif which == num0_idx:
        #   sig = 3*timeStepMult
        #   logH -= -0.5*((params[which] - 0.)/sig)**2
        #   params[which] += sig*dnest4.randh()
        #   logH += -0.5*((params[which] - 0.)/sig)**2

        # elif which == aliasrc_idx:
        #     params[which] += 19.9*dnest4.randh()
        #     params[which] = dnest4.wrap(params[which], 0.1, 20)

        elif which == grad_idx:
            # logH -= -0.5*((params[which] - priors[which])/prior_vars[which])**2
            # params[which] += prior_vars[which] *dnest4.randh()
            # params[which] = dnest4.wrap(params[which], detector.gradList[0], detector.gradList[-1])
            # logH += -0.5*((params[which] - priors[which])/prior_vars[which])**2

            paramlist = detector.gradList
            params[which] += (paramlist[-1] - paramlist[0])*dnest4.randh()
            params[which] = dnest4.wrap(params[which], paramlist[0], paramlist[-1])

        elif which == imp_avg_idx:
            # logH -= -0.5*((params[which] - priors[which])/prior_vars[which])**2
            # params[which] += prior_vars[which] *dnest4.randh()
            # params[which] = dnest4.wrap(params[which], detector.impAvgList[0], detector.impAvgList[-1])
            # logH += -0.5*((params[which] - priors[which])/prior_vars[which])**2
            paramlist = detector.impAvgList
            params[which] += (paramlist[-1] - paramlist[0])*dnest4.randh()
            params[which] = dnest4.wrap(params[which], paramlist[0], paramlist[-1])

        elif which == pcrad_idx:
            paramlist = detector.pcRadList
            params[which] += (paramlist[-1] - paramlist[0])*dnest4.randh()
            params[which] = dnest4.wrap(params[which], paramlist[0], paramlist[-1])

        elif which == pclen_idx:
            paramlist = detector.pcLenList
            params[which] += (paramlist[-1] - paramlist[0])*dnest4.randh()
            params[which] = dnest4.wrap(params[which], paramlist[0], paramlist[-1])

        elif which == trap_idx:
            # let's go with log10-uniform on this
            # log_trap = np.log10(params[which])
            # log_trap += 3*dnest4.randh()
            # log_trap = dnest4.wrap(log_trap, 1, 4)
            # params[which] = 10**(log_trap)

            # p_guess = 350
            # sig = 50
            # logH -= -0.5*((params[which] - p_guess )/sig)**2
            # params[which] += sig*dnest4.randh()
            # params[which] = dnest4.wrap(params[which], 100, 600)
            # logH += -0.5*((params[which] - p_guess)/sig)**2

            traprc_min = 10
            params[which] += (1000 - traprc_min)*dnest4.randh()
            params[which] = dnest4.wrap(params[which], traprc_min, 1000)

        elif which == untrap_idx:
            traprc_min = 1000
            params[which] += (1000000 - traprc_min)*dnest4.randh()
            params[which] = dnest4.wrap(params[which], traprc_min, 1000000)
        elif which == energy_idx:
            wf_guess = self.conf.energy_guess
            sig = 10

            logH -= -0.5*((params[which] - wf_guess  )/sig)**2
            params[which] += sig*dnest4.randh()
            params[which] = dnest4.wrap(params[which], wf_guess - 30, wf_guess + 30)
            logH += -0.5*((params[which] - wf_guess)/sig)**2


        elif which >= velo_first_idx and which < velo_first_idx+4:
            logH -= -0.5*((params[which] - priors[which])/prior_vars[which])**2
            params[which] += prior_vars[which]*dnest4.randh()
            params[which] = dnest4.wrap(params[which], 1, priors[which]*10)
            logH += -0.5*((params[which] - priors[which])/prior_vars[which])**2

        elif which == velo_first_idx+4 or which == velo_first_idx+5:
            params[which] += (self.conf.beta_lims[1] - self.conf.beta_lims[0])  *dnest4.randh()
            params[which] = dnest4.wrap(params[which], self.conf.beta_lims[0], self.conf.beta_lims[1])

        else: #velocity or rc params: cant be below 0, can be arb. large
            print ("which value %d not supported" % which)
            exit(0)

        return logH
    def perturb(self, params):
        """
        Unlike in C++, this takes a numpy array of parameters as input,
        and modifies it in-place. The return value is still logH.
        """
        logH = 0.0

        #this adjusts one wf at a time
        #choose which set of params to adjust (detector, wf 1, wf2...)
        #
        # num_sets = num_waveforms+1
        # set_idx = rng.randint(num_sets)
        #
        # #choose how many t
        # reps = 1;
        # if(rng.rand() < 0.5):
        #     reps += np.int(np.power(100.0, rng.rand()));
        #
        # for i in range(reps):
        #     if set_idx == 0:
        #         #detector param
        #         which = rng.randint(len(priors))
        #     else:
        #         #waveform param (from the one waveform we're adjusting this go-around
        #         which = rng.randint(8) + len(priors) + (set_idx-1)*num_waveforms

        #this adjusts wfs simultaneouslt
        reps = 1
        if (rng.rand() < 0.5):
            reps += np.int(np.power(100.0, rng.rand()))

        for i in range(reps):
            if (rng.rand() < 0.5):
                #detector param
                which = rng.randint(len(priors))
            else:
                #waveform param (from any of the waveforms)
                which = rng.randint(8 * num_waveforms) + len(priors)

            if which >= len(priors):
                #this is a waveform variable!
                wf_which = np.int(
                    np.floor((which - len(priors)) / num_waveforms))
                wf_idx = (which - len(priors)) % num_waveforms

                wf_idx = (which - len(priors)) % num_waveforms
                rad_idx = len(priors) + wf_idx
                theta_idx = len(priors) + 2 * num_waveforms + wf_idx
                self.changed_wfs[wf_idx] = 1

                if wf_which == 0:
                    theta = params[theta_idx]

                    #FIND THE MAXIMUM RADIUS STILL INSIDE THE DETECTOR
                    theta_eq = np.arctan(detector.detector_length /
                                         detector.detector_radius)
                    theta_taper = np.arctan(detector.taper_length /
                                            detector.detector_radius)
                    #   print "theta: %f pi" % (theta / np.pi)
                    if theta <= theta_taper:
                        z = np.tan(theta) * (detector.detector_radius -
                                             detector.taper_length) / (
                                                 1 - np.tan(theta))
                        max_rad = z / np.sin(theta)
                    elif theta <= theta_eq:
                        max_rad = detector.detector_radius / np.cos(theta)
                    #   print "max rad radius: %f" %  max_rad
                    else:
                        theta_comp = np.pi / 2 - theta
                        max_rad = detector.detector_length / np.cos(theta_comp)
                    #   print "max rad length: %f" %  max_rad

                    #AND THE MINIMUM (from PC dimple)
                    #min_rad  = 1./ ( np.cos(theta)**2/detector.pcRad**2  +  np.sin(theta)**2/detector.pcLen**2 )

                    min_rad = np.amax([detector.pcRad, detector.pcLen])

                    total_max_rad = np.sqrt(detector.detector_length**2 +
                                            detector.detector_radius**2)

                    params[which] += total_max_rad * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], min_rad,
                                                max_rad)

                elif wf_which == 2:  #theta
                    rad = params[rad_idx]

                    #   print "rad: %f" % rad
                    if rad < np.amin([
                            detector.detector_radius - detector.taper_length,
                            detector.detector_length
                    ]):
                        max_val = np.pi / 2
                        min_val = 0
                    #   print "theta: min %f pi, max %f pi" % (min_val, max_val)
                    else:
                        if rad < detector.detector_radius - detector.taper_length:
                            #can't possibly hit the taper
                            #   print "less than taper adjustment"
                            min_val = 0
                        elif rad < np.sqrt(detector.detector_radius**2 +
                                           detector.taper_length**2):
                            #low enough that it could hit the taper region
                            #   print "taper adjustment"
                            a = detector.detector_radius - detector.taper_length
                            z = 0.5 * (np.sqrt(2 * rad**2 - a**2) - a)
                            min_val = np.arcsin(z / rad)
                        else:
                            #longer than could hit the taper
                            #   print  " longer thantaper adjustment"
                            min_val = np.arccos(detector.detector_radius / rad)

                        if rad < detector.detector_length:
                            max_val = np.pi / 2
                        else:
                            max_val = np.pi / 2 - np.arccos(
                                detector.detector_length / rad)
                    #   print "theta: min %f pi, max %f pi" % (min_val, max_val)

                    params[which] += np.pi / 2 * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], min_val,
                                                max_val)

                # if wf_which == 0:
                #     params[which] += (detector.detector_radius)*dnest4.randh()
                #     params[which] = dnest4.wrap(params[which] , 0, detector.detector_radius)
                elif wf_which == 1:
                    max_val = np.pi / 4
                    params[which] += np.pi / 4 * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], 0, max_val)
                    if params[which] < 0 or params[which] > np.pi / 4:
                        print "wtf phi"

                # elif wf_which == 2:
                #     params[which] += (detector.detector_length)*dnest4.randh()
                #     params[which] = dnest4.wrap(params[which] , 0, detector.detector_length)

                elif wf_which == 3:  #scale
                    wf = wfs[wf_idx]
                    min_scale = wf.wfMax - 0.01 * wf.wfMax
                    max_scale = wf.wfMax + 0.005 * wf.wfMax
                    params[which] += (max_scale - min_scale) * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], min_scale,
                                                max_scale)
                #   print "  adjusted scale to %f" %  ( params[which])

                elif wf_which == 4:  #t0
                    params[which] += 1 * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], min_maxt,
                                                max_maxt)
                elif wf_which == 5:  #smooth
                    params[which] += 0.1 * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], 0, 25)
                #   print "  adjusted smooth to %f" %  ( params[which])

                elif wf_which == 6:  #wf baseline slope (m)
                    logH -= -0.5 * (params[which] / 1E-4)**2
                    params[which] += 1E-4 * dnest4.randh()
                    logH += -0.5 * (params[which] / 1E-4)**2
                elif wf_which == 7:  #wf baseline incercept (b)
                    logH -= -0.5 * (params[which] / 1E-2)**2
                    params[which] += 1E-2 * dnest4.randh()
                    logH += -0.5 * (params[which] / 1E-2)**2
                else:
                    print "wf which value %d (which value %d) not supported" % (
                        wf_which, which)
                    exit(0)

                #   params[which] += 0.01*dnest4.randh()
                #   params[which]=dnest4.wrap(params[which], -1, 1)
                #   print "  adjusted b to %f" %  ( params[which])

            elif which == ba_idx:  #lets call this phi
                params[which] += (tf_phi_max + np.pi / 2) * dnest4.randh()
                params[which] = dnest4.wrap(params[which], -np.pi / 2,
                                            tf_phi_max)
            elif which == c_idx:  #call it omega
                params[which] += 0.1 * dnest4.randh()
                params[which] = dnest4.wrap(params[which], 0.13, 0.14)
            elif which == dc_idx:  #d
                params[which] += 0.01 * dnest4.randh()
                params[which] = dnest4.wrap(params[which], 0.81, 0.82)

            elif which == rc1_idx or which == rc2_idx or which == rcfrac_idx:
                #all normally distributed priors
                logH -= -0.5 * (
                    (params[which] - priors[which]) / prior_vars[which])**2
                params[which] += prior_vars[which] * dnest4.randh()
                logH += -0.5 * (
                    (params[which] - priors[which]) / prior_vars[which])**2

            elif which == grad_idx:
                params[which] += (detector.gradList[-1] -
                                  detector.gradList[0]) * dnest4.randh()
                params[which] = dnest4.wrap(params[which],
                                            detector.gradList[0],
                                            detector.gradList[-1])
            elif which == imp_avg_idx:
                params[which] += (detector.impAvgList[-1] -
                                  detector.impAvgList[0]) * dnest4.randh()
                params[which] = dnest4.wrap(params[which],
                                            detector.impAvgList[0],
                                            detector.impAvgList[-1])

            elif which >= velo_first_idx and which < velo_first_idx + 6:
                mu_max = 100E5
                velo_which = (which - velo_first_idx) % 3
                #TODO: consider long transforming priors on two of these
                if velo_which == 0:  #mu0 parameter
                    params[which] += (mu_max - 10E3) * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], 10E3, mu_max)
                elif velo_which == 1:  #ln(1/beta)
                    space = np.log(1 / .1)
                    params[which] += space * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], 0,
                                                np.log(1 / .1))
                elif velo_which == 2:
                    minval, maxval = 5E6, 1E8
                    params[which] += (maxval - minval) * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], minval, maxval)

            # elif which >= k0_first_idx and which < k0_first_idx+4:
            #     minval, maxval = -50, 50
            #     params[which] += (maxval-minval) *dnest4.randh()
            #     params[which] = dnest4.wrap(params[which], minval, maxval)

            elif which == trap_idx:
                params[which] += (1000 - traprc_min) * dnest4.randh()
                params[which] = dnest4.wrap(params[which], traprc_min, 1000)

            else:  #velocity or rc params: cant be below 0, can be arb. large
                print "which value %d not supported" % which
                exit(0)

        return logH
    def from_prior(self):
        """
        Unlike in C++, this must *return* a numpy array of parameters.
        """
        r_arr = np.empty(num_waveforms)
        z_arr = np.empty(num_waveforms)

        rad_arr = np.empty(num_waveforms)
        phi_arr = np.empty(num_waveforms)
        theta_arr = np.empty(num_waveforms)
        scale_arr = np.empty(num_waveforms)
        t0_arr = np.empty(num_waveforms)
        smooth_arr = np.empty(num_waveforms)
        m_arr = np.empty(num_waveforms)
        b_arr = np.empty(num_waveforms)

        print "\n"
        #draw 8 waveform params for each waveform
        for (wf_idx, wf) in enumerate(wfs):
            (r, z, scale, t0) = draw_position(wf_idx)
            smooth_guess = 10
            rad = np.sqrt(r**2 + z**2)
            theta = np.arctan(z / r)

            r_arr[wf_idx] = r
            z_arr[wf_idx] = z
            rad_arr[wf_idx] = rad
            phi_arr[wf_idx] = rng.rand() * np.pi / 4
            theta_arr[wf_idx] = theta
            scale_arr[wf_idx] = 5 * rng.randn() + scale - .005 * scale
            t0_arr[wf_idx] = 3 * rng.randn() + maxt_guess
            smooth_arr[wf_idx] = np.clip(rng.randn() + smooth_guess, 0, 20)
            m_arr[wf_idx] = 0.0001 * rng.randn() + 0.
            b_arr[wf_idx] = 0.001 * rng.randn() + 0.

            # print "  creating wf %d" % wf_idx
            # print "  >>",
            # print rad_arr[wf_idx], phi_arr[wf_idx]/np.pi, theta_arr[wf_idx]/np.pi, t0_arr[wf_idx]
            # print "  ", rad_arr[wf_idx], theta_arr[wf_idx]/np.pi

        phi = (tf_phi_max + np.pi / 2) * rng.rand() - np.pi / 2
        omega = np.pi * rng.rand()
        d = rng.rand()

        #limit from 60 to 90
        rc1 = dnest4.wrap(prior_vars[rc1_idx] * rng.randn() + priors[rc1_idx],
                          65, 80)
        rc2 = dnest4.wrap(prior_vars[rc2_idx] * rng.randn() + priors[rc2_idx],
                          0.1, 10)
        rcfrac = dnest4.wrap(
            prior_vars[rcfrac_idx] * rng.randn() + priors[rcfrac_idx], 0.9, 1)

        grad = rng.randint(len(detector.gradList))
        avgImp = rng.randint(len(detector.impAvgList))
        charge_trapping = rng.rand() * (1000 - traprc_min) + traprc_min

        # grad = rng.randint(len(detector.gradList))
        # avgImp = rng.randint(len(detector.impAvgList))
        grad = rng.rand() * (detector.gradList[-1] -
                             detector.gradList[0]) + detector.gradList[0]
        avgImp = rng.rand() * (detector.impAvgList[-1] -
                               detector.impAvgList[0]) + detector.impAvgList[0]

        #6 hole drift params
        h_100_mu0 = .01 * h_100_mu0_prior * rng.randn() + h_100_mu0_prior
        lnbeta = np.log(1 / (.1 * h_100_beta_prior)) * rng.randn() + np.log(
            1. / h_100_beta_prior)
        h_100_lnbeta = dnest4.wrap(lnbeta, 0, np.log(1 / .1))
        h_100_emu = .01 * (h_100_e0_prior * h_100_mu0_prior
                           ) * rng.randn() + h_100_e0_prior * h_100_mu0_prior

        h_111_mu0 = .01 * h_111_mu0_prior * rng.randn() + h_111_mu0_prior
        lnbeta = np.log(1 / (.1 * h_111_beta_prior)) * rng.randn() + np.log(
            1. / h_111_e0_prior)
        h_111_lnbeta = dnest4.wrap(lnbeta, 0, np.log(1 / .1))
        h_111_emu = .01 * (h_111_e0_prior * h_111_mu0_prior
                           ) * rng.randn() + h_111_e0_prior * h_111_mu0_prior

        # k0_0 = prior_vars[k0_0_idx]*rng.randn() + k0_0_prior
        # k0_1 = prior_vars[k0_1_idx]*rng.randn() + k0_1_prior
        # k0_2 = prior_vars[k0_2_idx]*rng.randn() + k0_2_prior
        # k0_3 = prior_vars[k0_3_idx]*rng.randn() + k0_3_prior

        return np.hstack([
            phi,
            omega,
            d,
            #b, c, dc,
            rc1,
            rc2,
            rcfrac,
            h_100_mu0,
            h_100_lnbeta,
            h_100_emu,
            h_111_mu0,
            h_111_lnbeta,
            h_111_emu,
            #   k0_0, k0_1, k0_2, k0_3,
            grad,
            avgImp,
            charge_trapping,
            rad_arr[:],
            phi_arr[:],
            theta_arr[:],
            scale_arr[:],
            t0_arr[:],
            smooth_arr[:],
            m_arr[:],
            b_arr[:]
        ])
Example #28
0
    def from_prior(self):
        """
        Unlike in C++, this must *return* a numpy array of parameters.
        """
        num_waveforms = self.mpi_manager.num_waveforms

        r_arr = np.empty(num_waveforms)
        z_arr = np.empty(num_waveforms)

        rad_arr = np.empty(num_waveforms)
        phi_arr = np.empty(num_waveforms)
        theta_arr = np.empty(num_waveforms)
        scale_arr = np.empty(num_waveforms)
        t0_arr = np.empty(num_waveforms)
        smooth_arr = np.empty(num_waveforms)
        m_arr = np.empty(num_waveforms)
        b_arr = np.empty(num_waveforms)

        #draw 8 waveform params for each waveform
        for (wf_idx) in range(num_waveforms):
            (r, z, scale, t0) = self.draw_position(wf_idx)
            rad = np.sqrt(r**2 + z**2)
            theta = np.arctan(z / r)

            smooth_guess = 10

            r_arr[wf_idx] = r
            z_arr[wf_idx] = z
            rad_arr[wf_idx] = rad
            phi_arr[wf_idx] = rng.rand() * np.pi / 4
            theta_arr[wf_idx] = theta
            scale_arr[wf_idx] = 5 * rng.randn() + scale - .005 * scale
            t0_arr[wf_idx] = 3 * rng.randn() + self.maxt_guess
            smooth_arr[wf_idx] = np.clip(rng.randn() + smooth_guess, 0, 20)
            # m_arr[wf_idx] =  0.0001*rng.randn() + 0.
            # b_arr[wf_idx] =  0.001*rng.randn() + 0.

        phi = (tf_phi_max + np.pi / 2) * rng.rand() - np.pi / 2
        omega = np.pi * rng.rand()
        d = rng.rand()

        #limit from 60 to 90
        rc1 = dnest4.wrap(prior_vars[rc1_idx] * rng.randn() + priors[rc1_idx],
                          65, 80)
        rc2 = dnest4.wrap(prior_vars[rc2_idx] * rng.randn() + priors[rc2_idx],
                          0.1, 10)
        rcfrac = dnest4.wrap(
            prior_vars[rcfrac_idx] * rng.randn() + priors[rcfrac_idx], 0.9, 1)

        grad = rng.rand() * (self.mpi_manager.detector.gradList[-1] -
                             self.mpi_manager.detector.gradList[0]
                             ) + self.mpi_manager.detector.gradList[0]
        avgImp = rng.rand() * (self.mpi_manager.detector.impAvgList[-1] -
                               self.mpi_manager.detector.impAvgList[0]
                               ) + self.mpi_manager.detector.impAvgList[0]
        charge_trapping = rng.rand() * (1000 - traprc_min) + traprc_min

        aliasrc = dnest4.wrap(
            prior_vars[aliasrc_idx] * rng.randn() + priors[aliasrc_idx], 0.01,
            10)

        #6 hole drift params
        h_100_va = (va_lims[1] - va_lims[0]) * rng.rand() + va_lims[0]
        h_100_vmax = (vmax_lims[1] - vmax_lims[0]) * rng.rand() + vmax_lims[0]
        h_100_beta = (beta_lims[1] - beta_lims[0]) * rng.rand() + beta_lims[0]

        h_111_va = (va_lims[1] - va_lims[0]) * rng.rand() + va_lims[0]
        h_111_vmax = (vmax_lims[1] - vmax_lims[0]) * rng.rand() + vmax_lims[0]
        h_111_beta = (beta_lims[1] - beta_lims[0]) * rng.rand() + beta_lims[0]

        return np.hstack([
            phi,
            omega,
            d,
            #b, c, dc,
            rc1,
            rc2,
            rcfrac,
            aliasrc,
            h_100_va,
            h_111_va,
            h_100_vmax,
            h_111_vmax,
            h_100_beta,
            h_111_beta,
            grad,
            avgImp,
            charge_trapping,
            rad_arr[:],
            phi_arr[:],
            theta_arr[:],
            scale_arr[:],
            t0_arr[:],
            smooth_arr[:],
        ])
Example #29
0
    def perturb(self, params):
        """
        Unlike in C++, this takes a numpy array of parameters as input,
        and modifies it in-place. The return value is still logH.
        """
        logH = 0.0
        which = rng.randint(len(params))

        if which >= len(priors):
            #this is a waveform variable!
            wf_which = np.floor((which - len(priors)) / num_waveforms)

            if wf_which == 0:
                params[which] += (detector.detector_radius)*dnest4.randh()
                params[which] = dnest4.wrap(params[which] , 0, detector.detector_radius)
            elif wf_which == 1:
                max_val = np.pi/4
                params[which] += np.pi/4*dnest4.randh()
                params[which] = dnest4.wrap(params[which], 0, max_val)
                if params[which] < 0 or params[which] > np.pi/4:
                    print "wtf phi"
                #params[which] = np.clip(params[which], 0, max_val)
            elif wf_which == 2:
                params[which] += (detector.detector_length)*dnest4.randh()
                params[which] = dnest4.wrap(params[which] , 0, detector.detector_length)

            elif wf_which == 3: #scale
                wf_idx = (which - len(priors)) % num_waveforms
                wf = wfs[wf_idx]
                min_scale = wf.wfMax - 0.01*wf.wfMax
                max_scale = wf.wfMax + 0.005*wf.wfMax
                params[which] += (max_scale-min_scale)*dnest4.randh()
                params[which] = dnest4.wrap(params[which], min_scale, max_scale)
            #   print "  adjusted scale to %f" %  ( params[which])

            elif wf_which == 4: #t0
              params[which] += 1*dnest4.randh()
              params[which] = dnest4.wrap(params[which], min_maxt, max_maxt)
            elif wf_which == 5: #smooth
              params[which] += 0.1*dnest4.randh()
              params[which] = dnest4.wrap(params[which], 0, 25)
            #   print "  adjusted smooth to %f" %  ( params[which])

            elif wf_which == 6: #wf baseline slope (m)
                logH -= -0.5*(params[which]/1E-4)**2
                params[which] += 1E-4*dnest4.randh()
                logH += -0.5*(params[which]/1E-4)**2
            elif wf_which == 7: #wf baseline incercept (b)
                logH -= -0.5*(params[which]/1E-2)**2
                params[which] += 1E-2*dnest4.randh()
                logH += -0.5*(params[which]/1E-2)**2

            #   params[which] += 0.01*dnest4.randh()
            #   params[which]=dnest4.wrap(params[which], -1, 1)
            #   print "  adjusted b to %f" %  ( params[which])

        elif which == ba_idx: #b over a
            params[which] += 600*dnest4.randh()
            params[which] = dnest4.wrap(params[which], -200, 400)
        elif which == c_idx: #b over a
            params[which] += 0.01*dnest4.randh()
            params[which] = dnest4.wrap(params[which], -0.82, -0.8)
        elif which == dc_idx: #b over a
            params[which] += 0.01*dnest4.randh()
            params[which] = dnest4.wrap(params[which], -1.03, -0.98)

        elif which == rc1_idx or which == rc2_idx or which == rcfrac_idx:
            #all normally distributed priors
            logH -= -0.5*((params[which] - priors[which])/prior_vars[which])**2
            params[which] += prior_vars[which]*dnest4.randh()
            logH += -0.5*((params[which] - priors[which])/prior_vars[which])**2

        elif which == grad_idx:
          params[which] += (len(detector.gradList)-1)*dnest4.randh()
          params[which] = np.int(dnest4.wrap(params[which], 0, len(detector.gradList)-1))
        elif which >= velo_first_idx and which < velo_first_idx+6:
            velo_which =  (which - velo_first_idx)%3
            #TODO: consider long transforming priors on two of these
            if velo_which ==0: #mu0 parameter
                params[which] += (500E3 - 10E3)  *dnest4.randh()
                params[which] = dnest4.wrap(params[which], 10E3, 500E3)
            elif velo_which ==1: #ln(1/beta)
                space = np.log(1/.1)
                params[which] += space *dnest4.randh()
                params[which] = dnest4.wrap(params[which], 0, np.log(1/.1))
            elif velo_which == 2:
                space = 10E3 * 100 - 500E3 * 300
                params[which] += space *dnest4.randh()
                params[which] = dnest4.wrap(params[which], 10E3 * 100, 500E3 * 300)

        elif which == trap_idx:
          params[which] += prior_vars[trap_idx]*dnest4.randh()
          params[which] = dnest4.wrap(params[which], 50, 1000)

        else: #velocity or rc params: cant be below 0, can be arb. large
            print "which value %d not supported" % which
            exit(0)


        return logH
Example #30
0
    def from_prior(self):
        """
        Unlike in C++, this must *return* a numpy array of parameters.
        """
        r_arr      = np.empty(num_waveforms)
        z_arr      = np.empty(num_waveforms)

        rad_arr      = np.empty(num_waveforms)
        phi_arr    = np.empty(num_waveforms)
        theta_arr  = np.empty(num_waveforms)
        scale_arr  = np.empty(num_waveforms)
        t0_arr     = np.empty(num_waveforms)
        smooth_arr = np.empty(num_waveforms)
        m_arr      = np.empty(num_waveforms)
        b_arr      = np.empty(num_waveforms)

        print "\n"
        #draw 8 waveform params for each waveform
        for (wf_idx, wf) in enumerate(wfs):
            (r,z, scale, t0) = draw_position(wf_idx)
            smooth_guess = 10

            r_arr[wf_idx] = r
            z_arr[wf_idx] = z
            # rad_arr[wf_idx] = rad
            phi_arr[wf_idx] = rng.rand() * np.pi/4
            # theta_arr[wf_idx] = theta
            scale_arr[wf_idx] = 5*rng.randn() + scale - .005*scale
            t0_arr[wf_idx] = 3*rng.randn() + maxt_guess
            smooth_arr[wf_idx] = np.clip(rng.randn() + smooth_guess, 0, 20)
            m_arr[wf_idx] =  0.0001*rng.randn() + 0.
            b_arr[wf_idx] =  0.001*rng.randn() + 0.

            # print "  creating wf %d" % wf_idx
            # print "  >>",
            # print rad_arr[wf_idx], phi_arr[wf_idx]/np.pi, theta_arr[wf_idx]/np.pi, t0_arr[wf_idx]
            # print "  ", rad_arr[wf_idx], theta_arr[wf_idx]/np.pi

        b_over_a = rng.rand() * 400 - 100
        c = 0.05 *rng.randn() + c_prior
        dc =  0.01 *rng.randn() + dc_prior

        #limit from 60 to 90
        rc1 = dnest4.wrap(prior_vars[rc1_idx]*rng.randn() + priors[rc1_idx], 65, 80)
        rc2 = dnest4.wrap(prior_vars[rc2_idx]*rng.randn() + priors[rc2_idx], 0.1, 10)
        rcfrac = dnest4.wrap(prior_vars[rcfrac_idx]*rng.randn() + priors[rcfrac_idx], 0.9, 1)
        charge_trapping = dnest4.wrap(prior_vars[trap_idx]*rng.randn() + priors[trap_idx], 50, 1000)

        grad = np.int(np.clip(prior_vars[grad_idx]*np.int(rng.randn()) + priors[grad_idx], 0, len(detector.gradList)-1))

        #6 hole drift params
        h_100_mu0 = .01 * h_100_mu0_prior*rng.randn() + h_100_mu0_prior
        lnbeta = np.log(1/(.1*h_100_beta_prior))*rng.randn() + np.log(1./h_100_beta_prior)
        h_100_lnbeta = dnest4.wrap(lnbeta, 0, np.log(1/.1))
        h_100_emu = .01 * (h_100_e0_prior*h_100_mu0_prior)*rng.randn() + h_100_e0_prior*h_100_mu0_prior

        h_111_mu0 = .01 * h_111_mu0_prior*rng.randn() + h_111_mu0_prior
        lnbeta = np.log(1/(.1*h_111_beta_prior))*rng.randn() + np.log(1./h_111_e0_prior)
        h_111_lnbeta = dnest4.wrap(lnbeta, 0, np.log(1/.1))
        h_111_emu = .01 * (h_111_e0_prior*h_111_mu0_prior)*rng.randn() + h_111_e0_prior*h_111_mu0_prior

        return np.hstack([
              b_over_a, c, dc,
              rc1, rc2, rcfrac,
              h_100_mu0, h_100_lnbeta, h_100_emu, h_111_mu0, h_111_lnbeta, h_111_emu,
              grad, charge_trapping,
              r_arr[:], phi_arr[:], z_arr[:], scale_arr[:], t0_arr[:],smooth_arr[:], m_arr[:], b_arr[:]
            ])
Example #31
0
    def perturb_wf(self, params, wf_idx, ):
    #do both wf and detector params in case theres strong correlation
        logH = 0.0
        num_waveforms = self.num_waveforms
        detector = self.detector
        priors = self.priors

        wf_params = 6

        reps = 1
        if rng.rand() < 0.5:
            reps += np.int(np.power(100.0, rng.rand()));

        for i in range(reps):
            wf_which = rng.randint(wf_params)

            # my_which = rng.randint(len(priors) + 8)

            # if my_which < len(priors):
            #     #detector variable
            #     logH += self.perturb_detector(params, my_which)
            #
            # else:
            if wf_which < wf_params:
                #this is a waveform variable!
                # wf_which =  np.int(my_which - len(priors))

                #which idx of the global params array
                which = len(priors) + wf_which*num_waveforms + wf_idx

                rad_idx = len(priors) + wf_idx
                theta_idx =  len(priors) + 2*num_waveforms+ wf_idx
                self.changed_wfs[wf_idx] = 1

                if wf_which == 0:
                  theta = params[theta_idx]

                  IsInDetector = 0
                  while not(IsInDetector):
                    new_rad = self.get_new_rad(params[which], theta)
                    r = new_rad * np.cos(theta)
                    z = new_rad * np.sin(theta)
                    IsInDetector = detector.IsInDetector(r, 0,z)

                  params[which] = new_rad

                elif wf_which ==2: #theta
                    rad = params[rad_idx]

                    IsInDetector = 0
                    while not(IsInDetector):
                      new_theta = self.get_new_theta(rad, params[which])
                      r = rad * np.cos(new_theta)
                      z = rad * np.sin(new_theta)
                      IsInDetector = detector.IsInDetector(r, 0,z)
                    params[which] = new_theta

                # if wf_which == 0:
                #     params[which] += (detector.detector_radius)*dnest4.randh()
                #     params[which] = dnest4.wrap(params[which] , 0, detector.detector_radius)
                elif wf_which == 1:
                    max_val = np.pi/4
                    params[which] += np.pi/4*dnest4.randh()
                    params[which] = dnest4.wrap(params[which], 0, max_val)

                # elif wf_which == 3: #scale
                #     wf_guess = self.conf.energy_guess
                #     sig = 10
                #
                #     logH -= -0.5*((params[which] - wf_guess  )/sig)**2
                #     params[which] += sig*dnest4.randh()
                #     params[which] = dnest4.wrap(params[which], wf_guess - 30, wf_guess + 30)
                #     logH += -0.5*((params[which] - wf_guess)/sig)**2

                elif wf_which == 3: #t0
                  #gaussian around 0, sigma... 5?
                  t0_sig = self.maxt_sigma
                  logH -= -0.5*((params[which] - self.alignidx_guess )/t0_sig)**2
                  params[which] += t0_sig*dnest4.randh()
                  params[which] = dnest4.wrap(params[which], self.min_maxt, self.max_maxt)
                  logH += -0.5*((params[which] - self.alignidx_guess)/t0_sig)**2

                elif wf_which == 4: #smooth
                  #gaussian around 10
                  smooth_guess = 20
                  sig = 20*timeStepMult
                  logH -= -0.5*((params[which] - smooth_guess*timeStepMult )/sig)**2
                  params[which] += sig*dnest4.randh()
                  params[which] = dnest4.wrap(params[which], 1, 40*timeStepMult)
                  logH += -0.5*((params[which] - smooth_guess*timeStepMult)/sig)**2

                elif wf_which == 5: #p
                    #gaussian around 10

                    if self.conf.smooth_type == "gen_gaus":
                        p_guess = 2
                        sig = 10
                        logH -= -0.5*((params[which] - p_guess )/sig)**2
                        params[which] += sig*dnest4.randh()
                        params[which] = dnest4.wrap(params[which], 1, 20)
                        logH += -0.5*((params[which] - p_guess)/sig)**2
                    elif self.conf.smooth_type == "skew":
                        params[which] += 20*dnest4.randh()
                        params[which] = dnest4.wrap(params[which], -20, 0)

            else:
                print( "wf which value %d (which value %d) not supported" % (wf_which, which) )
                exit(0)

        return logH
Example #32
0
def perturb(coords, ndim, width):
    i = np.random.randint(ndim)
    coords[i] += (width[i]) * dnest4.randh()
    # Note: use the return value of wrap, unlike in C++
    coords[i] = dnest4.wrap(coords[i], -0.5 * width[i], 0.5 * width[i])
    return 0.0
    def from_prior(self):
        """
        Unlike in C++, this must *return* a numpy array of parameters.
        """
        r_arr = np.empty(num_waveforms)
        z_arr = np.empty(num_waveforms)

        rad_arr = np.empty(num_waveforms)
        phi_arr = np.empty(num_waveforms)
        theta_arr = np.empty(num_waveforms)
        scale_arr = np.empty(num_waveforms)
        t0_arr = np.empty(num_waveforms)
        smooth_arr = np.empty(num_waveforms)
        m_arr = np.empty(num_waveforms)
        b_arr = np.empty(num_waveforms)

        print "\n"
        #draw 8 waveform params for each waveform
        for (wf_idx, wf) in enumerate(wfs):
            (rad, theta, scale, t0) = draw_position(wf_idx)
            smooth_guess = 10
            t0 -= 20  #hack to go from 20 to 100 as t0guess
            t0 += t0_guess
            # r_arr[wf_idx] = r
            # z_arr[wf_idx] = z
            rad_arr[wf_idx] = rad
            phi_arr[wf_idx] = rng.rand() * np.pi / 4
            theta_arr[wf_idx] = theta
            scale_arr[wf_idx] = 5 * rng.randn() + scale
            t0_arr[wf_idx] = 3 * rng.randn() + t0
            smooth_arr[wf_idx] = np.clip(rng.randn() + smooth_guess, 0, 20)
            m_arr[wf_idx] = 0.0001 * rng.randn() + 0.
            b_arr[wf_idx] = 0.001 * rng.randn() + 0.

            # print "  creating wf %d" % wf_idx
            # print "  >>",
            # print r, phi_arr[wf_idx]/np.pi, z, t0_arr[wf_idx]
            # print "  ", rad_arr[wf_idx], theta_arr[wf_idx]/np.pi

        b_over_a = 0.1 * rng.randn() + ba_prior
        c = 0.05 * rng.randn() + c_prior
        dc = 0.01 * rng.randn() + dc_prior

        #limit from 60 to 90
        rc1 = np.exp(-1. / prior_vars[rc1_idx]) + np.exp(-1. / 5) * rng.randn()
        rc1 = dnest4.wrap(rc1, np.exp(-1. / 60), np.exp(-1. / 90))
        #limit from 0.01 to 10
        rc2 = np.exp(
            -1. / prior_vars[rc2_idx]) + np.exp(-1. / 0.5) * rng.randn()
        rc2 = dnest4.wrap(rc1, np.exp(-1. / 0.01), np.exp(-1. / 10))

        rcfrac = dnest4.wrap(
            prior_vars[rcfrac_idx] * rng.randn() + priors[rcfrac_idx], 0.9, 1)

        grad = np.int(
            np.clip(
                prior_vars[grad_idx] * np.int(rng.randn()) + priors[grad_idx],
                0,
                len(detector.gradList) - 1))

        charge_trapping = np.exp(-1. / 200) + np.exp(-1. / 50) * rng.randn()
        charge_trapping = dnest4.wrap(rc1, np.exp(-1. / 50),
                                      np.exp(-1. / 5000))

        #6 hole drift params
        h_100_mu0 = .01 * h_100_mu0_prior * rng.randn() + h_100_mu0_prior
        lnbeta = np.log(1 / (.1 * h_100_beta_prior)) * rng.randn() + np.log(
            1. / h_100_beta_prior)
        h_100_lnbeta = dnest4.wrap(lnbeta, 0, np.log(1 / .1))
        h_100_emu = .01 * (h_100_e0_prior * h_100_mu0_prior
                           ) * rng.randn() + h_100_e0_prior * h_100_mu0_prior

        h_111_mu0 = .01 * h_111_mu0_prior * rng.randn() + h_111_mu0_prior
        lnbeta = np.log(1 / (.1 * h_111_beta_prior)) * rng.randn() + np.log(
            1. / h_111_e0_prior)
        h_111_lnbeta = dnest4.wrap(lnbeta, 0, np.log(1 / .1))
        h_111_emu = .01 * (h_111_e0_prior * h_111_mu0_prior
                           ) * rng.randn() + h_111_e0_prior * h_111_mu0_prior

        # import matplotlib.pyplot as plt
        # plt.figure(0)
        #
        # d = dc*c
        # detector.SetTransferFunction(b_over_a, c, d, rc1, rc2, rcfrac)
        # detector.siggenInst.set_hole_params(h_100_mu0, h_100_beta, h_100_e0, h_111_mu0, h_111_beta, h_111_e0)
        # detector.trapping_rc = charge_trapping
        # detector.SetFieldsGradIdx(grad)
        # for (wf_idx,wf) in enumerate(wfs):
        #     rad, phi, theta = rad_arr[wf_idx], phi_arr[wf_idx], theta_arr[wf_idx]
        #     scale, t0, smooth =  scale_arr[wf_idx], t0_arr[wf_idx], smooth_arr[wf_idx]
        #     m, b = m_arr[wf_idx], b_arr[wf_idx]
        #
        #     r = rad * np.cos(theta)
        #     z = rad * np.sin(theta)
        #
        #     dataLen = wfs[wf_idx].wfLength
        #     ml_wf = detector.MakeSimWaveform(r, phi, z, scale, t0,  dataLen, h_smoothing = smooth)
        #
        #     start_idx = -baseline_origin_idx
        #     end_idx = dataLen - baseline_origin_idx - 1
        #     baseline_trend = np.linspace(m*start_idx+b, m*end_idx+b, dataLen)
        #     ml_wf += baseline_trend
        #
        #     t_data = np.arange(dataLen) * 10
        #     plt.plot(t_data, ml_wf[:dataLen], color="b", alpha=0.1)
        #     plt.plot(t_data, wf.windowedWf, color="r", alpha=0.1)
        # plt.xlim(9000,11000)
        # plt.show()

        return np.hstack([
            b_over_a,
            c,
            dc,
            rc1,
            rc2,
            rcfrac,
            h_100_mu0,
            h_100_lnbeta,
            h_100_emu,
            h_111_mu0,
            h_111_lnbeta,
            h_111_emu,
            grad,
            charge_trapping,
            #   r_arr[:], phi_arr[:], z_arr[:], scale_arr[:], t0_arr[:],smooth_arr[:], m_arr[:], b_arr[:]
            rad_arr[:],
            phi_arr[:],
            theta_arr[:],
            scale_arr[:],
            t0_arr[:],
            smooth_arr[:],
            m_arr[:],
            b_arr[:]
        ])
    def perturb(self, params):
        """
        Unlike in C++, this takes a numpy array of parameters as input,
        and modifies it in-place. The return value is still logH.
        """
        logH = 0.0

        #this adjusts one wf at a time
        #choose which set of params to adjust (detector, wf 1, wf2...)
        #
        # num_sets = num_waveforms+1
        # set_idx = rng.randint(num_sets)
        #
        # #choose how many t
        # reps = 1;
        # if(rng.rand() < 0.5):
        #     reps += np.int(np.power(100.0, rng.rand()));
        #
        # for i in range(reps):
        #     if set_idx == 0:
        #         #detector param
        #         which = rng.randint(len(priors))
        #     else:
        #         #waveform param (from the one waveform we're adjusting this go-around
        #         which = rng.randint(8) + len(priors) + (set_idx-1)*num_waveforms

        #this adjusts wfs simultaneouslt
        reps = 1;
        if(rng.rand() < 0.5):
            reps += np.int(np.power(100.0, rng.rand()));

        for i in range(reps):
            if(rng.rand() < 0.5):
                #detector param
                which = rng.randint(len(priors))
            else:
                #waveform param (from any of the waveforms)
                which = rng.randint(8*num_waveforms) + len(priors)

            if which >= len(priors):
                #this is a waveform variable!
                wf_which =  np.int(np.floor((which - len(priors)) / num_waveforms))
                wf_idx = (which - len(priors)) % num_waveforms

                wf_idx = (which - len(priors)) % num_waveforms
                rad_idx = len(priors) + wf_idx
                theta_idx =  len(priors) + 2*num_waveforms+ wf_idx
                self.changed_wfs[wf_idx] = 1

                if wf_which == 0:
                  theta = params[theta_idx]

                  #FIND THE MAXIMUM RADIUS STILL INSIDE THE DETECTOR
                  theta_eq = np.arctan(detector.detector_length/detector.detector_radius)
                  theta_taper = np.arctan(detector.taper_length/detector.detector_radius)
                #   print "theta: %f pi" % (theta / np.pi)
                  if theta <= theta_taper:
                     z = np.tan(theta)*(detector.detector_radius - detector.taper_length) / (1-np.tan(theta))
                     max_rad = z / np.sin(theta)
                  elif theta <= theta_eq:
                      max_rad = detector.detector_radius / np.cos(theta)
                    #   print "max rad radius: %f" %  max_rad
                  else:
                      theta_comp = np.pi/2 - theta
                      max_rad = detector.detector_length / np.cos(theta_comp)
                    #   print "max rad length: %f" %  max_rad

                  #AND THE MINIMUM (from PC dimple)
                  #min_rad  = 1./ ( np.cos(theta)**2/detector.pcRad**2  +  np.sin(theta)**2/detector.pcLen**2 )

                  min_rad = np.amax([detector.pcRad, detector.pcLen])

                  total_max_rad = np.sqrt(detector.detector_length**2 + detector.detector_radius**2 )

                  params[which] += total_max_rad*dnest4.randh()
                  params[which] = dnest4.wrap(params[which] , min_rad, max_rad)

                elif wf_which ==2: #theta
                  rad = params[rad_idx]

                #   print "rad: %f" % rad
                  if rad < np.amin([detector.detector_radius - detector.taper_length, detector.detector_length]):
                      max_val = np.pi/2
                      min_val = 0
                    #   print "theta: min %f pi, max %f pi" % (min_val, max_val)
                  else:
                      if rad < detector.detector_radius - detector.taper_length:
                          #can't possibly hit the taper
                        #   print "less than taper adjustment"
                          min_val = 0
                      elif rad < np.sqrt(detector.detector_radius**2 + detector.taper_length**2):
                          #low enough that it could hit the taper region
                        #   print "taper adjustment"
                          a = detector.detector_radius - detector.taper_length
                          z = 0.5 * (np.sqrt(2*rad**2-a**2) - a)
                          min_val = np.arcsin(z/rad)
                      else:
                          #longer than could hit the taper
                        #   print  " longer thantaper adjustment"
                          min_val = np.arccos(detector.detector_radius/rad)

                      if rad < detector.detector_length:
                          max_val = np.pi/2
                      else:
                          max_val = np.pi/2 - np.arccos(detector.detector_length/rad)
                    #   print "theta: min %f pi, max %f pi" % (min_val, max_val)

                  params[which] += np.pi/2*dnest4.randh()
                  params[which] = dnest4.wrap(params[which], min_val, max_val)

                # if wf_which == 0:
                #     params[which] += (detector.detector_radius)*dnest4.randh()
                #     params[which] = dnest4.wrap(params[which] , 0, detector.detector_radius)
                elif wf_which == 1:
                    max_val = np.pi/4
                    params[which] += np.pi/4*dnest4.randh()
                    params[which] = dnest4.wrap(params[which], 0, max_val)
                    if params[which] < 0 or params[which] > np.pi/4:
                        print "wtf phi"

                # elif wf_which == 2:
                #     params[which] += (detector.detector_length)*dnest4.randh()
                #     params[which] = dnest4.wrap(params[which] , 0, detector.detector_length)

                elif wf_which == 3: #scale
                    wf = wfs[wf_idx]
                    min_scale = wf.wfMax - 0.01*wf.wfMax
                    max_scale = wf.wfMax + 0.005*wf.wfMax
                    params[which] += (max_scale-min_scale)*dnest4.randh()
                    params[which] = dnest4.wrap(params[which], min_scale, max_scale)
                #   print "  adjusted scale to %f" %  ( params[which])

                elif wf_which == 4: #t0
                  params[which] += 1*dnest4.randh()
                  params[which] = dnest4.wrap(params[which], min_maxt, max_maxt)
                elif wf_which == 5: #smooth
                  params[which] += 0.1*dnest4.randh()
                  params[which] = dnest4.wrap(params[which], 0, 25)
                #   print "  adjusted smooth to %f" %  ( params[which])

                elif wf_which == 6: #wf baseline slope (m)
                    logH -= -0.5*(params[which]/1E-4)**2
                    params[which] += 1E-4*dnest4.randh()
                    logH += -0.5*(params[which]/1E-4)**2
                elif wf_which == 7: #wf baseline incercept (b)
                    logH -= -0.5*(params[which]/1E-2)**2
                    params[which] += 1E-2*dnest4.randh()
                    logH += -0.5*(params[which]/1E-2)**2
                else:
                    print "wf which value %d (which value %d) not supported" % (wf_which, which)
                    exit(0)

                #   params[which] += 0.01*dnest4.randh()
                #   params[which]=dnest4.wrap(params[which], -1, 1)
                #   print "  adjusted b to %f" %  ( params[which])

            elif which == ba_idx: #lets call this phi
               params[which] += (tf_phi_max + np.pi/2)*dnest4.randh()
               params[which] = dnest4.wrap(params[which], -np.pi/2, tf_phi_max)
            elif which == c_idx: #call it omega
                params[which] += 0.1*dnest4.randh()
                params[which] = dnest4.wrap(params[which], 0.13, 0.14)
            elif which == dc_idx: #d
                params[which] += 0.01*dnest4.randh()
                params[which] = dnest4.wrap(params[which], 0.81, 0.82)

            elif which == rc1_idx or which == rc2_idx or which == rcfrac_idx:
                #all normally distributed priors
                logH -= -0.5*((params[which] - priors[which])/prior_vars[which])**2
                params[which] += prior_vars[which]*dnest4.randh()
                logH += -0.5*((params[which] - priors[which])/prior_vars[which])**2

            elif which == grad_idx:
              params[which] += (detector.gradList[-1] - detector.gradList[0])*dnest4.randh()
              params[which] = dnest4.wrap(params[which], detector.gradList[0], detector.gradList[-1])
            elif which == imp_avg_idx:
              params[which] += (detector.impAvgList[-1] - detector.impAvgList[0])*dnest4.randh()
              params[which] = dnest4.wrap(params[which], detector.impAvgList[0], detector.impAvgList[-1])

            elif which >= velo_first_idx and which < velo_first_idx+6:
                mu_max = 100E5
                velo_which =  (which - velo_first_idx)%3
                #TODO: consider long transforming priors on two of these
                if velo_which ==0: #mu0 parameter
                    params[which] += (mu_max - 10E3)  *dnest4.randh()
                    params[which] = dnest4.wrap(params[which], 10E3, mu_max)
                elif velo_which ==1: #ln(1/beta)
                    space = np.log(1/.1)
                    params[which] += space *dnest4.randh()
                    params[which] = dnest4.wrap(params[which], 0, np.log(1/.1))
                elif velo_which == 2:
                    minval, maxval = 5E6, 1E8
                    params[which] += (maxval-minval) *dnest4.randh()
                    params[which] = dnest4.wrap(params[which], minval, maxval)

            # elif which >= k0_first_idx and which < k0_first_idx+4:
            #     minval, maxval = -50, 50
            #     params[which] += (maxval-minval) *dnest4.randh()
            #     params[which] = dnest4.wrap(params[which], minval, maxval)

            elif which == trap_idx:
              params[which] += (1000 - traprc_min)*dnest4.randh()
              params[which] = dnest4.wrap(params[which], traprc_min, 1000)

            else: #velocity or rc params: cant be below 0, can be arb. large
                print "which value %d not supported" % which
                exit(0)


        return logH
Example #35
0
    def from_prior(self):
        priors = self.priors
        prior_vars = self.prior_vars
        detector=self.detector

        num_waveforms = self.num_waveforms
        rad_arr    = np.empty(num_waveforms)
        phi_arr    = np.empty(num_waveforms)
        theta_arr  = np.empty(num_waveforms)
        scale_arr  = np.empty(num_waveforms)
        t0_arr     = np.empty(num_waveforms)
        smooth_arr = np.empty(num_waveforms)
        p_arr = np.empty(num_waveforms)

        #draw waveform params for each waveform
        for (wf_idx) in range(num_waveforms):
            (r,z, scale, t0) = self.draw_position(wf_idx)
            rad = np.sqrt(r**2+z**2)
            theta = np.arctan(z/r)
            smooth_guess = 20*timeStepMult

            rad_arr[wf_idx] = rad
            phi_arr[wf_idx] = rng.rand() * np.pi/4
            theta_arr[wf_idx] = theta
            scale_arr[wf_idx] = 20*rng.randn() + scale
            t0_arr[wf_idx] = dnest4.wrap(self.maxt_sigma*rng.randn() + self.alignidx_guess, self.min_maxt, self.max_maxt)
            smooth_arr[wf_idx] = dnest4.wrap(rng.randn() + smooth_guess, 1, 40)

            if self.conf.smooth_type == "gen_gaus":
                p_arr[wf_idx] = dnest4.wrap(10*rng.randn() + 2, 1, 20)
            elif self.conf.smooth_type == "skew":
                p_arr[wf_idx] = rng.rand()*20 - 20

        #TF params
        phi = dnest4.wrap(prior_vars[phi_idx]*rng.randn() + priors[phi_idx], -np.pi, 0)
        omega = dnest4.wrap(prior_vars[omega_idx]*rng.randn() + priors[omega_idx], 0, np.pi)
        d = dnest4.wrap(prior_vars[d_idx]*rng.randn() + priors[d_idx], 0.5, 0.999)
        # gain = dnest4.wrap(prior_vars[d_idx]*rng.randn() + priors[d_idx], 0, 5000)

        #RC params are normal & clipped
        rc1 = prior_vars[rc1_idx]*rng.randn() + priors[rc1_idx]
        rc2 = prior_vars[rc2_idx]*rng.randn() + priors[rc2_idx]
        rcfrac = dnest4.wrap(prior_vars[rcfrac_idx]*rng.randn() + priors[rcfrac_idx], 0, 1)

        # aliasrc = dnest4.wrap(prior_vars[aliasrc_idx]*rng.randn() + priors[aliasrc_idx], 0.01, 10)
        # aliasrc = rng.rand()*(10 - 0.01) +  0.01

        h_100_va = dnest4.wrap(prior_vars[velo_first_idx]*rng.randn() + priors[velo_first_idx], 1, 10*priors[velo_first_idx])
        h_111_va = dnest4.wrap(prior_vars[velo_first_idx+1]*rng.randn() + priors[velo_first_idx+1], 1, 10*priors[velo_first_idx]+1)
        h_100_vmax = dnest4.wrap(prior_vars[velo_first_idx+2]*rng.randn() + priors[velo_first_idx+2], 1, 10*priors[velo_first_idx+2])
        h_111_vmax = dnest4.wrap(prior_vars[velo_first_idx+3]*rng.randn() + priors[velo_first_idx+3], 1, 10*priors[velo_first_idx+3])
        h_100_beta = (self.conf.beta_lims[1] - self.conf.beta_lims[0]) * rng.rand() + self.conf.beta_lims[0]
        h_111_beta = (self.conf.beta_lims[1] - self.conf.beta_lims[0]) * rng.rand() + self.conf.beta_lims[0]

        # grad = dnest4.wrap(prior_vars[grad_idx]*rng.randn() + priors[grad_idx], detector.gradList[0], detector.gradList[-1])
        # avgImp = dnest4.wrap(prior_vars[imp_avg_idx]*rng.randn() + priors[imp_avg_idx], detector.impAvgList[0], detector.impAvgList[-1])

        grad = rng.rand()*(detector.gradList[-1] - detector.gradList[0]) +  detector.gradList[0]
        avgImp = rng.rand()*(detector.impAvgList[-1] - detector.impAvgList[0]) +  detector.impAvgList[0]
        pcRad = rng.rand()*(detector.pcRadList[-1] - detector.pcRadList[0]) +  detector.pcRadList[0]
        pcLen = rng.rand()*(detector.pcLenList[-1] - detector.pcLenList[0]) +  detector.pcLenList[0]

        #uniform random for charge trapping
        charge_trapping = rng.rand()*(1000 - 10) + 10
        charge_releasing = rng.rand()*(1000000 - 1000) +  1000

        #
        # charge_trapping = rng.rand()*(100 - 0.1) +  0.1
        # charge_releasing = rng.rand()*(100 - 0.1) +  0.1
        energy = dnest4.wrap(rng.rand()*10 + self.conf.energy_guess, self.conf.energy_guess-30, self.conf.energy_guess+30)

        if doPC:
            return np.hstack([
                  phi, omega, d,#gain,
                  rc1, rc2, rcfrac,#num0,#aliasrc,
                  h_100_va, h_111_va, h_100_vmax, h_111_vmax, h_100_beta, h_111_beta,
                  grad, avgImp,pcRad, pcLen, charge_trapping,charge_releasing, energy,
                  rad_arr[:], phi_arr[:], theta_arr[:], t0_arr[:],smooth_arr[:], p_arr[:]
                #   rad_arr[:], phi_arr[:], theta_arr[:], scale_arr[:], t0_arr[:],smooth_arr[:], p_arr[:]
                ])
        else:
            return np.hstack([
                  phi, omega, d,#gain,
                  rc1, rc2, rcfrac,#num0,#aliasrc,
                  h_100_va, h_111_va, h_100_vmax, h_111_vmax, h_100_beta, h_111_beta,
                  grad, avgImp, charge_trapping,charge_releasing, energy,
                  rad_arr[:], phi_arr[:], theta_arr[:], t0_arr[:],smooth_arr[:], p_arr[:]
                #   rad_arr[:], phi_arr[:], theta_arr[:], scale_arr[:], t0_arr[:],smooth_arr[:], p_arr[:]
                ])
Example #36
0
    def perturb_wf(self, params, wf_idx):

        wf_which = rng.randint(8) #8 wf params
        which =  len(priors) + wf_which*num_waveforms + wf_idx

        if wf_which == 0 or wf_which == 4: #radius and t0
          wf_idx = (which - len(priors)) % num_waveforms
          rad_idx = len(priors) + wf_idx
          theta_idx =  len(priors) + 2*num_waveforms+ wf_idx
          t0_idx =  len(priors) + 4*num_waveforms+ wf_idx

          theta = params[theta_idx]

          #FIND THE MAXIMUM RADIUS STILL INSIDE THE DETECTOR
          theta_eq = np.arctan(detector.detector_length/detector.detector_radius)
          theta_taper = np.arctan(detector.taper_length/detector.detector_radius)
        #   print "theta: %f pi" % (theta / np.pi)
          if theta <= theta_taper:
             z = np.tan(theta)*(detector.detector_radius - detector.taper_length) / (1-np.tan(theta))
             max_rad = z / np.sin(theta)
          elif theta <= theta_eq:
              max_rad = detector.detector_radius / np.cos(theta)
            #   print "max rad radius: %f" %  max_rad
          else:
              theta_comp = np.pi/2 - theta
              max_rad = detector.detector_length / np.cos(theta_comp)
            #   print "max rad length: %f" %  max_rad

          #AND THE MINIMUM (from PC dimple)
          #min_rad  = 1./ ( np.cos(theta)**2/detector.pcRad**2  +  np.sin(theta)**2/detector.pcLen**2 )
          min_rad = np.amax([detector.pcRad, detector.pcLen])

          mean = [0, 0]
          cov = [[1, -0.8], [-0.8, 1]]
          jumps = np.array((0.1*dnest4.randh(), 0.1*dnest4.randh()))
          (r_jump, t0_jump) = np.dot(cov, jumps)

          params[rad_idx] = dnest4.wrap(params[rad_idx] + r_jump , min_rad, max_rad)
          params[t0_idx] = dnest4.wrap(params[t0_idx] + t0_jump , min_t0, max_t0)

        elif wf_which == 1:
            max_val = np.pi/4
            params[which] += np.pi/4*dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0, max_val)
            if params[which] < 0 or params[which] > np.pi/4:
                print "wtf phi"
            #params[which] = np.clip(params[which], 0, max_val)

        elif wf_which ==2: #theta
          wf_idx = (which - len(priors)) % num_waveforms
          rad_idx = len(priors) + wf_idx
          rad = params[rad_idx]
        #   print "rad: %f" % rad
          if rad < np.amin([detector.detector_radius - detector.taper_length, detector.detector_length]):
              max_val = np.pi/2
              min_val = 0
            #   print "theta: min %f pi, max %f pi" % (min_val, max_val)
          else:
              if rad < detector.detector_radius - detector.taper_length:
                  #can't possibly hit the taper
                #   print "less than taper adjustment"
                  min_val = 0
              elif rad < np.sqrt(detector.detector_radius**2 + detector.taper_length**2):
                  #low enough that it could hit the taper region
                #   print "taper adjustment"
                  a = detector.detector_radius - detector.taper_length
                  z = 0.5 * (np.sqrt(2*rad**2-a**2) - a)
                  min_val = np.arcsin(z/rad)
              else:
                  #longer than could hit the taper
                #   print  " longer thantaper adjustment"
                  min_val = np.arccos(detector.detector_radius/rad)

              if rad < detector.detector_length:
                  max_val = np.pi/2
              else:
                  max_val = np.pi/2 - np.arccos(detector.detector_length/rad)
            #   print "theta: min %f pi, max %f pi" % (min_val, max_val)

          params[which] += (max_val-min_val)*dnest4.randh()
          params[which] = dnest4.wrap(params[which], min_val, max_val)
        #   params[which] = np.clip(params[which], min_val, max_val)
          if params[which] < min_val or params[which] > max_val:
            print "wtf theta"

        elif wf_which == 3: #scale
          wf_idx = (which - len(priors)) % num_waveforms
          wf = wfs[wf_idx]
          params[which] += dnest4.randh()
          params[which] = dnest4.wrap(params[which], wf.wfMax - 10*wf.baselineRMS, wf.wfMax + 10*wf.baselineRMS)
          params[which] = np.clip(params[which], wf.wfMax - 50*wf.baselineRMS, wf.wfMax + 50*wf.baselineRMS)
        #   print "  adjusted scale to %f" %  ( params[which])
        elif wf_which == 5: #smooth
          params[which] += 0.1*dnest4.randh()
          params[which] = dnest4.wrap(params[which], 0, 25)
        #   print "  adjusted smooth to %f" %  ( params[which])

        elif wf_which == 6: #wf baseline slope (m)
          params[which] += 0.001*dnest4.randh()
          params[which]=dnest4.wrap(params[which], -0.01, 0.01)
        #   print "  adjusted m to %f" %  ( params[which])
        elif wf_which == 7: #wf baseline incercept (b)
          params[which] += 0.01*dnest4.randh()
          params[which]=dnest4.wrap(params[which], -1, 1)
        #   print "  adjusted b to %f" %  ( params[which])
        else:
            print "unknown wf param number drawn: %d" % wf_which
            exit(0)
Example #37
0
 def perturb_uniform_parameter(self, parameter):
     logH = 0
     parameter += (self.lim_hi - self.lim_lo)  *dnest4.randh()
     parameter = dnest4.wrap(parameter, self.lim_lo, self.lim_hi)
     return (logH, parameter)
Example #38
0
    def perturb(self, params):
        """
        Unlike in C++, this takes a numpy array of parameters as input,
        and modifies it in-place. The return value is still logH.
        """
        logH = 0.0

        reps = 1;
        if(rng.rand() < 0.5):
            reps += np.int(np.power(100.0, rng.rand()));

        # print "going to perturb %d reps" % reps

        for i in range(reps):
            # print "   rep iteration %d" % i
            which = rng.randint(len(params))

            if which == 0:
              rad_idx = 0
              theta_idx =  2

              theta = params[theta_idx]

              #FIND THE MAXIMUM RADIUS STILL INSIDE THE DETECTOR
              theta_eq = np.arctan(detector.detector_length/detector.detector_radius)
              theta_taper = np.arctan(detector.taper_length/detector.detector_radius)
            #   print "theta: %f pi" % (theta / np.pi)
              if theta <= theta_taper:
                 z = np.tan(theta)*(detector.detector_radius - detector.taper_length) / (1-np.tan(theta))
                 max_rad = z / np.sin(theta)
              elif theta <= theta_eq:
                  max_rad = detector.detector_radius / np.cos(theta)
                #   print "max rad radius: %f" %  max_rad
              else:
                  theta_comp = np.pi/2 - theta
                  max_rad = detector.detector_length / np.cos(theta_comp)
                #   print "max rad length: %f" %  max_rad

              #AND THE MINIMUM (from PC dimple)
              #min_rad  = 1./ ( np.cos(theta)**2/detector.pcRad**2  +  np.sin(theta)**2/detector.pcLen**2 )

              min_rad = np.amax([detector.pcRad, detector.pcLen])

              total_max_rad = np.sqrt(detector.detector_length**2 + detector.detector_radius**2 )

              params[which] += total_max_rad*dnest4.randh()
              params[which] = dnest4.wrap(params[which] , min_rad, max_rad)

            elif which ==2: #theta
              rad_idx = 0
              rad = params[rad_idx]

            #   print "rad: %f" % rad
              if rad < np.amin([detector.detector_radius - detector.taper_length, detector.detector_length]):
                  max_val = np.pi/2
                  min_val = 0
                #   print "theta: min %f pi, max %f pi" % (min_val, max_val)
              else:
                  if rad < detector.detector_radius - detector.taper_length:
                      #can't possibly hit the taper
                    #   print "less than taper adjustment"
                      min_val = 0
                  elif rad < np.sqrt(detector.detector_radius**2 + detector.taper_length**2):
                      #low enough that it could hit the taper region
                    #   print "taper adjustment"
                      a = detector.detector_radius - detector.taper_length
                      z = 0.5 * (np.sqrt(2*rad**2-a**2) - a)
                      min_val = np.arcsin(z/rad)
                  else:
                      #longer than could hit the taper
                    #   print  " longer thantaper adjustment"
                      min_val = np.arccos(detector.detector_radius/rad)

                  if rad < detector.detector_length:
                      max_val = np.pi/2
                  else:
                      max_val = np.pi/2 - np.arccos(detector.detector_length/rad)
                #   print "theta: min %f pi, max %f pi" % (min_val, max_val)

              params[which] += np.pi/2*dnest4.randh()
              params[which] = dnest4.wrap(params[which], min_val, max_val)

            # if which == 0:
            #     params[which] += (detector.detector_radius)*dnest4.randh()
            #     params[which] = dnest4.wrap(params[which] , 0, detector.detector_radius)
            elif which == 1:
                max_val = np.pi/4
                params[which] += np.pi/4*dnest4.randh()
                params[which] = dnest4.wrap(params[which], 0, max_val)
                if params[which] < 0 or params[which] > np.pi/4:
                    print "wtf phi"
                #params[which] = np.clip(params[which], 0, max_val)
            # elif which == 2:
            #     params[which] += (detector.detector_length)*dnest4.randh()
            #     params[which] = dnest4.wrap(params[which] , 0, detector.detector_length)

            elif which == 3: #scale
                min_scale = wf.wfMax - 0.01*wf.wfMax
                max_scale = wf.wfMax + 0.005*wf.wfMax
                params[which] += (max_scale-min_scale)*dnest4.randh()
                params[which] = dnest4.wrap(params[which], min_scale, max_scale)
            #   print "  adjusted scale to %f" %  ( params[which])

            elif which == 4: #t0
              params[which] += 1*dnest4.randh()
              params[which] = dnest4.wrap(params[which], min_maxt, max_maxt)
            elif which == 5: #smooth
              params[which] += 0.1*dnest4.randh()
              params[which] = dnest4.wrap(params[which], 0, 25)
                #   print "  adjusted smooth to %f" %  ( params[which])

                # elif which == 6: #wf baseline slope (m)
                #     logH -= -0.5*(params[which]/1E-4)**2
                #     params[which] += 1E-4*dnest4.randh()
                #     logH += -0.5*(params[which]/1E-4)**2
                # elif which == 7: #wf baseline incercept (b)
                #     logH -= -0.5*(params[which]/1E-2)**2
                #     params[which] += 1E-2*dnest4.randh()
                #     logH += -0.5*(params[which]/1E-2)**2

                #   params[which] += 0.01*dnest4.randh()
                #   params[which]=dnest4.wrap(params[which], -1, 1)
                #   print "  adjusted b to %f" %  ( params[which])

            else: #velocity or rc params: cant be below 0, can be arb. large
                print "which value %d not supported" % which
                exit(0)


        return logH
    def from_prior(self):
        """
        Unlike in C++, this must *return* a numpy array of parameters.
        """
        r_arr      = np.empty(num_waveforms)
        z_arr      = np.empty(num_waveforms)

        rad_arr      = np.empty(num_waveforms)
        phi_arr    = np.empty(num_waveforms)
        theta_arr  = np.empty(num_waveforms)
        scale_arr  = np.empty(num_waveforms)
        t0_arr     = np.empty(num_waveforms)
        smooth_arr = np.empty(num_waveforms)
        m_arr      = np.empty(num_waveforms)
        b_arr      = np.empty(num_waveforms)

        print "\n"
        #draw 8 waveform params for each waveform
        for (wf_idx, wf) in enumerate(wfs):
            (r,z, scale, t0) = draw_position(wf_idx)
            smooth_guess = 10
            rad = np.sqrt(r**2+z**2)
            theta = np.arctan(z/r)

            r_arr[wf_idx] = r
            z_arr[wf_idx] = z
            rad_arr[wf_idx] = rad
            phi_arr[wf_idx] = rng.rand() * np.pi/4
            theta_arr[wf_idx] = theta
            scale_arr[wf_idx] = 5*rng.randn() + scale - .005*scale
            t0_arr[wf_idx] = 3*rng.randn() + maxt_guess
            smooth_arr[wf_idx] = np.clip(rng.randn() + smooth_guess, 0, 20)
            m_arr[wf_idx] =  0.0001*rng.randn() + 0.
            b_arr[wf_idx] =  0.001*rng.randn() + 0.

            # print "  creating wf %d" % wf_idx
            # print "  >>",
            # print rad_arr[wf_idx], phi_arr[wf_idx]/np.pi, theta_arr[wf_idx]/np.pi, t0_arr[wf_idx]
            # print "  ", rad_arr[wf_idx], theta_arr[wf_idx]/np.pi

        phi = (tf_phi_max + np.pi/2) * rng.rand() - np.pi/2
        omega = np.pi * rng.rand()
        d = rng.rand()

        #limit from 60 to 90
        rc1 = dnest4.wrap(prior_vars[rc1_idx]*rng.randn() + priors[rc1_idx], 65, 80)
        rc2 = dnest4.wrap(prior_vars[rc2_idx]*rng.randn() + priors[rc2_idx], 0.1, 10)
        rcfrac = dnest4.wrap(prior_vars[rcfrac_idx]*rng.randn() + priors[rcfrac_idx], 0.9, 1)

        grad = rng.randint(len(detector.gradList))
        avgImp = rng.randint(len(detector.impAvgList))
        charge_trapping = rng.rand()*(1000 - traprc_min) +  traprc_min

        # grad = rng.randint(len(detector.gradList))
        # avgImp = rng.randint(len(detector.impAvgList))
        grad = rng.rand()*( detector.gradList[-1] - detector.gradList[0] ) + detector.gradList[0]
        avgImp = rng.rand()*( detector.impAvgList[-1] - detector.impAvgList[0] ) + detector.impAvgList[0]


        #6 hole drift params
        h_100_mu0 = .01 * h_100_mu0_prior*rng.randn() + h_100_mu0_prior
        lnbeta = np.log(1/(.1*h_100_beta_prior))*rng.randn() + np.log(1./h_100_beta_prior)
        h_100_lnbeta = dnest4.wrap(lnbeta, 0, np.log(1/.1))
        h_100_emu = .01 * (h_100_e0_prior*h_100_mu0_prior)*rng.randn() + h_100_e0_prior*h_100_mu0_prior

        h_111_mu0 = .01 * h_111_mu0_prior*rng.randn() + h_111_mu0_prior
        lnbeta = np.log(1/(.1*h_111_beta_prior))*rng.randn() + np.log(1./h_111_e0_prior)
        h_111_lnbeta = dnest4.wrap(lnbeta, 0, np.log(1/.1))
        h_111_emu = .01 * (h_111_e0_prior*h_111_mu0_prior)*rng.randn() + h_111_e0_prior*h_111_mu0_prior

        # k0_0 = prior_vars[k0_0_idx]*rng.randn() + k0_0_prior
        # k0_1 = prior_vars[k0_1_idx]*rng.randn() + k0_1_prior
        # k0_2 = prior_vars[k0_2_idx]*rng.randn() + k0_2_prior
        # k0_3 = prior_vars[k0_3_idx]*rng.randn() + k0_3_prior

        return np.hstack([
              phi, omega, d,
              #b, c, dc,
              rc1, rc2, rcfrac,
              h_100_mu0, h_100_lnbeta, h_100_emu, h_111_mu0, h_111_lnbeta, h_111_emu,
            #   k0_0, k0_1, k0_2, k0_3,
              grad, avgImp, charge_trapping,
              rad_arr[:], phi_arr[:], theta_arr[:], scale_arr[:], t0_arr[:],smooth_arr[:], m_arr[:], b_arr[:]
            ])
Example #40
0
    def perturb(self, params):
        """
        Unlike in C++, this takes a numpy array of parameters as input,
        and modifies it in-place. The return value is still logH.
        """
        logH = 0.0
        self.changed_wfs[:] = 0

        det_or_wf = rng.randint(2)
        if det_or_wf == 0:
            which = rng.randint(len(priors))
        else:
            which = rng.randint(num_waveforms*8) + len(priors)

        if which < len(priors):
            self.changed_wfs[:] = 1

        if which >= len(priors):
            #this is a waveform variable!
            wf_which =  np.int(np.floor((which - len(priors)) / num_waveforms))
            wf_idx = (which - len(priors)) % num_waveforms
            self.changed_wfs[wf_idx] = 1

            if wf_which == 0:
                params[which] += (detector.detector_radius)*dnest4.randh()
                params[which] = dnest4.wrap(params[which] , 0, detector.detector_radius)
            elif wf_which == 1:
                max_val = np.pi/4
                params[which] += np.pi/4*dnest4.randh()
                params[which] = dnest4.wrap(params[which], 0, max_val)
                if params[which] < 0 or params[which] > np.pi/4:
                    print "wtf phi"
                #params[which] = np.clip(params[which], 0, max_val)
            elif wf_which == 2:
                params[which] += (detector.detector_length)*dnest4.randh()
                params[which] = dnest4.wrap(params[which] , 0, detector.detector_length)

            elif wf_which == 3: #scale
                wf = wfs[wf_idx]
                min_scale = wf.wfMax - 0.01*wf.wfMax
                max_scale = wf.wfMax + 0.005*wf.wfMax
                params[which] += (max_scale-min_scale)*dnest4.randh()
                params[which] = dnest4.wrap(params[which], min_scale, max_scale)
            #   print "  adjusted scale to %f" %  ( params[which])

            elif wf_which == 4: #t0
              params[which] += 1*dnest4.randh()
              params[which] = dnest4.wrap(params[which], min_maxt, max_maxt)
            elif wf_which == 5: #smooth
              params[which] += 0.1*dnest4.randh()
              params[which] = dnest4.wrap(params[which], 0, 25)
            #   print "  adjusted smooth to %f" %  ( params[which])

            elif wf_which == 6: #wf baseline slope (m)
                logH -= -0.5*(params[which]/1E-4)**2
                params[which] += 1E-4*dnest4.randh()
                logH += -0.5*(params[which]/1E-4)**2
            elif wf_which == 7: #wf baseline incercept (b)
                logH -= -0.5*(params[which]/1E-2)**2
                params[which] += 1E-2*dnest4.randh()
                logH += -0.5*(params[which]/1E-2)**2

            #   params[which] += 0.01*dnest4.randh()
            #   params[which]=dnest4.wrap(params[which], -1, 1)
            #   print "  adjusted b to %f" %  ( params[which])

        elif which == ba_idx: #lets call this phi
           params[which] += np.pi*dnest4.randh()
           params[which] = dnest4.wrap(params[which], -np.pi/2, np.pi/2)
        elif which == c_idx: #call it omega
            params[which] += 0.1*dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0.13, 0.14)
        elif which == dc_idx: #d
            params[which] += 0.01*dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0.81, 0.82)

        elif which == rc1_idx or which == rc2_idx or which == rcfrac_idx:
            #all normally distributed priors
            logH -= -0.5*((params[which] - priors[which])/prior_vars[which])**2
            params[which] += prior_vars[which]*dnest4.randh()
            logH += -0.5*((params[which] - priors[which])/prior_vars[which])**2

        elif which == grad_idx:
          params[which] += (len(detector.gradList)-1)*dnest4.randh()
          params[which] = np.int(dnest4.wrap(params[which], 0, len(detector.gradList)-1))
        elif which >= velo_first_idx and which < velo_first_idx+6:
            mu_max = 100E5
            velo_which =  (which - velo_first_idx)%3
            #TODO: consider long transforming priors on two of these
            if velo_which ==0: #mu0 parameter
                params[which] += (mu_max - 10E3)  *dnest4.randh()
                params[which] = dnest4.wrap(params[which], 10E3, mu_max)
            elif velo_which ==1: #ln(1/beta)
                space = np.log(1/.1)
                params[which] += space *dnest4.randh()
                params[which] = dnest4.wrap(params[which], 0, np.log(1/.1))
            elif velo_which == 2:
                minval, maxval = 5E6, 1E8
                params[which] += (maxval-minval) *dnest4.randh()
                params[which] = dnest4.wrap(params[which], minval, maxval)

        elif which == trap_idx:
          params[which] += prior_vars[trap_idx]*dnest4.randh()
          params[which] = dnest4.wrap(params[which], 50, 1000)

        else: #velocity or rc params: cant be below 0, can be arb. large
            print "which value %d not supported" % which
            exit(0)


        return logH
Example #41
0
    def perturb(self, params):
        """
         Unlike in C++, this takes a numpy array of parameters as input,
         and modifies it in-place. The return value is still logH.
         """
        logH = 0.0
        which = rng.randint(len(params))

        if which >= len(priors):
            #this is a waveform variable!
            wf_which = np.floor((which - len(priors)) / num_waveforms)
            if wf_which == 0:
                params[which] += (detector.detector_radius) * dnest4.randh()
                params[which] = dnest4.wrap(params[which], 0,
                                            detector.detector_radius)
            elif wf_which == 1:
                max_val = np.pi / 4
                params[which] += np.pi / 4 * dnest4.randh()
                params[which] = dnest4.wrap(params[which], 0, max_val)
                if params[which] < 0 or params[which] > np.pi / 4:
                    print "wtf phi"
                #params[which] = np.clip(params[which], 0, max_val)
            elif wf_which == 2:
                params[which] += (detector.detector_length) * dnest4.randh()
                params[which] = dnest4.wrap(params[which], 0,
                                            detector.detector_length)

            elif wf_which == 3:  #scale
                wf_idx = (which - len(priors)) % num_waveforms
                wf = wfs[wf_idx]
                params[which] += dnest4.randh()
                params[which] = dnest4.wrap(params[which],
                                            wf.wfMax - 10 * wf.baselineRMS,
                                            wf.wfMax + 10 * wf.baselineRMS)
                params[which] = np.clip(params[which],
                                        wf.wfMax - 50 * wf.baselineRMS,
                                        wf.wfMax + 50 * wf.baselineRMS)
            #   print "  adjusted scale to %f" %  ( params[which])

            elif wf_which == 4:  #t0
                params[which] += 1 * dnest4.randh()
                params[which] = dnest4.wrap(params[which], min_maxt, max_maxt)
            elif wf_which == 5:  #smooth
                params[which] += 0.1 * dnest4.randh()
                params[which] = dnest4.wrap(params[which], 0, 25)
            #   print "  adjusted smooth to %f" %  ( params[which])

            elif wf_which == 6:  #wf baseline slope (m)
                logH -= -0.5 * (params[which] / 1E-4)**2
                params[which] += 1E-4 * dnest4.randh()
                logH += -0.5 * (params[which] / 1E-4)**2
            elif wf_which == 7:  #wf baseline incercept (b)
                logH -= -0.5 * (params[which] / 1E-2)**2
                params[which] += 1E-2 * dnest4.randh()
                logH += -0.5 * (params[which] / 1E-2)**2

            #   params[which] += 0.01*dnest4.randh()
            #   params[which]=dnest4.wrap(params[which], -1, 1)
            #   print "  adjusted b to %f" %  ( params[which])

        elif which == ba_idx:  #b over a
            params[which] += 70 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], -50, 20)
        elif which == c_idx:  #b over a
            #this is now 1/gain (so assume it goes from 1 to e^-...7?)
            log_gain = np.log(params[which])
            log_gain += 10 * dnest4.randh()
            log_gain = dnest4.wrap(log_gain, -10, 0.0)
            params[which] = np.exp(log_gain)
            #  params[which] += 4*dnest4.randh()
            #  params[which] = dnest4.wrap(params[which], -2, 2)
        elif which == dc_idx:  #b over a
            params[which] += 1 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0, 1)

        elif which == rc1_idx or which == rc2_idx or which == rcfrac_idx:
            #all normally distributed priors
            logH -= -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2
            params[which] += prior_vars[which] * dnest4.randh()
            logH += -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2

        else:  #velocity or rc params: cant be below 0, can be arb. large
            print "which value %d not supported" % which
            exit(0)

        return logH
Example #42
0
    def from_prior(self):
        """
        Unlike in C++, this must *return* a numpy array of parameters.
        """
        r_arr      = np.empty(num_waveforms)
        z_arr      = np.empty(num_waveforms)

        rad_arr      = np.empty(num_waveforms)
        phi_arr    = np.empty(num_waveforms)
        theta_arr  = np.empty(num_waveforms)
        scale_arr  = np.empty(num_waveforms)
        t0_arr     = np.empty(num_waveforms)
        smooth_arr = np.empty(num_waveforms)
        m_arr      = np.empty(num_waveforms)
        b_arr      = np.empty(num_waveforms)

        print "\n"
        #draw 8 waveform params for each waveform
        for (wf_idx, wf) in enumerate(wfs):
            (r,z, scale, t0, smooth) = draw_position(wf_idx)
            smooth_guess = 10
            t0 -= 20 #hack to go from 20 to 100 as t0guess
            t0 += t0_guess
            # r_arr[wf_idx] = r
            # z_arr[wf_idx] = z
            rad_arr[wf_idx] = np.sqrt(r**2+z**2)
            phi_arr[wf_idx] = rng.rand() * np.pi/4
            theta_arr[wf_idx] = np.arctan(z/r)
            scale_arr[wf_idx] = 5*rng.randn() + scale
            t0_arr[wf_idx] = 3*rng.randn() + t0
            smooth_arr[wf_idx] = np.clip(rng.randn() + smooth_guess, 0, 20)
            m_arr[wf_idx] =  0.001*rng.randn() + 0.
            b_arr[wf_idx] =  0.01*rng.randn() + 0.

            print "  creating wf %d" % wf_idx
            print "  >>",
            print r, phi_arr[wf_idx]/np.pi, z, t0_arr[wf_idx]
            # print "  ", rad_arr[wf_idx], theta_arr[wf_idx]/np.pi

        b_over_a = 0.1*rng.randn() + ba_prior
        c = 0.05 *rng.randn() + c_prior
        dc =  0.01 *rng.randn() + dc_prior

        rc1 = dnest4.wrap(prior_vars[rc1_idx]*rng.randn() + priors[rc1_idx], 60, 90)
        rc2 = dnest4.wrap(prior_vars[rc2_idx]*rng.randn() + priors[rc2_idx], 0, 5)
        rcfrac = dnest4.wrap(prior_vars[rcfrac_idx]*rng.randn() + priors[rcfrac_idx], 0.9, 1)

        grad = np.int(np.clip(prior_vars[grad_idx]*np.int(rng.randn()) + priors[grad_idx], 0, len(detector.gradList)-1))
        charge_trapping = np.exp(20.0*rng.rand())

        #6 hole drift params
        h_100_mu0 = .01*velo_var * h_100_mu0_prior*rng.randn() + h_100_mu0_prior
        h_100_beta = .01*velo_var * h_100_beta_prior*rng.randn() + h_100_beta_prior
        h_100_e0 = .01*velo_var * h_100_e0_prior*rng.randn() + h_100_e0_prior
        h_111_mu0 = .01*velo_var * h_111_mu0_prior*rng.randn() + h_111_mu0_prior
        h_111_beta = .01*velo_var * h_111_beta_prior*rng.randn() + h_111_beta_prior
        h_111_e0 = .01*velo_var * h_111_e0_prior*rng.randn() + h_111_e0_prior

        self.changed_wfs[:] = True

        return np.hstack([
              b_over_a, c, dc,
              rc1, rc2, rcfrac,
              h_100_mu0, h_100_beta, h_100_e0, h_111_mu0, h_111_beta, h_111_e0,
              grad, charge_trapping,
            #   r_arr[:], phi_arr[:], z_arr[:], scale_arr[:], t0_arr[:],smooth_arr[:], m_arr[:], b_arr[:]
              rad_arr[:], phi_arr[:], theta_arr[:], scale_arr[:], t0_arr[:],smooth_arr[:], m_arr[:], b_arr[:]
            ])
Example #43
0
     def perturb(self, params):
         """
         Unlike in C++, this takes a numpy array of parameters as input,
         and modifies it in-place. The return value is still logH.
         """
         logH = 0.0
         which = rng.randint(len(params))

         if which >= len(priors):
             #this is a waveform variable!
             wf_which = np.floor((which - len(priors)) / num_waveforms)
             if wf_which == 0:
                 params[which] += (detector.detector_radius)*dnest4.randh()
                 params[which] = dnest4.wrap(params[which] , 0, detector.detector_radius)
             elif wf_which == 1:
                 max_val = np.pi/4
                 params[which] += np.pi/4*dnest4.randh()
                 params[which] = dnest4.wrap(params[which], 0, max_val)
                 if params[which] < 0 or params[which] > np.pi/4:
                     print "wtf phi"
                 #params[which] = np.clip(params[which], 0, max_val)
             elif wf_which == 2:
                 params[which] += (detector.detector_length)*dnest4.randh()
                 params[which] = dnest4.wrap(params[which] , 0, detector.detector_length)

             elif wf_which == 3: #scale
               wf_idx = (which - len(priors)) % num_waveforms
               wf = wfs[wf_idx]
               params[which] += dnest4.randh()
               params[which] = dnest4.wrap(params[which], wf.wfMax - 10*wf.baselineRMS, wf.wfMax + 10*wf.baselineRMS)
               params[which] = np.clip(params[which], wf.wfMax - 50*wf.baselineRMS, wf.wfMax + 50*wf.baselineRMS)
             #   print "  adjusted scale to %f" %  ( params[which])

             elif wf_which == 4: #t0
               params[which] += 1*dnest4.randh()
               params[which] = dnest4.wrap(params[which], min_maxt, max_maxt)
             elif wf_which == 5: #smooth
               params[which] += 0.1*dnest4.randh()
               params[which] = dnest4.wrap(params[which], 0, 25)
             #   print "  adjusted smooth to %f" %  ( params[which])

             elif wf_which == 6: #wf baseline slope (m)
                 logH -= -0.5*(params[which]/1E-4)**2
                 params[which] += 1E-4*dnest4.randh()
                 logH += -0.5*(params[which]/1E-4)**2
             elif wf_which == 7: #wf baseline incercept (b)
                 logH -= -0.5*(params[which]/1E-2)**2
                 params[which] += 1E-2*dnest4.randh()
                 logH += -0.5*(params[which]/1E-2)**2

             #   params[which] += 0.01*dnest4.randh()
             #   params[which]=dnest4.wrap(params[which], -1, 1)
             #   print "  adjusted b to %f" %  ( params[which])

         elif which == ba_idx: #b over a
           params[which] += 70*dnest4.randh()
           params[which] = dnest4.wrap(params[which], -50, 20)
         elif which == c_idx: #b over a
            #this is now 1/gain (so assume it goes from 1 to e^-...7?)
            log_gain = np.log(params[which])
            log_gain += 10*dnest4.randh()
            log_gain = dnest4.wrap(log_gain, -10, 0.0)
            params[which] = np.exp(log_gain)
            #  params[which] += 4*dnest4.randh()
            #  params[which] = dnest4.wrap(params[which], -2, 2)
         elif which == dc_idx: #b over a
             params[which] += 1*dnest4.randh()
             params[which] = dnest4.wrap(params[which], 0, 1)

         elif which == rc1_idx or which == rc2_idx or which == rcfrac_idx:
             #all normally distributed priors
             logH -= -0.5*((params[which] - priors[which])/prior_vars[which])**2
             params[which] += prior_vars[which]*dnest4.randh()
             logH += -0.5*((params[which] - priors[which])/prior_vars[which])**2

         else: #velocity or rc params: cant be below 0, can be arb. large
             print "which value %d not supported" % which
             exit(0)


         return logH
Example #44
0
    def from_prior(self):
        """
        Unlike in C++, this must *return* a numpy array of parameters.
        """
        r_arr = np.empty(num_waveforms)
        z_arr = np.empty(num_waveforms)

        rad_arr = np.empty(num_waveforms)
        phi_arr = np.empty(num_waveforms)
        theta_arr = np.empty(num_waveforms)
        scale_arr = np.empty(num_waveforms)
        t0_arr = np.empty(num_waveforms)
        smooth_arr = np.empty(num_waveforms)
        # m_arr      = np.empty(num_waveforms)
        # b_arr      = np.empty(num_waveforms)

        #draw 8 waveform params for each waveform
        for (wf_idx, wf) in enumerate(wfs):
            (r, z, scale, t0) = draw_position(wf_idx)
            smooth_guess = 10
            rad = np.sqrt(r**2 + z**2)
            theta = np.arctan(z / r)

            r_arr[wf_idx] = r
            z_arr[wf_idx] = z
            rad_arr[wf_idx] = rad
            phi_arr[wf_idx] = rng.rand() * np.pi / 4
            theta_arr[wf_idx] = theta
            scale_arr[wf_idx] = 5 * rng.randn() + scale - .005 * scale
            t0_arr[wf_idx] = 3 * rng.randn() + maxt_guess
            smooth_arr[wf_idx] = np.clip(rng.randn() + smooth_guess, 0, 20)
            # m_arr[wf_idx] =  0.0001*rng.randn() + 0.
            # b_arr[wf_idx] =  0.001*rng.randn() + 0.

        phi = (tf_phi_max + np.pi / 2) * rng.rand() - np.pi / 2
        omega = np.pi * rng.rand()
        d = rng.rand()

        #limit from 60 to 90
        rc1 = dnest4.wrap(prior_vars[rc1_idx] * rng.randn() + priors[rc1_idx],
                          65, 80)
        rc2 = dnest4.wrap(prior_vars[rc2_idx] * rng.randn() + priors[rc2_idx],
                          0.1, 10)
        rcfrac = dnest4.wrap(
            prior_vars[rcfrac_idx] * rng.randn() + priors[rcfrac_idx], 0.9, 1)

        aliasrc = dnest4.wrap(
            prior_vars[aliasrc_idx] * rng.randn() + priors[aliasrc_idx], 0.01,
            10)

        # grad = rng.randint(len(detector.gradList))
        # avgImp = rng.randint(len(detector.impAvgList))
        charge_trapping = rng.rand() * (1000 - traprc_min) + traprc_min

        grad = prior_vars[grad_idx] * rng.randn() + priors[grad_idx]
        avgImp = grad = prior_vars[grad_idx +
                                   1] * rng.randn() + priors[grad_idx + 1]
        grad = dnest4.wrap(grad, detector.gradList[0], detector.gradList[-1])
        avgImp = dnest4.wrap(avgImp, detector.impAvgList[0],
                             detector.impAvgList[-1])

        h_100_beta = (beta_lims[1] - beta_lims[0]) * rng.rand() + beta_lims[0]
        h_111_beta = (beta_lims[1] - beta_lims[0]) * rng.rand() + beta_lims[0]

        h_100_va = dnest4.wrap(
            prior_vars[velo_first_idx] * rng.randn() + priors[velo_first_idx],
            1, 10 * priors[velo_first_idx])
        h_111_va = dnest4.wrap(
            prior_vars[velo_first_idx + 1] * rng.randn() +
            priors[velo_first_idx + 1], 1, 10 * priors[velo_first_idx])
        h_100_vmax = dnest4.wrap(
            prior_vars[velo_first_idx + 2] * rng.randn() +
            priors[velo_first_idx + 2], 1, 10 * priors[velo_first_idx])
        h_111_vmax = dnest4.wrap(
            prior_vars[velo_first_idx + 3] * rng.randn() +
            priors[velo_first_idx + 3], 1, 10 * priors[velo_first_idx])

        return np.hstack([
            phi,
            omega,
            d,
            #b, c, dc,
            rc1,
            rc2,
            rcfrac,
            aliasrc,
            h_100_va,
            h_111_va,
            h_100_vmax,
            h_111_vmax,
            h_100_beta,
            h_111_beta,
            #   k0_0, k0_1, k0_2, k0_3,
            grad,
            avgImp,
            charge_trapping,
            rad_arr[:],
            phi_arr[:],
            theta_arr[:],
            scale_arr[:],
            t0_arr[:],
            smooth_arr[:],
        ])
Example #45
0
    def from_prior(self):
        """
        Unlike in C++, this must *return* a numpy array of parameters.
        """
        r_arr      = np.empty(num_waveforms)
        z_arr      = np.empty(num_waveforms)

        rad_arr      = np.empty(num_waveforms)
        phi_arr    = np.empty(num_waveforms)
        theta_arr  = np.empty(num_waveforms)
        scale_arr  = np.empty(num_waveforms)
        t0_arr     = np.empty(num_waveforms)
        smooth_arr = np.empty(num_waveforms)
        m_arr      = np.empty(num_waveforms)
        b_arr      = np.empty(num_waveforms)

        print "\n"
        #draw 8 waveform params for each waveform
        for (wf_idx, wf) in enumerate(wfs):
            (r,z, scale, t0) = draw_position(wf_idx)
            smooth_guess = 10

            r_arr[wf_idx] = r
            z_arr[wf_idx] = z
            # rad_arr[wf_idx] = rad
            phi_arr[wf_idx] = rng.rand() * np.pi/4
            # theta_arr[wf_idx] = theta
            scale_arr[wf_idx] = 5*rng.randn() + scale - .005*scale
            t0_arr[wf_idx] = 3*rng.randn() + maxt_guess
            smooth_arr[wf_idx] = np.clip(rng.randn() + smooth_guess, 0, 20)
            m_arr[wf_idx] =  0.0001*rng.randn() + 0.
            b_arr[wf_idx] =  0.001*rng.randn() + 0.

            # print "  creating wf %d" % wf_idx
            # print "  >>",
            # print rad_arr[wf_idx], phi_arr[wf_idx]/np.pi, theta_arr[wf_idx]/np.pi, t0_arr[wf_idx]
            # print "  ", rad_arr[wf_idx], theta_arr[wf_idx]/np.pi

        phi = np.pi * rng.rand() - np.pi/2
        omega = np.pi * rng.rand()
        d = rng.rand()

        # b = rng.rand() * 0
        # c = 0.05 *rng.randn() + c_prior
        # dc =  0.01 *rng.randn() + dc_prior

        #limit from 60 to 90
        rc1 = dnest4.wrap(prior_vars[rc1_idx]*rng.randn() + priors[rc1_idx], 65, 80)
        rc2 = dnest4.wrap(prior_vars[rc2_idx]*rng.randn() + priors[rc2_idx], 0.1, 10)
        rcfrac = dnest4.wrap(prior_vars[rcfrac_idx]*rng.randn() + priors[rcfrac_idx], 0.9, 1)
        charge_trapping = dnest4.wrap(prior_vars[trap_idx]*rng.randn() + priors[trap_idx], 50, 1000)

        grad = np.int(np.clip(prior_vars[grad_idx]*np.int(rng.randn()) + priors[grad_idx], 0, len(detector.gradList)-1))

        #6 hole drift params
        h_100_mu0 = .01 * h_100_mu0_prior*rng.randn() + h_100_mu0_prior
        lnbeta = np.log(1/(.1*h_100_beta_prior))*rng.randn() + np.log(1./h_100_beta_prior)
        h_100_lnbeta = dnest4.wrap(lnbeta, 0, np.log(1/.1))
        h_100_emu = .01 * (h_100_e0_prior*h_100_mu0_prior)*rng.randn() + h_100_e0_prior*h_100_mu0_prior

        h_111_mu0 = .01 * h_111_mu0_prior*rng.randn() + h_111_mu0_prior
        lnbeta = np.log(1/(.1*h_111_beta_prior))*rng.randn() + np.log(1./h_111_e0_prior)
        h_111_lnbeta = dnest4.wrap(lnbeta, 0, np.log(1/.1))
        h_111_emu = .01 * (h_111_e0_prior*h_111_mu0_prior)*rng.randn() + h_111_e0_prior*h_111_mu0_prior

        self.changed_wfs[:] = 1

        return np.hstack([
              phi, omega, d,
              #b, c, dc,
              rc1, rc2, rcfrac,
              h_100_mu0, h_100_lnbeta, h_100_emu, h_111_mu0, h_111_lnbeta, h_111_emu,
              grad, charge_trapping,
              r_arr[:], phi_arr[:], z_arr[:], scale_arr[:], t0_arr[:],smooth_arr[:], m_arr[:], b_arr[:]
            ])
Example #46
0
    def perturb_detector(self, params, which):
        logH = 0.0

        if which == ba_idx:  #lets call this phi
            params[which] += (tf_phi_max + np.pi / 2) * dnest4.randh()
            params[which] = dnest4.wrap(params[which], -np.pi / 2, tf_phi_max)
        elif which == c_idx:  #call it omega
            params[which] += 0.1 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0.07, 0.2)
        elif which == dc_idx:  #d
            params[which] += 0.01 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0.7, 0.9)

        elif which == rc1_idx or which == rc2_idx or which == rcfrac_idx:
            #all normally distributed priors
            logH -= -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2
            params[which] += prior_vars[which] * dnest4.randh()
            logH += -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2

        elif which == aliasrc_idx:
            params[which] += 9.9 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0.1, 10)

        elif which == grad_idx:
            logH -= -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2
            params[which] += prior_vars[which] * dnest4.randh()
            params[which] = dnest4.wrap(params[which], detector.gradList[0],
                                        detector.gradList[-1])
            logH += -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2

        elif which == imp_avg_idx:
            logH -= -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2
            params[which] += prior_vars[which] * dnest4.randh()
            params[which] = dnest4.wrap(params[which], detector.impAvgList[0],
                                        detector.impAvgList[-1])
            logH += -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2

        elif which == trap_idx:
            params[which] += (1000 - traprc_min) * dnest4.randh()
            params[which] = dnest4.wrap(params[which], traprc_min, 1000)

        elif which >= velo_first_idx and which < velo_first_idx + 4:
            logH -= -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2
            params[which] += prior_vars[which] * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 1, priors[which] * 10)
            logH += -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2

        elif which == velo_first_idx + 4 or which == velo_first_idx + 5:
            params[which] += (beta_lims[1] - beta_lims[0]) * dnest4.randh()
            params[which] = dnest4.wrap(params[which], beta_lims[0],
                                        beta_lims[1])

        else:  #velocity or rc params: cant be below 0, can be arb. large
            print("which value %d not supported" % which)
            exit(0)

        return logH
    def perturb(self, params):
        """
        Unlike in C++, this takes a numpy array of parameters as input,
        and modifies it in-place. The return value is still logH.
        """
        logH = 0.0
        which = rng.randint(len(params))

        if which == 0 or which == 4:  #radius and t0
            #FIND THE MAXIMUM RADIUS STILL INSIDE THE DETECTOR
            theta_eq = np.arctan(detector.detector_length /
                                 detector.detector_radius)
            theta_taper = np.arctan(detector.taper_length /
                                    detector.detector_radius)
            theta = params[2]
            #   print "theta: %f pi" % (theta / np.pi)
            if theta <= theta_taper:
                z = np.tan(theta) * (detector.detector_radius -
                                     detector.taper_length) / (1 -
                                                               np.tan(theta))
                max_rad = z / np.sin(theta)
            elif theta <= theta_eq:
                max_rad = detector.detector_radius / np.cos(theta)
            #   print "max rad radius: %f" %  max_rad
            else:
                theta_comp = np.pi / 2 - theta
                max_rad = detector.detector_length / np.cos(theta_comp)
            #   print "max rad length: %f" %  max_rad

            #AND THE MINIMUM (from PC dimple)
            #min_rad  = 1./ ( np.cos(theta)**2/detector.pcRad**2  +  np.sin(theta)**2/detector.pcLen**2 )
            min_rad = np.amax([detector.pcRad, detector.pcLen])

            mean = [0, 0]
            cov = [[1, -0.8], [-0.8, 1]]
            jumps = np.array((0.1 * dnest4.randh(), 0.1 * dnest4.randh()))
            (r_jump, t0_jump) = np.dot(cov, jumps)
            params[0] = dnest4.wrap(params[0] + r_jump, min_rad, max_rad)
            params[4] = dnest4.wrap(params[4] + t0_jump, min_t0, max_t0)

            if not checkPosition(params):
                print "... in radius step"

        elif which == 1:
            max_val = np.pi / 4
            params[which] += np.pi / 4 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0, max_val)
            if params[which] < 0 or params[which] > np.pi / 4:
                print "wtf phi"
            #params[which] = np.clip(params[which], 0, max_val)

        elif which == 2:  #theta
            rad = params[0]
            #   print "rad: %f" % rad
            if rad < np.amin([
                    detector.detector_radius - detector.taper_length,
                    detector.detector_length
            ]):
                max_val = np.pi / 2
                min_val = 0
            #   print "theta: min %f pi, max %f pi" % (min_val, max_val)
            else:
                if rad < detector.detector_radius - detector.taper_length:
                    #can't possibly hit the taper
                    #   print "less than taper adjustment"
                    min_val = 0
                elif rad < np.sqrt(detector.detector_radius**2 +
                                   detector.taper_length**2):
                    #low enough that it could hit the taper region
                    #   print "taper adjustment"
                    a = detector.detector_radius - detector.taper_length
                    z = 0.5 * (np.sqrt(2 * rad**2 - a**2) - a)
                    min_val = np.arcsin(z / rad)
                else:
                    #longer than could hit the taper
                    #   print  " longer thantaper adjustment"
                    min_val = np.arccos(detector.detector_radius / rad)

                if rad < detector.detector_length:
                    max_val = np.pi / 2
                else:
                    max_val = np.pi / 2 - np.arccos(
                        detector.detector_length / rad)
            #   print "theta: min %f pi, max %f pi" % (min_val, max_val)

            params[which] += (max_val - min_val) * dnest4.randh()
            params[which] = dnest4.wrap(params[which], min_val, max_val)
            #   params[which] = np.clip(params[which], min_val, max_val)
            if params[which] < min_val or params[which] > max_val:
                print "wtf theta"
            if not checkPosition(params):
                print "... in theta step with rad %f" % rad
                print "theta: min %f pi, max %f pi" % (min_val / np.pi,
                                                       max_val / np.pi)

        elif which == 3:  #scale
            params[which] += dnest4.randh()
            params[which] = dnest4.wrap(params[which],
                                        wf.wfMax - 10 * wf.baselineRMS,
                                        wf.wfMax + 10 * wf.baselineRMS)


#        elif which == 4: #t0
#          params[which] += 0.1*dnest4.randh()
#          params[which] = np.clip(params[which], 0, wf.wfLength)
        elif which == 5:  #smooth
            params[which] += 0.1 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0, 20)

        elif which == 6 or which == 7:  #m and b, respectively
            #normally distributed, no cutoffs
            params[which] += prior_vars[which] * dnest4.randh()
            if which == 6:
                dnest4.wrap(params[which], -0.01, 0.01)
                params[which] = np.clip(params[which], -0.01, 0.01)
            if which == 7:
                dnest4.wrap(params[which], -01, 01)
                params[which] = np.clip(params[which], -01, 01)
        else:
            print "which value %d not supported" % which
            exit(0)

        return logH
Example #48
0
    def perturb_wf(
        self,
        params,
        wf_idx,
    ):
        #do both wf and detector params in case theres strong correlation
        logH = 0.0

        reps = 1
        if rng.rand() < 0.5:
            reps += np.int(np.power(100.0, rng.rand()))

        for i in range(reps):
            wf_which = rng.randint(6)

            # my_which = rng.randint(len(priors) + 8)

            # if my_which < len(priors):
            #     #detector variable
            #     logH += self.perturb_detector(params, my_which)
            #
            # else:
            if wf_which < 6:
                #this is a waveform variable!
                # wf_which =  np.int(my_which - len(priors))

                #which idx of the global params array
                which = len(priors) + wf_which * num_waveforms + wf_idx

                rad_idx = len(priors) + wf_idx
                theta_idx = len(priors) + 2 * num_waveforms + wf_idx
                self.changed_wfs[wf_idx] = 1

                if wf_which == 0:
                    theta = params[theta_idx]

                    #FIND THE MAXIMUM RADIUS STILL INSIDE THE DETECTOR
                    theta_eq = np.arctan(detector.detector_length /
                                         detector.detector_radius)
                    theta_taper = np.arctan(detector.taper_length /
                                            detector.detector_radius)
                    #   print "theta: %f pi" % (theta / np.pi)
                    if theta <= theta_taper:
                        z = np.tan(theta) * (detector.detector_radius -
                                             detector.taper_length) / (
                                                 1 - np.tan(theta))
                        max_rad = z / np.sin(theta)
                    elif theta <= theta_eq:
                        max_rad = detector.detector_radius / np.cos(theta)
                    #   print "max rad radius: %f" %  max_rad
                    else:
                        theta_comp = np.pi / 2 - theta
                        max_rad = detector.detector_length / np.cos(theta_comp)
                    #   print "max rad length: %f" %  max_rad

                    #AND THE MINIMUM (from PC dimple)
                    #min_rad  = 1./ ( np.cos(theta)**2/detector.pcRad**2  +  np.sin(theta)**2/detector.pcLen**2 )

                    min_rad = np.amax([detector.pcRad, detector.pcLen])

                    total_max_rad = np.sqrt(detector.detector_length**2 +
                                            detector.detector_radius**2)

                    params[which] += total_max_rad * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], min_rad,
                                                max_rad)

                elif wf_which == 2:  #theta
                    rad = params[rad_idx]

                    #   print "rad: %f" % rad
                    if rad < np.amin([
                            detector.detector_radius - detector.taper_length,
                            detector.detector_length
                    ]):
                        max_val = np.pi / 2
                        min_val = 0
                    #   print "theta: min %f pi, max %f pi" % (min_val, max_val)
                    else:
                        if rad < detector.detector_radius - detector.taper_length:
                            #can't possibly hit the taper
                            #   print "less than taper adjustment"
                            min_val = 0
                        elif rad < np.sqrt(detector.detector_radius**2 +
                                           detector.taper_length**2):
                            #low enough that it could hit the taper region
                            #   print "taper adjustment"
                            a = detector.detector_radius - detector.taper_length
                            z = 0.5 * (np.sqrt(2 * rad**2 - a**2) - a)
                            min_val = np.arcsin(z / rad)
                        else:
                            #longer than could hit the taper
                            #   print  " longer thantaper adjustment"
                            min_val = np.arccos(detector.detector_radius / rad)

                        if rad < detector.detector_length:
                            max_val = np.pi / 2
                        else:
                            max_val = np.pi / 2 - np.arccos(
                                detector.detector_length / rad)
                    #   print "theta: min %f pi, max %f pi" % (min_val, max_val)

                    params[which] += np.pi / 2 * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], min_val,
                                                max_val)

                # if wf_which == 0:
                #     params[which] += (detector.detector_radius)*dnest4.randh()
                #     params[which] = dnest4.wrap(params[which] , 0, detector.detector_radius)
                elif wf_which == 1:
                    max_val = np.pi / 4
                    params[which] += np.pi / 4 * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], 0, max_val)
                    if params[which] < 0 or params[which] > np.pi / 4:
                        print("wtf phi")

                # elif wf_which == 2:
                #     params[which] += (detector.detector_length)*dnest4.randh()
                #     params[which] = dnest4.wrap(params[which] , 0, detector.detector_length)

                elif wf_which == 3:  #scale
                    wf = wfs[wf_idx]
                    min_scale = wf.wfMax - 0.01 * wf.wfMax
                    max_scale = wf.wfMax + 0.005 * wf.wfMax
                    params[which] += (max_scale - min_scale) * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], min_scale,
                                                max_scale)
                #   print "  adjusted scale to %f" %  ( params[which])

                elif wf_which == 4:  #t0
                    params[which] += 1 * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], min_maxt,
                                                max_maxt)
                elif wf_which == 5:  #smooth
                    params[which] += 0.1 * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], 0, 25)
                #   print "  adjusted smooth to %f" %  ( params[which])

                # elif wf_which == 6: #wf baseline slope (m)
                #     logH -= -0.5*(params[which]/1E-4)**2
                #     params[which] += 1E-4*dnest4.randh()
                #     logH += -0.5*(params[which]/1E-4)**2
                # elif wf_which == 7: #wf baseline incercept (b)
                #     logH -= -0.5*(params[which]/1E-2)**2
                #     params[which] += 1E-2*dnest4.randh()
                #     logH += -0.5*(params[which]/1E-2)**2
                else:
                    print("wf which value %d (which value %d) not supported" %
                          (wf_which, which))
                    exit(0)

                #   params[which] += 0.01*dnest4.randh()
                #   params[which]=dnest4.wrap(params[which], -1, 1)
                #   print "  adjusted b to %f" %  ( params[which])

        return logH
Example #49
0
    def from_prior(self):
        """
        Unlike in C++, this must *return* a numpy array of parameters.
        """
        r_arr = np.empty(num_waveforms)
        z_arr = np.empty(num_waveforms)

        rad_arr = np.empty(num_waveforms)
        phi_arr = np.empty(num_waveforms)
        theta_arr = np.empty(num_waveforms)
        scale_arr = np.empty(num_waveforms)
        t0_arr = np.empty(num_waveforms)
        smooth_arr = np.empty(num_waveforms)
        m_arr = np.empty(num_waveforms)
        b_arr = np.empty(num_waveforms)

        print "\n"
        #draw 8 waveform params for each waveform
        for (wf_idx, wf) in enumerate(wfs):
            (r, z, scale, t0, smooth) = draw_position(wf_idx)
            smooth_guess = 10
            t0 -= 20  #hack to go from 20 to 100 as t0guess
            t0 += t0_guess
            # r_arr[wf_idx] = r
            # z_arr[wf_idx] = z
            rad_arr[wf_idx] = np.sqrt(r**2 + z**2)
            phi_arr[wf_idx] = rng.rand() * np.pi / 4
            theta_arr[wf_idx] = np.arctan(z / r)
            scale_arr[wf_idx] = 5 * rng.randn() + scale
            t0_arr[wf_idx] = 3 * rng.randn() + t0
            smooth_arr[wf_idx] = np.clip(rng.randn() + smooth_guess, 0, 20)
            m_arr[wf_idx] = 0.001 * rng.randn() + 0.
            b_arr[wf_idx] = 0.01 * rng.randn() + 0.

            print "  creating wf %d" % wf_idx
            print "  >>",
            print r, phi_arr[wf_idx] / np.pi, z, t0_arr[wf_idx]
            # print "  ", rad_arr[wf_idx], theta_arr[wf_idx]/np.pi

        b_over_a = 0.1 * rng.randn() + ba_prior
        c = 0.05 * rng.randn() + c_prior
        dc = 0.01 * rng.randn() + dc_prior

        rc1 = dnest4.wrap(prior_vars[rc1_idx] * rng.randn() + priors[rc1_idx],
                          60, 90)
        rc2 = dnest4.wrap(prior_vars[rc2_idx] * rng.randn() + priors[rc2_idx],
                          0, 5)
        rcfrac = dnest4.wrap(
            prior_vars[rcfrac_idx] * rng.randn() + priors[rcfrac_idx], 0.9, 1)

        grad = np.int(
            np.clip(
                prior_vars[grad_idx] * np.int(rng.randn()) + priors[grad_idx],
                0,
                len(detector.gradList) - 1))
        charge_trapping = np.exp(20.0 * rng.rand())

        #6 hole drift params
        h_100_mu0 = .01 * velo_var * h_100_mu0_prior * rng.randn(
        ) + h_100_mu0_prior
        h_100_beta = .01 * velo_var * h_100_beta_prior * rng.randn(
        ) + h_100_beta_prior
        h_100_e0 = .01 * velo_var * h_100_e0_prior * rng.randn(
        ) + h_100_e0_prior
        h_111_mu0 = .01 * velo_var * h_111_mu0_prior * rng.randn(
        ) + h_111_mu0_prior
        h_111_beta = .01 * velo_var * h_111_beta_prior * rng.randn(
        ) + h_111_beta_prior
        h_111_e0 = .01 * velo_var * h_111_e0_prior * rng.randn(
        ) + h_111_e0_prior

        self.changed_wfs[:] = True

        return np.hstack([
            b_over_a,
            c,
            dc,
            rc1,
            rc2,
            rcfrac,
            h_100_mu0,
            h_100_beta,
            h_100_e0,
            h_111_mu0,
            h_111_beta,
            h_111_e0,
            grad,
            charge_trapping,
            #   r_arr[:], phi_arr[:], z_arr[:], scale_arr[:], t0_arr[:],smooth_arr[:], m_arr[:], b_arr[:]
            rad_arr[:],
            phi_arr[:],
            theta_arr[:],
            scale_arr[:],
            t0_arr[:],
            smooth_arr[:],
            m_arr[:],
            b_arr[:]
        ])
Example #50
0
    def from_prior(self):
        """
        Unlike in C++, this must *return* a numpy array of parameters.
        """
        # 6 waveform parameters
#        print "location: (%f, %f)" % (r, z)

        (r,z) = draw_position()
        rad = np.sqrt(r**2+z**2)
        theta = np.arctan(z/r)
        phi = rng.rand() * np.pi/4


#        det_max = np.sqrt(detector.detector_radius**2 + detector.detector_length**2)
#        rad = rng.rand() * det_max
#        phi = rng.rand() * np.pi/4
#        z = rng.rand() * np.pi/2

        t0 = np.clip(0.5*rng.randn() + t0_pad, min_t0, max_t0)
        scale = 10*rng.randn() + wf_guess[3]
        smooth = np.clip(rng.randn() + wf_guess[5], 0, 20)

        m =  prior_vars[6]*rng.randn() + priors[6]
        b =  prior_vars[7]*rng.randn() + priors[7]

#        import matplotlib.pyplot as plt
#        plt.figure()
#
#        for idx in range(len(location_idxs[0])):
#          r_idx= location_idxs[0][idx]
#          z_idx= location_idxs[1][idx]
#          plt.scatter(r_arr[r_idx], z_arr[z_idx])
#
#          print "location: (%f, %f)" % (r_arr[r_idx], z_arr[z_idx])
#        plt.xlim(0, detector.detector_radius)
#        plt.ylim(0, detector.detector_length)
#        plt.show()
#
#        print "wf max is %f, scale set to %f" % (wf.wfMax, scale)

        b_over_a = 0.1*rng.randn() + ba_prior

        mean = [0, 0]
        cov = [[1, -0.99], [-0.99, 1]]
        x, y = np.random.multivariate_normal(mean, cov, 1).T
        c = 0.01*x + c_prior
        d = 0.01*y + d_prior

        rc1 = dnest4.wrap(prior_vars[rc1_idx]*rng.randn() + priors[rc1_idx], 50, 100)
        rc2 = dnest4.wrap(prior_vars[rc2_idx]*rng.randn() + priors[rc2_idx], 0, 5)
        rcfrac = dnest4.wrap(prior_vars[rcfrac_idx]*rng.randn() + priors[rcfrac_idx], 0.9, 1)

        charge_trapping = prior_vars[trap_idx]*rng.randn() + priors[trap_idx]

        grad = np.int(np.clip(prior_vars[grad_idx]*np.int(rng.randn()) + priors[grad_idx], 0, max_grad))

        #6 hole drift params

        h_100_mu0 = .01*var * h_100_mu0_prior*rng.randn() + h_100_mu0_prior
        h_100_beta = .01*var * h_100_beta_prior*rng.randn() + h_100_beta_prior
        h_100_e0 = .01*var * h_100_e0_prior*rng.randn() + h_100_e0_prior
        h_111_mu0 = .01*var * h_111_mu0_prior*rng.randn() + h_111_mu0_prior
        h_111_beta = .01*var * h_111_beta_prior*rng.randn() + h_111_beta_prior
        h_111_e0 = .01*var * h_111_e0_prior*rng.randn() + h_111_e0_prior

        print "\n"
        print "new waveform:"
        print "  wf params: ",
        print  r, phi, z, scale, t0, smooth, m, b
        print "  tf params: ",
        print b_over_a, c, d, rc1, rc2, rcfrac
        print "  velo params: ",
        print h_100_mu0, h_100_beta, h_100_e0, h_111_mu0, h_111_beta, h_111_e0


#        import matplotlib.pyplot as plt
#        from matplotlib import gridspec
#        plt.ion()
#        fig1 = plt.figure(1, figsize=(20,10))
#        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")
#
#        dataLen = wf.wfLength
#        t_data = np.arange(dataLen) * 10
#        ax0.plot(t_data, wf.windowedWf, color="r")
#
#        fitSamples = 300
#        detector.SetTransferFunction(b_over_a, c, d, rc1, rc2, rcfrac)
#        detector.siggenInst.set_hole_params(h_100_mu0, h_100_beta, h_100_e0, h_111_mu0, h_111_beta, h_111_e0)
#        ml_wf = detector.MakeSimWaveform(r, phi, z, scale, t0,  fitSamples, h_smoothing = smooth)
#        ax0.plot(t_data, ml_wf[:dataLen], color="g", alpha=0.5)
#        ax1.plot(t_data, ml_wf[:dataLen] -  wf.windowedWf, color="g",alpha=0.5)
#
#
#        value = raw_input('  --> Press q to quit, s to skip, any other key to continue\n')
#        if value == 'q':
#          exit(0)


        return np.array([
              rad, phi, theta, scale, t0, smooth, m, b,
              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,
              charge_trapping, grad
            ])
Example #51
0
    def perturb_wf(self, params, wf_idx):

        wf_which = rng.randint(8)  #8 wf params
        which = len(priors) + wf_which * num_waveforms + wf_idx

        if wf_which == 0 or wf_which == 4:  #radius and t0
            wf_idx = (which - len(priors)) % num_waveforms
            rad_idx = len(priors) + wf_idx
            theta_idx = len(priors) + 2 * num_waveforms + wf_idx
            t0_idx = len(priors) + 4 * num_waveforms + wf_idx

            theta = params[theta_idx]

            #FIND THE MAXIMUM RADIUS STILL INSIDE THE DETECTOR
            theta_eq = np.arctan(detector.detector_length /
                                 detector.detector_radius)
            theta_taper = np.arctan(detector.taper_length /
                                    detector.detector_radius)
            #   print "theta: %f pi" % (theta / np.pi)
            if theta <= theta_taper:
                z = np.tan(theta) * (detector.detector_radius -
                                     detector.taper_length) / (1 -
                                                               np.tan(theta))
                max_rad = z / np.sin(theta)
            elif theta <= theta_eq:
                max_rad = detector.detector_radius / np.cos(theta)
            #   print "max rad radius: %f" %  max_rad
            else:
                theta_comp = np.pi / 2 - theta
                max_rad = detector.detector_length / np.cos(theta_comp)
            #   print "max rad length: %f" %  max_rad

            #AND THE MINIMUM (from PC dimple)
            #min_rad  = 1./ ( np.cos(theta)**2/detector.pcRad**2  +  np.sin(theta)**2/detector.pcLen**2 )
            min_rad = np.amax([detector.pcRad, detector.pcLen])

            mean = [0, 0]
            cov = [[1, -0.8], [-0.8, 1]]
            jumps = np.array((0.1 * dnest4.randh(), 0.1 * dnest4.randh()))
            (r_jump, t0_jump) = np.dot(cov, jumps)

            params[rad_idx] = dnest4.wrap(params[rad_idx] + r_jump, min_rad,
                                          max_rad)
            params[t0_idx] = dnest4.wrap(params[t0_idx] + t0_jump, min_t0,
                                         max_t0)

        elif wf_which == 1:
            max_val = np.pi / 4
            params[which] += np.pi / 4 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0, max_val)
            if params[which] < 0 or params[which] > np.pi / 4:
                print "wtf phi"
            #params[which] = np.clip(params[which], 0, max_val)

        elif wf_which == 2:  #theta
            wf_idx = (which - len(priors)) % num_waveforms
            rad_idx = len(priors) + wf_idx
            rad = params[rad_idx]
            #   print "rad: %f" % rad
            if rad < np.amin([
                    detector.detector_radius - detector.taper_length,
                    detector.detector_length
            ]):
                max_val = np.pi / 2
                min_val = 0
            #   print "theta: min %f pi, max %f pi" % (min_val, max_val)
            else:
                if rad < detector.detector_radius - detector.taper_length:
                    #can't possibly hit the taper
                    #   print "less than taper adjustment"
                    min_val = 0
                elif rad < np.sqrt(detector.detector_radius**2 +
                                   detector.taper_length**2):
                    #low enough that it could hit the taper region
                    #   print "taper adjustment"
                    a = detector.detector_radius - detector.taper_length
                    z = 0.5 * (np.sqrt(2 * rad**2 - a**2) - a)
                    min_val = np.arcsin(z / rad)
                else:
                    #longer than could hit the taper
                    #   print  " longer thantaper adjustment"
                    min_val = np.arccos(detector.detector_radius / rad)

                if rad < detector.detector_length:
                    max_val = np.pi / 2
                else:
                    max_val = np.pi / 2 - np.arccos(
                        detector.detector_length / rad)
            #   print "theta: min %f pi, max %f pi" % (min_val, max_val)

            params[which] += (max_val - min_val) * dnest4.randh()
            params[which] = dnest4.wrap(params[which], min_val, max_val)
            #   params[which] = np.clip(params[which], min_val, max_val)
            if params[which] < min_val or params[which] > max_val:
                print "wtf theta"

        elif wf_which == 3:  #scale
            wf_idx = (which - len(priors)) % num_waveforms
            wf = wfs[wf_idx]
            params[which] += dnest4.randh()
            params[which] = dnest4.wrap(params[which],
                                        wf.wfMax - 10 * wf.baselineRMS,
                                        wf.wfMax + 10 * wf.baselineRMS)
            params[which] = np.clip(params[which],
                                    wf.wfMax - 50 * wf.baselineRMS,
                                    wf.wfMax + 50 * wf.baselineRMS)
        #   print "  adjusted scale to %f" %  ( params[which])
        elif wf_which == 5:  #smooth
            params[which] += 0.1 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0, 25)
        #   print "  adjusted smooth to %f" %  ( params[which])

        elif wf_which == 6:  #wf baseline slope (m)
            params[which] += 0.001 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], -0.01, 0.01)
        #   print "  adjusted m to %f" %  ( params[which])
        elif wf_which == 7:  #wf baseline incercept (b)
            params[which] += 0.01 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], -1, 1)
        #   print "  adjusted b to %f" %  ( params[which])
        else:
            print "unknown wf param number drawn: %d" % wf_which
            exit(0)
Example #52
0
 def perturb(self, coords):
     i = np.random.randint(self.ndim)
     coords[i] += self.width*dnest4.randh()
     # Note: use the return value of wrap, unlike in C++
     coords[i] = dnest4.wrap(coords[i], -0.5*self.width, 0.5*self.width)
     return 0.0