Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[patch:lib] Rename normalize function to center #62

Merged
merged 2 commits into from
Jan 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Sequentia offers the use of **multivariate observation sequences with differing

### Preprocessing methods

- [x] Normalization
- [x] Centering
- [x] Downsampling (decimation and averaging)
- [x] Filtering (mean and median)
- [x] Discrete Fourier Transform
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import numpy as np
from sequentia.preprocessing import normalize
from sequentia.preprocessing import center

# Create some sample data
X = [np.random.random((10 * i, 3)) for i in range(1, 4)]

# Normalize the data
X = normalize(X)
# Center the data
X = center(X)
2 changes: 1 addition & 1 deletion docs/_includes/examples/preprocessing/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

# Create the Preprocess object
pre = Preprocess()
pre.normalize()
pre.center()
pre.filtrate(n=5, method='median')
pre.downsample(n=5, method='decimate')
pre.fft()
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Sequentia offers some appropriate classification algorithms for these kinds of t
:maxdepth: 1
:caption: Preprocessing Methods

sections/preprocessing/normalize.rst
sections/preprocessing/center.rst
sections/preprocessing/downsample.rst
sections/preprocessing/filtrate.rst
sections/preprocessing/fft.rst
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
.. _normalize:
.. _center:

Normalization (``normalize``)
Centering (``center``)
=============================

Normalizing centers an observation sequence about the mean of its observations – that is, given:
Centers an observation sequence about the mean of its observations – that is, given:

.. math::

Expand All @@ -22,17 +22,17 @@ Where :math:`\overline{o_d}` represents the mean of the :math:`d^\text{th}` feat

We subtract :math:`\boldsymbol{\mu}` from each observation, or row in :math:`O`. This centers the observations.

For further information, please see the `preprocessing tutorial notebook <https://nbviewer.jupyter.org/github/eonu/sequentia/blob/master/notebooks/2%20-%20Preprocessing%20%28Tutorial%29.ipynb#Normalization-(normalize)>`_.
For further information, please see the `preprocessing tutorial notebook <https://nbviewer.jupyter.org/github/eonu/sequentia/blob/master/notebooks/2%20-%20Preprocessing%20%28Tutorial%29.ipynb#Centering-(center)>`_.

Example
-------

.. literalinclude:: ../../_includes/examples/preprocessing/normalize.py
.. literalinclude:: ../../_includes/examples/preprocessing/center.py
:language: python
:linenos:

API reference
-------------

.. automodule:: sequentia.preprocessing
.. autofunction:: normalize
.. autofunction:: center
4 changes: 2 additions & 2 deletions lib/sequentia/preprocessing/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .methods import (
downsample, normalize, fft, filtrate,
_downsample, _normalize, _fft, _filtrate
downsample, center, fft, filtrate,
_downsample, _center, _fft, _filtrate
)
from .preprocess import Preprocess
12 changes: 6 additions & 6 deletions lib/sequentia/preprocessing/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import numpy as np
from ..internals import _Validator

def normalize(X):
"""Normalizes an observation sequence (or multiple sequences) by centering observations around the mean.
def center(X):
"""Centers an observation sequence (or multiple sequences) by centering observations around the mean.

Parameters
----------
Expand All @@ -12,14 +12,14 @@ def normalize(X):

Returns
-------
normalized: numpy.ndarray or List[numpy.ndarray]
The normalized input observation sequence(s).
centered: numpy.ndarray or List[numpy.ndarray]
The centered input observation sequence(s).
"""
val = _Validator()
val.observation_sequences(X, allow_single=True)
return _normalize(X)
return _center(X)

def _normalize(X):
def _center(X):
def transform(x):
return x - x.mean(axis=0)

Expand Down
12 changes: 6 additions & 6 deletions lib/sequentia/preprocessing/preprocess.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import numpy as np
from .methods import _normalize, _downsample, _fft, _filtrate
from .methods import _center, _downsample, _fft, _filtrate
from ..internals import _Validator

class Preprocess:
Expand All @@ -9,9 +9,9 @@ def __init__(self):
self._transforms = []
self._val = _Validator()

def normalize(self):
"""Normalizes an observation sequence (or multiple sequences) by centering observations around the mean."""
self._transforms.append((_normalize, {}))
def center(self):
"""Centers an observation sequence (or multiple sequences) by centering observations around the mean."""
self._transforms.append((_center, {}))

def downsample(self, n, method='decimate'):
"""Downsamples an observation sequence (or multiple sequences) by either:
Expand Down Expand Up @@ -93,8 +93,8 @@ def summary(self):

