API¶

00_Documentation.ipynb¶

This notebooks contains reference documentation for each of NileRedQunat's submodules and functions.

Table of Contents

  • utils.py

    • read_file()
    • fix_strain_dtype()
    • plate_to_list()
    • list_to_plate()
    • alphanumeric_sort_key()
    • map_metadata()
    • default_plot_name()
    • save_plot()
  • qc.py

    • outliers_iqr()
    • outliers_z_score()
    • outliers_madmedianrule()
    • canonical_wells()
    • detect_outliers()
    • normalize_wells()
    • plot_heatmap()
    • plot_histogram()
    • plot_strip_box()
    • plot_qq()
  • analyse.py

    • subtract_background_absorbance()
    • get_fluorescence_signal()
    • signal_biomass_normalisation()
    • analyse()
  • standard_curve.py

    • aggregate_per_group()
    • merge_methods_data()
    • PassingBablok_fit()
    • ODR_fit()
    • pooled_variance()
    • WLS_fit()
    • compute_ci_wls_ols()
    • compute_ci_odr()
    • compute_ci_passing_bablok()
    • compute_r2()
    • plot_standard_curve_with_ci()
    • plot_bland_altman()
    • fit_standard_curve()

utils.py¶

read_file¶


read_file¶

 read_file (filename, sheet_name=None, dtype=None)

Read a CSV/TSV/XLSX file into a DataFrame (first column as index).

Type Default Details
filename str or os.PathLike Path to the input file. Supported extensions (case-insensitive):
.csv, .tsv, .xlsx.
sheet_name NoneType None Excel sheet to read when filename is .xlsx.
Defaults to the first sheet when None.
dtype NoneType None Dtype(s) to enforce. For Excel files, pandas may not be able to
enforce all dtypes on read; this function will attempt a best-effort
post-cast for columns specified in a dtype dict.
Returns pandas.DataFrame DataFrame with the first column used as index.

fix_strain_dtype¶


fix_strain_dtype¶

 fix_strain_dtype (x)

*Normalize a single Strain label to a canonical string.

Numeric-like inputs (e.g., 1, 1.0, '001', '12.0') are converted to their integer string form ('1', '12') when the numeric value is an integer. Non-integer numerics (e.g., '12.5') are returned as their trimmed string. Non-numeric inputs are returned as trimmed strings unchanged.*

Type Details
x Any A single value from the Strain column (scalar).
Returns str or pandas.NA Canonicalized string label. Missing values (NaN/NA) are returned as pandas.NA.

plate_to_list()¶


plate_to_list¶

 plate_to_list (filename, parameter, save=True)

Reshape data from a microtiter plate format to a long format indexed by column Well.

Type Default Details
filename str, os.PathLike or pandas.DataFrame Path to a plate-layout table (CSV/TSV/XLSX; rows = letters A.., cols = 1..N),
or an in-memory DataFrame already in plate layout.
The first column is treated as the row index when reading from file.
parameter str Name for the value column in the long table (e.g., Strain, Abs, FI_bg).
save bool True If True and filename is a path, write <base>_<parameter>_long.csv next to the input.
Returns pandas.DataFrame Long-format DataFrame with index Well (e.g., 'A1', 'B12') and one column parameter.

list_to_plate¶


list_to_plate¶

 list_to_plate (filename, parameter, save=True)

Convert a long-format table with Well IDs back to a microtiter plate layout format.

Type Default Details
filename str, os.PathLike or pandas.DataFrame Either a path to a file loadable by read_file or a long-format
DataFrame. Required columns: Welland a value column named by parameter.
parameter str Name of the value column to pivot (e.g., Strain, Abs, FI_bg).
save bool True If True and filename is a path, write <base>_<parameter>_list_to_plate.csv
next to the input.
Returns plate_df: pandas.DataFrame Plate layout with rows as letters (A..H/P) and columns as integers (1..12/24).
Missing wells appear as NaN.

alphanumeric_sort_key¶


alphanumeric_sort_key¶

 alphanumeric_sort_key (s)

Natural (alphanumeric) sort key that sorts digit numerically (e.g. A1, A2, A10,..). Splits digit from string value.

Type Details
s Any Value to transform into a sort key. Non-strings are
converted to string.
Returns tuple A tuple of parts where digit substrings are converted to integers and
non-digit substrings are case-folded strings. Suitable for use as the
key in sorting.

