sigpyproc.utils#

Utility functions for sigpyproc.

This module contains utility functions for sigpyproc.

Classes

FrequencyChannels

A class to handle frequency channels.

Functions

apply_along_axes

Apply a 1D function along one or more axes.

detect_file_type

Detect file type based on file extension.

duration_string

Convert duration in seconds to human readable string.

gaussian

Generate a Gaussian profile.

get_callerfunc

Get the name of the function that called the current function.

get_logger

Get a fancy configured logger.

nearest_factor

Calculate the factor of num closest to fac.

next2_to_n

Find the next power of 2 greater than or equal to x.

next_multiple

Return the smallest multiple of y that is greater than or equal to x.

pad_centre

Pad an array with zeros up to the target_length.

validate_path

Validate a path based on various criteria.

class sigpyproc.utils.FrequencyChannels(freqs)[source]#

Bases: object

A class to handle frequency channels.

Parameters:
freqsArrayLike

Central frequencies of the channels in MHz.

Attributes:
freqsndarray

Central frequencies of the channels in MHz.

arrayQuantity

Frequency array in Astropy Quantity format.

nchans

Number of frequency channels.

fch1

Central frequency of the first channel.

foff

Frequency offset between channels.

ftop

Edge frequency of the top channel.

fcenter

Central frequency of the entire band.

fbottom

Edge frequency of the bottom channel.

bandwidth

Bandwidth of the entire band.

Methods

from_pfits(fcenter, bandwidth, nchans)

Create from pfits parameters.

from_sig(fch1, foff, nchans)

Create from sigproc parameters.

freqs#
array#
property nchans#

Number of frequency channels.

Returns:
int

Number of channels.

property fch1#

Central frequency of the first channel.

Returns:
Quantity

Central frequency of the first channel in MHz.

property foff#

Frequency offset between channels.

Returns:
Quantity

Frequency offset between channels in MHz.

property ftop#

Edge frequency of the top channel.

Returns:
Quantity

Edge frequency of the top channel in MHz.

property fcenter#

Central frequency of the entire band.

Returns:
Quantity

Central frequency of the entire band in MHz.

property fbottom#

Edge frequency of the bottom channel.

Returns:
Quantity

Edge frequency of the bottom channel in MHz.

property bandwidth#

Bandwidth of the entire band.

Returns:
Quantity

Bandwidth in MHz.

classmethod from_sig(fch1, foff, nchans)[source]#

Create from sigproc parameters.

Parameters:
fch1float

Central frequency of the first channel.

fofffloat

Frequency offset between channels.

nchansint

Number of frequency channels.

Returns:
FrequencyChannels

FrequencyChannels object.

classmethod from_pfits(fcenter, bandwidth, nchans)[source]#

Create from pfits parameters.

Parameters:
fcenterfloat

Central frequency of the band.

bandwidthfloat

Total bandwidth in MHz.

nchansint

Number of frequency channels.

Returns:
FrequencyChannels

FrequencyChannels object.

sigpyproc.utils.apply_along_axes(func, data, axis=None)[source]#

Apply a 1D function along one or more axes.

sigpyproc.utils.detect_file_type(filename)[source]#

Detect file type based on file extension.

Supported extensions:

  • sigproc (.fil).

  • pfits (.fits, .sf).

  • fbh5 (.h5).

Parameters:
filenamestr | Path

File name to detect format for.

Returns:
str

File type name.

sigpyproc.utils.duration_string(duration)[source]#

Convert duration in seconds to human readable string.

Parameters:
durationfloat

Duration in seconds.

Returns:
str

Human readable duration string.

sigpyproc.utils.gaussian(x, mu, fwhm, amp=1.0)[source]#

Generate a Gaussian profile.

Parameters:
xndarray

Array of x values.

mufloat

Mean of the Gaussian.

fwhmfloat

Full width at half maximum.

ampfloat, optional

Amplitude of the Gaussian, by default 1.0.

Returns:
ndarray

Gaussian profile.

sigpyproc.utils.get_callerfunc(stack)[source]#

Get the name of the function that called the current function.

Parameters:
stacklist[inspect.FrameInfo]

Stack trace from stack().

Returns:
str

Name of the calling function.

sigpyproc.utils.get_logger(name, *, level=logging.INFO, quiet=False, log_file=None)[source]#

Get a fancy configured logger.

Parameters:
namestr

Logger name.

levelint or str, optional

Logging level, by default logging.INFO.

quietbool, optional

If True set level as logging.WARNING, by default False.

log_filestr, optional

Path to log file, by default None.

Returns:
logging.Logger

A logging object.

sigpyproc.utils.nearest_factor(num, fac)[source]#

Calculate the factor of num closest to fac.

Parameters:
numint

Number that we wish to factor.

facint

Number around which we wish to find factor.

Returns:
int

Nearest factor.

sigpyproc.utils.next2_to_n(x)[source]#

Find the next power of 2 greater than or equal to x.

Parameters:
xint

Number to find the next power of 2 for.

Returns:
int

Next power of 2 greater than or equal to x.

Raises:
ValueError

If x is not positive.

sigpyproc.utils.next_multiple(x, y)[source]#

Return the smallest multiple of y that is greater than or equal to x.

Parameters:
xint

The number to adjust.

yint

The factor to which x should be a multiple.

Returns:
int

The smallest multiple of y that is greater than or equal to x.

Raises:
ValueError

If y is not positive.

sigpyproc.utils.pad_centre(array, target_length)[source]#

Pad an array with zeros up to the target_length.

Parameters:
arraynp.ndarray

N-D numpy array to pad

target_lengthint

Target length along last axis

Returns:
np.ndarray

Padded array

Raises:
ValueError

If target_length is less than the last axis length of the array

sigpyproc.utils.validate_path(path, *, exists=True, file_okay=True, dir_okay=False, readable=True, writable=False, resolve_path=True)[source]#

Validate a path based on various criteria.

Parameters:
pathstr | Path

Path to validate.

existsbool, optional

Whether the path must exist, by default True.

file_okaybool, optional

Whether a file path is acceptable, by default True.

dir_okaybool, optional

Whether a directory path is acceptable, by default False.

readablebool, optional

Whether the path must be readable, by default True.

writablebool, optional

Whether the path must be writable, by default False.

resolve_pathbool, optional

Whether to resolve the path to an absolute path, by default True.

Returns:
Path

Validated path.

Raises:
ValueError

If neither file_okay nor dir_okay is True.

FileNotFoundError

If exists is True and the path does not exist.

NotADirectoryError

If a directory path is expected but a file path was found.

IsADirectoryError

If a file path is expected but a directory path was found.

PermissionError

If the path lacks the required permissions.