Commit f9952731 authored by Yifan Wang's avatar Yifan Wang
Browse files

complete the traning data part, also plot some figures

parent 18514b44
%% Cell type:code id: tags:
``` python
import sys
import time
import numpy as np
import pandas as pd
import utils.samplefiles
import matplotlib
import matplotlib.pyplot as plt
```
%% Cell type:code id: tags:
``` python
# Start the stopwatch
script_start_time = time.time()
```
%% Cell type:code id: tags:
``` python
hdf_file_path = './output/train.hdf'
plot_path = './plots/signalnoise_id0.png'
# which sample is used to shown
sample_id = 0
```
%% Cell type:code id: tags:
``` python
data = utils.samplefiles.SampleFile()
data.read_hdf(hdf_file_path)
```
%% Output
key: config_file values: default.json
%% Cell type:code id: tags:
``` python
# when split_injections_noise is set to be True, GWs contain signals and pure waves are splited
df, noise = data.as_dataframe(injection_parameters=True,
static_arguments=True,
command_line_arguments=False,
split_injections_noise=True)
```
%% Cell type:code id: tags:
``` python
print(df.columns)
```
%% Output
Index(['approximant', 'bandpass_lower', 'bandpass_upper', 'coa_phase', 'dec',
'delta_f', 'delta_t', 'distance', 'domain', 'event_time', 'f_lower',
'fd_length', 'h1_output_signal', 'h1_signal', 'h1_snr', 'h1_strain',
'inclination', 'injection_snr', 'l1_output_signal', 'l1_signal',
'l1_snr', 'l1_strain', 'mass1', 'mass2', 'noise_interval_width',
'original_sampling_rate', 'polarization', 'ra', 'sample_length',
'scale_factor', 'seconds_after_event', 'seconds_before_event', 'spin1z',
'spin2z', 'target_sampling_rate', 'td_length', 'tukey_alpha',
'waveform_length', 'whitening_max_filter_duration',
'whitening_segment_duration'],
dtype='object')
%% Cell type:code id: tags:
``` python
print(df.injection_snr[sample_id])
```
%% Output
7.340279606636548
%% Cell type:code id: tags:
``` python
print(df.mass1[sample_id])
print(df.mass2[sample_id])
print(df.spin1z[sample_id])
print(df.spin2z[sample_id])
print(df.scale_factor[sample_id])
```
%% Output
36.217808319315374
76.55000144869413
0.7305299539277823
0.5974611672286425
0.017164934231118905
%% Cell type:code id: tags:
``` python
print(df.coa_phase[sample_id])
print(df.inclination[sample_id])
print(df.ra[sample_id])
print(df.dec[sample_id])
print(df.polarization[sample_id])
```
%% Output
0.9801424781769557
0.48680334688549004
3.776917009710014
0.821770111935331
4.448951217224888
%% Cell type:code id: tags:
``` python
sample = df.loc[sample_id]
```
%% Cell type:code id: tags:
``` python
# Read out and construct some necessary values for plotting
seconds_before_event = float(sample['seconds_before_event'])
seconds_after_event = float(sample['seconds_after_event'])
target_sampling_rate = float(sample['target_sampling_rate'])
sample_length = float(sample['sample_length'])
print(seconds_before_event)
print(target_sampling_rate)
print(sample_length)
```
%% Output
5.5
2048.0
8.0
%% Cell type:code id: tags:
``` python
# Create a grid on which the sample can be plotted so that the
# event_time is at position 0
grid = np.linspace(0 - seconds_before_event, 0 + seconds_after_event, int(target_sampling_rate * sample_length))
# for time from -0.15s to 0.05s
#grid = np.linspace(0 - seconds_before_event, 0 + seconds_after_event, int(target_sampling_rate * sample_length)+1)
```
%% Cell type:code id: tags:
``` python
det_name = 'H1'
det_string = 'h1_strain'
```
%% Cell type:code id: tags:
``` python
maximum = np.max(sample[det_string])
print(maximum)
```
%% Output
145.16235
%% Cell type:code id: tags:
``` python
maximum = max(np.max(sample['h1_signal']), np.max(sample['l1_signal']))
print(maximum)
```
%% Output
5.173865814288047e-21
%% Cell type:code id: tags:
``` python
maximum = max(np.max(sample['h1_output_signal']), np.max(sample['l1_output_signal']))
print(maximum)
```
%% Output
23.74179629063074
%% Cell type:code id: tags:
``` python
plt.plot(grid, sample[det_string], color='C0',label = 'strain')
plt.plot(grid, sample['h1_output_signal'], color='C1',label = 'signal')
#plt.xlim(-1.5, 0.5)
#plt.ylim(-150, 150)
plt.xticks(fontsize=14)
plt.yticks(fontsize=14)
plt.ylabel('Whitened Strain and Signal ({})'
.format(det_name), fontsize=15)
plt.xlabel('Time (s)', fontsize=15)
plt.legend(fontsize=15)
# Adjust the size and spacing of the subplots
plt.gcf().set_size_inches(9, 6, forward=True)
plt.tight_layout(rect=[0, 0, 1, 0.9])
plt.subplots_adjust(wspace=0, hspace=0)
# plt.show()
plt.savefig(plot_path)
```
%% Output
%% Cell type:code id: tags:
``` python
```
...@@ -24,24 +24,8 @@ from utils.waveforms import WaveformParameterGenerator ...@@ -24,24 +24,8 @@ from utils.waveforms import WaveformParameterGenerator
if __name__ == '__main__': if __name__ == '__main__':
# -------------------------------------------------------------------------
# Preliminaries
# -------------------------------------------------------------------------
# Disable output buffering ('flush' option is not available for Python 2)
#sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
# Start the stopwatch
script_start = time.time() script_start = time.time()
print('')
print('GENERATE A GW DATA SAMPLE FILE')
print('')
# -------------------------------------------------------------------------
# Parse the command line arguments
# -------------------------------------------------------------------------
# Set up the parser and add arguments # Set up the parser and add arguments
parser = argparse.ArgumentParser(description='Generate a GW data sample.') parser = argparse.ArgumentParser(description='Generate a GW data sample.')
parser.add_argument('--config-file', parser.add_argument('--config-file',
...@@ -50,9 +34,7 @@ if __name__ == '__main__': ...@@ -50,9 +34,7 @@ if __name__ == '__main__':
default='default.json') default='default.json')
# Parse the arguments that were passed when calling this script # Parse the arguments that were passed when calling this script
print('Parsing command line arguments...', end=' ')
command_line_arguments = vars(parser.parse_args()) command_line_arguments = vars(parser.parse_args())
print('Done!')
# ------------------------------------------------------------------------- # -------------------------------------------------------------------------