def load(index, evt_name="GW150914"): ''' 从 .json 目录中读取引力波事件的信息,从里面记载的文件名中 进一步读取拉伸 (strain) 数据。 - index: .json 目录的文件名 - evt_name: 想要读取的引力波事例名 - 返回:H 地信号,L 地信号,时间,事例信息 ''' events = json.load(open(index, "r")) event = events[evt_name] import readligo as rl # FIXME!! data 路径写死了,应当可变 fn_H1 = "data/" + event['fn_H1'] strain_H1, time, chan_dict_H1 = rl.loaddata(fn_H1) fn_L1 = "data/" + event['fn_L1'] strain_L1, time, chan_dict_L1 = rl.loaddata(fn_L1) event['tevent'] = event['tevent'] - time[0] time = time - time[0] return strain_H1, strain_L1, time, event
def LoadLIGO(data_address, GPS): H1_list = [ file for file in os.listdir(data_address) if 'H1' in file if str(GPS) in file ] L1_list = [ file for file in os.listdir(data_address) if 'L1' in file if str(GPS) in file ] H1_list.sort() L1_list.sort() assert len(H1_list) == len(L1_list) == 1 # Same length for H1/L1 file # Same GPS for sorted H1/L1 files list: assert len([ i for i, H1 in enumerate(H1_list) if H1.split('-')[2] != L1_list[i].split('-')[2] ]) == 0 # Loading data strain_H1, time_H1, chan_dict_H1 = rl.loaddata( os.path.join(data_address, H1_list[0]), "H1") strain_L1, time_L1, chan_dict_L1 = rl.loaddata( os.path.join(data_address, L1_list[0]), "L1") assert np.allclose(time_L1, time_H1, atol=0, rtol=0) # Check GPS time for H1/L1 GPStime = time_H1 logger.debug('Loaded strain_H1/GPStime/chan_dict_H1 shape: {}/{}/{}', strain_H1.shape, GPStime.shape, len(chan_dict_H1)) logger.debug('Loaded strain_L1/GPStime/chan_dict_L1 shape: {}/{}/{}', strain_L1.shape, GPStime.shape, len(chan_dict_L1)) # Until here may costs 2.1 s return GPStime, strain_H1, chan_dict_H1, strain_L1, chan_dict_L1
def fileCheck(fileStr): if os.path.exists('psd/' + fileStr + 'pxx') and \ os.path.exists('psd/' + fileStr + 'freqs') and \ os.path.exists('data/' + fileStr + 'Noise.h5'): return strain, time, chan_dict = rl.loaddata('raw/' + fileStr[:-1] + 'prev.hdf5', fileStr[-2] + '1') strain = decimate(strain, 2) fs = 8192 pxx, freqs = mlab.psd(strain, Fs=fs, NFFT=fs) with open('psd/' + fileStr + 'freqs', 'wb') as fn: pickle.dump(freqs, fn) with open('psd/' + fileStr + 'pxx', 'wb') as fn: pickle.dump(pxx, fn) interp_psd = interp1d(freqs, pxx) whitened = whiten(strain, interp_psd) whitened = whitened[100000:-100000] with h5py.File('data/' + fileStr + 'Noise.h5', 'w') as fn: fn['noise'] = whitened
def read_strainset(datadir="/media/kawahara/finbacko1", rel=1, ver="O", placelist=["L", "H"], dataid=1126256640): strain = [] time = [] chan_dict = [] place = [] for place_ in placelist: filename = getfilename(datadir, rel, "O", place_, dataid) print(filename) try: strain_, time_, chan_dict_ = rl.loaddata(filename, place_ + str(rel)) strain.append(strain_) time.append(time_) chan_dict.append(chan_dict_) place.append(place_) except: print("Could not read " + filename) return place, strain, time, chan_dict
fband = event['fband'] # frequency band for bandpassing signal print("Reading in parameters for event " + event["name"]) print(event) # ## Read in the data # We will make use of the data, and waveform template, defined above. # In[5]: #---------------------------------------------------------------- # Load LIGO data from a single file. # FIRST, define the filenames fn_H1 and fn_L1, above. #---------------------------------------------------------------- try: # read in data from H1 and L1, if available: strain_H1, time_H1, chan_dict_H1 = rl.loaddata(fn_H1, 'H1') strain_L1, time_L1, chan_dict_L1 = rl.loaddata(fn_L1, 'L1') except: print("Cannot find data files!") print("You can download them from https://losc.ligo.org/s/events/" + eventname) print("Quitting.") quit() # ## Data Gaps # **NOTE** that in general, LIGO strain time series data has gaps (filled with NaNs) when the detectors are not taking valid ("science quality") data. Analyzing these data requires the user to # <a href='https://losc.ligo.org/segments/'>loop over "segments"</a> of valid data stretches. # # **In this tutorial, for simplicity, we assume there are no data gaps - this will not work for all times!** See the # <a href='https://losc.ligo.org/segments/'>notes on segments</a> for details. #
def testReal(trained_model, event, crop, step, output, length, twoChan=False): # strain data preprocessing if length is not 4096 and length is not 32: length = 4096 num_predict = 0 if not twoChan: strain, time, chan_dict = rl.loaddata('raw/' + event + '-' + str(length) + '.hdf5', event[-1] + '1') strain = decimate(strain, 2) fs = 8192 pxx, freqs = mlab.psd(strain, Fs=fs, NFFT=fs) interp_psd = interp1d(freqs, pxx) whitened = whiten(strain, interp_psd) whitened = whitened[crop:-crop] whitened /= np.std(whitened) num_predict = len(whitened) # prepare prediction dataset x_predict = [] for start in range(0, len(whitened)-8192, step): waveform = whitened[start:start+8192] waveform /= np.std(waveform) x_predict.append(waveform) x_predict = np.asarray(x_predict) x_predict = np.expand_dims(x_predict, axis=-1) else: strain_H, time_H, chan_dict_H = rl.loaddata('raw/' + event + 'H-' + str(length) + '.hdf5', 'H1') strain_L, time_L, chan_dict_L = rl.loaddata('raw/' + event + 'L-' + str(length) + '.hdf5', 'L1') strain_H = decimate(strain_H, 2) strain_L = decimate(strain_L, 2) fs = 8192 pxx_H, freqs_H = mlab.psd(strain_H, Fs=fs, NFFT=fs) interp_psd_H = interp1d(freqs_H, pxx_H) pxx_L, freqs_L = mlab.psd(strain_L, Fs=fs, NFFT=fs) interp_psd_L = interp1d(freqs_L, pxx_L) whitened_H = whiten(strain_H, interp_psd_H) whitened_H = whitened_H[crop:-crop] whitened_H /= np.std(whitened_H) whitened_L = whiten(strain_L, interp_psd_L) whitened_L = whitened_L[crop:-crop] whitened_L /= np.std(whitened_L) # prepare prediction dataset x_predict = [] if len(whitened_H) < len(whitened_L): whitened_L = whitened_L[:len(whitened_H)] elif len(whitened_H) > len(whitened_L): whitened_H = whitened_H[:len(whitened_L)] num_predict = len(whitened_H) for start in range(0, len(whitened_H) - 8192, step): waveform_H = whitened_H[start:start + 8192] waveform_H /= np.std(waveform_H) waveform_L = whitened_L[start:start + 8192] waveform_L /= np.std(waveform_L) waveform = np.stack([waveform_H, waveform_L], axis=-1) x_predict.append(waveform) x_predict = np.asarray(x_predict) # get prediction y_temp = trained_model.predict(x_predict) y_predict = np.zeros(num_predict) for start in range(0, num_predict-8192, step): y_predict[start:start+8192] += y_temp[start//step] * (step/8192.) y_predict = np.convolve(y_predict, np.ones(1024)) / 1024 plt.figure(figsize=(30, 30)) counter = 1 total_peaks = {} for threshold in [0.5, 0.6, 0.7, 0.8]: peaks, _ = find_peaks(y_predict, height=threshold, distance=8192) plt.subplot(2, 2, counter) plt.plot(y_predict) plt.plot(peaks, y_predict[peaks], 'x') plt.plot(np.repeat(threshold, len(y_predict)), "--", color='gray') plt.axvline(x=len(y_predict)/2, color='r') plt.xlabel('time step') plt.ylabel('probability') plt.title('threshold-' + str(threshold)) counter += 1 total_peaks[threshold] = peaks plt.savefig(output + '-testReal.png') total_peaks['middle'] = len(y_predict)/2 return total_peaks
plt.tick_params(axis='y', labelsize=14) plt.xlabel('Frequency (Hz)', fontsize=18) plt.ylabel(r'PSD (strain / $\sqrt{Hz}$)', fontsize=18) plt.title('PSD for L1 data starting at GPS ' + str(time_seg[0])) plt.xlim([1, np.max(freqs)]) plt.ylim([1e-26, 1e-16]) plt.savefig('original_PSD.pdf') return pxx, freqs # Read in data fname = 'L-L1_LOSC_4_V1-842657792-4096.hdf5' print '=== Start reading data ===' print '* File name: ', fname strain, time, channel_dictionary = rl.loaddata(fname) data_len = len(time) time_spacing = time[1] - time[0] fs = int(1/time_spacing) print '* Number of samples: ', data_len, ' = 2^', np.log2(data_len) print '* Start GPS Time: ', time[0] print '* End GPS Time: ', time[data_len - 1] print '* Sampling Time: ', time_spacing, 'sec => ', fs, ' Hz' print '' # blank line # Select a data segment without errors ("good data") print '=== DQ good segments ===' seglist = rl.dq_channel_to_seglist(channel_dictionary['DEFAULT'], fs) num_seg = len(seglist)
import numpy as np import matplotlib.pyplot as plt import readligo as rl #---------------------------------------------------------------- # Load all GWOSC data from a single file #---------------------------------------------------------------- strain, time, chan_dict = rl.loaddata( 'H-H1_LOSC_4_V1-815411200-4096.hdf5', 'H1') slice_list = rl.dq_channel_to_seglist(chan_dict['DATA']) for slice in slice_list: time_seg = time[slice] strain_seg = strain[slice] # -- Do stuff with strain segment here
import numpy as np import h5py import matplotlib.pyplot as plt import matplotlib.mlab as mlab import readligo as rl fileName = 'V-V1_GWOSC_O2_4KHZ_R1-1185615872-4096.hdf5' strain, time, channel_dict = rl.loaddata(fileName) ts = time[1] - time[0] #-- Time between samples fs = int(1.0 / ts) #-- Sampling frequency segList = rl.dq_channel_to_seglist(channel_dict['DEFAULT'], fs) length = 16 # seconds strain_seg = strain[segList[0]][0:(length * fs)] time_seg = time[segList[0]][0:(length * fs)] fig = plt.figure(figsize=(10, 10)) fig.subplots_adjust(wspace=0.3, hspace=0.3) plt.subplot(321) plt.plot(time_seg - time_seg[0], strain_seg) plt.xlabel('Time since GPS ' + str(time_seg[0])) plt.ylabel('Strain') window = np.blackman(strain_seg.size) windowed_strain = strain_seg * window freq_domain = np.fft.rfft(windowed_strain) / fs freq = np.fft.rfftfreq(len(windowed_strain)) * fs plt.subplot(322) plt.loglog(freq, abs(freq_domain)) plt.axis([10, fs / 2.0, 1e-24, 1e-18]) plt.grid('on') plt.xlabel('Freq (Hz)') plt.ylabel('Strain / Hz')
fn_L1 = event['fn_L1'] # File name for L1 data fs = event['fs'] # Set sampling rate print("Reading in parameters for event " + event["name"]) print(event) # ## Read in the data # We will make use of the data, and waveform template, defined above. # In[5]: #---------------------------------------------------------------- # Load LIGO data from a single file. # FIRST, define the filenames fn_H1 and fn_L1, above. #---------------------------------------------------------------- try: # read in data from H1 and L1, if available: strain_H1, time_H1, chan_dict_H1 = rl.loaddata(file_path + fn_H1, 'H1') strain_L1, time_L1, chan_dict_L1 = rl.loaddata(file_path + fn_L1, 'L1') except: print("Cannot find data files!") print("You can download them from https://losc.ligo.org/s/events/" + eventname) print("Quitting.") quit() # ## Data Gaps # ## First look at the data from H1 and L1 # In[6]: # both H1 and L1 will have the same time vector, so: time = time_H1 # the time sample interval (uniformly sampled!)
from scipy.interpolate import interp1d from scipy.signal import butter, filtfilt, iirdesign, zpk2tf, freqz # matplotlibはグラフを描くライブラリ import matplotlib.pyplot as plt import matplotlib.mlab as mlab # h5pyは多次元データのフォーマットで、h5pyはh5を読むためのライブラリ import h5py # LIGO-specific readligo.py import readligo as rl # 観測データを読み込む fn_H1 = 'H-H1_LOSC_4_V1-1126259446-32.hdf5' strain_H1, time_H1, chan_dict_H1 = rl.loaddata(fn_H1, 'H1') # sampling rate: fs = 4096 # both H1 and L1 will have the same time vector, so: time = time_H1 # the time sample interval (uniformly sampled!) dt = time[1] - time[0] # コンピューターシミュレーションによる、ブラックホールが衝突したときに発生する重力波の波形 NRtime, NR_H1 = np.genfromtxt('GW150914_4_NR_waveform.txt').transpose() # データの内覧 print(' time_H1: len, min, mean, max = ', len(time_H1), time_H1.min(), time_H1.mean(), time_H1.max())
import numpy as np from scipy import signal from scipy.interpolate import interp1d from scipy.signal import butter, filtfilt, iirdesign, zpk2tf, freqz import matplotlib.pyplot as plt import matplotlib.mlab as mlab import h5py import readligo as rl fn_H1 = 'H-H1_LOSC_4_V1-1126259446-32.hdf5' strain_H1, time_H1, chan_dict_H1 = rl.loaddata(fn_H1, 'H1') fn_L1 = 'L-L1_LOSC_4_V1-1126259446-32.hdf5' strain_L1, time_L1, chan_dict_L1 = rl.loaddata(fn_L1, 'L1') fs = 4096 time = time_H1 dt = time[1] - time[0] NRtime, NR_H1 = np.genfromtxt('GW150914_4_NR_waveform.txt').transpose() print(' time_H1: len, min, mean, max = ', len(time_H1), time_H1.min(), time_H1.mean(), time_H1.max()) print('strain_H1: len, min, mean, max = ', len(strain_H1), strain_H1.min(), strain_H1.mean(), strain_H1.max()) print('strain_L1: len, min, mean, max = ', len(strain_L1), strain_L1.min(), strain_L1.mean(), strain_L1.max()) # plot +- 5 seconds around the event tevent = 1126259462.422 # mon sept 15 09:50:45 GMT 2015 deltat = 5. # seconds around the event # index into the strain time series for this time interval indxt = np.where((time_H1 >= tevent-deltat) & (time_H1 < tevent+deltat))
plottype = 'png' name = '1185619968' fn_H1 = 'DataFiles\H1_1185619968.hdf5' fn_L1 = 'DataFiles\L1_1185619968.hdf5' fs = 4096 tevent = 1185619968 m1 = 41.743 m2 = 29.237 a1 = 0.355 a2 = -0.769 approx = 'lalsim.SEOBNRv2' fband = [43.0, 300.0] f_min = 10.0 strain1, time1, channel_dict1 = rl.loaddata(fn_H1, 'H1') ts1 = time1[1] - time1[0] #-- Time between samples fs1 = int(1.0 / ts1) #-- Sampling frequency strain2, time2, channel_dict2 = rl.loaddata(fn_L1, 'L1') ts2 = time2[1] - time2[0] #-- Time between samples fs2 = int(1.0 / ts2) #-- Sampling frequency segList1 = rl.dq_channel_to_seglist(channel_dict1['DEFAULT'], fs1) length1 = 16 # seconds strain_seg1 = strain1[segList1[0]][0:(length1 * fs1)] time_seg1 = time1[segList1[0]][0:(length1 * fs1)] segList2 = rl.dq_channel_to_seglist(channel_dict2['DEFAULT'], fs2) length2 = 16 # seconds strain_seg2 = strain2[segList2[0]][0:(length2 * fs2)]
#---------------------- # Import needed modules #---------------------- import pandas from pandas import rolling_std import numpy as np import h5py import matplotlib.pyplot as plt import matplotlib.mlab as mlab import readligo as rl #--------------------- # Read in strain data #--------------------- fileName1 = 'LIGO\H1_1185619968.hdf5' strain1, time1, channel_dict1 = rl.loaddata(fileName1, 'H1') ts1 = time1[1] - time1[0] #-- Time between samples fs1 = int(1.0 / ts1) #-- Sampling frequency fileName2 = 'LIGO\L1_1185619968.hdf5' strain2, time2, channel_dict2 = rl.loaddata(fileName2, 'H1') ts2 = time2[1] - time2[0] #-- Time between samples fs2 = int(1.0 / ts2) #-- Sampling frequency #--------------------------------------------------------- # Find a good segment, get first 16 seconds of data #--------------------------------------------------------- segList1 = rl.dq_channel_to_seglist(channel_dict1['DEFAULT'], fs1) length1 = 16 # seconds strain_seg1 = strain1[segList1[0]][0:(length1 * fs1)] time_seg1 = time1[segList1[0]][0:(length1 * fs1)]
notext=True) plt.close() else: print("Location information already present. Remove first to replot") t0 = times[event_name] # Get the current time from GPS time to UTC # in 1980: 19 leapseconds, in 2015: 36 leapseconds, 2017: 37 if event_name[-6:-4] == "15": utc = datetime(1980, 1, 6) + timedelta(seconds=t0 - (36 - 19)) else: utc = datetime(1980, 1, 6) + timedelta(seconds=t0 - (37 - 19)) detector = "H1" data_file = "data/" + event_name + ".hdf5" strain, time, chan_dict_H1 = rl.loaddata(data_file, 'H1') dt = time[1] - time[0] fs = int(np.round(1 / dt)) rel_time = time - t0 #-- How much data to use for the ASD? deltat = 15 # Number of seconds on each side of data N_samp = deltat * fs # -- Center the ASD segment on the requested time indx = np.where(np.abs(rel_time) < dt)[0][0] strain_seg = strain[indx - N_samp:indx + N_samp] time_seg = rel_time[indx - N_samp:indx + N_samp] # number of sample for the fast fourier transform: NFFT = 1 * fs
coefs.append((bn, an)) return coefs # and then define the filter function: def filter_data(data_in,coefs): data = data_in.copy() for coef in coefs: b,a = coef # filtfilt applies a linear filter twice, once forward and once backwards. # The combined filter has linear phase. data = filtfilt(b, a, data) return data fn_16 = 'H-H1_LOSC_16_V1-1126259446-32.hdf5' strain_16, time_16, chan_dict = rl.loaddata(fn_16, 'H1') # sampling rate: fs = 16384 time = time_16 # plot +- 5 seconds around the event: tevent = 1126259462.422 # Mon Sep 14 09:50:45 GMT 2015 # get filter coefficients coefs = get_filter_coefs(fs) # filter the data: strain_16_filt = filter_data(strain_16, coefs) h1_16_filt_array = [list(t) for t in zip(time-tevent, strain_16_filt)] # H1 csv
# @BEGIN LOAD_DATA @desc Load hdf5 data. # @in fn_d @as FN_Detector @uri file:{Detector}_LOSC_4_V1-1126259446-32.hdf5 # @in fn_sr @as FN_Sampling_rate @uri file:H-H1_LOSC_{downsampling}_V1-1126259446-32.hdf5 # @out strain_H1 @as strain_H1 # @out strain_L1 @as strain_L1 # @out strain_16 @as strain_16 # @out strain_4 @as strain_4 #---------------------------------------------------------------- # Load LIGO data from a single file #---------------------------------------------------------------- # First from H1 fn_H1 = 'H-H1_LOSC_4_V1-1126259446-32.hdf5' strain_H1, time_H1, chan_dict_H1 = rl.loaddata(fn_H1, 'H1') # and then from L1 fn_L1 = 'L-L1_LOSC_4_V1-1126259446-32.hdf5' strain_L1, time_L1, chan_dict_L1 = rl.loaddata(fn_L1, 'L1') # sampling rate: fs = 4096 # both H1 and L1 will have the same time vector, so: time = time_H1 # the time sample interval (uniformly sampled!) dt = time[1] - time[0] # @END LOAD_DATA # ## Adding a numerical relativity template
# fs = 4096 # # strain, time, dq = rl.loaddata(fn_H1, 'H1') # L1 para el otro caso # # #N_fft = len(time) # # N_fft = fs # # f, M = getPeriodogram(strain, fs, N_fft) # # aux = [(x/10000)**(1/2) for x in M] # # # # usaron mas datos y tambien usaron whitening # # plt.loglog(f, aux, 'b', label='H1_periodogram') # # strain_H1, time_H1, chan_dict_H1 = rl.loaddata(fn_H1, 'H1') # # NFFT = 1 * fs # # fmin = 10 # # fmax = 2000 #--------------------- # Read in strain data #--------------------- strain, time, channel_dict = rl.loaddata(fn_H1, 'H1') ts = time[1] - time[0] #-- Time between samples fs = int(1.0 / ts) #-- Sampling frequency #--------------------------------------------------------- # Find a good segment, get first 16 seconds of data #--------------------------------------------------------- segList = rl.dq_channel_to_seglist(channel_dict['DEFAULT'], fs) length = 16 # seconds strain_seg = strain[segList[0]][0:(length*fs)] time_seg = time[segList[0]][0:(length*fs)] f, M = getPeriodogram(strain_seg, fs, len(strain_seg)) # # usaron mas datos y tambien usaron whitening plt.loglog(f, np.sqrt(M), 'b', label='H1_periodogram')