Beispiel #1
0
def spline_fit(data,
               bins = None,
               range = None,
               weights = None,
               order = None,
               filename = 'spline.fits'):

    if bins is None:
        bins = data.shape[1]*[10]
        counts,bin_arrays = np.histogramdd(data,range=range,weights=weights)
        vars,bin_arrays = np.histogramdd(data,range=range,weights=weights**2)
    else:
        counts,bin_arrays = np.histogramdd(data,bins=bins,range=range,weights=weights)
        vars,bin_arrays = np.histogramdd(data,bins=bins,range=range,weights=weights**2)
    coords = [(b[1:]+b[:-1])/2. for b in bin_arrays]

    if order == None:
        order = list(np.zeros_like(bins))
    knots = pad_knots(bin_arrays, order)

    w = 1./np.sqrt(vars)
    w[~np.isfinite(w)] = np.nanmin(w)
    
    result = glam.fit(counts,w,coords,knots,order,0)
    
    if not filename is None:
        if os.path.exists(filename):
            os.system('rm '+filename)
        if filename[-5:]=='.fits':
            splinefitstable.write(result,filename)
        else:
            splinefitstable.write(result,filename+'.fits')

    return result
Beispiel #2
0
def makeTable(bintype='logdist', plot=False):

    # Starting parameters
    my.setupShowerLLH(verbose=False)
    s = load_sim(bintype=bintype)
    outFile = '%s/IT73_sim/Zfix_%s.fits' % (my.llh_data, bintype)
    nbins = 100
    thetaMax = 40.
    minE = 4.0

    thetaMax *= np.pi / 180.
    zbins = np.linspace(1, np.cos(thetaMax), nbins+1)[::-1]

    ebins = getEbins()
    t = np.log10(s['MC_energy'])
    r = np.log10(s['ML_energy'])
    z = np.pi - s['zenith']

    # Calculate cut values
    c0 = s['cuts']['llh']
    ecut = r >= minE
    c0 *= ecut

    # Store median and standard deviation info
    x1 = np.cos(z)[c0]
    if x1.min() > zbins.min():
        zbins = zbins[zbins >= x1.min()]
    y = (r - t)[c0]
    medians, sigL, sigR, vars = getMedian(x1, y, zbins)

    w = 1/vars
    nknots = 30
    step_scale = 2/5.
    step = (zbins.max() - zbins.min()) * step_scale
    mids = (zbins[1:] + zbins[:-1]) / 2.
    axes = [mids]
    knots = [np.linspace(zbins.min()-step, zbins.max()+step, nknots)]

    tab = glam.fit(medians, w, axes, knots, order=(4), penalties={2:1e-4})
    if os.path.exists(outFile):
        os.remove(outFile)
    splinefitstable.write(tab, outFile)

    if plot:
        fig, ax = plt.subplots()
        ax.set_title('Energy Resolution vs Reconstructed Zenith', fontsize=18)
        ax.set_xlabel('Cos(zenith)', fontsize=16)
        ax.set_ylabel('Ereco - Etrue (median)', fontsize=16)
        lw = 2
        ms = 7*lw
        pltParams = dict(fmt='.', lw=lw, ms=ms)
        # Energy resolution vs zenith
        x = getMids(zbins)
        ax.errorbar(x, medians, yerr=(sigL,sigR), **pltParams)
        # Spline fit
        fitx = np.linspace(x.min(), x.max(), len(x)*3)
        fit = glam.grideval(tab, [fitx])
        ax.plot(fitx, fit)
        plt.show()
Beispiel #3
0
from icecube.photospline.splinetable import SplineTable
from icecube.photospline.splinefitstable import write

fname = "constant.fits"

# make a constant spline surface
spline = SplineTable()
spline.ndim = 2
spline.order = [2 for i in range(spline.ndim)]
spline.knots = [numpy.linspace(0, 1, 10) for i in range(spline.ndim)]
nsplines = tuple(knots.size-order-1 for knots,order in zip(spline.knots, spline.order))
spline.coefficients = numpy.ones(nsplines)

if os.path.exists(fname):
	os.unlink(fname)
write(spline, fname)

x = [0.5]*spline.ndim

spline = I3SplineTable(fname)

assert spline.eval(x) == spline.eval(x, [0,0]) == 1.

# all second derivatives must be zero
for i in range(len(x)):
	derivs = [0]*2
	derivs[i] = 2
	assert spline.eval(x, derivs) == 0.

os.unlink(fname)
                      w,
                      bin_centers,
                      knots,
                      order,
                      smooth,
                      penalties=penalties)
    spline.geometry = Geometry.SPHERICAL
    spline.extents = extents[:3]
    spline.ngroup = table.header['n_group']

    print("Saving table to %s..." % abs_outputfile)
    spline.knots = [
        spline.knots[i] * axis_scale[i] for i in range(0, len(spline.knots))
    ]
    check_exists(abs_outputfile)
    splinefitstable.write(spline, abs_outputfile)

    # clean up
    del (w, z, bin_centers, bin_widths, order, penalties, knots, spline)