map_metadata¶


map_metadata¶

 map_metadata (filename, data, save=True, sheet_name=None)

Read a metadata table and align/merge it to data by Well column, in natural (alphanumeric) order.

Type Default Details
filename str or os.PathLike Path to the metadata file accepted by read_file. Must contain a
Well identifier either as a column named Well or as the index
(first column in the file).
data pandas.DataFrame Data to be merged with metadata. Must also contain Well identifiers,
either in a Well column or in the index.
save bool True If True and filename is a path, write the merged table next to
the input as <base>_w_metadata.csv.
sheet_name NoneType None
Returns pandas.DataFrame Merged table, sorted alphanumerically by Well (A1, A2, …, H12 / P24).
The output contains a Well column and all columns from both inputs.

default_plot_name¶


default_plot_name¶

 default_plot_name (plot_name, title=None)

Build a filename from a plot base name and optional title.

Type Default Details
plot_name Any Base name of the plot (e.g., 'Heatmap').
title NoneType None Extra descriptor appended after an underscore (e.g., condition, method).
Returns str A filesystem-friendly filename ending with .pdf.

save_plot¶


save_plot¶

 save_plot (fig, save, plot_name, title, close=True)

Save a figure to PDF using a consistent naming policy & path definition.

Type Default Details
fig matplotlib.figure.Figure or matplotlib.axes.Axes Figure to save. If an Axes is provided, its parent Figure is used.
save bool or str or os.PathLike If True, save to the current working directory using
default_plot_name(plot_name, title). If str or PathLike,
treat as a file path. If the path has no extension, .pdf is appended.
If False, do not save (early return).
plot_name Any Base name for the output file (e.g., Heatmap).
title Any Extra descriptor appended to the base name (e.g., condition/method).
close bool True If True, closes the figure after saving to free memory.
Returns str or None Absolute path to the saved file, or None if save is False.

qc.py¶

outliers_z_score¶


outliers_z_score¶

 outliers_z_score (data, group, columns=None, threshold=1.96, ddof=0)

Detect and separate outliers from specific columns based on per-group Z-score (default threshold 1.96).

Type Default Details
data pandas.DataFrame Input table. Can contain numerical and non-numerical columns.
group str or list Grouping variable(s) to apply for Z-score calculations (e.g. [Condition,Strain])
columns NoneType None List of columns to check for outliers. If None, all numerical columns will be used.
threshold float 1.96 Z-score threshold for defining an outlier.
ddof int 0
Returns tuple of pandas.DataFrame 1. data_wo_outliers: Data with outlier rows removed.
2. outliers: Detected outliers.

outliers_madmedianrule¶


outliers_madmedianrule¶

 outliers_madmedianrule (data, group, columns=None)

Detect and separate outliers from selected columns using per-group MAD-median rule.

Type Default Details
data pandas.DataFrame Input table. Can contain numerical and non-numerical columns.
group str or list Grouping variable(s) to apply MAD-median rule (e.g. [Condition,Strain])
columns NoneType None List of columns to check for outliers. If None, all numerical columns will be used.
Returns tuple of pandas.DataFrame 1. data_wo_outliers: Data with outlier rows removed.
2. outliers: Detected outliers.

detect_outliers¶


detect_outliers¶

 detect_outliers (data, group, method='IQR', columns=None)

Detects outliers based on the provided grouping, outlier detection method ('IQR', 'Z-score' or 'MAD-Median'), and specific columns.

Type Default Details
data pandas.DataFrame Input table. Can contain numerical and non-numerical columns.
group str or list Grouping variable(s). Can be a list of variables.
method str IQR Outlier detection method; either 'IQR', 'Z-score' or 'MAD-Median'. Defaults to 'IQR'.
columns NoneType None List of columns to check for outliers. If None, all numerical columns will be used.
Returns tuple of pandas.DataFrames 1. data: Copy of original data with added Outlier column (boolean).
2. data_wo_outliers: Data with outlier rows removed.
3. outliers: Detected outliers.

canonical_wells¶


canonical_wells¶

 canonical_wells (plate_format)

Create Well positions based on plate format.

normalize_wells¶


normalize_wells¶

 normalize_wells (s)

