Utility Module

utils provides helper functions for analysis. Many are called directly by methods on a DelaySpectrum object like normalized_fourier_transform but others like bootstrap_array and weighted_average are provided to help make analysis easier.

Read and Write support for PAPER miriad files with pyuvdata.

simpleDS.utils.bootstrap_array(array, nboot=100, axis=0)[source]

Bootstrap resample the input array along the given axis.

Randomly sample, with replacement along the given axis nboot times. Output array will always have N+1 dimensions with the extra axis immediately following the bootstrapped axis.

Parameters
  • array (numpy array) – N-dimensional array to bootstrap resample.

  • nboot (int) – Number of resamples to draw.

  • axis (int) – Axis along which resampling is performed

Returns

array – The resampled array, if input is N-D output is N+1-D, extra dimension is added imediately suceeding the sampled axis.

Return type

numpy array

Raises

ValueError – If axis parameter is greater than the dimensions of the input array.

simpleDS.utils.combine_nsamples(nsample_1, nsample_2=None, axis=- 1)[source]

Combine the nsample arrays for use in cross-multiplication.

Uses numpy slicing to generate array of all sample cross-multiples. Used to find the combine samples for a the delay spectrum. The geometric mean is taken between nsamples_1 and nsamples_2 because nsmaples array is used to compute thermal variance in the delay spectrum.

Parameters
  • nsample_1 (array of float) – (Nbls, Ntimes, Nfreqs) array from get_nsamples_array can also have shape (Npols, Nbls, Ntimes, Nfreqs)

  • nsample_2 (array of float, optional) – Same type as nsample_1 if take cross-multiplication Defaults to copying nsample_1 for auto-correlation

  • axis (int) – The axis over which the cross multiplication should occur.

Returns

samples_out – (Nbls, Nbls, Nfreqs, Ntimes) array of geometric mean of the input sample arrays. Can also have shape (Npols, Nbls, Nbls, Ntimes, Nfreqs)

Return type

array of float

Raises

ValueError – If both input arrays have different shapes.

simpleDS.utils.cross_multiply_array(array_1, array_2=None, axis=0)[source]

Cross multiply the arrays along the given axis.

Cross multiplies along axis and computes array_1.conj() * array_2 if axis has length M then a new axis of size M will be inserted directly succeeding the original.

Parameters
  • array_1 (array_like) – N-dimensional array_like

  • array_2 (array_like, optional) – N-dimenional array. Defaults to copy of array_1

  • axis (int) – Axis along which to cross multiply

Returns

cross_array – N+1 Dimensional array

Return type

array_like

Raises

ValueError – If input arrays have different shapes.

simpleDS.utils.fold_along_delay(delays, array, uncertainty, weights=None, axis=- 1)[source]

Fold input array over the delay axis.

Averages input array along the given axis by splitting the delay array around the value 0 and averaging values in array correspoding to the same delay magnitutde.

Parameters
  • delays (Astropy Quantity units equivalent to time.) – A 1-Dimensional numpy array of interferometric delays.

  • array (Astropy Quantity units equivalent to time, or power.) – An N-Dimensional Quantity to average across delay magnitudes.

  • uncertainty (Astropy Quantity units equivalent to array) – An N-Dimensional Quantity of uncertainty values for input array

  • weights (array_like) – Weights to use while averaging the input array. Must have same shape as input array. Default: np.ones_like(array)

  • axis (int) – The axis over which the input array is to be folded. Must have the same shape as the size of input delays.

Returns

  • array (Astropy Quantity with units equivalent to input array) – The N-Dimensional input array folded over the axis specified give axis will have size np.shape(array)[axis]/2 if shape is even or (np.shape(array)[axis] + 1)/2 if shape is odd

  • uncertainty (Astropy Quantity with units equivalent to input uncertainty.) – The folded uncertainties corresponding to the input array.

Raises

ValueError – If shape of the specified axis is not equal to the length of the delay array. If input delays either have no 0 bin or evenly spaced around zero. If uncertainty is not the same shape as array.