if opts.prob:
    z = table.values / norm.reshape(norm.shape + (1, ))
    # XXX HACK: ignore weights for normalized timing
    w = 1000 * numpy.ones(table.values.shape)

    # XXX HACK: don't believe anything that happens outside the
    #           tracking volume of the table
    #scalp(table, w, low=opts.ice_bottom, high=opts.ice_top)

    order, penalties, knots = spline_spec(4)
    bin_centers = [b.copy() for b in table.bin_centers]
Beispiel #5
0
        pad_knots(numpy.linspace(0, numpy.sqrt(100), 5)**2,
                  2),  # bundle multiplicity
        pad_knots(numpy.linspace(0, 250**(1. / 2),
                                 11)**2),  # distance to shower axis
        pad_knots(numpy.linspace(0, 20, 11), 2),  # log(energy) [log(E/GeV)]
    ]

    order = order[0:2] + [2, 2] + [order[-1]]
    penalties[2] = penalties[2][0:2] + [smooth / 1e4, smooth / 1e1
                                        ] + [penalties[2][-1]]
    penalties = {
        2:
        [smooth / 1e5, smooth / 1e5, smooth * 1e4, smooth / 1e3, smooth / 1e1]
    }  # Penalize curvature
    # penalties[2][0] *= 1e1
    # penalties[2][1] *= 1e4
    # penalties[2][-1] *= 1e1

    spline = glam.fit(hes.bincontent,
                      weights,
                      hes._h_bincenters,
                      knots,
                      order,
                      smooth,
                      penalties=penalties)
    spline.bias = bias

if os.path.exists(outfile):
    os.unlink(outfile)
splinefitstable.write(spline, outfile)
Beispiel #6
0
        sublist = lowmirror + sublist + highmirror
        print('done')

        print('\tStacking...', end=' ')
        intermedtables.append((stack_tables(sublist), z))
        print('done')

    # We no longer need to original tables
    del tables

    if len(zpos) > 1:
        finaltab = stack_tables(intermedtables)
    else:
        finaltab = intermedtables[0][0]

    try:
        targetfile = args[1]
    except IndexError:
        targetfile = os.path.normpath(
            os.path.join(
                os.path.abspath(sourcedir), '..',
                os.path.basename(os.path.abspath(sourcedir)) + '.fits'))

    finaltab.extents += extents

    try:
        splinefitstable.write(finaltab, targetfile)
        print("Output written to", targetfile)
    except Exception as inst:
        print(inst)
Beispiel #7
0
def spline(config, inFile, outFile, plot=False):

    # Load input median file
    d = np.load(inFile)
    d = d.item()
    xbins, ybins = d['xbins'], d['ybins']
    energies = d['medians']
    vars = d['var']
    vars[vars<1e-15] = np.inf   # Variances sufficiently close to 0 aren't real
    w = 1/vars
    emin, emax = energies[energies!=0].min(), energies.max()
    ebins = [emin] + getEbins() + [emax]

    axes, knots = [],[]
    binList = [xbins, ybins]
    nknots = 30
    step_scale = 2/5.
    for bins in binList:
        mids = (bins[1:]+bins[:-1])/2.
        axes += [mids]
        step = (bins.max() - bins.min()) * step_scale
        knots += [np.linspace(bins.min()-step, bins.max()+step, nknots)]

    tab = glam.fit(energies, w, axes, knots, order=(4), \
            penalties={2:1e-3})

    if plot:

        # Look at spline fit with finer binning
        fitaxes = [[],[]]
        fitaxes[0] = np.linspace(xbins.min(),xbins.max(),len(xbins)*3)
        fitaxes[1] = np.linspace(ybins.min(),ybins.max(),len(ybins)*3)

        fit = glam.grideval(tab, fitaxes)
        #err_fit = 10**glam.grideval(err_tab,axes)

        fig = plt.figure(figsize=(17,6))
        mpl.rc("font", family="serif")
        X, Y = np.meshgrid(axes[0], axes[1])
        fitX, fitY = np.meshgrid(fitaxes[0], fitaxes[1])

        # Setup custom colormap
        cmap = plt.cm.jet
        cmap = cmap_discretize(cmap, ebins)
        cmap.set_under('white')

        ax = fig.add_subplot(121)
        p = ax.pcolor(fitX, fitY, fit.T, cmap=cmap, vmin=emin, vmax=emax)
        cb = fig.colorbar(p, ax=ax)
        ax.set_title('Median Energy')
        ax.set_xlabel('cos(zenith)')
        ax.set_ylabel('log10(Nchannel)')
        ax.set_xlim(xbins.min(), xbins.max())
        ax.set_ylim(ybins.min(), ybins.max())

        ax = fig.add_subplot(122)
        p = ax.pcolor(X, Y, energies.T, cmap=cmap, vmin=emin, vmax=emax)
        ax.set_title('Median Energy')
        ax.set_xlabel('cos(zenith)')
        ax.set_ylabel('log10(Nchannel)')
        ax.set_xlim(xbins.min(), xbins.max())
        ax.set_ylim(ybins.min(), ybins.max())
        plt.show()

    if outFile:
        if os.path.exists(outFile):
            os.remove(outFile)
        splinefitstable.write(tab, outFile)