Uppercase, strip, remove spaces, drop leading zeros in the numeric part.

plot_heatmap¶


plot_heatmap¶

 plot_heatmap (data, columns, plate_format, save=True, annotate=True)

Generate heatmaps in a microtiter plate format for one or more variables.

Type Default Details
data pandas.DataFrame Input table. Required column Well. Can contain numerical and non-numerical columns.
columns str or list of str Name(s) of column(s) to plot.
plate_format {'96', '384'} Plate format used to generate a canonical list of wells (96 or 384).
save bool True If True, save to CWD via utils.save_plot function. If a path, save there.
If False, only show the plot.
annotate bool True Whether to annotate each cell with its value.
Returns matplotlib.figure.Figure Figure containing the heatmaps.

plot_histogram¶


plot_histogram¶

 plot_histogram (data, columns, save=True)

Plot histograms by Condition for one or more numeric variables.

Type Default Details
data pandas.DataFrame Input table. Required columns Condition and the numeric column(s) to plot.
columns str or list of str Name(s) of numeric column(s) to plot.
save bool True If True, save to CWD via utils.save_plot function. If a path, save there.
If False, only show the plot.
Returns matplotlib.figure.Figure Figure containing the histogram plot.

plot_strip_box¶


plot_strip_box¶

 plot_strip_box (data, columns, exclude_strains=None, save=True)

Generate Strip + box plots by Strain and Condition, with optional strain exclusion. strip plots with either box or violin plots overlaid. Highlights outliers with red border.

Type Default Details
data pandas.DataFrame Input table. Required columns Condition, Strain and the numeric column(s) to plot.
columns str or list of str Name(s) of numeric column(s) to plot on the y-axis.
exclude_strains NoneType None Strain label(s) to exclude from plotting.
save bool True If True, save to CWD via :func:utils.save_plot. If a path, save there.
If False, only show the plot.
Returns matplotlib.figure.Figure Figure containing the strip plots.

plot_qq¶


plot_qq¶

 plot_qq (data, columns, save=True)

Generate Q-Q plots per Condition for the requested numeric column(s).

Type Default Details
data pandas.DataFrame Input table. Required columns Condition and the numeric columns to test.
columns str or list of str Name(s) of numeric column(s) to plot.
save bool True If True, save to CWD via :func:utils.save_plot. If a path, save there.
If False, only show the plot.
Returns matplotlib.figure.Figure Figure containing the Q-Q plots.

analyse.py¶

subtract_background_absorbance¶


subtract_background_absorbance¶

 subtract_background_absorbance (data, blanks, absorbance_column='Abs',
                                 contamination_thr=0.2)

Subtract background absorbance within each Condition group.

Type Default Details
data pandas.DataFrame Input table. Required columns: absorbance column, Condition & Strain ( only when blank is a string label).
blanks
absorbance_column str Abs
contamination_thr float 0.2 Threshold for flagging/removing contaminated blank wells when using a
string label (rows with Abs >= contamination_thr are dropped before
computing per-condition blank means.

get_fluorescence_signal¶


get_fluorescence_signal¶

 get_fluorescence_signal (data, fi_bg_col='FI_bg', fi_fp_col='FI_fp')

Get Fluorescence signal by subtracting the background fluorescence from probe fluorescence.

Type Default Details
data pandas.DataFrame Input table. Required columns: background fluorescence intensity (e.g. FI_bg) & fluorescence intensity of the fluorescent probe (e.g. FI_fp).
fi_bg_col str FI_bg
fi_fp_col str FI_fp
Returns data: pandas.DataFrame Copy of data with a new column Fluorescence, representing subtracted background fluorescence intensity. Non-numeric entries (e.g., 'INVALID')
are coerced to missing values.

signal_biomass_normalisation¶


signal_biomass_normalisation¶

 signal_biomass_normalisation (data)

Normalise the Fluorescence Intensity (Fluorescence) by Biomass (Absorbance) and apply log transform.

Type Details
data pandas.DataFrame Input table. Required columns: Absorbance (biomass proxy with subtracted blank) and Fluorescence (background-subtracted fluorescence).
Returns data: pandas.DataFrame Copy of data with two added columns:Lipids = (FI/A), representing biomass normalised fluorescence intensity and Log(Lipids), representing
natural logarithm of Lipids variable.

analyse¶


analyse¶

 analyse (filename, blanks, absorbance_column='Abs',
          contamination_thr=0.2, fi_bg_col='FI_bg', fi_fp_col='FI_fp',
          outlier_method='IQR', outlier_columns=None, save=True)

Automated analysis of fluorescence data. 1) read file, 2) subtract background absorbance, 3) subtract background fluorescence, 4) normalize by biomass & log, 5) detect and drop outliers.

