Skip to content

Commit

Permalink
shifted tests to proper test routine
Browse files Browse the repository at this point in the history
  • Loading branch information
Clancy James committed Jan 10, 2024
1 parent 93a749a commit 86b8fee
Show file tree
Hide file tree
Showing 3 changed files with 288 additions and 210 deletions.
195 changes: 1 addition & 194 deletions zdm/energetics.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,200 +46,7 @@ def init_igamma_splines(gammas, reinit=False,k=3):
else:
igamma_splines[gamma] = interpolate.splrep(avals, numer,k=k)



def test_spline_accuracy(gamma = -1.5837, Ntest = 100):
"""
Function to test the accuracy of the spline interplation
It explores different methods of spline interpolation
Ntest [int]: number of random values to generate
gamma [float]: value of the index gamma to test at
Output: This produces three plots, being
spline_test.pdf: compares splines to truth
spline_errors.pdf: plots absolute value of errors
spline_rel_errors.pdf: plots absolute value of relative errors
"""
global SplineMin,SplineMax,NSpline,SplineLog

# generate random values in the range of the splines
lnewavals = np.random.rand(Ntest) * (SplineMax - SplineMin) + SplineMin
lnewavals = np.sort(lnewavals)
newavals = 10**lnewavals


# generate the true values at these avalues via direct calculation
truth = np.zeros(Ntest)
for i,av in enumerate(newavals):
truth[i] = mpmath.gammainc(gamma, a=av)

# sets up for plotting results
from matplotlib import pyplot as plt
plt.figure()
ax1=plt.gca()
plt.plot(newavals,truth,label='truth')
plt.xlabel('a')
plt.ylabel('cumulative gamma function')
plt.xscale('log')
plt.yscale('log')

# sets up for plotting differences between truth and interpolation
plt.figure()
ax2=plt.gca()
plt.xlabel('a')
plt.ylabel('|interpolation - truth|')
plt.xscale('log')
plt.yscale('log')

# sets up for plotting relative differences between truth and interpolation
plt.figure()
ax3=plt.gca()
plt.xlabel('a')
plt.ylabel('|interpolation - truth|/truth')
plt.xscale('log')
plt.yscale('log')

# now use different spline methods to evaluate
# standard method: cubic, linear

SplineLog=False
init_igamma_splines([gamma],reinit=True,k=3)
result = interpolate.splev(newavals, igamma_splines[gamma])
ax1.plot(newavals,result,label='cubic spline, linear')
diff = np.abs(result - truth)
ax2.plot(newavals,diff,label='cubic spline, linear')
rdiff = diff/truth
ax3.plot(newavals,rdiff,label='cubic spline, linear')

# now use different spline methods to evaluate
# standard method: cubic, linear
init_igamma_splines([gamma],reinit=True,k=1)
result = interpolate.splev(newavals, igamma_splines[gamma])
ax1.plot(newavals,result,label='linear spline, linear')
diff = np.abs(result - truth)
ax2.plot(newavals,diff,label='linear spline, linear')
rdiff = diff/truth
ax3.plot(newavals,rdiff,label='linear spline, linear')


SplineLog=True
init_igamma_splines([gamma],reinit=True,k=3)
result = 10**interpolate.splev(lnewavals, igamma_splines[gamma])
ax1.plot(newavals,result,label='cubic spline, log')
diff = np.abs(result - truth)
ax2.plot(newavals,diff,label='cubic spline, log')
rdiff = diff/truth
ax3.plot(newavals,rdiff,label='cubic spline, log')


# now use different spline methods to evaluate
# standard method: cubic, linear
init_igamma_splines([gamma],reinit=True,k=1)
result = 10**interpolate.splev(lnewavals, igamma_splines[gamma])
ax1.plot(newavals,result,label='linear spline, log')
diff = np.abs(result - truth)
ax2.plot(newavals,diff,label='linear spline, log')
rdiff = diff/truth
ax3.plot(newavals,rdiff,label='linear spline, log')

plt.sca(ax1)
plt.legend()
plt.tight_layout()
plt.savefig('spline_test.pdf')
plt.close()

plt.sca(ax2)
plt.legend()
plt.tight_layout()
plt.savefig('spline_errors.pdf')

plt.sca(ax3)
plt.legend()
plt.ylim(1e-14,1)
plt.tight_layout()
plt.savefig('spline_rel_errors.pdf')
plt.close()


def time_splines(gamma = -1.5837, Ntimetest = 100000, Nreps=100):
"""
Function to time different methods of spline interpolation.
It explores different methods of spline interpolation
gamma [float]: value of the index gamma to test at
Ntimetest [int]: number of random values to generate to test on
Nreps [int]: number of repetitions for timing
"""
global SplineMin,SplineMax,NSpline

import time

# begins with very large array of values to evaluate on
lnewavals = np.random.rand(Ntimetest) * (SplineMax - SplineMin) + SplineMin
newavals = 10**lnewavals

# now use different spline methods to evaluate
# standard method: cubic, linear

SplineLog=False
t0=time.time()
for i in np.arange(Nreps):
init_igamma_splines([gamma],reinit=True,k=3)
t1=time.time()
for i in np.arange(Nreps):
result = interpolate.splev(newavals, igamma_splines[gamma])
t2=time.time()
dt1_ci = t1-t0
dt2_ci = t2-t1

# now use different spline methods to evaluate
# standard method: cubic, linear
t0=time.time()
for i in np.arange(Nreps):
init_igamma_splines([gamma],reinit=True,k=1)
t1=time.time()
for i in np.arange(Nreps):
result = interpolate.splev(newavals, igamma_splines[gamma])
t2=time.time()
dt1_li = t1-t0
dt2_li = t2-t1

SplineLog=True
t0=time.time()
for i in np.arange(Nreps):
init_igamma_splines([gamma],reinit=True,k=3)
t1=time.time()
for i in np.arange(Nreps):
result = 10**interpolate.splev(lnewavals, igamma_splines[gamma])
t2=time.time()
dt1_co = t1-t0
dt2_co = t2-t1

# now use different spline methods to evaluate
# standard method: cubic, linear
t0=time.time()
for i in np.arange(Nreps):
init_igamma_splines([gamma],reinit=True,k=1)
t1=time.time()
for i in np.arange(Nreps):
result = 10**interpolate.splev(lnewavals, igamma_splines[gamma])
t2=time.time()
dt1_lo = t1-t0
dt2_lo = t2-t1

print("Performing ",Nreps," spline initialisations took...")
print("Cubic spline in linear space: ",dt1_ci)
print("Linear spline in linear space: ",dt1_li)
print("Cubic spline in log space: ",dt1_co)
print("Linear spline in log space: ",dt1_lo)

print("Performing ",Nreps," x ",Ntimetest," spline evaluations took...")
print("Cubic spline in linear space: ",dt2_ci)
print("Linear spline in linear space: ",dt2_li)
print("Cubic spline in log space: ",dt2_co)
print("Linear spline in log space: ",dt2_lo)


def init_igamma_linear(gammas:list, reinit:bool=False,
log:bool=False):
""" Setup the linear interpolator for gamma
Expand Down
16 changes: 0 additions & 16 deletions zdm/tests/Performance/test_splines.py

This file was deleted.

Loading

0 comments on commit 86b8fee

Please sign in to comment.