def test_atlas():
    n_voxels = 500
    n_timeframes = 100
    n_subjects = 4
    atlas = np.repeat(np.arange(10), 50)

    X_train = [
        np.random.rand(n_voxels, n_timeframes) for _ in range(n_subjects)
    ]
    srm = IdentifiableFastSRM(identifiability=None,
                              atlas=atlas,
                              n_components=5,
                              n_iter=1)
    S = srm.fit_transform(X_train)
    X_pred = srm.inverse_transform(S)
def test_class_srm_inverse_transform(
    input_format,
    low_ram,
    tempdir,
    atlas,
    n_jobs,
    n_timeframes,
    aggregate,
    identifiability,
):

    with tempfile.TemporaryDirectory() as datadir:
        X, W, S = generate_ica_friendly_data(n_voxels, n_timeframes,
                                             n_subjects, datadir, 0,
                                             input_format)

        if tempdir:
            temp_dir = datadir
        else:
            temp_dir = None

        srm = IdentifiableFastSRM(
            identifiability=identifiability,
            n_subjects_ica=n_subjects,
            atlas=atlas,
            n_components=n_components,
            n_iter=10,
            temp_dir=temp_dir,
            low_ram=low_ram,
            n_jobs=n_jobs,
            aggregate=aggregate,
        )

        srm.fit(X)
        shared_response_raw = srm.transform(X)
        # Check inverse transform
        if input_format == "list_of_array":
            reconstructed_data = srm.inverse_transform(shared_response_raw,
                                                       subjects_indexes=[0, 2])
            for i, ii in enumerate([0, 2]):
                assert_array_almost_equal(reconstructed_data[i], X[ii])

            reconstructed_data = srm.inverse_transform(shared_response_raw,
                                                       subjects_indexes=None)
            for i in range(len(X)):
                assert_array_almost_equal(reconstructed_data[i], X[i])
        else:
            reconstructed_data = srm.inverse_transform(
                shared_response_raw,
                sessions_indexes=[1],
                subjects_indexes=[0, 2],
            )
            for i, ii in enumerate([0, 2]):
                for j, jj in enumerate([1]):
                    assert_array_almost_equal(reconstructed_data[i][j],
                                              safe_load(X[ii][jj]))

            reconstructed_data = srm.inverse_transform(
                shared_response_raw,
                subjects_indexes=None,
                sessions_indexes=None,
            )

            for i in range(len(X)):
                for j in range(len(X[i])):
                    assert_array_almost_equal(reconstructed_data[i][j],
                                              safe_load(X[i][j]))
def test_memory():
    n_voxels = 500
    n_timeframes = [100, 101]
    n_subjects = 4

    with tempfile.TemporaryDirectory() as datadir:
        X, W, S = generate_ica_friendly_data(n_voxels, n_timeframes,
                                             n_subjects, datadir, 0)

        dts = []
        for (low_ram, tempdir, n_jobs, aggregate, identifiability) in [
            (True, True, 1, "mean", "decorr"),
            (False, False, 2, None, "ica"),
            (True, True, 1, "mean", None),
            (False, False, 1, None, None),
        ]:
            if tempdir:
                temp_dir = datadir
            else:
                temp_dir = None

            srm = IdentifiableFastSRM(
                identifiability=identifiability,
                n_subjects_ica=n_subjects,
                atlas=np.arange(n_voxels),
                n_components=n_components,
                n_iter=100,
                n_iter_reduced=100,
                temp_dir=temp_dir,
                low_ram=low_ram,
                tol=0,
                n_jobs=n_jobs,
                aggregate=aggregate,
                memory=datadir + "/memory",
            )
            t0 = time()
            srm.fit(X)
            t1 = time()

            dts.append(t1 - t0)

            shared_response_raw = srm.transform(X)

            # Check inverse transform
            reconstructed_data = srm.inverse_transform(
                shared_response_raw,
                sessions_indexes=[1],
                subjects_indexes=[0, 2],
            )
            for i, ii in enumerate([0, 2]):
                for j, jj in enumerate([1]):
                    assert_array_almost_equal(reconstructed_data[i][j],
                                              safe_load(X[ii][jj]))

            reconstructed_data = srm.inverse_transform(
                shared_response_raw,
                subjects_indexes=None,
                sessions_indexes=None,
            )

            for i in range(len(X)):
                for j in range(len(X[i])):
                    assert_array_almost_equal(reconstructed_data[i][j],
                                              safe_load(X[i][j]))

            srm.clean()

    for i in range(len(dts) - 1):
        assert dts[0] > dts[i + 1]
def test_fastsrm_class(identifiability):
    n_jobs = 1
    with tempfile.TemporaryDirectory() as datadir:
        np.random.seed(0)

        np.random.seed(0)
        paths, W, S = generate_ica_friendly_data(n_voxels, n_timeframes,
                                                 n_subjects, datadir, 0)

        atlas = np.arange(1, n_voxels + 1)
        srm = IdentifiableFastSRM(
            identifiability=identifiability,
            n_subjects_ica=n_subjects,
            atlas=atlas,
            n_components=n_components,
            n_iter=10,
            temp_dir=datadir,
            low_ram=True,
            n_jobs=n_jobs,
        )

        # Raises an error because model is not fitted yet
        with pytest.raises(NotFittedError):
            srm.transform(paths)

        srm.fit(paths)

        # An error can occur if temporary directory already exists
        with pytest.raises(
                ValueError,
                match=("Path %s already exists. When a model "
                       "is used, filesystem should be "
                       r"cleaned by using the .clean\(\) "
                       "method" % srm.temp_dir),
        ):
            # Error can occur if the filesystem is uncleaned
            create_temp_dir(srm.temp_dir)
            create_temp_dir(srm.temp_dir)

        shared_response = srm.transform(paths)

        # Raise error when wrong index
        with pytest.raises(
                ValueError,
                match=("subjects_indexes should be either "
                       "a list, an array or None but "
                       "received type <class 'int'>"),
        ):
            srm.transform(paths, subjects_indexes=1000)

        with pytest.raises(
                ValueError,
                match=("subjects_indexes should be either "
                       "a list, an array or None but "
                       "received type <class 'int'>"),
        ):
            srm.inverse_transform(shared_response, subjects_indexes=1000)

        with pytest.raises(
                ValueError,
                match=("sessions_indexes should be either "
                       "a list, an array or None but "
                       "received type <class 'int'>"),
        ):
            srm.inverse_transform(shared_response, sessions_indexes=1000)

        with pytest.raises(
                ValueError,
                match=("Input data imgs has len 5 whereas "
                       "subject_indexes has len 1. "
                       "The number of basis used to compute "
                       "the shared response should be equal to "
                       "the number of subjects in imgs"),
        ):
            srm.transform(paths, subjects_indexes=[0])

        with pytest.raises(
                ValueError,
                match=("Index 1 of subjects_indexes has value 8 "
                       "whereas value should be between 0 and 4"),
        ):
            srm.transform(paths[:2], subjects_indexes=[0, 8])

        with pytest.raises(
                ValueError,
                match=("Index 1 of sessions_indexes has value 8 "
                       "whereas value should be between 0 and 1"),
        ):
            srm.inverse_transform(shared_response, sessions_indexes=[0, 8])

        # Check behavior of .clean
        assert os.path.exists(srm.temp_dir_)
        srm.clean()
        assert not os.path.exists(srm.temp_dir_)