utils package

Subpackages

utils.draw module

Utility functions for drawing figures

set_fig_size(w, h, ax=None)[source]

Set figure size

Parameters:
  • w (float) – width in inches

  • h (float) – height in inches

  • ax (axis object) –

remove_right_top(ax)[source]

Remove top and right axis

Parameters:

ax (axis object) –

get_ax_lim(ax_min, ax_max, base=10)[source]

Get axis limit

Parameters:
  • ax_min (float) –

  • ax_max (float) –

  • base (int, default = 10) –

Returns:

  • ax_min (float)

  • ax_max (float)

utils.functions module

Utility functions used for analysis

unique(input_list: list) list[source]

Extract unique strings from the list in the order they appear

Parameters:

input_list (list) –

Return type:

list of unique, ordered string

find_str(string: str, pattern: str) list[source]

Find all indices of patterns in a string

Parameters:
  • string (str) – input string

  • pattern (str) – string pattern to search

Returns:

ind – list of starting indices

Return type:

list

find_data_path()[source]

Request the user to manually find dir path and return it

list_files(dir: str, ext: str) list[source]

Return the list of files in the current directory

Parameters:
  • dir (path) –

  • ext (str) – file extension (e.g., .wav, .rhd etc)

Returns:

files

Return type:

list

open_folder(path) None[source]

Open the directory in win explorer

myround(x: int, base=5) int[source]

Round to the next multiple of the base

Parameters:
  • x (int) – input value

  • base (int) – base value (5 by default)

Return type:

int

extract_ind(timestamp: ndarray, range: list)[source]

Extract timestamp indices from array from the specified range

Parameters:
  • timestamp (np.ndarray) –

  • range (list) – [start end]

Returns:

  • ind (np.ndarray) – index of the array

  • array (np.ndarray) – array within the range

normalize(array: ndarray) ndarray[source]

Normalizes an array by its average and sd

Parameters:

array (np.ndarray) – Input array

Return type:

np.ndarray

exists(var) bool[source]

Check if a variable exists

Parameters:

var (str) – Note that the argument should be in parenthesis

Return type:

bool

para_interp(x, y)[source]

Get max value by performing parabolic interpolation given three data points

Parameters:
  • x (np.ndarray) –

  • y (np.ndarray) –

Returns:

  • x_max (float) – max index

  • y_max (float) – estimated max value

utils.save module

Utility functions to make output directories & save output files

make_dir(parent_path, *dir_name, add_date=True)[source]

Make a new directory

Parameters:
  • parent_path (path) –

  • dir_name (str) – (optional), if not exists, files will be saved in parent_dir

  • add_date (bool) – make a sub-dir with a date

Returns:

save_path

Return type:

path

save_fig(fig, save_path, name, fig_ext='.png', view_folder=False, dpi=None) None[source]

Save figure

Parameters:
  • fig (figure object) –

  • save_path (path) – directory path to save figures

  • name (str) – name of the figure

  • fig_ext (str) – figure extension (e.g., ‘.pdf’ for vector output), ‘.png’ by default

  • view_folder (bool) – open the folder where the figure is saved

  • dpi (int) – increase the value for enhanced resolution

save2json(filename, data) None[source]

Save data in .json format

Parameters:
  • filename (str) –

  • data (arr) –

utils.spect module

Utility functions for drawing spectrograms

Spectrogram adapted from code by Kyle Kastner and Tim Sainburg https://github.com/timsainb/python_spectrograms_and_inversion

by David Nicholson https://github.com/NickleDave/vak

butter_bandpass(lowcut, highcut, fs, order=5)[source]
butter_bandpass_filter(data, lowcut, highcut, fs, order=5)[source]
spectrogram(dat, samp_freq, fft_size=512, step_size=64, thresh=None, transform_type=None, freq_range=None)[source]

creates a spectrogram

Parameters:
  • dat (numpy.ndarray) – audio signal

  • samp_freq (int) – sampling frequency in Hz

  • fft_size (int) – size of window for Fast Fourier transform, number of time bins.

  • step_size (int) – step size for Fast Fourier transform

  • transform_type (str) – one of {‘log_spect’, ‘log_spect_plus_one’}. ‘log_spect’ transforms the spectrogram to log(spectrogram), and ‘log_spect_plus_one’ does the same thing but adds one to each element. Default is None. If None, no transform is applied.

  • thresh (int) – threshold minimum power for log spectrogram

  • freq_range (tuple) – of two elements, lower and higher frequencies.

Returns:

  • spect (numpy.ndarray) – spectrogram

  • freqbins (numpy.ndarray) – vector of centers of frequency bins from spectrogram

utils.stats module

Utility functions for statistical testing

get_sig(pval)[source]

Returns asterisk depending on the magnitude of significance

z_test(count1, nbos1, count2, nobs2)[source]

Z-test for proportion

paired_ttest(arr1, arr2)[source]

Performs paired t-test between two arrays

two_sample_ttest(arr1, arr2)[source]

Performs independent two-sample t-test between two arrays

rank_sum_test(arr1, arr2)[source]

Performs rank-sum test (non-parametric independent 2-sample test)

signed_rank_test(arr1, arr2)[source]

Wilcoxon signed-rank test (non-parametric paired test)

two_sample_ks_test(arr1, arr2, alternative='two-sided')[source]

Performs Kolmogorov-Smirnov test to compare two distributions