def test_select_obs2(self):
        settings = config.Config()
        settings.OBS_MODE = "single_max"
        initializer = VDAInit(settings)
        u_c = random.rand(10, ) - 1
        u_c[3] = 1  #this will be max value

        observations, obs_idx, nobs = initializer.select_obs(settings, u_c)
        assert nobs == 1
        assert obs_idx == [3]
        assert observations == [1]
    def test_env_override(self):
        settings1 = config.Config()  #should initialize env vars
        seed1 = int(os.environ.get("SEED"))
        seed_1 = settings1.SEED

        settings1.SEED = 100
        settings1.export_env_vars()
        seed2 = int(os.environ.get("SEED"))

        assert seed1 == seed_1
        assert seed1 != seed2
    def test_vda_init_normalized(self, tmpdir):
        """End-to-end setup test"""
        X = np.zeros((3, 4))
        X[:, :2] = np.arange(6).reshape((3, 2))
        X[0, 3] = 1
        X = X.T
        #X = (m x n)
        INTERMEDIATE_FP = "inter"
        p = tmpdir.mkdir(INTERMEDIATE_FP).join("X_fp.npy")
        p.dump(X)
        p.allow_pickel = True

        settings = config.Config()
        settings.set_X_fp(str(p))
        settings.set_n(3)
        settings.FORCE_GEN_X = False
        settings.OBS_MODE = "single_max"
        settings.OBS_VARIANCE = 0.1
        settings.TDA_IDX_FROM_END = 0
        settings.HIST_FRAC = 0.5
        settings.SHUFFLE_DATA = False
        settings.NORMALIZE = True

        vda_initilizer = VDAInit(settings)
        data = vda_initilizer.run()

        X_ret = data.get("X")
        X_train = data.get("train_X")
        V = vda_initilizer.create_V_from_X(data["train_X"], settings)
        M, n = X_ret.shape
        u_c = data.get("u_c")
        u_0 = data.get("u_0")
        nobs = len(data["observations"])

        mean_exp = np.array([0.5, 2.5, 4.5])
        std_exp = np.array([0.5, 0.5, 0.5])
        X_exp = ((X - mean_exp) * 2)
        X_00_exp = -1
        print("X_exp", X_exp.shape)
        print("X_ret", X_ret.shape)
        assert X_00_exp == X_train[0, 0] and X_00_exp == X_ret[0, 0]
        assert np.array_equal(X_exp, X_ret)
        assert (3, 4) == (n, M)
        assert np.array_equal(np.array(X_exp[:2, :]), data.get("train_X"))
        assert np.array_equal(np.array(X_exp[-1, :]), u_c)
        assert np.array_equal(X_exp[:2, :], V)
        assert np.allclose(np.zeros((3)), u_0)
        assert data.get("observations") == [1.]
        assert nobs == 1
        #assert np.allclose(np.array([1, 0, 0]), data.get("G"))
        assert [1.] == data.get("d")
        assert np.array_equal(std_exp, data.get("std"))
        assert np.array_equal(mean_exp, data.get("mean"))
    def test_select_obs1(self):
        settings = config.Config()
        settings.OBS_MODE = "rand"
        settings.OBS_FRAC = 1.0 / 3.0

        initializer = VDAInit(settings)
        u_c = random.rand(10, )

        observations, obs_idx, nobs = initializer.select_obs(settings, u_c)

        for idx, obs_idx in enumerate(obs_idx):
            assert u_c[obs_idx] == observations[idx]

        assert nobs == 3, "nobs should be 3"
    def __settings(self, tmpdir, normalize, force_init=False):
        if hasattr(self, "settings") and not force_init:
            return self.settings
        else:
            X = np.zeros((3, 4))
            X[:, :2] = np.arange(6).reshape((3, 2))
            X[0, 3] = 1
            X = X.T

            INTERMEDIATE_FP = "inter"
            p = tmpdir.mkdir(INTERMEDIATE_FP).join("X_fp.npy")
            p.dump(X)
            p.allow_pickel = True

            settings = config.Config()
            settings.set_X_fp(str(p))
            print(str(p))
            print(settings.get_X_fp())
            settings.set_n(3)
            settings.FORCE_GEN_X = False
            settings.OBS_MODE = "single_max"
            settings.OBS_VARIANCE = 0.5
            settings.TDA_IDX_FROM_END = 0
            settings.HIST_FRAC = 0.5
            settings.ALPHA = 1.0
            settings.COMPRESSION_METHOD = "SVD"  #we aren't actually using SVD truncated matrix here
            settings.SAVE = False
            settings.TOL = 1e-8
            settings.NORMALIZE = normalize
            settings.SHUFFLE_DATA = False

            vda_initilizer = VDAInit(settings)
            data = vda_initilizer.run()

            data["V"] = vda_initilizer.create_V_from_X(data["train_X"],
                                                       settings).T
            data["G_V"] = (data["V"][data["obs_idx"]]).astype(float)
            self.G_V = data["G_V"]
            self.u_0 = data.get("u_0")
            self.d = data.get("d")
            nobs = len(data["observations"])
            self.R_inv = (1 / settings.OBS_VARIANCE) * np.eye(nobs)
            data["R_inv"] = self.R_inv

            self.nobs = nobs
            self.settings = settings
            self.data = data

            return settings
    def test_TSVD(self):
        "Test truncated SVD Rossella et al. mode"
        settings = config.Config()
        settings.COMPRESSION_METHOD = "SVD"
        DA = DAPipeline(settings)
        V = np.random.rand(10, 8)

        V_trunc, U_trunc, s_trunc, W_trunc = TSVD(V,
                                                  settings,
                                                  trunc_idx=5,
                                                  test=True)
        V_trunc, U_trunc, s_trunc, W_trunc = TSVD(V,
                                                  settings,
                                                  trunc_idx=None,
                                                  test=True)
    def test_create_H(self):
        settings = config.Config()
        settings.OBS_MODE = "single_max"
        initializer = VDAInit(settings)
        n = 3
        u_c = random.rand(n, ) - 1
        u_c[2] = 1  #this will be max value

        initializer = VDAInit(settings)

        observations, obs_idx, nobs = initializer.select_obs(settings, u_c)

        H = VDAInit.create_H(obs_idx, n, nobs)
        assert H @ u_c == [1]
        assert H.shape == (1, 3)
        assert np.array_equal(H, np.array([[0, 0, 1]]))
    def test_check_import(self):
        initializer = VDAInit(config.Config())

        method = initializer.run
        assert callable(method), "Should be able to import DA method"
    def test_env_set(self):
        settings = config.Config()  #should initialize env vars
        seed = os.environ.get("SEED")

        assert seed is not None