simpleDS.utils.generate_noise(noise_power)[source]

Generate noise given an input array of noise power.

Parameters

noise_power (array_like of float) – N-dimensional array of noise power to generate white noise.

Returns

noise – Complex white noise drawn from a Gaussian distribution with width given by the value of the input noise_power array.

Return type

array_like of complex

simpleDS.utils.get_data_array(uv, reds, squeeze=True)[source]

Remove data from pyuvdata object and store in numpy array.

Uses UVData.get_data function to create a matrix of data of shape (Npols, Nbls, Ntimes, Nfreqs). Only valid to call on a set of redundant baselines with the same number of times.

Parameters
  • uv (UVdata object, or subclass) – Data object which can support uv.get_data(ant_1, ant_2, pol)

  • reds (list of ints) – list of all redundant baselines of interest as baseline numbers.

  • squeeze (bool) – set true to squeeze the polarization dimension. This has no effect for data with Npols > 1.

Returns

data_array – (Nbls , Ntimes, Nfreqs) numpy array or (Npols, Nbls, Ntimes, Nfreqs) if squeeze == False

Return type

complex arrary

simpleDS.utils.get_flag_array(uv, reds, squeeze=True)[source]

Remove nsamples from pyuvdata object and store in numpy array.

Uses UVData.get_flags function to create a matrix of data of shape (Npols, Nbls, Ntimes, Nfreqs). Only valid to call on a set of redundant baselines with the same number of times.

Parameters
  • uv (UVdata object, or subclass) – Data object which can support uv.get_data(ant_1, ant_2, pol)

  • reds (list of ints) – list of all redundant baselines of interest as baseline numbers.

  • squeeze (bool) – set true to squeeze the polarization dimension. This has no effect for data with Npols > 1.

Returns

flag_array – (Nbls, Ntimes, Nfreqs) numpy array (Npols, Nbls, Ntimes, Nfreqs) if squeeze == False

Return type

bool array

simpleDS.utils.get_integration_time(uv, reds, squeeze=True)[source]

Extract the integration_time array from pyuvdata objectself.

Extracts the integration time array from a UVdata object to create a matrix of shape (Npols, Nbls, Ntimes, Nfreqs). Only valid to call on a set of redundant baselines with the same number of times.

Parameters
  • uv (UVdata object, or subclass) – Data object which can support uv.get_data(ant_1, ant_2, pol)

  • reds (list of ints) – list of all redundant baselines of interest as baseline numbers.

  • squeeze (bool) – set true to squeeze the polarization dimension. This has no effect for data with Npols > 1.

Returns

integration_time – (Nbls, Ntimes) numpy array of integration times.

Return type

float array

simpleDS.utils.get_nsample_array(uv, reds, squeeze=True)[source]

Remove nsamples from pyuvdata object and store in numpy array.

Uses UVData.get_nsamples function to create a matrix of data of shape (Npols, Nbls, Ntimes, Nfreqs). Only valid to call on a set of redundant baselines with the same number of times.

Parameters
  • uv (UVdata object, or subclass) – Data object which can support uv.get_data(ant_1, ant_2, pol)

  • reds (list of ints) – list of all redundant baselines of interest as baseline numbers.

  • squeeze (bool) – set true to squeeze the polarization dimension. This has no effect for data with Npols > 1.

Returns

nsample_array – (Nbls, Ntimes, Nfreqs) numpy array or (Npols, Nbls, Ntimes, Nfreqs) if squeeze == False

Return type

float array

simpleDS.utils.jy_to_mk(freqs)[source]

Calculate the Jy/sr to mK conversion lambda^2/(2 * K_boltzman).

Parameters

freqs (Astropy Quantity with units equivalent to frequency) – frequencies where the conversion should be calculated.

Returns

The conversion factor from Jy to mK * sr at the given frequencies.

Return type

Astropy Quantity

simpleDS.utils.lst_align(uv1, uv2, ra_range, inplace=True, atol=1e-08, rtol=1e-05)[source]

Align the LST values of two pyuvdata objects within the given range.

