ftoolss 6.0.26__tar.gz → 6.0.35__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. {ftoolss-6.0.26 → ftoolss-6.0.35}/PKG-INFO +1 -1
  2. {ftoolss-6.0.26 → ftoolss-6.0.35}/setup.py +1 -1
  3. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fmpfit/gaussian_deviate.c +2 -2
  4. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss.egg-info/PKG-INFO +1 -1
  5. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss.egg-info/SOURCES.txt +1 -0
  6. ftoolss-6.0.35/tests/test_fmpfit_golden.py +427 -0
  7. {ftoolss-6.0.26 → ftoolss-6.0.35}/MANIFEST.in +0 -0
  8. {ftoolss-6.0.26 → ftoolss-6.0.35}/README.md +0 -0
  9. {ftoolss-6.0.26 → ftoolss-6.0.35}/pyproject.toml +0 -0
  10. {ftoolss-6.0.26 → ftoolss-6.0.35}/setup.cfg +0 -0
  11. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/__init__.py +0 -0
  12. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fgaussian/__init__.py +0 -0
  13. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fgaussian/benchmark_all_variants.py +0 -0
  14. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fgaussian/benchmark_fgaussian_f32.py +0 -0
  15. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fgaussian/benchmark_fgaussian_f32_vs_f64.py +0 -0
  16. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fgaussian/fgaussian_f32_ext.c +0 -0
  17. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fgaussian/fgaussian_f64_ext.c +0 -0
  18. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fgaussian/fgaussian_jacobian.py +0 -0
  19. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fgaussian/fgaussian_jacobian_f32_ext.c +0 -0
  20. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fgaussian/fgaussian_jacobian_f64_ext.c +0 -0
  21. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fgaussian/fgaussian_jacobian_f64_f32_ext.c +0 -0
  22. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fgaussian/test_subprocess_isolation.py +0 -0
  23. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fmedian/__init__.py +0 -0
  24. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fmedian/fmedian_ext.c +0 -0
  25. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fmedian3/__init__.py +0 -0
  26. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fmedian3/fmedian3_ext.c +0 -0
  27. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fmpfit/__init__.py +0 -0
  28. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fmpfit/benchmark_f32_vs_f64.py +0 -0
  29. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fmpfit/benchmark_fmpfit_block_f32_vs_f64.py +0 -0
  30. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fmpfit/cmpfit-1.5/DISCLAIMER +0 -0
  31. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fmpfit/cmpfit-1.5/mpfit.c +0 -0
  32. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fmpfit/cmpfit-1.5/mpfit.h +0 -0
  33. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fmpfit/compare_c_python.py +0 -0
  34. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fmpfit/compare_errors.py +0 -0
  35. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fmpfit/example_fmpfit_block_f32_5_N.py +0 -0
  36. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fmpfit/example_fmpfit_block_f64_5_N.py +0 -0
  37. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fmpfit/example_fmpfit_f32_5_N.py +0 -0
  38. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fmpfit/example_fmpfit_f64_5_N.py +0 -0
  39. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fmpfit/fmpfit_f32_block_ext.c +0 -0
  40. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fmpfit/fmpfit_f32_ext.c +0 -0
  41. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fmpfit/fmpfit_f64_block_ext.c +0 -0
  42. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fmpfit/fmpfit_f64_ext.c +0 -0
  43. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fmpfit/test_xerror_bugcase.py +0 -0
  44. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fmpfit/test_xerror_comparison.py +0 -0
  45. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fsigma/__init__.py +0 -0
  46. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fsigma/fsigma_ext.c +0 -0
  47. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fsigma3/__init__.py +0 -0
  48. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/fsigma3/fsigma3_ext.c +0 -0
  49. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/quickselect/quickselect.c +0 -0
  50. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/sorting/benchmark_sortN.c +0 -0
  51. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/sorting/benchmark_sorts.c +0 -0
  52. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/sorting/demo_sortN.c +0 -0
  53. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/sorting/radix_sort_double.c +0 -0
  54. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/sorting/radix_sort_struct.c +0 -0
  55. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/sorting/radix_sort_uint64.c +0 -0
  56. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/sorting/sorting.c +0 -0
  57. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/sorting/sorting_general.c +0 -0
  58. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/sorting/sorting_networks_generated.c +0 -0
  59. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/sorting/sorting_networks_generic.c +0 -0
  60. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/sorting/test_all_sorts.c +0 -0
  61. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/sorting/test_double_as_int.c +0 -0
  62. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/sorting/test_double_reversibility.c +0 -0
  63. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/sorting/test_sortN.c +0 -0
  64. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss/sorting/test_sorting_general.c +0 -0
  65. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss.egg-info/dependency_links.txt +0 -0
  66. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss.egg-info/not-zip-safe +0 -0
  67. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss.egg-info/requires.txt +0 -0
  68. {ftoolss-6.0.26 → ftoolss-6.0.35}/src/ftoolss.egg-info/top_level.txt +0 -0
  69. {ftoolss-6.0.26 → ftoolss-6.0.35}/tests/test_fgaussian.py +0 -0
  70. {ftoolss-6.0.26 → ftoolss-6.0.35}/tests/test_fmedian.py +0 -0
  71. {ftoolss-6.0.26 → ftoolss-6.0.35}/tests/test_fmedian3.py +0 -0
  72. {ftoolss-6.0.26 → ftoolss-6.0.35}/tests/test_fmpfit.py +0 -0
  73. {ftoolss-6.0.26 → ftoolss-6.0.35}/tests/test_fmpfit_block_vs_curvefit.py +0 -0
  74. {ftoolss-6.0.26 → ftoolss-6.0.35}/tests/test_fmpfit_concurrent.py +0 -0
  75. {ftoolss-6.0.26 → ftoolss-6.0.35}/tests/test_fmpfit_consistency.py +0 -0
  76. {ftoolss-6.0.26 → ftoolss-6.0.35}/tests/test_fmpfit_vs_curvefit.py +0 -0
  77. {ftoolss-6.0.26 → ftoolss-6.0.35}/tests/test_fsigma.py +0 -0
  78. {ftoolss-6.0.26 → ftoolss-6.0.35}/tests/test_fsigma3.py +0 -0
  79. {ftoolss-6.0.26 → ftoolss-6.0.35}/tests/test_integration.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ftoolss
