diff --git a/pycbc/psd/__init__.py b/pycbc/psd/__init__.py index 95e7f0fa9ae..3e11330ed47 100644 --- a/pycbc/psd/__init__.py +++ b/pycbc/psd/__init__.py @@ -1,4 +1,3 @@ -#!/usr/bin/python # Copyright (C) 2014 Alex Nitz, Andrew Miller, Tito Dal Canton # # This program is free software; you can redistribute it and/or modify it @@ -44,9 +43,9 @@ def from_cli(opt, length, delta_f, low_frequency_cutoff, length : int The length in samples of the output PSD. delta_f : float - The frequency step of the output PSD. + The frequency step of the output PSD in hertz. low_frequency_cutoff: float - The low frequncy cutoff to use when calculating the PSD. + The low frequency cutoff to use when calculating the PSD, in hertz. strain : {None, TimeSeries} Time series containing the data from which the PSD should be measured, when psd_estimation is in use. @@ -60,6 +59,7 @@ def from_cli(opt, length, delta_f, low_frequency_cutoff, If 'single' the PSD will be converted to float32, if not already in that precision. If 'double' the PSD will be converted to float64, if not already in that precision. + Returns ------- psd : FrequencySeries @@ -95,8 +95,10 @@ def from_cli(opt, length, delta_f, low_frequency_cutoff, psd = from_txt(psd_file_name, length, delta_f, f_low, is_asd_file=is_asd_file) elif opt.asd_file: - err_msg = "ASD files are only valid as ASCII files (.dat or " - err_msg += ".txt). Supplied {}.".format(psd_file_name) + raise ValueError( + "ASD files must be in ASCII format (extension .dat or " + f".txt). Got {psd_file_name} instead" + ) elif psd_file_name.endswith(('.xml', '.xml.gz')): psd = from_xml(psd_file_name, length, delta_f, f_low, ifo_string=opt.psd_file_xml_ifo_string, @@ -121,7 +123,7 @@ def from_cli(opt, length, delta_f, low_frequency_cutoff, else: # Shouldn't be possible to get here - raise ValueError("Shouldn't be possible to raise this!") + raise RuntimeError("Shouldn't be possible to raise this!") if opt.psd_inverse_length: psd = inverse_spectrum_truncation(psd, @@ -138,10 +140,10 @@ def from_cli(opt, length, delta_f, low_frequency_cutoff, return psd.astype(float32) elif precision == 'double': return psd.astype(float64) - else: - err_msg = "If provided the precision kwarg must be either 'single' " - err_msg += "or 'double'. You provided %s." %(precision) - raise ValueError(err_msg) + raise ValueError( + "If provided, the precision kwarg must be either 'single' or " + f"'double'. You provided {precision}" + ) def from_cli_single_ifo(opt, length, delta_f, low_frequency_cutoff, ifo, **kwargs): @@ -430,9 +432,9 @@ def generate_overlapping_psds(opt, gwstrain, flen, delta_f, flow, flen : int The length in samples of the output PSDs. delta_f : float - The frequency step of the output PSDs. + The frequency step of the output PSDs in hertz. flow: float - The low frequncy cutoff to use when calculating the PSD. + The low frequency cutoff to use when calculating the PSD, in hertz. dyn_range_factor : {1, float} For PSDs taken from models or text files, if `dyn_range_factor` is not None, then the PSD is multiplied by `dyn_range_factor` ** 2. @@ -522,7 +524,7 @@ def associate_psds_to_segments(opt, fd_segments, gwstrain, flen, delta_f, flow, delta_f : float The frequency step of the output PSDs. flow: float - The low frequncy cutoff to use when calculating the PSD. + The low frequency cutoff to use when calculating the PSD, in hertz. dyn_range_factor : {1, float} For PSDs taken from models or text files, if `dyn_range_factor` is not None, then the PSD is multiplied by `dyn_range_factor` ** 2. @@ -551,8 +553,7 @@ def associate_psds_to_segments(opt, fd_segments, gwstrain, flen, delta_f, flow, psd_overlap = curr_overlap best_psd = psd if best_psd is None: - err_msg = "No PSDs found intersecting segment!" - raise ValueError(err_msg) + raise ValueError("No PSDs found intersecting segment!") fd_segment.psd = best_psd def associate_psds_to_single_ifo_segments(opt, fd_segments, gwstrain, flen, diff --git a/pycbc/psd/analytical.py b/pycbc/psd/analytical.py index ebc149a30c8..b216601754a 100644 --- a/pycbc/psd/analytical.py +++ b/pycbc/psd/analytical.py @@ -1,4 +1,3 @@ -#!/usr/bin/python # Copyright (C) 2012-2016 Alex Nitz, Tito Dal Canton, Leo Singer # 2022 Shichao Wu # @@ -101,9 +100,9 @@ def from_string(psd_name, length, delta_f, low_freq_cutoff, **kwargs): length : int Length of the frequency series in samples. delta_f : float - Frequency resolution of the frequency series. + Frequency resolution of the frequency series, in hertz. low_freq_cutoff : float - Frequencies below this value are set to zero. + Frequencies below this value (in hertz) are set to zero. **kwargs : All other keyword arguments are passed to the PSD model. @@ -115,8 +114,9 @@ def from_string(psd_name, length, delta_f, low_freq_cutoff, **kwargs): # check if valid PSD model if psd_name not in get_psd_model_list(): - raise ValueError(psd_name + ' not found among analytical ' - 'PSD functions.') + raise ValueError( + psd_name + ' not found among analytical PSD functions.' + ) # make sure length has the right type for CreateREAL8FrequencySeries if not isinstance(length, numbers.Integral) or length <= 0: @@ -156,9 +156,9 @@ def flat_unity(length, delta_f, low_freq_cutoff): length : int Length of output Frequencyseries. delta_f : float - Frequency step for output FrequencySeries. + Frequency step for output FrequencySeries, in hertz. low_freq_cutoff : int - Low-frequency cutoff for output FrequencySeries. + Low-frequency cutoff for output FrequencySeries, in hertz. Returns ------- diff --git a/pycbc/psd/read.py b/pycbc/psd/read.py index 4ab9f29882a..05ba2d6f42d 100644 --- a/pycbc/psd/read.py +++ b/pycbc/psd/read.py @@ -1,4 +1,3 @@ -#!/usr/bin/python # Copyright (C) 2012 Alex Nitz, Tito Dal Canton # # This program is free software; you can redistribute it and/or modify it @@ -31,15 +30,15 @@ def from_numpy_arrays(freq_data, noise_data, length, delta_f, low_freq_cutoff): Parameters ---------- freq_data : array - Array of frequencies. + Array of frequencies in hertz. noise_data : array PSD values corresponding to frequencies in freq_arr. length : int Length of the frequency series in samples. delta_f : float - Frequency resolution of the frequency series in Herz. + Frequency resolution of the frequency series in hertz. low_freq_cutoff : float - Frequencies below this value are set to zero. + Frequencies below this value (in hertz) are set to zero. Returns ------- @@ -48,8 +47,10 @@ def from_numpy_arrays(freq_data, noise_data, length, delta_f, low_freq_cutoff): """ # Only include points above the low frequency cutoff if freq_data[0] > low_freq_cutoff: - raise ValueError('Lowest frequency in input data ' - ' is higher than requested low-frequency cutoff ' + str(low_freq_cutoff)) + raise ValueError( + f'Lowest frequency in input PSD data ({freq_data[0]} Hz) is ' + f'higher than requested low-frequency cutoff ({low_freq_cutoff} Hz)' + ) kmin = int(low_freq_cutoff / delta_f) flow = kmin * delta_f @@ -94,14 +95,14 @@ def from_txt(filename, length, delta_f, low_freq_cutoff, is_asd_file=True): ---------- filename : string Path to a two-column ASCII file. The first column must contain - the frequency (positive frequencies only) and the second column + the frequency in hertz (positive frequencies only) and the second column must contain the amplitude density OR power spectral density. length : int Length of the frequency series in samples. delta_f : float - Frequency resolution of the frequency series in Herz. + Frequency resolution of the frequency series in hertz. low_freq_cutoff : float - Frequencies below this value are set to zero. + Frequencies below this value (in hertz) are set to zero. is_asd_file : Boolean If false assume that the second column holds power spectral density. If true assume that the second column holds amplitude spectral density. @@ -147,7 +148,7 @@ def from_xml(filename, length, delta_f, low_freq_cutoff, ifo_string=None, length : int Length of the frequency series in samples. delta_f : float - Frequency resolution of the frequency series in Herz. + Frequency resolution of the frequency series in hertz. low_freq_cutoff : float Frequencies below this value are set to zero. ifo_string : string