diff --git a/experiments/synthetic/clustering.py b/experiments/synthetic/clustering.py index a08ef4d..91a3343 100644 --- a/experiments/synthetic/clustering.py +++ b/experiments/synthetic/clustering.py @@ -31,17 +31,15 @@ warnings.filterwarnings("ignore") -if __name__ == "__main__": - arguments = docopt(__doc__) - - SEED = int(arguments['--seed']) +def run(args: dict) -> None: + """Main runner.""" + SEED = int(args['--seed']) set_seed(SEED) - x, y = load(str(arguments[''])) - + x, y = load(str(args[''])) dm = get_dip_means( - arguments=arguments, + arguments=args, seed=SEED ) @@ -50,3 +48,9 @@ logger.info(f'The NMI score is {normalized_mutual_info_score(y, clusters)}') plot_clustered_data(x, clusters) + + +if __name__ == "__main__": + arguments = docopt(__doc__) + + run(args=arguments) diff --git a/experiments/synthetic/two_gaussians_mix.py b/experiments/synthetic/two_gaussians_mix.py index 7cb6a92..b1ec16b 100644 --- a/experiments/synthetic/two_gaussians_mix.py +++ b/experiments/synthetic/two_gaussians_mix.py @@ -31,24 +31,24 @@ warnings.filterwarnings("ignore") -if __name__ == "__main__": - arguments = docopt(__doc__) +def run(args: dict) -> None: + """Main runner.""" - SEED = int(arguments['--seed']) + SEED = int(args['--seed']) set_seed(SEED) - n_samples = int(arguments['--samples']) - std = float(arguments['--noise']) + n_samples = int(args['--samples']) + std = float(args['--noise']) g = TwoDimGaussianSumGenerator( n=n_samples, cluster_std=std, random_state=SEED ) - mct = get_monte_carlo_test(arguments=arguments, workers_num=1) + mct = get_monte_carlo_test(arguments=args, workers_num=1) tr = 'unimodal' if mct.test(g.x) else 'bimodal' - msg = dict(arguments) + msg = dict(args) msg['groundtruth'] = g.t msg['result'] = tr msg.pop('--help') @@ -63,3 +63,9 @@ 'The inputs and the output of the experiments is: ' f'{msg}' ) + + +if __name__ == "__main__": + arguments = docopt(__doc__) + + run(args=arguments) diff --git a/experiments/synthetic/unimodality.py b/experiments/synthetic/unimodality.py index 4397dbe..1bfa438 100644 --- a/experiments/synthetic/unimodality.py +++ b/experiments/synthetic/unimodality.py @@ -51,20 +51,20 @@ def get_dataset(name: str) -> Callable: raise ValueError(msg) -if __name__ == "__main__": - arguments = docopt(__doc__) +def run(args: dict) -> None: + """Main runner.""" - SEED = int(arguments['--seed']) + SEED = int(args['--seed']) set_seed(SEED) - data_func = get_dataset(str(arguments[''])) - n_samples = int(arguments['--samples']) - noise = float(arguments['--noise']) + data_func = get_dataset(str(args[''])) + n_samples = int(args['--samples']) + noise = float(args['--noise']) x, y = data_func(n_samples=n_samples, noise=noise, random_state=SEED) - mct = get_monte_carlo_test(arguments=arguments, workers_num=1) + mct = get_monte_carlo_test(arguments=args, workers_num=1) - msg = dict(arguments) + msg = dict(args) msg['result'] = 'unimodal' if mct.test(x) else 'multimodal' msg.pop('--help') @@ -78,3 +78,8 @@ def get_dataset(name: str) -> Callable: f'{msg}' ) + +if __name__ == "__main__": + arguments = docopt(__doc__) + + run(args=arguments) diff --git a/tests/experiments/test_synthetic.py b/tests/experiments/test_synthetic.py new file mode 100644 index 0000000..e6c1359 --- /dev/null +++ b/tests/experiments/test_synthetic.py @@ -0,0 +1,68 @@ +"""Tests written for the synthetic experiments module.""" +import pytest + +from experiments.common import get_dip_means +from experiments.synthetic.two_gaussians_mix import run as run_gaussians +from experiments.synthetic.unimodality import run as run_unimodality + + +@pytest.mark.parametrize("projector", ['jl', 'i']) +@pytest.mark.parametrize("distance", ['euclidean', 'mahalanobis']) +@pytest.mark.parametrize("observer", ['percentile', 'random']) +def test_get_dip_means(projector: str, distance: str, observer: str) -> None: + """Test that the factory for DipMeans works properly.""" + + args = { + '': projector, + '--dist': distance, + '--obs': observer, + '': 0.05, + '': 5, + } + + get_dip_means(args, seed=42) + + +@pytest.mark.parametrize("projector", ['jl', 'i']) +@pytest.mark.parametrize("distance", ['euclidean', 'mahalanobis']) +@pytest.mark.parametrize("observer", ['percentile', 'random']) +def test_run_gaussians(projector: str, distance: str, observer: str) -> None: + """Test that the unimodality experiments on 2D gaussians mixture work properly.""" + + args = { + '--seed': 42, + '--samples': 200, + '--noise': 0.5, + '': projector, + '--dist': distance, + '--obs': observer, + '': 0.05, + '': 5, + '--help': False, + '--plot': 'False', + } + + run_gaussians(args) + + +@pytest.mark.parametrize("projector", ['jl', 'i']) +@pytest.mark.parametrize("distance", ['euclidean', 'mahalanobis']) +@pytest.mark.parametrize("observer", ['percentile', 'random']) +def test_run_unimodality(projector: str, distance: str, observer: str) -> None: + """Test that the unimodality experiments work properly.""" + + args = { + '--seed': 42, + '': 'circles', + '--samples': 200, + '--noise': 0.5, + '': projector, + '--dist': distance, + '--obs': observer, + '': 0.05, + '': 5, + '--help': False, + '--plot': 'False', + } + + run_unimodality(args)