Commit 296ac07
feat(fitfunctions): add hinge, composite, and Heaviside fit functions (#422)
* fix(fitfunctions): catch FitFailedError in make_fit when return_exception=True
The exception handler on line 813 only caught RuntimeError and ValueError,
but FitFailedError (raised by _run_least_squares when max_nfev exceeded)
inherits from FitFunctionError, not RuntimeError. This caused make_fit to
raise instead of returning the exception when return_exception=True.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* feat(fitfunctions): add HingeSaturation class for saturation modeling
Piecewise linear function with hinge point for modeling saturation behavior:
- Rising region: f(x) = m1*(x-x1) where m1 = yh/(xh-x1)
- Plateau region: f(x) = m2*(x-x2) where x2 = xh - yh/m2
Parameters: xh (hinge x), yh (hinge y), x1 (x-intercept), m2 (plateau slope)
Includes 24 comprehensive tests covering:
- Function evaluation (rising, plateau, sloped plateau)
- Parameter recovery from clean and noisy data (2σ tolerance)
- Initial parameter estimation
- Weighted fitting with heteroscedastic noise
- Edge cases and error handling
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* test(fitfunctions): add tests for hinge piecewise linear functions
Add comprehensive test coverage for TwoLine, Saturation, HingeMin,
HingeMax, and HingeAtPoint fit functions. Tests include:
- Function evaluation with known parameters
- Parameter recovery from clean and noisy data
- Derived property consistency (xs, s, theta, m2, x_intercepts)
- Continuity at hinge points
- Initial guess (p0) estimation
- Edge cases and numerical stability
Tests written first following TDD - implementations in subsequent commits.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* test(fitfunctions): add tests for Gaussian-Heaviside composite functions
Add comprehensive test coverage for GaussianPlusHeavySide,
GaussianTimesHeavySide, and GaussianTimesHeavySidePlusHeavySide.
Tests include:
- Function evaluation with known parameters
- Parameter recovery from clean and noisy data
- Gaussian component behavior (normalization, peak location)
- Heaviside step transitions
- Component interaction verification
- Initial guess (p0) estimation with guess_x0 parameter
Tests written first following TDD - implementations in subsequent commits.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* test(fitfunctions): add tests for HeavySide step function
Add comprehensive test coverage for HeavySide fit function. Tests include:
- Function evaluation with known parameters
- Step transition behavior (x < x0, x == x0, x > x0)
- Parameter recovery from clean and noisy data
- Initial guess (p0) estimation with optional guess parameters
- Edge cases (step at data boundary, flat data)
- TeX function representation
Tests written first following TDD - implementation in subsequent commit.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* feat(fitfunctions): add hinge piecewise linear functions
Add five piecewise linear fit functions for modeling transitions:
- TwoLine: Two intersecting lines (minimum), params: x1, x2, m1, m2
- Saturation: Linear rise with saturation plateau, params: x1, xs, s, theta
- HingeMin: Minimum of two lines at hinge point, params: m1, x1, x2, h
- HingeMax: Maximum of two lines at hinge point, params: m1, x1, x2, h
- HingeAtPoint: Piecewise linear with specified hinge point,
params: m1, b1, m2, b2
All classes include:
- Analytic function definitions using np.minimum/np.maximum
- Data-driven initial guess (p0) estimation
- Derived properties (xs, s, theta, m2, x_intercepts as applicable)
- TeX function representations for plotting
Contributed from nh/vanishing_speed_hinge_fits.py with improvements:
- Consistent API with existing FitFunction classes
- TODO comments for future data-driven p0 estimation
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* feat(fitfunctions): add Gaussian-Heaviside composite functions
Add three composite fit functions combining Gaussian and Heaviside:
- GaussianPlusHeavySide: Gaussian + Heaviside step
params: x0, y0, y1, mu, sigma, A
- GaussianTimesHeavySide: Gaussian × Heaviside step
params: x0, mu, sigma, A
- GaussianTimesHeavySidePlusHeavySide: (Gaussian × Heaviside) + Heaviside
params: x0, y1, mu, sigma, A
All classes include:
- Analytic function definitions
- Data-driven initial guess (p0) estimation
- Optional guess_x0 parameter for step location hint
- TeX function representations
Contributed from nh/vanishing_speed_hinge_fits.py with fixes:
- Fixed typo bug: return gaussian_heavy_size -> gaussian_heavy_side
- Renamed p0_x0 to guess_x0 for API consistency
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* feat(fitfunctions): add HeavySide step function
Add HeavySide fit function for modeling abrupt transitions:
- HeavySide: Step function using np.heaviside
params: x0 (transition point), y0 (baseline), y1 (step height)
Features:
- Analytic function: y1 * H(x0 - x) + y0
- Data-driven initial guess (p0) estimation
- Optional guess_x0, guess_y0, guess_y1 parameters
- TeX function representation
Contributed from nh/vanishing_speed_hinge_fits.py with fixes:
- Implemented p0 estimation (original raised NotImplementedError)
Also updates __init__.py to export all new classes:
- TwoLine, Saturation, HingeMin, HingeMax, HingeAtPoint
- GaussianPlusHeavySide, GaussianTimesHeavySide,
GaussianTimesHeavySidePlusHeavySide
- HeavySide
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* docs(fitfunctions): add module-specific contribution guide
Add comprehensive CONTRIBUTING.md for the fitfunctions module covering:
- Development workflow (TDD: tests before implementation)
- FitFunction class requirements (function, p0, TeX_function)
- Data-driven p0 estimation (no hardcoded domain values)
- Test categories E1-E7 with tolerance specifications
- Test patterns and anti-patterns
- Non-trivial test criteria (6 requirements)
- Test parameterization for DRY multi-case tests
- Quality checklist for PR submissions
This standalone document will be integrated into unified project docs
once all submodules have contribution standards.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* style(fitfunctions): apply Black formatting to source and test files
Fix CI validation failure caused by Black formatting violations in:
- solarwindpy/fitfunctions/composite.py (2 line-length issues)
- tests/fitfunctions/test_composite.py
- tests/fitfunctions/test_heaviside.py
- tests/fitfunctions/test_hinge.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>1 parent 6bbaa8a commit 296ac07
9 files changed
Lines changed: 6936 additions & 3 deletions
File tree
- solarwindpy/fitfunctions
- tests/fitfunctions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
0 commit comments