Skip to content

Commit

Permalink
alias
Browse files Browse the repository at this point in the history
  • Loading branch information
leliel12 committed Jan 6, 2018
1 parent f478476 commit 2620e35
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
28 changes: 20 additions & 8 deletions skcriteria/tests/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,32 @@ def test_from_list(self):
self.assertAllClose(arr, arr_result)
self.assertIsInstance(arr_result, np.ndarray)

def test_from_array(self):
arr = np.array(
[random.choice(self.min_max) for _ in self.rrange(100, 1000)])
arr_result = criteriarr(arr)
self.assertAllClose(arr, arr_result)
self.assertIsInstance(arr_result, np.ndarray)
self.assertIs(arr, arr_result)

def test_no_min_max(self):
arr = [
random.choice(self.min_max) for _ in self.rrange(100, 1000)] + [2]
with self.assertRaises(ValueError):
criteriarr(arr)

def test_alias(self):
original = np.array([-1, 1])
criterias = (
[min, max],
[np.min, np.max],
[np.amin, np.amax],
[np.nanmin, np.nanmax],
["minimize", "maximize"],
["min", "max"])
for arr in criterias:
parsed = criteriarr(arr)
self.assertArrayEqual(parsed, original)
with self.assertRaises(ValueError):
criteriarr(arr + [2])
with self.assertRaises(ValueError):
criteriarr(arr + [range])
with self.assertRaises(ValueError):
criteriarr(arr + ["foo"])



class IsMtx(SKCriteriaTestCase):

Expand Down
23 changes: 18 additions & 5 deletions skcriteria/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
# IMPORTS
# =============================================================================

import itertools as it

import numpy as np


Expand All @@ -67,9 +69,16 @@
MIN = -1
"""Int: Minimization criteria"""

MIN_ALIASES = [MIN, min, np.min, np.nanmin, np.amin, "min", "minimize"]
"""Another ways to name the minimization criterias."""

MAX = 1
"""Int: Maximization criteria"""

MAX_ALIASES = [MAX, max, np.max, np.nanmax, np.amax, "max", "maximize"]
"""Another way to name the maximization criterias."""


CRITERIA_STR = {
MIN: "min",
MAX: "max"
Expand All @@ -82,6 +91,9 @@
}


ALIASES = dict(it.chain(dict.fromkeys(MIN_ALIASES, MIN).items(),
dict.fromkeys(MAX_ALIASES, MAX).items()))

# =============================================================================
# EXCEPTIONS
# =============================================================================
Expand Down Expand Up @@ -122,8 +134,9 @@ def is_mtx(mtx, size=None):


def criteriarr(criteria):
"""Validate if the iterable only contains MIN (-1) and MAX (1) values. And
also always returns an ndarray representation of the iterable.
"""Validate if the iterable only contains MIN (or any alias) and MAX
(or any alias) values. And also always returns an ndarray representation
of the iterable.
Parameters
----------
Expand All @@ -145,9 +158,9 @@ def criteriarr(criteria):
"""

criteria = np.asarray(criteria)
if np.setdiff1d(criteria, [MIN, MAX]):
msg = "Criteria Array only accept '{}' or '{}' Values. Found {}"
criteria = np.array([ALIASES.get(c, c) for c in criteria])
if np.setdiff1d(criteria, [MIN, MAX]).size > 0:
msg = "Criteria Array only accept minimize or maximize Values. Found {}"
raise DataValidationError(msg.format(MAX, MIN, criteria))
return criteria

Expand Down

0 comments on commit 2620e35

Please sign in to comment.