Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jmyrberg committed Dec 14, 2020
0 parents commit 5c14439
Show file tree
Hide file tree
Showing 20 changed files with 476 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .readthedocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# .readthedocs.yml
# Read the Docs configuration file
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details

# Required
version: 2

# Build documentation in the docs/ directory with Sphinx
sphinx:
builder: html
configuration: docs/source/conf.py
fail_on_warning: false

# Optionally build your docs in additional formats such as PDF and ePub
formats: all

# Optionally set the version of Python and requirements required to build your docs
python:
version: 3.8
install:
- requirements: docs/requirements.txt
- requirements: requirements.txt
60 changes: 60 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
language: python
python:
- '3.8'
os:
- linux
branches:
only:
- master
install:
- pip install -r requirements.txt -r requirements-dev.txt
script: pytest -v
jobs:
include:
- stage: test
- stage: release
install: skip
script: skip
python: '3.8'
before_deploy:
- export RELEASE_VERSION=$( cat VERSION )
- git config --local user.name "jmyrberg"
- git config --local user.email "jesse.myrberg@gmail.com"
- git tag $RELEASE_VERSION
deploy:
provider: releases
name: Version $RELEASE_VERSION
tag_name: "$RELEASE_VERSION"
cleanup: false
prerelease: true
token:
secure:
file:
- dist/*.tar.gz
file_glob: true
on:
branch: master
repo: jmyrberg/mknapsack
after_success:
- git push --tags
- stage: deploy
install: skip
script: skip
python: '3.8'
deploy:
provider: pypi
username: jmyrberg
distributions: sdist
cleanup: false
password:
secure:
on:
branch: master
repo: jmyrberg/aakr
stages:
- name: test
if: type IN (pull_request, cron, api)
- name: release
if: type IN (push)
- name: deploy
if: type IN (push)
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Jesse Myrberg

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include VERSION
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# aakr

[![Build Status](https://travis-ci.com/jmyrberg/aakr.svg?branch=master)](https://travis-ci.com/jmyrberg/aakr)

Python implementation of the Auto-Associative Kernel Regression (AAKR). The algorithm is suitable for signal reconstruction, which further be used for e.g. condition monitoring or anomaly detection.


## Installation

`pip install aakr`


## Example usage

Give examples of normal conditions as pandas DataFrame or numpy array.

```python
from aakr import AAKR

aakr = AAKR()
aakr.fit(X_obs_nc)
```

Predict normal condition for given observations.

```python
X_nc = aakr.predict(X_obs)
```


## References

* [MTM algorithm by Martello and Toth](http://people.sc.fsu.edu/~jburkardt/f77_src/knapsack/knapsack.f) (Fortran)
* [A modified Auto Associative Kernel Regression method for robust signal reconstruction in nuclear power plant components](https://www.researchgate.net/publication/292538769_A_modified_Auto_Associative_Kernel_Regression_method_for_robust_signal_reconstruction_in_nuclear_power_plant_components)

---
Jesse Myrberg (jesse.myrberg@gmail.com)
6 changes: 6 additions & 0 deletions Untitled.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 4
}
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.0.1dev0
4 changes: 4 additions & 0 deletions aakr/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from ._aakr import AAKR
from ._version import __version__

__all__ = ['AAKR', '__version__']
84 changes: 84 additions & 0 deletions aakr/_aakr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
"""Module for models."""


import numpy as np

from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.metrics.pairwise import pairwise_distances, pairwise_kernels
from sklearn.utils.validation import check_array, check_is_fitted


class AAKR(TransformerMixin, BaseEstimator):
"""A template estimator to be used as a reference implementation.
For more information regarding how to build your own estimator, read more
in the :ref:`User Guide <user_guide>`.
Parameters
----------
demo_param : str, default='demo_param'
A parameter used for demonstation of how to pass and store paramters.
Examples
--------
>>> from aakr import AAKR
>>> import numpy as np
>>> X = np.arange(100).reshape(50, 2)
>>> aakr = AAKR()
>>> aakr.fit(X)
AAKR(metric='euclidean', bw=1, n_jobs=None)
"""
def __init__(self, metric='euclidean', bw=1, n_jobs=None):
self.metric = metric
self.bw = bw
self.n_jobs = n_jobs

def fit(self, X, y=None):
"""A reference implementation of a fitting function for a transformer.
Parameters
----------
X : array-like, shape (n_samples, n_features)
Training examples from normal conditions.
y : None
Not needed, exists for compability purposes.
Returns
-------
self : object
Returns self.
"""
X = check_array(X)
self.X_ = X
return self

def predict(self, X, **kwargs):
""" A reference implementation of a prediction for a classifier.
Parameters
----------
X : array-like, shape (n_samples, n_features)
The input samples.
Returns
-------
X_nc : ndarray, shape (n_samples, n_features)
Expected values in normal conditions for each sample and feature.
"""
# Validation
check_is_fitted(self, 'X_')

X = check_array(X)

if X.shape[1] != self.X_.shape[1]:
raise ValueError('Shape of input is different from what was seen'
'in `fit`')

# Kernel regression
D = pairwise_distances(X=self.X_, Y=X, metric=self.metric,
n_jobs=self.n_jobs, **kwargs)
k = 1 / np.sqrt(2 * np.pi * self.bw ** 2)
w = k * np.exp(-D ** 2 / (2 * self.bw ** 2))
X_nc = w.T.dot(self.X_) / w.sum(0)[:, None]

return X_nc
2 changes: 2 additions & 0 deletions aakr/_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
with open('./VERSION', 'r') as f:
__version__ = f.read()
123 changes: 123 additions & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html


# -- Init --------------------------------------------------------------------
# See https://github.com/miyakogi/m2r/issues/51
import sphinx

def monkeypatch(cls):
""" decorator to monkey-patch methods """
def decorator(f):
method = f.__name__
old_method = getattr(cls, method)
setattr(cls, method, lambda self, *args,
**kwargs: f(old_method, self, *args, **kwargs))
return decorator

# workaround until https://github.com/miyakogi/m2r/pull/55 is merged
@monkeypatch(sphinx.registry.SphinxComponentRegistry)
def add_source_parser(_old_add_source_parser, self, *args, **kwargs):
# signature is (parser: Type[Parser], **kwargs), but m2r expects
# the removed (str, parser: Type[Parser], **kwargs).
if isinstance(args[0], str):
args = args[1:]
return _old_add_source_parser(self, *args, **kwargs)


# -- Path setup --------------------------------------------------------------

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
import sys
sys.path.insert(0, os.path.abspath('../../'))


# -- Project information -----------------------------------------------------

project = 'finscraper'
copyright = '2020, Jesse Myrberg'
author = 'Jesse Myrberg'

# The full version, including alpha/beta/rc tags
with open('../../VERSION', 'r') as f:
release = f.read().strip()


# -- General configuration ---------------------------------------------------

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'm2r',
'sphinxcontrib.napoleon',
'sphinx.ext.autodoc',
'sphinx_rtd_theme',
]

# Autodoc settings
autodoc_default_options = {
'inherited-members': True
}

# Napoleon settings
napoleon_google_docstring = True
napoleon_numpy_docstring = True
napoleon_include_init_with_doc = True
napoleon_include_private_with_doc = False
napoleon_include_special_with_doc = False
napoleon_use_admonition_for_examples = False
napoleon_use_admonition_for_notes = False
napoleon_use_admonition_for_references = False
napoleon_use_ivar = False
napoleon_use_param = True
napoleon_use_rtype = True
napoleon_use_keyword = True
napoleon_custom_sections = None

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = []

source_suffix = ['.rst', '.md']


# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'sphinx_rtd_theme'

html_theme_options = {
'canonical_url': '',
#'analytics_id': 'UA-XXXXXXX-1', # Provided by Google in your dashboard
'logo_only': False,
'display_version': True,
'prev_next_buttons_location': 'bottom',
'style_external_links': False,
#'vcs_pageview_mode': '',
#'style_nav_header_background': 'white',
# Toc options
'collapse_navigation': True,
'sticky_navigation': True,
'navigation_depth': 4,
'includehidden': True,
'titles_only': False
}

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
Empty file added pytest.ini
Empty file.
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest>=5.4.2
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
numpy>=1.19.4
pandas>=1.1.5
scikit-learn>=0.23.2
8 changes: 8 additions & 0 deletions scripts/build-documentation.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh

conda activate finscraper && \
rm -rf docs/build && \
sphinx-apidoc -f -o docs/source finscraper && \
rm -rf docs/source/modules.rst && \
sphinx-build -b html docs/source docs/build
open -a /Applications/Safari.app file://~/Documents/Personal/finscraper/docs/build/index.html
Loading

0 comments on commit 5c14439

Please sign in to comment.