|
1
|
|
|
from itertools import chain |
|
2
|
|
|
from numbers import Real, Integral |
|
3
|
|
|
from ..data.value import Value, Unknown |
|
4
|
|
|
from math import isnan |
|
5
|
|
|
import numpy as np |
|
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
class Instance: |
|
9
|
|
|
def __init__(self, domain, data=None, id=None): |
|
|
|
|
|
|
10
|
|
|
""" |
|
11
|
|
|
Construct a new data instance. |
|
12
|
|
|
|
|
13
|
|
|
:param domain: domain that describes the instance's variables |
|
14
|
|
|
:type domain: Orange.data.Domain |
|
15
|
|
|
:param data: instance's values |
|
16
|
|
|
:type data: Orange.data.Instance or a sequence of values |
|
17
|
|
|
:param id: instance id |
|
18
|
|
|
:type id: hashable value |
|
19
|
|
|
""" |
|
20
|
|
|
if data is None and isinstance(domain, Instance): |
|
21
|
|
|
data = domain |
|
22
|
|
|
domain = data.domain |
|
23
|
|
|
|
|
24
|
|
|
self._domain = domain |
|
25
|
|
|
if data is None: |
|
26
|
|
|
self._x = np.repeat(Unknown, len(domain.attributes)) |
|
27
|
|
|
self._y = np.repeat(Unknown, len(domain.class_vars)) |
|
28
|
|
|
self._metas = np.array([var.Unknown for var in domain.metas], |
|
29
|
|
|
dtype=object) |
|
30
|
|
|
self._weight = 1 |
|
31
|
|
|
elif isinstance(data, Instance) and data.domain == domain: |
|
32
|
|
|
self._x = np.array(data._x) |
|
|
|
|
|
|
33
|
|
|
self._y = np.array(data._y) |
|
|
|
|
|
|
34
|
|
|
self._metas = np.array(data._metas) |
|
|
|
|
|
|
35
|
|
|
self._weight = data._weight |
|
|
|
|
|
|
36
|
|
|
else: |
|
37
|
|
|
self._x, self._y, self._metas = domain.convert(data) |
|
38
|
|
|
self._weight = 1 |
|
39
|
|
|
|
|
40
|
|
|
if id is not None: |
|
41
|
|
|
self.id = id |
|
42
|
|
|
else: |
|
43
|
|
|
from Orange.data import Table |
|
44
|
|
|
self.id = Table.new_id() |
|
45
|
|
|
|
|
46
|
|
|
@property |
|
47
|
|
|
def domain(self): |
|
48
|
|
|
"""The domain describing the instance's values.""" |
|
49
|
|
|
return self._domain |
|
50
|
|
|
|
|
51
|
|
|
@property |
|
52
|
|
|
def x(self): |
|
53
|
|
|
""" |
|
54
|
|
|
Instance's attributes as a 1-dimensional numpy array whose length |
|
55
|
|
|
equals `len(self.domain.attributes)`. |
|
56
|
|
|
""" |
|
57
|
|
|
return self._x |
|
58
|
|
|
|
|
59
|
|
|
@property |
|
60
|
|
|
def y(self): |
|
61
|
|
|
""" |
|
62
|
|
|
Instance's classes as a 1-dimensional numpy array whose length |
|
63
|
|
|
equals `len(self.domain.attributes)`. |
|
64
|
|
|
""" |
|
65
|
|
|
return self._y |
|
66
|
|
|
|
|
67
|
|
|
@property |
|
68
|
|
|
def metas(self): |
|
69
|
|
|
""" |
|
70
|
|
|
Instance's meta attributes as a 1-dimensional numpy array whose length |
|
71
|
|
|
equals `len(self.domain.attributes)`. |
|
72
|
|
|
""" |
|
73
|
|
|
return self._metas |
|
74
|
|
|
|
|
75
|
|
|
@property |
|
76
|
|
|
def list(self): |
|
77
|
|
|
""" |
|
78
|
|
|
All instance's values, including attributes, classes and meta |
|
79
|
|
|
attributes, as a list whose length equals `len(self.domain.attributes) |
|
80
|
|
|
+ len(self.domain.class_vars) + len(self.domain.metas)`. |
|
81
|
|
|
""" |
|
82
|
|
|
n_self, n_metas = len(self), len(self._metas) |
|
83
|
|
|
return [self[i].value if i < n_self else self[n_self - i - 1].value |
|
84
|
|
|
for i in range(n_self + n_metas)] |
|
85
|
|
|
|
|
86
|
|
|
@property |
|
87
|
|
|
def weight(self): |
|
88
|
|
|
"""The weight of the data instance. Default is 1.""" |
|
89
|
|
|
return self._weight |
|
90
|
|
|
|
|
91
|
|
|
@weight.setter |
|
92
|
|
|
def weight(self, weight): |
|
93
|
|
|
self._weight = weight |
|
94
|
|
|
|
|
95
|
|
|
def __setitem__(self, key, value): |
|
96
|
|
|
if not isinstance(key, Integral): |
|
97
|
|
|
key = self._domain.index(key) |
|
98
|
|
|
value = self._domain[key].to_val(value) |
|
99
|
|
|
if key >= 0 and not isinstance(value, (int, float)): |
|
100
|
|
|
raise TypeError("Expected primitive value, got '%s'" % |
|
101
|
|
|
type(value).__name__) |
|
102
|
|
|
|
|
103
|
|
|
if 0 <= key < len(self._domain.attributes): |
|
104
|
|
|
self._x[key] = value |
|
105
|
|
|
elif len(self._domain.attributes) <= key: |
|
106
|
|
|
self._y[key - len(self.domain.attributes)] = value |
|
107
|
|
|
else: |
|
108
|
|
|
self._metas[-1 - key] = value |
|
109
|
|
|
|
|
110
|
|
|
def __getitem__(self, key, key_id=None, key_var=None): |
|
111
|
|
|
# key_id can explicitly be given to prevent the extra dictionary |
|
112
|
|
|
# lookup. The derived LazyRowInstance passes the parameter, because |
|
113
|
|
|
# the key_id is already looked up there. key_var is included for |
|
114
|
|
|
# compatibility. |
|
115
|
|
|
if key_id is None: |
|
116
|
|
|
if not isinstance(key, Integral): |
|
117
|
|
|
key = self._domain.index(key) |
|
118
|
|
|
else: |
|
119
|
|
|
key = key_id |
|
120
|
|
|
|
|
121
|
|
|
if 0 <= key < len(self._domain.attributes): |
|
122
|
|
|
value = self._x[key] |
|
123
|
|
|
elif key >= len(self._domain.attributes): |
|
124
|
|
|
value = self._y[key - len(self.domain.attributes)] |
|
125
|
|
|
else: |
|
126
|
|
|
value = self._metas[-1 - key] |
|
127
|
|
|
return Value(self._domain[key], value) |
|
128
|
|
|
|
|
129
|
|
|
#TODO Should we return an instance of `object` if we have a meta attribute |
|
|
|
|
|
|
130
|
|
|
# that is not Discrete or Continuous? E.g. when we have strings, we'd |
|
131
|
|
|
# like to be able to use startswith, lower etc... |
|
132
|
|
|
# Or should we even return Continuous as floats and use Value only |
|
133
|
|
|
# for discrete attributes?! |
|
134
|
|
|
# Same in Table.__getitem__ |
|
135
|
|
|
|
|
136
|
|
|
@staticmethod |
|
137
|
|
|
def str_values(data, variables, limit=True): |
|
138
|
|
|
if limit: |
|
139
|
|
|
s = ", ".join(var.str_val(val) |
|
140
|
|
|
for var, val in zip(variables, data[:5])) |
|
141
|
|
|
if len(data) > 5: |
|
142
|
|
|
s += ", ..." |
|
143
|
|
|
return s |
|
144
|
|
|
else: |
|
145
|
|
|
return ", ".join(var.str_val(val) |
|
146
|
|
|
for var, val in zip(variables, data)) |
|
147
|
|
|
|
|
148
|
|
|
def _str(self, limit): |
|
149
|
|
|
s = "[" + self.str_values(self._x, self._domain.attributes, limit) |
|
150
|
|
|
if self._domain.class_vars: |
|
151
|
|
|
s += " | " + \ |
|
152
|
|
|
self.str_values(self._y, self._domain.class_vars, limit) |
|
153
|
|
|
s += "]" |
|
154
|
|
|
if self._domain.metas: |
|
155
|
|
|
s += " {" + \ |
|
156
|
|
|
self.str_values(self._metas, self._domain.metas, limit) + \ |
|
157
|
|
|
"}" |
|
158
|
|
|
return s |
|
159
|
|
|
|
|
160
|
|
|
def __str__(self): |
|
161
|
|
|
return self._str(False) |
|
162
|
|
|
|
|
163
|
|
|
def __repr__(self): |
|
164
|
|
|
return self._str(True) |
|
165
|
|
|
|
|
166
|
|
|
def __eq__(self, other): |
|
167
|
|
|
if not isinstance(other, Instance): |
|
168
|
|
|
other = Instance(self._domain, other) |
|
169
|
|
|
|
|
170
|
|
|
def same(x1, x2): |
|
171
|
|
|
nan1 = np.isnan(x1) |
|
172
|
|
|
nan2 = np.isnan(x2) |
|
173
|
|
|
return np.array_equal(nan1, nan2) and \ |
|
174
|
|
|
np.array_equal(x1[~nan1], x2[~nan2]) |
|
175
|
|
|
|
|
176
|
|
|
return same(self._x, other._x) and same(self._y, other._y) \ |
|
|
|
|
|
|
177
|
|
|
and all(m1 == m2 or |
|
178
|
|
|
type(m1) == type(m2) == float and isnan(m1) and isnan(m2) |
|
179
|
|
|
for m1, m2 in zip(self._metas, other._metas)) |
|
|
|
|
|
|
180
|
|
|
|
|
181
|
|
|
def __iter__(self): |
|
182
|
|
|
return chain(iter(self._x), iter(self._y)) |
|
183
|
|
|
|
|
184
|
|
|
def values(self): |
|
185
|
|
|
return (Value(var, val) |
|
186
|
|
|
for var, val in zip(self.domain.variables, self)) |
|
187
|
|
|
|
|
188
|
|
|
def __len__(self): |
|
189
|
|
|
return len(self._x) + len(self._y) |
|
190
|
|
|
|
|
191
|
|
|
def attributes(self): |
|
192
|
|
|
"""Return iterator over the instance's attributes""" |
|
193
|
|
|
return iter(self._x) |
|
194
|
|
|
|
|
195
|
|
|
def classes(self): |
|
196
|
|
|
"""Return iterator over the instance's class attributes""" |
|
197
|
|
|
return iter(self._y) |
|
198
|
|
|
|
|
199
|
|
|
# A helper function for get_class and set_class |
|
200
|
|
|
def _check_single_class(self): |
|
201
|
|
|
if not self._domain.class_vars: |
|
202
|
|
|
raise TypeError("Domain has no class variable") |
|
203
|
|
|
elif len(self._domain.class_vars) > 1: |
|
204
|
|
|
raise TypeError("Domain has multiple class variables") |
|
205
|
|
|
|
|
206
|
|
|
def get_class(self): |
|
207
|
|
|
""" |
|
208
|
|
|
Return the class value as an instance of :obj:`Orange.data.Value`. |
|
209
|
|
|
Throws an exception if there are multiple classes. |
|
210
|
|
|
""" |
|
211
|
|
|
self._check_single_class() |
|
212
|
|
|
return Value(self._domain.class_var, self._y[0]) |
|
213
|
|
|
|
|
214
|
|
|
def get_classes(self): |
|
215
|
|
|
""" |
|
216
|
|
|
Return the class value as a list of instances of |
|
217
|
|
|
:obj:`Orange.data.Value`. |
|
218
|
|
|
""" |
|
219
|
|
|
return (Value(var, value) |
|
220
|
|
|
for var, value in zip(self._domain.class_vars, self._y)) |
|
221
|
|
|
|
|
222
|
|
|
def set_class(self, value): |
|
223
|
|
|
""" |
|
224
|
|
|
Set the instance's class. Throws an exception if there are multiple |
|
225
|
|
|
classes. |
|
226
|
|
|
""" |
|
227
|
|
|
self._check_single_class() |
|
228
|
|
|
if not isinstance(value, Real): |
|
229
|
|
|
self._y[0] = self._domain.class_var.to_val(value) |
|
230
|
|
|
else: |
|
231
|
|
|
self._y[0] = value |
|
232
|
|
|
|
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.pyfiles in your module folders. Make sure that you place one file in each sub-folder.