Skip to content

Commit

Permalink
generalized
Browse files Browse the repository at this point in the history
  • Loading branch information
leliel12 committed Jun 13, 2017
1 parent 711cd27 commit db1eeab
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 63 deletions.
39 changes: 30 additions & 9 deletions skcriteria/plot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@

import six

import numpy as np

from six.moves import zip

from .. import norm
from .radar import radar_plot
from .multihist import multihist_plot
Expand Down Expand Up @@ -97,21 +101,38 @@ def __call__(self, plotname="scatter_matrix", **kwargs):
def to_str(self):
return "PlotProxy for {}".format(self._data)

def preprocess(self, data, mnorm, wnorm):
def preprocess(self, data, mnorm, wnorm, weighted):
# normalization
nmtx = norm.norm(mnorm, data.mtx, criteria=data.criteria, axis=0)
nweights = (
norm.norm(wnorm, data.weights, criteria=data.criteria)
if data.weights is not None else None)
return nmtx, data.criteria, nweights

def plot(self, func, mnorm="none", wnorm="none", **kwargs):
data = self._data
nmtx, criteria, nweights = self.preprocess(data, mnorm, wnorm)
# weight the data
if weighted and nweights is not None:
wmtx = np.multiply(nmtx, nweights)
else:
wmtx = nmtx

# labels for criteria
if nweights is not None:
clabels = [
"{} (w.{:.2f})".format(cn, cw)
for cn, cw in zip(data.cnames, nweights)]
else:
clabels = ["{}".format(cn) for cn in data.cnames]

return wmtx, data.criteria, nweights, data.anames, clabels

def plot(self, func, mnorm="none", wnorm="none", weighted=True, **kwargs):
mtx, criteria, weights, anames, cnames, = self.preprocess(
self._data, mnorm, wnorm, weighted)

kwargs.update({
"mtx": nmtx, "criteria": criteria,
"weights": nweights,
"anames": kwargs.get("anames", data.anames),
"cnames": kwargs.get("cnames", data.cnames)})
"mtx": mtx, "criteria": criteria,
"weights": weights,
"anames": kwargs.pop("anames", anames),
"cnames": kwargs.pop("cnames", cnames)})
return func(**kwargs)

def radar(self, **kwargs):
Expand Down
21 changes: 3 additions & 18 deletions skcriteria/plot/multihist.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@

def multihist_plot(
mtx, criteria, weights, anames, cnames,
weighted=True, cmap=None, ax=None,
subplots_kwargs=None, hist_kwargs=None):
cmap=None, ax=None, subplots_kwargs=None, hist_kwargs=None):

# create ax if necesary
if ax is None:
Expand All @@ -66,31 +65,17 @@ def multihist_plot(
cmap = cm.get_cmap(name=cmap)
colors = cmap(np.linspace(0, 1, mtx.shape[1]))

# weight the data
if weighted and weights is not None:
wdata = np.multiply(mtx, weights)
else:
wdata = mtx

# histogram
hist_kwargs = hist_kwargs or {}
hist_kwargs.setdefault("histtype", "stepfilled")
hist_kwargs.setdefault("alpha", 0.8)

for arr, col in zip(wdata.T, colors):
for arr, col in zip(mtx.T, colors):
ax.hist(arr, color=col, **hist_kwargs)

ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)

# labels for criteria
if weights is not None:
clabels = [
"{} (w.{:.2f})".format(cn, cw)
for cn, cw in zip(cnames, weights)]
else:
clabels = ["{}".format(cn) for cn in cnames]

ax.legend(clabels, loc="best")
ax.legend(cnames, loc="best")

return ax
24 changes: 6 additions & 18 deletions skcriteria/plot/radar.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def unit_poly_verts(theta):
return verts


def radar_plot(mtx, criteria, weights, anames, cnames, weighted=True,
def radar_plot(mtx, criteria, weights, anames, cnames,
frame="polygon", cmap=None, ax=None, labelrow=5,
subplots_kwargs=None):

Expand All @@ -183,35 +183,23 @@ def radar_plot(mtx, criteria, weights, anames, cnames, weighted=True,
mincrits = np.squeeze(np.where(criteria == util.MIN))
if np.any(mincrits):
mincrits_inverted = 1.0 / mtx[:, mincrits]
pdata = mtx.astype(mincrits_inverted.dtype.type)
pdata[:, mincrits] = mincrits_inverted
pmtx = mtx.astype(mincrits_inverted.dtype.type)
pmtx[:, mincrits] = mincrits_inverted
else:
pdata = mtx

# weight the data
if weighted and weights is not None:
wdata = np.multiply(pdata, weights)
else:
wdata = pdata
pmtx = mtx

# colors
cmap = cm.get_cmap(name=cmap)
colors = cmap(np.linspace(0, 1, mtx.shape[0]))

# Plot
ax.set_rgrids([0.5])
for d, color in zip(norm.sum(wdata, axis=0), colors):
for d, color in zip(norm.sum(pmtx, axis=0), colors):
ax.plot(theta, d, color=color)
ax.fill(theta, d, facecolor=color, alpha=0.25)

# labels for criteria
if weights is not None:
clabels = [
"{}\n(w.{:.2f})".format(cn, cw)
for cn, cw in zip(cnames, weights)]
else:
clabels = ["{}".format(cn) for cn in cnames]
ax.set_varlabels(clabels)
ax.set_varlabels(cnames)

# legend
colfactor = 1
Expand Down
22 changes: 4 additions & 18 deletions skcriteria/plot/scmtx.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
import matplotlib.pyplot as plt
from matplotlib import cm

from six.moves import range, zip
from six.moves import range


# =============================================================================
Expand All @@ -61,7 +61,7 @@
def scatter_plot_matrix(
data, labels, colors, axes=None,
subplots_kwargs=None, scatter_kwargs=None, hist_kwargs=None):
""" Create a scatter plot matrix from the given data.
"""Create a scatter plot matrix from the given data.
Parameters
----------
Expand Down Expand Up @@ -153,25 +153,11 @@ def scatter_plot_matrix(
return axes


def scmtx_plot(mtx, criteria, weights, anames, cnames, weighted=True,
def scmtx_plot(mtx, criteria, weights, anames, cnames,
frame="polygon", cmap=None, ax=None, **kwargs):

cmap = cm.get_cmap(name=cmap)
colors = cmap(np.linspace(0, 1, mtx.shape[1] ** 2))

# weight the data
if weighted and weights is not None:
wdata = np.multiply(mtx, weights)
else:
wdata = mtx

# labels for criteria
if weights is not None:
clabels = [
"{} (w.{:.2f})".format(cn, cw)
for cn, cw in zip(cnames, weights)]
else:
clabels = ["{}".format(cn) for cn in cnames]

return scatter_plot_matrix(
wdata.T, labels=clabels, colors=colors, axes=ax, **kwargs)
mtx.T, labels=cnames, colors=colors, axes=ax, **kwargs)

0 comments on commit db1eeab

Please sign in to comment.