import numpy as np
from rlscore.learner.cg_rankrls import CGRankRLS
from rlscore.learner.cg_rankrls import EarlyStopCB
from rlscore.utilities.reader import read_sparse
from rlscore.measure import cindex
from rlscore.utilities.reader import read_qids
train_labels = np.loadtxt("./legacy_tests/data/rank_train.labels")
test_labels = np.loadtxt("./legacy_tests/data/rank_test.labels")
train_features = read_sparse("./legacy_tests/data/rank_train.features")
test_features = read_sparse("./legacy_tests/data/rank_test.features")
train_qids = read_qids("./legacy_tests/data/rank_train.qids")
test_qids = read_qids("./legacy_tests/data/rank_test.qids")
kwargs = {}
kwargs["Y"] = train_labels
kwargs["X"] = train_features
kwargs["qids"] = train_qids
kwargs["regparam"] = 1
kwargs["callbackfun"] = EarlyStopCB(test_features, test_labels, test_qids, measure=cindex)
learner = CGRankRLS(**kwargs)
P = learner.predict(test_features)
test_perf = cindex(test_labels, P)
from rlscore.measure.measure_utilities import UndefinedPerformance
from rlscore.measure.measure_utilities import qids_to_splits
test_qids = qids_to_splits(test_qids)
perfs = []
for query in test_qids:
    try:
        perf = cindex(test_labels[query], P[query])
        perfs.append(perf)
    except UndefinedPerformance:
        pass
test_perf = np.mean(perfs)
print("test set performance: %f" %test_perf)