| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
# |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
from PyQt5.QtWidgets import QDialog |
| 25 |
|
| 26 |
from sympy import S |
| 27 |
from sympy.core.sympify import SympifyError |
| 28 |
|
| 29 |
from .repetition_ui import Ui_DialogRepetition |
| 30 |
from wxgeometrie.geolib.routines import nice_str |
| 31 |
|
| 32 |
|
| 33 |
def repetition_experiences(_profondeur=3, _numeroter=True, evts=[], probas=[]): |
| 34 |
"""Génère le code d'un arbre de probabilités correspondant à la répétition |
| 35 |
d'expériences aléatoires identiques et indépendantes. |
| 36 |
Typiquement, un schéma de Bernoulli. |
| 37 |
|
| 38 |
>>> from wxgeometrie.modules.probabilites import repetition_experiences |
| 39 |
>>> print(repetition_experiences()) |
| 40 |
>A_1:0.5 |
| 41 |
>>A_2:0.5 |
| 42 |
>>>A_3:0.5 |
| 43 |
>>>&A_3:0.5 |
| 44 |
>>&A_2:0.5 |
| 45 |
>>>A_3:0.5 |
| 46 |
>>>&A_3:0.5 |
| 47 |
>&A_1:0.5 |
| 48 |
>>A_2:0.5 |
| 49 |
>>>A_3:0.5 |
| 50 |
>>>&A_3:0.5 |
| 51 |
>>&A_2:0.5 |
| 52 |
>>>A_3:0.5 |
| 53 |
>>>&A_3:0.5 |
| 54 |
""" |
| 55 |
|
| 56 |
if not evts: |
| 57 |
evts = ['A'] |
| 58 |
if len(evts) == 1: |
| 59 |
e. |
| 60 |
evt = evts[0] |
| 61 |
evts.append(evt[1:] if evt.startswith('&') else '&' + evt) |
| 62 |
|
| 63 |
tés |
| 64 |
. |
| 65 |
if len(probas) > len(evts): |
| 66 |
evts += (len(probas) - len(evts))*[''] |
| 67 |
else: |
| 68 |
if len(probas) == len(evts) - 1: |
| 69 |
try: |
| 70 |
probas.append(nice_str(1 - sum(S(proba.replace(',', '.')) for proba in probas))) |
| 71 |
except SympifyError: |
| 72 |
pass |
| 73 |
if len(probas) < len(evts): |
| 74 |
probas += (len(evts) - len(probas))*[''] |
| 75 |
|
| 76 |
e. |
| 77 |
lines = [''] |
| 78 |
for niveau in range(1, _profondeur + 1): |
| 79 |
prefixe = niveau*'>' |
| 80 |
suffixe = ('_' + str(niveau) if _numeroter else '') |
| 81 |
for i in range(len(lines), 0, -1): |
| 82 |
if lines[i - 1].startswith((niveau - 1)*'>'): |
| 83 |
for evt, proba in reversed(list(zip(evts, probas))): |
| 84 |
|
| 85 |
lines.insert(i, prefixe + evt + suffixe + ':' + proba) |
| 86 |
assert len(lines) < 10000 |
| 87 |
return '\n'.join(lines).strip() |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
class DialogRepetition(QDialog, Ui_DialogRepetition): |
| 92 |
def __init__(self, parent): |
| 93 |
QDialog.__init__(self, parent) |
| 94 |
|
| 95 |
|
| 96 |
self.setupUi(self) |
| 97 |
|
| 98 |
def accept(self): |
| 99 |
n = self.niveaux.value() |
| 100 |
num = self.numeroter.isChecked() |
| 101 |
evts = [evt.strip() for evt in self.evenements.text().split(';')] |
| 102 |
probas = [proba.strip() for proba in self.probas.text().split(';')] |
| 103 |
|
| 104 |
code = repetition_experiences(n, num, evts, probas) |
| 105 |
self.parent().instructions.setPlainText(code) |
| 106 |
self.parent().appliquer.click() |