Type Default Details
filename str or os.PathLike Path to the input file accepted by function utils.read_file.
blanks float or str Background specification for absorbance subtraction passed to
function subtract_background_absorbance
absorbance_column str Abs
contamination_thr float 0.2 Threshold for flagging/removing contaminated blank wells when using a
string label.
fi_bg_col str FI_bg
fi_fp_col str FI_fp
outlier_method str IQR Outlier detection method; either 'IQR', 'Z-score' or 'MAD-Median'. Defaults to 'IQR'.
outlier_columns NoneType None
save bool True If True, save the normalized + outlier-flagged table and the outliers
table next to filename (CSV). If False, do not write files.
Returns tuple of pandas.DataFrames 1. data: Copy of original data with added Absorbance, Fluorescence,Lipids, Log(Lipids) and Outlier (boolean) columns and removed blank wells.
2. data_wo_outliers: Data with outlier rows removed.
3. outliers: Detected outliers.

standard_curve.py¶

aggregate_per_group¶


aggregate_per_group¶

 aggregate_per_group (df, value_column)

Aggregate data per group (Strain × Condition) with mean, Standard deviation (sd), size (n), standard error of mean (sem) & variance (var).

Type Details
df pandas.DataFrame Input table. Required columns Condition, Strain, and
the numeric column specified by value_column.
value_column str Name of the column to aggregate.
Returns pandas.DataFrame Aggregated table with one row per (Condition × Strain) and columns:
<value>_mean, <value>_sd, <value>_n, <value>_se,
<value>_var.

merge_methods_data()¶


merge_methods_data¶

 merge_methods_data (df_m1, df_m2, m1_column, m2_column)

Intersect aggregated data from two methods at (Condition × Strain).

Type Details
df_m1 pandas.DataFrame Input table for method 1. Required columns Condition, Strain, and m1_column.
df_m2 pandas.DataFrame Input table for method 2. Required columns Condition, Strain, and m2_column.
m1_column str Name of the numeric value column in df_m1 to aggregate.
m2_column str Name of the numeric value column in df_m2 to aggregate.
Returns pandas.DataFrame Inner join of the two per-group aggregates on (Condition and Strain).
Column names follow the pattern produced by function aggregate_per_group.

PassingBablok_fit¶


PassingBablok_fit¶

 PassingBablok_fit (x, y)

Fit Passing–Bablok regression for the model: y = a + b x. x, y are per-level means (one point per Strain × Condition).

Type Details
x
y
Returns float Intercept (a) and slope (b).

ODR_fit¶


ODR_fit¶

 ODR_fit (x, y, sx=None, sy=None, lam=None, alpha=0.05)

*Fit ODR regression form model: y = a + b x.

  • If sx & sy (per-point SDs) are provided → use them.
  • Else if lam (lambda = Var_x / Var_y) is provided → Deming via x-scaling.
  • Else → equal-error total least squares.*
Type Default Details
x
y
sx NoneType None
sy NoneType None
lam NoneType None Ratio Var_x / Var_y for Deming regression. When set (and sx/sy
are not provided), implements Deming by scaling x by
s = sqrt(lam) and performing ODR with equal errors.
alpha float 0.05 Significance level for the two-sided confidence interval. Must satisfy
0 < alpha < 1. The returned band has nominal coverage 1 - alpha.
Returns float, dict Intercept (a) and slope (b). Pointwise 95% CI for the mean line with keys:
{"xg", "yline", "lo", "hi"} as produced by compute_ci_odr function.

pooled_variance¶


pooled_variance¶

 pooled_variance (variance, n)

Compute the pooled within-group variance across groups.

Type Details
variance array-like Per-group variances (ideally sample variances with ddof=1).
n array-like Per-group sample sizes.
Returns float or None The pooled variance. Returns None if no valid groups are found
(i.e., after filtering to finite values with n >= 2) or if the
denominator is zero.

