def test_fstatisticsAndReshapedSpectrum(self): """ Test for mtspec_pad with jackknife interval errors. The result is compared to the output of test_recreatePaperFigures.py in the same directory. This is assumed to be correct because they are identical to the figures in the paper on the machine that created these. """ data = load_mtdata('v22_174_series.dat.gz') # Calculate the spectra. spec, freq, jackknife, fstatistics, _ = mtspec(data, 4930., 3.5, nfft=312, number_of_tapers=5, statistics=True, rshape=0, fcrit=0.9) # No NaNs are supposed to be in the output. self.assertEqual(np.isnan(spec).any(), False) self.assertEqual(np.isnan(freq).any(), False) self.assertEqual(np.isnan(jackknife).any(), False) self.assertEqual(np.isnan(fstatistics).any(), False) # Load the good data. datafile = os.path.join(os.path.dirname(__file__), 'data', 'fstatistics.npz') record = np.load(datafile) spec2 = record['spec'] jackknife2 = record['jackknife'] fstatistics2 = record['fstatistics'] freq2 = np.arange(157) * 6.50127447e-07 # Compare. np.testing.assert_almost_equal(freq, freq2) np.testing.assert_almost_equal(spec / spec, spec2 / spec) np.testing.assert_almost_equal(jackknife / jackknife, jackknife2 / jackknife, 5) np.testing.assert_almost_equal(fstatistics / fstatistics, fstatistics2 / fstatistics, 5)
def test_figure1(self): """ Recreate Figure 1 """ data = load_mtdata('v22_174_series.dat.gz') spec, freq, jackknife, _, _ = mtspec(data, 4930., 3.5, number_of_tapers=5, nfft=312, statistics=True) fig = plt.figure() ax1 = fig.add_subplot(2, 1, 1) ax1.plot(data, color='black') ax1.set_xlim(0, len(data)) ax2 = fig.add_subplot(2, 1, 2) ax2.set_yscale('log') ax2.plot(freq, spec, color='black') try: ax2.fill_between(freq, jackknife[:, 0], jackknife[:, 1], color='grey') except: ax2.plot(freq, jackknife[:, 0], '--', color = 'red') ax2.plot(freq, jackknife[:, 1], '--', color = 'red') ax2.set_xlim(freq[0], freq[-1]) outfile = os.path.join(self.outpath, 'fig1.pdf') fig.savefig(outfile) stat = os.stat(outfile) self.assertTrue(abs(stat.st_mtime - time.time()) < 3)
def test_eigenspectraOutput(self): """ Tests the eigenspectra output using a nonadaptive spectra. This also at least somewhat tests the weights. """ data = load_mtdata('PASC.dat.gz') # Calculate the spectra. spec, freq, eigspec, eigcoef, weights = \ mtspec(data, 1.0, 4.5, number_of_tapers=5, adaptive=False, optional_output=True) # No NaNs are supposed to be in the output. self.assertEqual(np.isnan(spec).any(), False) self.assertEqual(np.isnan(spec).any(), False) self.assertEqual(np.isnan(eigspec).any(), False) self.assertEqual(np.isnan(eigcoef).any(), False) self.assertEqual(np.isnan(weights).any(), False) # The weights should all be one for the nonadaptive spectrum. np.testing.assert_almost_equal(weights, np.ones((43201, 5), 'float64')) # Sum over the eigenspectra to get the nonadaptive spectrum. new_spec = eigspec.sum(axis=1) / float(eigspec.shape[1]) new_spec[1:] *= 2.0 # Compare the output and the newly calculated spectrum. Normalize with # the maximum values to avoid scaling issues. np.testing.assert_almost_equal(spec[:10] / spec.max(), new_spec[:10] / new_spec.max())
def test_figure1(self): """ Recreate Figure 1 """ data = load_mtdata('v22_174_series.dat.gz') spec, freq, jackknife, _, _ = mtspec(data, 4930., 3.5, number_of_tapers=5, nfft=312, statistics=True) fig = plt.figure() ax1 = fig.add_subplot(2, 1, 1) ax1.plot(data, color='black') ax1.set_xlim(0, len(data)) ax2 = fig.add_subplot(2, 1, 2) ax2.set_yscale('log') ax2.plot(freq, spec, color='black') try: ax2.fill_between(freq, jackknife[:, 0], jackknife[:, 1], color='grey') except: ax2.plot(freq, jackknife[:, 0], '--', color='red') ax2.plot(freq, jackknife[:, 1], '--', color='red') ax2.set_xlim(freq[0], freq[-1]) outfile = os.path.join(self.outpath, 'fig1.pdf') fig.savefig(outfile) stat = os.stat(outfile) self.assertTrue(abs(stat.st_mtime - time.time()) < 3)
def test_figure2(self): """ Recreate Figure 2 """ data = load_mtdata('v22_174_series.dat.gz') spec, freq, jackknife, fstatistics, _ = mtspec(data, 4930., 3.5, number_of_tapers=5, nfft=312, statistics=True, rshape=0, fcrit=0.9) fig = plt.figure() ax1 = fig.add_subplot(2, 1, 1) ax1.plot(freq, fstatistics, color='black') ax1.set_xlim(freq[0], freq[-1]) ax2 = fig.add_subplot(2, 1, 2) ax2.set_yscale('log') ax2.plot(freq, spec, color='black') ax2.set_xlim(freq[0], freq[-1]) outfile = os.path.join(self.outpath, 'fig2.pdf') fig.savefig(outfile) stat = os.stat(outfile) self.assertTrue(abs(stat.st_mtime - time.time()) < 3)
def test_paddedMultitaperSpectrumWithErrors(self): """ Test for mtspec_pad with jackknife interval errors. The result is compared to the output of test_recreatePaperFigures.py in the same directory. This is assumed to be correct because they are identical to the figures in the paper on the machine that created these. """ data = load_mtdata('v22_174_series.dat.gz') # Calculate the spectra. spec, freq, jackknife, _, _ = mtspec(data, 4930., 3.5, nfft=312, number_of_tapers=5, statistics=True) # No NaNs are supposed to be in the output. self.assertEqual(np.isnan(spec).any(), False) self.assertEqual(np.isnan(spec).any(), False) self.assertEqual(np.isnan(jackknife).any(), False) # Load the good data. datafile = os.path.join(os.path.dirname(__file__), 'data', 'mtspec_pad_with_errors.npz') record = np.load(datafile) spec2 = record['spec'] jackknife2 = record['jackknife'] freq2 = np.arange(157) * 6.50127447e-07 # Compare. np.testing.assert_almost_equal(freq, freq2) np.testing.assert_almost_equal(spec / spec, spec2 / spec, 6) np.testing.assert_almost_equal(jackknife / jackknife, jackknife2 / jackknife, 6)
def test_figure3(self): """ Recreate Figure 2 """ data = load_mtdata('PASC.dat.gz') fig = plt.figure() ax1 = fig.add_subplot(3, 1, 1) ax1.plot(data, color='black') ax1.set_xlim(0, len(data)) spec, freq = mtspec(data, 1.0, 1.5, number_of_tapers=1) ax2 = fig.add_subplot(3, 2, 3) ax2.set_yscale('log') ax2.set_xscale('log') ax2.plot(freq, spec, color='black') ax2.set_xlim(freq[0], freq[-1]) spec, freq = mtspec(data, 1.0, 4.5, number_of_tapers=5) ax3 = fig.add_subplot(3, 2, 4) ax3.set_yscale('log') ax3.set_xscale('log') ax3.plot(freq, spec, color='black') ax3.set_xlim(freq[0], freq[-1]) spec, freq = sine_psd(data, 1.0) ax4 = fig.add_subplot(3, 2, 5) ax4.set_yscale('log') ax4.set_xscale('log') ax4.plot(freq, spec, color='black') ax4.set_xlim(freq[0], freq[-1]) spec, freq = mtspec(data, 1.0, 4.5, number_of_tapers=5, quadratic=True) ax5 = fig.add_subplot(3, 2, 6) ax5.set_yscale('log') ax5.set_xscale('log') ax5.plot(freq, spec, color='black') ax5.set_xlim(freq[0], freq[-1]) outfile = os.path.join(self.outpath, 'fig3.pdf') fig.savefig(outfile) stat = os.stat(outfile) self.assertTrue(abs(stat.st_mtime - time.time()) < 3)
def test_quadraticMultitaperIsDifferent(self): """ The quadratic and the normal multitaper spectra look quite similar. Check that they are different. """ data = load_mtdata('v22_174_series.dat.gz') # Calculate the spectra. spec, freq = mtspec(data, 1.0, 4.5, number_of_tapers=2) # No NaNs are supposed to be in the output. self.assertEqual(np.isnan(spec).any(), False) self.assertEqual(np.isnan(freq).any(), False) spec2, freq2 = mtspec(data, 1.0, 4.5, number_of_tapers=2, quadratic=True) # No NaNs are supposed to be in the output. self.assertEqual(np.isnan(spec2).any(), False) self.assertEqual(np.isnan(freq2).any(), False) # Test that these are not equal. self.assertRaises(AssertionError, np.testing.assert_almost_equal, spec, spec2)
def test_multitaperSpectrum(self): """ Test for mtspec. The result is compared to the output of test_recreatePaperFigures.py in the same directory. This is assumed to be correct because they are identical to the figures in the paper on the machine that created these. """ data = load_mtdata('PASC.dat.gz') # Calculate the spectra. spec, freq = mtspec(data, 1.0, 4.5, number_of_tapers=5) # No NaNs are supposed to be in the output. self.assertEqual(np.isnan(spec).any(), False) self.assertEqual(np.isnan(spec).any(), False) # Load the good data. datafile = os.path.join(os.path.dirname(__file__), 'data', 'multitaper.npz') spec2 = np.load(datafile)['spec'] freq2 = np.arange(43201) * 1.15740741e-05 # Compare, normalize for subdigit comparision np.testing.assert_almost_equal(freq, freq2) np.testing.assert_almost_equal(spec / spec, spec2 / spec, 5)
def test_sinePSD(self): """ Test for the sine_psd spectra. The result is compared to the output of test_recreatePaperFigures.py in the same directory. This is assumed to be correct because they are identical to the figures in the paper on the machine that created these. """ data = load_mtdata('PASC.dat.gz') # Calculate the spectra. spec, freq = sine_psd(data, 1.0) # No NaNs are supposed to be in the output. self.assertEqual(np.isnan(spec).any(), False) self.assertEqual(np.isnan(freq).any(), False) # Load the good data. datafile = os.path.join(os.path.dirname(__file__), 'data', 'sine_psd.npz') spec2 = np.load(datafile)['spec'] freq2 = np.arange(43201) * 1.15740741e-05 # Compare. np.testing.assert_almost_equal(freq, freq2) np.testing.assert_almost_equal(spec / spec, spec2 / spec, 2)
def test_multitaperSpectrumOptionalOutput(self): """ Test for mtspec. The result is compared to the output of test_recreatePaperFigures.py in the same directory. This is assumed to be correct because they are identical to the figures in the paper on the machine that created these. """ data = load_mtdata('PASC.dat.gz') # Calculate the spectra. spec, freq, eigspec, eigcoef, weights = \ mtspec(data, 1.0, 4.5, number_of_tapers=5, optional_output=True) # No NaNs are supposed to be in the output. self.assertEqual(np.isnan(spec).any(), False) self.assertEqual(np.isnan(spec).any(), False) self.assertEqual(np.isnan(eigspec).any(), False) self.assertEqual(np.isnan(eigcoef).any(), False) self.assertEqual(np.isnan(weights).any(), False) #XXX: Verify if this is correct, if so savez the data #import matplotlib.pyplot as plt #plt.plot(np.abs(eigcoef[:,0])) #plt.show() #import ipdb; ipdb.set_trace() #np.savez('data/multitaper.npz', spec=spec.astype('float32'), # eigspec=eigspec.astype('float32'), # eigcoef=eigcoef.astype('float32'), # weights=weights.astype('float32')) # Load the good data. datafile = os.path.join(os.path.dirname(__file__), 'data', 'multitaper.npz') record = np.load(datafile) spec2 = record['spec'] #eigspec2 = record['eigspec'] #eigcoef2 = record['eigcoef'] #weights2 = record['weights'] freq2 = np.arange(43201) * 1.15740741e-05 # Compare, normalize for subdigit comparision np.testing.assert_almost_equal(freq, freq2) np.testing.assert_almost_equal(spec / spec, spec2 / spec, 5)
def test_sinePSDStatistics(self): """ Test for the sine_psd spectra with optional output. The result is compared to the output of test_recreatePaperFigures.py in the same directory. This is assumed to be correct because they are identical to the figures in the paper on the machine that created these. """ data = load_mtdata('PASC.dat.gz') # Calculate the spectra. spec, freq, errors, tapers = sine_psd(data, 1.0, statistics=True) # No NaNs are supposed to be in the output. self.assertEqual(np.isnan(spec).any(), False) self.assertEqual(np.isnan(freq).any(), False) self.assertEqual(np.isnan(errors).any(), False) self.assertEqual(np.isnan(tapers).any(), False) #XXX: assert for errors and tapers is missing # Load the good data. datafile = os.path.join(os.path.dirname(__file__), 'data', 'sine_psd.npz') spec2 = np.load(datafile)['spec'] freq2 = np.arange(43201) * 1.15740741e-05 # Compare #XXX really bad precision for spec (linux 64bit) np.testing.assert_almost_equal(freq, freq2) np.testing.assert_almost_equal(spec / spec, spec2 / spec, 2)
import matplotlib as mpl mpl.rcParams['font.size'] = 9.0 import matplotlib.pyplot as plt from mtspec import mtspec from mtspec.util import load_mtdata data = load_mtdata('v22_174_series.dat.gz') spec, freq, jackknife, _, _ = mtspec(data, 4930., 3.5, number_of_tapers=5, nfft=312, statistics=True) fig = plt.figure() ax1 = fig.add_subplot(2, 1, 1) ax1.plot(data, color='black') ax1.set_xlim(0, len(data)) ax2 = fig.add_subplot(2, 1, 2) ax2.set_yscale('log') ax2.plot(freq, spec, color='black') ax2.plot(freq, jackknife[:, 0], '--', color = 'red') ax2.plot(freq, jackknife[:, 1], '--', color = 'red') ax2.set_xlim(freq[0], freq[-1])
import matplotlib as mpl mpl.rcParams['font.size'] = 9.0 import matplotlib.pyplot as plt from mtspec import mtspec, sine_psd from mtspec.util import load_mtdata data = load_mtdata('PASC.dat.gz') fig = plt.figure() ax1 = fig.add_subplot(3, 1, 1) ax1.plot(data, color='black') ax1.set_xlim(0, len(data)) spec, freq = mtspec(data, 1.0, 1.5, number_of_tapers=1) ax2 = fig.add_subplot(3, 2, 3) ax2.set_yscale('log') ax2.set_xscale('log') ax2.plot(freq, spec, color='black') ax2.set_xlim(freq[0], freq[-1]) spec, freq = mtspec(data, 1.0, 4.5, number_of_tapers=5) ax3 = fig.add_subplot(3, 2, 4) ax3.set_yscale('log') ax3.set_xscale('log') ax3.plot(freq, spec, color='black') ax3.set_xlim(freq[0], freq[-1]) spec, freq = sine_psd(data, 1.0)