import alpenglow as prs
import alpenglow.Getter as rs
import alpenglow.experiments
import pandas as pd
import math
import pytest
import sys
from alpenglow.evaluation import DcgScore
import alpenglow.cpp
compiler = alpenglow.cpp.__compiler
stdlib = alpenglow.cpp.__stdlib


class TestBatchAndOnlineFactorExperiment:
    def test_batchAndOnlineFactorExperiment(self):
        boModelExperiment = alpenglow.experiments.BatchAndOnlineFactorExperiment(
            top_k=100,
            seed=254938879,
            dimension=10,
            period_length=1000,
            batch_learning_rate=0.07,
            batch_negative_rate=20,
            online_learning_rate=0.15,
            online_negative_rate=120,
            number_of_iterations=3,
            clear_model=True,
        )
        boRankings = boModelExperiment.run("python/test_alpenglow/test_data_4", experimentType="online_id", verbose=True, exclude_known=True)
        desired_ranks = [101.0, 101.0, 101.0, 101.0, 101.0, 101.0, 101.0, 101.0, 101.0, 101.0, 101.0, 101.0, 101.0, 101.0, 101.0, 101.0, 101.0, 7.0, 101.0, 101.0, 101.0, 101.0, 101.0, 101.0, 101.0, 101.0, 101.0, 101.0, 4.0, 101.0, 101.0, 3.0, 101.0, 101.0, 101.0, 101.0, 101.0, 101.0, 101.0, 1.0, 101.0, 101.0, 101.0, 101.0, 101.0, 101.0, 101.0, 101.0, 1.0, 101.0, 101.0, 101.0, 17.0, 16.0, 21.0, 101.0, 101.0, 26.0, 101.0, 1.0, 101.0, 101.0, 101.0, 101.0, 101.0, 101.0, 14.0, 101.0, 19.0, 43.0, 46.0, 101.0, 101.0, 101.0, 101.0, 101.0, 101.0, 3.0, 101.0, 7.0, 101.0, 5.0, 101.0, 101.0, 101.0, 33.0, 101.0, 101.0, 37.0, 101.0, 10.0, 42.0, 101.0, 101.0, 101.0, 101.0, 101.0, 101.0, 9.0, 101.0, 6.0, 36.0, 101.0, 35.0, 56.0, 30.0, 63.0, 101.0, 101.0, 41.0, 101.0, 101.0, 101.0, 18.0, 3.0, 49.0, 50.0, 101.0, 60.0, 41.0, 101.0, 101.0, 15.0, 50.0, 101.0, 101.0, 101.0, 66.0, 4.0, 39.0, 101.0, 56.0, 61.0, 28.0, 42.0, 4.0, 7.0, 2.0, 101.0, 38.0, 101.0, 101.0, 19.0, 38.0, 67.0, 101.0, 3.0, 3.0, 38.0, 28.0, 48.0, 21.0, 101.0, 70.0, 101.0, 101.0, 13.0, 101.0, 15.0, 30.0, 101.0, 32.0, 33.0, 35.0, 32.0, 73.0, 101.0, 75.0, 101.0, 101.0, 5.0, 101.0, 101.0, 101.0, 101.0, 50.0, 101.0, 13.0, 45.0, 101.0, 101.0, 101.0, 101.0, 64.0, 72.0, 56.0, 101.0, 101.0, 74.0, 101.0, 85.0, 101.0, 59.0, 55.0, 95.0, 1.0, 101.0, 101.0, 68.0, 76.0, 101.0, 4.0, 57.0, 36.0, 14.0, 40.0, 1.0, 101.0, 101.0, 101.0, 101.0, 65.0, 101.0, 15.0, 101.0, 11.0, 101.0, 50.0, 53.0, 101.0, 101.0, 1.0, 1.0, 47.0, 12.0, 101.0, 101.0, 73.0, 14.0, 40.0, 13.0, 101.0, 100.0, 101.0, 76.0, 74.0, 65.0, 70.0, 88.0, 45.0, 91.0, 101.0, 101.0, 101.0, 101.0, 101.0, 1.0, 29.0, 101.0, 55.0, 10.0, 37.0, 53.0, 75.0, 101.0, 14.0, 19.0, 6.0, 83.0, 101.0, 101.0, 86.0, 101.0, 4.0, 17.0, 100.0, 101.0, 16.0, 82.0, 71.0, 69.0, 22.0, 8.0, 101.0, 101.0, 69.0, 101.0, 101.0, 101.0, 101.0, 76.0, 52.0, 8.0, 101.0, 101.0, 101.0, 101.0, 101.0, 92.0, 101.0, 6.0, 81.0, 42.0, 21.0, 2.0, 35.0, 101.0, 101.0, 101.0, 15.0, 101.0, 101.0, 11.0, 101.0, 10.0, 40.0, 101.0, 101.0, 101.0, 101.0, 52.0, 71.0, 77.0, 101.0, 7.0, 46.0, 10.0, 49.0, 101.0, 101.0, 101.0, 101.0, 68.0, 101.0, 24.0, 101.0, 101.0, 101.0, 74.0, 101.0, 53.0, 4.0, 91.0, 101.0, 57.0, 101.0, 35.0, 12.0, 1.0, 101.0, 11.0, 6.0, 101.0, 101.0, 101.0, 79.0, 45.0, 97.0, 2.0, 86.0, 101.0, 6.0, 92.0, 101.0, 1.0, 101.0, 11.0, 9.0, 60.0, 10.0, 89.0, 92.0, 53.0, 101.0, 1.0, 101.0, 101.0, 101.0, 9.0, 42.0, 74.0, 74.0, 101.0, 101.0, 101.0, 1.0, 28.0, 101.0, 28.0, 80.0, 101.0, 14.0, 14.0, 101.0, 8.0, 12.0, 7.0, 10.0, 101.0, 101.0, 15.0, 1.0, 101.0, 59.0, 17.0, 45.0, 14.0, 96.0, 101.0, 101.0, 101.0, 22.0, 12.0, 101.0, 63.0, 101.0, 79.0, 101.0, 101.0, 48.0, 13.0, 101.0, 101.0, 3.0, 3.0, 101.0, 15.0, 101.0, 101.0, 101.0, 101.0, 101.0, 6.0, 101.0, 72.0, 101.0, 18.0, 101.0, 89.0, 59.0, 72.0, 101.0, 101.0, 101.0, 1.0, 101.0, 3.0, 101.0, 64.0, 101.0, 101.0, 11.0, 101.0, 19.0, 10.0, 12.0, 101.0, 9.0, 10.0, 97.0, 101.0, 4.0, 1.0, 16.0, 6.0, 16.0, 101.0, 80.0, 66.0, 58.0, 101.0, 101.0, 93.0, 67.0, 101.0, 4.0, 27.0, 90.0, 2.0, 62.0, 100.0, 101.0, 13.0, 37.0, 19.0, 7.0, 12.0, 101.0, 1.0, 101.0, 12.0, 101.0, 60.0, 101.0, 101.0, 58.0, 101.0, 101.0, 101.0, 84.0, 101.0, 12.0, 51.0, 101.0, 101.0, 9.0, 101.0, 39.0, 4.0, 36.0, 6.0, 101.0, 101.0, 3.0, 45.0, 101.0, 6.0, 24.0, 2.0, 70.0, 6.0, 14.0, 72.0, 101.0, 101.0, 7.0, 101.0, 27.0, 101.0, 39.0, 86.0, 50.0, 101.0, 2.0, 31.0, 70.0, 101.0, 88.0, 10.0, 2.0, 101.0, 6.0, 101.0, 101.0, 1.0, 101.0, 72.0, 3.0, 101.0, 6.0, 101.0, 101.0, 11.0, 15.0, 101.0, 13.0, 3.0, 61.0, 85.0, 101.0, 101.0, 72.0, 101.0, 3.0, 1.0, 101.0, 26.0, 5.0, 23.0, 33.0, 29.0, 7.0, 101.0, 23.0, 101.0, 52.0, 100.0, 60.0, 92.0, 101.0, 22.0, 101.0, 14.0, 101.0, 36.0, 101.0, 3.0, 3.0, 75.0, 50.0, 101.0, 99.0, 101.0, 69.0, 101.0, 101.0, 101.0, 101.0, 26.0, 45.0, 101.0, 101.0, 16.0, 77.0, 101.0, 101.0, 77.0, 21.0, 101.0, 4.0, 101.0, 11.0, 12.0, 8.0, 4.0, 4.0, 11.0, 15.0, 101.0, 1.0, 101.0, 101.0, 44.0, 18.0, 10.0, 82.0, 32.0, 101.0, 101.0, 101.0, 10.0, 101.0, 101.0, 101.0, 13.0, 60.0, 71.0, 101.0, 22.0, 53.0, 8.0, 1.0, 31.0, 79.0, 11.0, 101.0, 76.0, 101.0, 19.0, 76.0, 1.0, 101.0, 101.0, 6.0, 101.0, 101.0, 12.0, 101.0, 2.0, 23.0, 101.0, 101.0, 101.0, 5.0, 101.0, 101.0, 101.0, 101.0, 73.0, 10.0, 101.0, 4.0, 101.0, 20.0, 17.0, 59.0, 8.0, 4.0, 2.0, 6.0, 6.0, 5.0, 30.0, 101.0, 19.0, 5.0, 13.0, 101.0, 51.0, 77.0, 8.0, 57.0, 72.0, 9.0, 4.0, 101.0, 46.0, 13.0, 101.0, 47.0, 101.0, 83.0, 16.0, 69.0, 101.0, 101.0, 34.0, 61.0, 37.0, 4.0, 101.0, 78.0, 101.0, 101.0, 17.0, 101.0, 101.0, 4.0, 22.0, 101.0, 13.0, 101.0, 41.0, 101.0, 20.0, 7.0, 3.0, 17.0, 2.0, 12.0, 101.0, 101.0, 68.0, 47.0, 101.0, 101.0, 101.0, 1.0, 17.0, 2.0, 101.0, 18.0, 43.0, 58.0, 12.0, 20.0, 31.0, 21.0, 55.0, 10.0, 53.0, 101.0, 66.0, 42.0, 101.0, 9.0, 10.0, 101.0, 101.0, 48.0, 38.0, 101.0, 3.0, 7.0, 101.0, 2.0, 1.0, 101.0, 101.0, 1.0, 101.0, 2.0, 101.0, 101.0, 9.0, 101.0, 46.0, 3.0, 101.0, 17.0, 4.0, 101.0, 101.0, 12.0, 101.0, 15.0, 14.0, 94.0, 101.0, 101.0, 3.0, 101.0, 6.0, 8.0, 101.0, 101.0, 81.0, 44.0, 101.0, 101.0, 18.0, 3.0, 94.0, 56.0, 18.0, 19.0, 5.0, 5.0, 101.0, 35.0, 101.0, 70.0, 14.0, 19.0, 5.0, 10.0, 17.0, 17.0, 2.0, 101.0, 65.0, 10.0, 101.0, 2.0, 53.0, 63.0, 101.0, 1.0, 98.0, 48.0, 3.0, 21.0, 1.0, 101.0, 101.0, 82.0, 101.0, 101.0, 10.0, 9.0, 3.0, 101.0, 16.0, 48.0, 101.0, 6.0, 101.0, 2.0, 101.0, 64.0, 101.0, 42.0, 101.0, 101.0, 101.0, 3.0, 14.0, 101.0, 9.0, 101.0, 101.0, 45.0, 101.0, 22.0, 29.0, 4.0, 58.0, 3.0, 11.0, 21.0, 101.0, 101.0, 23.0, 101.0, 101.0, 10.0, 17.0, 81.0, 51.0, 41.0, 101.0, 2.0, 18.0, 83.0, 101.0, 101.0, 14.0, 8.0, 14.0, 35.0, 24.0, 9.0, 14.0, 101.0, 4.0, 9.0, 101.0, 49.0, 4.0, 14.0, 3.0, 101.0, 31.0, 12.0, 14.0, 101.0, 16.0, 12.0, 6.0, 4.0, 101.0, 56.0, 101.0, 35.0, 45.0, 101.0, 101.0, 74.0, 101.0, 101.0, 58.0, 6.0, 10.0, 61.0, 46.0, 101.0, 14.0, 101.0, 101.0, 101.0, 101.0, 101.0, 101.0, 44.0, 4.0, 101.0, 101.0, 101.0, 101.0, 21.0, 101.0, 12.0, 7.0, 30.0, 45.0, 93.0, 101.0, 21.0, 101.0, 101.0, 101.0, 3.0, 101.0, 7.0, 101.0, 19.0, 12.0, 7.0, 8.0, 12.0, 55.0, 2.0, 101.0, 101.0, 3.0, 54.0, 10.0, 26.0, 101.0, 13.0, 51.0, 101.0, 11.0, 14.0, 95.0, 2.0, 101.0, 101.0, 5.0, 9.0, 55.0, 101.0, 50.0, 101.0, 13.0, 4.0, 69.0, 4.0, 17.0, 85.0, 63.0, 101.0, 1.0, 101.0, 10.0, 101.0, 29.0, 1.0, 101.0, 1.0, 6.0, 101.0, 101.0, 3.0, 40.0, 10.0, 45.0, 101.0, 100.0, 101.0]

        if(compiler == "gcc" and stdlib == "libstdc++"):
            assert list(boRankings["rank"].fillna(101)) == desired_ranks

        assert DcgScore(boRankings).mean() == pytest.approx(0.1712397251170552, abs=5*1e-3)
