Ejemplo n.º 1
0
def test_neuroharmony_behaviour(resources):
    """Test Neuroharmony."""
    x_train, x_test = resources.X_train_split, resources.X_test_split
    neuroharmony = Neuroharmony(
        resources.features,
        resources.regression_features,
        resources.covariates,
        resources.eliminate_variance,
        param_distributions=dict(
            RandomForestRegressor__n_estimators=[5, 10, 15, 20],
            RandomForestRegressor__random_state=[42, 78],
            RandomForestRegressor__warm_start=[False, True],
        ),
        estimator_args=dict(n_jobs=1, random_state=42),
        randomized_search_args=dict(cv=5, n_jobs=27),
    )
    x_train_harmonized = neuroharmony.fit_transform(x_train)
    x_test_harmonized = neuroharmony.predict(x_test)
    data_harmonized = concat([x_train_harmonized, x_test_harmonized],
                             sort=False)
    KS_original = ks_test_grid(resources.original_data, resources.features,
                               "scanner")
    KS_harmonized = ks_test_grid(data_harmonized, resources.features,
                                 "scanner")
    assert KS_original[resources.features[0]].shape == (resources.n_scanners,
                                                        resources.n_scanners)
    assert KS_harmonized[resources.features[0]].shape == (resources.n_scanners,
                                                          resources.n_scanners)
    assert isinstance(x_test, NDFrame)
    assert isinstance(neuroharmony, BaseEstimator)
    assert not neuroharmony.prediction_is_covered_.all(
    ), "No subjects out of the range."
Ejemplo n.º 2
0
def model(resources):
    """Define mock model.

    Parameters
    ----------
    resources : namedtuple
        Basic information needed to build a neuroharmony model.

    Returns
    -------
    Neuroharmony class
        Neuroharmony model.
    """
    neuroharmony = Neuroharmony(
        resources.features,
        resources.regression_features,
        resources.covariates,
        resources.eliminate_variance,
        param_distributions=dict(
            RandomForestRegressor__n_estimators=[5, 10, 15, 20],
            RandomForestRegressor__random_state=[42, 78],
            RandomForestRegressor__warm_start=[False, True],
        ),
        estimator_args=dict(n_jobs=1, random_state=42),
        randomized_search_args=dict(cv=5, n_jobs=27),
    )
    return neuroharmony
Ejemplo n.º 3
0
def test_model_strategy(resources, model_strategy):
    """Test the model_strategy is implemented."""
    x_train, x_test = resources.X_train_split, resources.X_test_split
    neuroharmony = Neuroharmony(
        resources.features,
        resources.regression_features,
        resources.covariates,
        resources.eliminate_variance,
        param_distributions=dict(
            RandomForestRegressor__n_estimators=[5, 10, 15, 20],
            RandomForestRegressor__random_state=[42, 78],
            RandomForestRegressor__warm_start=[False, True],
        ),
        model_strategy=model_strategy,
        estimator_args=dict(n_jobs=1, random_state=42),
        randomized_search_args=dict(cv=5, n_jobs=27),
    )
    x_train_harmonized = neuroharmony.fit_transform(x_train)
    x_test_harmonized = neuroharmony.predict(x_test)
    assert isinstance(x_train_harmonized, NDFrame)
    assert isinstance(x_test_harmonized, NDFrame)
Ejemplo n.º 4
0
def model(resources):
    neuroharmony = Neuroharmony(
        resources.features,
        resources.regression_features,
        resources.covariates,
        resources.eliminate_variance,
        param_distributions=dict(
            RandomForestRegressor__n_estimators=[5, 10, 15, 20],
            RandomForestRegressor__random_state=[42, 78],
            RandomForestRegressor__warm_start=[False, True],
        ),
        estimator_args=dict(n_jobs=1, random_state=42),
        randomized_search_args=dict(cv=5, n_jobs=27),
    )
    return neuroharmony
Ejemplo n.º 5
0
def test_model_invalid_strategy(resources):
    """Test the model_strategy is implemented."""
    with pytest.raises(ValueError):
        Neuroharmony(
            resources.features,
            resources.regression_features,
            resources.covariates,
            resources.eliminate_variance,
            param_distributions=dict(
                RandomForestRegressor__n_estimators=[5, 10, 15, 20],
                RandomForestRegressor__random_state=[42, 78],
                RandomForestRegressor__warm_start=[False, True],
            ),
            model_strategy=None,
            estimator_args=dict(n_jobs=1, random_state=42),
            randomized_search_args=dict(cv=5, n_jobs=27),
        )