Skip to content

Commit

Permalink
fix #1
Browse files Browse the repository at this point in the history
  • Loading branch information
leliel12 committed Feb 2, 2017
1 parent eaa25e2 commit 9a8099a
Show file tree
Hide file tree
Showing 4 changed files with 247 additions and 96 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
# =============================================================================

REQUIREMENTS = [
"numpy", "scipy", "six", "attrs"
"numpy", "scipy", "six", "attrs", "mock"
]


Expand Down
54 changes: 53 additions & 1 deletion skcriteria/norm.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"""


# =============================================================================
# IMPORTS
# =============================================================================
Expand All @@ -29,10 +28,59 @@
from numpy import linalg


# =============================================================================
# EXCEPTIONS
# =============================================================================

class DuplicatedNameError(ValueError):
pass


class NormalizerNotFound(AttributeError):
pass


# =============================================================================
# REGISTERS
# =============================================================================

NORMALIZERS = {}


def register(name, func=None):
if name in NORMALIZERS:
raise DuplicatedNameError(name)
if func is None:
def _dec(func):
NORMALIZERS[name] = func
return func
return _dec
else:
NORMALIZERS[name] = func
return func


def norm(name, arr, axis=None):
try:
return NORMALIZERS[name](arr, axis=axis)
except KeyError:
raise NormalizerNotFound(name)


# =============================================================================
# IMPLEMENTATIONS
# =============================================================================

@register("none")
def none(arr, axis=None):
"""This do not nothing and only try to return an numpy.ndarray
of the given data
"""
return np.asarray(arr)


@register("sum")
def sum(arr, axis=None):
r"""Divide of every value on the array by sum of values along an
axis.
Expand Down Expand Up @@ -81,6 +129,7 @@ def sum(arr, axis=None):
return arr / sumval


@register("max")
def max(arr, axis=None):
r"""Divide of every value on the array by max value along an axis.
Expand Down Expand Up @@ -124,6 +173,7 @@ def max(arr, axis=None):
return arr / maxval


@register("vector")
def vector(arr, axis=None):
r"""Caculates the set of ratios as the square roots of the sum of squared
responses of a given axis as denominators. If *axis* is *None* sum all
Expand Down Expand Up @@ -172,6 +222,7 @@ def vector(arr, axis=None):
return arr / frob


@register("push_negatives")
def push_negatives(arr, axis=None):
r"""If an array has negative values this function increment the values
proportionally to made all the array positive along an axis.
Expand Down Expand Up @@ -228,6 +279,7 @@ def push_negatives(arr, axis=None):
return arr - delta


@register("add1to0")
def add1to0(arr, axis=None):
r"""If a value in the array is 0, then an :math:`1` is added to
all the values
Expand Down
95 changes: 1 addition & 94 deletions skcriteria/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
# IMPORTS
# =============================================================================

import collections
import random

import numpy as np
Expand All @@ -32,7 +31,7 @@

from . import core

from .. import util, norm, rank
from .. import util, rank


# =============================================================================
Expand Down Expand Up @@ -69,98 +68,6 @@ def test_criteriarr(self):
arr_result = util.criteriarr(arr)


# =============================================================================
# norm.py TEST
# =============================================================================

class NormTest(core.SKCriteriaTestCase):

def setUp(self):
super(NormTest, self).setUp()
cols = random.randint(100, 1000)
rows = random.randint(100, 1000)
self.mtx = [
[random.randint(1, 1000) for _ in range(cols)]
for _ in range(rows)
]
self.arr = [random.randint(1, 1000) for _ in range(cols)]

def _test_normalizer(self, normfunc, mtx_result, arr_result, **kwargs):
mtx_func_result = normfunc(self.mtx, axis=0)
arr_func_result = normfunc(self.arr)
self.assertAllClose(mtx_result, mtx_func_result, **kwargs)
self.assertAllClose(arr_result, arr_func_result, **kwargs)

def test_SumNormalizer(self):
sums = collections.defaultdict(float)
for row in self.mtx:
for coln, col in enumerate(row):
sums[coln] += col
mtx_result = [
[(col / sums[coln]) for coln, col in enumerate(row)]
for row in self.mtx
]
arr_sum = float(sum(self.arr))
arr_result = [(col / arr_sum) for col in self.arr]
self._test_normalizer(norm.sum, mtx_result, arr_result)

def test_MaxNormalizer(self):
maxes = collections.defaultdict(lambda: None)
for row in self.mtx:
for coln, col in enumerate(row):
if maxes[coln] is None or maxes[coln] < col:
maxes[coln] = col
mtx_result = [
[(float(col) / maxes[coln]) for coln, col in enumerate(row)]
for row in self.mtx
]
arr_max = float(max(self.arr))
arr_result = [(col / arr_max) for col in self.arr]
self._test_normalizer(norm.max, mtx_result, arr_result)

def test_VectorNormalizer(self):
colsums = collections.defaultdict(float)
for row in self.mtx:
for coln, col in enumerate(row):
colsums[coln] += col ** 2
mtx_result = [
[(col / np.sqrt(colsums[coln])) for coln, col in enumerate(row)]
for row in self.mtx
]
arr_sum = sum([col ** 2 for col in self.arr])
arr_result = [(col / np.sqrt(arr_sum)) for col in self.arr]
self._test_normalizer(norm.vector, mtx_result, arr_result)

def test_push_negatives(self):
self.mtx = [
[1, -2, 3],
[4, 5, 6]
]
mtx_result = [
[1, 0, 3],
[4, 7, 6]
]

self.arr = [1, -2, 3]
arr_result = [3, 0, 5]
self._test_normalizer(norm.push_negatives, mtx_result, arr_result)

def test_add1to0(self):
self.mtx = [
[1, 0, 3],
[4, 5, 0]
]

mtx_result = [
[1, 1, 4],
[4, 6, 1],
]

self.arr = [1, 0, 0]
arr_result = [1, 1, 1]
self._test_normalizer(norm.add1to0, mtx_result, arr_result, atol=1)


# =============================================================================
# RANK TEST
# =============================================================================
Expand Down

0 comments on commit 9a8099a

Please sign in to comment.