API
labo1.to_significant_figures
to_significant_figures(x: float, dx: float | None = None, /, n: int = 2)
Rounds to n
significant figures based on the uncertainty dx
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n |
int
|
Number of significant figures. |
2
|
Examples:
>>> to_significant_figures(0.1234, n=2)
'0.12'
>>> to_significant_figures(12.34, n=2)
'12'
>>> to_significant_figures(1234, n=2)
'1200'
>>> to_significant_figures(12.34, 5.678, n=2)
('12.3', '5.7')
Source code in src/labo1/round.py
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 |
|
labo1.curve_fit
curve_fit(
func: Callable[..., NDArray],
/,
x: ArrayLike,
y: ArrayLike,
y_err: ArrayLike | None = None,
*,
initial_params: Sequence[float] | Mapping[str, float] | None = None,
estimate_errors: bool = False,
**kwargs,
)
Use non-linear least squares to fit a function to data.
Returns a Result
object with the parameters, errors,
and methods to quickly plot the fit.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable[..., NDArray]
|
The function to fit. Its signature must start with the independent |
required |
variable |
`x` followed by its N parameters to fit
|
|
required |
y_err |
ArrayLike | None
|
Errors or uncertainties for |
None
|
initial_params |
Sequence[float] | Mapping[str, float] | None
|
Initial guess for the parameters. |
None
|
estimate_errors |
bool
|
Whether to estimate a global scale factor for the errors |
False
|
**kwargs |
Passed to scipy.optimize.curve_fit. |
{}
|
Examples:
>>> def f(x, a, b):
... return a * x + b
...
>>> x = np.array([0.0, 1.0, 2.0])
>>> y = np.array([0.0, 0.9, 2.1])
>>> curve_fit(f, x, y, estimate_errors=True)
Result(a=1.050 ± 0.087, b=-0.05 ± 0.11)
Source code in src/labo1/fit.py
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 |
|
labo1.fit.Result
dataclass
Source code in src/labo1/fit.py
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 |
|
chi2
property
chi2
Sum of the standardized squared residuals.
covariance
instance-attribute
covariance: NDArray
Covariance matrix of the parameters.
errors
property
errors: NDArray
Standard deviation for the parameters.
The square-root of the diagonal of the covariance matrix.
names
property
names: Sequence[str]
Names of the parameters.
Extracted from the function signature.
params
instance-attribute
params: NDArray
Optimal parameters found by least squares.
reduced_chi2
property
reduced_chi2
χ² divided by the degree of freedom.
The degree of freedom is the number of measuments minus the number of fitted parameters.
residuals
property
residuals
The difference between the measured and predicted y
.
standardized_residuals
property
standardized_residuals
Residuals divided by their corresponding error.
eval
eval(x: NDArray) -> NDArray
Evaluates the function with the parameters.
Source code in src/labo1/fit.py
47 48 49 |
|
plot
plot(
*,
x_err: ArrayLike | None = None,
x_eval: int | ArrayLike | None = None,
label: str | None = None,
fig: Figure | SubFigure | None = None,
axes: Axes | None = None
) -> tuple[Figure | SubFigure, Axes]
Errorbar plot of the data and line plot of the function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_eval |
int | ArrayLike | None
|
Evaluation points for the line plot of the function. For an |
None
|
x_err |
ArrayLike | None
|
Error bars for |
None
|
label |
str | None
|
Name of the line plot for the legend. |
None
|
axes |
Axes | None
|
Axes on which to plot. |
None
|
fig |
Figure | SubFigure | None
|
Figure on which to create the |
None
|
Returns:
Type | Description |
---|---|
tuple[Figure | SubFigure, Axes]
|
The axes on which it plotted and its corresponding figure. |
Source code in src/labo1/fit.py
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 |
|
plot_with_residuals
plot_with_residuals(
*,
x_err: ArrayLike | None = None,
x_eval: int | ArrayLike | None = None,
label: str | None = None,
fig: Figure | SubFigure | None = None,
axes: Sequence[Axes] | None = None
) -> tuple[Figure | SubFigure, Sequence[Axes]]
Errorbar plot of the data and residuals, and line plot of the function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_eval |
int | ArrayLike | None
|
Evaluation points for the line plot of the function. For an |
None
|
x_err |
ArrayLike | None
|
Error bars for |
None
|
label |
str | None
|
Name of the line plot for the legend. |
None
|
axes |
Sequence[Axes] | None
|
Axes on which to plot. |
None
|
fig |
Figure | SubFigure | None
|
Figure on which to create the |
None
|
Returns:
Type | Description |
---|---|
tuple[Figure | SubFigure, Sequence[Axes]]
|
The axes on which it plotted and its corresponding figure. |
Source code in src/labo1/fit.py
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 |
|