for i, (transform, kwargs) in enumerate(self._transforms):
idx = i + 1
if transform == _normalize:
steps.append(('{}. Normalization'.format(idx), None))
if transform == _center:
steps.append(('{}. Centering'.format(idx), None))
elif transform == _downsample:
header = 'Decimating' if kwargs['method'] == 'decimate' else 'Averaging'
steps.append((
Expand Down
26 changes: 13 additions & 13 deletions lib/test/lib/preprocessing/test_methods.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest
import numpy as np
from sequentia.preprocessing import downsample, normalize, fft, filtrate
from sequentia.preprocessing import downsample, center, fft, filtrate
from ...support import assert_equal, assert_all_equal

# Set seed for reproducible randomness
Expand All @@ -13,13 +13,13 @@
X_odd = rng.random((7, 2))
Xs = [i * rng.random((3 * i, 2)) for i in range(1, 4)]

# =========== #
# normalize() #
# =========== #
# ======== #
# center() #
# ======== #

def test_normalize_single_even():
"""Normalize a single even-length observation sequence"""
assert_equal(normalize(X_even), np.array([
def test_center_single_even():
"""Center a single even-length observation sequence"""
assert_equal(center(X_even), np.array([
[-0.07922094, 0.09684335 ],
[-0.02527107, -0.07346283],
[-0.20437965, 0.0275481 ],
Expand All @@ -28,9 +28,9 @@ def test_normalize_single_even():
[0.16369059 , -0.0894511 ]
]))

def test_normalize_single_odd():
"""Normalize a single odd-length observation sequence"""
assert_equal(normalize(X_odd), np.array([
def test_center_single_odd():
"""Center a single odd-length observation sequence"""
assert_equal(center(X_odd), np.array([
[0.14006915 , 0.2206014 ],
[-0.35693936, -0.61786594],
[-0.40775702, 0.1276246 ],
Expand All @@ -40,9 +40,9 @@ def test_normalize_single_odd():
[-0.30970099, -0.06507422]
]))

def test_normalize_multiple():
"""Normalize multiple observation sequences"""
assert_all_equal(normalize(Xs), [
def test_center_multiple():
"""Center multiple observation sequences"""
assert_all_equal(center(Xs), [
np.array([
[-0.16656579, 0.23348073 ],
[0.21192925 , -0.29652624],
Expand Down
48 changes: 24 additions & 24 deletions lib/test/lib/preprocessing/test_preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import numpy as np
from sequentia.preprocessing import (
Preprocess,
downsample, normalize, fft, filtrate,
_downsample, _normalize, _fft, _filtrate
downsample, center, fft, filtrate,
_downsample, _center, _fft, _filtrate
)
from ...support import assert_equal, assert_all_equal

Expand All @@ -16,9 +16,9 @@
X = rng.random((7, 2))
Xs = [i * rng.random((3 * i, 2)) for i in range(1, 4)]

# Normalization preprocessor
# Centering preprocessor
norm = Preprocess()
norm.normalize()
norm.center()

# Discrete Fourier Transform preprocessor
fourier = Preprocess()
Expand All @@ -36,35 +36,35 @@

# Combined preprocessor
combined = Preprocess()
combined.normalize()
combined.center()
combined.filtrate(**filt_kwargs)
combined.downsample(**down_kwargs)
combined.fft()

# ====================== #
# Preprocess.normalize() #
# ====================== #
# =================== #
# Preprocess.center() #
# =================== #

def test_normalize_adds_transform():
"""Applying a single normalization transformation"""
def test_center_adds_transform():
"""Applying a single centering transformation"""
assert len(norm._transforms) == 1
assert norm._transforms[0] == (_normalize, {})
assert norm._transforms[0] == (_center, {})

def test_normalize_single():
"""Applying normalization to a single observation sequence"""
assert_equal(norm.transform(X), normalize(X))
def test_center_single():
"""Applying centering to a single observation sequence"""
assert_equal(norm.transform(X), center(X))

def test_normalize_multiple():
"""Applying normalization to multiple observation sequences"""
assert_all_equal(norm.transform(Xs), normalize(Xs))
def test_center_multiple():
"""Applying centering to multiple observation sequences"""
assert_all_equal(norm.transform(Xs), center(Xs))

def test_normalize_summary(capsys):
"""Summary of a normalization transformation"""
def test_center_summary(capsys):
"""Summary of a centering transformation"""
norm.summary()
assert capsys.readouterr().out == (
'Preprocessing summary:\n'
'======================\n'
'1. Normalization\n'
'1. Centering\n'
'======================\n'
)

Expand Down Expand Up @@ -159,7 +159,7 @@ def test_combined_adds_transforms():
"""Applying multiple filtering transformations"""
assert len(combined._transforms) == 4
assert combined._transforms == [
(_normalize, {}),
(_center, {}),
(_filtrate, filt_kwargs),
(_downsample, down_kwargs),
(_fft, {})
Expand All @@ -168,7 +168,7 @@ def test_combined_adds_transforms():
def test_combined_single():
"""Applying combined transformations to a single observation sequence"""
X_pre = X
X_pre = normalize(X_pre)
X_pre = center(X_pre)
X_pre = filtrate(X_pre, **filt_kwargs)
X_pre = downsample(X_pre, **down_kwargs)
X_pre = fft(X_pre)
Expand All @@ -177,7 +177,7 @@ def test_combined_single():
def test_combined_multiple():
"""Applying combined transformations to multiple observation sequences"""
Xs_pre = Xs
Xs_pre = normalize(Xs_pre)
Xs_pre = center(Xs_pre)
Xs_pre = filtrate(Xs_pre, **filt_kwargs)
Xs_pre = downsample(Xs_pre, **down_kwargs)
Xs_pre = fft(Xs_pre)
Expand All @@ -189,7 +189,7 @@ def test_combined_summary(capsys):
assert capsys.readouterr().out == (
' Preprocessing summary: \n'
'==========================================\n'
'1. Normalization\n'
'1. Centering\n'
'------------------------------------------\n'
'2. Filtering:\n'
' Median filter with window size (n=3)\n'
Expand Down
38 changes: 19 additions & 19 deletions notebooks/2 - Preprocessing (Tutorial).ipynb

Large diffs are not rendered by default.