import pandas as pd

import numpy as np

import skcriteria as sc
from skcriteria.madm import topsis, wsum, moora, wprod, electre

df = pd.read_csv("sites.csv")[:-3]
df

anames = df.columns[2:].values

def to_apply(r):
    new = []
    for e in r:
        if isinstance(e , str):
            e = float(e.replace("*", ""))
        new.append(e)
    return new

mtx = df[anames][3:].dropna()[anames].apply(to_apply).T.values
criteria = [sc.MIN if c == "min" else sc.MAX for c in df.Criteria[3:][~df.Armazones.isnull()].values]

cnames = df["Criteria/Alternatives"][3:].apply(lambda r: r.strip())[~df[3:].Armazones.isnull()].values
cnames = map(lambda s: s.decode("utf8"), cnames)

data = sc.Data(mtx, criteria, anames=anames, cnames=cnames)

"""
Explanation: European Extremely Large Telescope site selection
a comparison between real selection and multicriteria-decision-analysis suggestions
Juan B Cabral – Bruno O Sanchez  – Manuel Starck Cuffini
Instituto de Astronomía Teórica y Experimental
jbcabral@oac.unc.edu.ar- bruno@oac.unc.edu.ar- mstarck@oac.unc.edu.ar
Data Mangling
End of explanation
"""


data

"""
Explanation: Final Data
End of explanation
"""


dm = topsis.TOPSIS()
topsis_dec = dm.decide(data)
topsis_dec

"""
Explanation: TOPSIS
https://en.wikipedia.org/wiki/TOPSIS
End of explanation
"""


dm = wsum.MDWeightedSum()
wsum_dec = dm.decide(data)
wsum_dec

"""
Explanation: Weighted Sum Model
https://en.wikipedia.org/wiki/Weighted_sum_model
End of explanation
"""


dm = wprod.WeightedProduct()
wprod_dec = dm.decide(data)
wprod_dec

"""
Explanation: Weighted Product
https://en.wikipedia.org/wiki/Weighted_product_model
End of explanation
"""


dm = electre.ELECTRE1()
electre_dec = dm.decide(data)
electre_dec

"""
Explanation: ELECTRE 1
https://en.wikipedia.org/wiki/ELECTRE
End of explanation
"""


methods = ["TOPSIS", "WSUM", "WPROD"]
kernel = np.array([1 if idx  in electre_dec.kernel_ else 0 for idx, _ in enumerate(anames)])
ranks = np.vstack((topsis_dec.rank_, wsum_dec.rank_, wprod_dec.rank_, kernel)).T
rdf = pd.DataFrame(ranks, index=anames, columns=methods + ["ELECTRE 1 Kernel"])
rdf

rdf[methods].T.describe()

"""
Explanation: Results
End of explanation
"""


