API Reference

Single patient

seminal.azoospermia_fsh(fsh, clip=True)[source]

Probability of azoospermia from serum FSH alone (quadratic model).

Parameters:
  • fsh (float or array-like) – Serum follicle-stimulating hormone in IU/L.

  • clip (bool, default True) – Clip the result to [0, 1]. The published quadratic exceeds 100% above roughly 30 IU/L and is unbounded; the paper’s own validation tables carry unclipped values above 1. Set clip=False to reproduce that behaviour exactly.

Returns:

Probability between 0 and 1 (a fraction, not a percent).

Return type:

float or numpy.ndarray

Notes

The curve is U-shaped: 10.1% at undetectable FSH, a minimum of about 8.4% at FSH 3.6 IU/L, and 100% at about 29.9 IU/L.

Examples

>>> round(azoospermia_fsh(0), 3)
0.101
>>> round(azoospermia_fsh(20), 3)
0.439
seminal.azoospermia_lr(age, fsh, lh, testosterone, testis_volume, explain=False)[source]

Probability of azoospermia from the logistic regression model.

Parameters:
  • age (float) – Age in years.

  • fsh (float) – Serum FSH in IU/L.

  • lh (float) – Serum luteinizing hormone in IU/L.

  • testosterone (float) – Total testosterone in ng/dL. (Divide nmol/L by 0.03467 to convert.)

  • testis_volume (float) – Mean of left and right testis volume in cm^3 (mL), as estimated by Prader orchidometer in the development cohort.

  • explain (bool, default False) – Return a breakdown instead of just the probability.

Returns:

Probability between 0 and 1. With explain=True, a dict with risk, intercept, linear_predictor (log-odds) and contributions (each predictor’s log-odds term, largest first).

Return type:

float or dict

Notes

The paper prints the model form as 1/exp[-(b0 + bi*Xi)], which is a typesetting error (it would exceed 1). The standard logistic form 1/(1 + exp(-(b0 + sum(bi*Xi)))) is used here.

Examples

>>> round(azoospermia_lr(age=36, fsh=5.7, lh=4.5, testosterone=429,
...                      testis_volume=15), 3)
0.086

Cohorts

seminal.add_azoospermia_risk_cols(df, age_col='age', fsh_col='fsh', lh_col='lh', testosterone_col='testosterone', testis_volume_col='testis_volume', lr_col='azoo_lr', fsh_model_col='azoo_fsh', copy=True)[source]

Add both azoospermia probabilities to every row of a DataFrame.

Parameters:
  • df (pandas.DataFrame) – One row per semen analysis / hormone panel.

  • age_col (str) – Column names for the five logistic-model inputs. Only fsh_col is needed for the FSH-only model.

  • fsh_col (str) – Column names for the five logistic-model inputs. Only fsh_col is needed for the FSH-only model.

  • lh_col (str) – Column names for the five logistic-model inputs. Only fsh_col is needed for the FSH-only model.

  • testosterone_col (str) – Column names for the five logistic-model inputs. Only fsh_col is needed for the FSH-only model.

  • testis_volume_col (str) – Column names for the five logistic-model inputs. Only fsh_col is needed for the FSH-only model.

  • lr_col (str, default "azoo_lr") – Name of the logistic regression probability column.

  • fsh_model_col (str, default "azoo_fsh") – Name of the FSH-only quadratic probability column.

  • copy (bool, default True) – Work on a copy and leave df unchanged.

Returns:

df (or a copy) with the two probability columns added. A row with any logistic-model input missing gets NaN in lr_col; the FSH-only column needs only FSH.

Return type:

pandas.DataFrame

WHO 2021 semen reference distributions

seminal.semen_percentile(parameter: str, value, edition: str = 'who2020')[source]

Where a value sits in the fertile-man distribution, in percent.

Linear interpolation between the published centiles. Values below the 2.5th centile return 2.5 and values above the 97.5th return 97.5, since the tails are not published. The result is therefore an approximation of the empirical percentile, exact only at the nine published points.

Parameters:
  • parameter (str) – One of PARAMETERS.

  • value (float or array-like) – The measured value, in the parameter’s published units.

  • edition ({"who2020", "2010_2020"}, default "who2020")

Returns:

Percentile between 2.5 and 97.5; NaN for NaN input.

Return type:

float or numpy.ndarray

Examples

>>> semen_percentile("concentration", 16)
5.0
>>> round(semen_percentile("concentration", 66), 1)
50.0
seminal.lower_reference_limit(parameter: str, edition: str = 'who2020') → float[source]

The 5th centile (“lower reference limit”) for one parameter.

edition="who2010" returns the 5th-edition limit (Cooper et al. 2010).

seminal.below_reference(parameter: str, value, edition: str = 'who2020')[source]

True where value is below the lower reference limit (5th centile).

Returns a bool, or a boolean/object array for array input (NaN stays NaN).

seminal.semen_reference(parameter: str | None = None, edition: str = 'who2020') → DataFrame[source]

The published reference distribution as a DataFrame.

Parameters:
  • parameter (str, optional) – One of PARAMETERS; omit for every parameter.

  • edition ({"who2020", "2010_2020"}, default "who2020") – Table 3 (WHO 2020 combined data) or Table 2 (2010-2020 studies only) of Campbell et al. 2021.

Returns:

One row per parameter: label, n, the nine centile columns (p2.5 … p97.5) and p5_ci_low / p5_ci_high.

Return type:

pandas.DataFrame

seminal.add_semen_percentile_cols(df, columns: dict | None = None, edition: str = 'who2020', suffix: str = '_pct', flag_suffix: str | None = '_below_lrl', copy: bool = True)[source]

Add percentile (and optional below-limit flag) columns to a DataFrame.

Parameters:
  • df (pandas.DataFrame) – One row per semen analysis.

  • columns (dict, optional) – Mapping of parameter name to column name, e.g. {"concentration": "conc", "progressive_motility": "pr"}. Defaults to the parameter names themselves for any that are present in df.

  • edition ({"who2020", "2010_2020"}, default "who2020")

  • suffix (str, default "_pct") – Appended to the column name for the percentile column.

  • flag_suffix (str or None, default "_below_lrl") – Appended for the boolean below-5th-centile column; None to skip.

  • copy (bool, default True) – Work on a copy and leave df unchanged.

Return type:

pandas.DataFrame

Constants

seminal.azoospermia.LR_COEFS

Table 3 coefficients on the log-odds scale: intercept, age, fsh, lh, testosterone, testis_volume.

seminal.azoospermia.FSH_QUAD

Fig. 1 quadratic in percent: a (FSH²), b (FSH), c (constant).

seminal.semen.PARAMETERS

The nine semen parameters and their labels.

seminal.semen.CENTILES

The nine published centiles: 2.5, 5, 10, 25, 50, 75, 90, 95, 97.5.

seminal.azoospermia.REFERENCE_RANGES

Reference limits quoted in the paper for FSH, LH, total testosterone and mean testis volume. Informational only; the models do not threshold on them.