video699.common

This module implements commonly useful constants, and functions.

Module Contents

video699.common.COLOR_RGBA_TRANSPARENT = [0, 0, 0, 0]
video699.common.get_batches(iterable, batch_size)

Returns an iterator of batches produced from an iterable.

Parameters:
  • iterable (iterable) – An iterable.
  • batch_size (int) – The size of every batch with the exception of the last one, which may contain fewer elements.
Returns:

batches – An iterable of batches.

Return type:

iterable of tuple

video699.common.change_aspect_ratio_by_upscaling(original_width, original_height, new_aspect_ratio)

Returns new dimensions that upscale an image to a new aspect ratio.

Parameters:
  • original_width (int) – The original width of an image.
  • original_height (int) – The original height of an image.
  • new_aspect_ratio (Fraction) – A new aspect ratio. The ratio must be non-zero.
Returns:

  • rescaled_width (int) – The width of the image after rescaling.
  • rescaled_height (int) – The height of the image after rescaling.

Raises:

ValueError – When some of the original dimensions or the new aspect ratio is zero.

video699.common.rescale_and_keep_aspect_ratio(original_width, original_height, new_width=None, new_height=None)

Returns new dimensions, and margins that rescale an image and keep its aspect ratio.

Notes

The rescaled image is vertically and horizontally centered and its dimensions do not exceed the new dimensions of the image either vertically or horizontally.

Parameters:
  • original_width (int) – The original width of an image.
  • original_height (int) – The original height of an image.
  • new_width (int) – The new width of the image including the margins. When unspecified or None and the new height is specified, the new height minimizes the margins. When both the new width and the new width are unspecified or None, the new width equals the original width.
  • new_height (int) – The new height of the image including the margins. When unspecified or None and the new width is specified, the new height minimizes the margins. When both the new width and the new height are unspecified or None, the new height equals the original height.
Returns:

  • rescaled_width (int) – The width of the image after rescaling.
  • rescaled_height (int) – The height of the image after rescaling.
  • top_margin (int) – The width of the top margin of the rescaled image.
  • bottom_margin (int) – The width of the bottom margin of the rescaled image.
  • left_margin (int) – The width of the left margin of the rescaled image.
  • right_margin (int) – The width of the right margin of the rescaled image.

Raises:

ValueError – When some of the original or new dimensions is zero.

video699.common.timedelta_as_xsd_duration(timedelta)

Serializes a timedelta object as a string that satisfies the XML Schema duration datatype.

Parameters:timedelta (timedelta) – A timedelta object.
Returns:duration – A string that satisfies the XML Schema duration datatype.
Return type:str
video699.common.xsd_duration_as_timedelta(duration)

Deerializes a timedelta object as a string that satisfies the XML Schema duration datatype.

Parameters:duration (str) – A string that satisfies the XML Schema duration datatype.
Returns:timedelta – A timedelta object.
Return type:timedelta
video699.common.benjamini_hochberg(p_values)

Adjusts p-values from independent hypothesis tests to q-values.

The q-values are determined using the false discovery rate (FDR) controlling procedure of Benjamini and Hochberg [BenjaminiHochberg1995].

[BenjaminiHochberg1995]Benjamini, Yoav; Hochberg, Yosef (1995). “Controlling the false discovery rate: a practical and powerful approach to multiple testing”. Journal of the Royal Statistical Society, Series B. 57 (1): 289–300. MR 1325392.

Notes

This method was adapted from code posted to Stack Overflow by Eric Talevich.

Parameters:p_values (iterable of scalar) – p-values from independent hypothesis tests.
Returns:q_values – The p-values adjusted using the FDR controlling procedure.
Return type:iterable of scalar
video699.common.binomial_confidence_interval(num_successes, num_trials, significance_level)

Computes a Wald confidence interval for the parameter p of a binomial random variable.

Given a sample of Bernoulli trials, we approximate an adjusted Wald confidence interval for the population success probability \(p\) of a binomial random variable using the central limit theorem. The Wald interval was first described by [Simon12] and the adjustment for small samples was proposed by [AgrestiCouli98].

[Simon12]Laplace, Pierre Simon (1812). Théorie analytique des probabilités (in French). p. 283.
[AgrestiCouli98]Agresti, Alan; Coull, Brent A. (1998). “Approximate is better than ‘exact’ for interval estimation of binomial proportions”. The American Statistician. 52: 119–126. doi:10.2307/2685469.
Parameters:
  • num_successes (int) – The number of successful Bernoulli trials in the sample.
  • num_trials (int) – The sample size.
  • significance_level (scalar) – The likelihood that an observation of the random variable falls into the confidence interval.
Returns:

  • pointwise_estimate (scalar) – An unbiased pointwise estimate of the expected value of the binomial random variable.
  • lower_bound (scalar) – The lower bound of the confidence interval.
  • upper_bound (scalar) – The upper bound of the confidence interval.

Raises:

ValueError – If the number of trials is less than or equal to zero, or the number of successes is greater than the number of trials.