Module statkit.views

Functions

def format_p_value(number: float,
latex: bool = True,
symbol: str | None = None,
format: Literal['scientific', 'compact'] = 'scientific') ‑> str
Expand source code
def format_p_value(
    number: float,
    latex: bool = True,
    symbol: Optional[str] = None,
    format: Literal["scientific", "compact"] = "scientific",
) -> str:
    r"""Format p-value with two significant digits as string except when ≥ 0.1.

    Args:
        number: Floating point number to format.
        latex: Format string as LaTeX math (with enclosing $ characters).
        symbol: When not `None` but, e.g., "p" it prints "p = number".
        format: `scientific`, represent p-value with two significant digits,
            unless >= 0.1; `compact`, uses the representation (floating point or
            scientific notation) with the least number of decimals including the minus
            sign.

    Returns:
        A string representation of the number.

    Example:
        ```python
            >>> print(format_p_value(0.0012, symbol='p'))
            $p = 1.2 \cdot 10^{-3}$
        ```
    """
    if format == "scientific":
        number_str = _format_scientific(number, latex)
    elif format == "compact":
        number_str = _format_compact(number, latex)

    if symbol:
        if latex:
            return r"${} = {}$".format(
                symbol,
                number_str.removeprefix("$").removesuffix("$"),
            )
        return f"{symbol} = {number_str}"

    return number_str

Format p-value with two significant digits as string except when ≥ 0.1.

Args

number
Floating point number to format.
latex
Format string as LaTeX math (with enclosing $ characters).
symbol
When not None but, e.g., "p" it prints "p = number".
format
scientific, represent p-value with two significant digits, unless >= 0.1; compact, uses the representation (floating point or scientific notation) with the least number of decimals including the minus sign.

Returns

A string representation of the number.

Example

    >>> print(format_p_value(0.0012, symbol='p'))
    $p = 1.2 \cdot 10^{-3}$