sigima 0.0.1.dev0__py3-none-any.whl → 1.0.0__py3-none-any.whl

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 (264) hide show
  1. sigima/__init__.py +142 -2
  2. sigima/client/__init__.py +105 -0
  3. sigima/client/base.py +780 -0
  4. sigima/client/remote.py +469 -0
  5. sigima/client/stub.py +814 -0
  6. sigima/client/utils.py +90 -0
  7. sigima/config.py +444 -0
  8. sigima/data/logo/Sigima.svg +135 -0
  9. sigima/data/tests/annotations.json +798 -0
  10. sigima/data/tests/curve_fitting/exponential_fit.txt +511 -0
  11. sigima/data/tests/curve_fitting/gaussian_fit.txt +100 -0
  12. sigima/data/tests/curve_fitting/piecewiseexponential_fit.txt +1022 -0
  13. sigima/data/tests/curve_fitting/polynomial_fit.txt +100 -0
  14. sigima/data/tests/curve_fitting/twohalfgaussian_fit.txt +1000 -0
  15. sigima/data/tests/curve_formats/bandwidth.txt +201 -0
  16. sigima/data/tests/curve_formats/boxcar.npy +0 -0
  17. sigima/data/tests/curve_formats/datetime.txt +1001 -0
  18. sigima/data/tests/curve_formats/dynamic_parameters.txt +4000 -0
  19. sigima/data/tests/curve_formats/fw1e2.txt +301 -0
  20. sigima/data/tests/curve_formats/fwhm.txt +319 -0
  21. sigima/data/tests/curve_formats/multiple_curves.csv +29 -0
  22. sigima/data/tests/curve_formats/noised_saw.mat +0 -0
  23. sigima/data/tests/curve_formats/oscilloscope.csv +111 -0
  24. sigima/data/tests/curve_formats/other/other2/recursive2.txt +5 -0
  25. sigima/data/tests/curve_formats/other/recursive1.txt +5 -0
  26. sigima/data/tests/curve_formats/paracetamol.npy +0 -0
  27. sigima/data/tests/curve_formats/paracetamol.txt +1010 -0
  28. sigima/data/tests/curve_formats/paracetamol_dx_dy.csv +1000 -0
  29. sigima/data/tests/curve_formats/paracetamol_dy.csv +1001 -0
  30. sigima/data/tests/curve_formats/pulse1.npy +0 -0
  31. sigima/data/tests/curve_formats/pulse2.npy +0 -0
  32. sigima/data/tests/curve_formats/simple.txt +5 -0
  33. sigima/data/tests/curve_formats/spectrum.mca +2139 -0
  34. sigima/data/tests/curve_formats/square2.npy +0 -0
  35. sigima/data/tests/curve_formats/step.npy +0 -0
  36. sigima/data/tests/fabry-perot1.jpg +0 -0
  37. sigima/data/tests/fabry-perot2.jpg +0 -0
  38. sigima/data/tests/flower.npy +0 -0
  39. sigima/data/tests/image_formats/NF 180338201.scor-data +11003 -0
  40. sigima/data/tests/image_formats/binary_image.npy +0 -0
  41. sigima/data/tests/image_formats/binary_image.png +0 -0
  42. sigima/data/tests/image_formats/centroid_test.npy +0 -0
  43. sigima/data/tests/image_formats/coordinated_text/complex_image.txt +10011 -0
  44. sigima/data/tests/image_formats/coordinated_text/complex_ref_image.txt +10010 -0
  45. sigima/data/tests/image_formats/coordinated_text/image.txt +15 -0
  46. sigima/data/tests/image_formats/coordinated_text/image2.txt +14 -0
  47. sigima/data/tests/image_formats/coordinated_text/image_no_unit_no_label.txt +14 -0
  48. sigima/data/tests/image_formats/coordinated_text/image_with_nan.txt +15 -0
  49. sigima/data/tests/image_formats/coordinated_text/image_with_unit.txt +14 -0
  50. sigima/data/tests/image_formats/fiber.csv +480 -0
  51. sigima/data/tests/image_formats/fiber.jpg +0 -0
  52. sigima/data/tests/image_formats/fiber.png +0 -0
  53. sigima/data/tests/image_formats/fiber.txt +480 -0
  54. sigima/data/tests/image_formats/gaussian_spot_with_noise.npy +0 -0
  55. sigima/data/tests/image_formats/mr-brain.dcm +0 -0
  56. sigima/data/tests/image_formats/noised_gaussian.mat +0 -0
  57. sigima/data/tests/image_formats/sif_reader/nd_lum_image_no_glue.sif +0 -0
  58. sigima/data/tests/image_formats/sif_reader/raman1.sif +0 -0
  59. sigima/data/tests/image_formats/tiling.txt +10 -0
  60. sigima/data/tests/image_formats/uint16.tiff +0 -0
  61. sigima/data/tests/image_formats/uint8.tiff +0 -0
  62. sigima/data/tests/laser_beam/TEM00_z_13.jpg +0 -0
  63. sigima/data/tests/laser_beam/TEM00_z_18.jpg +0 -0
  64. sigima/data/tests/laser_beam/TEM00_z_23.jpg +0 -0
  65. sigima/data/tests/laser_beam/TEM00_z_30.jpg +0 -0
  66. sigima/data/tests/laser_beam/TEM00_z_35.jpg +0 -0
  67. sigima/data/tests/laser_beam/TEM00_z_40.jpg +0 -0
  68. sigima/data/tests/laser_beam/TEM00_z_45.jpg +0 -0
  69. sigima/data/tests/laser_beam/TEM00_z_50.jpg +0 -0
  70. sigima/data/tests/laser_beam/TEM00_z_55.jpg +0 -0
  71. sigima/data/tests/laser_beam/TEM00_z_60.jpg +0 -0
  72. sigima/data/tests/laser_beam/TEM00_z_65.jpg +0 -0
  73. sigima/data/tests/laser_beam/TEM00_z_70.jpg +0 -0
  74. sigima/data/tests/laser_beam/TEM00_z_75.jpg +0 -0
  75. sigima/data/tests/laser_beam/TEM00_z_80.jpg +0 -0
  76. sigima/enums.py +195 -0
  77. sigima/io/__init__.py +123 -0
  78. sigima/io/base.py +311 -0
  79. sigima/io/common/__init__.py +5 -0
  80. sigima/io/common/basename.py +164 -0
  81. sigima/io/common/converters.py +189 -0
  82. sigima/io/common/objmeta.py +181 -0
  83. sigima/io/common/textreader.py +58 -0
  84. sigima/io/convenience.py +157 -0
  85. sigima/io/enums.py +17 -0
  86. sigima/io/ftlab.py +395 -0
  87. sigima/io/image/__init__.py +9 -0
  88. sigima/io/image/base.py +177 -0
  89. sigima/io/image/formats.py +1016 -0
  90. sigima/io/image/funcs.py +414 -0
  91. sigima/io/signal/__init__.py +9 -0
  92. sigima/io/signal/base.py +129 -0
  93. sigima/io/signal/formats.py +290 -0
  94. sigima/io/signal/funcs.py +723 -0
  95. sigima/objects/__init__.py +260 -0
  96. sigima/objects/base.py +937 -0
  97. sigima/objects/image/__init__.py +88 -0
  98. sigima/objects/image/creation.py +556 -0
  99. sigima/objects/image/object.py +524 -0
  100. sigima/objects/image/roi.py +904 -0
  101. sigima/objects/scalar/__init__.py +57 -0
  102. sigima/objects/scalar/common.py +215 -0
  103. sigima/objects/scalar/geometry.py +502 -0
  104. sigima/objects/scalar/table.py +784 -0
  105. sigima/objects/shape.py +290 -0
  106. sigima/objects/signal/__init__.py +133 -0
  107. sigima/objects/signal/constants.py +27 -0
  108. sigima/objects/signal/creation.py +1428 -0
  109. sigima/objects/signal/object.py +444 -0
  110. sigima/objects/signal/roi.py +274 -0
  111. sigima/params.py +405 -0
  112. sigima/proc/__init__.py +96 -0
  113. sigima/proc/base.py +381 -0
  114. sigima/proc/decorator.py +330 -0
  115. sigima/proc/image/__init__.py +513 -0
  116. sigima/proc/image/arithmetic.py +335 -0
  117. sigima/proc/image/base.py +260 -0
  118. sigima/proc/image/detection.py +519 -0
  119. sigima/proc/image/edges.py +329 -0
  120. sigima/proc/image/exposure.py +406 -0
  121. sigima/proc/image/extraction.py +458 -0
  122. sigima/proc/image/filtering.py +219 -0
  123. sigima/proc/image/fourier.py +147 -0
  124. sigima/proc/image/geometry.py +661 -0
  125. sigima/proc/image/mathops.py +340 -0
  126. sigima/proc/image/measurement.py +195 -0
  127. sigima/proc/image/morphology.py +155 -0
  128. sigima/proc/image/noise.py +107 -0
  129. sigima/proc/image/preprocessing.py +182 -0
  130. sigima/proc/image/restoration.py +235 -0
  131. sigima/proc/image/threshold.py +217 -0
  132. sigima/proc/image/transformations.py +393 -0
  133. sigima/proc/signal/__init__.py +376 -0
  134. sigima/proc/signal/analysis.py +206 -0
  135. sigima/proc/signal/arithmetic.py +551 -0
  136. sigima/proc/signal/base.py +262 -0
  137. sigima/proc/signal/extraction.py +60 -0
  138. sigima/proc/signal/features.py +310 -0
  139. sigima/proc/signal/filtering.py +484 -0
  140. sigima/proc/signal/fitting.py +276 -0
  141. sigima/proc/signal/fourier.py +259 -0
  142. sigima/proc/signal/mathops.py +420 -0
  143. sigima/proc/signal/processing.py +580 -0
  144. sigima/proc/signal/stability.py +175 -0
  145. sigima/proc/title_formatting.py +227 -0
  146. sigima/proc/validation.py +272 -0
  147. sigima/tests/__init__.py +7 -0
  148. sigima/tests/common/__init__.py +0 -0
  149. sigima/tests/common/arithmeticparam_unit_test.py +26 -0
  150. sigima/tests/common/basename_unit_test.py +126 -0
  151. sigima/tests/common/client_unit_test.py +412 -0
  152. sigima/tests/common/converters_unit_test.py +77 -0
  153. sigima/tests/common/decorator_unit_test.py +176 -0
  154. sigima/tests/common/examples_unit_test.py +104 -0
  155. sigima/tests/common/kernel_normalization_unit_test.py +242 -0
  156. sigima/tests/common/roi_basic_unit_test.py +73 -0
  157. sigima/tests/common/roi_geometry_unit_test.py +171 -0
  158. sigima/tests/common/scalar_builder_unit_test.py +142 -0
  159. sigima/tests/common/scalar_unit_test.py +991 -0
  160. sigima/tests/common/shape_unit_test.py +183 -0
  161. sigima/tests/common/stat_unit_test.py +138 -0
  162. sigima/tests/common/title_formatting_unit_test.py +338 -0
  163. sigima/tests/common/tools_coordinates_unit_test.py +60 -0
  164. sigima/tests/common/transformations_unit_test.py +178 -0
  165. sigima/tests/common/validation_unit_test.py +205 -0
  166. sigima/tests/conftest.py +129 -0
  167. sigima/tests/data.py +998 -0
  168. sigima/tests/env.py +280 -0
  169. sigima/tests/guiutils.py +163 -0
  170. sigima/tests/helpers.py +532 -0
  171. sigima/tests/image/__init__.py +28 -0
  172. sigima/tests/image/binning_unit_test.py +128 -0
  173. sigima/tests/image/blob_detection_unit_test.py +312 -0
  174. sigima/tests/image/centroid_unit_test.py +170 -0
  175. sigima/tests/image/check_2d_array_unit_test.py +63 -0
  176. sigima/tests/image/contour_unit_test.py +172 -0
  177. sigima/tests/image/convolution_unit_test.py +178 -0
  178. sigima/tests/image/datatype_unit_test.py +67 -0
  179. sigima/tests/image/edges_unit_test.py +155 -0
  180. sigima/tests/image/enclosingcircle_unit_test.py +88 -0
  181. sigima/tests/image/exposure_unit_test.py +223 -0
  182. sigima/tests/image/fft2d_unit_test.py +189 -0
  183. sigima/tests/image/filtering_unit_test.py +166 -0
  184. sigima/tests/image/geometry_unit_test.py +654 -0
  185. sigima/tests/image/hough_circle_unit_test.py +147 -0
  186. sigima/tests/image/imageobj_unit_test.py +737 -0
  187. sigima/tests/image/morphology_unit_test.py +71 -0
  188. sigima/tests/image/noise_unit_test.py +57 -0
  189. sigima/tests/image/offset_correction_unit_test.py +72 -0
  190. sigima/tests/image/operation_unit_test.py +518 -0
  191. sigima/tests/image/peak2d_limits_unit_test.py +41 -0
  192. sigima/tests/image/peak2d_unit_test.py +133 -0
  193. sigima/tests/image/profile_unit_test.py +159 -0
  194. sigima/tests/image/projections_unit_test.py +121 -0
  195. sigima/tests/image/restoration_unit_test.py +141 -0
  196. sigima/tests/image/roi2dparam_unit_test.py +53 -0
  197. sigima/tests/image/roi_advanced_unit_test.py +588 -0
  198. sigima/tests/image/roi_grid_unit_test.py +279 -0
  199. sigima/tests/image/spectrum2d_unit_test.py +40 -0
  200. sigima/tests/image/threshold_unit_test.py +91 -0
  201. sigima/tests/io/__init__.py +0 -0
  202. sigima/tests/io/addnewformat_unit_test.py +125 -0
  203. sigima/tests/io/convenience_funcs_unit_test.py +470 -0
  204. sigima/tests/io/coordinated_text_format_unit_test.py +495 -0
  205. sigima/tests/io/datetime_csv_unit_test.py +198 -0
  206. sigima/tests/io/imageio_formats_test.py +41 -0
  207. sigima/tests/io/ioregistry_unit_test.py +69 -0
  208. sigima/tests/io/objmeta_unit_test.py +87 -0
  209. sigima/tests/io/readobj_unit_test.py +130 -0
  210. sigima/tests/io/readwriteobj_unit_test.py +67 -0
  211. sigima/tests/signal/__init__.py +0 -0
  212. sigima/tests/signal/analysis_unit_test.py +135 -0
  213. sigima/tests/signal/check_1d_arrays_unit_test.py +169 -0
  214. sigima/tests/signal/convolution_unit_test.py +404 -0
  215. sigima/tests/signal/datetime_unit_test.py +176 -0
  216. sigima/tests/signal/fft1d_unit_test.py +303 -0
  217. sigima/tests/signal/filters_unit_test.py +403 -0
  218. sigima/tests/signal/fitting_unit_test.py +929 -0
  219. sigima/tests/signal/fwhm_unit_test.py +111 -0
  220. sigima/tests/signal/noise_unit_test.py +128 -0
  221. sigima/tests/signal/offset_correction_unit_test.py +34 -0
  222. sigima/tests/signal/operation_unit_test.py +489 -0
  223. sigima/tests/signal/peakdetection_unit_test.py +145 -0
  224. sigima/tests/signal/processing_unit_test.py +657 -0
  225. sigima/tests/signal/pulse/__init__.py +112 -0
  226. sigima/tests/signal/pulse/crossing_times_unit_test.py +123 -0
  227. sigima/tests/signal/pulse/plateau_detection_unit_test.py +102 -0
  228. sigima/tests/signal/pulse/pulse_unit_test.py +1824 -0
  229. sigima/tests/signal/roi_advanced_unit_test.py +392 -0
  230. sigima/tests/signal/signalobj_unit_test.py +603 -0
  231. sigima/tests/signal/stability_unit_test.py +431 -0
  232. sigima/tests/signal/uncertainty_unit_test.py +611 -0
  233. sigima/tests/vistools.py +1030 -0
  234. sigima/tools/__init__.py +59 -0
  235. sigima/tools/checks.py +290 -0
  236. sigima/tools/coordinates.py +308 -0
  237. sigima/tools/datatypes.py +26 -0
  238. sigima/tools/image/__init__.py +97 -0
  239. sigima/tools/image/detection.py +451 -0
  240. sigima/tools/image/exposure.py +77 -0
  241. sigima/tools/image/extraction.py +48 -0
  242. sigima/tools/image/fourier.py +260 -0
  243. sigima/tools/image/geometry.py +190 -0
  244. sigima/tools/image/preprocessing.py +165 -0
  245. sigima/tools/signal/__init__.py +86 -0
  246. sigima/tools/signal/dynamic.py +254 -0
  247. sigima/tools/signal/features.py +135 -0
  248. sigima/tools/signal/filtering.py +171 -0
  249. sigima/tools/signal/fitting.py +1171 -0
  250. sigima/tools/signal/fourier.py +466 -0
  251. sigima/tools/signal/interpolation.py +70 -0
  252. sigima/tools/signal/peakdetection.py +126 -0
  253. sigima/tools/signal/pulse.py +1626 -0
  254. sigima/tools/signal/scaling.py +50 -0
  255. sigima/tools/signal/stability.py +258 -0
  256. sigima/tools/signal/windowing.py +90 -0
  257. sigima/worker.py +79 -0
  258. sigima-1.0.0.dist-info/METADATA +233 -0
  259. sigima-1.0.0.dist-info/RECORD +262 -0
  260. {sigima-0.0.1.dev0.dist-info → sigima-1.0.0.dist-info}/licenses/LICENSE +29 -29
  261. sigima-0.0.1.dev0.dist-info/METADATA +0 -60
  262. sigima-0.0.1.dev0.dist-info/RECORD +0 -6
  263. {sigima-0.0.1.dev0.dist-info → sigima-1.0.0.dist-info}/WHEEL +0 -0
  264. {sigima-0.0.1.dev0.dist-info → sigima-1.0.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,929 @@
1
+ # Copyright (c) DataLab Platform Developers, BSD 3-Clause license, see LICENSE file.
2
+
3
+ """
4
+ Curve fitting unit tests
5
+ ========================
6
+
7
+ This module contains comprehensive tests for the curve fitting functions
8
+ in sigima.tools.signal.fitting, validating mathematical accuracy and robustness.
9
+ """
10
+
11
+ # pylint: disable=invalid-name # Allows short reference names like x, y, ...
12
+ # pylint: disable=duplicate-code
13
+
14
+ from __future__ import annotations
15
+
16
+ from typing import Callable
17
+
18
+ import numpy as np
19
+ import pytest
20
+ import scipy.special
21
+
22
+ import sigima.objects
23
+ import sigima.proc.signal
24
+ from sigima.tests import guiutils
25
+ from sigima.tests.data import get_test_signal
26
+ from sigima.tests.env import execenv
27
+ from sigima.tests.helpers import check_array_result, check_scalar_result
28
+ from sigima.tools.signal import fitting, peakdetection, pulse
29
+
30
+ EXPECTED_FIT_PARAMS = {
31
+ "gaussian_fit": {
32
+ "amp": 151.5516963005346,
33
+ "sigma": 10.093908516282582,
34
+ "x0": 49.98522207721181,
35
+ "y0": 0.14038830988167578,
36
+ "fit_type": "gaussian",
37
+ },
38
+ "exponential_fit": {
39
+ "a": 23299.374597935774,
40
+ "b": -1.012051879085819,
41
+ "y0": 0.3018450161545937,
42
+ "fit_type": "exponential",
43
+ },
44
+ "twohalfgaussian_fit": {
45
+ "amp_left": 2.989346344212517,
46
+ "amp_right": 2.508788078396881,
47
+ "sigma_left": 0.9821153800588559,
48
+ "sigma_right": 4.737040821453857,
49
+ "x0": 0.9751190925078642,
50
+ "y0_left": 1.9970402083155143,
51
+ "y0_right": 2.4917164605006117,
52
+ "fit_type": "twohalfgaussian",
53
+ },
54
+ "piecewiseexponential_fit": {
55
+ "x_center": 4.985324084088387,
56
+ "a_left": 0.9784183389713168,
57
+ "b_left": 1.0050512118447683,
58
+ "a_right": 22480.004610557487,
59
+ "b_right": -1.00498734825442,
60
+ "y0": 0.05215861106687306,
61
+ "fit_type": "doubleexponential",
62
+ },
63
+ }
64
+
65
+
66
+ def __check_tools_proc_interface(
67
+ toolsfunc: Callable[..., np.ndarray],
68
+ procfunc: Callable[[sigima.objects.SignalObj], sigima.objects.SignalObj],
69
+ x: np.ndarray,
70
+ y: np.ndarray,
71
+ ):
72
+ """Helper to check interface between `sigima.tools` and `sigima.proc`."""
73
+ fitted_y, params = toolsfunc(x, y)
74
+ src = sigima.objects.create_signal("Test data", x, y)
75
+ dst = procfunc(src)
76
+ check_array_result(
77
+ f"{toolsfunc.__name__}-proc interface", dst.y, fitted_y, rtol=1e-10
78
+ )
79
+ guiutils.view_curves_if_gui([src, dst], title=f"Test {toolsfunc.__name__}")
80
+
81
+ # Also try to fit real experimental data if available
82
+ try:
83
+ experiment_signal = get_test_signal(f"{toolsfunc.__name__}.txt")
84
+ fitted_signal = procfunc(experiment_signal)
85
+ guiutils.view_curves_if_gui(
86
+ [experiment_signal, fitted_signal], title=f"Test {toolsfunc.__name__}"
87
+ )
88
+ fit_params = fitted_signal.metadata["fit_params"]
89
+ exp_params = EXPECTED_FIT_PARAMS.get(toolsfunc.__name__)
90
+ if exp_params is None:
91
+ for key, value in fit_params.items():
92
+ if isinstance(value, np.floating):
93
+ fit_params[key] = float(value)
94
+ raise ValueError(f"Unable to find expected params for: {repr(fit_params)}")
95
+ for key, exp_value in exp_params.items():
96
+ assert key in fit_params, f"Missing fit parameter: {key}"
97
+ act_value = fit_params[key]
98
+ if isinstance(exp_value, (int, float, np.floating)):
99
+ check_scalar_result(f"Parameter {key}", act_value, exp_value, rtol=1e-5)
100
+ else:
101
+ assert act_value == exp_value, (
102
+ f"Parameter {key} differs: {act_value} != {exp_value}"
103
+ )
104
+ except FileNotFoundError:
105
+ pass
106
+
107
+ return fitted_y, params
108
+
109
+
110
+ @pytest.mark.validation
111
+ def test_signal_linear_fit() -> None:
112
+ """Linear fitting validation test."""
113
+ execenv.print("Testing linear fitting with perfect synthetic data...")
114
+
115
+ # Generate perfect linear data
116
+ x = np.linspace(0, 10, 100)
117
+ a_true, b_true = 2.5, 1.3
118
+ y = a_true * x + b_true
119
+
120
+ fitted_y, params = fitting.linear_fit(x, y)
121
+ check_scalar_result("Linear fit slope", params["a"], a_true, rtol=1e-10)
122
+ check_scalar_result("Linear fit intercept", params["b"], b_true, rtol=1e-10)
123
+ check_array_result("Linear fit y-values", fitted_y, y, rtol=1e-10)
124
+
125
+ execenv.print("Testing linear fitting with noisy synthetic data...")
126
+
127
+ # Set random seed for reproducible tests
128
+ np.random.seed(42)
129
+
130
+ x = np.linspace(0, 10, 100)
131
+ a_true, b_true = 2.5, 1.3
132
+ y_clean = a_true * x + b_true
133
+ noise = np.random.normal(0, 0.1, len(x))
134
+ y = y_clean + noise
135
+
136
+ fitted_y, params = __check_tools_proc_interface(
137
+ fitting.linear_fit, sigima.proc.signal.linear_fit, x, y
138
+ )
139
+ # With noise, we expect reasonable accuracy
140
+ assert np.abs(params["a"] - a_true) < 0.05, "Slope should be accurate within 5%"
141
+ assert np.abs(params["b"] - b_true) < 0.1, "Intercept should be accurate within 0.1"
142
+
143
+
144
+ @pytest.mark.validation
145
+ def test_polynomial_fit() -> None:
146
+ """Polynomial fitting validation test."""
147
+ execenv.print("Testing polynomial fitting with perfect synthetic data...")
148
+
149
+ # Generate perfect quadratic data
150
+ x = np.linspace(-5, 5, 100)
151
+ a_true, b_true, c_true = 1.0, -2.0, 1.0
152
+ y = a_true * x**2 + b_true * x + c_true
153
+
154
+ fitted_y, params = fitting.polynomial_fit(x, y, degree=2)
155
+ check_scalar_result("Polynomial fit a", params["a"], a_true, rtol=1e-10)
156
+ check_scalar_result("Polynomial fit b", params["b"], b_true, rtol=1e-10)
157
+ check_scalar_result("Polynomial fit c", params["c"], c_true, rtol=1e-10)
158
+ check_array_result("Polynomial fit y-values", fitted_y, y, rtol=1e-10)
159
+
160
+ execenv.print("Testing polynomial fitting with noisy synthetic data...")
161
+
162
+ # Set random seed for reproducible tests
163
+ np.random.seed(123)
164
+
165
+ x = np.linspace(-5, 5, 100)
166
+ a_true, b_true, c_true = 1.0, -2.0, 1.0
167
+ y_clean = a_true * x**2 + b_true * x + c_true
168
+ noise = np.random.normal(0, 2.0, len(x))
169
+ y = y_clean + noise
170
+
171
+ # Test tools interface
172
+ fitted_y_tools, _params_tools = fitting.polynomial_fit(x, y, degree=2)
173
+
174
+ # Test proc interface (needs PolynomialFitParam)
175
+ src = sigima.objects.create_signal("Test data", x, y)
176
+ poly_param = sigima.proc.signal.PolynomialFitParam()
177
+ poly_param.degree = 2
178
+ dst = sigima.proc.signal.polynomial_fit(src, poly_param)
179
+ fitted_y = dst.y
180
+ params = dst.metadata["fit_params"]
181
+
182
+ # Check that both interfaces give similar results
183
+ check_array_result(
184
+ "polynomial_fit-proc interface", fitted_y, fitted_y_tools, rtol=1e-10
185
+ )
186
+ guiutils.view_curves_if_gui([src, dst], title="Test polynomial_fit")
187
+ # With noise, we expect reasonable accuracy
188
+ assert np.abs(params["a"] - a_true) < 0.1, "Coefficient a should be accurate"
189
+ assert np.abs(params["b"] - b_true) < 0.2, "Coefficient b should be accurate"
190
+ assert np.abs(params["c"] - c_true) < 0.5, "Coefficient c should be accurate"
191
+
192
+
193
+ @pytest.mark.validation
194
+ def test_signal_gaussian_fit() -> None:
195
+ """Gaussian fitting validation test."""
196
+ execenv.print("Testing Gaussian fitting with perfect synthetic data...")
197
+
198
+ x = np.linspace(-5, 5, 200)
199
+ peak_amplitude_true, sigma_true, x0_true, y0_true = 3.0, 1.5, 0.5, 0.2
200
+ # Generate data using the peak amplitude form
201
+ y = peak_amplitude_true * np.exp(-0.5 * ((x - x0_true) / sigma_true) ** 2) + y0_true
202
+
203
+ _fitted_y, params = fitting.gaussian_fit(x, y)
204
+
205
+ # Convert fitted amp to peak amplitude for comparison
206
+ fitted_peak_amplitude = pulse.GaussianModel.amplitude(
207
+ params["amp"], params["sigma"]
208
+ )
209
+ check_scalar_result(
210
+ "Gaussian peak amplitude", fitted_peak_amplitude, peak_amplitude_true, rtol=1e-3
211
+ )
212
+ check_scalar_result("Gaussian sigma", params["sigma"], sigma_true, rtol=1e-3)
213
+ check_scalar_result("Gaussian center", params["x0"], x0_true, rtol=1e-3)
214
+ check_scalar_result("Gaussian offset", params["y0"], y0_true, rtol=1e-3)
215
+
216
+ execenv.print("Testing Gaussian fitting with noisy synthetic data...")
217
+
218
+ # Set random seed for reproducible tests
219
+ np.random.seed(123)
220
+
221
+ x = np.linspace(-5, 5, 200)
222
+ amp_true, sigma_true, x0_true, y0_true = 3.0, 1.5, 0.5, 0.2
223
+ y_clean = amp_true * np.exp(-0.5 * ((x - x0_true) / sigma_true) ** 2) + y0_true
224
+ noise = np.random.normal(0, 0.05, len(x))
225
+ y = y_clean + noise
226
+
227
+ _fitted_y, params = __check_tools_proc_interface(
228
+ fitting.gaussian_fit, sigima.proc.signal.gaussian_fit, x, y
229
+ )
230
+ # With noise, expect reasonable accuracy
231
+ assert np.abs(params["x0"] - x0_true) < 0.2, "Gaussian center should be accurate"
232
+ assert np.abs(params["y0"] - y0_true) < 0.2, "Gaussian offset should be accurate"
233
+
234
+
235
+ @pytest.mark.validation
236
+ def test_signal_lorentzian_fit() -> None:
237
+ """Lorentzian fitting validation test."""
238
+ execenv.print("Testing Lorentzian fitting with perfect synthetic data...")
239
+
240
+ x = np.linspace(-10, 10, 200)
241
+ peak_amplitude_true, sigma_true, x0_true, y0_true = 4.0, 1.5, -0.5, 0.2
242
+
243
+ # Lorentzian function using peak amplitude: peak_amp / (1 + ((x - x0) / sigma)^2)
244
+ y = peak_amplitude_true / (1 + ((x - x0_true) / sigma_true) ** 2) + y0_true
245
+ y += np.random.normal(0, 0.1, len(x))
246
+
247
+ _fitted_y, params = __check_tools_proc_interface(
248
+ fitting.lorentzian_fit, sigima.proc.signal.lorentzian_fit, x, y
249
+ )
250
+
251
+ # Convert fitted amp to peak amplitude for comparison
252
+ fitted_peak_amplitude = pulse.LorentzianModel.amplitude(
253
+ params["amp"], params["sigma"]
254
+ )
255
+ check_scalar_result(
256
+ "Lorentzian peak amplitude",
257
+ fitted_peak_amplitude,
258
+ peak_amplitude_true,
259
+ rtol=1e-2,
260
+ )
261
+ assert np.abs(params["sigma"] - sigma_true) / sigma_true < 0.1, (
262
+ "Sigma should be accurate"
263
+ )
264
+ assert np.abs(params["x0"] - x0_true) < 0.1, "Center should be accurate"
265
+ assert np.abs(params["y0"] - y0_true) < 0.1, "Offset should be accurate"
266
+
267
+
268
+ @pytest.mark.validation
269
+ def test_signal_voigt_fit() -> None:
270
+ """Voigt fitting validation test."""
271
+ execenv.print("Testing Voigt fitting with synthetic data...")
272
+
273
+ # Generate synthetic Voigt-like data (approximate using Gaussian for simplicity)
274
+ x = np.linspace(-10, 10, 200)
275
+ amplitude_true = 2.0
276
+ sigma_true = 1.5
277
+ x0_true = 2.0
278
+ y0_true = 0.5
279
+
280
+ # Use Gaussian as approximation for test data
281
+ y = amplitude_true * np.exp(-0.5 * ((x - x0_true) / sigma_true) ** 2) + y0_true
282
+ y += np.random.normal(0, 0.02, x.size) # Add small amount of noise
283
+
284
+ fitted_y, params = __check_tools_proc_interface(
285
+ fitting.voigt_fit, sigima.proc.signal.voigt_fit, x, y
286
+ )
287
+
288
+ # Check that fitted curve is reasonable
289
+ assert isinstance(fitted_y, np.ndarray), "Fitted y should be numpy array"
290
+ assert fitted_y.shape == y.shape, "Fitted y should have same shape as input"
291
+
292
+ # Check parameter structure
293
+ assert "amp" in params, "Should have amplitude parameter"
294
+ assert "sigma" in params, "Should have sigma parameter"
295
+ assert "x0" in params, "Should have x0 parameter"
296
+ assert "y0" in params, "Should have y0 parameter"
297
+
298
+ # Parameters should be reasonable (within factor of 5 of true values)
299
+ assert 0.1 * amplitude_true < params["amp"] < 5 * amplitude_true
300
+ assert 0.1 * sigma_true < params["sigma"] < 5 * sigma_true
301
+ assert x0_true - 2 * sigma_true < params["x0"] < x0_true + 2 * sigma_true
302
+
303
+
304
+ @pytest.mark.validation
305
+ def test_signal_exponential_fit() -> None:
306
+ """Exponential decay fitting validation test."""
307
+ execenv.print("Testing exponential decay fitting...")
308
+
309
+ x = np.linspace(0, 5, 100)
310
+ a_true, b_true, y0_true = 10.0, -2.0, 1.0
311
+ y = a_true * np.exp(b_true * x) + y0_true
312
+ y += np.random.normal(0, 0.2, len(x)) # Add some noise
313
+
314
+ _fitted_y, params = __check_tools_proc_interface(
315
+ fitting.exponential_fit, sigima.proc.signal.exponential_fit, x, y
316
+ )
317
+ # Check parameter accuracy
318
+ assert np.abs(params["a"] - a_true) / a_true < 0.1, "Amplitude should be accurate"
319
+ assert np.abs(params["b"] - b_true) / abs(b_true) < 0.1, (
320
+ "Decay rate should be accurate"
321
+ )
322
+ assert np.abs(params["y0"] - y0_true) < 0.2, "Offset should be accurate"
323
+
324
+ execenv.print("Testing exponential growth fitting...")
325
+
326
+ x = np.linspace(0, 3, 50)
327
+ a_true, b_true, y0_true = 2.0, 1.5, 0.5
328
+ y = a_true * np.exp(b_true * x) + y0_true
329
+
330
+ _fitted_y, params = fitting.exponential_fit(x, y)
331
+ # Growth fitting is more challenging due to rapid increase
332
+ assert np.abs(params["b"] - b_true) / b_true < 0.2, (
333
+ "Growth rate should be reasonably accurate"
334
+ )
335
+
336
+
337
+ @pytest.mark.validation
338
+ def test_signal_piecewiseexponential_fit() -> None:
339
+ """Piecewise exponential (raise-decay) fitting validation test."""
340
+ execenv.print("Testing piecewise exponential (raise-decay) fitting...")
341
+
342
+ # Set random seed for reproducible test results
343
+ np.random.seed(42)
344
+
345
+ x = np.linspace(0, 10, 100)
346
+ amp1_true, amp2_true = 8.0, 3.0
347
+ tau1_true, tau2_true = 0.5, 3.0
348
+ y0_true = 1.0
349
+
350
+ y = (
351
+ amp1_true * np.exp(-x / tau1_true)
352
+ + amp2_true * np.exp(-x / tau2_true)
353
+ + y0_true
354
+ + np.random.normal(0, 0.2, len(x))
355
+ )
356
+
357
+ fitted_y, _params = __check_tools_proc_interface(
358
+ fitting.piecewiseexponential_fit,
359
+ sigima.proc.signal.piecewiseexponential_fit,
360
+ x,
361
+ y,
362
+ )
363
+
364
+ # Verify the fit quality is good (R² > 0.95)
365
+ r2 = 1 - np.sum((y - fitted_y) ** 2) / np.sum((y - np.mean(y)) ** 2)
366
+ assert r2 > 0.95, f"Fit quality R² = {r2:.3f} should be > 0.95"
367
+
368
+ # Verify the overall model makes sense: check that fitted curve is reasonable
369
+ rms_error = np.sqrt(np.mean((y - fitted_y) ** 2))
370
+ assert rms_error < 1.0, f"RMS error = {rms_error:.3f} should be < 1.0"
371
+
372
+
373
+ @pytest.mark.validation
374
+ def test_signal_planckian_fit() -> None:
375
+ """Planckian fitting validation test.
376
+
377
+ This test uses realistic parameters that produce a characteristic
378
+ blackbody radiation curve with a prominent peak, as would be
379
+ observed in thermal radiation measurements.
380
+ """
381
+ execenv.print("Testing Planckian fitting with synthetic blackbody data...")
382
+
383
+ # Wavelength range in micrometers (typical range for thermal radiation)
384
+ x = np.linspace(0.5, 5.0, 150)
385
+
386
+ # True parameters for realistic blackbody curve with more prominent peak
387
+ amp_true = 50.0 # Higher amplitude for more prominent peak
388
+ x0_true = 1.0 # Peak wavelength (Wien's displacement)
389
+ sigma_true = 0.8 # Temperature factor (sharper curve)
390
+ y0_true = 0.5 # Baseline offset
391
+
392
+ # Generate true Planckian data using the actual model
393
+ y = fitting.PlanckianFitComputer.evaluate(
394
+ x, amp=amp_true, x0=x0_true, sigma=sigma_true, y0=y0_true
395
+ )
396
+
397
+ # Add realistic noise (proportional to signal strength)
398
+ noise_level = 0.02 * (np.max(y) - np.min(y))
399
+ y += np.random.normal(0, noise_level, x.size)
400
+
401
+ fitted_y, params = __check_tools_proc_interface(
402
+ fitting.planckian_fit, sigima.proc.signal.planckian_fit, x, y
403
+ )
404
+
405
+ # Check that fitted curve is reasonable
406
+ assert isinstance(fitted_y, np.ndarray), "Fitted y should be numpy array"
407
+ assert fitted_y.shape == y.shape, "Fitted y should have same shape as input"
408
+
409
+ # Check parameter structure
410
+ assert "amp" in params, "Should have amp parameter"
411
+ assert "x0" in params, "Should have x0 parameter"
412
+ assert "sigma" in params, "Should have sigma parameter"
413
+ assert "y0" in params, "Should have y0 parameter"
414
+
415
+ # Check that the fit produces a realistic peak location
416
+ # The peak should be close to the Wien displacement law prediction
417
+ peak_x_fitted = x[np.argmax(fitted_y)]
418
+ peak_x_true = x[np.argmax(y)]
419
+
420
+ execenv.print(f"True peak at: {peak_x_true:.3f} μm")
421
+ execenv.print(f"Fitted peak at: {peak_x_fitted:.3f} μm")
422
+ execenv.print(
423
+ f"Fitted parameters: amp={params['amp']:.2f}, "
424
+ f"x0={params['x0']:.2f}, σ={params['sigma']:.2f}"
425
+ )
426
+ # Peak location should be reasonably accurate (within 20% of wavelength range)
427
+ # Planckian fitting can be challenging due to the complex function form
428
+ wavelength_tolerance = 0.20 * (x[-1] - x[0])
429
+ assert np.abs(peak_x_fitted - peak_x_true) < wavelength_tolerance, (
430
+ f"Peak location accuracy: fitted={peak_x_fitted:.3f}, true={peak_x_true:.3f}"
431
+ )
432
+
433
+ # Check that the curve has a reasonable dynamic range
434
+ # Use a more relaxed criterion since Planckian curves can be quite flat
435
+ dynamic_range = np.max(fitted_y) - np.min(fitted_y)
436
+ mean_level = np.mean(fitted_y)
437
+ assert dynamic_range > 0.05 * mean_level, (
438
+ "Fitted curve should have reasonable dynamic range"
439
+ )
440
+
441
+ # Check that there is a discernible peak (not completely flat)
442
+ peak_value = np.max(fitted_y)
443
+ edge_values = [fitted_y[0], fitted_y[-1]]
444
+ max_edge = np.max(edge_values)
445
+ assert peak_value > max_edge, "Peak should be higher than edge values"
446
+
447
+ # Parameters should be in reasonable ranges for physical systems
448
+ assert params["amp"] > 0, "Amplitude should be positive"
449
+ assert params["x0"] > 0, "Peak wavelength should be positive"
450
+ assert params["sigma"] > 0, "Sigma should be positive"
451
+
452
+
453
+ @pytest.mark.validation
454
+ def test_signal_cdf_fit() -> None:
455
+ """CDF fitting validation test."""
456
+ execenv.print("Testing CDF fitting with synthetic data...")
457
+
458
+ # Generate synthetic CDF data
459
+ x = np.linspace(-5, 5, 200)
460
+ amplitude_true = 2.0
461
+ mu_true = 0.5
462
+ sigma_true = 1.0
463
+ baseline_true = 1.0
464
+
465
+ # Generate CDF data using error function
466
+ erf = scipy.special.erf # pylint: disable=no-member
467
+ y = amplitude_true * erf((x - mu_true) / (sigma_true * np.sqrt(2))) + baseline_true
468
+ y += np.random.normal(0, 0.05, x.size) # Add small amount of noise
469
+
470
+ fitted_y, params = __check_tools_proc_interface(
471
+ fitting.cdf_fit, sigima.proc.signal.cdf_fit, x, y
472
+ )
473
+
474
+ # Check that fitted curve is reasonable
475
+ assert isinstance(fitted_y, np.ndarray), "Fitted y should be numpy array"
476
+ assert fitted_y.shape == y.shape, "Fitted y should have same shape as input"
477
+
478
+ # Check parameter structure
479
+ assert "amplitude" in params, "Should have amplitude parameter"
480
+ assert "mu" in params, "Should have mu parameter"
481
+ assert "sigma" in params, "Should have sigma parameter"
482
+ assert "baseline" in params, "Should have baseline parameter"
483
+
484
+ # Parameters should be reasonable (within factor of 3 of true values)
485
+ assert 0.3 * amplitude_true < params["amplitude"] < 3 * amplitude_true
486
+ assert 0.3 * sigma_true < params["sigma"] < 3 * sigma_true
487
+ assert mu_true - 2 * sigma_true < params["mu"] < mu_true + 2 * sigma_true
488
+
489
+
490
+ @pytest.mark.validation
491
+ def test_signal_sigmoid_fit() -> None:
492
+ """Sigmoid fitting validation test."""
493
+ execenv.print("Testing Sigmoid fitting with synthetic data...")
494
+
495
+ # Generate synthetic sigmoid data
496
+ x = np.linspace(-5, 5, 200)
497
+ amplitude_true = 3.0
498
+ k_true = 1.0
499
+ x0_true = 1.0
500
+ offset_true = 0.5
501
+
502
+ # Generate sigmoid data
503
+ y = offset_true + amplitude_true / (1.0 + np.exp(-k_true * (x - x0_true)))
504
+ y += np.random.normal(0, 0.05, x.size) # Add small amount of noise
505
+
506
+ fitted_y, params = __check_tools_proc_interface(
507
+ fitting.sigmoid_fit, sigima.proc.signal.sigmoid_fit, x, y
508
+ )
509
+
510
+ # Check that fitted curve is reasonable
511
+ assert isinstance(fitted_y, np.ndarray), "Fitted y should be numpy array"
512
+ assert fitted_y.shape == y.shape, "Fitted y should have same shape as input"
513
+
514
+ # Check parameter structure
515
+ assert "amplitude" in params, "Should have amplitude parameter"
516
+ assert "k" in params, "Should have k parameter"
517
+ assert "x0" in params, "Should have x0 parameter"
518
+ assert "offset" in params, "Should have offset parameter"
519
+
520
+ # Parameters should be reasonable (within factor of 3 of true values)
521
+ assert 0.3 * amplitude_true < params["amplitude"] < 3 * amplitude_true
522
+ assert 0.3 * k_true < params["k"] < 3 * k_true
523
+ assert x0_true - 2 < params["x0"] < x0_true + 2
524
+
525
+
526
+ @pytest.mark.validation
527
+ def test_signal_twohalfgaussian_fit() -> None:
528
+ """Two half-Gaussian fitting validation test."""
529
+ execenv.print("Testing two half-Gaussian fitting...")
530
+
531
+ x = np.linspace(-5, 10, 200)
532
+ (
533
+ amp_true,
534
+ x0_true,
535
+ sigma_left_true,
536
+ sigma_right_true,
537
+ y0_left_true,
538
+ y0_right_true,
539
+ ) = (5.0, 2.0, 1.0, 2.5, 0.3, 0.5)
540
+
541
+ # Create asymmetric Gaussian with separate baselines (enhanced test)
542
+ y = np.zeros_like(x)
543
+ for i, xi in enumerate(x):
544
+ if xi < x0_true:
545
+ y[i] = y0_left_true + amp_true * np.exp(
546
+ -0.5 * ((xi - x0_true) / sigma_left_true) ** 2
547
+ )
548
+ else:
549
+ y[i] = y0_right_true + amp_true * np.exp(
550
+ -0.5 * ((xi - x0_true) / sigma_right_true) ** 2
551
+ )
552
+
553
+ # Add noise
554
+ y += np.random.normal(0, 0.1, len(x))
555
+
556
+ # Test the tools layer directly for now
557
+ _fitted_y, params = __check_tools_proc_interface(
558
+ fitting.twohalfgaussian_fit, sigima.proc.signal.twohalfgaussian_fit, x, y
559
+ )
560
+
561
+ # Check that center position is reasonable
562
+ assert np.abs(params["x0"] - x0_true) < 0.5, "Center should be accurate"
563
+
564
+ # Check that baseline offsets are reasonable
565
+ assert np.abs(params["y0_left"] - y0_left_true) < 0.3, (
566
+ "Left baseline should be accurate"
567
+ )
568
+ assert np.abs(params["y0_right"] - y0_right_true) < 0.3, (
569
+ "Right baseline should be accurate"
570
+ )
571
+
572
+ # Check that the fitted parameters make sense
573
+ assert params["sigma_left"] > 0, "Left sigma should be positive"
574
+ assert params["sigma_right"] > 0, "Right sigma should be positive"
575
+ assert "amp_left" in params, "Should have amp_left parameter"
576
+ assert "amp_right" in params, "Should have amp_right parameter"
577
+ assert params["amp_left"] > 0, "Left amplitude should be positive"
578
+ assert params["amp_right"] > 0, "Right amplitude should be positive"
579
+
580
+
581
+ # This is not a validation test as there is no computation function for multi
582
+ # gaussian fitting in sigima.proc.signal
583
+ def test_multigaussian_single_peak() -> None:
584
+ """Multi-Gaussian fitting validation test with single peak."""
585
+ execenv.print("Testing multi-Gaussian fitting with single peak...")
586
+
587
+ x = np.linspace(-10, 10, 200)
588
+ amp_true, sigma_true, x0_true, y0_true = 4.0, 1.5, -0.5, 0.2
589
+
590
+ # Single Gaussian
591
+ y = amp_true * np.exp(-0.5 * ((x - x0_true) / sigma_true) ** 2) + y0_true
592
+ y += np.random.normal(0, 0.02, len(x))
593
+
594
+ # Find peak indices for the function
595
+ peaks = peakdetection.peak_indices(
596
+ y, thres=0.2
597
+ ) # Use higher threshold for better detection
598
+ execenv.print(f"Detected peaks at indices: {peaks}")
599
+
600
+ # If no peaks detected, use manual peak
601
+ if len(peaks) == 0:
602
+ peak_idx = np.argmax(y)
603
+ peaks = np.array([peak_idx])
604
+
605
+ _y, params = fitting.multigaussian_fit(x, y, peak_indices=peaks.tolist())
606
+
607
+ # Check results - expect at least one peak
608
+ assert len(peaks) >= 1, "Should detect at least one peak"
609
+ assert "amp_1" in params, "Should have amplitude for first peak"
610
+ assert "sigma_1" in params, "Should have sigma for first peak"
611
+ assert "x0_1" in params, "Should have x0 for first peak"
612
+ assert "y0" in params, "Should have y0 baseline parameter"
613
+
614
+
615
+ # This is not a validation test as there is no computation function for multi
616
+ # gaussian fitting in sigima.proc.signal
617
+ def test_multigaussian_double_peak() -> None:
618
+ """Multi-Gaussian fitting validation test with double peaks."""
619
+ execenv.print("Testing multi-Gaussian fitting with double peaks...")
620
+
621
+ x = np.linspace(-10, 10, 300)
622
+ # Two well-separated peaks
623
+ amp1, sigma1, x01 = 3.0, 1.0, -3.0
624
+ amp2, sigma2, x02 = 2.0, 1.5, 4.0
625
+ y0_true = 0.1
626
+
627
+ y = (
628
+ amp1 * np.exp(-0.5 * ((x - x01) / sigma1) ** 2)
629
+ + amp2 * np.exp(-0.5 * ((x - x02) / sigma2) ** 2)
630
+ + y0_true
631
+ )
632
+ y += np.random.normal(0, 0.02, len(x))
633
+
634
+ # Find peak indices
635
+ peaks = peakdetection.peak_indices(y, thres=0.3, min_dist=20)
636
+ execenv.print(f"Detected peaks at indices: {peaks}")
637
+
638
+ # If insufficient peaks detected, use manual peaks
639
+ if len(peaks) < 2:
640
+ peaks = np.array([np.argmax(y[:150]), 150 + np.argmax(y[150:])])
641
+
642
+ try:
643
+ yf, params = fitting.multigaussian_fit(x, y, peak_indices=peaks.tolist())
644
+ guiutils.view_curves_if_gui([[x, y], [x, yf]], title="Test multigaussian_fit")
645
+
646
+ # Check that we detected two peaks and got results
647
+ assert len(peaks) >= 2, "Should detect at least two peaks"
648
+ assert "amp_1" in params, "Should have amplitude for first peak"
649
+ assert "amp_2" in params, "Should have amplitude for second peak"
650
+ assert "sigma_1" in params, "Should have sigma for first peak"
651
+ assert "sigma_2" in params, "Should have sigma for second peak"
652
+ assert "x0_1" in params, "Should have x0 for first peak"
653
+ assert "x0_2" in params, "Should have x0 for second peak"
654
+ assert "y0" in params, "Should have y0 baseline parameter"
655
+ except ValueError as e:
656
+ if "infeasible" in str(e):
657
+ execenv.print(
658
+ "Multi-Gaussian fit failed due to optimization bounds "
659
+ "(expected for complex fitting)"
660
+ )
661
+ else:
662
+ raise
663
+
664
+
665
+ # This is not a validation test as there is no computation function for multi
666
+ # lorentzian fitting in sigima.proc.signal
667
+ def test_multilorentzian_single_peak() -> None:
668
+ """Multi-Lorentzian fitting validation test with single peak."""
669
+ execenv.print("Testing multi-Lorentzian fitting with single peak...")
670
+
671
+ x = np.linspace(-10, 10, 200)
672
+ amp_true, sigma_true, x0_true, y0_true = 4.0, 1.5, -0.5, 0.2
673
+
674
+ # Single Lorentzian
675
+ y = amp_true / (1 + ((x - x0_true) / sigma_true) ** 2) + y0_true
676
+ y += np.random.normal(0, 0.02, len(x))
677
+
678
+ # Find peak indices for the function
679
+ peaks = peakdetection.peak_indices(
680
+ y, thres=0.2
681
+ ) # Use higher threshold for better detection
682
+ execenv.print(f"Detected peaks at indices: {peaks}")
683
+
684
+ # If no peaks detected, use manual peak
685
+ if len(peaks) == 0:
686
+ peak_idx = np.argmax(y)
687
+ peaks = np.array([peak_idx])
688
+
689
+ _y, params = fitting.multilorentzian_fit(x, y, peak_indices=peaks.tolist())
690
+
691
+ # Check results - expect at least one peak
692
+ assert len(peaks) >= 1, "Should detect at least one peak"
693
+ assert "amp_1" in params, "Should have amplitude for first peak"
694
+ assert "sigma_1" in params, "Should have sigma for first peak"
695
+ assert "x0_1" in params, "Should have x0 for first peak"
696
+ assert "y0" in params, "Should have y0 baseline parameter"
697
+
698
+
699
+ # This is not a validation test as there is no computation function for multi
700
+ # lorentzian fitting in sigima.proc.signal
701
+ def test_multilorentzian_double_peak() -> None:
702
+ """Multi-Lorentzian fitting validation test with double peaks."""
703
+ execenv.print("Testing multi-Lorentzian fitting with double peaks...")
704
+
705
+ x = np.linspace(-10, 10, 300)
706
+ # Two well-separated peaks
707
+ amp1, sigma1, x01 = 3.0, 1.0, -3.0
708
+ amp2, sigma2, x02 = 2.0, 1.5, 4.0
709
+ y0_true = 0.1
710
+
711
+ y = (
712
+ amp1 / (1 + ((x - x01) / sigma1) ** 2)
713
+ + amp2 / (1 + ((x - x02) / sigma2) ** 2)
714
+ + y0_true
715
+ )
716
+ y += np.random.normal(0, 0.02, len(x))
717
+
718
+ # Find peak indices
719
+ peaks = peakdetection.peak_indices(y, thres=0.3, min_dist=20)
720
+ execenv.print(f"Detected peaks at indices: {peaks}")
721
+
722
+ # If insufficient peaks detected, use manual peaks
723
+ if len(peaks) < 2:
724
+ peaks = np.array([np.argmax(y[:150]), 150 + np.argmax(y[150:])])
725
+
726
+ try:
727
+ yf, params = fitting.multilorentzian_fit(x, y, peak_indices=peaks.tolist())
728
+ guiutils.view_curves_if_gui([[x, y], [x, yf]], title="Test multilorentzian_fit")
729
+
730
+ # Check that we detected two peaks and got results
731
+ assert len(peaks) >= 2, "Should detect at least two peaks"
732
+ assert "amp_1" in params, "Should have amplitude for first peak"
733
+ assert "amp_2" in params, "Should have amplitude for second peak"
734
+ assert "sigma_1" in params, "Should have sigma for first peak"
735
+ assert "sigma_2" in params, "Should have sigma for second peak"
736
+ assert "x0_1" in params, "Should have x0 for first peak"
737
+ assert "x0_2" in params, "Should have x0 for second peak"
738
+ assert "y0" in params, "Should have y0 baseline parameter"
739
+ except ValueError as e:
740
+ if "infeasible" in str(e):
741
+ execenv.print(
742
+ "Multi-Lorentzian fit failed due to optimization bounds "
743
+ "(expected for complex fitting)"
744
+ )
745
+ else:
746
+ raise
747
+
748
+
749
+ @pytest.mark.validation
750
+ def test_sinusoidal_fit() -> None:
751
+ """Sinusoidal fitting validation test."""
752
+ execenv.print("Testing sinusoidal fitting with synthetic data...")
753
+
754
+ # Generate synthetic sinusoidal data
755
+ x = np.linspace(0, 10, 200)
756
+ amplitude_true = 2.0
757
+ frequency_true = 1.5 # cycles per unit x
758
+ phase_true = 0.5 # radians
759
+ offset_true = 1.0
760
+
761
+ # Generate sinusoidal data
762
+ y = offset_true + amplitude_true * np.sin(
763
+ 2 * np.pi * frequency_true * x + phase_true
764
+ )
765
+ y += np.random.normal(0, 0.1, x.size) # Add small amount of noise
766
+
767
+ fitted_y, params = __check_tools_proc_interface(
768
+ fitting.sinusoidal_fit, sigima.proc.signal.sinusoidal_fit, x, y
769
+ )
770
+
771
+ # Check that fitted curve is reasonable
772
+ assert isinstance(fitted_y, np.ndarray), "Fitted y should be numpy array"
773
+ assert fitted_y.shape == y.shape, "Fitted y should have same shape as input"
774
+
775
+ # Check parameter structure
776
+ assert "amplitude" in params, "Should have amplitude parameter"
777
+ assert "frequency" in params, "Should have frequency parameter"
778
+ assert "phase" in params, "Should have phase parameter"
779
+ assert "offset" in params, "Should have offset parameter"
780
+
781
+ # Parameters should be reasonable (within factor of 2 of true values)
782
+ assert 0.5 * amplitude_true < params["amplitude"] < 2 * amplitude_true
783
+ assert 0.5 * frequency_true < params["frequency"] < 2 * frequency_true
784
+ assert -np.pi < params["phase"] < np.pi # Phase should be within -π to π
785
+ assert 0.5 * offset_true < params["offset"] < 2 * offset_true
786
+
787
+
788
+ def test_fitting_error_handling() -> None:
789
+ """Test error handling in fitting functions."""
790
+ execenv.print("Testing fitting error handling...")
791
+
792
+ # Test with insufficient data points
793
+ x_short = np.array([1, 2])
794
+ y_short = np.array([1, 2])
795
+
796
+ fitted_y, params = fitting.linear_fit(x_short, y_short)
797
+ # Should either succeed (linear fit needs only 2 points) or fail gracefully
798
+ assert isinstance(fitted_y, np.ndarray), "Result should be a numpy array"
799
+ assert "a" in params and "b" in params, "Params should have a and b attributes"
800
+
801
+ # Test with mismatched array sizes - this should raise an exception
802
+ x_mismatch = np.array([1, 2, 3])
803
+ y_mismatch = np.array([1, 2])
804
+
805
+ try:
806
+ fitted_y, params = fitting.linear_fit(x_mismatch, y_mismatch)
807
+ # If no exception is raised, we just check that we got some result
808
+ assert fitted_y is not None, (
809
+ "Should get some result even with mismatched arrays"
810
+ )
811
+ except (TypeError, ValueError):
812
+ # This is expected behavior - the function raises an exception
813
+ pass
814
+
815
+
816
+ def test_fitting_functions_available() -> None:
817
+ """Test that expected fitting functions are available."""
818
+ execenv.print("Testing availability of fitting functions...")
819
+
820
+ # Check that expected functions exist and are callable
821
+ expected_functions = [
822
+ "linear_fit",
823
+ "gaussian_fit",
824
+ "lorentzian_fit",
825
+ "voigt_fit",
826
+ "exponential_fit",
827
+ "piecewiseexponential_fit",
828
+ "planckian_fit",
829
+ "cdf_fit",
830
+ "sigmoid_fit",
831
+ "twohalfgaussian_fit",
832
+ "multilorentzian_fit",
833
+ "sinusoidal_fit",
834
+ ]
835
+
836
+ for func_name in expected_functions:
837
+ assert hasattr(fitting, func_name), f"Function {func_name} should exist"
838
+ func = getattr(fitting, func_name)
839
+ assert callable(func), f"Function {func_name} should be callable"
840
+
841
+
842
+ @pytest.mark.validation
843
+ def test_signal_evaluate_fit() -> None:
844
+ """Test evaluate_fit as a computation function (2-to-1)."""
845
+ execenv.print("Testing evaluate_fit computation function...")
846
+
847
+ # Create a signal with linear data
848
+ x1 = np.linspace(0, 10, 100)
849
+ y1 = 3.0 * x1 + 1.0 + np.random.normal(0, 0.5, len(x1))
850
+ src1 = sigima.objects.create_signal("Test data 1", x1, y1)
851
+
852
+ # Perform a fit to get fit parameters
853
+ fitted_signal = sigima.proc.signal.linear_fit(src1)
854
+ assert "fit_params" in fitted_signal.metadata, "Fit should produce metadata"
855
+
856
+ # Create a second signal with a different x-axis
857
+ x2 = np.linspace(-5, 15, 50)
858
+ y2 = np.zeros_like(x2) # Data doesn't matter, only x-axis is used
859
+ src2 = sigima.objects.create_signal("Test data 2", x2, y2)
860
+
861
+ # Evaluate fit from src1 on x-axis of src2 (2-to-1 operation)
862
+ result = sigima.proc.signal.evaluate_fit(fitted_signal, src2)
863
+
864
+ # Verify the result
865
+ assert len(result.x) == len(x2), "Result should have same length as src2"
866
+ check_array_result("X-axis", result.x, x2, rtol=1e-10)
867
+
868
+ # The y values should be the fit evaluated on x2
869
+ fit_params = sigima.proc.signal.extract_fit_params(fitted_signal)
870
+ expected_y = fitting.evaluate_fit(x2, **fit_params)
871
+ check_array_result("Evaluated fit", result.y, expected_y, rtol=1e-10)
872
+
873
+ # Check that fit parameters are preserved
874
+ assert "fit_params" in result.metadata, "Result should contain fit_params"
875
+ result_params = sigima.proc.signal.extract_fit_params(result)
876
+ assert result_params["a"] == fit_params["a"], "Fitted a should match"
877
+ assert result_params["b"] == fit_params["b"], "Fitted b should match"
878
+
879
+
880
+ def test_fitting_user_experience() -> None:
881
+ """Test user experience aspects of fitting functions."""
882
+ execenv.print("Testing user experience of fitting functions...")
883
+
884
+ # Test that fitting functions return results in expected formats
885
+ x = np.linspace(0, 10, 100)
886
+ y = 3.0 * x + 1.0 + np.random.normal(0, 0.5, len(x))
887
+
888
+ fitted_y, params = fitting.linear_fit(x, y)
889
+ assert isinstance(fitted_y, np.ndarray), "Fitted y should be a numpy array"
890
+ assert "a" in params and "b" in params, "Params should have a and b attributes"
891
+ fitted_y2 = fitting.evaluate_fit(x, **params)
892
+ check_array_result("Evaluate fit", fitted_y2, fitted_y, rtol=1e-10)
893
+
894
+ # Test that metadata is correctly attached to SignalObj when using proc functions
895
+ src = sigima.objects.create_signal("Test data", x, y)
896
+ dst = sigima.proc.signal.linear_fit(src)
897
+ assert "fit_params" in dst.metadata, "Metadata should contain fit_params"
898
+ fit_params = sigima.proc.signal.extract_fit_params(dst)
899
+ assert "a" in fit_params and "b" in fit_params, "fit_params should contain a and b"
900
+ assert fit_params["a"] == params["a"], "Fitted a should match"
901
+ assert fit_params["b"] == params["b"], "Fitted b should match"
902
+ # Use the new 2-to-1 signature: evaluate fit from dst on x-axis of src
903
+ dst2 = sigima.proc.signal.evaluate_fit(dst, src)
904
+ check_array_result("Evaluate fit on SignalObj", dst2.y, dst.y, rtol=1e-10)
905
+
906
+
907
+ if __name__ == "__main__":
908
+ guiutils.enable_gui()
909
+ # test_signal_linear_fit()
910
+ test_polynomial_fit()
911
+ test_signal_gaussian_fit()
912
+ test_signal_lorentzian_fit()
913
+ test_signal_voigt_fit()
914
+ test_signal_exponential_fit()
915
+ test_signal_piecewiseexponential_fit()
916
+ test_signal_planckian_fit()
917
+ test_signal_cdf_fit()
918
+ test_signal_sigmoid_fit()
919
+ test_signal_twohalfgaussian_fit()
920
+ test_multigaussian_single_peak()
921
+ test_multigaussian_double_peak()
922
+ test_multilorentzian_single_peak()
923
+ test_multilorentzian_double_peak()
924
+ test_sinusoidal_fit()
925
+ test_fitting_error_handling()
926
+ test_fitting_functions_available()
927
+ test_signal_evaluate_fit()
928
+ test_fitting_user_experience()
929
+ execenv.print("All fitting unit tests passed!")