<reponame>sigeisler/being-robust-can-be-practical
import os

import numpy as np

from being_robust import BeingRobust


class TestBeingRobust():

    def test_result_is_close_to_original_matlab_code(self):
        X = np.load(os.path.join('tests', 'from_matlab.npy'), allow_pickle=True)
        br = BeingRobust(random_state=42).fit(X)

        target_mean = np.array([
            1.0457074118572391, 0.98780335256270324, 1.0001468648641678, 0.99874392565066461, 0.996063607892838,
            0.99611303767865322, 0.99669909900359832, 0.99552521728971755, 0.99431887247037076, 1.0029932982718508,
            0.99273998845194178, 1.0010746196674074, 0.99843031940666316, 0.993388023879779, 0.99474388333336949,
            0.99891606162969937, 0.9964355225210425, 0.99519652954010429, 0.99177178248522979, 0.99073935799563606,
            0.9957639887343297, 0.99895701614384058, 0.99148535317477926, 0.99581181782362738, 0.99373664245548676,
            0.98499197587826515, 0.99295653850201071, 0.99560700632697241, 1.0031010187841145, 0.99700926454319194,
            0.99299587162685388, 0.98973074130102279, 0.99245665352026724, 1.0017793515357285, 0.99771724995440081,
            0.996741112076676, 0.99761860948014092, 0.99588153065466689, 0.99422771384823827, 0.99435476012000878,
            0.9952497221245975, 1.0017278406954262, 0.99463057137569177, 0.99406091556972875, 0.99572352380814322,
            0.99884251784356826, 0.9931564237490208, 0.99795172086994777, 0.99381121017370244, 0.9990214092421873,
            0.99128652219584434, 1.0009155492466641, 0.99336082321253671, 1.0016651617589505, 0.99260931329781965,
            0.99429079534023446, 0.99387255857881041, 0.99497149575226518, 0.99557248676784738, 0.99880184432620756,
            0.99290845797173921, 0.99544694425686975, 0.99515315643577718, 0.99526307124120317, 1.0044209113038378,
            1.0048649262110079, 1.0009942584537124, 0.9939557219724342, 0.99205752809837255, 0.9954657980889442,
            0.99741336885002707, 0.99434052656578709, 0.99473629303099154, 0.99809604436708554, 0.99106243976704278,
            1.0007483099794494, 0.99420875508668693, 0.99070589665930686, 0.99571597093842146, 0.9937274736684828,
            1.001249632420093, 0.998280096582369, 0.99568643168584592, 0.99690236517109765, 0.9884318538553466,
            0.99118740608337919, 0.99759845234721456, 0.99783282161984588, 0.99764708043605232, 1.000007318562766,
            0.99368136055880962, 0.99031473069914611, 0.9969143832459777, 0.99483731585414859, 0.99401268356812666,
            0.99284407063322289, 0.99057669665874726, 0.99355055800747272, 0.99552832680953263, 0.9935616533543008
        ])

        assert np.allclose(br.location_, target_mean, rtol=1e-3, atol=1e-5)
