File: contingency.py

package info (click to toggle)
orange3 3.40.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,912 kB
  • sloc: python: 162,745; ansic: 622; makefile: 322; sh: 93; cpp: 77
file content (340 lines) | stat: -rw-r--r-- 13,462 bytes parent folder | download | duplicates (3)
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
338
339
340
import numpy as np

from Orange import data


def _get_variable(variable, dat, attr_name, expected_type=None, expected_name=""):
    failed = False
    if isinstance(variable, data.Variable):
        datvar = getattr(dat, "variable", None)
        if datvar is not None and datvar is not variable:
            raise ValueError("variable does not match the variable in the data")
    elif hasattr(dat, "domain"):
        variable = dat.domain[variable]
    elif hasattr(dat, attr_name):
        variable = dat.variable
    else:
        failed = True
    if failed or (expected_type is not None and not isinstance(variable, expected_type)):
        if not expected_type or isinstance(variable, data.Variable):
            raise ValueError("expected %s variable not %s" % (expected_name, variable))
        else:
            raise ValueError("expected %s, not '%s'" % (
                expected_type.__name__, type(variable).__name__))
    return variable


def _create_discrete(cls, *args):
    return cls(*args)


class Discrete(np.ndarray):
    def __new__(cls, dat, col_variable=None, row_variable=None,
                row_unknowns=None, col_unknowns=None, unknowns=None):
        if isinstance(dat, data.Storage):
            if unknowns is not None or row_unknowns is not None or \
                    col_unknowns is not None:
                raise TypeError(
                    "incompatible arguments (data storage and 'unknowns'")
            return cls.from_data(dat, col_variable, row_variable)

        if row_variable is not None:
            row_variable = _get_variable(row_variable, dat, "row_variable")
        if col_variable is not None:
            col_variable = _get_variable(col_variable, dat, "col_variable")

        self = super().__new__(cls, dat.shape)
        self.row_variable = row_variable
        self.col_variable = col_variable

        self.col_unknowns = np.zeros(dat.shape[0]) \
            if col_unknowns is None else col_unknowns
        self.row_unknowns = np.zeros(dat.shape[1]) \
            if col_unknowns is None else row_unknowns
        self.unknowns = unknowns or 0

        self[...] = dat

        return self

    @classmethod
    def from_data(cls, data, col_variable, row_variable=None):
        if row_variable is None:
            row_variable = data.domain.class_var
            if row_variable is None:
                raise ValueError(
                    "row_variable needs to be specified (data has no class)")
        row_variable = _get_variable(row_variable, data, "row_variable")
        col_variable = _get_variable(col_variable, data, "col_variable")
        try:
            dist, col_unknowns, row_unknowns, unknowns = \
                data._compute_contingency([col_variable], row_variable)[0]

            self = super().__new__(cls, dist.shape)
            self[...] = dist
            self.col_unknowns = col_unknowns
            self.row_unknowns = row_unknowns
            self.unknowns = unknowns
        except NotImplementedError:
            shape = len(row_variable.values), len(col_variable.values)
            self = super().__new__(cls, shape)
            self[...] = np.zeros(shape)
            self.col_unknowns = np.zeros(shape[0])
            self.row_unknowns = np.zeros(shape[1])
            self.unknowns = 0
            rind = data.domain.index(row_variable)
            cind = data.domain.index(col_variable)
            for row in data:
                rval, cval = row[rind], row[cind]
                w = row.weight
                if np.isnan(rval) and np.isnan(cval):
                    self.unknowns += w
                elif np.isnan(rval):
                    self.row_unknowns[int(cval)] += w
                elif np.isnan(cval):
                    self.col_unknowns[int(rval)] += w
                else:
                    self[int(rval), int(cval)] += w
        self.row_variable = row_variable
        self.col_variable = col_variable
        return self

    @property
    def array_with_unknowns(self):
        """
        This property returns a contingency array with unknowns arrays added
        as a column and row.

        Returns
        -------
        np.array
            Array with concatenated unknowns as a row and column
        """
        return np.vstack(
            (np.hstack((np.array(self), self.col_unknowns.reshape(-1, 1))),
             np.append(self.row_unknowns, self.unknowns)))

    def __eq__(self, other):
        return (
            np.array_equal(self, other) and
            (not hasattr(other, "col_unknowns") or
             np.array_equal(self.col_unknowns, other.col_unknowns)) and
            (not hasattr(other, "row_unknowns") or
             np.array_equal(self.row_unknowns, other.row_unknowns)) and
            (not hasattr(other, "unknowns") or
             self.unknowns == other.unknowns)
        )

    def __getitem__(self, index):
        if isinstance(index, str):
            if len(self.shape) == 2:  # contingency
                index = self.row_variable.to_val(index)
                contingency_row = super().__getitem__(index)
                contingency_row.col_variable = self.col_variable
                return contingency_row
            else:  # Contingency row
                column = self.strides == self.base.strides[:1]
                if column:
                    index = self.row_variable.to_val(index)
                else:
                    index = self.col_variable.to_val(index)

        elif isinstance(index, tuple):
            if isinstance(index[0], str):
                index = (self.row_variable.to_val(index[0]), index[1])
            if isinstance(index[1], str):
                index = (index[0], self.col_variable.to_val(index[1]))
        result = super().__getitem__(index)
        if isinstance(result, Discrete):
            if not isinstance(index, tuple):
                result.col_unknowns = self.col_unknowns[index]
                result.row_unknowns = self.row_unknowns
            else:
                result.col_unknowns = self.col_unknowns[index[0]]
                result.row_unknowns = self.col_unknowns[index[1]]
            result.unknowns = self.unknowns
        if result.strides:
            result.col_variable = self.col_variable
            result.row_variable = self.row_variable
        return result

    def __setitem__(self, index, value):
        if isinstance(index, str):
            index = self.row_variable.to_val(index)
        elif isinstance(index, tuple):
            if isinstance(index[0], str):
                index = (self.row_variable.to_val(index[0]), index[1])
            if isinstance(index[1], str):
                index = (index[0], self.col_variable.to_val(index[1]))
        super().__setitem__(index, value)

    def normalize(self, axis=None):
        t = np.sum(self, axis=axis)
        if t > 1e-6:
            self[:] /= t
            if axis is None or axis == 1:
                self.unknowns /= t

    def __reduce__(self):
        return (
            _create_discrete,
            (Discrete, np.copy(self), self.col_variable, self.row_variable,
             self.col_unknowns, self.row_unknowns, self.unknowns)
        )

    def __array_finalize__(self, obj):
        # defined in __new__, pylint: disable=attribute-defined-outside-init
        """See http://docs.scipy.org/doc/numpy/user/basics.subclassing.html"""
        if obj is None:
            return
        self.col_variable = getattr(obj, 'col_variable', None)
        self.row_variable = getattr(obj, 'row_variable', None)
        self.col_unknowns = getattr(obj, 'col_unknowns', None)
        self.row_unknowns = getattr(obj, 'row_unknowns', None)
        self.unknowns = getattr(obj, 'unknowns', None)


