Data and fitting#

class bindcurve.DoseResponseData(table)[source]#

Long-form dose-response observations, one row per measured response.

Parameters:

table (pandas.DataFrame) – Columns compound_id, concentration and response are required. experiment_id identifies independent experiments and defaults to a single experiment. Other columns are kept.

Notes

Concentrations must be finite and positive and share one unit. Rows with the same compound, experiment and concentration are technical replicates.

classmethod from_wide(table, *, prefix='response_')[source]#

Create data from one row per concentration with replicate columns.

Parameters:
  • table (DataFrame) – Columns compound_id and concentration, optionally experiment_id, and one column per technical replicate. No other columns are allowed.

  • prefix (str) – Start of the names of the replicate response columns. Missing replicate values are ignored.

Return type:

DoseResponseData

classmethod from_csv(path, *, format='long', prefix='response_', **read_csv_kwargs)[source]#

Read a long or wide CSV file.

Parameters:
  • path (str | Path) – CSV file to read.

  • format (Literal['long', 'wide']) – "long" for one row per response, as in DoseResponseData, or "wide" for one row per concentration, as in from_wide.

  • prefix (str) – Start of the names of the replicate response columns of wide files.

  • **read_csv_kwargs (object) – Passed to pandas.read_csv, e.g. sep=";".

Return type:

DoseResponseData

property table: DataFrame#

A copy of the validated observation table.

property compounds: list[str]#

Sorted compound identifiers.

select(compounds)[source]#

Return the observations of the given compound or compounds.

Parameters:

compounds (str | Iterable[str]) – One compound ID or several.

Raises:

KeyError – If a compound is not in the data.

Return type:

DoseResponseData

summary()[source]#

One row per compound with counts and the concentration range.

Columns are compound_id, N_exp (independent experiments), N_obs (observations, counting technical replicates), concentration_min and concentration_max.

Return type:

DataFrame

bindcurve.fit(data, model, *, fixed=None, bounds=None, errors='raise')[source]#

Fit a model separately to every experiment of every compound.

Technical replicates are averaged per concentration, each mean is weighted by its replicate count, and standard errors are scaled by the residual scatter.

Parameters:
  • data (DoseResponseData) – Observations to fit.

  • model (str | Model) – A built-in model name such as "ic50" (see get_model) or a model.

  • fixed (Mapping[str, float] | None) – Values held fixed in every fit. Assay constants of binding models, such as RT, LsT and Kds, must be given here.

  • bounds (Mapping[str, tuple[float | None, float | None]] | None) – {name: (lower, upper)} limits of fitted parameters; None leaves a side unbounded.

  • errors (Literal['raise', 'collect']) – Re-raise errors, or record them as failed fits and continue.

Returns:

One fit per compound and experiment.

Return type:

FitResults

Raises:
  • KeyError – If fixed or bounds name a parameter the model does not have.

  • ValueError – If an assay constant is missing, or a fixed value or bound is invalid.

Warns:

UserWarning – If converged fits carry quality warnings, see FitResult.warnings.

class bindcurve.FitResults(model, data, fixed, fits)[source]#

Experiment-level fits of one model to a dataset.

Returned by fit.

model#

The fitted model.

Type:

bindcurve.models.base.Model

data#

The fitted observations.

Type:

bindcurve.data.DoseResponseData

fixed#

Parameter values fixed in every fit.

Type:

dict[str, float]

fits#

One result per compound and experiment, including failed fits.

Type:

tuple[bindcurve.results.FitResult, …]

property free: list[str]#

Names of the fitted parameters.

experiments()[source]#

One row per experiment-level fit with estimates and diagnostics.

Columns are compound_id, experiment_id, success, message, warnings, n_data, rss and r_squared (see FitResult), then each fitted parameter and its standard error (Kd, Kd_stderr, …).

Return type:

DataFrame

summary()[source]#

One row per compound summarizing successful fits across experiments.

Each independent experiment contributes one value per parameter. N_fit, N_failed and N_flagged count the successful, failed and flagged (successful but with warnings) fits.

Concentration parameters are summarized on the log10 scale. For IC50, the columns are IC50 (geometric mean), logIC50, logIC50_SD, logIC50_SEM, and IC50_CI95_lower and IC50_CI95_upper (Student-t 95% confidence interval, back-transformed).

Other parameters get the arithmetic mean and the same statistics on the linear scale, e.g. hill_slope, hill_slope_SD, hill_slope_SEM, hill_slope_CI95_lower and hill_slope_CI95_upper.

SD, SEM and the interval are NaN for a compound with fewer than two successful fits.

Return type:

DataFrame

parameters(compound_id)[source]#

Summary parameter values of one compound, ready for Model.evaluate.

Fitted parameters take their summary centers (geometric means for concentrations); fixed parameters keep their fixed values.

Parameters:

compound_id (str) – Compound to return.

Raises:
  • KeyError – If the compound is not in the data.

  • ValueError – If the compound has no successful fits.

Return type:

dict[str, float]

report(parameter=None, *, uncertainty='sd', log=False, digits=2, unit=None)[source]#

Format one concentration parameter per compound for publication.

Parameters:
  • parameter (str | None) – Concentration parameter to report. Defaults to the only fitted concentration parameter.

  • uncertainty (Literal['sd', 'sem', 'ci95']) – Spread across independent experiments: "sd", "sem" or "ci95". On the linear scale, every choice is shown as a back-transformed range, e.g. 10**(mean ± SD) for "sd".

  • log (bool) – Report log10 values, e.g. -6.12 ± 0.15, instead of linear ones.

  • digits (int) – Significant figures for linear values, decimals for log values.

  • unit (str | None) – Unit appended to linear values.

Returns:

Columns compound_id, report (the formatted text), N_fit, N_failed and N_flagged.

Return type:

pandas.DataFrame

class bindcurve.FitResult(compound_id, experiment_id, model, success, message='', values=<factory>, stderr=<factory>, free=(), covariance=None, n_data=None, rss=None, r_squared=None, warnings=())[source]#

Fit of one model to one experiment of one compound.

Created by fit and stored in FitResults.fits.

compound_id#

The fitted compound.

Type:

str

experiment_id#

The fitted experiment.

Type:

str

model#

The fitted model.

Type:

bindcurve.models.base.Model

success#

Whether the optimizer converged. A failed fit has no values.

Type:

bool

message#

Optimizer message, or the error of a failed fit.

Type:

str

values#

All parameter values, fitted and fixed. Empty for a failed fit.

Type:

dict[str, float]

stderr#

Standard errors of the fitted parameters, when they could be estimated.

Type:

dict[str, float]

free#

Names of the fitted parameters, in the order of covariance.

Type:

tuple[str, …]

covariance#

Covariance matrix of the fitted parameters, scaled by the residual variance; None when it could not be estimated.

Type:

numpy.ndarray | None

n_data#

Number of fitted concentrations (replicate means).

Type:

int | None

rss#

Residual sum of squares of the replicate means.

Type:

float | None

r_squared#

Coefficient of determination of the replicate means; None when the means are all equal.

Type:

float | None

warnings#

Reasons why a converged fit may not be supported by the data.

Type:

tuple[str, …]

predict(x)[source]#

Evaluate the fitted curve at concentrations x.

Parameters:

x (ndarray) – Concentrations on the linear scale, x >= 0.

Return type:

ndarray