WLS_fit¶


WLS_fit¶

 WLS_fit (x, y, variance, n, condition, alpha=0.05)

Fit the linear model y = a + b x via Weighted Least Squares (WLS), when weights are available or sufficient.

Type Default Details
x
y
variance array-like or None Per-group variance of the response. If provided, WLS is attempted using weights w = n / variance.
n array-like Per-group replicate counts corresponding to variance. Used only to form weights; values can be 1 or larger.
condition str Label used in warning messages to identify the subset being fitted.
alpha float 0.05 Significance level for the two-sided confidence interval. Must satisfy
0 < alpha < 1. The returned band has nominal coverage 1 - alpha.
Returns float, dict, {"wls", "ols"} Intercept (a) and slope (b). Pointwise 95% CI for the mean line with keys:
{"xg", "yline", "lo", "hi"} as produced by compute_ci_wls_ols function.
method_used - The fitting method actually used.

compute_ci_wls_ols¶


compute_ci_wls_ols¶

 compute_ci_wls_ols (results, x, alpha=0.05, num=200)

Compute point-wise (1 - alpha) CI for fitted mean line y = a + b x from OLS/WLS fit.

Type Default Details
results statsmodels.regression.linear_model.RegressionResults Fitted results object (e.g., from statsmodels.api.OLS or
statsmodels.api.WLS).
x array-like Original predictor values used to determine the plotting grid.
alpha float 0.05 Significance level for the two-sided confidence interval. Must satisfy
0 < alpha < 1. The returned band has nominal coverage 1 - alpha.
num int 200 Number of grid points between min(x) and max(x) when
min(x) < max(x). If all x are equal, a single-point grid is used.
Returns dict Pointwise 95% CI for the mean line with keys: {"xg", "yline", "lo", "hi"}. xg (grid of x), yline (fitted mean line at xg), lo (lower CI), hi (upper CI).

compute_ci_odr¶


compute_ci_odr¶

 compute_ci_odr (output, x, alpha=0.05, num=200, scale_x=1.0)

Compute point-wise (1 - alpha) Confidence interval (CI) for fitted line y = a + b x from ODR fit.

Type Default Details
output scipy.odr.Output Fitted results object (e.g., from statsmodels.api.OLS or
statsmodels.api.WLS).
x array-like Original predictor values used to determine the plotting grid.
alpha float 0.05 Significance level for the two-sided confidence interval. Must satisfy
0 < alpha < 1. The returned band has nominal coverage 1 - alpha.
num int 200 Number of grid points between min(x) and max(x) when
min(x) < max(x). If all x are equal, a single-point grid is used.
scale_x float 1.0
Returns dict Pointwise 95% CI for the mean line with keys: {"xg", "yline", "lo", "hi"}. xg (grid of x), yline (fitted mean line at xg), lo (lower CI), hi (upper CI).

compute_ci_passing_bablok¶


compute_ci_passing_bablok¶

 compute_ci_passing_bablok (x, y, a, b, alpha=0.05, B=1000, num=200,
                            random_state=7, pb_fit=None)

Compute point-wise (1 - alpha) Confidence interval (CI) for fitted line y = a + b x from Passing–Bablok fit.

Type Default Details
x
y
a
b
alpha float 0.05 alpha : float, default 0.05
Significance level for the two-sided confidence interval. Must satisfy
0 < alpha < 1. The returned band has nominal coverage 1 - alpha.
B int 1000 Number of bootstrap resamples.
num int 200 Number of grid points between min(x) and max(x) when
min(x) < max(x). If all x are equal, a single-point grid is used.
random_state int 7 Seed for the bootstrap RNG (NumPy PCG64).
pb_fit NoneType None Callable implementing the Passing–Bablok estimator with signature
pb_fit(x, y) -> (a_hat, b_hat). Defaults to PassingBablok_fit.
Returns dict Pointwise 95% CI for the mean line with keys: {"xg", "yline", "lo", "hi"}. xg (grid of x), yline (fitted mean line at xg), lo (lower CI), hi (upper CI).

compute_r2¶


compute_r2¶

 compute_r2 (x, y, a, b, w=None)

Compute Coefficient of determination (R²) between observed y and fitted ŷ = a + b x, with optional sample weights.

