summaryrefslogtreecommitdiff
path: root/python-phylab.spec
blob: 08f96650aea7a0bf56a1011fca7a8c8e0d3ecd89 (plain)
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
%global _empty_manifest_terminate_build 0
Name:		python-phylab
Version:	0.4.1
Release:	1
Summary:	A Python module for basic data analysis and curve fitting for Physics laboratory students.
License:	GPLv3
URL:		https://github.com/BernardoTomelleri/phylab
Source0:	https://mirrors.aliyun.com/pypi/web/packages/f5/d3/4c8ec11838854c8fff7f324eb897dd3b2cb8d1eb32e66a9bb43cdc6aa8be/phylab-0.4.1.tar.gz
BuildArch:	noarch

Requires:	python3-numpy
Requires:	python3-scipy
Requires:	python3-matplotlib

%description
[![Build Status](https://github.com/BernardoTomelleri/phylab/actions//workflows/PyPI-publish-release.yml/badge.svg)](https://github.com/BernardoTomelleri/phylab/actions/workflows/PyPI-publish-release.yml)
[![GPLv3 License](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://opensource.org/licenses/gpl-3.0.html)
A Python package for basic data analysis and curve fitting for Physics laboratory students.
## Table of contents
- [Phy lab](#phy-lab)
  * [Table of contents](#table-of-contents)
  * [Philosophy behind this](#philosophy-behind-this)
    + [How this library came about](#how-this-library-came-about)
  * [Contents](#contents)
    + [model functions](#model-functions)
    + [testing and printing of fit results](#testing-and-printing-of-fit-results)
    + [curve-fitting routines](#curve-fitting-routines)
    + [Fourier transform utilities](#fourier-transform-utilities)
    + [data plotting](#data-plotting)
    + [importing data from files](#importing-data-from-files)
  * [Setting up](#setting-up)
    + [Prerequisites](#prerequisites)
    + [Installation](#installation)
  * [Using Phy lab](#using-phy-lab)
  * [Development](#development)
  * [License](#license)
## Philosophy behind this
Because this was written by someone starting to learn about experimental Physics
and Python (for students in a similar situation) all the functions in this module
try to use only the basic data structures and functionalities found in Python
that are briefly discussed with students (functions, arrays & not much else).
This is why you won't find classes, methods or even dicts in this package
and why the code inside may be very, very inefficient/ugly.
On the upside though, all the definitions inside should be easily comprehensible
and modifiable by anyone just starting out with programming.
### How this library came about
While writing the python scripts for individual experiments (at UniPi's Physics Lab)
I noticed that a significant amount of code was common to almost all of them.
I started writing down these common functions in a python module everytime this
happened, so that I could import them later to save a lot of time and hassle.
That's basically the story behind it: a collection of useful ideas I had while
learning how to analyze experimental data with Python's SciPy.
Now that it's grown into a powerful enough collection of tools, I decided to
release it for any and all students that may find it helpful for dealing
with similar problems.
## Contents
- The main module containing all the functions. [lab]
- A simple showcase of a couple of things this library allows you to do. [circfit]
  - Finding the best-fitting circle and ellipse for simulated or real sampled data points.
  - A quick comparison between using `curve_fit`, an algebraic circle fit
   (weighted [Coope] method) and an algebraic ellipse fit.
  - Plotting the $\chi^2(a, b)$ surface for a pair of parameters $a, b$ of the circle or the ellipse.
- Folder containing further examples and data that can be used in the demos. [examples]
For another example of where this package can come in handy feel free to check
out [FFT repository] and [Lock-in detector]. A small paper (in italiano) on fitting,
computing Fourier transforms and/or simulating the effect of a Lock-in detector
on real sampled signals or internally generated ones.
### model functions
A few definitions of the model functions more commonly encountered in the first
years of Physics lab (e.g. [dampened oscillator], [square wave])
along with a few digital filters (e.g. [Butterworth]).
Right at the beginning of the module so you can immediately start adding
the models you need.
### testing and printing of fit results
Goodness of fit tests, evaluation of parameter uncertainties and
correlations  ([chi-square test], [errcor]).
Simple print formatters that allow you to quickly display the results
of a fit and associated uncertainties with the desired number of significant
digits. ([print correlations], [print parameters])
### curve-fitting routines
Weighted least-square fitting accounting for uncertainties on x and y axes
with linear error propagation ([propagated fit]), relying on [scipy.optimize.curve_fit].
Weighted orthogonal distance regression thanks to [ODRPACK].
Weighted algebraic fits (like [ellipse fit] and others).
### Fourier transform utilities
Functions for computing FFTs of real and complex signals and other
associated quantities ([sampling], [FFT], [FWHM]), applying window functions
and displaying their output through Matplotlib ([plotfft]).
### data plotting
Instead of having to write multiple calls to function in order to:
activate axis minor ticks, setting their size, orientation and spacing,
placing grids, setting errorbar sizes, etc.. (stuff that's present in
almost all experimental graphs)
You can set some sensible defaults for plots of fitted data, so you can
do all of the above in a faster and less error-prone way.
([grid], [plot fit &  residuals])
### importing data from files
Load a selected range of data from (.txt, .csv, .py, etc.) files as NumPy
arrays, loop over files in a directory with a few calls to function
([measured range], [file loop]).
## Setting up
This library was written entirely in Python 3.x, but because of its entry
level design should be effortless to readapt to Python 2.x.
Should be completely OS independent.
### Prerequisites
Phy lab requires three core packages from the SciPy ecosystem to work:
[Numpy], [SciPy] and [Matplotlib]. You should be able to obtain all 3 via `pip`
```
python -m pip install --user --upgrade numpy scipy matplotlib
```
Check [SciPy's installation page](https://www.scipy.org/install.html) for more details.
### Installation
The cleanest way to install and manage phylab is using `pip`:
```
python -m pip install --user phylab
```
Then simply import the main module with
```
import phylab as lab
```
Alternatively, you can clone the repository or download the latest release
manually and import the main module directly inside your script just as before.
## Using Phy lab
For a quick guide on how to use this library and to show just how much of a difference
using these few functions can have, compare [beat] and [beat_naive].
These two scripts do the same thing, but the first one is three times shorter
at 50 lines, runs ~ 0.2 seconds (30%) faster using less memory and can be
easily extended to work with more than 2 datasets, remove outliers,
compute FFT and so on... As you can see for example in [beat_ext].
## Development
Any and all suggestions are always appreciated, If you want to contribute
in any way don't hesitate to contact me. I'm always happy to learn something
new, so if you know how to improve any part of the code, find something
that needs fixing or even if you'd like to see something added going forwards,
feel free to let me know (here or at bernardo.tomelleri@gmail.com).
## License
Phy lab is licensed under the GNU General Public License v3.0 or later.
[//]: # (These are reference links used in the body of the readme and get
stripped out by the markdown processor.
See - http://stackoverflow.com/questions/4823468/store-comments-in-markdown-syntax)
   [lab]: <https://github.com/BernardoTomelleri/phylab/blob/master/phylab/lab.py>
   [circfit]: <https://github.com/BernardoTomelleri/phylab/blob/master/examples/circfit.py>
   [examples]: <https://github.com/BernardoTomelleri/phylab/tree/master/examples>
   [FFT repository]: <https://github.com/BernardoTomelleri/FFT>
   [Lock-in detector]: <https://github.com/BernardoTomelleri/FFT/blob/master/lockin.py>
   [Coope]: <https://ir.canterbury.ac.nz/bitstream/handle/10092/11104/coope_report_no69_1992.pdf?sequence=1&isAllowed=y>
   [dampened oscillator]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L58>
   [square wave]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L66>
   [Butterworth]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L131>
   [chi-square test]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L161>
   [errcor]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L292>
   [print correlations]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L301>
   [print parameters]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L308>
   [propagated fit]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L381>
   [ODRPACK]: <https://docs.scipy.org/doc/external/odrpack_guide.pdf>
   [scipy.optimize.curve_fit]: <https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html>
   [ellipse fit]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L589>
   [sampling]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L836>
   [FFT]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L851>
   [FWHM]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L336>
   [plotfft]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L801>
   [grid]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L653>
   [plot fit &  residuals]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L729>
   [measured range]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L902>
   [file loop]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L943>
   [SciPy]: <https://www.scipy.org/scipylib/index.html>   
   [NumPy]: <https://numpy.org/>
   [Matplotlib]: <https://matplotlib.org/stable/index.html>
   [beat]: <https://github.com/BernardoTomelleri/phylab/blob/master/examples/beat.py>
   [beat_naive]: <https://github.com/BernardoTomelleri/phylab/blob/master/examples/beat_naive.py>
   [beat_ext]: <https://github.com/BernardoTomelleri/phylab/blob/master/examples/beat_ext.py>

%package -n python3-phylab
Summary:	A Python module for basic data analysis and curve fitting for Physics laboratory students.
Provides:	python-phylab
BuildRequires:	python3-devel
BuildRequires:	python3-setuptools
BuildRequires:	python3-pip
%description -n python3-phylab
[![Build Status](https://github.com/BernardoTomelleri/phylab/actions//workflows/PyPI-publish-release.yml/badge.svg)](https://github.com/BernardoTomelleri/phylab/actions/workflows/PyPI-publish-release.yml)
[![GPLv3 License](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://opensource.org/licenses/gpl-3.0.html)
A Python package for basic data analysis and curve fitting for Physics laboratory students.
## Table of contents
- [Phy lab](#phy-lab)
  * [Table of contents](#table-of-contents)
  * [Philosophy behind this](#philosophy-behind-this)
    + [How this library came about](#how-this-library-came-about)
  * [Contents](#contents)
    + [model functions](#model-functions)
    + [testing and printing of fit results](#testing-and-printing-of-fit-results)
    + [curve-fitting routines](#curve-fitting-routines)
    + [Fourier transform utilities](#fourier-transform-utilities)
    + [data plotting](#data-plotting)
    + [importing data from files](#importing-data-from-files)
  * [Setting up](#setting-up)
    + [Prerequisites](#prerequisites)
    + [Installation](#installation)
  * [Using Phy lab](#using-phy-lab)
  * [Development](#development)
  * [License](#license)
## Philosophy behind this
Because this was written by someone starting to learn about experimental Physics
and Python (for students in a similar situation) all the functions in this module
try to use only the basic data structures and functionalities found in Python
that are briefly discussed with students (functions, arrays & not much else).
This is why you won't find classes, methods or even dicts in this package
and why the code inside may be very, very inefficient/ugly.
On the upside though, all the definitions inside should be easily comprehensible
and modifiable by anyone just starting out with programming.
### How this library came about
While writing the python scripts for individual experiments (at UniPi's Physics Lab)
I noticed that a significant amount of code was common to almost all of them.
I started writing down these common functions in a python module everytime this
happened, so that I could import them later to save a lot of time and hassle.
That's basically the story behind it: a collection of useful ideas I had while
learning how to analyze experimental data with Python's SciPy.
Now that it's grown into a powerful enough collection of tools, I decided to
release it for any and all students that may find it helpful for dealing
with similar problems.
## Contents
- The main module containing all the functions. [lab]
- A simple showcase of a couple of things this library allows you to do. [circfit]
  - Finding the best-fitting circle and ellipse for simulated or real sampled data points.
  - A quick comparison between using `curve_fit`, an algebraic circle fit
   (weighted [Coope] method) and an algebraic ellipse fit.
  - Plotting the $\chi^2(a, b)$ surface for a pair of parameters $a, b$ of the circle or the ellipse.
- Folder containing further examples and data that can be used in the demos. [examples]
For another example of where this package can come in handy feel free to check
out [FFT repository] and [Lock-in detector]. A small paper (in italiano) on fitting,
computing Fourier transforms and/or simulating the effect of a Lock-in detector
on real sampled signals or internally generated ones.
### model functions
A few definitions of the model functions more commonly encountered in the first
years of Physics lab (e.g. [dampened oscillator], [square wave])
along with a few digital filters (e.g. [Butterworth]).
Right at the beginning of the module so you can immediately start adding
the models you need.
### testing and printing of fit results
Goodness of fit tests, evaluation of parameter uncertainties and
correlations  ([chi-square test], [errcor]).
Simple print formatters that allow you to quickly display the results
of a fit and associated uncertainties with the desired number of significant
digits. ([print correlations], [print parameters])
### curve-fitting routines
Weighted least-square fitting accounting for uncertainties on x and y axes
with linear error propagation ([propagated fit]), relying on [scipy.optimize.curve_fit].
Weighted orthogonal distance regression thanks to [ODRPACK].
Weighted algebraic fits (like [ellipse fit] and others).
### Fourier transform utilities
Functions for computing FFTs of real and complex signals and other
associated quantities ([sampling], [FFT], [FWHM]), applying window functions
and displaying their output through Matplotlib ([plotfft]).
### data plotting
Instead of having to write multiple calls to function in order to:
activate axis minor ticks, setting their size, orientation and spacing,
placing grids, setting errorbar sizes, etc.. (stuff that's present in
almost all experimental graphs)
You can set some sensible defaults for plots of fitted data, so you can
do all of the above in a faster and less error-prone way.
([grid], [plot fit &  residuals])
### importing data from files
Load a selected range of data from (.txt, .csv, .py, etc.) files as NumPy
arrays, loop over files in a directory with a few calls to function
([measured range], [file loop]).
## Setting up
This library was written entirely in Python 3.x, but because of its entry
level design should be effortless to readapt to Python 2.x.
Should be completely OS independent.
### Prerequisites
Phy lab requires three core packages from the SciPy ecosystem to work:
[Numpy], [SciPy] and [Matplotlib]. You should be able to obtain all 3 via `pip`
```
python -m pip install --user --upgrade numpy scipy matplotlib
```
Check [SciPy's installation page](https://www.scipy.org/install.html) for more details.
### Installation
The cleanest way to install and manage phylab is using `pip`:
```
python -m pip install --user phylab
```
Then simply import the main module with
```
import phylab as lab
```
Alternatively, you can clone the repository or download the latest release
manually and import the main module directly inside your script just as before.
## Using Phy lab
For a quick guide on how to use this library and to show just how much of a difference
using these few functions can have, compare [beat] and [beat_naive].
These two scripts do the same thing, but the first one is three times shorter
at 50 lines, runs ~ 0.2 seconds (30%) faster using less memory and can be
easily extended to work with more than 2 datasets, remove outliers,
compute FFT and so on... As you can see for example in [beat_ext].
## Development
Any and all suggestions are always appreciated, If you want to contribute
in any way don't hesitate to contact me. I'm always happy to learn something
new, so if you know how to improve any part of the code, find something
that needs fixing or even if you'd like to see something added going forwards,
feel free to let me know (here or at bernardo.tomelleri@gmail.com).
## License
Phy lab is licensed under the GNU General Public License v3.0 or later.
[//]: # (These are reference links used in the body of the readme and get
stripped out by the markdown processor.
See - http://stackoverflow.com/questions/4823468/store-comments-in-markdown-syntax)
   [lab]: <https://github.com/BernardoTomelleri/phylab/blob/master/phylab/lab.py>
   [circfit]: <https://github.com/BernardoTomelleri/phylab/blob/master/examples/circfit.py>
   [examples]: <https://github.com/BernardoTomelleri/phylab/tree/master/examples>
   [FFT repository]: <https://github.com/BernardoTomelleri/FFT>
   [Lock-in detector]: <https://github.com/BernardoTomelleri/FFT/blob/master/lockin.py>
   [Coope]: <https://ir.canterbury.ac.nz/bitstream/handle/10092/11104/coope_report_no69_1992.pdf?sequence=1&isAllowed=y>
   [dampened oscillator]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L58>
   [square wave]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L66>
   [Butterworth]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L131>
   [chi-square test]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L161>
   [errcor]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L292>
   [print correlations]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L301>
   [print parameters]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L308>
   [propagated fit]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L381>
   [ODRPACK]: <https://docs.scipy.org/doc/external/odrpack_guide.pdf>
   [scipy.optimize.curve_fit]: <https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html>
   [ellipse fit]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L589>
   [sampling]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L836>
   [FFT]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L851>
   [FWHM]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L336>
   [plotfft]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L801>
   [grid]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L653>
   [plot fit &  residuals]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L729>
   [measured range]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L902>
   [file loop]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L943>
   [SciPy]: <https://www.scipy.org/scipylib/index.html>   
   [NumPy]: <https://numpy.org/>
   [Matplotlib]: <https://matplotlib.org/stable/index.html>
   [beat]: <https://github.com/BernardoTomelleri/phylab/blob/master/examples/beat.py>
   [beat_naive]: <https://github.com/BernardoTomelleri/phylab/blob/master/examples/beat_naive.py>
   [beat_ext]: <https://github.com/BernardoTomelleri/phylab/blob/master/examples/beat_ext.py>

%package help
Summary:	Development documents and examples for phylab
Provides:	python3-phylab-doc
%description help
[![Build Status](https://github.com/BernardoTomelleri/phylab/actions//workflows/PyPI-publish-release.yml/badge.svg)](https://github.com/BernardoTomelleri/phylab/actions/workflows/PyPI-publish-release.yml)
[![GPLv3 License](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://opensource.org/licenses/gpl-3.0.html)
A Python package for basic data analysis and curve fitting for Physics laboratory students.
## Table of contents
- [Phy lab](#phy-lab)
  * [Table of contents](#table-of-contents)
  * [Philosophy behind this](#philosophy-behind-this)
    + [How this library came about](#how-this-library-came-about)
  * [Contents](#contents)
    + [model functions](#model-functions)
    + [testing and printing of fit results](#testing-and-printing-of-fit-results)
    + [curve-fitting routines](#curve-fitting-routines)
    + [Fourier transform utilities](#fourier-transform-utilities)
    + [data plotting](#data-plotting)
    + [importing data from files](#importing-data-from-files)
  * [Setting up](#setting-up)
    + [Prerequisites](#prerequisites)
    + [Installation](#installation)
  * [Using Phy lab](#using-phy-lab)
  * [Development](#development)
  * [License](#license)
## Philosophy behind this
Because this was written by someone starting to learn about experimental Physics
and Python (for students in a similar situation) all the functions in this module
try to use only the basic data structures and functionalities found in Python
that are briefly discussed with students (functions, arrays & not much else).
This is why you won't find classes, methods or even dicts in this package
and why the code inside may be very, very inefficient/ugly.
On the upside though, all the definitions inside should be easily comprehensible
and modifiable by anyone just starting out with programming.
### How this library came about
While writing the python scripts for individual experiments (at UniPi's Physics Lab)
I noticed that a significant amount of code was common to almost all of them.
I started writing down these common functions in a python module everytime this
happened, so that I could import them later to save a lot of time and hassle.
That's basically the story behind it: a collection of useful ideas I had while
learning how to analyze experimental data with Python's SciPy.
Now that it's grown into a powerful enough collection of tools, I decided to
release it for any and all students that may find it helpful for dealing
with similar problems.
## Contents
- The main module containing all the functions. [lab]
- A simple showcase of a couple of things this library allows you to do. [circfit]
  - Finding the best-fitting circle and ellipse for simulated or real sampled data points.
  - A quick comparison between using `curve_fit`, an algebraic circle fit
   (weighted [Coope] method) and an algebraic ellipse fit.
  - Plotting the $\chi^2(a, b)$ surface for a pair of parameters $a, b$ of the circle or the ellipse.
- Folder containing further examples and data that can be used in the demos. [examples]
For another example of where this package can come in handy feel free to check
out [FFT repository] and [Lock-in detector]. A small paper (in italiano) on fitting,
computing Fourier transforms and/or simulating the effect of a Lock-in detector
on real sampled signals or internally generated ones.
### model functions
A few definitions of the model functions more commonly encountered in the first
years of Physics lab (e.g. [dampened oscillator], [square wave])
along with a few digital filters (e.g. [Butterworth]).
Right at the beginning of the module so you can immediately start adding
the models you need.
### testing and printing of fit results
Goodness of fit tests, evaluation of parameter uncertainties and
correlations  ([chi-square test], [errcor]).
Simple print formatters that allow you to quickly display the results
of a fit and associated uncertainties with the desired number of significant
digits. ([print correlations], [print parameters])
### curve-fitting routines
Weighted least-square fitting accounting for uncertainties on x and y axes
with linear error propagation ([propagated fit]), relying on [scipy.optimize.curve_fit].
Weighted orthogonal distance regression thanks to [ODRPACK].
Weighted algebraic fits (like [ellipse fit] and others).
### Fourier transform utilities
Functions for computing FFTs of real and complex signals and other
associated quantities ([sampling], [FFT], [FWHM]), applying window functions
and displaying their output through Matplotlib ([plotfft]).
### data plotting
Instead of having to write multiple calls to function in order to:
activate axis minor ticks, setting their size, orientation and spacing,
placing grids, setting errorbar sizes, etc.. (stuff that's present in
almost all experimental graphs)
You can set some sensible defaults for plots of fitted data, so you can
do all of the above in a faster and less error-prone way.
([grid], [plot fit &  residuals])
### importing data from files
Load a selected range of data from (.txt, .csv, .py, etc.) files as NumPy
arrays, loop over files in a directory with a few calls to function
([measured range], [file loop]).
## Setting up
This library was written entirely in Python 3.x, but because of its entry
level design should be effortless to readapt to Python 2.x.
Should be completely OS independent.
### Prerequisites
Phy lab requires three core packages from the SciPy ecosystem to work:
[Numpy], [SciPy] and [Matplotlib]. You should be able to obtain all 3 via `pip`
```
python -m pip install --user --upgrade numpy scipy matplotlib
```
Check [SciPy's installation page](https://www.scipy.org/install.html) for more details.
### Installation
The cleanest way to install and manage phylab is using `pip`:
```
python -m pip install --user phylab
```
Then simply import the main module with
```
import phylab as lab
```
Alternatively, you can clone the repository or download the latest release
manually and import the main module directly inside your script just as before.
## Using Phy lab
For a quick guide on how to use this library and to show just how much of a difference
using these few functions can have, compare [beat] and [beat_naive].
These two scripts do the same thing, but the first one is three times shorter
at 50 lines, runs ~ 0.2 seconds (30%) faster using less memory and can be
easily extended to work with more than 2 datasets, remove outliers,
compute FFT and so on... As you can see for example in [beat_ext].
## Development
Any and all suggestions are always appreciated, If you want to contribute
in any way don't hesitate to contact me. I'm always happy to learn something
new, so if you know how to improve any part of the code, find something
that needs fixing or even if you'd like to see something added going forwards,
feel free to let me know (here or at bernardo.tomelleri@gmail.com).
## License
Phy lab is licensed under the GNU General Public License v3.0 or later.
[//]: # (These are reference links used in the body of the readme and get
stripped out by the markdown processor.
See - http://stackoverflow.com/questions/4823468/store-comments-in-markdown-syntax)
   [lab]: <https://github.com/BernardoTomelleri/phylab/blob/master/phylab/lab.py>
   [circfit]: <https://github.com/BernardoTomelleri/phylab/blob/master/examples/circfit.py>
   [examples]: <https://github.com/BernardoTomelleri/phylab/tree/master/examples>
   [FFT repository]: <https://github.com/BernardoTomelleri/FFT>
   [Lock-in detector]: <https://github.com/BernardoTomelleri/FFT/blob/master/lockin.py>
   [Coope]: <https://ir.canterbury.ac.nz/bitstream/handle/10092/11104/coope_report_no69_1992.pdf?sequence=1&isAllowed=y>
   [dampened oscillator]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L58>
   [square wave]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L66>
   [Butterworth]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L131>
   [chi-square test]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L161>
   [errcor]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L292>
   [print correlations]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L301>
   [print parameters]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L308>
   [propagated fit]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L381>
   [ODRPACK]: <https://docs.scipy.org/doc/external/odrpack_guide.pdf>
   [scipy.optimize.curve_fit]: <https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html>
   [ellipse fit]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L589>
   [sampling]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L836>
   [FFT]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L851>
   [FWHM]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L336>
   [plotfft]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L801>
   [grid]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L653>
   [plot fit &  residuals]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L729>
   [measured range]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L902>
   [file loop]: <https://github.com/BernardoTomelleri/phylab/blob/1a4a86d121a1c07b91af5dfc7ec03443b1939035/phylab/lab.py#L943>
   [SciPy]: <https://www.scipy.org/scipylib/index.html>   
   [NumPy]: <https://numpy.org/>
   [Matplotlib]: <https://matplotlib.org/stable/index.html>
   [beat]: <https://github.com/BernardoTomelleri/phylab/blob/master/examples/beat.py>
   [beat_naive]: <https://github.com/BernardoTomelleri/phylab/blob/master/examples/beat_naive.py>
   [beat_ext]: <https://github.com/BernardoTomelleri/phylab/blob/master/examples/beat_ext.py>

%prep
%autosetup -n phylab-0.4.1

%build
%py3_build

%install
%py3_install
install -d -m755 %{buildroot}/%{_pkgdocdir}
if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi
if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi
if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi
if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi
pushd %{buildroot}
if [ -d usr/lib ]; then
	find usr/lib -type f -printf "\"/%h/%f\"\n" >> filelist.lst
fi
if [ -d usr/lib64 ]; then
	find usr/lib64 -type f -printf "\"/%h/%f\"\n" >> filelist.lst
fi
if [ -d usr/bin ]; then
	find usr/bin -type f -printf "\"/%h/%f\"\n" >> filelist.lst
fi
if [ -d usr/sbin ]; then
	find usr/sbin -type f -printf "\"/%h/%f\"\n" >> filelist.lst
fi
touch doclist.lst
if [ -d usr/share/man ]; then
	find usr/share/man -type f -printf "\"/%h/%f.gz\"\n" >> doclist.lst
fi
popd
mv %{buildroot}/filelist.lst .
mv %{buildroot}/doclist.lst .

%files -n python3-phylab -f filelist.lst
%dir %{python3_sitelib}/*

%files help -f doclist.lst
%{_docdir}/*

%changelog
* Tue Jun 20 2023 Python_Bot <Python_Bot@openeuler.org> - 0.4.1-1
- Package Spec generated