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 read_injections(self, group=None): """Gets injection parameters. Injections are retrieved from ``[{group}/]injections``. Parameters ---------- group : str, optional Group that the injections group is in. Default (None) is to look in the top-level. Returns ------- FieldArray Array of the injection parameters. """ if group is None or group == '/': group = self.injections_group else: group = '/'.join([group, self.injections_group]) injset = InjectionSet(self.filename, hdf_group=group) injections = injset.table.view(FieldArray) # close the new open filehandler to self injset._injhandler.filehandler.close() return injections
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 read_injections(self): """Gets injection parameters. Returns ------- FieldArray Array of the injection parameters. """ injset = InjectionSet(self.filename, hdf_group=self.injections_group) injections = injset.table.view(FieldArray) # close the new open filehandler to self injset._injhandler.filehandler.close() return injections
def get_static_params_from_injection(static_params, injection_file): """Gets FROM_INJECTION static params from injection. Parameters ---------- static_params : dict Dictionary of static params. injection_file : str Name of the injection file to use. Returns ------- dict : A dictionary mapping parameter names to values retrieved from the injection file. The dictionary will only contain parameters that were set to ``"FROM_INJECTION"`` in the ``static_params``. Parameter names -> values The injection parameters. """ # pull out the parameters that need replacing replace_params = {p: None for (p, val) in static_params.items() if val == 'FROM_INJECTION'} if replace_params != {}: # make sure there's actually an injection file if injection_file is None: raise ValueError("one or more static params are set to " "FROM_INJECTION, but no injection file " "provided in data section") inj = InjectionSet(injection_file) # make sure there's only one injection provided if inj.table.size > 1: raise ValueError("Some static params set to FROM_INJECTION, but " "more than one injection exists in the injection " "file.") for param in replace_params: try: injval = inj.table[param][0] except NameError: # means the parameter doesn't exist raise ValueError("Static param {} with placeholder " "FROM_INJECTION has no counterpart in " "injection file.".format(param)) replace_params[param] = injval return replace_params
def get_values_from_injection(cp, injection_file, update_cp=True): """Replaces all FROM_INJECTION values in a config file with the corresponding value from the injection. This looks for any options that start with ``FROM_INJECTION[:ARG]`` in a config file. It then replaces that value with the corresponding value from the injection file. An argument may be optionally provided, in which case the argument will be retrieved from the injection file. Functions of parameters in the injection file may be used; the syntax and functions available is the same as the ``--parameters`` argument in executables such as ``pycbc_inference_extract_samples``. If no ``ARG`` is provided, then the option name will try to be retrieved from the injection. For example, .. code-block:: ini mass1 = FROM_INJECTION will cause ``mass1`` to be retrieved from the injection file, while: .. code-black:: ini mass1 = FROM_INJECTION:'primary_mass(mass1, mass2)' will cause the larger of mass1 and mass2 to be retrieved from the injection file. Note that if spaces are in the argument, it must be encased in single quotes. The injection file may contain only one injection. Otherwise, a ValueError will be raised. Parameters ---------- cp : ConfigParser The config file within which to replace values. injection_file : str or None The injection file to get values from. A ValueError will be raised if there are any ``FROM_INJECTION`` values in the config file, and injection file is None, or if there is more than one injection. update_cp : bool, optional Update the config parser with the replaced parameters. If False, will just retrieve the parameter values to update, without updating the config file. Default is True. Returns ------- list The parameters that were replaced, as a tuple of section name, option, value. """ lookfor = 'FROM_INJECTION' # figure out what parameters need to be set replace_params = [] for sec in cp.sections(): for opt in cp.options(sec): val = cp.get(sec, opt) splitvals = shlex.split(val) replace_this = [] for ii, subval in enumerate(splitvals): if subval.startswith(lookfor): # determine what we should retrieve from the injection subval = subval.split(':', 1) if len(subval) == 1: subval = opt else: subval = subval[1] replace_this.append((ii, subval)) if replace_this: replace_params.append((sec, opt, splitvals, replace_this)) if replace_params: # check that we have an injection file if injection_file is None: raise ValueError("One or values are set to {}, but no injection " "file provided".format(lookfor)) # load the injection file inj = InjectionSet(injection_file).table.view(type=FieldArray) # make sure there's only one injection provided if inj.size > 1: raise ValueError("One or more values are set to {}, but more than " "one injection exists in the injection file." .format(lookfor)) # get the injection values to replace for ii, (sec, opt, splitvals, replace_this) in enumerate(replace_params): # replace the value in the shlex-splitted string with the value # from the injection for jj, arg in replace_this: splitvals[jj] = str(inj[arg][0]) # now rejoin the string... # shlex will strip quotes around arguments; this can be problematic # when rejoining if the the argument had a space in it. In python 3.8 # there is a shlex.join function which properly rejoins things taking # that into account. Since we need to continue to support earlier # versions of python, the following kludge tries to account for that. # If/when we drop support for all earlier versions of python, then the # following can just be replaced by: # replace_val = shlex.join(splitvals) for jj, arg in enumerate(splitvals): if ' ' in arg: arg = "'" + arg + "'" splitvals[jj] = arg replace_val = ' '.join(splitvals) replace_params[ii] = (sec, opt, replace_val) # replace in the config file if update_cp: for (sec, opt, replace_val) in replace_params: cp.set(sec, opt, replace_val) return replace_params
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
('tc', float), ('distance', float), ('ra', float), ('dec', float), ('approximant', 'S32')] static_params = {'f_lower': 17., 'f_ref': 17., 'taper': 'start', 'inclination': 0., 'coa_phase': 0., 'polarization': 0.} samples = FieldArray(2, dtype=dtype) # masses and spins are intended to match the highest # and lowest mass templates in the template bank samples['mass1'] = [290.929321, 1.1331687] samples['mass2'] = [3.6755455, 1.010624] samples['spin1z'] = [0.9934847, 0.029544285] samples['spin2z'] = [0.92713535, 0.020993788] # distance and sky locations to have network SNRs ~15 samples['tc'] = [1272790100.1, 1272790260.1] samples['distance'] = [178., 79.] samples['ra'] = [np.deg2rad(45), np.deg2rad(10)] samples['dec'] = [np.deg2rad(45), np.deg2rad(-45)] samples['approximant'] = ['SEOBNRv4_opt', 'SpinTaylorT4'] InjectionSet.write('injections.hdf', samples, static_args=static_params, injtype='cbc', cmd=" ".join(sys.argv))
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()