On this page Execution sequence

API Reference / mespy.median

Implementation

This page describes the internal flow of median and the order in which validation and the final median calculation are applied. Unlike Overview, the goal here is not to repeat the function signature, but to show how the result is actually built.

The following snippets are taken from the current src/mespy/stats_utils.py implementation. The only helper involved is _as_float_vector, which defines the numeric input contract before the final delegation to NumPy.

Execution sequence

The implementation follows this sequence:

  1. Converts x into a one-dimensional, finite float64 vector with _as_float_vector("x", x).

  2. Rejects empty, multidimensional, or non-finite inputs directly in the shared validator.

  3. Passes the validated vector to np.median(...).

  4. Converts the final result to a Python float.

Validation and delegation to NumPy

def median(x: ArrayLike) -> float:
    values = _as_float_vector("x", x)
    return float(np.median(values))

median is the most straightforward function in the whole module.

  • It contains no alternative branches, optional parameters, or hand-rewritten formulas.

  • All the robustness of the API is concentrated in the first line: _as_float_vector(...) requires that x is a numerical array, one-dimensional, not empty and composed only of finite values.

  • Once values is obtained, the function does not add any other statistical logic: it delegates the calculation of the median to np.median(values).

  • The final conversion to float makes the public API return type consistent and avoids exposing a NumPy scalar.

Edge cases and practical behavior

This feature does not implement special policies beyond initial validation.

  • If x is empty, the failure occurs before calling np.median(...), so the package avoids warning or silent nan.

  • If x contains NaN or infinities, the rejection always occurs in the common validator, in a manner consistent with all other functions of stats_utils.

  • If x has an even number of elements, the choice on how to combine the two central values is entirely determined by NumPy, because mespy does not redefine it.

  • Precisely for this minimal structure, the behavior of median is easy to read: common validation at the input, then single delegation to the underlying numerical engine.