inkfish-octopy 0.16.0__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.
- inkfish_octopy-0.16.0/LICENSE +21 -0
- inkfish_octopy-0.16.0/PKG-INFO +390 -0
- inkfish_octopy-0.16.0/README.md +363 -0
- inkfish_octopy-0.16.0/pyproject.toml +53 -0
- inkfish_octopy-0.16.0/setup.cfg +4 -0
- inkfish_octopy-0.16.0/src/inkfish_octopy.egg-info/PKG-INFO +390 -0
- inkfish_octopy-0.16.0/src/inkfish_octopy.egg-info/SOURCES.txt +29 -0
- inkfish_octopy-0.16.0/src/inkfish_octopy.egg-info/dependency_links.txt +1 -0
- inkfish_octopy-0.16.0/src/inkfish_octopy.egg-info/requires.txt +10 -0
- inkfish_octopy-0.16.0/src/inkfish_octopy.egg-info/top_level.txt +1 -0
- inkfish_octopy-0.16.0/src/octopy/__init__.py +87 -0
- inkfish_octopy-0.16.0/src/octopy/consts.py +137 -0
- inkfish_octopy-0.16.0/src/octopy/dimensions.py +182 -0
- inkfish_octopy-0.16.0/src/octopy/electronics.py +273 -0
- inkfish_octopy-0.16.0/src/octopy/fitting.py +241 -0
- inkfish_octopy-0.16.0/src/octopy/fonts/NotoSans-Bold.ttf +0 -0
- inkfish_octopy-0.16.0/src/octopy/fonts/NotoSans-Regular.ttf +0 -0
- inkfish_octopy-0.16.0/src/octopy/fonts/OFL.txt +93 -0
- inkfish_octopy-0.16.0/src/octopy/measurement.py +510 -0
- inkfish_octopy-0.16.0/src/octopy/methods.py +124 -0
- inkfish_octopy-0.16.0/src/octopy/plotting.py +525 -0
- inkfish_octopy-0.16.0/src/octopy/py.typed +0 -0
- inkfish_octopy-0.16.0/src/octopy/styling.py +87 -0
- inkfish_octopy-0.16.0/src/octopy/symbols.py +203 -0
- inkfish_octopy-0.16.0/tests/test_consts.py +62 -0
- inkfish_octopy-0.16.0/tests/test_dimensions.py +67 -0
- inkfish_octopy-0.16.0/tests/test_electronics.py +212 -0
- inkfish_octopy-0.16.0/tests/test_measurement.py +176 -0
- inkfish_octopy-0.16.0/tests/test_methods.py +0 -0
- inkfish_octopy-0.16.0/tests/test_plots.py +267 -0
- inkfish_octopy-0.16.0/tests/test_symbols.py +119 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 iancullinane06
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: inkfish-octopy
|
|
3
|
+
Version: 0.16.0
|
|
4
|
+
Summary: Lab assistance toolset
|
|
5
|
+
Author: iancullinane06
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/inkfish-labs/octopy
|
|
8
|
+
Project-URL: Issues, https://github.com/inkfish-labs/octopy/issues
|
|
9
|
+
Project-URL: Changelog, https://github.com/inkfish-labs/octopy/blob/main/CHANGELOG.md
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Typing :: Typed
|
|
14
|
+
Requires-Python: >=3.9
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
Requires-Dist: matplotlib>=3.5
|
|
18
|
+
Requires-Dist: numpy>=1.21
|
|
19
|
+
Requires-Dist: scipy>=1.7
|
|
20
|
+
Requires-Dist: seaborn>=0.11.2
|
|
21
|
+
Requires-Dist: pandas<=3.1.0
|
|
22
|
+
Provides-Extra: dev
|
|
23
|
+
Requires-Dist: ruff; extra == "dev"
|
|
24
|
+
Requires-Dist: build; extra == "dev"
|
|
25
|
+
Requires-Dist: twine; extra == "dev"
|
|
26
|
+
Dynamic: license-file
|
|
27
|
+
|
|
28
|
+
# OctoPy
|
|
29
|
+
|
|
30
|
+
[API reference and examples](https://www.inkfish-labs.github.io)
|
|
31
|
+
|
|
32
|
+
OctoPy helps with common physics-lab tasks: plotting measurements and
|
|
33
|
+
error bars, fitting polynomials, inspecting residuals, propagating independent
|
|
34
|
+
uncertainties, and finding mathematical symbols. It builds on NumPy,
|
|
35
|
+
Matplotlib, Seaborn, and SciPy.
|
|
36
|
+
|
|
37
|
+
## Installation
|
|
38
|
+
|
|
39
|
+
Install a published version from PyPI:
|
|
40
|
+
|
|
41
|
+
```powershell
|
|
42
|
+
python -m pip install ocotpy
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
The examples below describe the current source checkout; a published version
|
|
46
|
+
may not yet contain all these features. To install the checkout instead, run
|
|
47
|
+
this command from the project directory:
|
|
48
|
+
|
|
49
|
+
```powershell
|
|
50
|
+
python -m pip install .
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
For development, install it in editable mode:
|
|
54
|
+
|
|
55
|
+
```powershell
|
|
56
|
+
python -m pip install -e .
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Plotting
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
import matplotlib.pyplot as plt
|
|
63
|
+
from octopy import errorbar_plot, finish_plot, polynomial_fit, scatter_with_residuals
|
|
64
|
+
|
|
65
|
+
time = [0, 1, 2, 3] # seconds
|
|
66
|
+
position = [0.1, 2.0, 4.2, 5.9] # metres
|
|
67
|
+
uncertainty = [0.2, 0.2, 0.3, 0.2] # standard deviations, metres
|
|
68
|
+
|
|
69
|
+
fig, ax = plt.subplots()
|
|
70
|
+
errorbar_plot(time, position, yerr=uncertainty, ax=ax, label="measurements",
|
|
71
|
+
xlabel="Time (s)", ylabel="Position (m)")
|
|
72
|
+
ax.legend()
|
|
73
|
+
finish_plot(fig, "position.png", "plots", dpi=300, show=True)
|
|
74
|
+
|
|
75
|
+
fit = polynomial_fit(time, position, yerr=uncertainty)
|
|
76
|
+
print("Gradient and intercept:", fit.coefficients)
|
|
77
|
+
print("Reduced chi-squared:", fit.reduced_chi_squared)
|
|
78
|
+
fig = scatter_with_residuals(time, position, yerr=uncertainty,
|
|
79
|
+
xlabel="Time (s)", ylabel="Position (m)")
|
|
80
|
+
finish_plot(fig, "fit-and-residuals.pdf", "plots")
|
|
81
|
+
plt.close(fig)
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
`line_plot`, `errorbar_plot`, and `scatter_with_fit` accept an optional `ax`;
|
|
85
|
+
`scatter_with_residuals` accepts `axes=(main_ax, residual_ax)`. They create
|
|
86
|
+
axes if none are supplied and return the `Figure`. Call `finish_plot` when
|
|
87
|
+
ready to arrange, save, and optionally show the entire figure. No figure is
|
|
88
|
+
shown by default. The older `filename`, `save_directory`, and `show` arguments
|
|
89
|
+
on the plotting functions still work for one-step use.
|
|
90
|
+
|
|
91
|
+
Plotting does not change global Matplotlib settings. Call `apply_theme()`
|
|
92
|
+
explicitly *before* creating figures if you want the library's global Seaborn
|
|
93
|
+
theme. Use Matplotlib axes methods for scales, limits, and other customization.
|
|
94
|
+
Add units to axis labels as shown above.
|
|
95
|
+
|
|
96
|
+
### Plot types and styling
|
|
97
|
+
|
|
98
|
+
| Function | What it draws |
|
|
99
|
+
| --- | --- |
|
|
100
|
+
| `line_plot(x, y)` | Data connected by a line. |
|
|
101
|
+
| `errorbar_plot(x, y, xerr=..., yerr=...)` | Points with optional x and y error bars. |
|
|
102
|
+
| `scatter_with_fit(x, y, degree=1, yerr=..., model=...)` | Points and a polynomial or custom-model fit, in matching colours, with errors by default. |
|
|
103
|
+
| `scatter_with_bands(x, y, fit_range=...)` | Linear fit with confidence and prediction bands, optionally extended beyond the data. |
|
|
104
|
+
| `scatter_with_residuals(x, y, degree=1, yerr=...)` | A fit and a second axes showing residuals. |
|
|
105
|
+
| `bode_plot(f, gain, phase)` | Gain in dB on a log frequency axis, with phase on a twin axis. |
|
|
106
|
+
|
|
107
|
+
These functions accept equal-length, finite, one-dimensional data, including
|
|
108
|
+
NumPy arrays and numeric pandas Series. `errorbar_plot` accepts scalar,
|
|
109
|
+
per-point, or asymmetric errors: for asymmetric errors, pass a 2-by-N array
|
|
110
|
+
of lower and upper error magnitudes. Fit `yerr` values must instead be
|
|
111
|
+
positive scalars or per-point standard uncertainties.
|
|
112
|
+
|
|
113
|
+
For example, overlay two data series and adjust the axes before finishing:
|
|
114
|
+
|
|
115
|
+
```python
|
|
116
|
+
import matplotlib.pyplot as plt
|
|
117
|
+
from octopy import apply_theme, errorbar_plot, finish_plot, line_plot, style_axis
|
|
118
|
+
|
|
119
|
+
apply_theme() # Optional: changes global Matplotlib settings.
|
|
120
|
+
fig, ax = plt.subplots(figsize=(8, 5))
|
|
121
|
+
line_plot([1, 2, 3], [2, 4, 6], ax=ax, label="model", linestyle="--")
|
|
122
|
+
errorbar_plot([1, 2, 3], [2.1, 3.9, 6.2], yerr=0.2, ax=ax,
|
|
123
|
+
label="measurements", marker="o", capsize=4)
|
|
124
|
+
style_axis(ax, title="Position over time", xlabel="Time (s)", ylabel="Position (m)")
|
|
125
|
+
ax.legend()
|
|
126
|
+
finish_plot(fig, "comparison.png", "plots", dpi=300)
|
|
127
|
+
plt.close(fig)
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
`style_axis` leaves labels unchanged when they are omitted. `finish_plot`
|
|
131
|
+
applies a tight layout, creates missing save directories, and returns the
|
|
132
|
+
figure. It does not show or close the figure by default; use `show=True` to
|
|
133
|
+
show it and `plt.close(fig)` when you are finished with it.
|
|
134
|
+
|
|
135
|
+
### Fit results
|
|
136
|
+
|
|
137
|
+
`polynomial_fit` returns coefficients in descending power order, residuals
|
|
138
|
+
(`observed - predicted`), unweighted R², and, when `yerr` is supplied,
|
|
139
|
+
chi-squared and reduced chi-squared. Fits minimize squared residuals divided
|
|
140
|
+
by `yerr**2`: use independent, positive one-standard-deviation uncertainties,
|
|
141
|
+
not variances. Reduced chi-squared requires more measurements than fitted
|
|
142
|
+
parameters; it and R² are `None` when undefined. Fits do not account for
|
|
143
|
+
uncertainties in x, correlated errors, or systematic errors. Residual error
|
|
144
|
+
bars display the supplied measurement uncertainties, not fitted-residual
|
|
145
|
+
uncertainties. A good R² alone does not establish that a model is appropriate.
|
|
146
|
+
|
|
147
|
+
```python
|
|
148
|
+
from octopy import polynomial_fit
|
|
149
|
+
|
|
150
|
+
result = polynomial_fit([0, 1, 2, 3], [0.1, 2.0, 4.2, 5.9],
|
|
151
|
+
degree=1, yerr=[0.2, 0.2, 0.3, 0.2])
|
|
152
|
+
slope, intercept = result.coefficients
|
|
153
|
+
print("Slope:", slope, "intercept:", intercept)
|
|
154
|
+
print("Residuals:", result.residuals)
|
|
155
|
+
print("R²:", result.r_squared)
|
|
156
|
+
print("Chi-squared:", result.chi_squared)
|
|
157
|
+
print("Reduced chi-squared:", result.reduced_chi_squared)
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
For degree `d`, the fit needs at least `d + 1` distinct x values. The default
|
|
161
|
+
degree is 1, so the first coefficient is the gradient and the second is the
|
|
162
|
+
intercept. Without `yerr` the fit is unweighted and both chi-squared fields
|
|
163
|
+
are `None`.
|
|
164
|
+
|
|
165
|
+
## Measurements and uncertainty
|
|
166
|
+
|
|
167
|
+
`Measurement` stores a value (or array of values) and optional **absolute
|
|
168
|
+
standard uncertainty**. Use `from_pm` when uncertainties are known, or
|
|
169
|
+
`from_stdev` for repeated observations:
|
|
170
|
+
|
|
171
|
+
```python
|
|
172
|
+
from octopy import Measurement
|
|
173
|
+
|
|
174
|
+
length = Measurement.from_pm(2.0, 0.1, relative=0.02, granularity=0.01)
|
|
175
|
+
time = Measurement.from_stdev([1.2, 1.3, 1.4], mean_error=True)
|
|
176
|
+
speed = length / time
|
|
177
|
+
print(speed.value, speed.uncertainty, speed.relative_uncertainty())
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
`relative` is a fraction (`0.02` means 2%). Absolute, relative and
|
|
181
|
+
granularity contributions are combined in quadrature. Supply `granularity`
|
|
182
|
+
as an **absolute standard uncertainty**, not automatically as a raw instrument
|
|
183
|
+
resolution: convert the resolution to a standard uncertainty using an
|
|
184
|
+
appropriate distribution first. `from_stdev` uses sample standard deviation
|
|
185
|
+
(`ddof=1`); `mean_error=True` uses the standard error of the mean instead.
|
|
186
|
+
Granularity is combined afterward and is not reduced by the number of readings.
|
|
187
|
+
|
|
188
|
+
Use arrays for a series of measurements, or leave uncertainty unspecified:
|
|
189
|
+
|
|
190
|
+
```python
|
|
191
|
+
from octopy import Measurement
|
|
192
|
+
|
|
193
|
+
positions = Measurement.from_pm([1.0, 2.0, 3.0], [0.1, 0.1, 0.2])
|
|
194
|
+
unmeasured = Measurement.from_pm([1.0, 2.0, 3.0])
|
|
195
|
+
print(positions.value, positions.uncertainty)
|
|
196
|
+
print(unmeasured.uncertainty) # None
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
`str(measurement)` rounds to three decimal places and uses automatic formatting:
|
|
200
|
+
magnitudes from `10⁻³` up to (but not including) `10³` use decimal notation;
|
|
201
|
+
values outside that range use scientific notation. Choose another format or
|
|
202
|
+
precision with `measurement.to_string()`:
|
|
203
|
+
|
|
204
|
+
```python
|
|
205
|
+
measurement = Measurement.from_pm(2_000_000, 100_000)
|
|
206
|
+
measurement.to_string(format="raw") # 2000000.000 ± 100000.000
|
|
207
|
+
measurement.to_string(format="scientific", decimals=None) # 2 x 10⁶ ± 1 x 10⁵
|
|
208
|
+
measurement.to_string(unit="Pa", prefix="mega") # 2.000 ± 0.100 MPa
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
SI prefixes rescale only the displayed value and uncertainty; stored values
|
|
212
|
+
and arithmetic are unchanged. Supply the unprefixed unit separately from its
|
|
213
|
+
prefix (for example, `unit="Pa", prefix="mega"`).
|
|
214
|
+
Store or clear a display unit for future printing with `set_unit()`:
|
|
215
|
+
|
|
216
|
+
```python
|
|
217
|
+
measurement.set_unit("Pa", "mega")
|
|
218
|
+
measurement.set_unit(None) # clear the display unit
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
Choose a different number of decimal places with `decimals=`, or pass
|
|
222
|
+
`decimals=None` to retain the shortest round-trippable representation.
|
|
223
|
+
|
|
224
|
+
Both `value` and `uncertainty` are NumPy arrays, including zero-dimensional
|
|
225
|
+
arrays for scalar values. `from_stdev` requires at least two observations;
|
|
226
|
+
pass `axis=0` or `axis=1` to summarise repeated readings in a 2D array along
|
|
227
|
+
the chosen axis.
|
|
228
|
+
|
|
229
|
+
Arithmetic (`+`, `-`, `*`, `/`, and powers) uses first-order propagation in
|
|
230
|
+
quadrature and assumes independent input uncertainties. Correlated quantities,
|
|
231
|
+
including repeated use of the same measurement, require a covariance-aware
|
|
232
|
+
analysis instead. Missing uncertainties stay `None` when none are supplied;
|
|
233
|
+
when combining an uncertain value with one without an uncertainty, only the
|
|
234
|
+
supplied contribution is included. This does not establish that the other
|
|
235
|
+
measurement is exact. Relative uncertainty is undefined for zero values.
|
|
236
|
+
|
|
237
|
+
For repeated calculated values, `measurement.mean()` returns a measurement
|
|
238
|
+
whose uncertainty combines the sample standard error and the independently
|
|
239
|
+
propagated individual uncertainties in quadrature. This assumes independent
|
|
240
|
+
contributions; observed scatter may already include measurement noise, so
|
|
241
|
+
combining both can double-count it. See the [mean reference](docs/measurement.md)
|
|
242
|
+
and [speed-of-sound example](examples/speed_of_sound.py).
|
|
243
|
+
|
|
244
|
+
### CSV and pandas
|
|
245
|
+
|
|
246
|
+
Read a CSV with pandas and pass its numeric columns directly to the plotters,
|
|
247
|
+
or associate a value column with absolute and/or fractional error columns:
|
|
248
|
+
|
|
249
|
+
For example, `experiment.csv` might contain:
|
|
250
|
+
|
|
251
|
+
```csv
|
|
252
|
+
time,distance,distance_error
|
|
253
|
+
0,0.1,0.2
|
|
254
|
+
1,2.0,0.2
|
|
255
|
+
2,4.2,0.3
|
|
256
|
+
3,5.9,0.2
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
```python
|
|
260
|
+
import pandas as pd
|
|
261
|
+
from octopy import Measurement, scatter_with_residuals, finish_plot
|
|
262
|
+
|
|
263
|
+
data = pd.read_csv("experiment.csv")
|
|
264
|
+
distance = Measurement.from_dataframe(data, "distance", uncertainty="distance_error")
|
|
265
|
+
fig = scatter_with_residuals(data["time"], distance,
|
|
266
|
+
xlabel="Time (s)", ylabel="Distance (m)")
|
|
267
|
+
finish_plot(fig, "experiment.pdf")
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
Here, the column names are supplied to `from_dataframe`; use `relative="name"`
|
|
271
|
+
for a column of fractional uncertainties, and optionally pass both
|
|
272
|
+
`uncertainty` and `relative` to combine them. The CSV example needs pandas,
|
|
273
|
+
which can be installed with `python -m pip install pandas`.
|
|
274
|
+
|
|
275
|
+
`errorbar_plot` automatically uses uncertainties on `Measurement` x and y
|
|
276
|
+
values. `scatter_with_fit` and `scatter_with_residuals` draw x and y
|
|
277
|
+
uncertainties and weight the fit by y uncertainties by default (pass
|
|
278
|
+
`use_errors=False` to turn this off); `polynomial_fit` also weights by y
|
|
279
|
+
uncertainties. Explicit `yerr` overrides inferred values. Ordinary numeric pandas Series work without a `Measurement`. A Series
|
|
280
|
+
of scalar `Measurement` objects is also supported; its uncertainties must
|
|
281
|
+
either all be supplied or all be absent. `line_plot` uses the values only.
|
|
282
|
+
|
|
283
|
+
```python
|
|
284
|
+
import pandas as pd
|
|
285
|
+
from octopy import Measurement, errorbar_plot
|
|
286
|
+
|
|
287
|
+
readings = pd.Series([Measurement.from_pm(1.0, 0.1),
|
|
288
|
+
Measurement.from_pm(2.0, 0.2)])
|
|
289
|
+
fig = errorbar_plot(pd.Series([0, 1]), readings, xlabel="Time (s)", ylabel="Length (m)")
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
## Fitting and electronics
|
|
293
|
+
|
|
294
|
+
`fit_model` fits any model (built-ins: `exponential_decay`, `exponential_rise`,
|
|
295
|
+
`lorentzian`, `gaussian`, `power_law`, `diode`) with parameter uncertainties,
|
|
296
|
+
χ², reduced χ² and a p-value. Electronics helpers cover cutoff frequency,
|
|
297
|
+
roll-off slope, resonance and Q, time constants, rise time, and phase from a
|
|
298
|
+
time delay, sine fit or Lissajous figure. See [fitting](docs/fitting.md) and
|
|
299
|
+
[electronics](docs/electronics.md).
|
|
300
|
+
|
|
301
|
+
```python
|
|
302
|
+
from octopy import exponential_decay, fit_model, cutoff_frequency
|
|
303
|
+
|
|
304
|
+
fit = fit_model(exponential_decay, t, v, yerr=0.05)
|
|
305
|
+
print(fit.parameter("tau"))
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
## Additional analysis helpers
|
|
309
|
+
|
|
310
|
+
These functions are available from `octopy.methods`:
|
|
311
|
+
|
|
312
|
+
```python
|
|
313
|
+
import numpy as np
|
|
314
|
+
from octopy.methods import chi_squared, reduced_chi_squared, p_value_chi_squared, k_means
|
|
315
|
+
|
|
316
|
+
observed = [1.1, 1.9, 3.2]
|
|
317
|
+
expected = [1.0, 2.0, 3.0]
|
|
318
|
+
errors = [0.2, 0.2, 0.2]
|
|
319
|
+
dof = 2 # Set this for your model and fitted parameters.
|
|
320
|
+
print(chi_squared(observed, expected, errors))
|
|
321
|
+
print(reduced_chi_squared(observed, expected, errors, dof))
|
|
322
|
+
print(p_value_chi_squared(observed, expected, errors, dof))
|
|
323
|
+
|
|
324
|
+
points = np.array([[0.0, 0.0], [1.0, 0.0], [2.0, 0.0]])
|
|
325
|
+
centroids, labels = k_means(points, k=1)
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
For chi-squared, provide matching numeric arrays and positive standard
|
|
329
|
+
uncertainties, and use a positive number of degrees of freedom. The p-value
|
|
330
|
+
uses the chi-squared distribution. For `k_means`, provide a two-dimensional
|
|
331
|
+
NumPy array with one sample per row and `1 <= k <=` the number of samples.
|
|
332
|
+
Centroids are initialized randomly; results can vary and empty clusters can
|
|
333
|
+
produce invalid centroids. Inspect the result before drawing conclusions.
|
|
334
|
+
|
|
335
|
+
## Symbol utilities
|
|
336
|
+
|
|
337
|
+
```python
|
|
338
|
+
from octopy import print_symbols, search, symbols
|
|
339
|
+
|
|
340
|
+
print(symbols["pm"])
|
|
341
|
+
print(search("fraction"))
|
|
342
|
+
print_symbols(search("fraction_one"))
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
`symbols` and the mapping returned by `search(name_part)` are read-only.
|
|
346
|
+
Search matches names case-insensitively. `print_symbols()` prints all symbols;
|
|
347
|
+
pass a mapping to print a selection with its decimal and hexadecimal code points.
|
|
348
|
+
|
|
349
|
+
## Development
|
|
350
|
+
|
|
351
|
+
Ruff is an optional code linter; it is not required to use or publish the
|
|
352
|
+
library. Twine is a separate tool for checking and uploading distributions.
|
|
353
|
+
|
|
354
|
+
```powershell
|
|
355
|
+
python -m pip install -e ".[dev]"
|
|
356
|
+
ruff check .
|
|
357
|
+
python -m unittest discover -s tests
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
The version lives in `src/octopy/__init__.py`; see
|
|
361
|
+
[CHANGELOG.md](CHANGELOG.md) for release notes.
|
|
362
|
+
|
|
363
|
+
## Publishing
|
|
364
|
+
|
|
365
|
+
To publish manually with Twine, update the version in
|
|
366
|
+
`src/octopy/__init__.py`, then build and check the distributions:
|
|
367
|
+
|
|
368
|
+
```powershell
|
|
369
|
+
python -m pip install --upgrade build twine
|
|
370
|
+
python -m build
|
|
371
|
+
python -m twine check dist/*
|
|
372
|
+
python -m twine upload dist/*
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
Ensure `dist/` contains only the distributions for the version being
|
|
376
|
+
released. When prompted, use `__token__` as the username and your PyPI API
|
|
377
|
+
token as the password; don't commit or share the token. PyPI does not allow
|
|
378
|
+
uploading the same distribution version again.
|
|
379
|
+
|
|
380
|
+
Alternatively, the GitHub Actions workflow publishes when a GitHub Release is
|
|
381
|
+
published. To use it, configure a PyPI trusted publisher for this repository:
|
|
382
|
+
|
|
383
|
+
- Owner and repository: the values for this GitHub project
|
|
384
|
+
- Workflow: `.github/workflows/publish.yml`
|
|
385
|
+
- Environment: `pypi`
|
|
386
|
+
|
|
387
|
+
Then push the version change and publish a GitHub Release with a matching
|
|
388
|
+
version tag. The workflow runs the tests, builds the distributions, and
|
|
389
|
+
publishes them without requiring a PyPI API token in GitHub secrets. Choose
|
|
390
|
+
either the manual upload or the workflow for a given release, not both.
|