Skip to content

Commit

Permalink
fix tutorial and end core api
Browse files Browse the repository at this point in the history
  • Loading branch information
leliel12 committed Nov 10, 2017
1 parent 1e5a4a2 commit da2b323
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 10 deletions.
5 changes: 3 additions & 2 deletions doc/source/api/core.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Core Functionalities
--------------------
``skcriteria.core`` module
==========================

.. automodule:: skcriteria.core
:members:
:undoc-members:
:show-inheritance:
2 changes: 0 additions & 2 deletions doc/source/core.rst

This file was deleted.

2 changes: 1 addition & 1 deletion doc/source/tutorial/quickstart.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"# 2 alternatives by 3 criteria\n",
"mtx = [\n",
" [1, 2, 3], # alternative 1\n",
" [4, 5, 6], # alternative 1\n",
" [4, 5, 6], # alternative 2\n",
"]\n",
"mtx"
]
Expand Down
83 changes: 78 additions & 5 deletions skcriteria/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

__all__ = [
'MIN', 'MAX',
'DataValidationError',
'criteriarr',
'validate_data',
'Data']
Expand All @@ -74,8 +75,10 @@
# =============================================================================

MIN = -1
"""Int: Minimization criteria"""

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

CRITERIA_STR = {
MIN: "min",
Expand Down Expand Up @@ -106,7 +109,7 @@ class DataValidationError(ValueError):
# =============================================================================

def iter_equal(a, b):
"""Validate if two iterables are equals independently of their type"""
"""Validate if two iterables are equals independently of their type."""
if isinstance(a, np.ndarray) or isinstance(b, np.ndarray):
return np.allclose(a, b, equal_nan=True)
return a == b
Expand All @@ -129,7 +132,29 @@ def is_mtx(mtx, size=None):


def criteriarr(criteria):
"""Validate if the iterable only contains MIN (-1) and MAX (1) criteria"""
"""Validate if the iterable only contains MIN (-1) and MAX (1) values. And
also always returns an ndarray representation of the iterable. If
Parameters
----------
criteria : Array-like
Iterable containing all the values to be validated by the function.
Returns
-------
numpy.ndarray :
Criteria array.
Raises
------
DataValidationError :
if some value of the criteria array are not MIN (-1) or MAX (1)
"""

criteria = np.asarray(criteria)
if np.setdiff1d(criteria, [MIN, MAX]):
Expand All @@ -150,12 +175,37 @@ def validate_data(mtx, criteria, weights=None):
- The weight array must be None or an iterable with the same length
of the criteria.
Parameters
----------
mtx : 2D array-like
2D alternative matrix, where every column (axis 0) are a criteria, and
every row (axis 1) is an alternative.
criteria : Array-like
The sense of optimality of every criteria. Must has only
MIN (-1) and MAX (1) values. Must has the same elements as columns
has ``mtx``
weights : array like or None
The importance of every criteria. Must has the same elements as columns
has ``mtx`` or None.
Returns
-------
- A mtx as 2d numpy.ndarray.
- A criteria as numpy.ndarray.
- A weights as numpy.ndarray or None (if weights is None).
mtx : numpy.ndarray
mtx representations as 2d numpy.ndarray.
criteria : numpy.ndarray
A criteria as numpy.ndarray.
weights : numpy.ndarray or None
A weights as numpy.ndarray or None (if weights is None).
Raises
------
DataValidationError :
If the data are incompatible.
"""
mtx = np.asarray(mtx)
Expand Down Expand Up @@ -261,6 +311,23 @@ def _repr_html_(self):
return self.to_str(tablefmt="html")

def to_str(self, **params):
"""String representation of the Data object.
Parameters
----------
kwargs :
Parameters to configure
`tabulate <https://bitbucket.org/astanin/python-tabulate>`_
Return
------
str :
String representation of the Data object.
"""

params.update({
k: v for k, v in TABULATE_PARAMS.items() if k not in params})
rows = self._iter_rows()
Expand All @@ -272,26 +339,32 @@ def raw(self):

@property
def anames(self):
"""Names of the alternatives as tuple of string."""
return tuple(self._anames)

@property
def cnames(self):
"""Names of the criteria as tuple of string."""
return tuple(self._cnames)

@property
def mtx(self):
"""Alternative matrix as 2d numpy.ndarray."""
return self._mtx.copy()

@property
def criteria(self):
"""Sense of optimality of every criteria"""
return self._criteria.copy()

@property
def weights(self):
"""Relative importance of the criteria or None if all the same"""
return None if self._weights is None else self._weights.copy()

@property
def plot(self):
"""Data plotting accessor and method"""
return self._plot


Expand Down

0 comments on commit da2b323

Please sign in to comment.