Skip to content

Commit

Permalink
Add pre-commits
Browse files Browse the repository at this point in the history
  • Loading branch information
ClaudioSalvatoreArcidiacono committed Dec 7, 2024
1 parent f74a62d commit 5ea75a2
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 22 deletions.
19 changes: 19 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- repo: https://github.com/pycqa/isort
rev: 5.13.2
args: ["--profile", "black", "--filter-files"]
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: 24.4.2
hooks:
- id: black
2 changes: 1 addition & 1 deletion development_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ python -m venv venv
source venv/bin/activate
# Install the dependencies
make env-install
```
```
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dev = [
"polars",
"pandas",
"numpy",
"pre-commit",
]
doc = ["mkdocs", "mkdocs-material", "mkdocstrings[python]", "mkdocs-jupyter"]
build = ["build", "twine"]
Expand Down Expand Up @@ -56,5 +57,5 @@ exclude = '''
)/
'''

[tool.flake8]
max-line-length = 88
[tool.isort]
profile = "black"
2 changes: 1 addition & 1 deletion sklearo/encoding/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .woe import WOEEncoder

__all__ = ["WOEEncoder"]
__all__ = ["WOEEncoder"]
18 changes: 10 additions & 8 deletions sklearo/encoding/woe.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import narwhals as nw
from narwhals.typing import IntoFrameT, IntoSeriesT
import warnings
import math
from typing import Sequence, Literal, Any

import warnings
from collections import defaultdict
from typing import Any, Literal, Sequence

import narwhals as nw
from narwhals.typing import IntoFrameT, IntoSeriesT
from pydantic import validate_call

from sklearo.utils import select_columns
from sklearo.validation import check_X_y, check_if_fitted
from sklearo.validation import check_if_fitted, check_X_y


class WOEEncoder:
Expand Down Expand Up @@ -355,8 +355,10 @@ def transform(self, X: IntoFrameT) -> IntoFrameT:
for column, mapping in self.encoding_map_.items():
uniques = X[column].unique()
unseen_cats = uniques.filter(
(~uniques.is_in(next(iter(mapping.values())).keys())
& ~uniques.is_null())
(
~uniques.is_in(next(iter(mapping.values())).keys())
& ~uniques.is_null()
)
).to_list()
if unseen_cats:
unseen_per_col[column] = unseen_cats
Expand Down
7 changes: 3 additions & 4 deletions sklearo/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import inspect
import re
from typing import Sequence

import re
import narwhals as nw
from narwhals.typing import IntoFrameT
import inspect



def select_columns_by_regex_pattern(df: IntoFrameT, pattern: str):
Expand All @@ -29,4 +28,4 @@ def select_columns(df: IntoFrameT, columns: Sequence[nw.typing.DTypes | str] | s
elif isinstance(columns[0], str):
yield from columns
else:
raise ValueError("Invalid columns type")
raise ValueError("Invalid columns type")
2 changes: 1 addition & 1 deletion tests/encoding/test_woe.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
import pandas as pd
import polars as pl
import pytest
import numpy as np

from sklearo.encoding.woe import WOEEncoder

Expand Down
9 changes: 4 additions & 5 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import narwhals as nw
import pandas as pd
import polars as pl
import pytest
import narwhals as nw

from sklearo.utils import (
select_columns,
select_columns_by_regex_pattern,
select_columns_by_types,
select_columns,
)


select_columns_by_regex_pattern = nw.narwhalify(select_columns_by_regex_pattern)
select_columns_by_types = nw.narwhalify(select_columns_by_types)
select_columns = nw.narwhalify(select_columns)
Expand Down Expand Up @@ -72,8 +72,7 @@ def test_select_columns_empty_tuple(self, sample_data, DataFrame):
selected_columns = list(select_columns(df, ()))
assert selected_columns == []


def test_select_columns_invalid_type(self, sample_data, DataFrame):
df = DataFrame(sample_data)
with pytest.raises(ValueError, match="Invalid columns type"):
list(select_columns(df, [1, 2]))
list(select_columns(df, [1, 2]))

0 comments on commit 5ea75a2

Please sign in to comment.