class Continuous:
    def __init__(self, dat, col_variable=None, row_variable=None,
                 col_unknowns=None, row_unknowns=None, unknowns=None):
        if isinstance(dat, data.Storage):
            if unknowns is not None or row_unknowns is not None or \
                    col_unknowns is not None:
                raise TypeError(
                    "incompatible arguments (data storage and 'unknowns'")
            self.from_data(dat, col_variable, row_variable)
            return

        if row_variable is not None:
            row_variable = _get_variable(row_variable, dat, "row_variable")
        if col_variable is not None:
            col_variable = _get_variable(col_variable, dat, "col_variable")

        self.values, self.counts = dat

        self.row_variable = row_variable
        self.col_variable = col_variable
        self.col_unknowns = np.zeros(self.counts.shape[1]) \
            if col_unknowns is None else col_unknowns
        self.row_unknowns = np.zeros(self.counts.shape[0]) \
            if row_unknowns is None else row_unknowns
        self.unknowns = unknowns or 0

    def from_data(self, data, col_variable, row_variable=None):
        if row_variable is None:
            row_variable = data.domain.class_var
            if row_variable is None:
                raise ValueError("row_variable needs to be specified (data has no class)")
        self.row_variable = _get_variable(row_variable, data, "row_variable")
        self.col_variable = _get_variable(col_variable, data, "col_variable")
        try:
            conts = data._compute_contingency([col_variable], row_variable)
            (self.values, self.counts), self.col_unknowns, self.row_unknowns, \
            self.unknowns = conts[0]
        except NotImplementedError:
            raise NotImplementedError(
                "Fallback method for computation of contingencies is not implemented yet"
            )

    @property
    def array_with_unknowns(self):
        """
        This function returns the list of all items returned by __getitem__
        with adding a row of row_unknowns together with values.
        """
        # pylint: disable=unnecessary-comprehension
        other_rows = [x for x in self]
        ind = self.row_unknowns > 0
        unknown_rows = np.vstack((self.values[ind], self.row_unknowns[ind]))
        return other_rows + [unknown_rows]

    def __eq__(self, other):
        return (
            np.array_equal(self.values, other.values) and
            np.array_equal(self.counts, other.counts) and
            (not hasattr(other, "col_unknowns") or
             np.array_equal(self.col_unknowns, other.col_unknowns)) and
            (not hasattr(other, "row_unknowns") or
             np.array_equal(self.row_unknowns, other.row_unknowns)) and
            (not hasattr(other, "unknowns") or
             self.unknowns == other.unknowns)
        )

    def __getitem__(self, index):
        """ Return contingencies for a given class value. """
        if isinstance(index, (str, float)):
            index = self.row_variable.to_val(index)
        C = self.counts[index]
        ind = C > 0
        return np.vstack((self.values[ind], C[ind]))

    def __len__(self):
        return self.counts.shape[0]

    def __setitem__(self, index, value):
        raise NotImplementedError(
            "Setting individual class contingencies is not implemented yet. "
            "Set .values and .counts."
        )

    def normalize(self, axis=None):
        if axis is None:
            t = sum(np.sum(x[:, 1]) for x in self)
            if t > 1e-6:
                for x in self:
                    x[:, 1] /= t
        elif axis != 1:
            raise ValueError(
                "contingencies can be normalized only with axis=1 or without axis"
            )
        else:
            for i, x in enumerate(self):
                t = np.sum(x[:, 1])
                if t > 1e-6:
                    x[:, 1] /= t
                    self.unknowns[i] /= t
                else:
                    if self.unknowns[i] > 1e-6:
                        self.unknowns[i] = 1