Attempts to crudely align input UVData objects in LST by finding indices where the lsts fall within the input range.

Parameters
  • uv1 (UVData object or subclass) – First object where data is desired to lie within the ra_range. Must have a time_array parameter to find lsts.

  • uv2 (UVData object or subclass) – Must have the same properties as uv1

  • ra_range (length 2 list of float) – The inclusive start and stop times in hours for the LSTs to align.

  • inplace (bool) – If true performs UVData.select on uv1 and uv2 otherwise returns new objects.

  • atol (float) – Absolute tolerance with which the integrations time should agree

  • rtol (float) – Relative tolerance with which the integrations time should agree

Returns

(uv1, uv2) – only returns when inplace is False.

Return type

tuple of UVData objects

Raises

ValueError – if uv1 and uv2 have different integration times, or time cadences.

simpleDS.utils.noise_equivalent_bandwidth(window)[source]

Calculate the relative equivalent noise bandwidth of window function.

Approximates the relative Noise Equivalent Bandwidth of a spectral taper function.

Parameters

window (array_like) – A 1-Dimenaional array like.

Returns

Return type

float

simpleDS.utils.normalized_fourier_transform(data_array, delta_x, axis=-1, taper=<function blackmanharris>, inverse=False)[source]

Perform the Fourier transform over specified axis.

Perform the FFT over frequency using the specified taper function and normalizes by delta_x (the discrete of sampling rate along the axis).

Parameters
  • data_array (array_like) – N-dimenaional array of data to Fourier Transform

  • delta_x (Astropy Quantity) – The difference between channels in the data over the axis of transformation. This is used to properly normalize the Fourier Transform.

  • taper (callable) – Spectral taper function used in Fourier transform. Default is scipy.signal.windows.blackmanharris

  • inverse ((bool; Default False)) – Perform the inverse Fourier Transform with np.fft.ifft

Returns

fourier_arry – N-Dimenaional array of the Fourier transform along specified axis, and normalized by the provided delta_x.

Return type

array_like complex

Raises

ValueError – If delta_x is not a Quantity object.

simpleDS.utils.remove_auto_correlations(data_array, axes=(0, 1))[source]

Remove the auto-corrlation term from input array.

Takes an N x N array, removes the diagonal components, and returns a flattened N(N-1) dimenion in place of the array. If uses on a M dimensional array, returns an M-1 array.

Parameters
  • data_array (array) – Array shaped like (Nbls, Nbls, Ntimes, Nfreqs). Removes same baseline diagonal along the specifed axes.

  • axes (tuple of int, length 2) – axes over which the diagonal will be removed.

Returns

data_out – (Nbls * (Nbls-1), Ntimes, Nfreqs) array. if input has pols: (Npols, Nbls * (Nbls -1), Ntimes, Nfreqs)

Return type

array with the same type as data_array.

Raises

ValueError – If axes is not a length 2 tuple. If axes are not adjecent (e.g. axes=(2,7)). If axes do not have the same shape.

simpleDS.utils.weighted_average(array, uncertainty, weights=None, axis=- 1)[source]

Compute the weighted average and propagate uncertainty.

Performs weighted average of input array, and propagates the weighted average into the uncertainty.

Parameters
  • array (array_like) – N-dimensional array over which to average an axis.

  • uncertainty (array_like) – N-dimensional array of uncertainties associated with each point in input array.

  • weights (array_like, Optional) – N-dimenional or 1-Dimenaiona array of weights to apply to each data point. If weights are one dimensional must be of length N and assumped to be the same for each row M. if weights is None, uses inverse variance weighting

  • axis (int) – The axis over which the average is taken.

Returns

  • array (array_like) – MxN-1 dimenionals array averaged array with given weights.

  • uncertainty (array_like) – MxN-1 dimensional propagated uncertainty array for given weights.

Raises

ValueError – If either one of the array or uncertainty is an astropy Quantity object but the other is not. If array and uncertainty have different shapes. If weights are 1-Dimensional but length is not the same as the lenght of array along the given axis. If weights are N-Dimensional but shaped differs from array.