chances 0.1.9__py2.py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
chances/__init__.py ADDED
@@ -0,0 +1,5 @@
1
+ from .methods import Randomizer
2
+ from .utils import *
3
+
4
+
5
+ __version__ = "0.1.9"
@@ -0,0 +1,11 @@
1
+ """
2
+ diversipy is a collection of algorithms for sampling in hypercubes,
3
+ selecting diverse subsets, and measuring diversity.
4
+
5
+ For further documentation see the submodules.
6
+ """
7
+
8
+ from .hycusampling import *
9
+ from .subset import *
10
+ from .indicator import *
11
+ from .distance import *
@@ -0,0 +1,163 @@
1
+ """
2
+ This module provides some basic distance measures and a class to instantiate
3
+ arbitrary distance functions based on Minkowski metrics. For more exotic
4
+ distance functions (and faster implementations) have a look at
5
+ :mod:`scipy.spatial.distance`.
6
+ """
7
+ import math
8
+
9
+ import numpy as np
10
+
11
+
12
+
13
+ def calc_dists_to_boundary(points, cuboid=None):
14
+ """Calculate the distance of each point to the boundary of some cuboid.
15
+
16
+ This distance is simply the minimum of all differences between
17
+ a point and the lower and upper bounds. This function also checks if all
18
+ calculated distances are larger than zero. If not, some points must be
19
+ located outside the cuboid.
20
+
21
+ Parameters
22
+ ----------
23
+ points : array_like
24
+ 2-D array of `n` points.
25
+ cuboid : tuple of array_like, optional
26
+ Contains the min and max bounds of the considered cuboid. If
27
+ omitted, the unit hypercube is assumed.
28
+
29
+ Returns
30
+ -------
31
+ distances : numpy array
32
+ 1-D array of `n` distances
33
+
34
+ """
35
+ if cuboid is None:
36
+ dists_to_min_bounds = points.min(axis=1)
37
+ dists_to_max_bounds = (1.0 - points).min(axis=1)
38
+ else:
39
+ min_bounds, max_bounds = cuboid
40
+ dists_to_min_bounds = (points - np.asarray(min_bounds)).min(axis=1)
41
+ dists_to_max_bounds = (np.asarray(max_bounds) - points).min(axis=1)
42
+ distances = np.minimum(dists_to_min_bounds, dists_to_max_bounds)
43
+ assert np.all(distances >= 0.0) # are all points contained in cuboid?
44
+ return distances
45
+
46
+
47
+
48
+ def calc_euclidean_dist_matrix(points1, points2):
49
+ """Calculate Euclidean distance matrix between `points1` and `points2`.
50
+
51
+ Generates one column of the matrix at a time.
52
+
53
+ Parameters
54
+ ----------
55
+ points1 : array_like
56
+ 2-D array of n points.
57
+ points2 : array_like
58
+ 2-D array of m points.
59
+
60
+ Returns
61
+ -------
62
+ distances : (n,m) numpy array
63
+
64
+ """
65
+ num_points1, dim1 = points1.shape
66
+ num_points2, dim2 = points2.shape
67
+ assert dim1 == dim2
68
+ distances = np.zeros((num_points1, num_points2))
69
+ for i in range(dim1):
70
+ diff2 = points1[:, i, None] - points2[:, i]
71
+ diff2 **= 2
72
+ distances += diff2
73
+ # in place sqrt
74
+ np.sqrt(distances, distances)
75
+ return distances
76
+
77
+
78
+
79
+ def calc_manhattan_dist_matrix(points1, points2):
80
+ """Calculate Manhattan distance matrix between `points1` and `points2`.
81
+
82
+ Generates one column of the matrix at a time.
83
+
84
+ Parameters
85
+ ----------
86
+ points1 : array_like
87
+ 2-D array of n points.
88
+ points2 : array_like
89
+ 2-D array of m points.
90
+
91
+ Returns
92
+ -------
93
+ distances : (n,m) numpy array
94
+
95
+ """
96
+ num_points1, dim1 = points1.shape
97
+ num_points2, dim2 = points2.shape
98
+ assert dim1 == dim2
99
+ distances = np.zeros((num_points1, num_points2))
100
+ for i in range(dim1):
101
+ diff = np.abs(points1[:, i, None] - points2[:, i])
102
+ distances += diff
103
+ return distances
104
+
105
+
106
+
107
+ class DistanceMatrixFunction:
108
+ """General distance function.
109
+
110
+ This distance function can handle arbitrary exponents and can optionally
111
+ calculate torus distances. Slightly slower than the specialized
112
+ versions. Special cases ``exponent = 1`` and ``exponent = 2`` correspond
113
+ to Manhattan and Euclidean distance, respectively.
114
+
115
+ """
116
+ def __init__(self, exponent=2, max_dists_per_dim=None):
117
+ """Constructor.
118
+
119
+ Parameters
120
+ ----------
121
+ exponent : scalar, optional
122
+ The exponent in the distance calculation.
123
+ max_dists_per_dim : array_like, optional
124
+ 1-D array of largest possible distance in each dimension.
125
+ Providing these values has the consequence of treating the
126
+ cuboid as a torus. This is useful for eliminating edge effects
127
+ induced by the lack of neighbor points outside the bounds of
128
+ the cuboid.
129
+
130
+ """
131
+ self.exponent = exponent
132
+ self.max_dists_per_dim = max_dists_per_dim
133
+ if max_dists_per_dim is not None:
134
+ for max_dist in max_dists_per_dim:
135
+ assert max_dist > 0 or max_dist is None
136
+
137
+
138
+ def __call__(self, points1, points2):
139
+ """Calculate distance matrix, one column at a time."""
140
+ exponent = self.exponent
141
+ max_dists_per_dim = self.max_dists_per_dim
142
+ num_points1, dim1 = points1.shape
143
+ num_points2, dim2 = points2.shape
144
+ assert dim1 == dim2
145
+ distances = np.zeros((num_points1, num_points2))
146
+ if math.isinf(exponent):
147
+ for i in range(dim1):
148
+ diff = np.abs(points1[:, i, None] - points2[:, i], dtype="d")
149
+ if max_dists_per_dim is not None:
150
+ np.minimum(max_dists_per_dim[i] - diff, diff, out=diff)
151
+ assert np.all(diff >= 0)
152
+ distances = np.maximum(diff, distances)
153
+ else:
154
+ for i in range(dim1):
155
+ diff = np.abs(points1[:, i, None] - points2[:, i], dtype="d")
156
+ if max_dists_per_dim is not None:
157
+ np.minimum(max_dists_per_dim[i] - diff, diff, out=diff)
158
+ assert np.all(diff >= 0)
159
+ diff **= exponent
160
+ distances += diff
161
+ # in place root function
162
+ np.power(distances, 1.0 / exponent, out=distances)
163
+ return distances