EmptySpaceSearch 0.1__tar.gz
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.
- emptyspacesearch-0.1/LICENSE +21 -0
- emptyspacesearch-0.1/PKG-INFO +40 -0
- emptyspacesearch-0.1/README.md +1 -0
- emptyspacesearch-0.1/pyproject.toml +3 -0
- emptyspacesearch-0.1/setup.cfg +31 -0
- emptyspacesearch-0.1/src/EmptySpaceSearch.egg-info/PKG-INFO +40 -0
- emptyspacesearch-0.1/src/EmptySpaceSearch.egg-info/SOURCES.txt +12 -0
- emptyspacesearch-0.1/src/EmptySpaceSearch.egg-info/dependency_links.txt +1 -0
- emptyspacesearch-0.1/src/EmptySpaceSearch.egg-info/requires.txt +3 -0
- emptyspacesearch-0.1/src/EmptySpaceSearch.egg-info/top_level.txt +1 -0
- emptyspacesearch-0.1/src/ess/__init__.py +1 -0
- emptyspacesearch-0.1/src/ess/ess.py +228 -0
- emptyspacesearch-0.1/test/test_ess.py +147 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Mário Antunes
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: EmptySpaceSearch
|
|
3
|
+
Version: 0.1
|
|
4
|
+
Summary: Empty Space Search
|
|
5
|
+
Home-page: https://github.com/mariolpantunes/ess
|
|
6
|
+
Author: Mário Antunes
|
|
7
|
+
Author-email: mario.antunes@ua.pt
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.8
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: numpy>=2.1.0
|
|
15
|
+
Requires-Dist: numba>=0.61.0
|
|
16
|
+
Requires-Dist: hnswlib>=0.8.0
|
|
17
|
+
Dynamic: license-file
|
|
18
|
+
|
|
19
|
+
# Empty Space Search (ESS)
|
|
20
|
+
MIT License
|
|
21
|
+
|
|
22
|
+
Copyright (c) 2025 Mário Antunes
|
|
23
|
+
|
|
24
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
25
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
26
|
+
in the Software without restriction, including without limitation the rights
|
|
27
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
28
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
29
|
+
furnished to do so, subject to the following conditions:
|
|
30
|
+
|
|
31
|
+
The above copyright notice and this permission notice shall be included in all
|
|
32
|
+
copies or substantial portions of the Software.
|
|
33
|
+
|
|
34
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
35
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
36
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
37
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
38
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
39
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
40
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Empty Space Search (ESS)
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
[metadata]
|
|
2
|
+
name = EmptySpaceSearch
|
|
3
|
+
version = 0.1
|
|
4
|
+
author = Mário Antunes
|
|
5
|
+
author_email = mario.antunes@ua.pt
|
|
6
|
+
description = Empty Space Search
|
|
7
|
+
long_description = file: README.md, LICENSE
|
|
8
|
+
long_description_content_type = text/markdown
|
|
9
|
+
url = https://github.com/mariolpantunes/ess
|
|
10
|
+
classifiers =
|
|
11
|
+
Programming Language :: Python :: 3
|
|
12
|
+
License :: OSI Approved :: MIT License
|
|
13
|
+
Operating System :: OS Independent
|
|
14
|
+
|
|
15
|
+
[options]
|
|
16
|
+
package_dir =
|
|
17
|
+
= src
|
|
18
|
+
packages = find:
|
|
19
|
+
python_requires = >=3.8
|
|
20
|
+
install_requires =
|
|
21
|
+
numpy>=2.1.0
|
|
22
|
+
numba>=0.61.0
|
|
23
|
+
hnswlib>=0.8.0
|
|
24
|
+
|
|
25
|
+
[options.packages.find]
|
|
26
|
+
where = src
|
|
27
|
+
|
|
28
|
+
[egg_info]
|
|
29
|
+
tag_build =
|
|
30
|
+
tag_date = 0
|
|
31
|
+
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: EmptySpaceSearch
|
|
3
|
+
Version: 0.1
|
|
4
|
+
Summary: Empty Space Search
|
|
5
|
+
Home-page: https://github.com/mariolpantunes/ess
|
|
6
|
+
Author: Mário Antunes
|
|
7
|
+
Author-email: mario.antunes@ua.pt
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.8
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: numpy>=2.1.0
|
|
15
|
+
Requires-Dist: numba>=0.61.0
|
|
16
|
+
Requires-Dist: hnswlib>=0.8.0
|
|
17
|
+
Dynamic: license-file
|
|
18
|
+
|
|
19
|
+
# Empty Space Search (ESS)
|
|
20
|
+
MIT License
|
|
21
|
+
|
|
22
|
+
Copyright (c) 2025 Mário Antunes
|
|
23
|
+
|
|
24
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
25
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
26
|
+
in the Software without restriction, including without limitation the rights
|
|
27
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
28
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
29
|
+
furnished to do so, subject to the following conditions:
|
|
30
|
+
|
|
31
|
+
The above copyright notice and this permission notice shall be included in all
|
|
32
|
+
copies or substantial portions of the Software.
|
|
33
|
+
|
|
34
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
35
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
36
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
37
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
38
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
39
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
40
|
+
SOFTWARE.
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
setup.cfg
|
|
5
|
+
src/EmptySpaceSearch.egg-info/PKG-INFO
|
|
6
|
+
src/EmptySpaceSearch.egg-info/SOURCES.txt
|
|
7
|
+
src/EmptySpaceSearch.egg-info/dependency_links.txt
|
|
8
|
+
src/EmptySpaceSearch.egg-info/requires.txt
|
|
9
|
+
src/EmptySpaceSearch.egg-info/top_level.txt
|
|
10
|
+
src/ess/__init__.py
|
|
11
|
+
src/ess/ess.py
|
|
12
|
+
test/test_ess.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
ess
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import src.ess as ess
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import numba
|
|
2
|
+
import hnswlib
|
|
3
|
+
import logging
|
|
4
|
+
import numpy as np
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
logging.basicConfig(level=logging.INFO, format='%(message)s')
|
|
8
|
+
logger = logging.getLogger(__name__)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@numba.jit(nopython=True, parallel=True, fastmath=True)
|
|
12
|
+
def clip(a, vmin, vmax):
|
|
13
|
+
return np.maximum(np.minimum(a, vmax), vmin)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@numba.jit(nopython=True, parallel=True, fastmath=True)
|
|
17
|
+
def mean(a):
|
|
18
|
+
return np.divide(np.sum(a), a.shape[0])
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _scale(arr, min_val=None, max_val=None):
|
|
22
|
+
if min_val is None:
|
|
23
|
+
min_val = min(arr)
|
|
24
|
+
|
|
25
|
+
if max_val is None:
|
|
26
|
+
max_val = max(arr)
|
|
27
|
+
|
|
28
|
+
scl_arr = (arr - min_val) / (max_val - min_val)
|
|
29
|
+
return scl_arr, min_val, max_val
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _inv_scale(scl_arr, min_val, max_val):
|
|
33
|
+
return scl_arr*(max_val - min_val) + min_val
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@numba.jit(nopython=True, parallel=True, fastmath=True)
|
|
37
|
+
def _force(sigma, d):
|
|
38
|
+
"""
|
|
39
|
+
Optimized Force function.
|
|
40
|
+
"""
|
|
41
|
+
#ratio = sigma / d # Reuse this computation
|
|
42
|
+
#np.clip(ratio, a_min=None, a_max=3.1622, out=ratio) # Avoids overflow
|
|
43
|
+
#ratio = clip(sigma / d, 0, 3.1622)
|
|
44
|
+
ratio = np.minimum(sigma/d, 3.1622)
|
|
45
|
+
#attrac = ratio ** 6
|
|
46
|
+
#np.clip(attrac, a_min=None, a_max=1000, out=attrac) # Avoids overflow
|
|
47
|
+
#attrac = clip(attrac, 0, 1000)
|
|
48
|
+
attrac = np.minimum(ratio ** 6, 1000)
|
|
49
|
+
|
|
50
|
+
return np.abs(6 * (2 * attrac ** 2 - attrac) / d)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _elastic(es, neighbors, neighbors_dist):
|
|
54
|
+
"""
|
|
55
|
+
Optimized Elastic force with vectorization.
|
|
56
|
+
"""
|
|
57
|
+
sigma = mean(neighbors_dist) / 5.0
|
|
58
|
+
neighbors_dist = np.maximum(neighbors_dist, 0.001) # Avoids distances < 0.001
|
|
59
|
+
|
|
60
|
+
# Vectorized force computation
|
|
61
|
+
forces = _force(sigma, neighbors_dist)
|
|
62
|
+
|
|
63
|
+
# Vectorized displacement computation
|
|
64
|
+
vecs = (es - neighbors) / neighbors_dist[:, np.newaxis]
|
|
65
|
+
|
|
66
|
+
# Compute the directional force
|
|
67
|
+
#TODO: check this one
|
|
68
|
+
direc = np.sum(vecs * forces[:, np.newaxis], axis=0)
|
|
69
|
+
|
|
70
|
+
return direc
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _empty_center(coor, data, neigh, movestep, iternum:int=100, bounds=np.array([[-1, 1]])):
|
|
74
|
+
"""
|
|
75
|
+
Empty center search process.
|
|
76
|
+
"""
|
|
77
|
+
|
|
78
|
+
for i in range(iternum):
|
|
79
|
+
adjs_, distances_ = neigh.knn_query(coor, k=data.shape[1]+1)
|
|
80
|
+
|
|
81
|
+
logger.debug(f'Empty Centers {adjs_} {distances_}')
|
|
82
|
+
|
|
83
|
+
direc = _elastic(coor, data[adjs_[0]], distances_[0])
|
|
84
|
+
mag = np.linalg.norm(direc)
|
|
85
|
+
if mag < 1e-7:
|
|
86
|
+
break
|
|
87
|
+
direc /= mag
|
|
88
|
+
coor += direc * movestep
|
|
89
|
+
|
|
90
|
+
# TODO (4): should the bounds be fixed to [0, 1]?
|
|
91
|
+
# may help code here
|
|
92
|
+
if (coor < bounds[:, 0]).any() or (coor > bounds[:, 1]).any():
|
|
93
|
+
np.clip(coor, bounds[:, 0], bounds[:, 1], out=coor)
|
|
94
|
+
break
|
|
95
|
+
|
|
96
|
+
return coor
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _esa_01(samples, bounds, n:int=None, seed:int=None):
|
|
100
|
+
'''
|
|
101
|
+
apply esa in the experiment
|
|
102
|
+
'''
|
|
103
|
+
min_val = bounds[:,0]
|
|
104
|
+
max_val = bounds[:,1]
|
|
105
|
+
samples, _, _ = _scale(samples, min_val, max_val)
|
|
106
|
+
|
|
107
|
+
neigh = hnswlib.Index(space='l2', dim=samples.shape[1])
|
|
108
|
+
if seed is not None:
|
|
109
|
+
neigh.init_index(max_elements=len(samples)+n, ef_construction = 200, M=48,
|
|
110
|
+
random_seed = seed)
|
|
111
|
+
else:
|
|
112
|
+
neigh.init_index(max_elements=len(samples)+n, ef_construction = 200, M=48)
|
|
113
|
+
neigh.add_items(samples)
|
|
114
|
+
|
|
115
|
+
#TODO (2): improve by adding one point at a time (avoiding clustering points together)
|
|
116
|
+
coors = np.random.uniform(0, 1, (n, samples.shape[1]))
|
|
117
|
+
logger.debug(f'Coors({n}, {samples.shape[1]})\n{coors}')
|
|
118
|
+
es_params = []
|
|
119
|
+
logger.debug(f'Samples\n{samples}')
|
|
120
|
+
es_params = [_empty_center(coor.reshape(1, -1), samples, neigh,
|
|
121
|
+
movestep=0.01, iternum=100, bounds=np.array([[0, 1]]))[0] for coor in coors]
|
|
122
|
+
logger.debug(f'Params({len(es_params)})\n{es_params}')
|
|
123
|
+
#rv = np.array(es_params)[:n]
|
|
124
|
+
rv = np.array(es_params)
|
|
125
|
+
rv = _inv_scale(rv, min_val=min_val, max_val=max_val)
|
|
126
|
+
|
|
127
|
+
logger.debug(f'RV({rv.shape})\n{rv}')
|
|
128
|
+
|
|
129
|
+
return rv
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def _esa_02(samples, bounds, n:int=None, seed:int=None):
|
|
133
|
+
'''
|
|
134
|
+
apply esa in the experiment
|
|
135
|
+
'''
|
|
136
|
+
min_val = bounds[:,0]
|
|
137
|
+
max_val = bounds[:,1]
|
|
138
|
+
samples, _, _ = _scale(samples, min_val, max_val)
|
|
139
|
+
|
|
140
|
+
neigh = hnswlib.Index(space='l2', dim=samples.shape[1])
|
|
141
|
+
if seed is not None:
|
|
142
|
+
neigh.init_index(max_elements=len(samples)+n, ef_construction = 200, M=48,
|
|
143
|
+
random_seed = seed)
|
|
144
|
+
else:
|
|
145
|
+
neigh.init_index(max_elements=len(samples)+n, ef_construction = 200, M=48)
|
|
146
|
+
neigh.add_items(samples)
|
|
147
|
+
|
|
148
|
+
#TODO (2): improve by adding one point at a time (avoiding clustering points together)
|
|
149
|
+
coors = np.random.uniform(0, 1, (n, samples.shape[1]))
|
|
150
|
+
logger.debug(f'Coors({n}, {samples.shape[1]})\n{coors}')
|
|
151
|
+
es_params = []
|
|
152
|
+
logger.debug(f'Samples\n{samples}')
|
|
153
|
+
for c in coors:
|
|
154
|
+
es_param = _empty_center(c.reshape(1, -1), samples, neigh,
|
|
155
|
+
movestep=0.01, iternum=100, bounds=np.array([[0, 1]]))
|
|
156
|
+
es_params.append(es_param[0])
|
|
157
|
+
samples = np.concatenate((samples, es_param), axis=0)
|
|
158
|
+
#samples = np.append(samples, es_param)
|
|
159
|
+
logger.debug(f'Samples\n{samples}')
|
|
160
|
+
neigh.add_items(es_param)
|
|
161
|
+
#es_params = [_empty_center(coor.reshape(1, -1), samples, neigh,
|
|
162
|
+
#movestep=0.01, iternum=100, bounds=np.array([[0, 1]]))[0] for coor in coors]
|
|
163
|
+
logger.debug(f'Params({len(es_params)})\n{es_params}')
|
|
164
|
+
#rv = np.array(es_params)[:n]
|
|
165
|
+
rv = np.array(es_params)
|
|
166
|
+
rv = _inv_scale(rv, min_val=min_val, max_val=max_val)
|
|
167
|
+
|
|
168
|
+
logger.debug(f'RV({rv.shape})\n{rv}')
|
|
169
|
+
|
|
170
|
+
return rv
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def esa(samples, bounds, n:int=None, seed:int=None):
|
|
174
|
+
'''
|
|
175
|
+
apply esa in the experiment
|
|
176
|
+
'''
|
|
177
|
+
min_val = bounds[:,0]
|
|
178
|
+
max_val = bounds[:,1]
|
|
179
|
+
samples, _, _ = _scale(samples, min_val, max_val)
|
|
180
|
+
samples = samples.astype(np.float32)
|
|
181
|
+
|
|
182
|
+
neigh = hnswlib.Index(space='l2', dim=samples.shape[1])
|
|
183
|
+
if seed is not None:
|
|
184
|
+
neigh.init_index(max_elements=len(samples)+n, ef_construction = 200, M=48,
|
|
185
|
+
random_seed = seed)
|
|
186
|
+
else:
|
|
187
|
+
neigh.init_index(max_elements=len(samples)+n, ef_construction = 200, M=48)
|
|
188
|
+
|
|
189
|
+
#TODO: apply seed number here
|
|
190
|
+
coors = np.random.uniform(0, 1, (n, samples.shape[1])).astype(np.float32)
|
|
191
|
+
# increase the sample pool and keep original size as idx
|
|
192
|
+
idx = len(samples)
|
|
193
|
+
samples = np.concatenate((samples, coors), axis=0)
|
|
194
|
+
neigh.add_items(samples)
|
|
195
|
+
|
|
196
|
+
iternum = 100
|
|
197
|
+
movestep=0.01
|
|
198
|
+
|
|
199
|
+
for _ in range(iternum):
|
|
200
|
+
for i in range(idx, len(samples)):
|
|
201
|
+
p = samples[i]
|
|
202
|
+
|
|
203
|
+
adjs_, distances_ = neigh.knn_query(p, k=samples.shape[1]+2)
|
|
204
|
+
direc = _elastic(p, samples[adjs_[0, 1:]], distances_[0, 1:])
|
|
205
|
+
p += (direc/np.linalg.norm(direc)) * movestep
|
|
206
|
+
|
|
207
|
+
samples[i] = p
|
|
208
|
+
|
|
209
|
+
samples = clip(samples, 0, 1)
|
|
210
|
+
neigh = hnswlib.Index(space='l2', dim=samples.shape[1])
|
|
211
|
+
if seed is not None:
|
|
212
|
+
neigh.init_index(max_elements=len(samples)+n, ef_construction = 200, M=48,
|
|
213
|
+
random_seed = seed)
|
|
214
|
+
else:
|
|
215
|
+
neigh.init_index(max_elements=len(samples)+n, ef_construction = 200, M=48)
|
|
216
|
+
neigh.add_items(samples)
|
|
217
|
+
|
|
218
|
+
rv = samples[idx:]
|
|
219
|
+
rv = _inv_scale(rv, min_val=min_val, max_val=max_val)
|
|
220
|
+
|
|
221
|
+
return rv
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def ess(samples, bounds, n:int=None, seed:int=None):
|
|
225
|
+
if type(samples) is not np.ndarray:
|
|
226
|
+
samples = np.array(samples).astype(np.float32)
|
|
227
|
+
rv = esa(samples=samples, bounds=bounds, n=n, seed=seed)
|
|
228
|
+
return np.concatenate((samples, rv), axis=0)
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
|
|
3
|
+
__author__ = 'Mário Antunes'
|
|
4
|
+
__version__ = '0.1'
|
|
5
|
+
__email__ = 'mario.antunes@ua.pt'
|
|
6
|
+
__status__ = 'Development'
|
|
7
|
+
__license__ = 'MIT'
|
|
8
|
+
__copyright__ = '''
|
|
9
|
+
Copyright (c) 2021-2023 Stony Brook University
|
|
10
|
+
Copyright (c) 2021-2023 The Research Foundation of SUNY
|
|
11
|
+
'''
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
import unittest
|
|
15
|
+
import numpy as np
|
|
16
|
+
import ess.ess as ess
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def calculate_grid_coverage(points, bounds, grid):
|
|
20
|
+
"""
|
|
21
|
+
Calculates grid-based coverage of N-dimensional points.
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
points (np.ndarray): A 2D NumPy array of shape (N_points, N_dims)
|
|
25
|
+
where N_points is the number of points, and N_dims is the number of dimensions.
|
|
26
|
+
|
|
27
|
+
bounds (np.ndarray): A 2D array of shape (N_dims,2) specifying
|
|
28
|
+
the minimum and maximum coordinate for each dimension of the grid.
|
|
29
|
+
|
|
30
|
+
grid (int or tuple or list): The number of bins (cells)
|
|
31
|
+
along each dimension. If int, it's used for all N_dims.
|
|
32
|
+
If tuple/list (res1, res2, ..., resN), specifies bins for each dimension.
|
|
33
|
+
|
|
34
|
+
Returns:
|
|
35
|
+
percentage_coverage (float): The percentage of the total grid area (volume) covered.
|
|
36
|
+
"""
|
|
37
|
+
num_dims = points.shape[1]
|
|
38
|
+
|
|
39
|
+
if isinstance(grid, int):
|
|
40
|
+
bins = [grid] * num_dims
|
|
41
|
+
elif len(grid) != num_dims:
|
|
42
|
+
raise ValueError(f"grid_resolutions must be an int or a list/tuple of length {num_dims}")
|
|
43
|
+
else:
|
|
44
|
+
bins = list(grid)
|
|
45
|
+
|
|
46
|
+
# Define bin edges for each dimension
|
|
47
|
+
# np.histogramdd expects a list of 1D arrays for bin edges
|
|
48
|
+
bin_edges = []
|
|
49
|
+
for d in range(num_dims):
|
|
50
|
+
bin_edges.append(np.linspace(bounds[d, 0], bounds[d, 1], bins[d] + 1))
|
|
51
|
+
|
|
52
|
+
# Use np.histogramdd to count points in each N-dimensional bin
|
|
53
|
+
# The output 'H' is an N-dimensional array where H[i, j, k, ...]
|
|
54
|
+
# is the number of points in the corresponding N-dim cell.
|
|
55
|
+
H, _ = np.histogramdd(points, bins=bin_edges)
|
|
56
|
+
|
|
57
|
+
# Create the coverage map: 1 if cell has points, 0 otherwise
|
|
58
|
+
coverage_map = (H > 0).astype(int)
|
|
59
|
+
|
|
60
|
+
# Calculate metrics
|
|
61
|
+
total_covered_cells = np.sum(coverage_map)
|
|
62
|
+
|
|
63
|
+
total_grid_cells = np.prod(bins) # Product of resolutions for total cells
|
|
64
|
+
|
|
65
|
+
if total_grid_cells == 0: # Avoid division by zero if no cells are defined
|
|
66
|
+
percentage_coverage = 0.0
|
|
67
|
+
else:
|
|
68
|
+
percentage_coverage = (total_covered_cells / total_grid_cells)
|
|
69
|
+
|
|
70
|
+
# Calculate cell hyper-volume
|
|
71
|
+
#cell_dimensions = [(max_coords[d] - min_coords[d]) / bins[d] for d in range(num_dims)]
|
|
72
|
+
#cell_volume = np.prod(cell_dimensions)
|
|
73
|
+
|
|
74
|
+
return percentage_coverage
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
class TestESS(unittest.TestCase):
|
|
78
|
+
|
|
79
|
+
def test_ess_00(self):
|
|
80
|
+
points = np.array([[0,0], [5,5], [5,0], [0,5], [2.5,2.5]])
|
|
81
|
+
bounds = np.array([[0,5], [0,5]])
|
|
82
|
+
n_points = 100
|
|
83
|
+
grid = 10
|
|
84
|
+
|
|
85
|
+
rnd_points = np.random.uniform(bounds[0, 0], bounds[0, 1],
|
|
86
|
+
size=(n_points, points.shape[1]))
|
|
87
|
+
b = np.concatenate((points, rnd_points), axis=0)
|
|
88
|
+
|
|
89
|
+
a = ess.ess(points, bounds, n_points)
|
|
90
|
+
|
|
91
|
+
coverage_a = calculate_grid_coverage(a, bounds=bounds, grid=grid)
|
|
92
|
+
coverage_b = calculate_grid_coverage(b, bounds=bounds, grid=grid)
|
|
93
|
+
|
|
94
|
+
self.assertGreater(coverage_a, coverage_b)
|
|
95
|
+
|
|
96
|
+
def test_ess_01(self):
|
|
97
|
+
points = np.array([[0,0], [5,5], [5,0], [0,5], [2.5,2.5]])
|
|
98
|
+
bounds = np.array([[0, 100], [0, 100]])
|
|
99
|
+
n_points = 100
|
|
100
|
+
grid = 10
|
|
101
|
+
|
|
102
|
+
rnd_points = np.random.uniform(bounds[0, 0], bounds[0, 1],
|
|
103
|
+
size=(n_points, points.shape[1]))
|
|
104
|
+
b = np.concatenate((points, rnd_points), axis=0)
|
|
105
|
+
|
|
106
|
+
a = ess.ess(points, bounds, n_points)
|
|
107
|
+
|
|
108
|
+
coverage_a = calculate_grid_coverage(a, bounds=bounds, grid=grid)
|
|
109
|
+
coverage_b = calculate_grid_coverage(b, bounds=bounds, grid=grid)
|
|
110
|
+
|
|
111
|
+
self.assertGreater(coverage_a, coverage_b)
|
|
112
|
+
|
|
113
|
+
def test_ess_02(self):
|
|
114
|
+
points = np.array([[0,0,0], [5,5,5], [5,0,0], [0,5,0],
|
|
115
|
+
[0,0,5], [0,5,5], [5,0,5], [5,5,0], [2,2,2]])
|
|
116
|
+
bounds = np.array([[0, 5], [0, 5], [0, 5]])
|
|
117
|
+
n_points = 500
|
|
118
|
+
grid = 10
|
|
119
|
+
|
|
120
|
+
rnd_points = np.random.uniform(bounds[0, 0], bounds[0, 1],
|
|
121
|
+
size=(n_points, points.shape[1]))
|
|
122
|
+
b = np.concatenate((points, rnd_points), axis=0)
|
|
123
|
+
|
|
124
|
+
a = ess.ess(points, bounds, n_points)
|
|
125
|
+
|
|
126
|
+
coverage_a = calculate_grid_coverage(a, bounds=bounds, grid=grid)
|
|
127
|
+
coverage_b = calculate_grid_coverage(b, bounds=bounds, grid=grid)
|
|
128
|
+
|
|
129
|
+
self.assertGreater(coverage_a, coverage_b)
|
|
130
|
+
|
|
131
|
+
def test_ess_03(self):
|
|
132
|
+
points = np.array([[0,0,0], [5,5,5], [5,0,0], [0,5,0],
|
|
133
|
+
[0,0,5], [0,5,5], [5,0,5], [5,5,0], [2,2,2]])
|
|
134
|
+
bounds = np.array([[0, 10], [0, 10], [0, 10]])
|
|
135
|
+
n_points = 500
|
|
136
|
+
grid = 10
|
|
137
|
+
|
|
138
|
+
rnd_points = np.random.uniform(bounds[0, 0], bounds[0, 1],
|
|
139
|
+
size=(n_points, points.shape[1]))
|
|
140
|
+
b = np.concatenate((points, rnd_points), axis=0)
|
|
141
|
+
|
|
142
|
+
a = ess.ess(points, bounds, n_points)
|
|
143
|
+
|
|
144
|
+
coverage_a = calculate_grid_coverage(a, bounds=bounds, grid=grid)
|
|
145
|
+
coverage_b = calculate_grid_coverage(b, bounds=bounds, grid=grid)
|
|
146
|
+
|
|
147
|
+
self.assertGreater(coverage_a, coverage_b)
|