forked from automl/Auto-PyTorch
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
337 lines (266 loc) · 11.6 KB
/
Copy pathutils.py
File metadata and controls
337 lines (266 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# Implementation used from https://github.com/automl/auto-sklearn/blob/development/autosklearn/util/data.py
import warnings
from math import floor
from typing import (
Any,
Dict,
Iterator,
List,
Mapping,
Optional,
Sequence,
Tuple,
Type,
Union,
cast
)
import numpy as np
import pandas as pd
from scipy.sparse import issparse, spmatrix
# TODO: TypedDict with python 3.8
#
# When upgrading to python 3.8 as minimum version, this should be a TypedDict
# so that mypy can identify the fields types
DatasetCompressionSpec = Dict[str, Union[int, float, List[str]]]
DatasetDTypeContainerType = Union[Type, Dict[str, Type]]
DatasetCompressionInputType = Union[np.ndarray, spmatrix, pd.DataFrame]
# Default specification for arg `dataset_compression`
default_dataset_compression_arg: DatasetCompressionSpec = {
"memory_allocation": 0.1,
"methods": ["precision"]
}
def get_dataset_compression_mapping(
memory_limit: int,
dataset_compression: Union[bool, Mapping[str, Any]]
) -> Optional[DatasetCompressionSpec]:
"""
Internal function to get value for `BaseTask._dataset_compression`
based on the value of `dataset_compression` passed.
If True, it returns the default_dataset_compression_arg. In case
of a mapping, it is validated and returned as a `DatasetCompressionSpec`.
If False, it returns None.
Args:
memory_limit (int):
memory limit of the current search.
dataset_compression (Union[bool, Mapping[str, Any]]):
mapping passed to the `search` function.
Returns:
Optional[DatasetCompressionSpec]:
Validated data compression spec or None.
"""
dataset_compression_mapping: Optional[Mapping[str, Any]] = None
if not isinstance(dataset_compression, bool):
dataset_compression_mapping = dataset_compression
elif dataset_compression:
dataset_compression_mapping = default_dataset_compression_arg
if dataset_compression_mapping is not None:
dataset_compression_mapping = validate_dataset_compression_arg(
dataset_compression_mapping, memory_limit=memory_limit)
return dataset_compression_mapping
def validate_dataset_compression_arg(
dataset_compression: Mapping[str, Any],
memory_limit: int
) -> DatasetCompressionSpec:
"""Validate and return a correct dataset_compression argument
The returned value can be safely used with `reduce_dataset_size_if_too_large`.
Args:
dataset_compression: Mapping[str, Any]
The argumnents to validate
Returns:
DatasetCompressionSpec
The validated and correct dataset compression spec
"""
if not isinstance(dataset_compression, Mapping):
raise ValueError(
f"Unknown type for `dataset_compression` {type(dataset_compression)}"
f"\ndataset_compression = {dataset_compression}"
)
# Fill with defaults if they don't exist
dataset_compression = {
**default_dataset_compression_arg,
**dataset_compression
}
# Must contain known keys
if set(dataset_compression.keys()) != set(default_dataset_compression_arg.keys()):
raise ValueError(
f"Unknown key in dataset_compression, {list(dataset_compression.keys())}."
f"\nPossible keys are {list(default_dataset_compression_arg.keys())}"
)
memory_allocation = dataset_compression["memory_allocation"]
# "memory_allocation" must be float or int
if not (isinstance(memory_allocation, float) or isinstance(memory_allocation, int)):
raise ValueError(
"key 'memory_allocation' must be an `int` or `float`"
f"\ntype = {memory_allocation}"
f"\ndataset_compression = {dataset_compression}"
)
# "memory_allocation" if absolute, should be > 0 and < memory_limit
if isinstance(memory_allocation, int) and not (0 < memory_allocation < memory_limit):
raise ValueError(
f"key 'memory_allocation' if int must be in (0, memory_limit={memory_limit})"
f"\nmemory_allocation = {memory_allocation}"
f"\ndataset_compression = {dataset_compression}"
)
# "memory_allocation" must be in (0,1) if float
if isinstance(memory_allocation, float):
if not (0.0 < memory_allocation < 1.0):
raise ValueError(
"key 'memory_allocation' if float must be in (0, 1)"
f"\nmemory_allocation = {memory_allocation}"
f"\ndataset_compression = {dataset_compression}"
)
# convert to int so we can directly use
dataset_compression["memory_allocation"] = floor(memory_allocation * memory_limit)
# "methods" must be non-empty sequence
if (
not isinstance(dataset_compression["methods"], Sequence)
or len(dataset_compression["methods"]) <= 0
):
raise ValueError(
"key 'methods' must be a non-empty list"
f"\nmethods = {dataset_compression['methods']}"
f"\ndataset_compression = {dataset_compression}"
)
# "methods" must contain known methods
if any(
method not in cast(Sequence, default_dataset_compression_arg["methods"]) # mypy
for method in dataset_compression["methods"]
):
raise ValueError(
f"key 'methods' can only contain {default_dataset_compression_arg['methods']}"
f"\nmethods = {dataset_compression['methods']}"
f"\ndataset_compression = {dataset_compression}"
)
return cast(DatasetCompressionSpec, dataset_compression)
class _DtypeReductionMapping(Mapping):
"""
Unfortuantly, mappings compare by hash(item) and not the __eq__ operator
between the key and the item.
Hence we wrap the dict in a Mapping class and implement our own __getitem__
such that we do use __eq__ between keys and query items.
>>> np.float32 == dtype('float32') # True, they are considered equal
>>>
>>> mydict = { np.float32: 'hello' }
>>>
>>> # Equal by __eq__ but dict operations fail
>>> np.dtype('float32') in mydict # False
>>> mydict[dtype('float32')] # KeyError
This mapping class fixes that supporting the `in` operator as well as `__getitem__`
>>> reduction_mapping = _DtypeReductionMapping()
>>>
>>> reduction_mapping[np.dtype('float64')] # np.float32
>>> np.dtype('float32') in reduction_mapping # True
"""
# Information about dtype support
_mapping: Dict[type, type] = {
np.float32: np.float32,
np.float64: np.float32,
np.int32: np.int32,
np.int64: np.int32
}
# In spite of the names, np.float96 and np.float128
# provide only as much precision as np.longdouble,
# that is, 80 bits on most x86 machines and 64 bits
# in standard Windows builds.
_mapping.update({getattr(np, s): np.float64 for s in ['float96', 'float128'] if hasattr(np, s)})
@classmethod
def __getitem__(cls, item: type) -> type:
for k, v in cls._mapping.items():
if k == item:
return v
raise KeyError(item)
@classmethod
def __iter__(cls) -> Iterator[type]:
return iter(cls._mapping.keys())
@classmethod
def __len__(cls) -> int:
return len(cls._mapping)
reduction_mapping = _DtypeReductionMapping()
supported_precision_reductions = list(reduction_mapping)
def reduce_precision(
X: DatasetCompressionInputType
) -> Tuple[DatasetCompressionInputType, DatasetDTypeContainerType, DatasetDTypeContainerType]:
""" Reduce the precision of a dataset containing floats or ints
Note:
For dataframe, the column's precision is reduced using pd.to_numeric.
Args:
X (DatasetCompressionInputType):
The data to reduce precision of.
Returns:
Tuple[DatasetCompressionInputType, DatasetDTypeContainerType, DatasetDTypeContainerType]
Returns the reduced data X along with the dtypes it and the dtypes it was reduced to.
"""
reduced_dtypes: Optional[DatasetDTypeContainerType] = None
if isinstance(X, np.ndarray) or issparse(X):
dtypes = X.dtype
if X.dtype not in supported_precision_reductions:
raise ValueError(f"X.dtype = {X.dtype} not equal to any supported"
f" {supported_precision_reductions}")
reduced_dtypes = reduction_mapping[X.dtype]
X = X.astype(reduced_dtypes)
elif hasattr(X, 'iloc'):
dtypes = dict(X.dtypes)
col_names = X.dtypes.index
float_cols = col_names[[dt.name.startswith("float") for dt in X.dtypes.values]]
int_cols = col_names[[dt.name.startswith("int") for dt in X.dtypes.values]]
X[int_cols] = X[int_cols].apply(lambda column: pd.to_numeric(column, downcast='integer'))
X[float_cols] = X[float_cols].apply(lambda column: pd.to_numeric(column, downcast='float'))
reduced_dtypes = dict(X.dtypes)
else:
raise ValueError(f"Unrecognised data type of X, expected data type to "
f"be in (np.ndarray, spmatrix, pd.DataFrame), but got :{type(X)}")
return X, reduced_dtypes, dtypes
def megabytes(arr: DatasetCompressionInputType) -> float:
if isinstance(arr, np.ndarray):
memory_in_bytes = arr.nbytes
elif issparse(arr):
memory_in_bytes = arr.data.nbytes
elif hasattr(arr, 'iloc'):
memory_in_bytes = arr.memory_usage(index=True, deep=True).sum()
else:
raise ValueError(f"Unrecognised data type of X, expected data type to "
f"be in (np.ndarray, spmatrix, pd.DataFrame) but got :{type(arr)}")
return float(memory_in_bytes / (2**20))
def reduce_dataset_size_if_too_large(
X: DatasetCompressionInputType,
memory_allocation: int,
methods: List[str] = ['precision'],
) -> DatasetCompressionInputType:
f""" Reduces the size of the dataset if it's too close to the memory limit.
Follows the order of the operations passed in and retains the type of its
input.
Precision reduction will only work on the following data types:
- {supported_precision_reductions}
Precision reduction will only perform one level of precision reduction.
Technically, you could supply multiple rounds of precision reduction, i.e.
to reduce np.float128 to np.float32 you could use `methods = ['precision'] * 2`.
However, if that's the use case, it'd be advised to simply use the function
`autoPyTorch.data.utils.reduce_precision`.
Args:
X: DatasetCompressionInputType
The features of the dataset.
methods: List[str] = ['precision']
A list of operations that are permitted to be performed to reduce
the size of the dataset.
**precision**
Reduce the precision of float types
memory_allocation: int
The amount of memory to allocate to the dataset. It should specify an
absolute amount.
Returns:
DatasetCompressionInputType
The reduced X if reductions were needed
"""
for method in methods:
if method == 'precision':
# If the dataset is too big for the allocated memory,
# we then try to reduce the precision if it's a high precision dataset
if megabytes(X) > memory_allocation:
X, reduced_dtypes, dtypes = reduce_precision(X)
warnings.warn(
f'Dataset too large for allocated memory {memory_allocation}MB, '
f'reduced the precision from {dtypes} to {reduced_dtypes}',
)
else:
raise ValueError(f"Unknown operation `{method}`")
return X