def test_injection_presence(self): """Verify presence of signals at expected times""" injections = InjectionSet(self.inj_file.name) for det in self.detectors: for inj in self.injections: ts = TimeSeries(numpy.zeros(int(10 * self.sample_rate)), delta_t=1 / self.sample_rate, epoch=lal.LIGOTimeGPS(inj.end_time - 5), dtype=numpy.float64) injections.apply(ts, det.name) max_amp, max_loc = ts.abs_max_loc() # FIXME could test amplitude and time more precisely self.assertTrue(max_amp > 0 and max_amp < 1e-10) time_error = ts.sample_times.numpy()[max_loc] - inj.end_time self.assertTrue(abs(time_error) < 2 * self.earth_time)
def test_injection_presence(self): """Verify presence of signals at expected times""" injections = InjectionSet(self.inj_file.name) for det in self.detectors: for inj in self.injections: ts = TimeSeries(numpy.zeros(10 * self.sample_rate), delta_t=1/self.sample_rate, epoch=lal.LIGOTimeGPS(inj.end_time - 5), dtype=numpy.float64) injections.apply(ts, det.name) max_amp, max_loc = ts.abs_max_loc() # FIXME could test amplitude and time more precisely self.assertTrue(max_amp > 0 and max_amp < 1e-10) time_error = ts.sample_times.numpy()[max_loc] - inj.end_time self.assertTrue(abs(time_error) < 1.5 * self.earth_time)
def test_injection_absence(self): """Verify absence of signals outside known injection times""" clear_times = [ self.injections[0].end_time - 86400, self.injections[-1].end_time + 86400 ] injections = InjectionSet(self.inj_file.name) for det in self.detectors: for epoch in clear_times: ts = TimeSeries(numpy.zeros(int(10 * self.sample_rate)), delta_t=1 / self.sample_rate, epoch=lal.LIGOTimeGPS(epoch), dtype=numpy.float64) injections.apply(ts, det.name) max_amp, max_loc = ts.abs_max_loc() self.assertEqual(max_amp, 0)
def test_injection_absence(self): """Verify absence of signals outside known injection times""" clear_times = [ self.injections[0].end_time - 86400, self.injections[-1].end_time + 86400 ] injections = InjectionSet(self.inj_file.name) for det in self.detectors: for epoch in clear_times: ts = TimeSeries(numpy.zeros(10 * self.sample_rate), delta_t=1/self.sample_rate, epoch=lal.LIGOTimeGPS(epoch), dtype=numpy.float64) injections.apply(ts, det.name) max_amp, max_loc = ts.abs_max_loc() self.assertEqual(max_amp, 0)
def from_cli(opt, dyn_range_fac=1, precision='single'): """Parses the CLI options related to strain data reading and conditioning. Parameters ---------- opt : object Result of parsing the CLI with OptionParser, or any object with the required attributes (gps-start-time, gps-end-time, strain-high-pass, pad-data, sample-rate, (frame-cache or frame-files), channel-name, fake-strain, fake-strain-seed, gating_file). dyn_range_fac: {float, 1}, optional A large constant to reduce the dynamic range of the strain. Returns ------- strain : TimeSeries The time series containing the conditioned strain data. """ gating_info = {} if opt.frame_cache or opt.frame_files or opt.frame_type: if opt.frame_cache: frame_source = opt.frame_cache if opt.frame_files: frame_source = opt.frame_files logging.info("Reading Frames") if opt.frame_type: strain = query_and_read_frame(opt.frame_type, opt.channel_name, start_time=opt.gps_start_time-opt.pad_data, end_time=opt.gps_end_time+opt.pad_data) else: strain = read_frame(frame_source, opt.channel_name, start_time=opt.gps_start_time-opt.pad_data, end_time=opt.gps_end_time+opt.pad_data) if opt.zpk_z and opt.zpk_p and opt.zpk_k: logging.info("Highpass Filtering") strain = highpass(strain, frequency=opt.strain_high_pass) logging.info("Applying zpk filter") z = numpy.array(opt.zpk_z) p = numpy.array(opt.zpk_p) k = float(opt.zpk_k) strain = filter_zpk(strain.astype(numpy.float64), z, p, k) if opt.normalize_strain: logging.info("Dividing strain by constant") l = opt.normalize_strain strain = strain / l if opt.injection_file: logging.info("Applying injections") injections = InjectionSet(opt.injection_file) injections.apply(strain, opt.channel_name[0:2], distance_scale=opt.injection_scale_factor) if opt.sgburst_injection_file: logging.info("Applying sine-Gaussian burst injections") injections = SGBurstInjectionSet(opt.sgburst_injection_file) injections.apply(strain, opt.channel_name[0:2], distance_scale=opt.injection_scale_factor) logging.info("Highpass Filtering") strain = highpass(strain, frequency=opt.strain_high_pass) if precision == 'single': logging.info("Converting to float32") strain = (strain * dyn_range_fac).astype(pycbc.types.float32) if opt.gating_file is not None: logging.info("Gating glitches") gate_params = numpy.loadtxt(opt.gating_file) if len(gate_params.shape) == 1: gate_params = [gate_params] strain = gate_data(strain, gate_params) gating_info['file'] = \ [gp for gp in gate_params \ if (gp[0] + gp[1] + gp[2] >= strain.start_time) \ and (gp[0] - gp[1] - gp[2] <= strain.end_time)] if opt.autogating_threshold is not None: # the + 0 is for making a copy glitch_times = detect_loud_glitches( strain + 0., threshold=opt.autogating_threshold, cluster_window=opt.autogating_cluster, low_freq_cutoff=opt.strain_high_pass, high_freq_cutoff=opt.sample_rate/2, corrupt_time=opt.pad_data+opt.autogating_pad) gate_params = [[gt, opt.autogating_width, opt.autogating_taper] \ for gt in glitch_times] if len(glitch_times) > 0: logging.info('Autogating at %s', ', '.join(['%.3f' % gt for gt in glitch_times])) strain = gate_data(strain, gate_params) gating_info['auto'] = gate_params logging.info("Resampling data") strain = resample_to_delta_t(strain, 1.0/opt.sample_rate, method='ldas') logging.info("Highpass Filtering") strain = highpass(strain, frequency=opt.strain_high_pass) logging.info("Remove Padding") start = opt.pad_data*opt.sample_rate end = len(strain)-opt.sample_rate*opt.pad_data strain = strain[start:end] if opt.fake_strain: logging.info("Generating Fake Strain") duration = opt.gps_end_time - opt.gps_start_time tlen = duration * opt.sample_rate pdf = 1.0/128 plen = int(opt.sample_rate / pdf) / 2 + 1 logging.info("Making PSD for strain") strain_psd = pycbc.psd.from_string(opt.fake_strain, plen, pdf, opt.low_frequency_cutoff) logging.info("Making colored noise") strain = pycbc.noise.noise_from_psd(tlen, 1.0/opt.sample_rate, strain_psd, seed=opt.fake_strain_seed) strain._epoch = lal.LIGOTimeGPS(opt.gps_start_time) if opt.injection_file: logging.info("Applying injections") injections = InjectionSet(opt.injection_file) injections.apply(strain, opt.channel_name[0:2], distance_scale=opt.injection_scale_factor) if opt.sgburst_injection_file: logging.info("Applying sine-Gaussian burst injections") injections = SGBurstInjectionSet(opt.sgburst_injection_file) injections.apply(strain, opt.channel_name[0:2], distance_scale=opt.injection_scale_factor) if precision == 'single': logging.info("Converting to float32") strain = (dyn_range_fac * strain).astype(pycbc.types.float32) if opt.injection_file or opt.sgburst_injection_file: strain.injections = injections if opt.taper_data: logging.info("Tapering data") # Use auto-gating stuff for this, a one-sided gate is a taper pd_taper_window = opt.taper_data gate_params = [(strain.start_time, 0., pd_taper_window)] gate_params.append( (strain.end_time, 0., pd_taper_window) ) gate_data(strain, gate_params) strain.gating_info = gating_info return strain
def from_cli(opt, dyn_range_fac=1, precision='single'): """Parses the CLI options related to strain data reading and conditioning. Parameters ---------- opt : object Result of parsing the CLI with OptionParser, or any object with the required attributes (gps-start-time, gps-end-time, strain-high-pass, pad-data, sample-rate, (frame-cache or frame-files), channel-name, fake-strain, fake-strain-seed, gating_file). dyn_range_fac: {float, 1}, optional A large constant to reduce the dynamic range of the strain. Returns ------- strain : TimeSeries The time series containing the conditioned strain data. """ if opt.frame_cache or opt.frame_files: if opt.frame_cache: frame_source = opt.frame_cache if opt.frame_files: frame_source = opt.frame_files logging.info("Reading Frames") strain = read_frame(frame_source, opt.channel_name, start_time=opt.gps_start_time-opt.pad_data, end_time=opt.gps_end_time+opt.pad_data) if opt.zpk_z and opt.zpk_p and opt.zpk_k: logging.info("Highpass Filtering") strain = highpass(strain, frequency=opt.strain_high_pass) logging.info("Applying zpk filter") z = numpy.array(opt.zpk_z) p = numpy.array(opt.zpk_p) k = float(opt.zpk_k) strain = filter_zpk(strain.astype(numpy.float64), z, p, k) if opt.normalize_strain: logging.info("Dividing strain by constant") l = opt.normalize_strain strain = strain / l if opt.injection_file: logging.info("Applying injections") injections = InjectionSet(opt.injection_file) injections.apply(strain, opt.channel_name[0:2]) if opt.sgburst_injection_file: logging.info("Applying sine-Gaussian burst injections") injections = SGBurstInjectionSet(opt.sgburst_injection_file) injections.apply(strain, opt.channel_name[0:2]) logging.info("Highpass Filtering") strain = highpass(strain, frequency=opt.strain_high_pass) if precision == 'single': logging.info("Converting to float32") strain = (strain * dyn_range_fac).astype(float32) if opt.gating_file is not None: logging.info("Gating glitches") gate_params = numpy.loadtxt(opt.gating_file) if len(gate_params.shape) == 1: gate_params = [gate_params] strain = gate_data( strain, gate_params, data_start_time=(opt.gps_start_time - opt.pad_data)) logging.info("Resampling data") strain = resample_to_delta_t(strain, 1.0/opt.sample_rate, method='ldas') logging.info("Highpass Filtering") strain = highpass(strain, frequency=opt.strain_high_pass) logging.info("Remove Padding") start = opt.pad_data*opt.sample_rate end = len(strain)-opt.sample_rate*opt.pad_data strain = strain[start:end] if opt.fake_strain: logging.info("Generating Fake Strain") duration = opt.gps_end_time - opt.gps_start_time tlen = duration * opt.sample_rate pdf = 1.0/128 plen = int(opt.sample_rate / pdf) / 2 + 1 logging.info("Making PSD for strain") strain_psd = psd.from_string(opt.fake_strain, plen, pdf, opt.low_frequency_cutoff) logging.info("Making colored noise") strain = pycbc.noise.noise_from_psd(tlen, 1.0/opt.sample_rate, strain_psd, seed=opt.fake_strain_seed) strain._epoch = lal.LIGOTimeGPS(opt.gps_start_time) if opt.injection_file: logging.info("Applying injections") injections = InjectionSet(opt.injection_file) injections.apply(strain, opt.channel_name[0:2]) if opt.sgburst_injection_file: logging.info("Applying sine-Gaussian burst injections") injections = SGBurstInjectionSet(opt.sgburst_injection_file) injections.apply(strain, opt.channel_name[0:2]) if precision == 'single': logging.info("Converting to float32") strain = (dyn_range_fac * strain).astype(float32) if opt.injection_file: strain.injections = injections return strain
def from_cli(opt, dyn_range_fac=1, precision='single'): """Parses the CLI options related to strain data reading and conditioning. Parameters ---------- opt : object Result of parsing the CLI with OptionParser, or any object with the required attributes (gps-start-time, gps-end-time, strain-high-pass, pad-data, sample-rate, (frame-cache or frame-files), channel-name, fake-strain, fake-strain-seed, gating_file). dyn_range_fac: {float, 1}, optional A large constant to reduce the dynamic range of the strain. Returns ------- strain : TimeSeries The time series containing the conditioned strain data. """ gating_info = {} if opt.frame_cache or opt.frame_files or opt.frame_type: if opt.frame_cache: frame_source = opt.frame_cache if opt.frame_files: frame_source = opt.frame_files logging.info("Reading Frames") if opt.frame_type: strain = query_and_read_frame(opt.frame_type, opt.channel_name, start_time=opt.gps_start_time-opt.pad_data, end_time=opt.gps_end_time+opt.pad_data) else: strain = read_frame(frame_source, opt.channel_name, start_time=opt.gps_start_time-opt.pad_data, end_time=opt.gps_end_time+opt.pad_data) if opt.zpk_z and opt.zpk_p and opt.zpk_k: logging.info("Highpass Filtering") strain = highpass(strain, frequency=opt.strain_high_pass) logging.info("Applying zpk filter") z = numpy.array(opt.zpk_z) p = numpy.array(opt.zpk_p) k = float(opt.zpk_k) strain = filter_zpk(strain.astype(numpy.float64), z, p, k) if opt.normalize_strain: logging.info("Dividing strain by constant") l = opt.normalize_strain strain = strain / l if opt.injection_file: logging.info("Applying injections") injections = InjectionSet(opt.injection_file) injections.apply(strain, opt.channel_name[0:2], distance_scale=opt.injection_scale_factor) if opt.sgburst_injection_file: logging.info("Applying sine-Gaussian burst injections") injections = SGBurstInjectionSet(opt.sgburst_injection_file) injections.apply(strain, opt.channel_name[0:2], distance_scale=opt.injection_scale_factor) logging.info("Highpass Filtering") strain = highpass(strain, frequency=opt.strain_high_pass) if precision == 'single': logging.info("Converting to float32") strain = (strain * dyn_range_fac).astype(float32) if opt.gating_file is not None: logging.info("Gating glitches") gate_params = numpy.loadtxt(opt.gating_file) if len(gate_params.shape) == 1: gate_params = [gate_params] strain = gate_data(strain, gate_params) gating_info['file'] = \ [gp for gp in gate_params \ if (gp[0] + gp[1] + gp[2] >= strain.start_time) \ and (gp[0] - gp[1] - gp[2] <= strain.end_time)] if opt.autogating_threshold is not None: # the + 0 is for making a copy glitch_times = detect_loud_glitches( strain + 0., threshold=opt.autogating_threshold, cluster_window=opt.autogating_cluster, low_freq_cutoff=opt.strain_high_pass, high_freq_cutoff=opt.sample_rate/2, corrupted_time=opt.pad_data+opt.autogating_pad) gate_params = [[gt, opt.autogating_width, opt.autogating_taper] \ for gt in glitch_times] if len(glitch_times) > 0: logging.info('Autogating at %s', ', '.join(['%.3f' % gt for gt in glitch_times])) strain = gate_data(strain, gate_params) gating_info['auto'] = gate_params logging.info("Resampling data") strain = resample_to_delta_t(strain, 1.0/opt.sample_rate, method='ldas') logging.info("Highpass Filtering") strain = highpass(strain, frequency=opt.strain_high_pass) logging.info("Remove Padding") start = opt.pad_data*opt.sample_rate end = len(strain)-opt.sample_rate*opt.pad_data strain = strain[start:end] if opt.fake_strain: logging.info("Generating Fake Strain") duration = opt.gps_end_time - opt.gps_start_time tlen = duration * opt.sample_rate pdf = 1.0/128 plen = int(opt.sample_rate / pdf) / 2 + 1 logging.info("Making PSD for strain") strain_psd = pycbc.psd.from_string(opt.fake_strain, plen, pdf, opt.low_frequency_cutoff) logging.info("Making colored noise") strain = pycbc.noise.noise_from_psd(tlen, 1.0/opt.sample_rate, strain_psd, seed=opt.fake_strain_seed) strain._epoch = lal.LIGOTimeGPS(opt.gps_start_time) if opt.injection_file: logging.info("Applying injections") injections = InjectionSet(opt.injection_file) injections.apply(strain, opt.channel_name[0:2], distance_scale=opt.injection_scale_factor) if opt.sgburst_injection_file: logging.info("Applying sine-Gaussian burst injections") injections = SGBurstInjectionSet(opt.sgburst_injection_file) injections.apply(strain, opt.channel_name[0:2], distance_scale=opt.injection_scale_factor) if precision == 'single': logging.info("Converting to float32") strain = (dyn_range_fac * strain).astype(float32) if opt.injection_file or opt.sgburst_injection_file: strain.injections = injections strain.gating_info = gating_info return strain
row.amp_order = 0 row.coa_phase = 0 row.bandpass = 0 row.taper = inj.taper row.numrel_mode_min = 0 row.numrel_mode_max = 0 row.numrel_data = None row.source = 'ANTANI' row.process_id = 'process:process_id:0' row.simulation_id = 'sim_inspiral:simulation_id:0' sim_table.append(row) inj_file = open("injection.xml","w+") ligolw_utils.write_fileobj(xmldoc, inj_file) injection_set = InjectionSet("injection.xml") sample_rate = 4096 # Hz for det in [Detector(d) for d in ['H1', 'L1', 'V1']]: ts = TimeSeries(numpy.zeros(int(10 * sample_rate)), delta_t=1/sample_rate, epoch=lal.LIGOTimeGPS(end_time - 5), dtype=numpy.float64) injection_set.apply(ts, det.name) max_amp, max_loc = ts.abs_max_loc() pylab.plot(ts,label=det.name) pylab.legend()