Skip to content

Commit 3659407

Browse files
authored
[style] Remove prefix typing and adapt to google style doc (#307)
* [style] Remove prefix typing and adapt to google style doc The changes are the following: 1. [google doc style] Arguments --> Args 2. [style] typing.xxx --> xxx 3. [mypy] torch.tensor --> torch.Tensor * [fix] Fix typing np.array --> np.ndarray * [fix] Fix based on the Ravin's comment
1 parent f615052 commit 3659407

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+400
-309
lines changed

autoPyTorch/api/base_task.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def __init__(
195195
self.resampling_strategy = resampling_strategy
196196
self.resampling_strategy_args = resampling_strategy_args
197197

198-
self.stop_logging_server = None # type: Optional[multiprocessing.synchronize.Event]
198+
self.stop_logging_server: Optional[multiprocessing.synchronize.Event] = None
199199

200200
# Single core, local runs should use fork
201201
# to prevent the __main__ requirements in

autoPyTorch/data/base_feature_validator.py

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logging
2-
import typing
2+
from typing import List, Optional, Union
33

44
import numpy as np
55

@@ -12,8 +12,8 @@
1212
from autoPyTorch.utils.logging_ import PicklableClientLogger
1313

1414

15-
SUPPORTED_FEAT_TYPES = typing.Union[
16-
typing.List,
15+
SUPPORTED_FEAT_TYPES = Union[
16+
List,
1717
pd.DataFrame,
1818
np.ndarray,
1919
scipy.sparse.bsr_matrix,
@@ -29,54 +29,58 @@
2929
class BaseFeatureValidator(BaseEstimator):
3030
"""
3131
A class to pre-process features. In this regards, the format of the data is checked,
32-
and if applicable, features are encoded
32+
and if applicable, features are encoded.
3333
Attributes:
3434
feat_type (List[str]):
3535
List of the column types found by this estimator during fit.
3636
data_type (str):
3737
Class name of the data type provided during fit.
38+
column_transformer (Optional[BaseEstimator])
39+
Host a encoder object if the data requires transformation (for example,
40+
if provided a categorical column in a pandas DataFrame)
41+
transformed_columns (List[str])
42+
List of columns that were encoded.
3843
"""
39-
def __init__(self,
40-
logger: typing.Optional[typing.Union[PicklableClientLogger, logging.Logger
41-
]] = None,
42-
) -> None:
44+
def __init__(
45+
self,
46+
logger: Optional[Union[PicklableClientLogger, logging.Logger]] = None,
47+
):
4348
# Register types to detect unsupported data format changes
44-
self.feat_type = None # type: typing.Optional[typing.List[str]]
45-
self.data_type = None # type: typing.Optional[type]
46-
self.dtypes = [] # type: typing.List[str]
47-
self.column_order = [] # type: typing.List[str]
49+
self.feat_type: Optional[List[str]] = None
50+
self.data_type: Optional[type] = None
51+
self.dtypes: List[str] = []
52+
self.column_order: List[str] = []
4853

49-
self.column_transformer = None # type: typing.Optional[BaseEstimator]
50-
self.transformed_columns = [] # type: typing.List[str]
54+
self.column_transformer: Optional[BaseEstimator] = None
55+
self.transformed_columns: List[str] = []
5156

52-
self.logger: typing.Union[
57+
self.logger: Union[
5358
PicklableClientLogger, logging.Logger
5459
] = logger if logger is not None else logging.getLogger(__name__)
5560

5661
# Required for dataset properties
57-
self.num_features = None # type: typing.Optional[int]
58-
self.categories = [] # type: typing.List[typing.List[int]]
59-
60-
self.categorical_columns: typing.List[int] = []
61-
self.numerical_columns: typing.List[int] = []
62+
self.num_features: Optional[int] = None
63+
self.categories: List[List[int]] = []
64+
self.categorical_columns: List[int] = []
65+
self.numerical_columns: List[int] = []
6266

6367
self._is_fitted = False
6468

6569
def fit(
6670
self,
6771
X_train: SUPPORTED_FEAT_TYPES,
68-
X_test: typing.Optional[SUPPORTED_FEAT_TYPES] = None,
72+
X_test: Optional[SUPPORTED_FEAT_TYPES] = None,
6973
) -> BaseEstimator:
7074
"""
7175
Validates and fit a categorical encoder (if needed) to the features.
7276
The supported data types are List, numpy arrays and pandas DataFrames.
7377
CSR sparse data types are also supported
7478
75-
Arguments:
79+
Args:
7680
X_train (SUPPORTED_FEAT_TYPES):
7781
A set of features that are going to be validated (type and dimensionality
7882
checks) and a encoder fitted in the case the data needs encoding
79-
X_test (typing.Optional[SUPPORTED_FEAT_TYPES]):
83+
X_test (Optional[SUPPORTED_FEAT_TYPES]):
8084
A hold out set of data used for checking
8185
"""
8286

@@ -108,7 +112,7 @@ def _fit(
108112
X: SUPPORTED_FEAT_TYPES,
109113
) -> BaseEstimator:
110114
"""
111-
Arguments:
115+
Args:
112116
X (SUPPORTED_FEAT_TYPES):
113117
A set of features that are going to be validated (type and dimensionality
114118
checks) and a encoder fitted in the case the data needs encoding
@@ -123,7 +127,7 @@ def transform(
123127
X: SUPPORTED_FEAT_TYPES,
124128
) -> np.ndarray:
125129
"""
126-
Arguments:
130+
Args:
127131
X_train (SUPPORTED_FEAT_TYPES):
128132
A set of features, whose categorical features are going to be
129133
transformed

autoPyTorch/data/base_target_validator.py

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logging
2-
import typing
2+
from typing import List, Optional, Union, cast
33

44
import numpy as np
55

@@ -12,8 +12,8 @@
1212
from autoPyTorch.utils.logging_ import PicklableClientLogger
1313

1414

15-
SUPPORTED_TARGET_TYPES = typing.Union[
16-
typing.List,
15+
SUPPORTED_TARGET_TYPES = Union[
16+
List,
1717
pd.Series,
1818
pd.DataFrame,
1919
np.ndarray,
@@ -31,52 +31,55 @@ class BaseTargetValidator(BaseEstimator):
3131
"""
3232
A class to pre-process targets. It validates the data provided during fit (to make sure
3333
it matches AutoPyTorch expectation) as well as encoding the targets in case of classification
34+
3435
Attributes:
3536
is_classification (bool):
3637
A bool that indicates if the validator should operate in classification mode.
3738
During classification, the targets are encoded.
38-
encoder (typing.Optional[BaseEstimator]):
39+
encoder (Optional[BaseEstimator]):
3940
Host a encoder object if the data requires transformation (for example,
4041
if provided a categorical column in a pandas DataFrame)
41-
enc_columns (typing.List[str])
42+
enc_columns (List[str])
4243
List of columns that where encoded
4344
"""
4445
def __init__(self,
4546
is_classification: bool = False,
46-
logger: typing.Optional[typing.Union[PicklableClientLogger, logging.Logger
47-
]] = None,
48-
) -> None:
47+
logger: Optional[Union[PicklableClientLogger,
48+
logging.Logger
49+
]
50+
] = None,
51+
):
4952
self.is_classification = is_classification
5053

51-
self.data_type = None # type: typing.Optional[type]
54+
self.data_type: Optional[type] = None
5255

53-
self.encoder = None # type: typing.Optional[BaseEstimator]
56+
self.encoder: Optional[BaseEstimator] = None
5457

55-
self.out_dimensionality = None # type: typing.Optional[int]
56-
self.type_of_target = None # type: typing.Optional[str]
58+
self.out_dimensionality: Optional[int] = None
59+
self.type_of_target: Optional[str] = None
5760

58-
self.logger: typing.Union[
61+
self.logger: Union[
5962
PicklableClientLogger, logging.Logger
6063
] = logger if logger is not None else logging.getLogger(__name__)
6164

6265
# Store the dtype for remapping to correct type
63-
self.dtype = None # type: typing.Optional[type]
66+
self.dtype: Optional[type] = None
6467

6568
self._is_fitted = False
6669

6770
def fit(
6871
self,
6972
y_train: SUPPORTED_TARGET_TYPES,
70-
y_test: typing.Optional[SUPPORTED_TARGET_TYPES] = None,
73+
y_test: Optional[SUPPORTED_TARGET_TYPES] = None,
7174
) -> BaseEstimator:
7275
"""
7376
Validates and fit a categorical encoder (if needed) to the targets
7477
The supported data types are List, numpy arrays and pandas DataFrames.
7578
76-
Arguments:
79+
Args:
7780
y_train (SUPPORTED_TARGET_TYPES)
7881
A set of targets set aside for training
79-
y_test (typing.Union[SUPPORTED_TARGET_TYPES])
82+
y_test (Union[SUPPORTED_TARGET_TYPES])
8083
A hold out set of data used of the targets. It is also used to fit the
8184
categories of the encoder.
8285
"""
@@ -95,7 +98,7 @@ def fit(
9598
np.shape(y_test)
9699
))
97100
if isinstance(y_train, pd.DataFrame):
98-
y_test = typing.cast(pd.DataFrame, y_test)
101+
y_test = cast(pd.DataFrame, y_test)
99102
if y_train.columns.tolist() != y_test.columns.tolist():
100103
raise ValueError(
101104
"Train and test targets must both have the same columns, yet "
@@ -126,24 +129,24 @@ def fit(
126129
def _fit(
127130
self,
128131
y_train: SUPPORTED_TARGET_TYPES,
129-
y_test: typing.Optional[SUPPORTED_TARGET_TYPES] = None,
132+
y_test: Optional[SUPPORTED_TARGET_TYPES] = None,
130133
) -> BaseEstimator:
131134
"""
132-
Arguments:
135+
Args:
133136
y_train (SUPPORTED_TARGET_TYPES)
134137
The labels of the current task. They are going to be encoded in case
135138
of classification
136-
y_test (typing.Optional[SUPPORTED_TARGET_TYPES])
139+
y_test (Optional[SUPPORTED_TARGET_TYPES])
137140
A holdout set of labels
138141
"""
139142
raise NotImplementedError()
140143

141144
def transform(
142145
self,
143-
y: typing.Union[SUPPORTED_TARGET_TYPES],
146+
y: Union[SUPPORTED_TARGET_TYPES],
144147
) -> np.ndarray:
145148
"""
146-
Arguments:
149+
Args:
147150
y (SUPPORTED_TARGET_TYPES)
148151
A set of targets that are going to be encoded if the current task
149152
is classification
@@ -160,9 +163,10 @@ def inverse_transform(
160163
"""
161164
Revert any encoding transformation done on a target array
162165
163-
Arguments:
164-
y (typing.Union[np.ndarray, pd.DataFrame, pd.Series]):
166+
Args:
167+
y (Union[np.ndarray, pd.DataFrame, pd.Series]):
165168
Target array to be transformed back to original form before encoding
169+
166170
Returns:
167171
np.ndarray:
168172
The transformed array
@@ -176,6 +180,7 @@ def classes_(self) -> np.ndarray:
176180
which consist of a ndarray of shape (n_classes,)
177181
where n_classes are the number of classes seen while fitting
178182
a encoder to the targets.
183+
179184
Returns:
180185
classes_: np.ndarray
181186
The unique classes seen during encoding of a classifier

autoPyTorch/data/base_validator.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- encoding: utf-8 -*-
22
import logging.handlers
3-
import typing
3+
from typing import Optional, Tuple
44

55
import numpy as np
66

@@ -34,16 +34,16 @@ class BaseInputValidator(BaseEstimator):
3434
def __init__(
3535
self,
3636
is_classification: bool = False,
37-
logger_port: typing.Optional[int] = logging.handlers.DEFAULT_TCP_LOGGING_PORT,
37+
logger_port: Optional[int] = logging.handlers.DEFAULT_TCP_LOGGING_PORT,
3838
) -> None:
3939
raise NotImplementedError()
4040

4141
def fit(
4242
self,
4343
X_train: SUPPORTED_FEAT_TYPES,
4444
y_train: SUPPORTED_TARGET_TYPES,
45-
X_test: typing.Optional[SUPPORTED_FEAT_TYPES] = None,
46-
y_test: typing.Optional[SUPPORTED_TARGET_TYPES] = None,
45+
X_test: Optional[SUPPORTED_FEAT_TYPES] = None,
46+
y_test: Optional[SUPPORTED_TARGET_TYPES] = None,
4747
) -> BaseEstimator:
4848
"""
4949
Validates and fit a categorical encoder (if needed) to the features, and
@@ -58,14 +58,14 @@ def fit(
5858
+ Checks for dimensionality as well as missing values are performed.
5959
+ If performing a classification task, the data is going to be encoded
6060
61-
Arguments:
61+
Args:
6262
X_train (SUPPORTED_FEAT_TYPES):
6363
A set of features that are going to be validated (type and dimensionality
6464
checks). If this data contains categorical columns, an encoder is going to
6565
be instantiated and trained with this data.
6666
y_train (SUPPORTED_TARGET_TYPES):
6767
A set of targets that are going to be encoded if the task is for classification
68-
X_test (typing.Optional[SUPPORTED_FEAT_TYPES]):
68+
X_test (Optional[SUPPORTED_FEAT_TYPES]):
6969
A hold out set of features used for checking
7070
y_test (SUPPORTED_TARGET_TYPES):
7171
A hold out set of targets used for checking. Additionally, if the current task
@@ -97,15 +97,15 @@ def fit(
9797
def transform(
9898
self,
9999
X: SUPPORTED_FEAT_TYPES,
100-
y: typing.Optional[SUPPORTED_TARGET_TYPES] = None,
101-
) -> typing.Tuple[np.ndarray, typing.Optional[np.ndarray]]:
100+
y: Optional[SUPPORTED_TARGET_TYPES] = None,
101+
) -> Tuple[np.ndarray, Optional[np.ndarray]]:
102102
"""
103103
Transform the given target or features to a numpy array
104104
105-
Arguments:
105+
Args:
106106
X (SUPPORTED_FEAT_TYPES):
107107
A set of features to transform
108-
y (typing.Optional[SUPPORTED_TARGET_TYPES]):
108+
y (Optional[SUPPORTED_TARGET_TYPES]):
109109
A set of targets to transform
110110
111111
Returns:

0 commit comments

Comments
 (0)