Cals Module

minor functions for quick calculations and conversions

SciKit.cals.Pq(q, Rg, nu)[source]

Compute the excluded-volume (generalized Gaussian coil) form factor P(q) following the Hammouda model.

The form factor is expressed in terms of the lower incomplete gamma function and depends on the radius of gyration Rg and the Flory exponent nu:

U = q^2 * Rg^2 * (2*nu + 1) * (2*nu + 2) / 6

Parameters:
  • q (array-like) – Scattering vector magnitudes (1/Angstrom).

  • Rg (float) – Radius of gyration of the polymer chain (Angstrom).

  • nu (float) – Flory exponent (0 < nu <= 1); nu = 0.5 for ideal chain, nu ~ 0.588 for self-avoiding walk in good solvent.

Returns:

result – Form factor P(q), normalized so that P(0) = 1.

Return type:

ndarray

SciKit.cals.cal_csat(number, box_length, droplet_radius=0)[source]

Calculate the saturation concentration of the dilute phase in a simulation box containing a spherical droplet.

The dilute-phase volume is the total box volume minus the droplet volume:

V_dilute = box_length^3 - (4/3) * pi * droplet_radius^3

Parameters:
  • number (int or float) – Number of molecules in the dilute phase.

  • box_length (float) – Side length of the cubic simulation box in Angstroms.

  • droplet_radius (float, optional) – Radius of the spherical droplet in Angstroms. Default is 0 (no droplet).

Returns:

csat – Saturation concentration of the dilute phase in micromolar (uM).

Return type:

float

SciKit.cals.droplet(r, c)[source]

Fit a radial density profile to a hyperbolic tangent interface model to extract droplet properties such as the radius and interfacial width.

The model follows Rowlinson & Widom (1982) for a planar-like interface mapped onto a spherical geometry:

c(r) = (cin + cout)/2 - (cin - cout)/2 * tanh((r - r0) / d)

where cin and cout are the interior and exterior densities, r0 is the equimolar dividing surface radius, and d is the interfacial width.

Parameters:
  • r (array-like) – Radial distances from the droplet centre (Angstrom).

  • c (array-like) – Density (or concentration) values at each radial position.

Returns:

infloat

Fitted density inside the droplet (cin).

outfloat

Fitted density outside the droplet (cout).

r0float

Fitted equimolar radius of the droplet (Angstrom).

dfloat

Fitted interfacial width (Angstrom); smaller values indicate a sharper interface.

x_fitndarray

Uniformly spaced radial values over [0.9*r.min(), 1.1*r.max()] used to evaluate the fitted curve.

y_fitndarray

Fitted density profile evaluated at x_fit.

Return type:

dict with keys

SciKit.cals.general_fit(func, xs, ys, yerrs, xrange, p0=None, bounds=(-inf, inf), maxfev=10000)[source]

Fit an arbitrary model function to data with uncertainties and compute the fitted curve along with a propagated confidence interval.

The confidence band is obtained via numerical Jacobian propagation:

var(y_fit) = J @ cov @ J^T (diagonal elements)

where J is the Jacobian of func with respect to the fit parameters, evaluated at each point in xfit.

Parameters:
  • func (callable) – Model function with signature func(x, *params) -> array-like.

  • xs (array-like) – Independent variable data points.

  • ys (array-like) – Dependent variable data points.

  • yerrs (array-like) – Absolute uncertainties (standard deviations) on ys, used as weights in the chi-squared minimization.

  • xrange (tuple of (float, float)) – (x_min, x_max) range over which to evaluate the fitted curve.

  • p0 (array-like, optional) – Initial guesses for the fit parameters. Passed directly to scipy.optimize.curve_fit. Default is None (uses curve_fit defaults).

  • bounds (2-tuple of array-like, optional) – Lower and upper bounds on the fit parameters. Default is (-inf, inf) (unbounded).

  • maxfev (int, optional) – Maximum number of function evaluations allowed. Default is 10000.

Returns:

paramsndarray

Best-fit parameter values.

perrndarray

Standard errors of the fitted parameters (sqrt of covariance diagonal).

xfitndarray

10000 evenly spaced x values spanning xrange.

yfitndarray

Model evaluated at xfit with the best-fit parameters.

yfit_errndarray

1-sigma confidence band on yfit from error propagation.

Return type:

dict with keys

SciKit.cals.intraCF2nu(q, Wq, qmax=1.65, window=55)[source]

Estimate the Flory exponent nu from the intrachain structure factor W(q) by identifying the first linear (power-law) scaling region in a log-log plot of W(q) vs q.

The function first fits the full excluded-volume form factor to determine Rg, then restricts the analysis to the Porod regime 1/Rg < q < qmax. A sliding window of fixed size is used to fit local power-law slopes; two independent methods are applied to locate the best-fit linear region:

  • Method 1 (nu1): the window with the first local maximum in R².

  • Method 2 (nu2): the window with the first local minimum in slope (most negative power-law exponent).

In each case, nu = -1 / slope.

Parameters:
  • q (array-like) – Scattering vector magnitudes (1/Angstrom).

  • Wq (array-like) – Intrachain structure factor values corresponding to q.

  • qmax (float, optional) – Upper bound of the q range used for slope analysis. Default is 1.65.

  • window (int, optional) – Number of consecutive q points used in each sliding-window power-law fit. Default is 55.

Returns:

nu1float

Flory exponent from the first local R² maximum method.

nu2float

Flory exponent from the first local slope minimum method.

slopeslist of float

Power-law slopes from each sliding window position.

qslist of float

Starting q value of each sliding window.

R2list of float

Coefficient of determination (R²) for each sliding window fit.

Return type:

dict with keys

SciKit.cals.lower_incomplete_gamma(s, x)[source]

Evaluate the lower incomplete gamma function gamma(s, x).

Parameters:
  • s (float) – Shape parameter (upper integration limit exponent).

  • x (float or array-like) – Upper limit of integration.

Returns:

Value of the lower incomplete gamma function gamma(s, x).

Return type:

float or array-like

SciKit.cals.polymer_excluded_volume(q, Wq, qmin=0.0, qmax=inf, Rg_guess=10.0, nu_guess=0.5, maxfev=10000)[source]

Fit the excluded-volume form factor P(q) to measured scattering data W(q) to extract the radius of gyration Rg and the Flory exponent nu.

Parameters:
  • q (array-like) – Scattering vector magnitudes (1/Angstrom).

  • Wq (array-like) – Measured scattering intensities corresponding to q, normalized so that W(0) ~ 1.

  • qmin (float, optional) – Lower bound of the q range used for fitting. Default is 0.0.

  • qmax (float, optional) – Upper bound of the q range used for fitting. Default is infinity.

  • Rg_guess (float, optional) – Initial guess for the radius of gyration in Angstroms. Default is 10.0.

  • nu_guess (float, optional) – Initial guess for the Flory exponent. Default is 0.5.

  • maxfev (int, optional) – Maximum number of function evaluations for the curve fitting. Default is 10000.

Returns:

Rgfloat

Fitted radius of gyration (Angstrom).

nufloat

Fitted Flory exponent.

Rg_errfloat

Standard error of the fitted Rg.

nu_errfloat

Standard error of the fitted nu.

chi2float

Reduced chi-squared statistic of the fit.

q_fitndarray

The subset of q values used in the fit.

Wq_fitndarray

The fitted P(q) values evaluated over q_fit.

Return type:

dict with keys