def get_contingency(dat, col_variable, row_variable=None, col_unknowns=None,
                    row_unknowns=None, unks=None):
    variable = _get_variable(col_variable, dat, "col_variable")
    if variable.is_discrete:
        return Discrete(
            dat, col_variable, row_variable, col_unknowns, row_unknowns, unks)
    elif variable.is_continuous:
        return Continuous(
            dat, col_variable, row_variable, col_unknowns, row_unknowns)
    else:
        raise TypeError(
            "cannot compute distribution of '%s'" % type(variable).__name__)


def get_contingencies(dat, skip_discrete=False, skip_continuous=False):
    vars = dat.domain.attributes
    row_var = dat.domain.class_var
    if row_var is None:
        raise ValueError("data has no target variable")
    if skip_discrete:
        if skip_continuous:
            return []
        columns = [i for i, var in enumerate(vars) if var.is_continuous]
    elif skip_continuous:
        columns = [i for i, var in enumerate(vars) if var.is_discrete]
    else:
        columns = None
    try:
        dist_unks = dat._compute_contingency(columns)
        if columns is None:
            columns = np.arange(len(vars))
        contigs = []
        for col, (cont, col_unk, row_unk, unks) in zip(columns, dist_unks):
            contigs.append(get_contingency(
                cont, vars[col], row_var, col_unk, row_unk, unks))
    except NotImplementedError:
        if columns is None:
            columns = range(len(vars))
        contigs = [get_contingency(dat, i) for i in columns]
    return contigs