mantel/tests/models/test_create_kohonen_params.py
2024-06-11 21:08:08 +10:00

36 lines
1.8 KiB
Python

from hypothesis import given, strategies as st
import numpy as np
from returns.result import Success, Failure
from models import create_kohonen_params
from .common import generate_kohonen_samples, MINIMUM_NETWORK_DIMENSION
@given(
data=st.data(),
feature_size=st.integers(min_value=1, max_value=10),
width=st.integers(min_value=MINIMUM_NETWORK_DIMENSION, max_value=1000),
height=st.integers(min_value=MINIMUM_NETWORK_DIMENSION, max_value=1000),
num_iterations=st.integers(min_value=1, max_value=10000),
initial_learning_rate=st.floats(min_value=1e-5, max_value=1.0, allow_nan=False, allow_infinity=False)
)
def test_create_kohonen_params_valid_success(data, feature_size, width, height, num_iterations, initial_learning_rate):
X = data.draw(generate_kohonen_samples(feature_size))
result = create_kohonen_params(X, width, height, num_iterations, initial_learning_rate)
assert isinstance(result, Success)
@given(
X=st.one_of(
st.lists(st.floats(allow_nan=False, allow_infinity=False, min_value=-1e6, max_value=1e6)), # Not 2D
st.just(np.array([])) # Empty
),
width=st.integers(max_value=MINIMUM_NETWORK_DIMENSION - 1), # Includes non-positive
height=st.integers(max_value=MINIMUM_NETWORK_DIMENSION - 1), # Includes non-positive
num_iterations=st.integers(max_value=0), # Includes non-positive
initial_learning_rate=st.one_of(
st.floats(max_value=0.0, allow_nan=False, allow_infinity=False), # Includes non-positive and NaN/inf
)
)
def test_create_kohonen_params_invalid_failure(X, width, height, num_iterations, initial_learning_rate):
result = create_kohonen_params(X, width, height, num_iterations, initial_learning_rate)
assert isinstance(result, Failure) and isinstance(result.failure(), ValueError)