-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathodr-fit.py
executable file
·584 lines (476 loc) · 17.4 KB
/
odr-fit.py
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
"""odr-fit.py performs ODR fit on data with uncertainties in both the x and y.
It reads data from a CSV file, performs the Orthogonal Distance Regression
(ODR) analysis, and creates plots for the data with fit, residuals, and
parameter correlation ellipses.
"""
import sys
import matplotlib.pyplot as plt
import matplotlib.transforms as transforms
import numpy as np
import pandas as pd
from matplotlib.axes import Axes
from matplotlib.patches import Ellipse, Patch
from scipy import odr, stats
from typing import Callable
def read_data(
filename: str,
) -> tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray] | None:
"""Read x, y coordinates and their uncertainties from a CSV file.
Parameters
----------
filename : str
Path to the CSV file. File must contain columns 'x', 'dx', 'y', 'dy'
where dx and dy represent uncertainties in x and y values.
Returns
-------
tuple of numpy.ndarray or None
If successful, returns (x, dx, y, dy) where:
- x: array of x coordinates
- dx: array of x uncertainties
- y: array of y coordinates
- dy: array of y uncertainties
Returns None if there are any errors reading the file.
"""
try:
df = pd.read_csv(filename)
x = df["x"].to_numpy() # Convert to numpy array
dx = df["dx"].to_numpy()
y = df["y"].to_numpy()
dy = df["dy"].to_numpy()
return x, dx, y, dy
except Exception as e:
print(f"Error reading file: {e}")
return None
def linear_func(p: np.ndarray, x: np.ndarray) -> np.ndarray:
"""Compute a linear function of the form y = mx + b.
Parameters
----------
p : array-like, shape (2,)
Parameters of the linear function:
- p[0] (m): slope
- p[1] (b): y-intercept
x : array-like
Independent variable values
Returns
-------
array-like
Computed y values: m*x + b
"""
m, b = p
return m * x + b
def parabolic_func(p: np.ndarray, x: np.ndarray) -> np.ndarray:
"""Compute a parablic function.
Parameters
----------
p : array-like, shape (3,)
Parameters of the linear function:
x : array-like
Independent variable values
Returns
-------
array-like
Computed y values: p[0] + p[1] * x + p[2] * x**2
"""
return p[0] + p[1] * x + p[2] * x**2
def linear_odr(
x: np.ndarray, dx: np.ndarray, y: np.ndarray, dy: np.ndarray
) -> tuple[odr.Output, float, int, float, float]:
"""Orthogonal Distance Regression analysis on data with uncertainties.
Fits a linear model to data points with uncertainties in both x and y
using ODR method from scipy.odr. Also computes goodness-of-fit statistics.
Parameters
----------
x : array-like
X coordinates of the data points
dx : array-like
Uncertainties (standard deviations) in x coordinates
y : array-like
Y coordinates of the data points
dy : array-like
Uncertainties (standard deviations) in y coordinates
Returns
-------
results : ODR
ODR result object containing fit parameters and covariance matrix
chi_square : float
Chi-square statistic of the fit
degrees_freedom : int
Number of degrees of freedom (n_points - n_parameters)
chi_square_reduced : float
Reduced chi-square (chi-square / degrees_freedom)
p_value : float
P-value for the chi-square goodness-of-fit test
Notes
-----
Uses a linear function model: y = mx + b
Initial parameter guesses are [m=1.0, b=0.0]
"""
linear = odr.Model(linear_func)
data = odr.RealData(x, y, sx=dx, sy=dy)
odr_obj = odr.ODR(data, linear, beta0=[1.0, 0.0])
results = odr_obj.run()
degrees_freedom = len(x) - 2
chi_square = results.sum_square # type: ignore # ODR Output attribute exists at runtime
chi_square_reduced = chi_square / degrees_freedom
p_value = float(1 - stats.chi2.cdf(chi_square, degrees_freedom))
return results, chi_square, degrees_freedom, chi_square_reduced, p_value
def perform_odr(
x: np.ndarray,
dx: np.ndarray,
y: np.ndarray,
dy: np.ndarray,
model_func: Callable[[np.ndarray, np.ndarray], np.ndarray],
beta0: np.ndarray,
) -> tuple[odr.Output, float, int, float, float]:
"""Orthogonal Distance Regression analysis on data with uncertainties.
Fits a given model function to data points with uncertainties in both x and y
using ODR method from scipy.odr. Also computes goodness-of-fit statistics.
Parameters
----------
x : array-like
X coordinates of the data points
dx : array-like
Uncertainties (standard deviations) in x coordinates
y : array-like
Y coordinates of the data points
dy : array-like
Uncertainties (standard deviations) in y coordinates
model_func : callable
Model function to fit. Should take parameters (beta, x) where beta is
the parameter vector and x is the independent variable array
beta0 : array-like
Initial guesses for the model parameters
Returns
-------
results : ODR
ODR result object containing fit parameters and covariance matrix
chi_square : float
Chi-square statistic of the fit
degrees_freedom : int
Number of degrees of freedom (n_points - n_parameters)
chi_square_reduced : float
Reduced chi-square (chi-square / degrees_freedom)
p_value : float
P-value for the chi-square goodness-of-fit test
"""
model = odr.Model(model_func)
data = odr.RealData(x, y, sx=dx, sy=dy)
odr_obj = odr.ODR(data, model, beta0=beta0)
results = odr_obj.run()
n_params = len(beta0)
degrees_freedom = len(x) - n_params
chi_square = results.sum_square # type: ignore # ODR Output attribute exists at runtime
chi_square_reduced = chi_square / degrees_freedom
p_value = float(1 - stats.chi2.cdf(chi_square, degrees_freedom))
return results, chi_square, degrees_freedom, chi_square_reduced, p_value
def plot_fit(
x: np.ndarray,
dx: np.ndarray,
y: np.ndarray,
dy: np.ndarray,
results: odr.Output,
save_path: str,
) -> None:
"""Create and save a plot of data points with error bars and fit line.
Parameters
----------
x : array-like
X-coordinates of the data points
dx : array-like
Uncertainties (standard deviations) in x-coordinates
y : array-like
Y-coordinates of the data points
dy : array-like
Uncertainties (standard deviations) in y-coordinates
results : scipy.odr.Output
Results from ODR (Orthogonal Distance Regression) fit
save_path : str or Path
Path where the plot will be saved
Notes
-----
- Automatically switches between point markers and error bars only, based on
the relative size of uncertainties compared to data range
- Uses error bars when median uncertainty > 1% of data range
- Generates fit line using 100 evenly spaced points
- Creates a 10x8 inch figure with grid
- Saves plot and closes figure after completion
"""
plt.figure(figsize=(10, 8))
# Determine if error bars are visible
median_dx = np.median(dx)
median_dy = np.median(dy)
x_range = np.max(x) - np.min(x)
y_range = np.max(y) - np.min(y)
use_points = (median_dx / x_range < 0.01) and (median_dy / y_range < 0.01)
marker = "o" if use_points else "none"
plt.errorbar(x, y, xerr=dx, yerr=dy, fmt=marker, label="Data")
x_fit = np.linspace(min(x), max(x), 100)
y_fit = linear_func(results.beta, x_fit)
plt.plot(x_fit, y_fit, "r-", label="Fit")
plt.xlabel("X")
plt.ylabel("Y")
plt.title("ODR Fit with Uncertainties")
plt.legend()
plt.grid(True)
plt.savefig(save_path)
plt.show()
plt.close()
def plot_residuals(
x: np.ndarray,
dx: np.ndarray,
y: np.ndarray,
dy: np.ndarray,
results: odr.Output,
save_path: str,
) -> None:
"""Generate and save residuals plot for a linear fit with uncertainties.
Creates a figure showing the difference between observed and model values,
including total uncertainty from both y and x errors. Automatically adjusts
the plot style based on the relative size of error bars.
Parameters
----------
x : array-like
Independent variable values
dx : array-like
Uncertainties in x values
y : array-like
Dependent variable values (observations)
dy : array-like
Uncertainties in y values
results : object
Fit results containing at least:
- beta: array-like with [slope, intercept]
save_path : str or Path
Output path where the plot will be saved
Notes
-----
- Total uncertainty combines y errors (dy) and propagated x errors
- Points are shown only if median uncertainty is < 1% of residual range
- Plot includes a red horizontal line at y=0 for reference
- Figure is automatically closed after saving
The plot shows:
- Residuals (y - model) vs x
- Error bars showing combined uncertainty
- Grid for better readability
"""
plt.figure(figsize=(10, 6))
y_model = linear_func(results.beta, x)
residuals = y - y_model
total_uncertainty = np.sqrt(dy**2 + (results.beta[0] * dx) ** 2)
# Determine if error bars are visible
median_uncert = np.median(total_uncertainty)
resid_range = np.max(residuals) - np.min(residuals)
use_points = median_uncert / resid_range < 0.01
marker = "o" if use_points else "none"
plt.errorbar(x, residuals, yerr=total_uncertainty, fmt=marker)
plt.axhline(y=0, color="r", linestyle="-")
plt.xlabel("X")
plt.ylabel("Residuals")
plt.title("Residuals")
plt.grid(True)
plt.savefig(save_path)
plt.show()
plt.close()
def confidence_ellipse(
mean: np.ndarray, cov: np.ndarray, ax: Axes, n_std: float = 1.0, **kwargs
) -> Patch:
"""Plot a confidence ellipse representing a bivariate normal distribution.
This function creates an ellipse that visualizes the covariance structure
and mean of a 2D normally distributed dataset. The ellipse's size represents
the confidence interval determined by n_std standard deviations.
Parameters
----------
mean : array-like, shape (2,)
The center point (mean) of the ellipse in format [x, y]
cov : array-like, shape (2, 2)
The 2x2 covariance matrix of the distribution
ax : matplotlib.axes.Axes
The axes object to draw the ellipse on
n_std : float, optional (default=1.0)
The number of standard deviations determining the ellipse's size
**kwargs : dict
Additional keyword arguments passed to matplotlib.patches.Ellipse
Returns
-------
matplotlib.patches.Ellipse
The added ellipse patch object
Notes
-----
The ellipse is first created at (0,0) with initial radii, then transformed
using:
1. 45-degree rotation to align with correlation direction
2. Scaling according to covariance values and desired confidence level
3. Translation to the specified mean position
"""
pearson = cov[0, 1] / np.sqrt(cov[0, 0] * cov[1, 1])
ell_radius_x = np.sqrt(1 + pearson)
ell_radius_y = np.sqrt(1 - pearson)
ellipse = Ellipse(
(0, 0), width=ell_radius_x * 2, height=ell_radius_y * 2, **kwargs
)
scale_x = np.sqrt(cov[0, 0]) * n_std
scale_y = np.sqrt(cov[1, 1]) * n_std
transf = (
transforms.Affine2D()
.rotate_deg(45)
.scale(scale_x, scale_y)
.translate(mean[0], mean[1])
)
ellipse.set_transform(transf + ax.transData)
return ax.add_patch(ellipse)
def format_matrix(matrix: np.ndarray) -> str:
"""Convert a matrix into a neatly formatted string representation.
Converts each element to scientific notation with 6 decimal places and
aligns columns for readability.
Parameters
----------
matrix : array-like
Input 2D matrix that can be converted to a NumPy array.
Returns
-------
str
String representation of the matrix where:
- Each element is in scientific notation (1.234567e+00 format)
- Elements are right-aligned within columns
- Rows are separated by newlines
- Columns are separated by single spaces
Examples
--------
>>> m = [[1, 2], [3000, 4000]]
>>> print(format_matrix(m))
1.000000e+00 2.000000e+00
3.000000e+03 4.000000e+03
"""
matrix = np.asarray(matrix)
# Format each element with scientific notation
formatted_elements = [
[f"{element:1.6e}" for element in row] for row in matrix
]
# Get maximum width for alignment
width = max(
len(str(element)) for row in formatted_elements for element in row
)
# Create formatted rows
formatted_rows = [
" ".join(f"{element:>{width}}" for element in row)
for row in formatted_elements
]
# Return the full string with newlines
return "\n".join(formatted_rows)
def plot_ellipses(results: odr.Output, save_path: str) -> None:
"""Create and save a plot showing parameter correlation ellipses.
This function generates a figure showing confidence ellipses for the
correlation between slope and intercept parameters, typically from a linear
regression fit. The ellipses are drawn at 1σ, 2σ, and 3σ confidence levels
using chi-squared values for 2 degrees of freedom.
Parameters
----------
results : object
Results object containing:
- beta : array-like, shape (2,)
Best-fit parameters [slope, intercept]
- cov_beta : array-like, shape (2, 2)
Covariance matrix for the parameters
save_path : str
File path where the generated plot should be saved
Notes
-----
The confidence levels correspond to the following chi-squared values:
- 1σ: χ² = 2.30 (39.3% confidence)
- 2σ: χ² = 6.18 (95.4% confidence)
- 3σ: χ² = 11.83 (99.7% confidence)
The best-fit point is marked with a red star on the plot.
The function closes the plot after saving to free memory.
"""
plt.figure(figsize=(10, 8))
ax = plt.gca()
confidence_data = [
(2.30, "1σ (39.3%)", "red"),
(6.18, "2σ (95.4%)", "green"),
(11.83, "3σ (99.7%)", "blue"),
]
for chi2_val, label, color in confidence_data:
confidence_ellipse(
results.beta,
results.cov_beta,
ax,
n_std=np.sqrt(chi2_val),
alpha=0.25,
color=color,
label=label,
)
ax.plot(
results.beta[0], results.beta[1], "r*", label="Best fit", markersize=10
)
plt.xlabel("Slope (m)")
plt.ylabel("Intercept (b)")
plt.title("Parameter Correlation Ellipses")
plt.legend()
plt.grid(True)
plt.savefig(str(save_path))
plt.show()
plt.close()
def main() -> None:
"""Run orthogonal distance regression on input data and generate outputs.
This function performs the following operations:
1. Handles command-line arguments for input file selection
2. Reads data from the specified CSV file
3. Performs orthogonal distance regression (ODR)
4. Generates three plots: fit visualization, residuals,
and correlation ellipses
5. Saves detailed regression results to a text file
Command-line Usage:
python script.py [input_file.csv]
Input File Format:
Expected CSV file with columns containing x values, x uncertainties,
y values, and y uncertainties.
Generated Files:
- fit_plot.png: Visualization of data points and fitted line
- residuals_plot.png: Plot of fit residuals
- correlation_ellipses.png: Parameter correlation visualization
- fit_results.txt: Detailed regression results including:
* Slope and intercept with uncertainties
* Covariance matrix
* Pearson's correlation coefficient
* Chi-square statistics and p-value
"""
default_input = "data.csv"
if len(sys.argv) == 1:
input_file = default_input
elif len(sys.argv) == 2:
input_file = sys.argv[1]
else:
print("Usage: odr-analysis [input_file.csv]")
print(f"Default input file: {default_input}")
sys.exit(1)
data = read_data(input_file)
if data is None:
return
x, dx, y, dy = data
results, chi_square, degrees_freedom, chi_square_reduced, p_value = (
linear_odr(x, dx, y, dy)
)
# Create and save plots
plot_fit(x, dx, y, dy, results, "fit_plot.png")
plot_residuals(x, dx, y, dy, results, "residuals_plot.png")
plot_ellipses(results, "correlation_ellipses.png")
# Save results to text file
with open("fit_results.txt", "w") as f:
f.write("Regression Results:\n")
f.write("-----------------\n")
f.write(f"Slope: {results.beta[0]:.6f} ± {results.sd_beta[0]:.6f}\n")
f.write(
f"Intercept: {results.beta[1]:.6f} ± {results.sd_beta[1]:.6f}\n"
)
f.write("\nCovariance matrix:")
f.write(f"\n{format_matrix(results.cov_beta)}")
correlation = results.cov_beta[0, 1] / (
results.sd_beta[0] * results.sd_beta[1]
)
f.write(f"\nPearson's Correlation coefficient: {correlation:.6f}\n")
f.write(f"\nChi-square: {chi_square:.6f}\n")
f.write(f"Degrees of freedom: {degrees_freedom}\n")
f.write(f"Reduced chi-square: {chi_square_reduced:.6f}\n")
f.write(f"P-value: {p_value:.6f}\n")
if __name__ == "__main__":
main()