Skip to content

Commit

Permalink
fix: solve test run error on github
Browse files Browse the repository at this point in the history
  • Loading branch information
raulmarindev committed Nov 7, 2024
1 parent 669e52f commit 3c30cd9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
12 changes: 9 additions & 3 deletions openmodels/serializers/sklearn_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,19 +195,25 @@ def _convert_to_sklearn_types(value: Any, attr_type: str = "none") -> Any:
----------
value : Any
The JSON-deserialized value.
attr_type : str
The target type to convert to.
Returns
-------
Any
The scikit-learn type of the value.
"""

# Base case: if attr_type is not a list, convert value based on attr_type
if isinstance(attr_type, str):
if attr_type == "csr_matrix":
# Ensure all sparse matrix components are of correct dtype
return csr_matrix(
(value["data"], value["indices"], value["indptr"]),
shape=value["shape"],
(
np.array(value["data"], dtype=np.float64),
np.array(value["indices"], dtype=np.int32),
np.array(value["indptr"], dtype=np.int32),
),
shape=tuple(value["shape"]),
)
elif attr_type == "ndarray":
return np.array(value)
Expand Down
28 changes: 20 additions & 8 deletions openmodels/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ def transform(self, X: Union[np.ndarray, csr_matrix]) -> np.ndarray: ...

@runtime_checkable
class FittableModel(Protocol):
def fit(self, X: np.ndarray, y: np.ndarray) -> "FittableModel": ...
def fit(
self, X: Union[np.ndarray, csr_matrix], y: np.ndarray
) -> "FittableModel": ...


ModelType = Union[PredictorModel, TransformerModel, FittableModel]
Expand All @@ -51,16 +53,19 @@ def ensure_correct_sparse_format(


def fit_model(
model: FittableModel, x: np.ndarray, y: np.ndarray, abs: bool = False
model: FittableModel,
x: Union[np.ndarray, csr_matrix],
y: np.ndarray,
abs: bool = False,
) -> FittableModel:
"""
Fits a model to the provided data.
Parameters
----------
model : T
model : FittableModel
The scikit-learn model to fit.
x : np.ndarray
x : Union[np.ndarray, csr_matrix]
The training input samples.
y : np.ndarray
The target values (class labels in classification, real numbers in regression).
Expand All @@ -69,14 +74,21 @@ def fit_model(
Returns
-------
T
FittableModel
The fitted scikit-learn model.
"""
if not isinstance(model, FittableModel):
raise TypeError("Model must have a 'fit' method")

if abs:
model.fit(np.absolute(x), y)
if isinstance(x, csr_matrix):
# Handle absolute value for sparse matrix
x_abs = csr_matrix(
(np.absolute(x.data), x.indices, x.indptr), shape=x.shape
)
model.fit(x_abs, y)
else:
model.fit(np.absolute(x), y)
else:
model.fit(x, y)
return model
Expand Down Expand Up @@ -143,7 +155,7 @@ def run_test_model(
model: FittableModel,
x: np.ndarray,
y: np.ndarray,
x_sparse: Optional[np.ndarray],
x_sparse: Optional[Union[np.ndarray, csr_matrix]],
y_sparse: Optional[np.ndarray],
model_name: str,
abs: bool = False,
Expand All @@ -159,7 +171,7 @@ def run_test_model(
The training input samples.
y : np.ndarray
The target values (class labels in classification, real numbers in regression).
x_sparse : np.ndarray or None
x_sparse : Optional[Union[np.ndarray, csr_matrix]]
The sparse training input samples.
y_sparse : np.ndarray or None
The sparse target values.
Expand Down
5 changes: 4 additions & 1 deletion test/test_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from sklearn.naive_bayes import BernoulliNB, GaussianNB, MultinomialNB, ComplementNB
from sklearn.neural_network import MLPClassifier
from sklearn.tree import DecisionTreeClassifier
from openmodels.test_helpers import run_test_model
from openmodels.test_helpers import run_test_model, ensure_correct_sparse_format

# Define constants
N_SAMPLES = 50
Expand Down Expand Up @@ -109,6 +109,9 @@ def test_qda(data):

def test_svm(data):
x, y, x_sparse, y_sparse = data
# Ensure sparse data is properly formatted before testing
x_sparse = ensure_correct_sparse_format(x_sparse)

run_test_model(
svm.SVC(gamma=0.001, C=100.0, kernel="linear"),
x,
Expand Down
9 changes: 6 additions & 3 deletions test/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from sklearn.neural_network import MLPRegressor
from sklearn.svm import SVR
from openmodels.test_helpers import run_test_model
from openmodels.test_helpers import run_test_model, ensure_correct_sparse_format


@pytest.fixture(scope="module")
Expand All @@ -32,7 +32,7 @@ def data():
}
)
y_sparse = [random.random() for i in range(0, 100)]
x_sparse = feature_hasher.transform(features)
x_sparse = feature_hasher.transform(iter(features))

return x, y, x_sparse, y_sparse

Expand All @@ -57,6 +57,9 @@ def test_ridge_regression(data):

def test_svr(data):
x, y, x_sparse, y_sparse = data
# Ensure sparse data is properly formatted before testing
x_sparse = ensure_correct_sparse_format(x_sparse)

run_test_model(
SVR(gamma="scale", C=1.0, epsilon=0.2), x, y, x_sparse, y_sparse, "svr.json"
)
Expand Down Expand Up @@ -109,5 +112,5 @@ def test_mlp_regression(data):
def test_pls_regression(data):
x, y, _, _ = data
run_test_model(
PLSRegression(n_components=2), x, y, None, None, "pls-regression.json"
PLSRegression(n_components=2), x, y, None, None, "pls-regression.json" # type: ignore
)

0 comments on commit 3c30cd9

Please sign in to comment.