Skip to content

Commit 00a6d16

Browse files
committed
Move utils
1 parent 416e1db commit 00a6d16

File tree

6 files changed

+50
-52
lines changed

6 files changed

+50
-52
lines changed

i18n/si/msgs.jaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
__init__.py:
22
def `networks`:
33
networks: false
4+
utils.py:
5+
def `items_from_distmatrix`:
6+
label: oznaka
7+
def `weights_from_distances`:
8+
All distances must be positive: Vse razdalje morajo biti pozitivne
49
network/base.py:
510
class `Network`:
611
def `__init__`:
@@ -758,11 +763,6 @@ widgets/ownxsinglemode.py:
758763
def `main`:
759764
/Users/janez/Downloads/100_petrozavodsk_171_events_no_sqrt.net: false
760765
__main__: false
761-
widgets/utils.py:
762-
def `items_from_distmatrix`:
763-
label: oznaka
764-
def `weights_from_distances`:
765-
All distances must be positive: Vse razdalje morajo biti pozitivne
766766
widgets/tests/utils.py:
767767
class `NetworkTest`:
768768
def `_read_network`:

orangecontrib/network/widgets/tests/test_utils.py renamed to orangecontrib/network/tests/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from Orange.misc import DistMatrix
66
from Orange.data import Table, Domain, ContinuousVariable
7-
from orangecontrib.network.widgets.utils import items_from_distmatrix, weights_from_distances
7+
from orangecontrib.network.utils import items_from_distmatrix, weights_from_distances
88

99

1010
class TestItemsFromMatrix(unittest.TestCase):

orangecontrib/network/utils.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import numpy as np
2+
from Orange.data import Table, StringVariable, Domain
3+
4+
5+
def items_from_distmatrix(matrix):
6+
if matrix.row_items is not None:
7+
row_items = matrix.row_items
8+
if isinstance(row_items, Table):
9+
if matrix.axis == 1:
10+
items = row_items
11+
else:
12+
items = [[v.name] for v in row_items.domain.attributes]
13+
else:
14+
items = [[str(x)] for x in matrix.row_items]
15+
else:
16+
items = [[str(i)] for i in range(1, 1 + matrix.shape[0])]
17+
if not isinstance(items, Table):
18+
items = Table.from_list(
19+
Domain([], metas=[StringVariable('label')]),
20+
items)
21+
return items
22+
23+
24+
def weights_from_distances(weights):
25+
weights = weights.astype(np.float64)
26+
27+
if weights.size == 0:
28+
return weights
29+
30+
mi, ma = np.nanmin(weights), np.nanmax(weights)
31+
assert mi >= 0, "All distances must be positive"
32+
33+
if ma - mi < 1e-6:
34+
return np.ones(weights.shape)
35+
36+
weights /= ma
37+
weights *= np.log(10)
38+
np.exp(weights, out=weights)
39+
np.reciprocal(weights, out=weights)
40+
41+
return weights

orangecontrib/network/widgets/OWNxFromDistances.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
from orangecontrib.network.network import Network
2020
from orangecontrib.network.network.base import UndirectedEdges, DirectedEdges
2121
# This enables summarize in widget preview, pylint: disable=unused-import
22-
import orangecontrib.network.widgets
23-
from orangecontrib.network.widgets.utils import items_from_distmatrix, weights_from_distances
22+
from orangecontrib.network.utils import items_from_distmatrix, weights_from_distances
2423

2524

2625
class QIntValidatorWithFixup(QIntValidator):

orangecontrib/network/widgets/OWNxNeighbor.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import numpy as np
2-
from numpy.lib.stride_tricks import as_strided
32
import scipy.sparse as sp
43

54
from AnyQt.QtCore import Qt
@@ -15,8 +14,7 @@
1514
from orangecontrib.network.network import Network
1615
from orangecontrib.network.network.base import UndirectedEdges, DirectedEdges
1716
# This enables summarize in widget preview, pylint: disable=unused-import
18-
import orangecontrib.network.widgets
19-
from orangecontrib.network.widgets.utils import items_from_distmatrix, weights_from_distances
17+
from orangecontrib.network.utils import items_from_distmatrix, weights_from_distances
2018

2119

2220
class OWNxNeighbor(widget.OWWidget):
Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1 @@
1-
import numpy as np
2-
from Orange.data import Table, StringVariable, Domain
3-
4-
5-
def items_from_distmatrix(matrix):
6-
if matrix.row_items is not None:
7-
row_items = matrix.row_items
8-
if isinstance(row_items, Table):
9-
if matrix.axis == 1:
10-
items = row_items
11-
else:
12-
items = [[v.name] for v in row_items.domain.attributes]
13-
else:
14-
items = [[str(x)] for x in matrix.row_items]
15-
else:
16-
items = [[str(i)] for i in range(1, 1 + matrix.shape[0])]
17-
if not isinstance(items, Table):
18-
items = Table.from_list(
19-
Domain([], metas=[StringVariable('label')]),
20-
items)
21-
return items
22-
23-
24-
def weights_from_distances(weights):
25-
weights = weights.astype(np.float64)
26-
27-
if weights.size == 0:
28-
return weights
29-
30-
mi, ma = np.nanmin(weights), np.nanmax(weights)
31-
assert mi >= 0, "All distances must be positive"
32-
33-
if ma - mi < 1e-6:
34-
return np.ones(weights.shape)
35-
36-
weights /= ma
37-
weights *= np.log(10)
38-
np.exp(weights, out=weights)
39-
np.reciprocal(weights, out=weights)
40-
41-
return weights
1+
from orangecontrib.network.utils import items_from_distmatrix, weights_from_distances

0 commit comments

Comments
 (0)