# This is a comment
Wavelets in Jupyter Notebooks
wavelets
jupyter
A notebook to show off the power of fastpages and jupyter.
This is a notebook cobbled together from information and code from the following sources:
- pywavelets
- Ahmet Taspinar’s guide for using wavelet in ML
- Alexander Sauve’s introduction to wavelet for EDA
Why?
This notebook was created from the links above to test out how fastpages handle a combination of data and images within a notebook, when that notebook is converted for easy web viewing by jekyll.
The above author’s code seemed like a good dry run and test of the fastpage’s conversion from jupyter notebook to blog post.
import numpy as np
import pandas as pd
from scipy.fftpack import fft
import matplotlib.pyplot as plt
import pywt
def plot_wavelet(time, signal, scales,
# waveletname = 'cmor1.5-1.0',
= 'gaus5',
waveletname = plt.cm.seismic,
cmap = 'Wavelet Transform (Power Spectrum) of signal',
title = 'Period (years)',
ylabel = 'Time'):
xlabel
= time[1] - time[0]
dt = pywt.cwt(signal, scales, waveletname, dt)
[coefficients, frequencies] = (abs(coefficients)) ** 2
power = 1. / frequencies
period = [0.0625, 0.125, 0.25, 0.5, 1, 2, 4, 8]
levels = np.log2(levels)
contourlevels
= plt.subplots(figsize=(15, 10))
fig, ax = ax.contourf(time, np.log2(period), np.log2(power), contourlevels, extend='both',cmap=cmap)
im
=20)
ax.set_title(title, fontsize=18)
ax.set_ylabel(ylabel, fontsize=18)
ax.set_xlabel(xlabel, fontsize
= 2**np.arange(np.ceil(np.log2(period.min())), np.ceil(np.log2(period.max())))
yticks
ax.set_yticks(np.log2(yticks))
ax.set_yticklabels(yticks)
ax.invert_yaxis()= ax.get_ylim()
ylim 0], -1)
ax.set_ylim(ylim[
= fig.add_axes([0.95, 0.5, 0.03, 0.25])
cbar_ax =cbar_ax, orientation="vertical")
fig.colorbar(im, cax
plt.show()
def get_ave_values(xvalues, yvalues, n = 5):
= len(xvalues)
signal_length if signal_length % n == 0:
= 0
padding_length else:
= n - signal_length//n % n
padding_length
= np.array(xvalues)
xarr = np.array(yvalues)
yarr //n, n)
xarr.resize(signal_length//n, n)
yarr.resize(signal_length= xarr.reshape((-1,n))
xarr_reshaped = yarr.reshape((-1,n))
yarr_reshaped = xarr_reshaped[:,0]
x_ave = np.nanmean(yarr_reshaped, axis=1)
y_ave return x_ave, y_ave
def plot_signal_plus_average(time, signal, average_over = 5):
= plt.subplots(figsize=(15, 3))
fig, ax = get_ave_values(time, signal, average_over)
time_ave, signal_ave ='signal')
ax.plot(time, signal, label= 'time average (n={})'.format(5))
ax.plot(time_ave, signal_ave, label 0], time[-1]])
ax.set_xlim([time['Signal Amplitude', fontsize=18)
ax.set_ylabel('Signal + Time Average', fontsize=18)
ax.set_title('Time', fontsize=18)
ax.set_xlabel(
ax.legend()
plt.show()
def get_fft_values(y_values, T, N, f_s):
= np.linspace(0.0, 1.0/(2.0*T), N//2)
f_values = fft(y_values)
fft_values_ = 2.0/N * np.abs(fft_values_[0:N//2])
fft_values return f_values, fft_values
def plot_fft_plus_power(time, signal):
= time[1] - time[0]
dt = len(signal)
N = 1/dt
fs
= plt.subplots(figsize=(15, 3))
fig, ax = np.std(signal)**2
variance = get_fft_values(signal, dt, N, fs)
f_values, fft_values = variance * abs(fft_values) ** 2 # FFT power spectrum
fft_power 'r-', label='Fourier Transform')
ax.plot(f_values, fft_values, 'k--', linewidth=1, label='FFT Power Spectrum')
ax.plot(f_values, fft_power, 'Frequency [Hz / year]', fontsize=18)
ax.set_xlabel('Amplitude', fontsize=18)
ax.set_ylabel(
ax.legend()
plt.show()
= "http://paos.colorado.edu/research/wavelets/wave_idl/sst_nino3.dat"
dataset = pd.read_table(dataset)
df_nino = df_nino.shape[0]
N =1871
t0=0.25
dt= np.arange(0, N) * dt + t0
time = df_nino.values.squeeze()
signal
= np.arange(1, 128)
scales
plot_signal_plus_average(time, signal)
plot_fft_plus_power(time, signal) plot_wavelet(time, signal, scales)
# Create some fake data sets and show their fourier transforms (fft).
= 1
t_n = 100000
N = t_n / N
T = 1/T
f_s
= np.linspace(0, t_n, num=int(N))
xa = np.linspace(0, t_n/4, num=int(N/4))
xb
= [4, 30, 60, 90]
frequencies = np.sin(2*np.pi*frequencies[0]*xa), np.sin(2*np.pi*frequencies[0]*xb)
y1a, y1b = np.sin(2*np.pi*frequencies[1]*xa), np.sin(2*np.pi*frequencies[1]*xb)
y2a, y2b = np.sin(2*np.pi*frequencies[2]*xa), np.sin(2*np.pi*frequencies[2]*xb)
y3a, y3b = np.sin(2*np.pi*frequencies[3]*xa), np.sin(2*np.pi*frequencies[3]*xb)
y4a, y4b
= y1a + y2a + y3a + y4a
composite_signal1 = np.concatenate([y1b, y2b, y3b, y4b])
composite_signal2
= get_fft_values(composite_signal1, T, N, f_s)
f_values1, fft_values1 = get_fft_values(composite_signal2, T, N, f_s)
f_values2, fft_values2
= plt.subplots(nrows=2, ncols=2, figsize=(12,8))
fig, axarr 0,0].plot(xa, composite_signal1)
axarr[1,0].plot(xa, composite_signal2)
axarr[0,1].plot(f_values1, fft_values1)
axarr[1,1].plot(f_values2, fft_values2)
axarr[
0,1].set_xlim(0, 150)
axarr[1,1].set_xlim(0, 150)
axarr[
plt.tight_layout() plt.show()
# The El Nino Dataset
df_nino
-0.15 | |
---|---|
0 | -0.30 |
1 | -0.14 |
2 | -0.41 |
3 | -0.46 |
4 | -0.66 |
... | ... |
498 | -0.22 |
499 | 0.08 |
500 | -0.08 |
501 | -0.18 |
502 | -0.06 |
503 rows × 1 columns
df_nino.describe()
-0.15 | |
---|---|
count | 503.000000 |
mean | 0.000278 |
std | 0.735028 |
min | -1.850000 |
25% | -0.485000 |
50% | -0.070000 |
75% | 0.420000 |
max | 2.500000 |
df_nino.hist()
array([[<AxesSubplot:title={'center':'-0.15'}>]], dtype=object)