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
|
from typing import TYPE_CHECKING, Mapping, Optional
import numpy as np
import scipy.sparse as sp
from pandas import isna
from Orange.data import Instance, Table, Domain, Variable
from Orange.misc.collections import DictMissingConst
from Orange.util import Reprable, nan_eq, nan_hash_stand, frompyfunc
if TYPE_CHECKING:
from numpy.typing import DTypeLike
class Transformation(Reprable):
"""
Base class for simple transformations of individual variables. Derived
classes are used in continuization, imputation, discretization...
"""
def __init__(self, variable):
"""
:param variable: The variable whose transformed value is returned.
:type variable: int or str or :obj:`~Orange.data.Variable`
"""
self.variable = variable
self._create_cached_target_domain()
def _create_cached_target_domain(self):
""" If the same domain is used everytime this allows better caching of
domain transformations in from_table"""
if self.variable is not None:
if self.variable.is_primitive():
self._target_domain = Domain([self.variable])
else:
self._target_domain = Domain([], metas=[self.variable])
def __getstate__(self):
# Do not pickle the cached domain; rather recreate it after unpickling
state = self.__dict__.copy()
state.pop("_target_domain")
return state
def __setstate__(self, state):
# Ensure that cached target domain is created after unpickling.
# This solves the problem of unpickling old pickled models.
self.__dict__.update(state)
self._create_cached_target_domain()
def __call__(self, data):
"""
Return transformed column from the data by extracting the column view
from the data and passing it to the `transform` method.
"""
inst = isinstance(data, Instance)
if inst:
data = Table.from_list(data.domain, [data])
data = data.transform(self._target_domain)
if self.variable.is_primitive():
col = data.X
else:
col = data.metas
if not sp.issparse(col) and col.ndim > 1:
col = col.squeeze(axis=1)
transformed = self.transform(col)
if inst:
transformed = transformed[0]
return transformed
def transform(self, c):
"""
Return the transformed value of the argument `c`, which can be a number
of a vector view.
"""
raise NotImplementedError(
"ColumnTransformations must implement method 'transform'.")
def __eq__(self, other):
return type(other) is type(self) and self.variable == other.variable
def __hash__(self):
return hash((type(self), self.variable))
class Identity(Transformation):
"""Return an untransformed value of `c`.
"""
InheritEq = True
def transform(self, c):
return c
def __eq__(self, other): # pylint: disable=useless-parent-delegation
return super().__eq__(other)
def __hash__(self):
return super().__hash__()
# pylint: disable=abstract-method
class _Indicator(Transformation):
def __init__(self, variable, value):
"""
:param variable: The variable whose transformed value is returned.
:type variable: int or str or :obj:`~Orange.data.Variable`
:param value: The value to which the indicator refers
:type value: int or float
"""
super().__init__(variable)
self.value = value
def __eq__(self, other):
return super().__eq__(other) and self.value == other.value
def __hash__(self):
return hash((type(self), self.variable, self.value))
@staticmethod
def _nan_fixed(c, transformed):
if np.isscalar(c):
if c != c: # pylint: disable=comparison-with-itself
transformed = np.nan
else:
transformed = float(transformed)
else:
transformed = transformed.astype(float)
transformed[np.isnan(c)] = np.nan
return transformed
class Indicator(_Indicator):
"""
Return an indicator value that equals 1 if the variable has the specified
value and 0 otherwise.
"""
InheritEq = True
def transform(self, c):
if sp.issparse(c):
if self.value != 0:
# If value is nonzero, the matrix will become sparser:
# we transform the data and remove zeros
transformed = c.copy()
transformed.data = self.transform(c.data)
transformed.eliminate_zeros()
return transformed
else:
# Otherwise, it becomes dense anyway (or it wasn't really sparse
# before), so we just convert it to sparse before transforming
c = c.toarray().ravel()
return self._nan_fixed(c, c == self.value)
def __eq__(self, other): # pylint: disable=useless-parent-delegation
return super().__eq__(other)
def __hash__(self):
return super().__hash__()
class Indicator1(_Indicator):
"""
Return an indicator value that equals 1 if the variable has the specified
value and -1 otherwise.
"""
InheritEq = True
def transform(self, column):
# The result of this is always dense
if sp.issparse(column):
column = column.toarray().ravel()
return self._nan_fixed(column, (column == self.value) * 2 - 1)
class Normalizer(Transformation):
"""
Return a normalized variable; for the given `value`, the transformed value
if `(value - self.offset) * self.factor`.
"""
def __init__(self, variable, offset, factor):
"""
:param variable: The variable whose transformed value is returned.
:type variable: int or str or :obj:`~Orange.data.Variable`
:param offset:
:type offset: float
:param factor:
:type factor: float
"""
super().__init__(variable)
self.offset = offset
self.factor = factor
def transform(self, c):
if sp.issparse(c):
if self.offset != 0:
raise ValueError('Normalization does not work for sparse data.')
return c * self.factor
else:
return (c - self.offset) * self.factor
def __eq__(self, other):
return super().__eq__(other) \
and self.offset == other.offset and self.factor == other.factor
def __hash__(self):
return hash((type(self), self.variable, self.offset, self.factor))
class Lookup(Transformation):
"""
Transform a discrete variable according to lookup table (`self.lookup`).
"""
def __init__(self, variable, lookup_table, unknown=np.nan):
"""
:param variable: The variable whose transformed value is returned.
:type variable: int or str or :obj:`~Orange.data.DiscreteVariable`
:param lookup_table: transformations for each value of `self.variable`
:type lookup_table: np.array
:param unknown: The value to be used as unknown value.
:type unknown: float or int
"""
super().__init__(variable)
self.lookup_table = lookup_table
self.unknown = unknown
def transform(self, column):
# Densify DiscreteVariable values coming from sparse datasets.
if sp.issparse(column):
column = column.toarray().ravel()
mask = np.isnan(column)
column = column.astype(int)
column[mask] = 0
values = self.lookup_table[column]
return np.where(mask, self.unknown, values)
def __eq__(self, other):
return super().__eq__(other) \
and np.allclose(self.lookup_table, other.lookup_table,
equal_nan=True) \
and np.allclose(self.unknown, other.unknown, equal_nan=True)
def __hash__(self):
return hash(
(
type(self),
self.variable,
# nan value does not have constant hash in Python3.10
# to avoid different hashes for the same array change to None
# issue: https://bugs.python.org/issue43475#msg388508
tuple(None if isna(x) else x for x in self.lookup_table),
nan_hash_stand(self.unknown),
)
)
class MappingTransform(Transformation):
"""
Map values via a dictionary lookup.
Parameters
----------
variable: Variable
mapping: Mapping
The mapping (for the non NA values).
dtype: Optional[DTypeLike]
The optional target dtype.
unknown: Any
The constant with whitch to replace unknown values in input.
"""
def __init__(
self,
variable: Variable,
mapping: Mapping,
dtype: Optional['DTypeLike'] = None,
unknown=np.nan,
) -> None:
super().__init__(variable)
if any(nan_eq(k, np.nan) for k in mapping.keys()): # ill-defined mapping
raise ValueError("'nan' value in mapping.keys()")
self.mapping = mapping
self.dtype = dtype
self.unknown = unknown
self._mapper = self._make_dict_mapper(
DictMissingConst(unknown, mapping), dtype
)
@staticmethod
def _make_dict_mapper(mapping, dtype):
return frompyfunc(mapping.__getitem__, 1, 1, dtype)
def transform(self, c):
return self._mapper(c)
def __reduce_ex__(self, protocol):
return type(self), (self.variable, self.mapping, self.dtype, self.unknown)
def __eq__(self, other):
return super().__eq__(other) \
and nan_mapping_eq(self.mapping, other.mapping) \
and self.dtype == other.dtype \
and nan_eq(self.unknown, other.unknown)
def __hash__(self):
return hash((type(self), self.variable, nan_mapping_hash(self.mapping),
self.dtype, nan_hash_stand(self.unknown)))
def nan_mapping_hash(a: Mapping) -> int:
return hash(tuple((k, nan_hash_stand(v)) for k, v in a.items()))
def nan_mapping_eq(a: Mapping, b: Mapping) -> bool:
if len(a) != len(b):
return False
try:
for k, va in a.items():
vb = b[k]
if not nan_eq(va, vb):
return False
except LookupError:
return False
return True
|