Type Default Details
x
y
a
b
w NoneType None Non-negative sample weights.
Returns float R² in [-inf, 1] for valid inputs; np.nan when undefined.

plot_standard_curve_with_ci¶


plot_standard_curve_with_ci¶

 plot_standard_curve_with_ci (x, y, a, b, ci, method_used, xlabel=None,
                              ylabel=None, title=None, point_labels=None,
                              weights=None, show_r2=True, save=True)

Plot a standard-curve scatter, fitted line, and pointwise CI band.

Type Default Details
x
y
a
b
ci dict Confidence-band dictionary with keys:
{"xg", "yline", "lo", "hi"}.
method_used str Label of the regression method shown in the plot title.
xlabel NoneType None
ylabel NoneType None
title NoneType None Additional title line placed under the method label.
point_labels NoneType None Text labels to annotate points.
weights NoneType None Sample weights for R² (passed to compute_r2).
show_r2 bool True If True, append R² to the line legend.
save bool True If True, save via utils.save_plot to the CWD. If a path, save
there. If False, show the figure and do not save.
Returns matplotlib.axes.Axes The Axes object containing the plot.

plot_bland_altman¶


plot_bland_altman¶

 plot_bland_altman (m1, m2, title=None, point_labels=None, xlabel=None,
                    ylabel=None, save=True, alpha=0.05, agreement=1.96,
                    **kwargs)

Plot Bland–Altman (difference vs. mean) of two methods.

Type Default Details
m1
m2
title NoneType None Subtitle displayed under the main title (“Bland–Altman”).
point_labels NoneType None Text labels to annotate individual points.
xlabel NoneType None
ylabel NoneType None
save bool True If True, save via :func:utils.save_plot to the CWD. If a path, save there.
If False, show the figure and do not save.
alpha float 0.05 Two-sided significance level for mean/LoA confidence intervals.
The plotted envelope uses confidence = 1 - alpha.
agreement float 1.96 Multiplier for limits of agreement (e.g., 1.96 ≈ 95% for normal errors).
kwargs VAR_KEYWORD
Returns matplotlib.axes.Axes Axes containing the Bland–Altman plot.

fit_standard_curve¶


fit_standard_curve¶

 fit_standard_curve (df_method1, df_method2, m1_column, m2_column,
                     method='auto', alpha=0.05, min_levels=3,
                     min_fraction_with_se=0.5, plot=True, save=True)

*Calibrate a standard curve per Condition and predict values based on calibration method (Method 2).

Workflow 1) Aggregate both methods by (Condition × Strain) and keep only overlapping controls via merge_methods_data. 2) Choose the fitting method:

- `"wls"`: WLS with weights `w = n / variance`.
- `"odr"`: ODR with per-point `sx, sy` if enough groups; else Deming with
pooled variance ratio `lambda = Var_x / Var_y`; else WLS and fall back to Passing–Bablok.
- `"ols"`: OLS.
- `"pb"`: Passing–Bablok directly.
- `"auto"`: Auto tries ODR(sx,sy) → Deming (λ) → WLS(y|x) if y-variance available → PB;
x = (y-a)/b.

3) Compute pointwise 95% CI for the fitted mean line. 4) Predict values for all rows of df_method1 within each Condition as Lipids_predicted = max(0, a + b·m1). 5) (Optional) Plot standard curve + CI and a Bland–Altman agreement plot. 6) Collect agreement metrics (MSE, MAE, RMSE, R²) per-Condition for comparison to the reference method.*

Type Default Details
df_method1
df_method2
m1_column
m2_column
method str auto Fitting strategy.
alpha float 0.05 Significance level for the two-sided confidence interval. Must satisfy
0 < alpha < 1. The returned band has nominal coverage 1 - alpha.
min_levels int 3 Minimum number of control levels (rows) required to fit a model in a Condition.
min_fraction_with_se float 0.5 Minimum fraction of groups with per-point SE available (both methods) to
allow ODR with sx/sy (when method='odr' or 'auto').
plot bool True If True, produce the calibration/standard curve plot and Bland–Altman plot per Condition.
save bool True
Returns pandas.DataFrames 1. Copy of df_method1 with added columns: Calibration_Method, a, b, Lipids_predicted.
2. Per-Condition agreement metrics (MSE, MAE, RMSE, R²).