Exemplo n.º 1
0
def cosmicCorrection(file, binsize=10, removalRange=10):
    '''
     function finds and deletes cosmic ray suspects. Assigns each arrival time to a time bin, 
     then returns number of counts for each time bin. Calculates average and threshold 
     for cosmic ray suspects. Assigns time bins with high counts to a dict (weirdones) with time as key and count 
     as value. Deletes time bins before and time bins after the suspected high count.

       Parameters
        ----------
        file: string or int
            H5 File name
        binsize: int
            size of time bins. Default is 10 microseconds.
        removalRange: int
            time bins to be deleted around cosmic ray occurrence. Default is 10 time bins.

       Returns
        -------
        Dictionary with keys:
            'returnDict': dictionary with keys as counts and values as # of bins with that count. Used in histogram.
            'peaks': list of times where peak of cosmic ray occurred
            'cutOut': list of all deleted times, including peaks and surrounding areas
            'goodones': list of times with no cosmic ray suspects      

    '''
    file = pt.Photontable(str(file))  # grab data

    start_time = time.time()  # def bin size in microseconds
    photons = file.photonTable.read(
    )  # grabs list for all photon arrivals (read)

    hist, bins = np.histogram(
        photons['Time'], bins=int(
            (photons['Time'].max() + 1) /
            binsize))  # returns tuple. hist -> counts/bin and bins -> bin
    unique, counts = np.unique(hist, return_counts=True)

    avgcounts = np.average(list(unique), weights=list(counts))

    threshold = np.ceil(6 * poisson.std(avgcounts, loc=0) + avgcounts)

    localmax = scipy.signal.find_peaks(
        hist, height=threshold, threshold=10, distance=30)[0] * 10

    cutOut = []
    for i in localmax:
        for k in range(i - removalRange, i + removalRange + 1):
            cutOut.append(k)

    print("--- %s seconds ---" % (time.time() - start_time))
    unique, counts = np.unique(hist, return_counts=True)
    goodOnes = np.delete(bins, cutOut)
    returnDict = dict(zip(unique, counts))
    return {
        'returnDict': returnDict,
        'peaks': localmax,
        'cutOut': cutOut,
        'goodOnes': goodOnes,
        'threshold': threshold
    }
Exemplo n.º 2
0
    def findCosmicRayTimeStamps(self, pList, binSize=10, threshold=None):
        s = time.time()
        photons = pList

        # hist, bins = np.histogram(photons['Time'], bins=int((photons['Time'].max()+1)/binSize))
        hist = np.bincount((photons['Time'] / 10).astype(int))

        if threshold is None:
            unique, counts = np.unique(hist, return_counts=True)
            avgCounts = np.average(list(unique), weights=list(counts))
            threshold = np.ceil(6 * poisson.std(avgCounts, loc=0) + avgCounts)

        peaks = scipy.signal.find_peaks(
            hist, height=threshold, threshold=0, distance=20)[0] * 10
        cutOut = np.unique(
            np.array([np.arange(peak - 50, peak + 101, 1) for peak in peaks]))
        e = time.time()
        print(
            f"---- It took {int(e-s)} seconds to find the comic ray peaks ----"
        )
        return peaks, cutOut
Exemplo n.º 3
0
 def std(self, dist):
     return poisson.std(*self._get_params(dist))
Exemplo n.º 4
0
 def std(self, n, p):
     std = poisson.std(self, n, p)
     return std
Exemplo n.º 5
0
from scipy.stats import poisson
import numpy as np


# Assume the number of typo errors on a single page of a book follows
# Poisson distribution with parameter 1/3. Calculate the probability
# that on one page there are
# no typos (pmf(0,1/3) = 0.7165313105737893)
# exactly two typos = (pmf(0,1/3) = 0.03980729503187717)

k = 1
mu = 2.5

expect = poisson.expect(args=(mu,), loc=0)
mean = poisson.mean(mu)
var = poisson.var(mu)
sigma = poisson.std(mu)
pmf = poisson.pmf(k, mu, loc=0)
cdf = poisson.cdf(k, mu, loc=0)
e_x_squared = mu**2 + mu

print('expected = ', expect)
print('mean = ', mean)
print('variance = ', var)
print('std. dev. = ',sigma)
print('pmf = ', pmf)
print('cdf = ', cdf)
print('E[X]^2 = ', e_x_squared)