Skip to content

Commit

Permalink
Merge pull request #17 from LRydin/v0.4
Browse files Browse the repository at this point in the history
Merging with dev.
  • Loading branch information
LRydin authored Jan 18, 2021
2 parents ab356f0 + 4c79f3f commit 2a51c20
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 31 deletions.
33 changes: 28 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
language: python
python:
# - "3.4"
# - "3.5"
- "3.4"
- "3.5"
- "3.6"
- "3.7"
# - "nightly"
- "3.8"
- "3.9"
- "nightly"

arch:
- amd64
- arm64

# command to install dependencies

before_install:
Expand All @@ -14,9 +21,25 @@ install:
- pip install coverage
- pip install -r requirements.txt

# command to run tests
jobs:
include:
- stage: "Extra packages"
name: "Testing extra packages"
python: 3.6
install:
- pip install coverage
- pip install -r requirements.txt
- pip install EMD-signal
script:
- coverage run -m pytest test/test_EMD.py
- coverage run -m pytest test/test_MFDFA_extras.py
allow_failures:
- python: "nightly"


script:
coverage run -m pytest test/
- coverage run -m pytest test/test_fgn.py
- coverage run -m pytest test/test_MFDFA.py

after_success:
bash <(curl -s https://codecov.io/bash)
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 Rydin
Copyright (c) 2019-2021 Rydin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
22 changes: 20 additions & 2 deletions MFDFA/emddetrender.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
# Empirical Mode Decomposition algorithm', https://github.com/laszukdawid/PyEMD,
# licenced under the Apache 2.0 Licencing.

from PyEMD import EMD
# from PyEMD import EMD
import numpy as np

# Import of PyEMD is called inside the function

def detrendedtimeseries(timeseries: np.ndarray, modes: list):
"""
The function calculates the Intrinsic Mode Functions (IMFs) of a given
Expand Down Expand Up @@ -50,6 +52,7 @@ def detrendedtimeseries(timeseries: np.ndarray, modes: list):

return detrendedTimeseries


def IMFs(timeseries: np.ndarray):
"""
Extract the Intrinsic Mode Functions (IMFs) of a given timeseries.
Expand All @@ -62,14 +65,19 @@ def IMFs(timeseries: np.ndarray):
Notes
-----
.. versionadded:: 0.3
Returns
-------
IMFs: np.ndarray
The Intrinsic Mode Functions (IMFs) of the Empirical Mode Decomposition.
These are of shape ``(..., timeseries.size)``, with the first dimension
varying depending on the data. Last entry is the residuals.
"""

# Check if EMD-signal is installed
missing_library()
from PyEMD import EMD

# Initiate pyEMD's EMD function
emd = EMD()

Expand All @@ -78,3 +86,13 @@ def IMFs(timeseries: np.ndarray):

# Returns the IMFs as a (..., timeseries.size) numpy array.
return IMFs


def missing_library():
try:
import PyEMD.EMD as _EMD
except ImportError:
raise ImportError(("PyEMD is required to do Empirical Mode "
"decomposition. Please install PyEMD with 'pip "
"install EMD-signal'.")
)
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ Let `Xₜ` be a multi fractal stochastic process. This mean `Xₜ` scales with s
With the help of taking different powers variations of the `DFA`, one we can distinguish monofractal and multifractal processes.

# Changelog
- Version 0.4 - EMD is now optional. Restored back compatibility: py3.3 to py3.9
- Version 0.3 - Adding EMD detrending. First release. PyPI code.
- Version 0.2 - Removed experimental features. Added documentation
- Version 0.1 - Uploaded initial working code
Expand All @@ -125,7 +126,7 @@ This project was started in 2019 at the [Faculty of Mathematics, University of O


### Funding
Helmholtz Association Initiative *Energy System 2050 - A Contribution of the Research Field Energy* and the grant No. VH-NG-1025, *STORM - Stochastics for Time-Space Risk Models* project of the Research Council of Norway (RCN) No. 274410, and the *E-ON Stipendienfonds*.
Helmholtz Association Initiative *Energy System 2050 - A Contribution of the Research Field Energy* and the grant No. VH-NG-1025; *STORM - Stochastics for Time-Space Risk Models* project of the Research Council of Norway (RCN) No. 274410, and the *E-ON Stipendienfonds*.

### References
<sup>1</sup>Peng, C.-K., Buldyrev, S. V., Havlin, S., Simons, M., Stanley, H. E., & Goldberger, A. L. (1994). *Mosaic organization of DNA nucleotides*. [Physical Review E, 49(2), 1685–1689](https://doi.org/10.1103/PhysRevE.49.1685)\
Expand Down
2 changes: 1 addition & 1 deletion contributions.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Contributions
`MDFA` has lived so far out of collaborations. It welcomes reviews and ideas from everyone. If you want to share your ideas or report a bug, open an [issue](https://github.com/LRydin/KramersMoyal/issues) here on GitHub, or contact me directly: [leonardo.rydin"at"gmail.com](mailto:leonardo.rydin@gmail.com)
`MFDFA` has lived so far out of collaborations. It welcomes reviews and ideas from everyone. If you want to share your ideas or report a bug, open an [issue](https://github.com/LRydin/KramersMoyal/issues) here on GitHub, or contact me directly: [leonardo.rydin"at"gmail.com](mailto:leonardo.rydin@gmail.com)

I'm are happy to have you help me in furthering this project, helping me improve the code and documentation, make it more user-friendly, broadening its applicability, or devising other methodologies of implementation.

Expand Down
8 changes: 8 additions & 0 deletions docs/emd_detrending.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ Employing Empirical Mode Decompositions for detrending
`Empirical Mode Decomposition <https://en.wikipedia.org/wiki/Hilbert%E2%80%93Huang_transform>`_ (EMD), or maybe more correctly described, the Hilbert─Huang transform is a transformation analogous to a Fourier or Hilbert transform that decomposes a one-dimensional timeseries or signal into its Intrinsic Mode Functions (IMFs).
For our purposes, we simply want to employ EMD to detrend a timeseries.

.. warning::

To use this feature, you need to first install `PyEMD <https://github.com/laszukdawid/PyEMD>`_ (EMD-signal) with

::

pip install EMD-signal

Understanding :code:`MFDFA`'s :code:`EMD` detrender
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
11 changes: 9 additions & 2 deletions docs/installation.rst
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
Installation
============

For the moment the library is available from TestPyPI, so you can use
:code:`MFDFA` is available from PyPI, so you can use

::

pip install -i https://test.pypi.org/simple/ MFDFA
pip install MFDFA

Then on your favourite editor just use

.. code:: python
from MFDFA import MFDFA
.. warning::
To use the extension to include Empirical Mode Decomposition detrending you will also need

::

pip install EMD-signal
2 changes: 1 addition & 1 deletion docs/license.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ License

MIT License

Copyright (c) 2019-2020 Rydin
Copyright (c) 2019-2021 Rydin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
numpy
EMD-signal
6 changes: 4 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@

setuptools.setup(
name="MFDFA",
version="0.3",
version="0.4",
author="Leonardo Rydin Gorjao",
author_email="leonardo.rydin@gmail.com",
description="Multifractal Detrended Fluctuation Analysis in Python",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/LRydin/MFDFA",
packages=setuptools.find_packages(),
install_requires = ["numpy"],
extras_require = {"EMD-signal": ["EMD-signal"]},
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires='>=3.6',
python_requires='>=3.3',
)
15 changes: 0 additions & 15 deletions test/test_MFDFA.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,6 @@ def test_MFDFA():
assert edfa.ndim == 2, "Output is not 2 dimensional"
assert dfa.shape[1] <= q.shape[0], "Output shape mismatch"

# Testing EMD extension
lag, dfa = MFDFA(X, lag = lag, q = q, order = 0,
stat = False, extensions = {'EMD': [0]})

assert dfa.ndim == 2, "Output is not 2 dimensional"
assert dfa.shape[1] <= q.shape[0], "Output shape mismatch"

# Testing EMD and eDFA extensions together
lag, dfa, edfa = MFDFA(X, lag = lag, q = q, order = 1,
stat = False, extensions = {'eDFA':True, 'EMD': [0]})

assert dfa.ndim == 2, "Output is not 2 dimensional"
assert edfa.ndim == 2, "Output is not 2 dimensional"
assert dfa.shape[1] <= q.shape[0], "Output shape mismatch"

# Testing moving window
lag, dfa = MFDFA(X, lag = lag, q = q, order = 1,
stat = False, extensions = {'window': 5})
Expand Down
32 changes: 32 additions & 0 deletions test/test_MFDFA_extras.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import numpy as np

import sys
sys.path.append("../")
from MFDFA import MFDFA

def test_MFDFA():
for N in [1000, 10000]:
for q_list in [1, 2, 6, 21]:

X = np.random.normal(size = N, loc = 0)
q = np.linspace(-10, 10, q_list)

lag = np.unique(
np.logspace(
0, np.log10(X.size // 4), 25
).astype(int) + 1
)

lag, dfa = MFDFA(X, lag = lag, q = q, order = 0,
modified = True, stat = False, extensions = {'EMD': [0]})

assert dfa.ndim == 2, "Output is not 2 dimensional"
assert dfa.shape[1] <= q.shape[0], "Output shape mismatch"

lag, dfa, edfa = MFDFA(X, lag = lag, q = q, order = 1,
modified = True, stat = False,
extensions = {'eDFA':True, 'EMD': [0]})

assert dfa.ndim == 2, "Output is not 2 dimensional"
assert edfa.ndim == 2, "Output is not 2 dimensional"
assert dfa.shape[1] <= q.shape[0], "Output shape mismatch"

0 comments on commit 2a51c20

Please sign in to comment.