rxnfit.fit_metrics module — rxnfit 20260310 documentation

# rxnfit.fit_metrics module

Compute RSS, TSS, and R² for model vs. experimental data.

TSS uses per-species means over the same valid points used for RSS. All functions use the same valid-point definition (NaN excluded).

rxnfit.fit_metrics.expdata_df_to_datasets(expdata_df, function_names)

  Convert a single experimental DataFrame to datasets format.

  Parameters:

    - expdata_df (pandas.DataFrame) – Time in first column, species concentrations in remaining columns. Column names must include all function_names.

    - function_names (list[str]) – Chemical species names (e.g. ODE order).

  Returns:

    One-element list of dicts with keys ‘t_list’, ‘C_exp_list’

      (aligned to function_names order; valid points only, NaN removed).

  Return type:

    list

  Raises:

    ValueError – If a species in function_names is not in the DataFrame.

rxnfit.fit_metrics.fit_metrics(datasets, rss)

  Compute fit metrics (RSS, TSS, R²) from datasets and RSS.

  TSS is computed from the same valid points as RSS, using per-species means. R² = 1 - RSS/TSS.

  Parameters:

    - datasets (list[dict]) – List of dicts with ‘t_list’, ‘C_exp_list’ (each list of arrays in species order; valid points only).

    - rss (float) – Residual sum of squares (already computed).

  Returns:

    Keys ‘rss’, ‘tss’, ‘r2’ (all float). If TSS is zero or very

      small, r2 may be inf or extreme; callers should warn when tss < TSS_MIN_THRESHOLD.

  Return type:

    dict