File wellmet/dicebox/circumtri.py added (mode: 100644) (index 0000000..3ed327d) |
|
1 |
|
#!/usr/bin/env python |
|
2 |
|
# coding: utf-8 |
|
3 |
|
|
|
4 |
|
|
|
5 |
|
import numpy as np |
|
6 |
|
from scipy.spatial import distance |
|
7 |
|
|
|
8 |
|
from ..ghull import Ghull |
|
9 |
|
from .. import simplex as sx |
|
10 |
|
from .. import convex_hull as khull |
|
11 |
|
from ..reader import Store |
|
12 |
|
from ..candynodes import CandyNodes |
|
13 |
|
|
|
14 |
|
from collections import namedtuple |
|
15 |
|
from sortedcollections import ValueSortedDict |
|
16 |
|
|
|
17 |
|
|
|
18 |
|
|
|
19 |
|
|
|
20 |
|
|
|
21 |
|
def get_entropy(pf): |
|
22 |
|
return -pf * np.log(pf) - (1 - pf) * np.log(1 - pf) |
|
23 |
|
|
|
24 |
|
max_entropy = get_entropy(0.5) |
|
25 |
|
|
|
26 |
|
|
|
27 |
|
|
|
28 |
|
TriEstimation = namedtuple('TriEstimation', ( |
|
29 |
|
"nsim", |
|
30 |
|
"ndim", |
|
31 |
|
"nfacets", |
|
32 |
|
"r","R", |
|
33 |
|
"inner", |
|
34 |
|
"shell", |
|
35 |
|
"outer", |
|
36 |
|
"FORM_outside", |
|
37 |
|
"orth_outside", |
|
38 |
|
"shell_budget", |
|
39 |
|
"shell_inside", |
|
40 |
|
"shell_outside", |
|
41 |
|
"inside", |
|
42 |
|
"outside", |
|
43 |
|
"success_points", |
|
44 |
|
"failure_points", |
|
45 |
|
"success", |
|
46 |
|
"failure", |
|
47 |
|
"mix", |
|
48 |
|
"vertex_estimation", |
|
49 |
|
"weighted_vertex_estimation", |
|
50 |
|
"nsimplex", |
|
51 |
|
"tn_scheme", |
|
52 |
|
"tn_scheme_points", |
|
53 |
|
"newly_invalidated", |
|
54 |
|
"newly_estimated", |
|
55 |
|
"simplex_stats", |
|
56 |
|
"candidates_sets", |
|
57 |
|
"ncoplanar" |
|
58 |
|
)) |
|
59 |
|
|
|
60 |
|
|
|
61 |
|
#č Je třeba dávát bacha na odlišnosti v (staré) Triangulation třídě a nové Ghull třídě. |
|
62 |
|
#č Zatímco Triangulation drží starý stáv, dokud .update() není spustěn, |
|
63 |
|
#č Ghull, ale hlavně, odpovídající modely konvexních obálek jíž žádný .update() nemájí, |
|
64 |
|
#č nové tečky uvidí sami dřív než se naše skříňka probere. |
|
65 |
|
#č Takže teď odhady nově budeme ukladať hned pri incrementu. |
|
66 |
|
#č Triangulation používá i jínej kód, samotné třídy beztak zbytečně komplikováné, |
|
67 |
|
#č nechci teď to toho lezt. |
|
68 |
|
class CircumTri: |
|
69 |
|
|
|
70 |
|
#č míží nám sampling_space: Ghull umí vzorkovat outside pouze v G prostoru |
|
71 |
|
#č quadpy umístí integráční bodíky v prostoru triangulace. |
|
72 |
|
def __init__(bx, sample_box, scheme, tri_space='G', |
|
73 |
|
entropy_mode='weighted', shell_budget=1000, outer_budget=100): |
|
74 |
|
|
|
75 |
|
bx.scheme = scheme |
|
76 |
|
bx.tri_space = tri_space |
|
77 |
|
|
|
78 |
|
|
|
79 |
|
bx.shell_budget = shell_budget |
|
80 |
|
bx.outer_budget = outer_budget |
|
81 |
|
bx.entropy_mode = entropy_mode |
|
82 |
|
|
|
83 |
|
|
|
84 |
|
bx.sample_box = sample_box |
|
85 |
|
|
|
86 |
|
#č vítejte nové uložiště odhadů. |
|
87 |
|
#č Odhady z stm kódu už ale nemají na tohle sahat |
|
88 |
|
if hasattr(bx, 'filename'): |
|
89 |
|
bx.estimations = Store.create(bx.filename + "_tri", TriEstimation) |
|
90 |
|
else: |
|
91 |
|
bx.estimations = [] |
|
92 |
|
|
|
93 |
|
bx.CC = sx.CircumCenter(sample_box.nvar) |
|
94 |
|
|
|
95 |
|
# kind of interface to CandidatesWidget |
|
96 |
|
bx.candidates_index = {} |
|
97 |
|
bx.potential_index = ValueSortedDict() |
|
98 |
|
|
|
99 |
|
bx.regen() |
|
100 |
|
|
|
101 |
|
|
|
102 |
|
def init_parameters(bx): |
|
103 |
|
""" |
|
104 |
|
Returns dictionary of parameters the DiceBox was initialized with |
|
105 |
|
""" |
|
106 |
|
return "" |
|
107 |
|
return {'sample_object':bx.sample_box, 'scheme':bx.scheme.name,\ |
|
108 |
|
'tri_space':bx.tri_space, 'tree_space':bx.tree_space,\ |
|
109 |
|
'kechato_space':bx.kechato_space, 'potential':bx.potential,\ |
|
110 |
|
'p_norm':bx.p_norm, 'shell_budget':bx.shell_budget,\ |
|
111 |
|
'outer_budget':bx.outer_budget} |
|
112 |
|
|
|
113 |
|
|
|
114 |
|
def __repr__(bx): |
|
115 |
|
return "%s(**%s)"%(bx.__class__.__name__, repr(bx.init_parameters())) |
|
116 |
|
|
|
117 |
|
def __str__(bx): |
|
118 |
|
return "%s(%s)"%(bx.__class__.__name__, str(bx.init_parameters())) |
|
119 |
|
|
|
120 |
|
|
|
121 |
|
|
|
122 |
|
def __call__(bx): |
|
123 |
|
if bx.nsim < 1: # je to legální |
|
124 |
|
return bx.f_model.new_sample([], space='G', extend=True) |
|
125 |
|
else: |
|
126 |
|
# return node with the greatest potential |
|
127 |
|
key, value = bx.potential_index.peekitem() |
|
128 |
|
return bx.candidates_index[key] |
|
129 |
|
|
|
130 |
|
|
|
131 |
|
def regen(bx): |
|
132 |
|
""" |
|
133 |
|
regen() recreates data structures of the box. |
|
134 |
|
It shouldn't be called without reason, changed distribution, settings or so. |
|
135 |
|
""" |
|
136 |
|
|
|
137 |
|
#оӵ шайтан регенираци лэзьиз |
|
138 |
|
bx._logger(msg='regeneration started') |
|
139 |
|
|
|
140 |
|
bx.candidates_index.clear() |
|
141 |
|
bx.potential_index.clear() |
|
142 |
|
|
|
143 |
|
bx._regen_outside() |
|
144 |
|
bx._regen_inside() |
|
145 |
|
bx._nsim = bx.nsim |
|
146 |
|
|
|
147 |
|
|
|
148 |
|
|
|
149 |
|
|
|
150 |
|
|
|
151 |
|
def __len__(bx): |
|
152 |
|
return bx.sample_box.nsim |
|
153 |
|
|
|
154 |
|
def __getitem__(bx, slice): |
|
155 |
|
#č stačí vratit sample_box |
|
156 |
|
return bx.sample_box[slice] |
|
157 |
|
|
|
158 |
|
def __getattr__(dx, attr): |
|
159 |
|
if attr == 'dicebox': |
|
160 |
|
return dx |
|
161 |
|
|
|
162 |
|
#č branime sa rekurzii |
|
163 |
|
# defend against recursion |
|
164 |
|
#оӵ рекурсилы пезьдэт! |
|
165 |
|
if attr == 'sample_box': |
|
166 |
|
raise AttributeError |
|
167 |
|
|
|
168 |
|
#ё По всем вопросам обращайтесь |
|
169 |
|
#ё на нашу горячую линию |
|
170 |
|
else: |
|
171 |
|
return getattr(dx.sample_box, attr) |
|
172 |
|
|
|
173 |
|
|
|
174 |
|
# The DiceBox Observer |
|
175 |
|
def _logger(self, *args, msg="", indent=0, **kwargs): |
|
176 |
|
if not kwargs: |
|
177 |
|
kwargs = "" #č ať se nám prázdné závorky nezobrazujou |
|
178 |
|
print(self.__class__.__name__ + ":", msg, *args, kwargs) |
|
179 |
|
|
|
180 |
|
|
|
181 |
|
# inspired by Qt |
|
182 |
|
def connect(self, slot): self._logger = slot |
|
183 |
|
def disconnect(self): del(self._logger) |
|
184 |
|
|
|
185 |
|
|
|
186 |
|
# přidávání vzorků musí bejt explicitní! |
|
187 |
|
def add_sample(bx, input_sample): |
|
188 |
|
bx.sample_box.add_sample(input_sample) |
|
189 |
|
bx.increment(bx.sample_box[bx._nsim:]) |
|
190 |
|
bx._nsim = bx.nsim |
|
191 |
|
|
|
192 |
|
|
|
193 |
|
|
|
194 |
|
#č bejvalej .estimate_simplex() |
|
195 |
|
#č teď je to kolbek, který volá Triangulation |
|
196 |
|
def _on_add_simplex(bx, nodes): |
|
197 |
|
# -1=outside, 0=success, 1=failure, 2=mix |
|
198 |
|
if nodes.event_id != 2: |
|
199 |
|
return |
|
200 |
|
|
|
201 |
|
|
|
202 |
|
indices = nodes.indices |
|
203 |
|
failsi = bx.Tri.failsi[indices] |
|
204 |
|
vertices_model = bx.Tri.tri.points[indices] |
|
205 |
|
PDF = bx.Tri.PDF[indices] |
|
206 |
|
|
|
207 |
|
circum_center = bx.CC.get_circumcenter(vertices_model) |
|
208 |
|
r = distance.euclidean(circum_center, vertices_model[0]) |
|
209 |
|
circum_node = bx.f_model.new_sample(circum_center, space=bx.tri_space) |
|
210 |
|
#č můžeme nechat numpy pole z jednoho prvku. Můžeme zredukovat na float |
|
211 |
|
#circum_pdf = circum_node.pdf(bx.tri_space) |
|
212 |
|
circum_pdf = float(circum_node.pdf(bx.tri_space)) |
|
213 |
|
circum_potential = r * circum_pdf**(1/bx.nvar) |
|
214 |
|
|
|
215 |
|
#č nodes příjdou zabalené do CandyNodes. Ty mají .attrs a .kwargs |
|
216 |
|
#circum_node = CandyNodes(circum_node, nodes.attrs) |
|
217 |
|
#circum_node.potential = r * circum_pdf**(1/bx.nvar) |
|
218 |
|
|
|
219 |
|
|
|
220 |
|
nodes_model = getattr(nodes, bx.tri_space) |
|
221 |
|
dr = distance.cdist(nodes_model, [circum_center]).flatten() |
|
222 |
|
nodes_pdf = nodes.pdf(bx.tri_space) |
|
223 |
|
node_potentials = (r - dr) * nodes_pdf**(1/bx.nvar) |
|
224 |
|
|
|
225 |
|
if bx.entropy_mode == 'none': |
|
226 |
|
node_ratings = node_potentials * max_entropy |
|
227 |
|
circum_rating = circum_potential * max_entropy |
|
228 |
|
|
|
229 |
|
elif bx.entropy_mode == 'simple': |
|
230 |
|
node_ratings = node_potentials * get_entropy(nodes.pfv) |
|
231 |
|
|
|
232 |
|
# fp like a failure points. Number of failure points |
|
233 |
|
fp = len(failsi[failsi]) # the fastest solution |
|
234 |
|
circum_rating = circum_potential * get_entropy(fp / len(failsi)) |
|
235 |
|
|
|
236 |
|
elif bx.entropy_mode == 'weighted': |
|
237 |
|
node_ratings = node_potentials * get_entropy(nodes.pfw) |
|
238 |
|
|
|
239 |
|
# same as np.average(failsi, weights=pdf), but faster |
|
240 |
|
wfr = np.sum(PDF[failsi]) / np.sum(PDF) |
|
241 |
|
circum_rating = circum_potential * get_entropy(wfr) |
|
242 |
|
|
|
243 |
|
|
|
244 |
|
max_node = np.nanargmax(node_ratings) |
|
245 |
|
max_node_rating = node_ratings[max_node] |
|
246 |
|
if circum_rating > max_node_rating: |
|
247 |
|
#č nodes příjdou zabalené do CandyNodes. Ty mají .attrs a .kwargs |
|
248 |
|
circum_node = CandyNodes(circum_node, nodes.attrs) |
|
249 |
|
circum_node.potential = circum_potential |
|
250 |
|
circum_node.rating = circum_rating |
|
251 |
|
bx.candidates_index[tuple(indices)] = circum_node |
|
252 |
|
bx.potential_index[tuple(indices)] = circum_rating |
|
253 |
|
else: |
|
254 |
|
node = nodes[max_node] |
|
255 |
|
node.potential = node_potentials[max_node] |
|
256 |
|
node.rating = max_node_rating |
|
257 |
|
bx.candidates_index[tuple(indices)] = node |
|
258 |
|
bx.potential_index[tuple(indices)] = max_node_rating |
|
259 |
|
|
|
260 |
|
|
|
261 |
|
|
|
262 |
|
# callback |
|
263 |
|
#č sx.on_delete_simplex(indices=indices) |
|
264 |
|
def _invalidate_simplex(bx, simplex): |
|
265 |
|
bx.candidates_index.pop(simplex, None) |
|
266 |
|
bx.potential_index.pop(simplex, None) |
|
267 |
|
|
|
268 |
|
|
|
269 |
|
|
|
270 |
|
|
|
271 |
|
|
|
272 |
|
|
|
273 |
|
def _regen_outside(bx): |
|
274 |
|
bx.convex_hull = khull.QHull(bx.f_model, space=bx.tri_space, |
|
275 |
|
incremental=True, auto_update=False) |
|
276 |
|
bx.ghull = Ghull(bx.convex_hull) |
|
277 |
|
bx._R = -1 # update outer under R>_R condition |
|
278 |
|
bx._afacet = None |
|
279 |
|
bx._bfacet = np.inf |
|
280 |
|
#č konečně mám pořádnou stejtful třídu |
|
281 |
|
#č pokud mám aspoň jednu tečku, tak už je mi šuma |
|
282 |
|
#č zda se konvexní obálka vytvořila, či nikoliv |
|
283 |
|
if bx.nsim > 0: |
|
284 |
|
bx.estimate_outside() |
|
285 |
|
|
|
286 |
|
|
|
287 |
|
def _regen_inside(bx): |
|
288 |
|
failsi = bx.failsi |
|
289 |
|
# incremental triangulation require one more point |
|
290 |
|
if (bx.nsim > bx.nvar + 1) and np.any(failsi) and not np.all(failsi): |
|
291 |
|
#bx._logger(msg="triangulation started") |
|
292 |
|
bx.__regen_inside() |
|
293 |
|
else: |
|
294 |
|
#č jíž není nutný |
|
295 |
|
bx._logger(msg="triangulation skipped") |
|
296 |
|
|
|
297 |
|
def __regen_inside(bx): |
|
298 |
|
try: |
|
299 |
|
bx.Tri = sx.BetterCubatureIntegration(bx.samplebox, bx.scheme,\ |
|
300 |
|
tri_space=bx.tri_space, incremental=True,\ |
|
301 |
|
on_add_simplex=bx._on_add_simplex,\ |
|
302 |
|
on_delete_simplex=bx._invalidate_simplex) |
|
303 |
|
|
|
304 |
|
bx.Tri.integrate() # nic nevrácí, všecko je přes kolbeky |
|
305 |
|
#č tri - Deloneho triangulace |
|
306 |
|
bx.tri = bx.Tri.tri #č všichní tam očekávajou QHull |
|
307 |
|
bx._logger(msg="triangulation has been created") |
|
308 |
|
|
|
309 |
|
except BaseException as e: |
|
310 |
|
#č chcu zachytit spadnuti QHull na začatku, |
|
311 |
|
#č kdy ještě není dostatek teček. |
|
312 |
|
#č Jinak je třeba nechat QHull spadnout |
|
313 |
|
if bx.nsim > 2*bx.nvar + 3: |
|
314 |
|
#č no to teda ne! |
|
315 |
|
raise |
|
316 |
|
else: |
|
317 |
|
#č lze přípustit chybu triangulace |
|
318 |
|
bx._logger(msg='triangulation failed') |
|
319 |
|
|
|
320 |
|
|
|
321 |
|
|
|
322 |
|
def increment(bx, input_sample): |
|
323 |
|
#č tri - Deloneho triangulace |
|
324 |
|
if "tri" in dir(bx): |
|
325 |
|
bx.Tri.update() |
|
326 |
|
else: |
|
327 |
|
bx._regen_inside() |
|
328 |
|
|
|
329 |
|
|
|
330 |
|
if np.any(bx.convex_hull.is_outside(input_sample)): |
|
331 |
|
bx.convex_hull.update() |
|
332 |
|
bx.estimate_outside() |
|
333 |
|
|
|
334 |
|
#bx.estimations.append(bx.get_pf_estimation()) |
|
335 |
|
|
|
336 |
|
|
|
337 |
|
|
|
338 |
|
|
|
339 |
|
|
|
340 |
|
|
|
341 |
|
def _rate_outside_nodes(bx, outside_nodes): |
|
342 |
|
#č sice získáme filtrovaný outside, |
|
343 |
|
dh = bx.convex_hull.get_hyperplane_distances(outside_nodes) |
|
344 |
|
|
|
345 |
|
node_potentials = outside_nodes.pdf(bx.tri_space)**(1/bx.nvar) * dh |
|
346 |
|
|
|
347 |
|
max_node = np.nanargmax(node_potentials) |
|
348 |
|
max_node_rating = node_potentials[max_node] * max_entropy |
|
349 |
|
|
|
350 |
|
if max_node_rating > bx.potential_index[-1]: |
|
351 |
|
#č nodes příjdou zabalené do CandyNodes. Ty mají .attrs a .kwargs |
|
352 |
|
outside_node = CandyNodes(outside_nodes[max_node]) |
|
353 |
|
|
|
354 |
|
outside_node.potential = node_potentials[max_node] |
|
355 |
|
outside_node.rating = max_node_rating |
|
356 |
|
|
|
357 |
|
bx.candidates_index[-1] = outside_node |
|
358 |
|
bx.potential_index.pop(-1) |
|
359 |
|
bx.potential_index[-1] = max_node_rating |
|
360 |
|
|
|
361 |
|
|
|
362 |
|
|
|
363 |
|
def estimate_outside(bx): |
|
364 |
|
#č konečně mám pořádnou stejtful třídu |
|
365 |
|
#č pokud mám aspoň jednu tečku, tak už je mi šuma |
|
366 |
|
#č zda se konvexní obálka vytvořila, či nikoliv |
|
367 |
|
|
|
368 |
|
#č Máme 2 úkoly: |
|
369 |
|
#č 1. Získat odhady a uložit je, abychom nemuseli opakovaně integrovat, |
|
370 |
|
#č dokud se neobjeví nějaký nový vzorek zvenku. |
|
371 |
|
#č 2. Získat kandidaty. |
|
372 |
|
#č a. z mezíkruží (-12) |
|
373 |
|
#č b. fire, to co navrhne QHull (-1) |
|
374 |
|
#č c. boom, doporuření QHull můžou i zklamat (-11) |
|
375 |
|
#č cc. ze vdálenejších galaxí (-111) |
|
376 |
|
|
|
377 |
|
#č prace s tečkami v mezikruži se změnila |
|
378 |
|
#č teď tečky dostávám přes kolbek po částech |
|
379 |
|
#č a není předem známo, kolik těch částí bude. |
|
380 |
|
bx.candidates_index.pop(-1, None) |
|
381 |
|
#č těch kastomných slovníků se bojím... |
|
382 |
|
bx.potential_index.pop(-1, None) |
|
383 |
|
bx.potential_index[-1] = 0 |
|
384 |
|
|
|
385 |
|
# get candidates! |
|
386 |
|
#č explicitně (pokažde) počtem teček zadavám přesnost integrace |
|
387 |
|
#č takže změny bx.shell_budget budou při dálším spuštění aplikovány |
|
388 |
|
data = bx.ghull.integrate(bx.shell_budget, \ |
|
389 |
|
callback_outside=bx._rate_outside_nodes) |
|
390 |
|
ghull_estimation, convex_hull_estimation, global_stats = data |
|
391 |
|
#č uložíme. Не жалко. |
|
392 |
|
#č první úkol máme splněný |
|
393 |
|
bx.ghull_estimation = ghull_estimation |
|
394 |
|
bx.convex_hull_estimation = convex_hull_estimation |
|
395 |
|
bx.global_stats = global_stats |
|
396 |
|
bx._logger(msg="outside estimation:", ghull_stats=global_stats) |
|
397 |
|
|
|
398 |
|
|
|
399 |
|
|
|
400 |
|
#č zde už nám jde pouze o kandidaty |
|
401 |
|
|
|
402 |
|
# fire |
|
403 |
|
bx._fire() |
|
404 |
|
# boom |
|
405 |
|
|
|
406 |
|
#č Projedeme Moravou |
|
407 |
|
#č tyhle funkce už vracej pouhý f_model |
|
408 |
|
bx._rate_outside_nodes(bx.ghull.boom(bx.outer_budget, use_MC=True)) |
|
409 |
|
|
|
410 |
|
#č Už máte Mléčnou dráhu projdutou? |
|
411 |
|
#č tyhle funkce už vracej pouhý f_model |
|
412 |
|
bx._rate_outside_nodes(bx.ghull.boom(bx.outer_budget, use_MC=False)) |
|
413 |
|
|
|
414 |
|
|
|
415 |
|
def _fire(bx): |
|
416 |
|
qhull = bx.ghull.hull |
|
417 |
|
nodes = qhull.fire(bx.outer_budget, use_MC=True) |
|
418 |
|
if nodes is not None: |
|
419 |
|
bx._rate_outside_nodes(nodes) |
|
420 |
|
|
|
421 |
|
|
|
422 |
|
def get_pf_estimation(bx): |
|
423 |
|
#č dle toho, čo vidím v kódu (spouští nás .increment()) |
|
424 |
|
#č přinejmenším konvexní obálka musí |
|
425 |
|
#č zajištěně existovat |
|
426 |
|
# convex_hull_estimation -2: inside, -1: outside |
|
427 |
|
pf_inside = bx.convex_hull_estimation[-2] |
|
428 |
|
pf_outside = bx.convex_hull_estimation[-1] |
|
429 |
|
|
|
430 |
|
#č Ghull spouštíme sporadicky, |
|
431 |
|
#č takže musíme sami lepit nové etikety |
|
432 |
|
bx.global_stats['nsim'] = bx.nsim |
|
433 |
|
|
|
434 |
|
failsi = bx.failsi |
|
435 |
|
|
|
436 |
|
if 'tri' in dir(bx): |
|
437 |
|
#č Tri.get_pf_estimation() vrací: |
|
438 |
|
# 'TRI_estimation': tri_estimation, 'global_stats': {mix, failure}, \ |
|
439 |
|
#'vertex_estimation' : vertex_estimation, \ |
|
440 |
|
#'weighted_vertex_estimation' : weighted_vertex_estimation, |
|
441 |
|
#'coplanar':sx.tri.coplanar} |
|
442 |
|
estimations = bx.Tri.get_pf_estimation() |
|
443 |
|
# TRI-compatible estimation |
|
444 |
|
# -1=outside, 0=success, 1=failure, 2=mix |
|
445 |
|
#č to je JustTriangulation, |
|
446 |
|
#č outside (-1), ani success (1) nebudou korektní |
|
447 |
|
tri_estimation = estimations.pop('TRI_estimation') |
|
448 |
|
tri_estimation[-1] = pf_outside |
|
449 |
|
tri_estimation[0] = pf_inside - tri_estimation[1] - tri_estimation[2] |
|
450 |
|
estimations['TRI_overall_estimations'] = tri_estimation |
|
451 |
|
estimations['ghull_estimation'] = bx.ghull_estimation |
|
452 |
|
|
|
453 |
|
#č hrozně důležitý. Těšíme se na csv-čko. |
|
454 |
|
bx.global_stats.update(estimations['global_stats']) |
|
455 |
|
bx.global_stats['success_points'] = len(failsi[~failsi]) |
|
456 |
|
bx.global_stats['failure_points'] = len(failsi[failsi]) |
|
457 |
|
bx.global_stats['success'] = tri_estimation[0] |
|
458 |
|
bx.global_stats['candidates_sets'] = len(bx.candidates_index) |
|
459 |
|
estimations['global_stats'].update(bx.global_stats) |
|
460 |
|
return estimations |
|
461 |
|
|
|
462 |
|
|
|
463 |
|
#оӵ триангуляци ӧвӧл, иськем... |
|
464 |
|
|
|
465 |
|
#č může se stát, že první dvě tečky už hned májí různé barvy, |
|
466 |
|
#č ale žádnej simplex ještě nemáme. |
|
467 |
|
#č takže celou skříňku prostě bereme jako simplex |
|
468 |
|
event, event_id, fr, wfr = sx.get_simplex_event(bx, weighting_space=bx.tri_space) |
|
469 |
|
# -1=outside, 0=success, 1=failure, 2=mix |
|
470 |
|
tri_estimation = {-1:pf_outside, 0:0, 1:0, 2:0} |
|
471 |
|
tri_estimation[event_id] = pf_inside |
|
472 |
|
|
|
473 |
|
vertex_estimation = pf_inside * fr |
|
474 |
|
weighted_vertex_estimation = pf_inside * wfr |
|
475 |
|
|
|
476 |
|
global_stats = bx.global_stats |
|
477 |
|
# outside dodá Ghull |
|
478 |
|
global_stats['success_points'] = len(failsi[~failsi]) |
|
479 |
|
global_stats['failure_points'] = len(failsi[failsi]) |
|
480 |
|
global_stats['success'] = tri_estimation[0] |
|
481 |
|
global_stats['failure'] = tri_estimation[1] |
|
482 |
|
global_stats['mix'] = tri_estimation[2] |
|
483 |
|
global_stats['vertex_estimation'] = vertex_estimation |
|
484 |
|
global_stats['weighted_vertex_estimation'] = weighted_vertex_estimation |
|
485 |
|
global_stats['nsimplex'] = 0 |
|
486 |
|
global_stats['tn_scheme'] = bx.scheme.name |
|
487 |
|
global_stats['tn_scheme_points'] = bx.scheme.points.shape[1] |
|
488 |
|
global_stats['newly_invalidated'] = 0 |
|
489 |
|
global_stats['newly_estimated'] = 0 |
|
490 |
|
global_stats['simplex_stats'] = 0 |
|
491 |
|
global_stats['candidates_sets'] = len(bx.candidates_index) |
|
492 |
|
global_stats['ncoplanar'] = 0 |
|
493 |
|
|
|
494 |
|
return {'TRI_overall_estimations': tri_estimation, \ |
|
495 |
|
'vertex_estimation' : vertex_estimation, \ |
|
496 |
|
'weighted_vertex_estimation' : weighted_vertex_estimation, \ |
|
497 |
|
'ghull_estimation' : bx.ghull_estimation} |
|
498 |
|
|
|
499 |
|
|
File wellmet/qt_gui/qt_dicebox.py changed (mode: 100644) (index 74db06c..10e4c37) |
... |
... |
import numpy as np |
3 |
3 |
from .. import schemes |
from .. import schemes |
4 |
4 |
from ..candybox import CandyBox |
from ..candybox import CandyBox |
5 |
5 |
from ..dicebox.goal import Goal, Razitko, DiceBox |
from ..dicebox.goal import Goal, Razitko, DiceBox |
|
6 |
|
from ..dicebox.circumtri import CircumTri |
6 |
7 |
import pyqtgraph as pg |
import pyqtgraph as pg |
7 |
8 |
from pyqtgraph.Qt import QtCore, QtWidgets |
from pyqtgraph.Qt import QtCore, QtWidgets |
8 |
9 |
|
|
9 |
10 |
spaces = ['R', 'aR', 'Rn', 'aRn', 'P', 'GK', 'G', 'aG', 'U', 'aU'] |
spaces = ['R', 'aR', 'Rn', 'aRn', 'P', 'GK', 'G', 'aG', 'U', 'aU'] |
10 |
11 |
potentials = ['q_psee', 'psee', 'fee', 'fee2', 'ksee', 'chee', 'chee2', 'dd'] |
potentials = ['q_psee', 'psee', 'fee', 'fee2', 'ksee', 'chee', 'chee2', 'dd'] |
11 |
12 |
|
|
|
13 |
|
|
|
14 |
|
class CircumTriWidget(pg.parametertree.ParameterTree): |
|
15 |
|
def __init__(self, wt, parent=None): |
|
16 |
|
self.box = wt |
|
17 |
|
super().__init__(parent=parent, showHeader=False) |
|
18 |
|
self._set_param() |
|
19 |
|
self.setParameters(self.param, showTop=False) |
|
20 |
|
|
|
21 |
|
def _set_param(self): |
|
22 |
|
params = list() |
|
23 |
|
tschemes = schemes.get_tn_keys(self.box.nvar) |
|
24 |
|
params.append({'name': 'scheme', 'type': 'list', \ |
|
25 |
|
'title': "cubature scheme", \ |
|
26 |
|
'values': tschemes, 'value': 'stroud_tn_2_1b'}) |
|
27 |
|
params.append({'name': 'degree', 'type': 'int', \ |
|
28 |
|
'title': "degree",\ |
|
29 |
|
'limits': (0, float('inf')), 'value': 5, 'default': 5,\ |
|
30 |
|
'tip': "Used only with Grundmann-Möller and Silvester cubaturas"}) |
|
31 |
|
|
|
32 |
|
params.append({'name': 'tri_space', 'type': 'list', 'value': 'G', \ |
|
33 |
|
'title': "triangulation space", 'values': spaces}) |
|
34 |
|
params.append({'name': 'entropy_mode', 'type': 'list', 'value': 'weighted', \ |
|
35 |
|
'title': "entropy mode", \ |
|
36 |
|
'values': ['none', 'simple', 'weighted']}) |
|
37 |
|
params.append({'name': 'shell_budget', 'type': 'int', \ |
|
38 |
|
'title': "shell budget", \ |
|
39 |
|
'limits': (1, float('inf')), 'value': 1000, 'default': 1000,\ |
|
40 |
|
'tip': "Number of annulus candidates"}) |
|
41 |
|
|
|
42 |
|
params.append({'name': 'outer_budget', 'type': 'int', \ |
|
43 |
|
'title': "Outer budget", \ |
|
44 |
|
'limits': (1, float('inf')), 'value': 100, 'default': 100,\ |
|
45 |
|
'tip': "Number of candidates nodes outside of circumscribed d-ball"}) |
|
46 |
|
|
|
47 |
|
|
|
48 |
|
### Create tree of Parameter objects |
|
49 |
|
# I don't know why that signals do not work for me |
|
50 |
|
# Only sigTreeStateChanged works, but I don't want to struggle with it |
|
51 |
|
# May be I'll report the issue |
|
52 |
|
#self.param.sigValueChanged.connect(self.param_changed) |
|
53 |
|
#self.param.sigValueChanging.connect(self.param_changing) |
|
54 |
|
self.param = pg.parametertree.Parameter.create(name='params', type='group', children=params) |
|
55 |
|
|
|
56 |
|
#č branima sa rekurzii |
|
57 |
|
#оӵ рекурзилы пезьдэт! |
|
58 |
|
self.param_values = self.param.getValues() |
|
59 |
|
|
|
60 |
|
def __getattr__(self, attr): |
|
61 |
|
#č na teoreticky možnou rěkurziju vykašleme |
|
62 |
|
#оӵ рекурзия уз луы |
|
63 |
|
return self.param_values[attr][0] |
|
64 |
|
|
|
65 |
|
def setup_box(self): |
|
66 |
|
#č to je důležité! __getatr__ odsaď bere hodnoty |
|
67 |
|
self.param_values = self.param.getValues() |
|
68 |
|
|
|
69 |
|
scheme = schemes.get_tn_scheme(self.scheme, self.box.nvar, self.degree) |
|
70 |
|
|
|
71 |
|
self.box.sample_box = CircumTri( |
|
72 |
|
self.box.sample_box, #č rekurze) |
|
73 |
|
scheme, |
|
74 |
|
tri_space=self.tri_space, |
|
75 |
|
entropy_mode=self.entropy_mode, |
|
76 |
|
shell_budget=self.shell_budget, |
|
77 |
|
outer_budget=self.outer_budget, |
|
78 |
|
) |
|
79 |
|
|
|
80 |
|
|
|
81 |
|
|
|
82 |
|
|
12 |
83 |
class DumbDiceBoxWidget(QtWidgets.QWidget): |
class DumbDiceBoxWidget(QtWidgets.QWidget): |
13 |
84 |
def __init__(self, wt, parent=None): |
def __init__(self, wt, parent=None): |
14 |
85 |
self.box = wt |
self.box = wt |
|
... |
... |
class SetupDiceBoxWidget(pg.LayoutWidget): |
280 |
351 |
#č aby je nám Python nehodil |
#č aby je nám Python nehodil |
281 |
352 |
self.tabs = [] |
self.tabs = [] |
282 |
353 |
|
|
|
354 |
|
box_widget = CircumTriWidget(self.box, self) |
|
355 |
|
self.tab_widget.addTab(box_widget, "CircumTri") |
|
356 |
|
self.tabs.append(box_widget) |
|
357 |
|
|
283 |
358 |
# Add tab |
# Add tab |
284 |
359 |
box_widget = GoalWidget(self.box, self) |
box_widget = GoalWidget(self.box, self) |
285 |
360 |
self.tab_widget.addTab(box_widget, "Goal") |
self.tab_widget.addTab(box_widget, "Goal") |