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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
|
%global _empty_manifest_terminate_build 0
Name: python-PyMODAlib
Version: 0.11.3b1
Release: 1
Summary: Library providing Python implementations of MODA's algorithms.
License: GNU General Public License v3 (GPLv3)
URL: https://github.com/luphysics/PyMODAlib
Source0: https://mirrors.aliyun.com/pypi/web/packages/e0/d4/1420a8f1786f7e694024cdb9bce11ea68d71c5e250a58b7423c0a9e57fe9/PyMODAlib-0.11.3b1.tar.gz
BuildArch: noarch
Requires: python3-AsyncProcessScheduler
Requires: python3-chdir
Requires: python3-numpy
Requires: python3-scipy
Requires: python3-matplotlib
%description
# PyMODAlib
[](https://zenodo.org/badge/latestdoi/243930888)
[](https://github.com/luphysics/PyMODAlib/blob/master/LICENSE)
[](https://pypi.org/project/PyMODAlib)
[](https://pypi.org/project/PyMODAlib)
[](https://pymodalib.readthedocs.io/en/latest/?badge=latest)
[](https://github.com/psf/black)
## Introduction
PyMODAlib is a Python library containing the algorithms used by [PyMODA](https://github.com/luphysics/PyMODA). With PyMODAlib, you can write Python scripts to perform the same calculations as PyMODA.
Some of PyMODAlib's algorithms are MATLAB-packaged libraries, while some are Python translations of algorithms belonging to [MODA](https://github.com/luphysics/MODA).
### License
You may use, distribute and modify this software under the terms of the [GNU General Public License v3.0](https://opensource.org/licenses/GPL-3.0). See [LICENSE](/LICENSE).
### References and citations
To cite PyMODAlib or view its references, please see the DOI at [Zenodo](https://zenodo.org/badge/latestdoi/243930888).
## User Guide
This section describes how to get started with PyMODAlib.
For a full API reference, please see PyMODAlib's [ReadTheDocs](https://pymodalib.readthedocs.io/) page, which shows the parameters and output for every function.
### Prerequisites
PyMODAlib requires Python 3.6 or higher. Some features also require the MATLAB Runtime, version 9.6.
> **Note:** See [current status](#current-status) to check which functions require the MATLAB Runtime.
### Installing PyMODAlib
PyMODAlib can be installed using `pip`. Open a terminal and run:
```bash
pip install pymodalib
```
> **Tip:** On some systems, you may need to supply the `--user` flag; if the installation is not successful, try `pip install pymodalib --user`.
> **Note:** On macOS/Linux, you may need to replace `pip` with the correct command for your system (e.g. `pip3`, `python -m pip` or `python3 -m pip`).
### Updating PyMODAlib
PyMODAlib will be updated regularly. To update your installed version, open a terminal and run:
```bash
pip install -U pymodalib
```
### Current status
`PyMODAlib` is still in development. Currently, the features implemented are:
| Feature | Implemented | Requires MATLAB Runtime | Notes |
| --- | --- | --- | --- |
| Wavelet transform | :heavy_check_mark: | No | |
| Wavelet phase coherence | :heavy_check_mark: | No | |
| Group coherence | :heavy_check_mark: | No | Uses MATLAB Runtime unless `implementation="python"` is passed as a parameter |
| Detecting harmonics | :heavy_check_mark: | No | |
| Downsampling | :heavy_check_mark: | No | |
### Getting started
### Examples
There are downloadable examples of using PyMODAlib's functionality in the [examples](https://github.com/luphysics/PyMODAlib/tree/master/examples) directory. There should be an example for each function, which also demonstrates how to plot the results.
To download the dependencies required to run the examples, open a terminal and run:
```bash
pip install -U pymodalib matplotlib
```
> **Tip:** If this causes problems, try the solutions outlined in the [Installing PyMODAlib](#installing-pymodalib) section.
To try the examples, download the PyMODAlib repository [as a zip file](https://github.com/luphysics/PyMODAlib/zipball/master) or by using `git clone`, then run relevant Python files from the `examples` subfolders.
#### Wavelet transform
This snippet demonstrates how to calculate and plot the wavelet transform of a signal. You can download the data file using [this link](https://github.com/luphysics/PyMODAlib/raw/master/examples/1signal_10Hz.npy).
> **Note:** You can load data from `.mat` files using `scipy.io.loadmat`.
```python
import pymodalib
import numpy as np
from matplotlib import pyplot as plt
# Load the signal from a data file.
signal = np.load("1signal_10Hz.npy")
# Sampling frequency is 10Hz.
fs = 10
# Generate the time values for the signal.
times = pymodalib.generate_times(signal, fs)
# Calculate the wavelet transform.
wt, freq = pymodalib.wavelet_transform(signal, fs)
# Calculate the amplitude of the wavelet transform.
amp_wt = np.abs(wt)
# Get Axes object from matplotlib.
ax = plt.gca()
ax.set_xlabel("Time (s)")
ax.set_ylabel("Frequency (Hz)")
ax.set_title("Amplitude of wavelet transform")
# Create the 'x' and 'y' values for plotting.
mesh1, mesh2 = np.meshgrid(times, freq)
# Plot the wavelet transform using PyMODAlib's colormap.
pymodalib.contourf(ax, mesh1, mesh2, amp_wt, log=True)
# Show the plot.
plt.show()
```
This snippet will produce the following plot:

### PyMODAlib cache
> **Note:** This section is only relevant when using the *group coherence* functions.
The group coherence functions use a very large quantity of RAM. To mitigate this problem for machines with smaller RAM capacities, they will allocate arrays which are cached to disk. *This may result in significant disk usage.*
By default, PyMODAlib will use a folder named `.pymodalib` inside your home directory for its cache. However, it will show a `RuntimeWarning` unless you set the location manually. This warning is intended to make users aware of the risks of placing the cache folder on an SSD.
> :warning: If the cache folder is on an SSD, it may **reduce the lifespan** of the SSD.
#### Setting the cache location
To set the location of the cache folder manually, use the `PYMODALIB_CACHE` environment variable. Instructions for different operating systems are below.
***The cache location should be set to an empty folder which resides on an HDD.***
##### Windows
- Create a folder to use for the cache.
- Press the start button and type "environment" until the option "Edit the system environment variables" appears, and click it.
- Click "Environment variables" near the bottom right of the dialog.
- Click "New" in the "System variables" section of the window which appears.
- In the dialog which opens, enter "PYMODALIB_CACHE" as the variable name and click "Browse Directory" to choose the folder you created.
- Press "Ok" to close all dialogs.
- Restart your IDE and/or terminal.
##### Linux
Create a folder to use for the cache. Open a terminal and `cd` to the folder, then copy the following commands into the terminal:
```bash
echo "export PYMODALIB_CACHE=$(pwd)" >> ~/.bashrc
source ~/.bashrc
```
You may need to restart your IDE and any other open terminals.
##### macOS
Create a folder to use for the cache. Open a terminal and `cd` to the folder, then copy the following commands into the terminal:
```bash
echo "export PYMODALIB_CACHE=$(pwd)" >> ~/.bash_profile
source ~/.bash_profile
```
You may need to restart your IDE and any other open terminals.
## Developer guide
This guide is aimed at developers interested in contributing to PyMODAlib.
### Downloading the code
To download the code, you should fork the repository and clone your fork.
### Installing the requirements
Open a terminal in the `PyMODAlib` folder and run:
```bash
pip install -r requirements.txt
pip install pre-commit
```
### Git hooks
Git hooks are used to automatically format modified Python files with [Black](https://github.com/psf/black) when a commit is made. This ensures that the code follows a uniform style.
To install the Git hooks, open a terminal in the `PyMODAlib` folder and run:
```bash
pre-commit install
```
> **Tip:** If this causes an error, try `python -m pre-commit install`.
When you make a commit, the modified Python files will be formatted if necessary. If this occurs, you'll need to repeat your `git add` and `git commit` commands to make the commit.
> **Tip:** You can still use PyCharm's auto-formatter while writing code. Although it sometimes disagrees with `black`, `black` will undo its changes at commit-time, so no harm is done.
### Developing PyMODAlib
When developing PyMODAlib, you can test your changes by installing the library locally in "editable" mode. From the root of the repository, run:
```bash
pip install -e .
```
> **Note:** After making changes to PyMODAlib, you don't need to run the `pip install` command again. Any Python script which imports `pymodalib` will reflect the changes immediately.
Switching back to the release version of PyMODAlib is simple:
```bash
pip uninstall pymodalib -y
pip install -U pymodalib
```
### Project structure
The public-facing API is located in the `algorithms` package. This package contains wrappers for the actual implementations, which can be found in the `implementations` package.
This structure allows the implementation to be easily changed, while ensuring that the API remains backwards-compatible.
#### `__init__.py`
In `pymodalib.__init__.py`, many functions are imported from the `algorithms` package. This allows users to more easily find useful functions: for example, they can use `pymodalib.wavelet_transform` instead of `pymodalib.algorithms.wavelet.wavelet_transform`.
#### `implementations` package
The `implementations` package contains a `matlab` package and a `python` package. The `matlab` package contains wrappers for algorithms supplied by MATLAB-packaged libraries, while the `python` package contains algorithms implemented purely in Python.
### Docstring style
PyMODAlib uses [Numpy-style docstrings](https://numpydoc.readthedocs.io/en/latest/format.html).
To configure PyCharm to use this style, go to `Settings` -> `Tools` -> `Python Integrated Tools` and change `Docstring format` from `reStructuredText` to `NumPy`.
### "Gotchas"
When importing a function/package from another part of `pymodalib`, your IDE may auto-import the package without the `pymodalib` prefix. For example, `pymodalib.algorithms.wavelet` may be imported as `algorithms.wavelet`. This can cause some confusing errors.
For reliable uninstall behaviour, don't run `python setup.py install`. The command listed above, using `pip`, is more reliable. To install PyMODAlib from source without using editable mode, you can run `pip install .`.
Don't install the library from source if your terminal has navigated to the folder via a symlink.
### MATLAB-packaged libraries
Currently, the MATLAB-packaged libraries are not required if functionality that depends on them is not used. MATLAB-packaged libraries are installed by downloading PyMODA and installing its dependencies.
Functions that require the MATLAB Runtime will be marked with the `matlabwrapper` decorator. This will check if the correct version of the MATLAB Runtime is installed.
> **Note:** MATLAB libraries are still incompatible with Python 3.8. When MATLAB R2020a releases, Python 3.8 support will be added but the current required version of the MATLAB Runtime will no longer be supported (users will need to upgrade to the newer Runtime).
### Packaging the project for PyPI
This section describes how to publish an update to PyPI.
From the [documentation](https://packaging.python.org/guides/distributing-packages-using-setuptools/#packaging-your-project):
```bash
rm -r dist/
python setup.py sdist
python setup.py bdist_wheel
twine upload dist/*
```
%package -n python3-PyMODAlib
Summary: Library providing Python implementations of MODA's algorithms.
Provides: python-PyMODAlib
BuildRequires: python3-devel
BuildRequires: python3-setuptools
BuildRequires: python3-pip
%description -n python3-PyMODAlib
# PyMODAlib
[](https://zenodo.org/badge/latestdoi/243930888)
[](https://github.com/luphysics/PyMODAlib/blob/master/LICENSE)
[](https://pypi.org/project/PyMODAlib)
[](https://pypi.org/project/PyMODAlib)
[](https://pymodalib.readthedocs.io/en/latest/?badge=latest)
[](https://github.com/psf/black)
## Introduction
PyMODAlib is a Python library containing the algorithms used by [PyMODA](https://github.com/luphysics/PyMODA). With PyMODAlib, you can write Python scripts to perform the same calculations as PyMODA.
Some of PyMODAlib's algorithms are MATLAB-packaged libraries, while some are Python translations of algorithms belonging to [MODA](https://github.com/luphysics/MODA).
### License
You may use, distribute and modify this software under the terms of the [GNU General Public License v3.0](https://opensource.org/licenses/GPL-3.0). See [LICENSE](/LICENSE).
### References and citations
To cite PyMODAlib or view its references, please see the DOI at [Zenodo](https://zenodo.org/badge/latestdoi/243930888).
## User Guide
This section describes how to get started with PyMODAlib.
For a full API reference, please see PyMODAlib's [ReadTheDocs](https://pymodalib.readthedocs.io/) page, which shows the parameters and output for every function.
### Prerequisites
PyMODAlib requires Python 3.6 or higher. Some features also require the MATLAB Runtime, version 9.6.
> **Note:** See [current status](#current-status) to check which functions require the MATLAB Runtime.
### Installing PyMODAlib
PyMODAlib can be installed using `pip`. Open a terminal and run:
```bash
pip install pymodalib
```
> **Tip:** On some systems, you may need to supply the `--user` flag; if the installation is not successful, try `pip install pymodalib --user`.
> **Note:** On macOS/Linux, you may need to replace `pip` with the correct command for your system (e.g. `pip3`, `python -m pip` or `python3 -m pip`).
### Updating PyMODAlib
PyMODAlib will be updated regularly. To update your installed version, open a terminal and run:
```bash
pip install -U pymodalib
```
### Current status
`PyMODAlib` is still in development. Currently, the features implemented are:
| Feature | Implemented | Requires MATLAB Runtime | Notes |
| --- | --- | --- | --- |
| Wavelet transform | :heavy_check_mark: | No | |
| Wavelet phase coherence | :heavy_check_mark: | No | |
| Group coherence | :heavy_check_mark: | No | Uses MATLAB Runtime unless `implementation="python"` is passed as a parameter |
| Detecting harmonics | :heavy_check_mark: | No | |
| Downsampling | :heavy_check_mark: | No | |
### Getting started
### Examples
There are downloadable examples of using PyMODAlib's functionality in the [examples](https://github.com/luphysics/PyMODAlib/tree/master/examples) directory. There should be an example for each function, which also demonstrates how to plot the results.
To download the dependencies required to run the examples, open a terminal and run:
```bash
pip install -U pymodalib matplotlib
```
> **Tip:** If this causes problems, try the solutions outlined in the [Installing PyMODAlib](#installing-pymodalib) section.
To try the examples, download the PyMODAlib repository [as a zip file](https://github.com/luphysics/PyMODAlib/zipball/master) or by using `git clone`, then run relevant Python files from the `examples` subfolders.
#### Wavelet transform
This snippet demonstrates how to calculate and plot the wavelet transform of a signal. You can download the data file using [this link](https://github.com/luphysics/PyMODAlib/raw/master/examples/1signal_10Hz.npy).
> **Note:** You can load data from `.mat` files using `scipy.io.loadmat`.
```python
import pymodalib
import numpy as np
from matplotlib import pyplot as plt
# Load the signal from a data file.
signal = np.load("1signal_10Hz.npy")
# Sampling frequency is 10Hz.
fs = 10
# Generate the time values for the signal.
times = pymodalib.generate_times(signal, fs)
# Calculate the wavelet transform.
wt, freq = pymodalib.wavelet_transform(signal, fs)
# Calculate the amplitude of the wavelet transform.
amp_wt = np.abs(wt)
# Get Axes object from matplotlib.
ax = plt.gca()
ax.set_xlabel("Time (s)")
ax.set_ylabel("Frequency (Hz)")
ax.set_title("Amplitude of wavelet transform")
# Create the 'x' and 'y' values for plotting.
mesh1, mesh2 = np.meshgrid(times, freq)
# Plot the wavelet transform using PyMODAlib's colormap.
pymodalib.contourf(ax, mesh1, mesh2, amp_wt, log=True)
# Show the plot.
plt.show()
```
This snippet will produce the following plot:

### PyMODAlib cache
> **Note:** This section is only relevant when using the *group coherence* functions.
The group coherence functions use a very large quantity of RAM. To mitigate this problem for machines with smaller RAM capacities, they will allocate arrays which are cached to disk. *This may result in significant disk usage.*
By default, PyMODAlib will use a folder named `.pymodalib` inside your home directory for its cache. However, it will show a `RuntimeWarning` unless you set the location manually. This warning is intended to make users aware of the risks of placing the cache folder on an SSD.
> :warning: If the cache folder is on an SSD, it may **reduce the lifespan** of the SSD.
#### Setting the cache location
To set the location of the cache folder manually, use the `PYMODALIB_CACHE` environment variable. Instructions for different operating systems are below.
***The cache location should be set to an empty folder which resides on an HDD.***
##### Windows
- Create a folder to use for the cache.
- Press the start button and type "environment" until the option "Edit the system environment variables" appears, and click it.
- Click "Environment variables" near the bottom right of the dialog.
- Click "New" in the "System variables" section of the window which appears.
- In the dialog which opens, enter "PYMODALIB_CACHE" as the variable name and click "Browse Directory" to choose the folder you created.
- Press "Ok" to close all dialogs.
- Restart your IDE and/or terminal.
##### Linux
Create a folder to use for the cache. Open a terminal and `cd` to the folder, then copy the following commands into the terminal:
```bash
echo "export PYMODALIB_CACHE=$(pwd)" >> ~/.bashrc
source ~/.bashrc
```
You may need to restart your IDE and any other open terminals.
##### macOS
Create a folder to use for the cache. Open a terminal and `cd` to the folder, then copy the following commands into the terminal:
```bash
echo "export PYMODALIB_CACHE=$(pwd)" >> ~/.bash_profile
source ~/.bash_profile
```
You may need to restart your IDE and any other open terminals.
## Developer guide
This guide is aimed at developers interested in contributing to PyMODAlib.
### Downloading the code
To download the code, you should fork the repository and clone your fork.
### Installing the requirements
Open a terminal in the `PyMODAlib` folder and run:
```bash
pip install -r requirements.txt
pip install pre-commit
```
### Git hooks
Git hooks are used to automatically format modified Python files with [Black](https://github.com/psf/black) when a commit is made. This ensures that the code follows a uniform style.
To install the Git hooks, open a terminal in the `PyMODAlib` folder and run:
```bash
pre-commit install
```
> **Tip:** If this causes an error, try `python -m pre-commit install`.
When you make a commit, the modified Python files will be formatted if necessary. If this occurs, you'll need to repeat your `git add` and `git commit` commands to make the commit.
> **Tip:** You can still use PyCharm's auto-formatter while writing code. Although it sometimes disagrees with `black`, `black` will undo its changes at commit-time, so no harm is done.
### Developing PyMODAlib
When developing PyMODAlib, you can test your changes by installing the library locally in "editable" mode. From the root of the repository, run:
```bash
pip install -e .
```
> **Note:** After making changes to PyMODAlib, you don't need to run the `pip install` command again. Any Python script which imports `pymodalib` will reflect the changes immediately.
Switching back to the release version of PyMODAlib is simple:
```bash
pip uninstall pymodalib -y
pip install -U pymodalib
```
### Project structure
The public-facing API is located in the `algorithms` package. This package contains wrappers for the actual implementations, which can be found in the `implementations` package.
This structure allows the implementation to be easily changed, while ensuring that the API remains backwards-compatible.
#### `__init__.py`
In `pymodalib.__init__.py`, many functions are imported from the `algorithms` package. This allows users to more easily find useful functions: for example, they can use `pymodalib.wavelet_transform` instead of `pymodalib.algorithms.wavelet.wavelet_transform`.
#### `implementations` package
The `implementations` package contains a `matlab` package and a `python` package. The `matlab` package contains wrappers for algorithms supplied by MATLAB-packaged libraries, while the `python` package contains algorithms implemented purely in Python.
### Docstring style
PyMODAlib uses [Numpy-style docstrings](https://numpydoc.readthedocs.io/en/latest/format.html).
To configure PyCharm to use this style, go to `Settings` -> `Tools` -> `Python Integrated Tools` and change `Docstring format` from `reStructuredText` to `NumPy`.
### "Gotchas"
When importing a function/package from another part of `pymodalib`, your IDE may auto-import the package without the `pymodalib` prefix. For example, `pymodalib.algorithms.wavelet` may be imported as `algorithms.wavelet`. This can cause some confusing errors.
For reliable uninstall behaviour, don't run `python setup.py install`. The command listed above, using `pip`, is more reliable. To install PyMODAlib from source without using editable mode, you can run `pip install .`.
Don't install the library from source if your terminal has navigated to the folder via a symlink.
### MATLAB-packaged libraries
Currently, the MATLAB-packaged libraries are not required if functionality that depends on them is not used. MATLAB-packaged libraries are installed by downloading PyMODA and installing its dependencies.
Functions that require the MATLAB Runtime will be marked with the `matlabwrapper` decorator. This will check if the correct version of the MATLAB Runtime is installed.
> **Note:** MATLAB libraries are still incompatible with Python 3.8. When MATLAB R2020a releases, Python 3.8 support will be added but the current required version of the MATLAB Runtime will no longer be supported (users will need to upgrade to the newer Runtime).
### Packaging the project for PyPI
This section describes how to publish an update to PyPI.
From the [documentation](https://packaging.python.org/guides/distributing-packages-using-setuptools/#packaging-your-project):
```bash
rm -r dist/
python setup.py sdist
python setup.py bdist_wheel
twine upload dist/*
```
%package help
Summary: Development documents and examples for PyMODAlib
Provides: python3-PyMODAlib-doc
%description help
# PyMODAlib
[](https://zenodo.org/badge/latestdoi/243930888)
[](https://github.com/luphysics/PyMODAlib/blob/master/LICENSE)
[](https://pypi.org/project/PyMODAlib)
[](https://pypi.org/project/PyMODAlib)
[](https://pymodalib.readthedocs.io/en/latest/?badge=latest)
[](https://github.com/psf/black)
## Introduction
PyMODAlib is a Python library containing the algorithms used by [PyMODA](https://github.com/luphysics/PyMODA). With PyMODAlib, you can write Python scripts to perform the same calculations as PyMODA.
Some of PyMODAlib's algorithms are MATLAB-packaged libraries, while some are Python translations of algorithms belonging to [MODA](https://github.com/luphysics/MODA).
### License
You may use, distribute and modify this software under the terms of the [GNU General Public License v3.0](https://opensource.org/licenses/GPL-3.0). See [LICENSE](/LICENSE).
### References and citations
To cite PyMODAlib or view its references, please see the DOI at [Zenodo](https://zenodo.org/badge/latestdoi/243930888).
## User Guide
This section describes how to get started with PyMODAlib.
For a full API reference, please see PyMODAlib's [ReadTheDocs](https://pymodalib.readthedocs.io/) page, which shows the parameters and output for every function.
### Prerequisites
PyMODAlib requires Python 3.6 or higher. Some features also require the MATLAB Runtime, version 9.6.
> **Note:** See [current status](#current-status) to check which functions require the MATLAB Runtime.
### Installing PyMODAlib
PyMODAlib can be installed using `pip`. Open a terminal and run:
```bash
pip install pymodalib
```
> **Tip:** On some systems, you may need to supply the `--user` flag; if the installation is not successful, try `pip install pymodalib --user`.
> **Note:** On macOS/Linux, you may need to replace `pip` with the correct command for your system (e.g. `pip3`, `python -m pip` or `python3 -m pip`).
### Updating PyMODAlib
PyMODAlib will be updated regularly. To update your installed version, open a terminal and run:
```bash
pip install -U pymodalib
```
### Current status
`PyMODAlib` is still in development. Currently, the features implemented are:
| Feature | Implemented | Requires MATLAB Runtime | Notes |
| --- | --- | --- | --- |
| Wavelet transform | :heavy_check_mark: | No | |
| Wavelet phase coherence | :heavy_check_mark: | No | |
| Group coherence | :heavy_check_mark: | No | Uses MATLAB Runtime unless `implementation="python"` is passed as a parameter |
| Detecting harmonics | :heavy_check_mark: | No | |
| Downsampling | :heavy_check_mark: | No | |
### Getting started
### Examples
There are downloadable examples of using PyMODAlib's functionality in the [examples](https://github.com/luphysics/PyMODAlib/tree/master/examples) directory. There should be an example for each function, which also demonstrates how to plot the results.
To download the dependencies required to run the examples, open a terminal and run:
```bash
pip install -U pymodalib matplotlib
```
> **Tip:** If this causes problems, try the solutions outlined in the [Installing PyMODAlib](#installing-pymodalib) section.
To try the examples, download the PyMODAlib repository [as a zip file](https://github.com/luphysics/PyMODAlib/zipball/master) or by using `git clone`, then run relevant Python files from the `examples` subfolders.
#### Wavelet transform
This snippet demonstrates how to calculate and plot the wavelet transform of a signal. You can download the data file using [this link](https://github.com/luphysics/PyMODAlib/raw/master/examples/1signal_10Hz.npy).
> **Note:** You can load data from `.mat` files using `scipy.io.loadmat`.
```python
import pymodalib
import numpy as np
from matplotlib import pyplot as plt
# Load the signal from a data file.
signal = np.load("1signal_10Hz.npy")
# Sampling frequency is 10Hz.
fs = 10
# Generate the time values for the signal.
times = pymodalib.generate_times(signal, fs)
# Calculate the wavelet transform.
wt, freq = pymodalib.wavelet_transform(signal, fs)
# Calculate the amplitude of the wavelet transform.
amp_wt = np.abs(wt)
# Get Axes object from matplotlib.
ax = plt.gca()
ax.set_xlabel("Time (s)")
ax.set_ylabel("Frequency (Hz)")
ax.set_title("Amplitude of wavelet transform")
# Create the 'x' and 'y' values for plotting.
mesh1, mesh2 = np.meshgrid(times, freq)
# Plot the wavelet transform using PyMODAlib's colormap.
pymodalib.contourf(ax, mesh1, mesh2, amp_wt, log=True)
# Show the plot.
plt.show()
```
This snippet will produce the following plot:

### PyMODAlib cache
> **Note:** This section is only relevant when using the *group coherence* functions.
The group coherence functions use a very large quantity of RAM. To mitigate this problem for machines with smaller RAM capacities, they will allocate arrays which are cached to disk. *This may result in significant disk usage.*
By default, PyMODAlib will use a folder named `.pymodalib` inside your home directory for its cache. However, it will show a `RuntimeWarning` unless you set the location manually. This warning is intended to make users aware of the risks of placing the cache folder on an SSD.
> :warning: If the cache folder is on an SSD, it may **reduce the lifespan** of the SSD.
#### Setting the cache location
To set the location of the cache folder manually, use the `PYMODALIB_CACHE` environment variable. Instructions for different operating systems are below.
***The cache location should be set to an empty folder which resides on an HDD.***
##### Windows
- Create a folder to use for the cache.
- Press the start button and type "environment" until the option "Edit the system environment variables" appears, and click it.
- Click "Environment variables" near the bottom right of the dialog.
- Click "New" in the "System variables" section of the window which appears.
- In the dialog which opens, enter "PYMODALIB_CACHE" as the variable name and click "Browse Directory" to choose the folder you created.
- Press "Ok" to close all dialogs.
- Restart your IDE and/or terminal.
##### Linux
Create a folder to use for the cache. Open a terminal and `cd` to the folder, then copy the following commands into the terminal:
```bash
echo "export PYMODALIB_CACHE=$(pwd)" >> ~/.bashrc
source ~/.bashrc
```
You may need to restart your IDE and any other open terminals.
##### macOS
Create a folder to use for the cache. Open a terminal and `cd` to the folder, then copy the following commands into the terminal:
```bash
echo "export PYMODALIB_CACHE=$(pwd)" >> ~/.bash_profile
source ~/.bash_profile
```
You may need to restart your IDE and any other open terminals.
## Developer guide
This guide is aimed at developers interested in contributing to PyMODAlib.
### Downloading the code
To download the code, you should fork the repository and clone your fork.
### Installing the requirements
Open a terminal in the `PyMODAlib` folder and run:
```bash
pip install -r requirements.txt
pip install pre-commit
```
### Git hooks
Git hooks are used to automatically format modified Python files with [Black](https://github.com/psf/black) when a commit is made. This ensures that the code follows a uniform style.
To install the Git hooks, open a terminal in the `PyMODAlib` folder and run:
```bash
pre-commit install
```
> **Tip:** If this causes an error, try `python -m pre-commit install`.
When you make a commit, the modified Python files will be formatted if necessary. If this occurs, you'll need to repeat your `git add` and `git commit` commands to make the commit.
> **Tip:** You can still use PyCharm's auto-formatter while writing code. Although it sometimes disagrees with `black`, `black` will undo its changes at commit-time, so no harm is done.
### Developing PyMODAlib
When developing PyMODAlib, you can test your changes by installing the library locally in "editable" mode. From the root of the repository, run:
```bash
pip install -e .
```
> **Note:** After making changes to PyMODAlib, you don't need to run the `pip install` command again. Any Python script which imports `pymodalib` will reflect the changes immediately.
Switching back to the release version of PyMODAlib is simple:
```bash
pip uninstall pymodalib -y
pip install -U pymodalib
```
### Project structure
The public-facing API is located in the `algorithms` package. This package contains wrappers for the actual implementations, which can be found in the `implementations` package.
This structure allows the implementation to be easily changed, while ensuring that the API remains backwards-compatible.
#### `__init__.py`
In `pymodalib.__init__.py`, many functions are imported from the `algorithms` package. This allows users to more easily find useful functions: for example, they can use `pymodalib.wavelet_transform` instead of `pymodalib.algorithms.wavelet.wavelet_transform`.
#### `implementations` package
The `implementations` package contains a `matlab` package and a `python` package. The `matlab` package contains wrappers for algorithms supplied by MATLAB-packaged libraries, while the `python` package contains algorithms implemented purely in Python.
### Docstring style
PyMODAlib uses [Numpy-style docstrings](https://numpydoc.readthedocs.io/en/latest/format.html).
To configure PyCharm to use this style, go to `Settings` -> `Tools` -> `Python Integrated Tools` and change `Docstring format` from `reStructuredText` to `NumPy`.
### "Gotchas"
When importing a function/package from another part of `pymodalib`, your IDE may auto-import the package without the `pymodalib` prefix. For example, `pymodalib.algorithms.wavelet` may be imported as `algorithms.wavelet`. This can cause some confusing errors.
For reliable uninstall behaviour, don't run `python setup.py install`. The command listed above, using `pip`, is more reliable. To install PyMODAlib from source without using editable mode, you can run `pip install .`.
Don't install the library from source if your terminal has navigated to the folder via a symlink.
### MATLAB-packaged libraries
Currently, the MATLAB-packaged libraries are not required if functionality that depends on them is not used. MATLAB-packaged libraries are installed by downloading PyMODA and installing its dependencies.
Functions that require the MATLAB Runtime will be marked with the `matlabwrapper` decorator. This will check if the correct version of the MATLAB Runtime is installed.
> **Note:** MATLAB libraries are still incompatible with Python 3.8. When MATLAB R2020a releases, Python 3.8 support will be added but the current required version of the MATLAB Runtime will no longer be supported (users will need to upgrade to the newer Runtime).
### Packaging the project for PyPI
This section describes how to publish an update to PyPI.
From the [documentation](https://packaging.python.org/guides/distributing-packages-using-setuptools/#packaging-your-project):
```bash
rm -r dist/
python setup.py sdist
python setup.py bdist_wheel
twine upload dist/*
```
%prep
%autosetup -n PyMODAlib-0.11.3b1
%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-PyMODAlib -f filelist.lst
%dir %{python3_sitelib}/*
%files help -f doclist.lst
%{_docdir}/*
%changelog
* Thu Jun 08 2023 Python_Bot <Python_Bot@openeuler.org> - 0.11.3b1-1
- Package Spec generated
|