Пример #1
0
def test_histogram_str():
    hist = Histogram(nbins=[5, 10],
                     ranges=[[-2.5, 2.5], [-1, 1]],
                     name="testhisto")
    expected = ("Histogram(name='testhisto', axes=['axis0', 'axis1'], "
                "nbins=[ 5 10], ranges=[[-2.5  2.5]\n [-1.   1. ]])")
    assert str(hist) == expected
Пример #2
0
    def __init__(self, filename, col_altitude=0, col_thickness=2):
        """ 
        small class that calculates the height of the shower maximum
        given a parametrisation of the atmosphere 
        and certain parameters of the shower itself

        Parameters:
        -----------
        filename : string
            path to text file that contains a table of the 
            atmosphere parameters
        col_altitude : int
            column in the text file that contains the altitude/height
        col_thickness : int
            column in the text file that contains the thickness
        """

        altitude = []
        thickness = []
        atm_file = open(filename, "r")
        for line in atm_file:
            if line.startswith("#"):
                continue
            altitude.append(float(line.split()[col_altitude]))
            thickness.append(float(line.split()[col_thickness]))

        self.atmosphere = Histogram(axisNames=["altitude"])
        self.atmosphere.hist = thickness * u.g * u.cm ** -2
        self.atmosphere.bin_lower_edges = [np.array(altitude) * u.km]
Пример #3
0
def test_outliers():
    """
    Check that out-of-range values work as expected
    """
    H = Histogram(nbins=[5, 10], ranges=[[-2.5, 2.5], [-1, 1]])
    H.fill(np.array([
        [1, 1],
    ]))
    val1 = H.get_value((100, 100), outlier_value=-10000)
    val2 = H.get_value((-100, 0), outlier_value=None)
    assert val1 == -10000
    assert val2 == 0
Пример #4
0
def test_histogram_resample_inplace():
    hist = Histogram(nbins=[5, 11], ranges=[[-2.5, 2.5], [-1, 1]])
    hist.fill(np.array([[0, 0], [0, 0.5]]))

    for testpoint in [(0, 0), (0, 1), (1, 0), (3, 3)]:
        val0 = hist.get_value(testpoint)
        hist.resample_inplace((10, 22))
        hist.resample_inplace((5, 11))
        val2 = hist.get_value(testpoint)

        # at least check the resampling is undoable
        assert np.isclose(val0[0], val2[0])
Пример #5
0
def test_histogram_fits(histogram_file):
    """
    Write to fits,read back, and check
    """

    hist = Histogram(nbins=[5, 11], ranges=[[-2.5, 2.5], [-1, 1]])
    hist.fill(np.array([[0, 0], [0, 0.5]]))

    hist.to_fits().writeto(histogram_file, overwrite=True)
    newhist = Histogram.from_fits(histogram_file)

    # check that the values are the same
    compare_histograms(hist, newhist)
Пример #6
0
def test_histogram_range_fill_and_read():
    """
    Check that the correct bin is read and written for multiple
    binnings and fill positions
    """

    num = 100

    for nxbins in np.arange(1, 50, 1):
        for xx in np.arange(-2.0, 2.0, 0.1):
            pp = (xx + 0.01829384, 0.1)
            coords = np.ones((num, 2)) * np.array(pp)
            hist = Histogram(nbins=[nxbins, 10], ranges=[[-2.5, 2.5], [-1, 1]])
            hist.fill(coords)
            val = hist.get_value(pp)[0]
            assert val == num
            del hist
Пример #7
0
def test_histogram_fill_and_read():

    hist = Histogram(nbins=[5, 10], ranges=[[-2.5, 2.5], [-1, 1]])

    pa = (0.1, 0.1)
    pb = (-0.55, 0.55)

    a = np.ones((100, 2)) * pa  # point at 0.1,0.1
    b = np.ones((10, 2)) * pb  # 10 points at -0.5,0.5

    hist.fill(a)
    hist.fill(b)

    va = hist.get_value(pa)[0]
    vb = hist.get_value(pb)[0]

    assert va == 100
    assert vb == 10
Пример #8
0
def plot_muon_efficiency(outputpath):
    """
    Plot the muon efficiencies
    """
    fig, ax = plt.subplots(1, 1, figsize=(10, 10))
    figip, axip = plt.subplots(1, 1, figsize=(10, 10))
    figrw, axrw = plt.subplots(1, 1, figsize=(10, 10))

    nbins = 16
    t = Table.read(str(outputpath) + '_muontable.fits')
    print('Reading muon efficiency from table', outputpath, t['MuonEff'])

    if len(t['MuonEff']) < 1:
        print("No muon events to plot")
        return
    else:
        print("Found", len(t['MuonEff']), "muon events")

    (mu, sigma) = norm.fit(t['MuonEff'])

    print('Gaussian fit with mu=', mu, 'sigma=', sigma)

    conteff = ax.hist(t['MuonEff'], nbins)
    ax.set_xlim(0.2 * min(t['MuonEff']), 1.2 * max(t['MuonEff']))

    xtest = np.linspace(min(t['MuonEff']), max(t['MuonEff']), nbins)
    yg = mlab.normpdf(xtest, mu, sigma)
    print('mu', mu, 'sigma', sigma, 'yg', yg)
    ax.plot(xtest, yg, 'r', linewidth=2)

    ax.set_ylim(0., 1.2 * max(conteff[0]))
    ax.set_xlabel('Muon Efficiency')
    plt.draw()

    yimp = np.linspace(min(t['ImpactP']), max(t['ImpactP']), nbins)
    contimp = axip.hist(t['ImpactP'], nbins)
    axip.set_xlim(0.2 * min(t['ImpactP']), 1.2 * max(t['ImpactP']))
    axip.set_ylim(0., 1.2 * max(contimp[0]))
    axip.set_xlabel('Impact Parameter (m)')

    plt.draw()

    heffimp = Histogram(nbins=[16, 16],
                        ranges=[
                            (min(t['MuonEff']), max(t['MuonEff'])),
                            (min(t['ImpactP']), max(t['ImpactP']))
                        ])  # ,axisNames=["MuonEfficiency","ImpactParameter"])

    heffimp.fill([t['MuonEff'], t['ImpactP']])
    heffimp.draw_2d()

    yrw = np.linspace(min(t['RingWidth']), max(t['RingWidth']), nbins)
    contrw = axrw.hist(t['RingWidth'], nbins)
    axrw.set_xlim(0.2 * min(t['RingWidth']), 1.2 * max(t['RingWidth']))
    axrw.set_ylim(0., 1.2 * max(contrw[0]))
    axrw.set_xlabel('Ring Width ($^\circ$)')

    plt.draw()

    if outputpath is not None:
        print("saving figure at", outputpath)
        fig.savefig(str(outputpath) + '_MuonEff.png')
        figip.savefig(str(outputpath) + '_ImpactParameter.png')
        figrw.savefig(str(outputpath) + '_RingWidth.png')
    else:
        print("Not saving figure, no outputpath")
        plt.show()