A Python library for finite-size scaling (FSS) data collapse analysis:
- Without finite-size correction: Y ≈ f((U − U_c) L^a)
- With finite-size correction: Y ≈ f((U − U_c) L^a) · (1 + b L^c), with normalization support to eliminate amplitude degeneracy
- Universal function f(x) represented by linear splines with second-difference smoothing, no need for analytical forms
- Weighted least squares (weights = 1/σ²) with bootstrap uncertainty estimation
- Multi-optimizer support with random restarts and robust finite-size-correction variants
- Joint fitting of (U_c, a[, b, c]) parameters with spline curve f(x)
- Robust finite-size-correction interface: grid over (b, c), inner loop optimizes only (U_c, a)
- Optimizer options: Nelder–Mead, Powell, or "NM→Powell" combination; supports random_restarts
- Unified random_state for reproducibility
- Compatible with
numpy,scipy,matplotlib
These images are built from real data bundled in this repository (examples/sample_data.csv, derived from real_data_test/real_data_combined.csv).
Local development installation:
pip install -e .Or using requirements.txt:
pip install -r requirements.txt
pip install -e .Input data should be a numpy.ndarray of shape (N,3):
- Column 1: L (system size, positive numbers)
- Column 2: U (control parameter)
- Column 3: Y (observable, e.g., R)
Optional err (shape (N,) or (N,k)), last column is σ (vertical error bar) for each point.
-
Run robust finite-size-corrected example on the real dataset:
python examples/run_example.py
This reads
examples/sample_data.csv(real data) and producesexamples/plot_before.pngandexamples/plot_after.png. -
Rebuild README visuals from real data:
python examples/build_readme_images_from_real.py
This rebuilds the three images under
docs/images/usingreal_data_test/real_data_combined.csvparameters and workflow.
a(ν^(-1)): [0.3, 2.0] if no prior; widen and userandom_restartsif local minima issues- Finite-size correction exponent
c < 0(e.g., [-1.5, -0.05]); recommendnormalize=Trueto reduce amplitude degeneracy
n_knots: 10–16 commonly usedlam: tune between 1e-4~1e-2, watch for overfitting/over-smoothing
- Enable
random_restartswith widerboundsto avoid "local minimum traps" - Use
fit_data_collapse_fse_robustfor grid search over (b,c), inner optimization of (U_c,a) - Reproducibility: fix
random_state
- Optimization stuck at boundaries or oscillating: Relax/reset
bounds, increaserandom_restarts, or switch optimizers - Finite-size correction (b,c) unstable: Enable
normalize=True; use robust variant; moderately increaselam - Poor visual overlap: Ensure using same (U_c,a,b,c) set for plotting with finite-size correction; confirm
normalize/L_refconsistency
- Python 3.9+
- numpy, scipy, matplotlib, pandas (if using CSV)
- pytest (for running tests)
- Welcome issues/PRs; unit tests in
tests/ - Future plans: MCP service encapsulation and upstream contribution to mcp.science
MIT © 2025 Yin-Kai Yu (余荫铠)
- Library API:
src/datacollapse/README.md - GitHub: https://github.com/YinkaiYu/datacollapse
一个用于有限尺寸标度(Finite-Size Scaling, FSS)数据坍缩的 Python 库:
- 无有限尺寸修正:Y ≈ f((U − U_c) L^a)
- 带有限尺寸修正:Y ≈ f((U − U_c) L^a) · (1 + b L^c),支持归一化以降低幅度简并
- f(x) 由带二阶差分平滑的线性样条表示,无需预设解析形式
- 加权最小二乘(权重=1/σ²),并支持 bootstrap 估计不确定度
- 多优化器、多起点随机重启、有限尺寸修正的稳健变体
下图基于仓库内真实数据(examples/sample_data.csv,来自 real_data_test/real_data_combined.csv)。
- 运行基于真实数据的稳健有限尺寸修正示例:
python examples/run_example.py
- 用真实数据重建 README 图片:
python examples/build_readme_images_from_real.py
其余章节同上英文版。
- CLI (requires installation:
pip install -e .[all]):datacollapse-cli --csv examples/sample_data.csv --mode fse-robust --outdir out
- Script (no installation needed inside repo):
python examples/run_example.py python examples/build_readme_images_from_real.py
Planned endpoints (FastAPI):
- fit_nofse
- input: csv (L,U,Y[,sigma]), U_c_0, a_0, n_knots, lam, n_boot, bounds, optimizer, maxiter, random_restarts
- output: params (U_c,a), errs, logs, artifacts (optional images)
- fit_fse
- input: csv, U_c_0, a_0, b_0, c_0, n_knots, lam, n_boot, bounds (c<0), normalize, L_ref, optimizer, maxiter, random_restarts
- output: params (U_c,a,b,c), errs, logs, artifacts
- fit_fse_robust
- input: csv, U_c_0, a_0, b_grid, c_grid, n_knots, lam, n_boot, bounds_Ua, normalize, L_ref, optimizer, maxiter, random_restarts
- output: params (U_c,a,b,c), errs, per-cell logs, artifacts
- collapse_transform
- input: csv, params[, normalize, L_ref]
- output: x, Yc (arrays), or saved plot
JSON schema notes:
- bounds, bounds_Ua: [[lo,hi],[lo,hi], ...]
- normalize: boolean; L_ref: 'geom' | number
- Optimizer: 'NM' | 'Powell' | 'NM_then_Powell'
Security & limits:
- Max N points, execution timeout, concurrency limits
- Result caching & artifact expiration


