Skip to content

Commit

Permalink
Minor PSD module fixes (#5015)
Browse files Browse the repository at this point in the history
* Cleanup and minor fixes to PSD module

* Fix typos

* Apply suggestions from code review

---------

Co-authored-by: Gareth S Cabourn Davies <[email protected]>
  • Loading branch information
titodalcanton and GarethCabournDavies authored Jan 22, 2025
1 parent 52034ec commit 9b3efda
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 32 deletions.
31 changes: 16 additions & 15 deletions pycbc/psd/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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):
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 7 additions & 7 deletions pycbc/psd/analytical.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/python
# Copyright (C) 2012-2016 Alex Nitz, Tito Dal Canton, Leo Singer
# 2022 Shichao Wu
#
Expand Down Expand Up @@ -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.
Expand All @@ -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:
Expand Down Expand Up @@ -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
-------
Expand Down
21 changes: 11 additions & 10 deletions pycbc/psd/read.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
-------
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9b3efda

Please sign in to comment.