Skip to content

Commit 587918a

Browse files
committed
refactor: improve project structure, update docs workflow, and add notebook
- CI: Add `torch` dependency to `docs.yml` workflow to support autodoc imports. - Refactor: Reorganize imports in time-series model modules for better readability and consistency. - Fix: Use `Exception` instead of bare `except` in `data_feature_methods.py` for safer error handling. - Cleanup: Remove unused `NoFeaturesError` import in `test_data_pipeline.py`. - Refactor: Standardize scikit-learn metric imports in `global_params.py` and `project_score_save.py`.
1 parent 171baeb commit 587918a

9 files changed

Lines changed: 37 additions & 29 deletions

File tree

.github/workflows/docs.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ jobs:
7070
# Ensure setuptools and wheel are present before installing the project
7171
pip install --upgrade pip setuptools wheel
7272
# psutil/ipython required for imports during autodoc (H2OBaseClassifier, pipeline.data)
73+
# torch is needed for some model classes that are imported during autodoc
74+
pip install torch
7375
pip install psutil ipython
7476
pip install .[docs]
7577
@@ -133,6 +135,8 @@ jobs:
133135
run: |
134136
pip install --upgrade pip setuptools wheel
135137
# psutil/ipython required for imports during autodoc (H2OBaseClassifier, pipeline.data)
138+
# torch is needed for some model classes that are imported during autodoc
139+
pip install torch
136140
pip install psutil ipython
137141
pip install .[docs]
138142

ml_grid/model_classes_time_series/TapNetClassifier_module.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22

33
import numpy as np
44
import keras
5-
from sklearn.base import BaseEstimator, ClassifierMixin
5+
from sklearn.base import BaseEstimator, ClassifierMixin, TransformerMixin
6+
from sklearn.impute import SimpleImputer
7+
from sklearn.pipeline import Pipeline
8+
from sklearn.preprocessing import StandardScaler
9+
from skopt.space import Categorical
10+
11+
from ml_grid.pipeline.data import pipe
12+
from ml_grid.util.param_space import ParamSpace
613

714

815
class _DummyClassifier(BaseEstimator, ClassifierMixin):
@@ -44,16 +51,6 @@ def _predict_proba(self, X, **kwargs):
4451
except ImportError:
4552
TapNetClassifier = _DummyClassifier
4653

47-
from skopt.space import Categorical
48-
from sklearn.pipeline import Pipeline
49-
from sklearn.impute import SimpleImputer
50-
from sklearn.preprocessing import StandardScaler
51-
from sklearn.base import BaseEstimator, TransformerMixin
52-
53-
54-
from ml_grid.pipeline.data import pipe
55-
from ml_grid.util.param_space import ParamSpace
56-
5754

5855
class TimeSeriesStandardScaler(BaseEstimator, TransformerMixin):
5956
def __init__(self, epsilon=1e-6):

ml_grid/model_classes_time_series/shapeDTWClassifier_module.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from typing import Any, Dict, List
22
import numpy as np
33
from sklearn.base import BaseEstimator, ClassifierMixin
4+
from skopt.space import Categorical
5+
6+
from ml_grid.pipeline.data import pipe
47

58

69
class _DummyClassifier(BaseEstimator, ClassifierMixin):
@@ -25,8 +28,6 @@ def predict_proba(self, X):
2528
except ImportError:
2629
# ShapeDTW was removed in aeon v0.11.0. Use a dummy placeholder.
2730
ShapeDTW = _DummyClassifier
28-
from skopt.space import Categorical
29-
from ml_grid.pipeline.data import pipe
3031

3132

3233
class ShapeDTW_class:

ml_grid/pipeline/data_feature_methods.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def getNFeaturesMarkovBlanket(
215215
os.close(devnull_fd)
216216
os.close(old_stdout_fd)
217217
os.close(old_stderr_fd)
218-
except:
218+
except Exception:
219219
pass # Silently fail if restoration doesn't work
220220

221221
# Get the feature indices from the Markov blanket (MB)

ml_grid/pipeline/test_data_pipeline.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
# Ensure the project root is in the Python path to allow for module imports
1818
try:
19-
from ml_grid.pipeline.data import NoFeaturesError, pipe
19+
from ml_grid.pipeline.data import pipe
2020
from ml_grid.util.global_params import global_parameters
2121
except ImportError:
2222
# This allows the test to be run from the project root directory

ml_grid/util/global_params.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@
1010
from typing import Any, Callable, Dict, List, Union
1111
import logging
1212

13+
import numpy as np
14+
from sklearn.metrics import (
15+
accuracy_score,
16+
f1_score,
17+
make_scorer,
18+
recall_score,
19+
roc_auc_score,
20+
)
21+
1322
# Disable aeon telemetry to prevent SSL errors in restricted environments
1423
os.environ["AEON_TRACKING"] = "False"
1524

@@ -61,15 +70,6 @@ def _configure_tensorflow_gpu_env():
6170
_configure_tensorflow_gpu_env()
6271
# --- END FIX ---
6372

64-
import numpy as np
65-
from sklearn.metrics import (
66-
accuracy_score,
67-
f1_score,
68-
make_scorer,
69-
recall_score,
70-
roc_auc_score,
71-
)
72-
7373

7474
def custom_roc_auc_score(y_true: np.ndarray, y_pred: np.ndarray) -> float:
7575
"""Calculates ROC AUC score, handling cases with only one class in y_true.

ml_grid/util/project_score_save.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@
44
import pandas as pd
55
from ml_grid.util.global_params import global_parameters
66
from sklearn import metrics
7-
from sklearn.metrics import *
7+
from sklearn.metrics import (
8+
accuracy_score,
9+
f1_score,
10+
matthews_corrcoef,
11+
precision_recall_fscore_support,
12+
precision_score,
13+
recall_score,
14+
)
815
import pickle
916
import logging
1017
import warnings

notebooks/unit_test_synthetic.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@
332332
" try:\n",
333333
" # Try to get a meaningful name for the model\n",
334334
" model_name = error_info[0].__class__.__name__\n",
335-
" except:\n",
335+
" except Exception:\n",
336336
" model_name = \"Unknown Model\"\n",
337337
" error_exception = error_info[1]\n",
338338
" print(f\" {i+1}. Model: {model_name}, Error: {error_exception}\")\n",

tests/test_tabpfn_classifier_class.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import pandas as pd
55
from unittest.mock import MagicMock, patch
6+
import importlib.util
67
from ml_grid.model_classes.tabpfn_classifier_class import (
78
TabPFNClassifierClass,
89
)
@@ -27,9 +28,7 @@
2728

2829
# Conditionally mock tabpfn. If it's installed, we might want to use it for integration tests.
2930
# If not installed, we mock it to allow importing the wrapper class.
30-
try:
31-
import tabpfn
32-
except ImportError:
31+
if importlib.util.find_spec("tabpfn") is None:
3332
mock_tabpfn_module = MagicMock()
3433
sys.modules["tabpfn"] = mock_tabpfn_module
3534
sys.modules["tabpfn.constants"] = MagicMock()

0 commit comments

Comments
 (0)