Esempio n. 1
0
    def lobe_logprior(self, pars_array, hyper_pars):

        T_Csig = hyper_pars[0]
        L_Csig = hyper_pars[1]

        # Iterate over individual lobe region log-priors and sum
        logprior = 0

        for i, item in enumerate(self.lobe_data):

            pars = pars_array[i, :]  # 2D pars array [i,j], i=reg nr, j=par nr

            # Gaussian priors for kT and Z, based on fit result of surrounding region
            kT = pars[0]
            mu_kT = self.kT_prior[i, 0]
            sigma_kT = self.kT_prior[i, 1]
            p_kT = norm.pdf(kT, loc=mu_kT, scale=sigma_kT)
            if (kT > 10.0) or (kT < 0): p_kT = 0

            Z = pars[1]
            mu_Z = self.Z_prior[i, 0]
            sigma_Z = self.Z_prior[i, 1]
            p_Z = norm.pdf(Z, loc=mu_Z, scale=sigma_Z)
            if Z < 0.05 or Z > 1.0: p_Z = 0

            T_lognorm = pars[2]
            p_Tnorm = halfcauchy.pdf(T_lognorm, loc=0, scale=T_Csig)

            PL_lognorm = pars[4]
            p_PLnorm = halfcauchy.pdf(PL_lognorm, loc=0, scale=L_Csig)

            #print 'lobe ', p_kT, p_Z, p_Tnorm, p_PLnorm
            logprior_reg = np.log(p_kT * p_Z * p_Tnorm * p_PLnorm)

            if not np.isfinite(logprior_reg):
                logprior += -logmin
            else:
                logprior += logprior_reg

        # PI
        PI = pars_array[0, 3]
        p_PI = ((PI > 1.2) & (PI < 2.5))

        logprior_linked = np.log(p_PI)

        if not np.isfinite(logprior_linked):
            logprior += -logmin
        else:
            logprior += logprior_linked

        return logprior
Esempio n. 2
0
    def lobe_logprior(self, pars_array, hyper_pars):

        # Iterate over individual lobe region log-priors and sum
        logprior = 0
        T_Csig = hyper_pars

        for i, item in enumerate(self.lobe_data):
            pars = pars_array[i, :]  # 2D pars array [i,j], i=reg nr, j=par nr

            # Gaussian priors for kT and Z, based on fit result of surrounding region
            kT = pars[0]
            mu_kT = self.kT_prior[i, 0]
            sigma_kT = self.kT_prior[i, 1]
            p_kT = norm.pdf(kT, loc=mu_kT, scale=sigma_kT)
            if kT < 0.05: p_kT = 0

            Z = pars[1]
            mu_Z = self.Z_prior[i, 0]
            sigma_Z = self.Z_prior[i, 1]
            p_Z = norm.pdf(Z, loc=mu_Z, scale=sigma_Z)
            if Z < 0.05: p_Z = 0

            T_lognorm = pars[2]
            p_Tnorm = halfcauchy.pdf(T_lognorm, loc=0, scale=T_Csig)

            logprior_reg = np.log(p_kT * p_Z * p_Tnorm)

            if not np.isfinite(logprior_reg):
                logprior += -logmin
            else:
                logprior += logprior_reg

        return logprior
Esempio n. 3
0
    def compute_shadow(self, peaks, current_peak):
        # 1. Define time window for each peak, we will find previous peaks within these time windows
        roi_shadow = np.zeros(len(current_peak), dtype=strax.time_fields)
        roi_shadow['time'] = current_peak['center_time'] - self.config[
            'shadow_time_window_backward']
        roi_shadow['endtime'] = current_peak['center_time']

        # 2. Calculate S2 position shadow, S2 time shadow, and S1 time shadow
        result = np.zeros(len(current_peak), self.dtype)
        for key in ['s2_position_shadow', 's2_time_shadow', 's1_time_shadow']:
            is_position = 'position' in key
            type_str = key.split('_')[0]
            stype = 2 if 's2' in key else 1
            mask_pre = (peaks['type'] == stype) & (
                peaks['area'] > self.config['shadow_threshold'][key])
            split_peaks = strax.touching_windows(peaks[mask_pre], roi_shadow)
            array = np.zeros(len(current_peak), np.dtype(self.shadowdtype))

            # Initialization
            array['x'] = np.nan
            array['y'] = np.nan
            array['dt'] = self.config['shadow_time_window_backward']
            # The default value for shadow is set to be the lowest possible value
            if 'time' in key:
                array['shadow'] = self.config['shadow_threshold'][key] * array[
                    'dt']**self.config['shadow_deltatime_exponent']
            else:
                array['shadow'] = 0
            array['nearest_dt'] = self.config['shadow_time_window_backward']

            # Calculating shadow, the Major of the plugin. Only record the previous peak casting the largest shadow
            if len(current_peak):
                self.peaks_shadow(
                    current_peak, peaks[mask_pre], split_peaks,
                    self.config['shadow_deltatime_exponent'], array,
                    is_position,
                    self.getsigma(self.config['shadow_sigma_and_baseline'],
                                  current_peak['area']))

            # Fill results
            names = ['shadow', 'dt']
            if 's2' in key:  # Only previous S2 peaks have (x,y)
                names += ['x', 'y']
            if 'time' in key:  # Only time shadow gives the nearest large peak
                names += ['nearest_dt']
            for name in names:
                if name == 'nearest_dt':
                    result[f'{name}_{type_str}'] = array[name]
                else:
                    result[f'{name}_{key}'] = array[name]

        distance = np.sqrt(
            (result[f'x_s2_position_shadow'] - current_peak['x'])**2 +
            (result[f'y_s2_position_shadow'] - current_peak['y'])**2)
        # If distance is NaN, set largest distance
        distance = np.where(np.isnan(distance), 2 * straxen.tpc_r, distance)
        # HalfCauchy PDF when calculating S2 position shadow
        result['pdf_s2_position_shadow'] = halfcauchy.pdf(
            distance,
            scale=self.getsigma(self.config['shadow_sigma_and_baseline'],
                                current_peak['area']))

        # 6. Set time and endtime for peaks
        result['time'] = current_peak['time']
        result['endtime'] = strax.endtime(current_peak)
        return result
