Skip to content

Commit

Permalink
lint and add mypy to pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lbluque committed Sep 27, 2023
1 parent 4cf5126 commit 7a66993
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,8 @@ repos:
- id: rst-backticks
- id: rst-directive-colons
- id: rst-inline-touching-normal

- repo: https://github.com/pre-commit/mirrors-mypy
rev: '' # Use the sha / tag you want to point at
hooks:
- id: mypy
3 changes: 0 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,6 @@ omit = ["*/__init__.py"]
convention = "google"
add_ignore = ["D107"]

[tool.mypy]
plugins = ["numpy.typing.mypy_plugin"]

[[tool.mypy.overrides]]
module = ["sklearn.*", "scipy.linalg"]
ignore_missing_imports = true
12 changes: 5 additions & 7 deletions src/sparselm/model/_adaptive_lasso.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def _solve(
) -> NDArray[np.floating]:
"""Solve Lasso problem iteratively adaptive weights."""
assert self.canonicals_.parameters is not None
previous_weights = self._get_weights_value(self.canonicals_.parameters)
previous_weights = self._get_weights_value(self.canonicals_.parameters)
for i in range(self.max_iter):
self.canonicals_.problem.solve(
solver=self.solver, warm_start=self.warm_start, **solver_options
Expand All @@ -221,13 +221,13 @@ def _solve(
self.n_iter_ = i + 1 # save number of iterations for sklearn
self._iterative_update(
self.canonicals_.beta.value,
self.canonicals_.parameters,
self.canonicals_.parameters,
self.canonicals_.auxiliaries,
)
# check convergence
if self._check_convergence(self.canonicals_.parameters, previous_weights):
if self._check_convergence(self.canonicals_.parameters, previous_weights):
break
previous_weights = self._get_weights_value(self.canonicals_.parameters)
previous_weights = self._get_weights_value(self.canonicals_.parameters)
return self.canonicals_.beta.value


Expand Down Expand Up @@ -719,9 +719,7 @@ def _iterative_update(
)
parameters.adaptive_group_weights.value = (
self.canonicals_.parameters.lambda2.value * parameters.group_weights
) * update(
auxiliaries.group_norms.value, self.eps
)
) * update(auxiliaries.group_norms.value, self.eps)


class AdaptiveRidgedGroupLasso(AdaptiveGroupLasso, RidgedGroupLasso):
Expand Down
4 changes: 2 additions & 2 deletions src/sparselm/model/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ def generate_problem(

beta = cp.Variable(X.shape[1])
parameters = self._generate_params(X, y)
auxiliaries = self._generate_auxiliaries(X, y, beta, parameters)
auxiliaries = self._generate_auxiliaries(X, y, beta, parameters)
objective = self._generate_objective(X, y, beta, parameters, auxiliaries)
constraints = self._generate_constraints(X, y, beta, parameters, auxiliaries)
problem = cp.Problem(cp.Minimize(objective), constraints)
Expand Down Expand Up @@ -543,6 +543,6 @@ def _generate_objective(
assert parameters is not None and hasattr(parameters, "eta")
c0 = 2 * X.shape[0] # keeps hyperparameter scale independent
objective = super()._generate_objective(X, y, beta, parameters, auxiliaries) # type: ignore
objective += c0 * parameters.eta * cp.sum_squares(tikhonov_w @ beta)
objective += c0 * parameters.eta * cp.sum_squares(tikhonov_w @ beta)

return objective
18 changes: 9 additions & 9 deletions src/sparselm/model/_lasso.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def _generate_params(self, X: NDArray, y: NDArray) -> SimpleNamespace:
group_weights = (
np.ones(n_groups) if self.group_weights is None else self.group_weights
)
parameters.group_weights = group_weights
parameters.group_weights = group_weights
return parameters

@staticmethod
Expand Down Expand Up @@ -272,7 +272,7 @@ def _generate_regularization(
auxiliaries: SimpleNamespace | None = None,
) -> cp.Expression:
assert auxiliaries is not None
return parameters.alpha * (parameters.group_weights @ auxiliaries.group_norms)
return parameters.alpha * (parameters.group_weights @ auxiliaries.group_norms)


# TODO this implementation is not efficient, reimplement, or simply deprecate.
Expand Down Expand Up @@ -401,7 +401,7 @@ def _generate_params(self, X: NDArray, y: NDArray) -> SimpleNamespace:
group_weights = (
np.ones(n_groups) if self.group_weights is None else self.group_weights
)
parameters.group_weights = group_weights
parameters.group_weights = group_weights
return parameters

def generate_problem(
Expand Down Expand Up @@ -493,7 +493,7 @@ def _solve(self, X, y, solver_options, *args, **kwargs) -> NDArray[np.floating]:
[
sum(
self.canonicals_.beta.value[
self.canonicals_.auxiliaries.extended_coef_indices == i
self.canonicals_.auxiliaries.extended_coef_indices == i
]
)
for i in range(X.shape[1])
Expand Down Expand Up @@ -610,16 +610,16 @@ def _validate_params(self, X, y):
def _set_param_values(self) -> None:
super()._set_param_values()
assert self.canonicals_.parameters is not None
self.canonicals_.parameters.lambda1.value = self.l1_ratio * self.alpha
self.canonicals_.parameters.lambda2.value = (1 - self.l1_ratio) * self.alpha
self.canonicals_.parameters.lambda1.value = self.l1_ratio * self.alpha
self.canonicals_.parameters.lambda2.value = (1 - self.l1_ratio) * self.alpha

def _generate_params(self, X: NDArray, y: NDArray) -> SimpleNamespace:
"""Generate parameters."""
parameters = super()._generate_params(X, y)
# save for information purposes
parameters.l1_ratio = self.l1_ratio
parameters.lambda1 = cp.Parameter(nonneg=True, value=self.l1_ratio * self.alpha)
parameters.lambda2 = cp.Parameter(
parameters.l1_ratio = self.l1_ratio
parameters.lambda1 = cp.Parameter(nonneg=True, value=self.l1_ratio * self.alpha)
parameters.lambda2 = cp.Parameter(
nonneg=True, value=(1 - self.l1_ratio) * self.alpha
)
return parameters
Expand Down
2 changes: 1 addition & 1 deletion src/sparselm/model/_miqp/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def _generate_hierarchy_constraints(
z0_index = {gid: i for i, gid in enumerate(group_ids)}
constraints = [
z0[z0_index[high_id]] <= z0[z0_index[sub_id]]
for high_id, sub_ids in zip(group_ids, self.hierarchy)
for high_id, sub_ids in zip(group_ids, self.hierarchy)
for sub_id in sub_ids
]
return constraints
4 changes: 1 addition & 3 deletions src/sparselm/model/_miqp/_regularized_l0.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,7 @@ def _generate_objective(
c0 = 2 * X.shape[0] # keeps hyperparameter scale independent
objective = super()._generate_objective(
X, y, beta, parameters, auxiliaries
) + c0 * parameters.alpha * cp.sum(
auxiliaries.z0
)
) + c0 * parameters.alpha * cp.sum(auxiliaries.z0)
return objective


Expand Down
2 changes: 1 addition & 1 deletion tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

ESTIMATORS = getmembers(spm, isclass)
ESTIMATOR_NAMES = [est[0] for est in ESTIMATORS]
ESTIMATORS = [est[1] for est in ESTIMATORS]
ESTIMATORS = [est[1] for est in ESTIMATORS] # type: ignore


@pytest.fixture(params=ESTIMATORS, ids=ESTIMATOR_NAMES)
Expand Down

0 comments on commit 7a66993

Please sign in to comment.