Skip to content

Commit

Permalink
test: add functional tests for synthetic experiments
Browse files Browse the repository at this point in the history
  • Loading branch information
prokolyvakis committed Oct 3, 2023
1 parent 90827f5 commit 04f57ca
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 22 deletions.
18 changes: 11 additions & 7 deletions experiments/synthetic/clustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -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['<d>']))

x, y = load(str(args['<d>']))

dm = get_dip_means(
arguments=arguments,
arguments=args,
seed=SEED
)

Expand All @@ -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)
20 changes: 13 additions & 7 deletions experiments/synthetic/two_gaussians_mix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -63,3 +63,9 @@
'The inputs and the output of the experiments is: '
f'{msg}'
)


if __name__ == "__main__":
arguments = docopt(__doc__)

run(args=arguments)
21 changes: 13 additions & 8 deletions experiments/synthetic/unimodality.py
Original file line number Diff line number Diff line change
Expand Up @@ -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['<d>']))
n_samples = int(arguments['--samples'])
noise = float(arguments['--noise'])
data_func = get_dataset(str(args['<d>']))
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')

Expand All @@ -78,3 +78,8 @@ def get_dataset(name: str) -> Callable:
f'{msg}'
)


if __name__ == "__main__":
arguments = docopt(__doc__)

run(args=arguments)
68 changes: 68 additions & 0 deletions tests/experiments/test_synthetic.py
Original file line number Diff line number Diff line change
@@ -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 = {
'<pj>': projector,
'--dist': distance,
'--obs': observer,
'<pv>': 0.05,
'<sims>': 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,
'<pj>': projector,
'--dist': distance,
'--obs': observer,
'<pv>': 0.05,
'<sims>': 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,
'<d>': 'circles',
'--samples': 200,
'--noise': 0.5,
'<pj>': projector,
'--dist': distance,
'--obs': observer,
'<pv>': 0.05,
'<sims>': 5,
'--help': False,
'--plot': 'False',
}

run_unimodality(args)

0 comments on commit 04f57ca

Please sign in to comment.