Skip to content

Commit

Permalink
style: format
Browse files Browse the repository at this point in the history
  • Loading branch information
bbayukari committed Apr 18, 2024
1 parent 32eff32 commit 9839c72
Show file tree
Hide file tree
Showing 7 changed files with 289 additions and 93 deletions.
4 changes: 3 additions & 1 deletion pytest/create_test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ def __init__(self, N=100, P=5, K=2, seed=1):
self.seed = seed

def create_linear_model(self):
X, Y, true_params = make_regression(self.N, self.P, n_informative=self.K, coef=True, random_state=self.seed)
X, Y, true_params = make_regression(
self.N, self.P, n_informative=self.K, coef=True, random_state=self.seed
)

def linear_model(params):
return jnp.sum(jnp.square(Y - jnp.matmul(X, params)))
Expand Down
17 changes: 14 additions & 3 deletions pytest/test_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
import pytest

from create_test_model import CreateTestModel
from skscope import utilities, ScopeSolver, BaseSolver, HTPSolver, GraspSolver, IHTSolver
from skscope import (
utilities,
ScopeSolver,
BaseSolver,
HTPSolver,
GraspSolver,
IHTSolver,
)
import skscope._scope as _scope

model_creator = CreateTestModel()
Expand All @@ -25,7 +32,9 @@
def test_numeric_solver(model, solver_creator):
from skscope.numeric_solver import convex_solver_BFGS

solver = solver_creator(model["n_features"], model["n_informative"], numeric_solver=convex_solver_BFGS)
solver = solver_creator(
model["n_features"], model["n_informative"], numeric_solver=convex_solver_BFGS
)
solver.solve(model["loss"], jit=True)

assert set(model["support_set"]) == set(solver.get_support())
Expand Down Expand Up @@ -160,7 +169,9 @@ def test_scope_hessian():


def test_scope_dynamic_max_exchange_num():
solver = ScopeSolver(linear["n_features"], linear["n_informative"], is_dynamic_max_exchange_num=False)
solver = ScopeSolver(
linear["n_features"], linear["n_informative"], is_dynamic_max_exchange_num=False
)
solver.solve(linear["loss"], jit=True)

