From 80f5046f0ccf853193bf645810c498e5166d431f Mon Sep 17 00:00:00 2001 From: gcattan Date: Wed, 6 Oct 2021 19:30:04 +0200 Subject: [PATCH] add setup file, and tests infrasturcture --- .coveragerc | 8 +++ .gitignore | 7 +++ MANIFEST.in | 1 + covmatTest/__init__.py | 1 + covmatTest/get_covmat.py | 59 +++++++++++++++++++ setup.py | 122 +++++++++++++++++++++++++++++++++++++++ tests/conftest.py | 33 +++++++++++ tests/test_get_covmat.py | 5 ++ 8 files changed, 236 insertions(+) create mode 100644 .coveragerc create mode 100644 .gitignore create mode 100644 MANIFEST.in create mode 100644 covmatTest/__init__.py create mode 100644 covmatTest/get_covmat.py create mode 100644 setup.py create mode 100644 tests/conftest.py create mode 100644 tests/test_get_covmat.py diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 0000000..3b75ecc --- /dev/null +++ b/.coveragerc @@ -0,0 +1,8 @@ +[report] +include = */covmatTest/* +omit = + */python?.?/* + */site-packages/pytest/* + */setup.py + */tests/* + */examples/* diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eae3a7d --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +*.pyc +.DS_Store +.coverage +.coveralls.yml +/dist +/*.egg-info/* + diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..bb3ec5f --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1 @@ +include README.md diff --git a/covmatTest/__init__.py b/covmatTest/__init__.py new file mode 100644 index 0000000..bf15b5d --- /dev/null +++ b/covmatTest/__init__.py @@ -0,0 +1 @@ +from .get_covmat import get_covmat \ No newline at end of file diff --git a/covmatTest/get_covmat.py b/covmatTest/get_covmat.py new file mode 100644 index 0000000..a18930f --- /dev/null +++ b/covmatTest/get_covmat.py @@ -0,0 +1,59 @@ +from pyriemann.estimation import Covariances +from alphawaves.dataset import AlphaWaves +import numpy as np +import mne +import random + +""" +============================= +Get a covariance matrice sample from alphawaves dataset +============================= + +TODO + +""" +# Authors: Gregoire Cattan +# +# License: Apache 2.0 + +import warnings +warnings.filterwarnings("ignore") + +trials = [] + + +def _get_trials(raw): + events = mne.find_events(raw=raw, shortest_event=1, verbose=False) + event_id = {'closed': 1, 'open': 2} + epochs = mne.Epochs(raw, events, event_id, tmin=2.0, tmax=8.0, baseline=None, + verbose=False, preload=True) + epochs.pick_types(eeg=True) + return epochs.get_data() + +def _init(): + dataset = AlphaWaves(useMontagePosition = False) + + subjects = dataset.subject_list + n_subjects = len(subjects) + subject = random.randint(0, n_subjects - 1) + + raw = dataset._get_single_subject_data(subject) + + global trials + trials = _get_trials(raw) + return len(trials) + +def _get_covmat(n_channels): + global trials + n_trials = len(trials) + if n_trials == 0: + n_trials = _init() + index = random.randint(0, n_trials - 1) + trial = np.array([trials[index][:n_channels,:]]) + return Covariances(estimator='lwf').fit_transform(trial) + +def get_covmat(n_matrices, n_channels): + covset = np.zeros((n_matrices, n_channels, n_channels)) + for i in range(0, n_matrices): + covset[i] = _get_covmat(n_channels) + return covset diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..f836a42 --- /dev/null +++ b/setup.py @@ -0,0 +1,122 @@ +"""A setuptools based setup module. +See: +https://packaging.python.org/en/latest/distributing.html +https://github.com/pypa/sampleproject +""" + +from setuptools import setup, find_packages +from os import path +from io import open + +here = path.abspath(path.dirname(__file__)) + +with open(path.join(here, 'README.md'), encoding='utf-8') as f: + long_description = f.read() + +setup( + + name='covmatTest', + + version='0.1', + + license='Apache 2.0', + + description='Alpha Waves Dataset', + + long_description=long_description, + + long_description_content_type='text/markdown', + + url='https://github.com/gcattan/covmatTest', + + author='Gregoire Cattan', + + author_email='gcattan@hotmail.fr', # Optional + + # For a list of valid classifiers, see https://pypi.org/classifiers/ + classifiers=[ + # How mature is this project? Common values are + # 3 - Alpha + # 4 - Beta + # 5 - Production/Stable + 'Development Status :: 3 - Alpha', + + # Indicate who your project is intended for + 'Intended Audience :: Science/Research', + 'Topic :: Scientific/Engineering', + + # Pick your license as you wish + 'License :: OSI Approved', + + # Specify the Python versions you support here. In particular, ensure + # that you indicate whether you support Python 2, Python 3 or both. + 'Programming Language :: Python :: 3.7', + ], + + # Note that this is a string of words separated by whitespace, not a list. + keywords='EEG neuroscience test covariance matrices', # Optional + + packages=find_packages(exclude=['contrib', 'docs', 'tests']), # Required + + # For an analysis of "install_requires" vs pip's requirements files see: + # https://packaging.python.org/en/latest/requirements.html + install_requires=["alphawaves"], + + # List additional groups of dependencies here (e.g. development + # dependencies). Users will be able to install these using the "extras" + # syntax, for example: + # + # $ pip install sampleproject[dev] + # + # Similar to `install_requires` above, these must be valid existing + # projects. + # extras_require={ # Optional + # 'dev': ['check-manifest'], + # 'test': ['coverage'], + # }, + + # If there are data files included in your packages that need to be + # installed, specify them here. + # + # If using Python 2.6 or earlier, then these have to be included in + # MANIFEST.in as well. + # package_data={ # Optional + # 'sample': ['package_data.dat'], + # }, + + # Although 'package_data' is the preferred approach, in some case you may + # need to place data files outside of your packages. See: + # http://docs.python.org/3.4/distutils/setupscript.html#installing-additional-files + # + # In this case, 'data_file' will be installed into '/my_data' + # data_files=[('my_data', ['data/data_file'])], # Optional + + # To provide executable scripts, use entry points in preference to the + # "scripts" keyword. Entry points provide cross-platform support and allow + # `pip` to create the appropriate form of executable for the target + # platform. + # + # For example, the following would provide a command called `sample` which + # executes the function `main` from this package when invoked: + # entry_points={ # Optional + # 'console_scripts': [ + # 'sample=sample:main', + # ], + # }, + + # List additional URLs that are relevant to your project as a dict. + # + # This field corresponds to the "Project-URL" metadata fields: + # https://packaging.python.org/specifications/core-metadata/#project-url-multiple-use + # + # Examples listed include a pattern for specifying where the package tracks + # issues, where the source is hosted, where to say thanks to the package + # maintainers, and where to support the project financially. The key is + # what's used to render the link text on PyPI. + # project_urls={ # Optional + # 'Bug Reports': 'https://github.com/pypa/sampleproject/issues', + # 'Funding': 'https://donate.pypi.org', + # 'Say Thanks!': 'http://saythanks.io/to/example', + # 'Source': 'https://github.com/pypa/sampleproject/', + # }, +) diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..8f7d1cd --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,33 @@ +import pytest +from pytest import approx +import numpy as np + + +def is_positive_semi_definite(X): + cs = X.shape[-1] + return np.all(np.linalg.eigvals(X.reshape((-1, cs, cs))) >= 0.0) + + +def is_positive_definite(X): + cs = X.shape[-1] + return np.all(np.linalg.eigvals(X.reshape((-1, cs, cs))) > 0.0) + + +def is_symmetric(X): + return X == approx(np.swapaxes(X, -2, -1)) + + +@pytest.fixture +def is_spd(): + def _is_spd(X): + return is_symmetric(X) and is_positive_definite(X) + + return _is_spd + + +@pytest.fixture +def is_spsd(): + def _is_spsd(X): + return is_symmetric(X) and is_positive_semi_definite(X) + + return _is_spsd diff --git a/tests/test_get_covmat.py b/tests/test_get_covmat.py new file mode 100644 index 0000000..9c87c33 --- /dev/null +++ b/tests/test_get_covmat.py @@ -0,0 +1,5 @@ +from covmatTest import get_covmat + + +def test_canary(is_spd, is_spds): + get_covmat(1, 1)