cds2.guided_fit
Guided scientific fitting adds a user-controlled workflow on top of the low-level optimization API.
The workflow:
- inspects one or more CSV datasets and reports missing values;
- runs small pilot fits and repeated cross-validation before recommending one model;
- explains the recommendation with speed, expected fit behaviour and complexity;
- leaves final model selection to the user;
- includes measurement uncertainty when a
sigmacolumn is supplied; - detects suspicious residual outliers, quantifies their estimated RMSE effect and asks before excluding them;
- fits the selected model, calculates parameter uncertainty and 95% confidence intervals;
- checks held-out performance with repeated 5-fold cross-validation;
- cross-checks the fitted result with an independent numerical method;
- produces Matplotlib fit and residual PNG/PDF figures, a reproducibility manifest and an optional PDF/HTML/Markdown report; long PDF reports paginate automatically instead of truncating results;
- labels the overall result
reliable,cautionorunreliable; - recommends a different model when the selected fit is weak;
- recommends dataset-specific models when a single common model is materially weaker;
- compares manifest reruns with the saved analysis and warns when runtime versions, inputs, confidence intervals, fit parameters, RMSE or the reliability label change materially.
CLI
Interactive use:
cds2 guided-fit experiment.csv --x time --y response --sigma uncertainty
The CLI asks for decisions only when needed: missing-data handling, model selection, outlier exclusion and report format.
Non-interactive/reproducible use:
cds2 guided-fit experiment.csv \
--x time \
--y response \
--sigma uncertainty \
--model linear \
--missing interpolate \
--outliers keep \
--report pdf \
--seed 0 \
--output-dir results
Multiple datasets can be supplied to search for a common model:
cds2 guided-fit run-a.csv run-b.csv --x time --y response
Repeat an analysis from its saved manifest:
cds2 guided-fit-rerun results/guided_fit_manifest.json
The manifest stores the selected model, policies, seed, data hashes, input source metadata and NumPy/SciPy/pandas/Matplotlib/Python versions. Reruns compare those saved results with the new calculation and surface material instability instead of silently replacing the prior result.
What a drift warning looks like
When the runtime environment or numerical result has moved, the CLI makes that visible instead of silently treating the rerun as equivalent:
warning rerun differs materially from the saved analysis
detail runtime version changed: scipy <saved-version> -> <current-version>
detail confidence intervals changed materially for experiment: <relative-change>
The placeholders above are replaced by the versions and measured relative change from the actual rerun. A version drift is reported even when the numerical fit still happens to agree; confidence-interval drift is checked independently from RMSE and parameter drift.
API
Guided, user-controlled scientific model fitting.
The workflow recommends one model, but never silently chooses user-facing decisions such as model selection, missing-data treatment or outlier removal.
ModelComparison
dataclass
One candidate model scored by Gaussian-error information criteria.
n_params counts the fitted curve parameters plus one for the
estimated error variance. delta_aic/delta_bic are relative to
the best model in the comparison (0 for the winner); aic_weight is
the Akaike weight exp(-delta_aic / 2) normalised over candidates,
i.e. the relative likelihood of each model given the data.
Source code in src/cds2/guided_fit.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | |
compare_models(dataset, *, missing_policy='interpolate', outlier_policy='keep', exclude=())
Rank the candidate models by AIC and BIC on one dataset.
Each candidate is fitted with the same missing-data and outlier policies
as :func:run_guided_fit, then scored under a Gaussian error model:
AIC = 2k - 2 log L and BIC = k ln(n) - 2 log L with k the
number of curve parameters plus one for the estimated error variance.
With sigma the weighted Gaussian likelihood is used (the log sigma
normalisation is included); rmse is always reported in data units.
Results are sorted by AIC and carry deltas plus normalised Akaike
weights, so the output supports model averaging rather than a silent
winner-takes-all choice.
Candidates that cannot be fitted (e.g. power with x <= 0) or
that fit perfectly (relative RMSE at or below _PERFECT_FIT_RTOL,
where the Gaussian likelihood is effectively unbounded) are skipped;
if none remain, a RuntimeError
is raised. Note that with outlier_policy="exclude" different models
may retain different point sets, so their criteria are then computed on
slightly different data; the default "keep" compares like with like.
Raises: ValueError: invalid policy, or every model excluded. RuntimeError: no candidate could be fitted and scored.
Source code in src/cds2/guided_fit.py
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 | |
rerun_manifest(path, *, allow_outside_run_dir=False, trusted_roots=None)
Repeat a guided fit from a saved manifest.
By default every source_path recorded in the manifest must resolve
inside the manifest's own directory (the run directory) or the current
working directory, so a foreign manifest cannot make the tool read
arbitrary files. Pass allow_outside_run_dir=True to opt out of the
confinement, or trusted_roots to extend the whitelist explicitly
(the manifest directory itself is always trusted).
Source code in src/cds2/guided_fit.py
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 | |