3
- Version: 6.0.26
3
+ Version: 6.0.35
4
4
  Summary: Small C extensions for local image filters (fmedian, fsigma)
5
5
  Author:
6
6
  License: MIT
@@ -146,7 +146,7 @@ setup(
146
146
  # Version 5 introduced fmpfit
147
147
  # Version 5.2 introduces xerror_scipy (different handling of bounded params)
148
148
  # Version 6 changed name to ftoolss (ftools, Stein's) for publishing to PiPY
149
- version="6.0.26",
149
+ version="6.0.35",
150
150
  description="Small C extensions for local image filters (fmedian, fsigma)",
151
151
  long_description=read_readme(),
152
152
  long_description_content_type="text/markdown",
@@ -47,9 +47,9 @@ struct GD_STRUCT_NAME
47
47
  * Parameters: p[0] = amplitude, p[1] = mean, p[2] = sigma
48
48
  */
49
49
  int GD_FUNC_NAME(int m, int n, gd_real *p, gd_real *deviates,
50
- gd_real **derivs, void *user_data)
50
+ gd_real **derivs, void *private_data)
51
51
  {
52
- struct GD_STRUCT_NAME *pdata = (struct GD_STRUCT_NAME *)user_data;
52
+ struct GD_STRUCT_NAME *pdata = (struct GD_STRUCT_NAME *)private_data;
53
53
  int i;
54
54
  gd_real amp, mean, sigma;
55
55
  (void)n; /* Suppress unused parameter warning */
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ftoolss
3
- Version: 6.0.26
3
+ Version: 6.0.35
4
4
  Summary: Small C extensions for local image filters (fmedian, fsigma)
5
5
  Author:
6
6
  License: MIT
@@ -70,6 +70,7 @@ tests/test_fmpfit.py
70
70
  tests/test_fmpfit_block_vs_curvefit.py
71
71
  tests/test_fmpfit_concurrent.py
72
72
  tests/test_fmpfit_consistency.py
73
+ tests/test_fmpfit_golden.py
73
74
  tests/test_fmpfit_vs_curvefit.py
74
75
  tests/test_fsigma.py
75
76
  tests/test_fsigma3.py
@@ -0,0 +1,427 @@
1
+ #!/usr/bin/env python
2
+ """
3
+ Golden-value (regression) tests for fmpfit extensions.
4
+
5
+ Each test uses fixed input data and exact expected output, so any change
6
+ to the C implementation that alters numerical results will be caught.
7
+ Tolerances are tight (rtol=1e-6 for f64, rtol=1e-5 for f32) to allow
8
+ only negligible floating-point variation across platforms.
9
+
10
+ Test coverage:
11
+ - fmpfit_f64_pywrap: noise-free exact recovery, seeded Poisson noise
12
+ - fmpfit_f32_pywrap: seeded Poisson noise, parameter-at-bound (bugcase)
13
+ - fmpfit_f64_block_pywrap: block fit, 3 spectra, seeded Poisson noise
14
+ - fmpfit_f32_block_pywrap: block fit, 3 spectra, seeded Poisson noise
15
+ """
16
+
17
+ import numpy as np
18
+ import pytest
19
+
20
+ from ftoolss import fmpfit_f64_pywrap, fmpfit_f32_pywrap
21
+ from ftoolss.fmpfit import fmpfit_f64_block_pywrap, fmpfit_f32_block_pywrap
22
+
23
+ # ---------------------------------------------------------------------------
24
+ # Helpers
25
+ # ---------------------------------------------------------------------------
26
+
27
+ def _gaussian_5pt_f32(seed):
28
+ """Return (x, y, error) for a 5-point f32 Gaussian with Poisson noise."""
29
+ true_params = np.array([2.5, 0.0, 0.8], dtype=np.float32)
30
+ fwhm = float(2.355 * true_params[2])
31
+ x = np.linspace(-fwhm / 2, fwhm / 2, 5, dtype=np.float32)
32
+ rng = np.random.default_rng(seed)
33
+ y_true = true_params[0] * np.exp(-0.5 * ((x - true_params[1]) / true_params[2]) ** 2)
34
+ y = rng.poisson(y_true).astype(np.float32)
35
+ err = np.sqrt(np.maximum(y, 1.0)).astype(np.float32)
36
+ return x, y, err
37
+
38
+
39
+ def _gaussian_5pt_f64(seed):
40
+ """Return (x, y, error) for a 5-point f64 Gaussian with Poisson noise."""
41
+ true_params = np.array([2.5, 0.0, 0.8], dtype=np.float64)
42
+ fwhm = float(2.355 * true_params[2])
43
+ x = np.linspace(-fwhm / 2, fwhm / 2, 5, dtype=np.float64)
44
+ rng = np.random.default_rng(seed)
45
+ y_true = true_params[0] * np.exp(-0.5 * ((x - true_params[1]) / true_params[2]) ** 2)
46
+ y = rng.poisson(y_true).astype(np.float64)
47
+ err = np.sqrt(np.maximum(y, 1.0)).astype(np.float64)
48
+ return x, y, err
49
+
50
+
51
+ # ---------------------------------------------------------------------------
52
+ # f64 single-spectrum tests
53
+ # ---------------------------------------------------------------------------
54
+
55
+ class TestFmpfitF64Golden:
56
+ """Regression tests for fmpfit_f64_pywrap."""
57
+
58
+ def test_noisefree_exact_recovery(self):
59
+ """Noise-free Gaussian: fitter must recover exact parameters to high precision."""
60
+ x = np.linspace(-3.0, 3.0, 31, dtype=np.float64)
61
+ y = 3.0 * np.exp(-0.5 * (x / 1.2) ** 2)
62
+ err = np.ones_like(x) * 0.01
63
+ parinfo = [
64
+ {'value': 2.0, 'limits': [0.0, 10.0]},
65
+ {'value': 0.0, 'limits': [-3.0, 3.0]},
66
+ {'value': 1.0, 'limits': [0.1, 5.0]},
67
+ ]
68
+ result = fmpfit_f64_pywrap(0, parinfo=parinfo, functkw={'x': x, 'y': y, 'error': err})
69
+
70
+ # Parameters must be recovered to < 1e-10 absolute error
71
+ np.testing.assert_allclose(result.best_params[0], 3.0, atol=1e-10,
72
+ err_msg="amplitude")
73
+ np.testing.assert_allclose(result.best_params[1], 0.0, atol=1e-10,
74
+ err_msg="mean")
75
+ np.testing.assert_allclose(result.best_params[2], 1.2, atol=1e-10,
76
+ err_msg="sigma")
77
+
78
+ # Residuals are all near zero so chi-square must be essentially 0
79
+ assert result.bestnorm < 1e-20, f"bestnorm too large: {result.bestnorm}"
80
+ assert result.status in (1, 2, 3, 4), f"unexpected status {result.status}"
81
+
82
+ def test_noisefree_metadata(self):
83
+ """Noise-free fit: result metadata must match known values exactly."""
84
+ x = np.linspace(-3.0, 3.0, 31, dtype=np.float64)
85
+ y = 3.0 * np.exp(-0.5 * (x / 1.2) ** 2)
86
+ err = np.ones_like(x) * 0.01
87
+ parinfo = [
88
+ {'value': 2.0, 'limits': [0.0, 10.0]},
89
+ {'value': 0.0, 'limits': [-3.0, 3.0]},
90
+ {'value': 1.0, 'limits': [0.1, 5.0]},
91
+ ]
92
+ result = fmpfit_f64_pywrap(0, parinfo=parinfo, functkw={'x': x, 'y': y, 'error': err})
93
+
94
+ assert result.nfunc == 31
95
+ assert result.npar == 3
96
+ assert result.nfree == 3
97
+ assert result.npegged == 0
98
+
99
+ def test_noisefree_xerror_golden(self):
100
+ """Noise-free fit: parameter uncertainties must match golden values."""
101
+ x = np.linspace(-3.0, 3.0, 31, dtype=np.float64)
102
+ y = 3.0 * np.exp(-0.5 * (x / 1.2) ** 2)
103
+ err = np.ones_like(x) * 0.01
104
+ parinfo = [
105
+ {'value': 2.0, 'limits': [0.0, 10.0]},
106
+ {'value': 0.0, 'limits': [-3.0, 3.0]},
107
+ {'value': 1.0, 'limits': [0.1, 5.0]},
108
+ ]
109
+ result = fmpfit_f64_pywrap(0, parinfo=parinfo, functkw={'x': x, 'y': y, 'error': err})
110
+
111
+ expected_xerror = np.array([0.00376818, 0.001738, 0.00175788])
112
+ np.testing.assert_allclose(result.xerror, expected_xerror, rtol=1e-4,
113
+ err_msg="xerror golden values mismatch")
114
+
115
+ def test_5pt_poisson_seed42_params(self):
116
+ """f64, 5-point Poisson noise (seed=42): fixate best_params golden values."""
117
+ # Known input (seed=42 Poisson draws from amp=2.5, mean=0, sigma=0.8)
118
+ x = np.array([-0.942, -0.471, 0., 0.471, 0.942])
119
+ y = np.array([2., 1., 1., 3., 2.])
120
+ err = np.sqrt(np.maximum(y, 1.0))
121
+ parinfo = [
122
+ {'value': 2.0, 'limits': [0.0, 10.0]},
123
+ {'value': 0.0, 'limits': [-2.0, 2.0]},
124
+ {'value': 1.0, 'limits': [0.1, 5.0]},
125
+ ]
126
+ result = fmpfit_f64_pywrap(0, parinfo=parinfo, functkw={'x': x, 'y': y, 'error': err})
127
+
128
+ expected = np.array([2.10086825, 2.0, 2.57348856])
129
+ np.testing.assert_allclose(result.best_params, expected, rtol=1e-6,
130
+ err_msg="best_params golden mismatch (f64 5pt seed42)")
131
+ assert result.status == 1
132
+
133
+ def test_5pt_poisson_seed42_bestnorm(self):
134
+ """f64, 5-point Poisson noise (seed=42): fixate bestnorm golden value."""
135
+ x = np.array([-0.942, -0.471, 0., 0.471, 0.942])
136
+ y = np.array([2., 1., 1., 3., 2.])
137
+ err = np.sqrt(np.maximum(y, 1.0))
138
+ parinfo = [
139
+ {'value': 2.0, 'limits': [0.0, 10.0]},
140
+ {'value': 0.0, 'limits': [-2.0, 2.0]},
141
+ {'value': 1.0, 'limits': [0.1, 5.0]},
142
+ ]
143
+ result = fmpfit_f64_pywrap(0, parinfo=parinfo, functkw={'x': x, 'y': y, 'error': err})
144
+
145
+ np.testing.assert_allclose(result.bestnorm, 1.3372162756721824, rtol=1e-6,
146
+ err_msg="bestnorm golden mismatch (f64 5pt seed42)")
147
+
148
+ def test_5pt_poisson_seed42_xerror_scipy(self):
149
+ """f64, 5-point (seed=42): xerror_scipy golden values (vel is at bound, so
150
+ xerror[1] == 0 while xerror_scipy[1] is large but finite)."""
151
+ x = np.array([-0.942, -0.471, 0., 0.471, 0.942])
152
+ y = np.array([2., 1., 1., 3., 2.])
153
+ err = np.sqrt(np.maximum(y, 1.0))
154
+ parinfo = [
155
+ {'value': 2.0, 'limits': [0.0, 10.0]},
156
+ {'value': 0.0, 'limits': [-2.0, 2.0]},
157
+ {'value': 1.0, 'limits': [0.1, 5.0]},
158
+ ]
159
+ result = fmpfit_f64_pywrap(0, parinfo=parinfo, functkw={'x': x, 'y': y, 'error': err})
160
+
161
+ # mean is pegged at upper bound (2.0), so xerror[1] is 0
162
+ assert result.xerror[1] == pytest.approx(0.0, abs=1e-10), "xerror[1] should be 0 when pegged at bound"
163
+ # xerror_scipy uses Hessian inverse - should be large but finite
164
+ expected_scipy = np.array([6.14382867, 21.86389779, 14.82509948])
165
+ np.testing.assert_allclose(result.xerror_scipy, expected_scipy, rtol=1e-5,
166
+ err_msg="xerror_scipy golden mismatch (f64 5pt seed42)")
167
+
168
+
169
+ # ---------------------------------------------------------------------------
170
+ # f32 single-spectrum tests
171
+ # ---------------------------------------------------------------------------
172
+
173
+ class TestFmpfitF32Golden:
174
+ """Regression tests for fmpfit_f32_pywrap."""
175
+
176
+ def test_5pt_poisson_seed42_params(self):
177
+ """f32, 5-point Poisson noise (seed=42): fixate best_params golden values."""
178
+ x = np.array([-0.94200003, -0.47100002, 0., 0.47100002, 0.94200003], dtype=np.float32)
179
+ y = np.array([2., 1., 1., 3., 2.], dtype=np.float32)
180
+ err = np.sqrt(np.maximum(y, 1.0)).astype(np.float32)
181
+ parinfo = [
182
+ {'value': 2.0, 'limits': [0.0, 10.0]},
183
+ {'value': 0.0, 'limits': [-2.0, 2.0]},
184
+ {'value': 1.0, 'limits': [0.1, 5.0]},
185
+ ]
186
+ result = fmpfit_f32_pywrap(0, parinfo=parinfo, functkw={'x': x, 'y': y, 'error': err})
187
+
188
+ expected = np.array([2.100182, 2., 2.574894], dtype=np.float32)
189
+ np.testing.assert_allclose(result.best_params, expected, rtol=1e-3,
190
+ err_msg="best_params golden mismatch (f32 5pt seed42)")
191
+ assert result.status == 1
192
+
193
+ def test_5pt_poisson_seed42_bestnorm(self):
194
+ """f32, 5-point Poisson noise (seed=42): fixate bestnorm golden value."""
195
+ x = np.array([-0.94200003, -0.47100002, 0., 0.47100002, 0.94200003], dtype=np.float32)
196
+ y = np.array([2., 1., 1., 3., 2.], dtype=np.float32)
197
+ err = np.sqrt(np.maximum(y, 1.0)).astype(np.float32)
198
+ parinfo = [
199
+ {'value': 2.0, 'limits': [0.0, 10.0]},
200
+ {'value': 0.0, 'limits': [-2.0, 2.0]},
201
+ {'value': 1.0, 'limits': [0.1, 5.0]},
202
+ ]
203
+ result = fmpfit_f32_pywrap(0, parinfo=parinfo, functkw={'x': x, 'y': y, 'error': err})
204
+
205
+ np.testing.assert_allclose(result.bestnorm, 1.3372162580490112, rtol=1e-5,
206
+ err_msg="bestnorm golden mismatch (f32 5pt seed42)")
207
+
208
+ def test_5pt_poisson_seed42_xerror_scipy(self):
209
+ """f32, 5-point (seed=42): xerror_scipy golden values."""
210
+ x = np.array([-0.94200003, -0.47100002, 0., 0.47100002, 0.94200003], dtype=np.float32)
211
+ y = np.array([2., 1., 1., 3., 2.], dtype=np.float32)
212
+ err = np.sqrt(np.maximum(y, 1.0)).astype(np.float32)
213
+ parinfo = [
214
+ {'value': 2.0, 'limits': [0.0, 10.0]},
215
+ {'value': 0.0, 'limits': [-2.0, 2.0]},
216
+ {'value': 1.0, 'limits': [0.1, 5.0]},
217
+ ]
218
+ result = fmpfit_f32_pywrap(0, parinfo=parinfo, functkw={'x': x, 'y': y, 'error': err})
219
+
220
+ # mean is pegged at upper bound ? xerror[1] == 0
221
+ assert result.xerror[1] == pytest.approx(0.0, abs=1e-10), "xerror[1] should be 0 when pegged at bound"
222
+ expected_scipy = np.array([6.141826, 21.886211, 14.847473], dtype=np.float32)
223
+ np.testing.assert_allclose(result.xerror_scipy, expected_scipy, rtol=2e-3,
224
+ err_msg="xerror_scipy golden mismatch (f32 5pt seed42)")
225
+
226
+ def test_lw_at_upper_bound_bugcase(self):
227
+ """
228
+ Regression test from real MUSE data: linewidth converges to upper bound (200).
229
+ Fixates best_params, bestnorm, and xerror_scipy for this edge case.
230
+ """
231
+ xdata = np.array([-81.21540537932414, -40.60770268966206, 0.0,
232
+ 40.60770268966209, 81.21540537932415], dtype=np.float32)
233
+ ydata = np.array([0.8990826606750488, 0.9491528868675232, 0.9999998211860657,
234
+ 0.9884452223777771, 0.9523726105690002], dtype=np.float32)
235
+ y_sigma = np.ones(5, dtype=np.float32)
236
+ parinfo = [
237
+ {'value': 1.0, 'limits': [0.9, 1.1]},
238
+ {'value': 0.0, 'limits': [-81.21540537932412, 81.21540537932412]},
239
+ {'value': 69.1378943769105, 'limits': [59.137894376910495, 200.0]},
240
+ ]
241
+ result = fmpfit_f32_pywrap(
242
+ 0, parinfo=parinfo,
243
+ functkw={'x': xdata, 'y': ydata, 'error': y_sigma},
244
+ xtol=1.0e-6, ftol=1.0e-6, gtol=1.0e-6, maxiter=2000, quiet=1,
245
+ )
246
+
247
+ # Linewidth must be at the upper bound
248
+ np.testing.assert_allclose(result.best_params[2], 200.0, rtol=1e-5,
249
+ err_msg="linewidth should be at upper bound 200")
250
+ # Amplitude golden value
251
+ np.testing.assert_allclose(result.best_params[0], 1.0001495, rtol=1e-4,
252
+ err_msg="amplitude golden mismatch (bugcase)")
253
+ # Velocity golden value
254
+ np.testing.assert_allclose(result.best_params[1], 15.571712, rtol=1e-4,
255
+ err_msg="velocity golden mismatch (bugcase)")
256
+ # bestnorm golden value
257
+ np.testing.assert_allclose(result.bestnorm, 0.0002860676031559706, rtol=1e-4,
258
+ err_msg="bestnorm golden mismatch (bugcase)")
259
+ # npegged: linewidth is AT bound but via convergence (npegged counts forced pegs)
260
+ assert result.npegged == 0
261
+ # xerror_scipy must be finite and match golden values
262
+ expected_scipy = np.array([8.2430467e-03, 4.6672206e+00, 1.6366814e+01],
263
+ dtype=np.float32)
264
+ np.testing.assert_allclose(result.xerror_scipy, expected_scipy, rtol=1e-3,
265
+ err_msg="xerror_scipy golden mismatch (bugcase)")
266
+
267
+
268
+ # ---------------------------------------------------------------------------
269
+ # f64 block tests
270
+ # ---------------------------------------------------------------------------
271
+
272
+ class TestFmpfitF64BlockGolden:
273
+ """Regression tests for fmpfit_f64_block_pywrap."""
274
+
275
+ def _make_block_data(self, n_spectra=3):
276
+ """Return (x, y, err, p0, bnd) for n_spectra=3 spectra.
277
+
278
+ y values are hardcoded Poisson draws (legacy seed=42) so golden values
279
+ remain stable regardless of the NumPy random-generator version.
280
+ """
281
+ tp = np.array([2.5, 0.0, 0.8], dtype=np.float64)
282
+ fwhm = float(2.355 * tp[2])
283
+ x1d = np.linspace(-fwhm / 2, fwhm / 2, 5, dtype=np.float64)
284
+ p0_1d = np.array([2.0, 0.0, 1.0], dtype=np.float64)
285
+ bnd_1d = np.array([[0.0, 10.0], [-2.0, 2.0], [0.1, 5.0]], dtype=np.float64)
286
+ x = np.tile(x1d, (n_spectra, 1))
287
+ y = np.array([
288
+ [2., 1., 1., 3., 2.],
289
+ [0., 1., 2., 1., 1.],
290
+ [2., 2., 2., 4., 2.],
291
+ ], dtype=np.float64)[:n_spectra]
292
+ err = np.sqrt(np.maximum(y, 1.0)).astype(np.float64)
293
+ p0 = np.tile(p0_1d, (n_spectra, 1))
294
+ bnd = np.tile(bnd_1d, (n_spectra, 1, 1))
295
+ return x, y, err, p0, bnd
296
+
297
+ def test_block_f64_n3_seed42_params(self):
298
+ """Block f64, N=3, seed=42: fixate best_params for all 3 spectra."""
299
+ x, y, err, p0, bnd = self._make_block_data()
300
+ result = fmpfit_f64_block_pywrap(0, x, y, err, p0, bnd,
301
+ xtol=1e-6, ftol=1e-6, gtol=1e-6, maxiter=2000)
302
+
303
+ expected = np.array([
304
+ [2.10086825, 2.0, 2.57348856],
305
+ [1.58751874, 0.15956157, 0.60724679],
306
+ [2.5078758, 0.33855646, 1.54905626],
307
+ ])
308
+ np.testing.assert_allclose(result['best_params'], expected, rtol=1e-6,
309
+ err_msg="block f64 best_params golden mismatch")
310
+
311
+ def test_block_f64_n3_seed42_bestnorm(self):
312
+ """Block f64, N=3, seed=42: fixate bestnorm for all 3 spectra."""
313
+ x, y, err, p0, bnd = self._make_block_data()
314
+ result = fmpfit_f64_block_pywrap(0, x, y, err, p0, bnd,
315
+ xtol=1e-6, ftol=1e-6, gtol=1e-6, maxiter=2000)
316
+
317
+ expected = np.array([1.33721628, 0.45638885, 0.75819141])
318
+ np.testing.assert_allclose(result['bestnorm'], expected, rtol=1e-6,
319
+ err_msg="block f64 bestnorm golden mismatch")
320
+
321
+ def test_block_f64_n3_seed42_status(self):
322
+ """Block f64, N=3, seed=42: all spectra converged (status 1)."""
323
+ x, y, err, p0, bnd = self._make_block_data()
324
+ result = fmpfit_f64_block_pywrap(0, x, y, err, p0, bnd,
325
+ xtol=1e-6, ftol=1e-6, gtol=1e-6, maxiter=2000)
326
+
327
+ np.testing.assert_array_equal(result['status'], np.array([1, 1, 1], dtype=np.int32))
328
+
329
+ def test_block_f64_n3_matches_single(self):
330
+ """Block f64 results must match per-spectrum single fits."""
331
+ x, y, err, p0, bnd = self._make_block_data()
332
+ result_block = fmpfit_f64_block_pywrap(0, x, y, err, p0, bnd,
333
+ xtol=1e-6, ftol=1e-6, gtol=1e-6, maxiter=2000)
334
+
335
+ bnd_1d = bnd[0] # same for all
336
+ for i in range(3):
337
+ parinfo_i = [{'value': float(p0[i, j]), 'limits': [float(bnd_1d[j, 0]), float(bnd_1d[j, 1])]}
338
+ for j in range(3)]
339
+ r_single = fmpfit_f64_pywrap(0, parinfo=parinfo_i,
340
+ functkw={'x': x[i], 'y': y[i], 'error': err[i]},
341
+ xtol=1e-6, ftol=1e-6, gtol=1e-6, maxiter=2000)
342
+ np.testing.assert_allclose(result_block['best_params'][i], r_single.best_params,
343
+ rtol=1e-10,
344
+ err_msg=f"block != single for spectrum {i} (f64)")
345
+
346
+
347
+ # ---------------------------------------------------------------------------
348
+ # f32 block tests
349
+ # ---------------------------------------------------------------------------
350
+
351
+ class TestFmpfitF32BlockGolden:
352
+ """Regression tests for fmpfit_f32_block_pywrap."""
353
+
354
+ def _make_block_data(self, n_spectra=3):
355
+ """Return (x, y, err, p0, bnd) for n_spectra=3 f32 spectra.
356
+
357
+ y values are hardcoded Poisson draws (legacy seed=42) so golden values
358
+ remain stable regardless of the NumPy random-generator version.
359
+ """
360
+ tp = np.array([2.5, 0.0, 0.8], dtype=np.float32)
361
+ fwhm = float(2.355 * tp[2])
362
+ x1d = np.linspace(-fwhm / 2, fwhm / 2, 5, dtype=np.float32)
363
+ p0_1d = np.array([2.0, 0.0, 1.0], dtype=np.float32)
364
+ bnd_1d = np.array([[0.0, 10.0], [-2.0, 2.0], [0.1, 5.0]], dtype=np.float32)
365
+ x = np.tile(x1d, (n_spectra, 1))
366
+ y = np.array([
367
+ [2., 1., 1., 3., 2.],
368
+ [0., 1., 2., 1., 1.],
369
+ [2., 2., 2., 4., 2.],
370
+ ], dtype=np.float32)[:n_spectra]
371
+ err = np.sqrt(np.maximum(y, 1.0)).astype(np.float32)
372
+ p0 = np.tile(p0_1d, (n_spectra, 1))
373
+ bnd = np.tile(bnd_1d, (n_spectra, 1, 1))
374
+ return x, y, err, p0, bnd
375
+
376
+ def test_block_f32_n3_seed42_params(self):
377
+ """Block f32, N=3, seed=42: fixate best_params for all 3 spectra."""
378
+ x, y, err, p0, bnd = self._make_block_data()
379
+ result = fmpfit_f32_block_pywrap(0, x, y, err, p0, bnd,
380
+ xtol=1e-6, ftol=1e-6, gtol=1e-6, maxiter=2000)
381
+
382
+ expected = np.array([
383
+ [2.100182, 2., 2.574894],
384
+ [1.587335, 0.159487, 0.607375],
385
+ [2.507876, 0.338557, 1.549057],
386
+ ], dtype=np.float32)
387
+ np.testing.assert_allclose(result['best_params'], expected, rtol=1e-3,
388
+ err_msg="block f32 best_params golden mismatch")
389
+
390
+ def test_block_f32_n3_seed42_bestnorm(self):
391
+ """Block f32, N=3, seed=42: fixate bestnorm for all 3 spectra."""
392
+ x, y, err, p0, bnd = self._make_block_data()
393
+ result = fmpfit_f32_block_pywrap(0, x, y, err, p0, bnd,
394
+ xtol=1e-6, ftol=1e-6, gtol=1e-6, maxiter=2000)
395
+
396
+ expected = np.array([1.3372163, 0.4563889, 0.75819147], dtype=np.float32)
397
+ np.testing.assert_allclose(result['bestnorm'], expected, rtol=1e-5,
398
+ err_msg="block f32 bestnorm golden mismatch")
399
+
400
+ def test_block_f32_n3_seed42_status(self):
401
+ """Block f32, N=3, seed=42: all spectra converged (status 1)."""
402
+ x, y, err, p0, bnd = self._make_block_data()
403
+ result = fmpfit_f32_block_pywrap(0, x, y, err, p0, bnd,
404
+ xtol=1e-6, ftol=1e-6, gtol=1e-6, maxiter=2000)
405
+
406
+ np.testing.assert_array_equal(result['status'], np.array([1, 1, 1], dtype=np.int32))
407
+
408
+ def test_block_f32_n3_matches_single(self):
409
+ """Block f32 results must match per-spectrum single fits (within f32 tolerance)."""
410
+ x, y, err, p0, bnd = self._make_block_data()
411
+ result_block = fmpfit_f32_block_pywrap(0, x, y, err, p0, bnd,
412
+ xtol=1e-6, ftol=1e-6, gtol=1e-6, maxiter=2000)
413
+
414
+ bnd_1d = bnd[0]
415
+ for i in range(3):
416
+ parinfo_i = [{'value': float(p0[i, j]), 'limits': [float(bnd_1d[j, 0]), float(bnd_1d[j, 1])]}
417
+ for j in range(3)]
418
+ r_single = fmpfit_f32_pywrap(0, parinfo=parinfo_i,
419
+ functkw={'x': x[i], 'y': y[i], 'error': err[i]},
420
+ xtol=1e-6, ftol=1e-6, gtol=1e-6, maxiter=2000)
421
+ np.testing.assert_allclose(result_block['best_params'][i], r_single.best_params,
422
+ rtol=1e-5,
423
+ err_msg=f"block != single for spectrum {i} (f32)")
424
+
425
+
426
+ if __name__ == '__main__':
427
+ pytest.main([__file__, '-v'])
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes