Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudon committed Apr 29, 2024
1 parent ed8f0d4 commit 1a31152
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 60 deletions.
92 changes: 44 additions & 48 deletions examples/Example_7_data_clustering.ipynb

Large diffs are not rendered by default.

74 changes: 74 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Utils for tests."""
import networkx as nx
import numpy as np
import pytest

from pygenstability.constructors import load_constructor
Expand Down Expand Up @@ -44,3 +45,76 @@ def graph_signed():
def results(graph):
constructor = load_constructor("continuous_combinatorial", graph)
return pgs.run(graph, constructor=constructor)


def generate_circles(
n_samples_out=300,
n_groups_out=3,
gap_out=np.pi / 15,
n_samples_in=300,
n_groups_in=3,
gap_in=np.pi / 8,
offset_in=np.pi / 7,
factor=0.5,
noise=0.03,
seed=42,
):
"""Generate two circles with multiscale structure.
Adapted from: sklearn.datasets.make_circles
"""
rng = np.random.default_rng(seed)

# generate outer circle that is split into groups
linspace_out = []
for i in range(n_groups_out):
linspace_out += list(
np.linspace(
i * 2 / n_groups_out * np.pi + gap_out / 2,
(i + 1) * 2 / n_groups_out * np.pi - gap_out / 2,
int(n_samples_out / n_groups_out),
endpoint=False,
)
)
outer_circ_x = np.cos(linspace_out)
outer_circ_y = np.sin(linspace_out)

# generate inner circle that is split into groups
linspace_in = []
for j in range(n_groups_out):
linspace_in += list(
np.linspace(
j * 2 / n_groups_in * np.pi + gap_in / 2,
(j + 1) * 2 / n_groups_in * np.pi - gap_in / 2,
int(n_samples_in / n_groups_in),
endpoint=False,
)
+ offset_in
)
inner_circ_x = np.cos(linspace_in) * factor
inner_circ_y = np.sin(linspace_in) * factor

# combine circles
X = np.vstack([np.append(outer_circ_x, inner_circ_x), np.append(outer_circ_y, inner_circ_y)]).T

# create group labels
y = np.zeros(n_samples_out + n_samples_in)
for i in range(n_groups_in):
y[int(i * n_samples_in / n_groups_in) : int((i + 1) * n_samples_in / n_groups_in)] = i
for i in range(n_groups_out):
y[
int(n_samples_in + i * n_samples_out / n_groups_out) : int(
n_samples_in + (i + 1) * n_samples_out / n_groups_out
)
] = (i + n_groups_in)

# add noise to data
if noise is not None:
X += rng.normal(scale=noise, size=X.shape)

return X, y

@pytest.fixture()
def X():
X, y = generate_circles()
return X
14 changes: 2 additions & 12 deletions tests/test_pygenstability.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,9 @@
from pygenstability.constructors import load_constructor
from pygenstability import pygenstability as pgs

DATA = Path(__file__).absolute().parent / "data"

from .utils import _to_list

def _to_list(data):
"""Convert dict to list with floats for yaml encoding."""
data.pop("NVI", None) # NVI computation is unstable, we don't test it
data.pop("ttprime", None) # ttprime computation is unstable, we don't test it
for key, val in data.items():
if isinstance(val, dict):
data[key] = _to_list(data[key])
if isinstance(val, (np.ndarray, list)):
data[key] = np.array(val, dtype=float).tolist()
return data
DATA = Path(__file__).absolute().parent / "data"


def test_run(graph, graph_non_connected, graph_directed, graph_signed):
Expand Down

0 comments on commit 1a31152

Please sign in to comment.