assert set(linear["support_set"]) == set(solver.support_set)
Expand Down
68 changes: 51 additions & 17 deletions pytest/test_except.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
def test_group(model, solver_creator):
solver = solver_creator(model["n_features"], model["n_informative"])
solver.set_config(group=np.zeros((1, model["n_features"])))
with pytest.raises(ValueError, match=re.escape("Group should be an 1D array of integers.")):
with pytest.raises(
ValueError, match=re.escape("Group should be an 1D array of integers.")
):
solver.solve(model["loss"], jit=True)
solver.set_config(group=np.zeros(model["n_features"] + 1))
with pytest.raises(
Expand All @@ -38,7 +40,9 @@ def test_group(model, solver_creator):
with pytest.raises(ValueError, match=re.escape("Group should start from 0.")):
solver.solve(model["loss"], jit=True)
solver.set_config(group=-np.arange(model["n_features"]))
with pytest.raises(ValueError, match=re.escape("Group should be an incremental integer array.")):
with pytest.raises(
ValueError, match=re.escape("Group should be an incremental integer array.")
):
solver.solve(model["loss"], jit=True)
solver.set_config(group=np.arange(0, 2 * model["n_features"], 2))
with pytest.raises(ValueError, match=re.escape("There is a gap in group.")):
Expand All @@ -50,7 +54,9 @@ def test_group(model, solver_creator):
def test_preselect(model, solver_creator):
solver = solver_creator(model["n_features"], model["n_informative"])
solver.set_config(preselect=[-1])
with pytest.raises(ValueError, match=re.escape("preselect should be between 0 and dimensionality.")):
with pytest.raises(
ValueError, match=re.escape("preselect should be between 0 and dimensionality.")
):
solver.solve(model["loss"], jit=True)


Expand All @@ -72,7 +78,9 @@ def test_ic(model, solver_creator):
solver = solver_creator(model["n_features"])
with pytest.raises(
ValueError,
match=re.escape("ic_method should be provided for choosing sparsity level with information criterion."),
match=re.escape(
"ic_method should be provided for choosing sparsity level with information criterion."
),
):
solver.solve(model["loss"], jit=True)
solver.set_config(ic_method=utilities.LinearSIC)
Expand All @@ -87,14 +95,22 @@ def test_ic(model, solver_creator):
@pytest.mark.parametrize("solver_creator", solvers, ids=solvers_ids)
def test_cv(model, solver_creator):
solver = solver_creator(model["n_features"], cv=2)
with pytest.raises(ValueError, match=re.escape("cv should not be greater than sample_size.")):
with pytest.raises(
ValueError, match=re.escape("cv should not be greater than sample_size.")
):
solver.solve(model["loss"], jit=True)
solver.set_config(sample_size=model["n_samples"])
with pytest.raises(ValueError, match=re.escape("split_method should be provided when cv > 1.")):
with pytest.raises(
ValueError, match=re.escape("split_method should be provided when cv > 1.")
):
solver.solve(model["loss"], jit=True)
solver.set_config(split_method=lambda data, indeices: (data[0][indeices], data[1][indeices]))
solver.set_config(
split_method=lambda data, indeices: (data[0][indeices], data[1][indeices])
)
solver.set_config(cv_fold_id=np.zeros((1, model["n_samples"])))
with pytest.raises(ValueError, match=re.escape("cv_fold_id should be an 1D array of integers.")):
with pytest.raises(
ValueError, match=re.escape("cv_fold_id should be an 1D array of integers.")
):
solver.solve(model["loss_data"], data=model["data"], jit=True)
solver.set_config(cv_fold_id=np.zeros(1 + model["n_samples"]))
with pytest.raises(
Expand All @@ -105,21 +121,27 @@ def test_cv(model, solver_creator):
solver.set_config(cv_fold_id=np.zeros(model["n_samples"]))
with pytest.raises(
ValueError,
match=re.escape("The number of different elements in cv_fold_id should be equal to cv."),
match=re.escape(
"The number of different elements in cv_fold_id should be equal to cv."
),
):
solver.solve(model["loss_data"], data=model["data"], jit=True)


@pytest.mark.parametrize("model", models, ids=models_ids)
@pytest.mark.parametrize("solver_creator", solvers, ids=solvers_ids)
def test_init(model, solver_creator):
solver = solver_creator(model["n_features"], model["n_informative"], model["n_samples"])
solver = solver_creator(
model["n_features"], model["n_informative"], model["n_samples"]
)
with pytest.raises(
ValueError,
match=re.escape("The initial active set should be an 1D array of integers."),
):
solver.solve(model["loss"], init_support_set=np.zeros((1, model["n_features"])))
with pytest.raises(ValueError, match=re.escape("init_support_set contains wrong index.")):
with pytest.raises(
ValueError, match=re.escape("init_support_set contains wrong index.")
):
solver.solve(model["loss"], init_support_set=[-1])
with pytest.raises(
ValueError,
Expand All @@ -134,7 +156,9 @@ def test_log(model):
solver.set_config(file_log_level="info")
with pytest.raises(
ValueError,
match=re.escape("console_log_level and file_log_level must be in 'off', 'error', 'warning', 'debug'"),
match=re.escape(
"console_log_level and file_log_level must be in 'off', 'error', 'warning', 'debug'"
),
):
solver.solve(model["loss"], jit=True)
solver.set_config(file_log_level="error", log_file_name=123)
Expand All @@ -146,15 +170,21 @@ def test_log(model):
def test_gs(model):
solver = solvers[0](model["n_features"])
solver.set_config(path_type="123")
with pytest.raises(ValueError, match=re.escape("path_type should be 'seq' or 'gs'")):
with pytest.raises(
ValueError, match=re.escape("path_type should be 'seq' or 'gs'")
):
solver.solve(model["loss"], jit=True)
solver.set_config(gs_upper_bound=model["n_features"] + 1, path_type="gs")
with pytest.raises(
ValueError,
match=re.escape("gs_lower_bound and gs_upper_bound should be between 0 and dimensionality."),
match=re.escape(
"gs_lower_bound and gs_upper_bound should be between 0 and dimensionality."
),
):
solver.solve(model["loss"], jit=True)
solver.set_config(gs_upper_bound=model["n_features"], gs_lower_bound=model["n_features"] + 1)
solver.set_config(
gs_upper_bound=model["n_features"], gs_lower_bound=model["n_features"] + 1
)
with pytest.raises(
ValueError,
match=re.escape("gs_upper_bound should be larger than gs_lower_bound."),
Expand All @@ -168,7 +198,9 @@ def test_screening_size(model):
solver.set_config(screening_size=model["n_informative"] - 1)
with pytest.raises(
ValueError,
match=re.escape("screening_size should be between sparsity and dimensionality."),
match=re.escape(
"screening_size should be between sparsity and dimensionality."
),
):
solver.solve(model["loss"], jit=True)

Expand All @@ -177,5 +209,7 @@ def test_screening_size(model):
def test_splicing_type(model):
solver = solvers[0](model["n_features"], model["n_informative"])
solver.set_config(splicing_type="123")
with pytest.raises(ValueError, match=re.escape("splicing_type should be 'halve' or 'taper'.")):
with pytest.raises(
ValueError, match=re.escape("splicing_type should be 'halve' or 'taper'.")
):
solver.solve(model["loss"], jit=True)
25 changes: 19 additions & 6 deletions pytest/test_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@
@pytest.mark.parametrize("model", models, ids=models_ids)
@pytest.mark.parametrize("solver_creator", solvers, ids=solvers_ids)
def test_linear_constraint(model, solver_creator):
solver = solver_creator(model["n_features"], model["n_informative"], random_state=42)
solver = solver_creator(
model["n_features"], model["n_informative"], random_state=42
)
solver.solve(
model["loss"],
jit=True,
Expand All @@ -71,7 +73,9 @@ def test_linear_constraint(model, solver_creator):
@pytest.mark.parametrize("model", models, ids=models_ids)
@pytest.mark.parametrize("solver_creator", solvers, ids=solvers_ids)
def test_non_negative(model, solver_creator):
solver = solver_creator(model["n_features"], model["n_informative"], random_state=42)
solver = solver_creator(
model["n_features"], model["n_informative"], random_state=42
)
solver.solve(
model["loss"],
jit=True,
Expand All @@ -87,7 +91,9 @@ def test_non_negative(model, solver_creator):
@pytest.mark.parametrize("model", models, ids=models_ids)
@pytest.mark.parametrize("solver_creator", solvers, ids=solvers_ids)
def test_simplex_constraint(model, solver_creator):
solver = solver_creator(model["n_features"], model["n_informative"], random_state=42)
solver = solver_creator(
model["n_features"], model["n_informative"], random_state=42
)
solver.solve(
model["loss"],
jit=True,
Expand All @@ -104,7 +110,9 @@ def test_simplex_constraint(model, solver_creator):
@pytest.mark.parametrize("model", models, ids=models_ids)
@pytest.mark.parametrize("solver_creator", solvers, ids=solvers_ids)
def test_box_constraint(model, solver_creator):
solver = solver_creator(model["n_features"], model["n_informative"], random_state=42)
solver = solver_creator(
model["n_features"], model["n_informative"], random_state=42
)
solver.solve(
model["loss"],
jit=True,
Expand All @@ -121,7 +129,9 @@ def test_box_constraint(model, solver_creator):
@pytest.mark.parametrize("model", models, ids=models_ids)
@pytest.mark.parametrize("solver_creator", solvers, ids=solvers_ids)
def test_offset_sparse(model, solver_creator):
solver = solver_creator(model["n_features"], model["n_informative"], random_state=42)
solver = solver_creator(
model["n_features"], model["n_informative"], random_state=42
)
solver.solve(
model["loss"],
jit=True,
Expand All @@ -131,4 +141,7 @@ def test_offset_sparse(model, solver_creator):
],
)

assert np.sum(solver.get_estimated_params() == -1.0) == model["n_features"] - model["n_informative"]
assert (
np.sum(solver.get_estimated_params() == -1.0)
== model["n_features"] - model["n_informative"]
)
Loading

0 comments on commit 9839c72

Please sign in to comment.