Skip to content

Commit bfc3714

Browse files
committed
distances: Lint
1 parent 77e02db commit bfc3714

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

Orange/distance/__init__.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,12 @@ def _orange_to_numpy(x):
6060
elif isinstance(x, np.ndarray):
6161
return np.atleast_2d(x)
6262
else:
63-
return x # e.g. None
63+
return x # e.g. None
6464

6565

6666
class Distance:
67+
# Argument types in docstrings must be in a single line(?), hence
68+
# pylint: disable=line-too-long
6769
"""
6870
Base class for construction of distances models (:obj:`DistanceModel`).
6971
@@ -101,6 +103,9 @@ class Distance:
101103
axis (int):
102104
axis over which the distances are computed, 1 (default) for
103105
rows, 0 for columns
106+
impute (bool):
107+
if `True` (default is `False`), nans in the computed distances
108+
are replaced with zeros, and infs with very large numbers.
104109
105110
Attributes:
106111
axis (int):
@@ -392,6 +397,7 @@ def fit_rows(self, attributes, x, n_vals):
392397
curr_cont += 1
393398
else:
394399
continuous[col] = False
400+
# pylint: disable=not-callable
395401
return self.rows_model_type(
396402
attributes, impute, getattr(self, "normalize", False),
397403
continuous, discrete,
@@ -764,8 +770,8 @@ def fit_rows(self, attributes, x, n_vals):
764770
"""
765771

766772
ps = np.fromiter(
767-
(_distance.p_nonzero(x[:, col]) for col in range(len(n_vals))),
768-
dtype=np.double, count=len(n_vals))
773+
(_distance.p_nonzero(x[:, col]) for col in range(len(n_vals))),
774+
dtype=np.double, count=len(n_vals))
769775
return JaccardModel(attributes, self.axis, self.impute, ps)
770776

771777
fit_cols = fit_rows

Orange/distance/tests/test_distance.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import unittest
2+
from math import sqrt
23

34
import numpy as np
4-
from math import sqrt
55

66
from Orange.data import ContinuousVariable, DiscreteVariable, Domain, Table
77
from Orange import distance
@@ -206,8 +206,7 @@ def test_euclidean_disc(self):
206206
np.sqrt(np.array([[0, 2.5, 2.5, 2.5],
207207
[2.5, 0, 0.5, 1.5],
208208
[2.5, 0.5, 0, 2],
209-
[2.5, 1.5, 2, 0],
210-
])))
209+
[2.5, 1.5, 2, 0]])))
211210

212211
def test_euclidean_cont(self):
213212
assert_almost_equal = np.testing.assert_almost_equal
@@ -247,7 +246,7 @@ def test_euclidean_cont_normalized(self):
247246
assert_almost_equal(model.means, [2, 2.75, 1.5])
248247
assert_almost_equal(model.vars, [9, 2.1875, 1.25])
249248
assert_almost_equal(model.dist_missing2_cont, [1, 1, 1])
250-
return
249+
251250
dist = model(data)
252251
assert_almost_equal(
253252
dist,
@@ -266,9 +265,8 @@ def test_euclidean_cont_normalized(self):
266265

267266
data.X[1, 0] = np.nan
268267
model = distance.Euclidean(axis=1, normalize=True).fit(data)
269-
params = model.fit_params
270-
assert_almost_equal(params["means"], [3, 2.75, 1.5])
271-
assert_almost_equal(params["vars"], [8, 2.1875, 1.25])
268+
assert_almost_equal(model.means, [3, 2.75, 1.5])
269+
assert_almost_equal(model.vars, [8, 2.1875, 1.25])
272270
dist = model(data)
273271
assert_almost_equal(
274272
dist,
@@ -279,9 +277,8 @@ def test_euclidean_cont_normalized(self):
279277

280278
data.X[0, 0] = np.nan
281279
model = distance.Euclidean(axis=1, normalize=True).fit(data)
282-
params = model.fit_params
283-
assert_almost_equal(params["means"], [4, 2.75, 1.5])
284-
assert_almost_equal(params["vars"], [9, 2.1875, 1.25])
280+
assert_almost_equal(model.means, [4, 2.75, 1.5])
281+
assert_almost_equal(model.vars, [9, 2.1875, 1.25])
285282
dist = model(data)
286283
assert_almost_equal(
287284
dist,
@@ -441,7 +438,7 @@ def test_manhattan_disc(self):
441438
assert_almost_equal(model.dist_missing_disc,
442439
[[1/2, 1/2, 1, 1],
443440
[2/3, 2/3, 1, 2/3],
444-
[2/3, 1/3, 1, 1 ]])
441+
[2/3, 1/3, 1, 1]])
445442
assert_almost_equal(model.dist_missing2_disc,
446443
[1 - 2/4, 1 - 3/9, 1 - 5/9])
447444

@@ -600,7 +597,7 @@ def test_manhattan_cols_normalized(self):
600597
assert_almost_equal(
601598
dist,
602599
[[0, 4.5833333, 2],
603-
[4.5833333, 0, 4.25],
600+
[4.5833333, 0, 4.25],
604601
[2, 4.25, 0]])
605602

606603
data.X[1, 1] = np.nan
@@ -800,8 +797,8 @@ def test_cosine_cols(self):
800797
assert_almost_equal(
801798
dist,
802799
[[0, 0.47702364, 0.11050082],
803-
[0.47702364, 0, 0.181076975],
804-
[0.11050082, 0.181076975, 0]])
800+
[0.47702364, 0, 0.181076975],
801+
[0.11050082, 0.181076975, 0]])
805802

806803
data.X[1, 0] = np.nan
807804
data.X[1, 2] = 2
@@ -842,6 +839,7 @@ def test_jaccard_rows(self):
842839
model = distance.Jaccard().fit(self.data)
843840
assert_almost_equal(model.ps, np.array([0.5, 2/3, 0.75]))
844841

842+
# pylint: disable=bad-whitespace
845843
assert_almost_equal(
846844
model(self.data),
847845
1 - np.array([[ 1, 2 / 2.5, 1 / 2.5, 2/3 / 3],

0 commit comments

Comments
 (0)