Esempio n. 4
0
from scipy.stats import halfcauchy
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 1)

# Calculate a few first moments:

mean, var, skew, kurt = halfcauchy.stats(moments='mvsk')

# Display the probability density function (``pdf``):

x = np.linspace(halfcauchy.ppf(0.01), halfcauchy.ppf(0.99), 100)
ax.plot(x, halfcauchy.pdf(x), 'r-', lw=5, alpha=0.6, label='halfcauchy pdf')

# Alternatively, the distribution object can be called (as a function)
# to fix the shape, location and scale parameters. This returns a "frozen"
# RV object holding the given parameters fixed.

# Freeze the distribution and display the frozen ``pdf``:

rv = halfcauchy()
ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf')

# Check accuracy of ``cdf`` and ``ppf``:

vals = halfcauchy.ppf([0.001, 0.5, 0.999])
np.allclose([0.001, 0.5, 0.999], halfcauchy.cdf(vals))
# True

# Generate random numbers:

r = halfcauchy.rvs(size=1000)
from scipy.stats import halfcauchy
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 1)

# Calculate a few first moments:

mean, var, skew, kurt = halfcauchy.stats(moments='mvsk')

# Display the probability density function (``pdf``):

x = np.linspace(halfcauchy.ppf(0.01),
                halfcauchy.ppf(0.99), 100)
ax.plot(x, halfcauchy.pdf(x),
       'r-', lw=5, alpha=0.6, label='halfcauchy pdf')

# Alternatively, the distribution object can be called (as a function)
# to fix the shape, location and scale parameters. This returns a "frozen"
# RV object holding the given parameters fixed.

# Freeze the distribution and display the frozen ``pdf``:

rv = halfcauchy()
ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf')

# Check accuracy of ``cdf`` and ``ppf``:

vals = halfcauchy.ppf([0.001, 0.5, 0.999])
np.allclose([0.001, 0.5, 0.999], halfcauchy.cdf(vals))
# True

# Generate random numbers:
Esempio n. 6
0
    def jet_logprior(self, pars_array, hyper_pars):

        # hyperparameters for cauchy-distributed normalization priors
        T_Csig = hyper_pars[0]
        L_Csig = hyper_pars[1]
        J_Csig = hyper_pars[2]

        # Iterate over individual jet region log priors and sum
        logprior = 0

        for i, item in enumerate(self.jet_data):

            pars = pars_array[i, :]  # 2D pars array [i,j], i=reg nr, j=par nr

            # Gaussian priors for kT and Z, based on fit result of surrounding region
            kT = pars[0]
            mu_kT = self.kT_prior[i, 0]
            sigma_kT = self.kT_prior[i, 1]
            p_kT = norm.pdf(kT, loc=mu_kT, scale=sigma_kT)
            if kT < 0.05: p_kT = 0

            Z = pars[1]
            mu_Z = self.Z_prior[i, 0]
            sigma_Z = self.Z_prior[i, 1]
            p_Z = norm.pdf(Z, loc=mu_Z, scale=sigma_Z)
            if Z < 0.05: p_Z = 0

            # Half-Cauchy  prior
            # see e.g. Polson and Scott (2012)
            T_lognorm = pars[2]
            p_Tnorm = halfcauchy.pdf(T_lognorm * self.ratios[i],
                                     loc=0,
                                     scale=T_Csig)

            PL1_lognorm = pars[4]
            p_PL1norm = halfcauchy.pdf(PL1_lognorm * self.ratios[i],
                                       loc=0,
                                       scale=L_Csig)

            PL2_lognorm = pars[6]
            p_PL2norm = halfcauchy.pdf(PL2_lognorm, loc=0, scale=J_Csig)

            logprior_reg = np.log(p_kT * p_Z * p_Tnorm * p_PL1norm * p_PL2norm)

            if not np.isfinite(logprior_reg):
                logprior += -logmin
            else:
                logprior += logprior_reg

        # PI and hyper priors
        PI1 = pars_array[0, 3]
        p_PI1 = ((PI1 > 1.2) & (PI1 < 2.5))

        PI2 = pars_array[0, 5]
        p_PI2 = ((PI2 > 1.2) & (PI2 < 2.5))

        p_TCsig = ((T_Csig > 1e-7) & (T_Csig < 1e-3))

        p_LCsig = ((L_Csig > 1e-7) & (L_Csig < 1e-4))

        p_JCsig = ((J_Csig > 1e-7) & (J_Csig < 1e-4))

        logprior_linked = np.log(p_PI1 * p_PI2 * p_TCsig * p_LCsig * p_JCsig)

        if not np.isfinite(logprior_linked):
            logprior += -logmin
        else:
            logprior += logprior_linked

        return logprior