<jupyter_start><jupyter_text>Read in the strains<jupyter_code>with h5py.File('../data/strain/H1_2017_4096.hdf5', 'r') as file:
    strain_H1 = np.array(file['strain/Strain'])
    
with h5py.File('../data/strain/L1_2017_4096.hdf5', 'r') as file:
    strain_L1 = np.array(file['strain/Strain'])<jupyter_output><empty_output><jupyter_text>Whiten the strains and downsample<jupyter_code>psd_H1 = get_psd(strain_H1)
psd_L1 = get_psd(strain_L1)
white_H1 = apply_psd(strain_H1, psd_H1)
white_L1 = apply_psd(strain_L1, psd_L1)
# start = int((4096*(2048.6-30))/2)
# end = int((4096*(2048.6+30))/2)
start = int((4096*(2048.6-30))/2)
end = int((4096*(2048.6+30))/2)

final_H1 = white_H1[::2][start:end]
final_L1 = white_L1[::2][start:end]<jupyter_output><empty_output><jupyter_text>Build a timeseries object, swap axes, make torch Variable<jupyter_code>timeseries = np.array([np.dstack((final_H1, final_L1))])
timeseries = np.swapaxes(timeseries, 1, 3)
timeseries = np.swapaxes(timeseries, 2, 3)
timeseries = np.squeeze(timeseries, axis=2)

x = Variable(torch.from_numpy(timeseries), volatile=True).float()
model = TimeSeriesGatedFCN()

# Load the model weights: A little cumbersome, because we don't have CUDA
# and GPU parallelization like during training time
weights_file = './weights/timeseries_weights_0700_1200_8k.net'
state_dict = torch.load(weights_file, map_location=lambda storage, loc: storage)
new_state_dict = OrderedDict()
for k, v in state_dict.items():
    name = k[7:] # remove `module.`
    new_state_dict[name] = v

model.load_state_dict(new_state_dict)
# %%timeit
y_pred = model.forward(x)
y_pred = y_pred.view((y_pred.size()[0], y_pred.size()[-1]))
y_pred = y_pred.data.numpy()<jupyter_output><empty_output><jupyter_text>Make plots<jupyter_code>timeseries_H1 = timeseries[0, 0, :]
timeseries_L1 = timeseries[0, 1, :]

grid = np.linspace(-30, 30, 60*2048)
fig, axes = plt.subplots(nrows=3, ncols=1, sharex='col',
                         gridspec_kw={'height_ratios': [5, 5, 2]}, 
                         figsize=(16, 8))

axes[0].plot(grid, timeseries_H1)
axes[0].axvline(x=0, color='Gray', ls='--')

axes[1].plot(grid, timeseries_L1)
axes[1].axvline(x=0, color='Gray', ls='--')

axes[2].plot(grid, y_pred.reshape(-1), color='Orange')

smooth_pred = np.fromiter(map(lambda x: x>0.5, 
                              np.convolve(y_pred.reshape(-1), np.ones(100), mode='same')/100), 
                          dtype=np.float)
axes[2].plot(grid, smooth_pred, color='Red')

axes[2].set_ylim(-0.2, 1.2)
fig.subplots_adjust(hspace=0)
plt.setp([a.get_xticklabels() for a in fig.axes[:-1]], visible=False)

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17063042657606645, 'fraction_numerical': 0.05964514911287278, 'mean_word_length': 5.091954022988506, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '197831', 'license': 'Apache-2.0', 'n_tokens_mistral': 1179, 'n_tokens_neox': 1075, 'n_words': 172, 'path': 'quantum gravity/convwave/src/train/test_network_on_full_strain.ipynb', 'repo_name': 'HiteshDhola/Artificial-Intelligence-Deep-Learning-Machine-Learning-Tutorials', 'size': '2649'}