| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
# |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
import re |
| 25 |
|
| 26 |
from PyQt5.QtWidgets import QVBoxLayout, QLabel, QPushButton, QHBoxLayout,\ |
| 27 |
QMessageBox, QTextEdit |
| 28 |
|
| 29 |
from wxgeometrie.GUI.menu import MenuBar |
| 30 |
from wxgeometrie.GUI.panel import Panel_API_graphique |
| 31 |
from wxgeometrie.GUI.proprietes_objets import Proprietes |
| 32 |
from wxgeometrie.geolib import Segment, Texte, Point |
| 33 |
from wxgeometrie import param |
| 34 |
from .repetition import DialogRepetition, repetition_experiences |
| 35 |
from wxgeometrie.mathlib.parsers import VAR |
| 36 |
|
| 37 |
|
| 38 |
class ProbaMenuBar(MenuBar): |
| 39 |
def __init__(self, panel): |
| 40 |
MenuBar.__init__(self, panel) |
| 41 |
|
| 42 |
self.ajouter("Fichier", ["nouveau"], ["ouvrir"], ["ouvrir ici"], None, |
| 43 |
["enregistrer"], ["enregistrer_sous"], ["exporter"], |
| 44 |
["exporter&sauver"], None, ['session'], None, |
| 45 |
["imprimer"], ["presse-papier"], None, |
| 46 |
["proprietes"], None, self.panel.doc_ouverts, None, |
| 47 |
["fermer"], ["quitter"]) |
| 48 |
self.ajouter("Editer", ["annuler"], ["refaire"], ["modifier"], ["supprimer"]) |
| 49 |
self.ajouter("creer") |
| 50 |
self.ajouter("Affichage", ["onglet"], ["plein_ecran"], None, ["repere"], ["quadrillage"], |
| 51 |
["orthonorme"], None, ["zoom_texte"], |
| 52 |
["zoom_ligne"], ["zoom_general"], None, |
| 53 |
["fenetre"], ["zoomer"], ["dezoomer"], |
| 54 |
["orthonormaliser"], ["zoom_auto"]) |
| 55 |
self.ajouter("Autres actions", ["detecter"], |
| 56 |
["Répétition d'expériences indépendantes", |
| 57 |
"Construire un arbre correspondant à la répétition d'expériences aléatoires indépendantes.", |
| 58 |
None, self.panel.repeter_experiences_independantes]) |
| 59 |
self.ajouter("Outils", ["Style des sommets", "Modifier le style des sommets de l'arbre.", |
| 60 |
None, self.panel.proprietes_sommets], |
| 61 |
["Style des arêtes", "Modifier le style des arêtes de l'arbre.", |
| 62 |
None, self.panel.proprietes_aretes], |
| 63 |
["Style de la légende", "Modifier le style des titres de chaque niveau.", |
| 64 |
None, self.panel.proprietes_titres], |
| 65 |
None, |
| 66 |
["options"]) |
| 67 |
) |
| 68 |
self.ajouter("avance1") |
| 69 |
self.ajouter("?") |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
class Probabilites(Panel_API_graphique): |
| 75 |
|
| 76 |
titre = "Arbre de probabilités"e |
| 77 |
|
| 78 |
def __init__(self, *args, **kw): |
| 79 |
Panel_API_graphique.__init__(self, *args, **kw) |
| 80 |
|
| 81 |
self.entrees = QVBoxLayout() |
| 82 |
|
| 83 |
self.entrees.addWidget(QLabel(" Instructions :")) |
| 84 |
|
| 85 |
self.instructions = QTextEdit(self) |
| 86 |
self.instructions.setMinimumSize(200, 300) |
| 87 |
self.instructions.setPlainText("""||Tirage 1|Tirage 2|Tirage 3 |
| 88 |
omega |
| 89 |
>A:0,7 |
| 90 |
>>B:0,2 |
| 91 |
>>C:0,8 |
| 92 |
>&A:0,3 |
| 93 |
>>E:0,1 |
| 94 |
>>>F |
| 95 |
>>>G |
| 96 |
>>>H |
| 97 |
>>&E:0,9""") |
| 98 |
self.entrees.addWidget(self.instructions) |
| 99 |
self.appliquer = QPushButton("Générer l'arbre", self) |
| 100 |
self.appliquer.clicked.connect(self.Appliquer) |
| 101 |
self.entrees.addWidget(self.appliquer) |
| 102 |
|
| 103 |
self.sizer = QHBoxLayout() |
| 104 |
self.sizer.addWidget(self.canvas, 1) |
| 105 |
self.sizer.addLayout(self.entrees, 0) |
| 106 |
self.finaliser(contenu=self.sizer) |
| 107 |
|
| 108 |
|
| 109 |
def _sauvegarder(self, fgeo, feuille = None): |
| 110 |
Panel_API_graphique._sauvegarder(self, fgeo, feuille) |
| 111 |
fgeo.contenu["Instructions"] = [self.instructions.toPlainText()] |
| 112 |
|
| 113 |
|
| 114 |
def _ouvrir(self, fgeo): |
| 115 |
Panel_API_graphique._ouvrir(self, fgeo) |
| 116 |
if "Instructions" in fgeo.contenu: |
| 117 |
self.instructions.setPlainText(fgeo.contenu["Instructions"][0]) |
| 118 |
|
| 119 |
|
| 120 |
def Appliquer(self, event): |
| 121 |
with self.canvas.geler_affichage(actualiser=True, sablier=True): |
| 122 |
self.creer_feuille() |
| 123 |
instructions = [instruction for instruction in self.instructions.toPlainText().split("\n") if instruction.strip()] |
| 124 |
nbr_colonnes = 0 |
| 125 |
if instructions[0].startswith("|"): |
| 126 |
legende = instructions[0] |
| 127 |
instructions = instructions[1:] |
| 128 |
self.canvas.fenetre = -.1, 1.1, -.1, 1.2 |
| 129 |
else: |
| 130 |
legende = None |
| 131 |
self.canvas.fenetre = -.1, 1.1, -.1, 1.1 |
| 132 |
|
| 133 |
antes |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
à: |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
if instructions[0].startswith('>>'): |
| 148 |
profondeur = len(instructions[0]) - len(instructions[0].lstrip('>')) |
| 149 |
evenements = {} |
| 150 |
for instruction in instructions: |
| 151 |
assert instruction.startswith(profondeur*'>') |
| 152 |
nom, proba = instruction[profondeur:].split(':') |
| 153 |
evenements[nom.strip()] = proba.strip() |
| 154 |
instructions = repetition_experiences(_profondeur=profondeur, _numeroter=True, **evenements).split('\n') |
| 155 |
|
| 156 |
for instruction in instructions: |
| 157 |
if instruction.startswith(">"): |
| 158 |
nbr_colonnes = max(nbr_colonnes, len(instruction) - len(instruction.lstrip(">"))) |
| 159 |
if instructions[0].startswith(">"): |
| 160 |
instructions = [""] + instructions |
| 161 |
nbr_colonnes += 1 |
| 162 |
nbr_lignes = [] |
| 163 |
for i in range(nbr_colonnes): |
| 164 |
nbr_lignes.append(len([instruction for instruction in instructions if instruction.startswith(i*">") and not instruction.startswith((i+1)*">")])) |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
s |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
arbre = [] |
| 182 |
ligne_precedente = [-1 for i in range(nbr_colonnes)] e |
| 183 |
for instruction in instructions: |
| 184 |
colonne = len(instruction) - len(instruction.lstrip(">")) |
| 185 |
branche = arbre |
| 186 |
for i in range(colonne): ) |
| 187 |
branche = branche[ligne_precedente[i]]["liste"] |
| 188 |
branche.append({"texte": instruction.lstrip(">"), "liste": []}) |
| 189 |
ligne_precedente[colonne] += 1 |
| 190 |
for i in range(colonne + 1, nbr_colonnes): |
| 191 |
ligne_precedente[i] = -1 |
| 192 |
print(arbre) |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
def compter_ramifications(branche): |
| 197 |
if len(branche["liste"]) > 0: |
| 198 |
return sum(compter_ramifications(tige) for tige in branche["liste"]) |
| 199 |
else: |
| 200 |
return 1 |
| 201 |
|
| 202 |
ramifications = sum(compter_ramifications(branche) for branche in arbre) |
| 203 |
if param.debug: |
| 204 |
print("Nombre de ramifications: " + str(ramifications)) |
| 205 |
|
| 206 |
def formater_texte(texte): |
| 207 |
if texte: |
| 208 |
if texte.startswith("&"): |
| 209 |
texte = r"\overline{" + texte[1:] + "}" |
| 210 |
texte = texte.replace("&", r"\overline ") |
| 211 |
|
| 212 |
texte = re.sub("(\\\\overline)[ ]+(%s)" % VAR, lambda m:'%s{%s}' % (m.group(1), m.group(2)), texte) |
| 213 |
texte = "$" + texte + "$" if texte[0] != '$' else texte |
| 214 |
if param.latex: |
| 215 |
texte = "$" + texte + "$" |
| 216 |
texte = texte.replace(" inter ", r"\ \cap \ ").replace(" union ", r"\ \cup \ ").replace("Omega", r"\Omega").replace("omega", r"\Omega") |
| 217 |
if param.latex: |
| 218 |
texte = re.sub("[0-9]+/[0-9]+",lambda s:"\\frac{" + s.group().replace("/", "}{") + "}", texte) |
| 219 |
return texte |
| 220 |
|
| 221 |
|
| 222 |
def creer_point(x, y, texte): |
| 223 |
texte = formater_texte(texte) |
| 224 |
M = Point(x, y, style = "o", couleur = "w", taille = 0) |
| 225 |
M.label(texte) |
| 226 |
M.etiquette.style(_rayon_=0, niveau=15, alignement_vertical="center", |
| 227 |
alignement_horizontal="center", fond=True, |
| 228 |
couleur_fond="w") |
| 229 |
return M |
| 230 |
|
| 231 |
|
| 232 |
def creer_segment(point1, point2, texte): |
| 233 |
texte = formater_texte(texte) |
| 234 |
s = Segment(point1, point2) |
| 235 |
s.label(texte) |
| 236 |
style = {'_rayon_': 0, 'niveau': 15} |
| 237 |
placement = self.param('placement_probabilites') |
| 238 |
if placement == 'dessus': |
| 239 |
style.update(alignement_vertical='center', couleur_fond='w', |
| 240 |
fond=True, alignement_horizontal='center', |
| 241 |
angle=0) |
| 242 |
elif placement == 'longe': |
| 243 |
style.update(alignement_vertical='auto', fond=False, |
| 244 |
alignement_horizontal='center', angle='auto') |
| 245 |
elif placement == 'decale': |
| 246 |
style.update(alignement_vertical='auto', fond=False, |
| 247 |
alignement_horizontal='right', angle=0) |
| 248 |
else: |
| 249 |
print("Placement: mode '%s' non reconnu." % placement) |
| 250 |
s.etiquette.style(**style) |
| 251 |
return s |
| 252 |
|
| 253 |
ramification = [0] |
| 254 |
def parcourir_branche(branche, n, ramification = ramification): |
| 255 |
txt = branche["texte"].rsplit(":", 1) |
| 256 |
if len(txt) == 2: |
| 257 |
txt_pt, txt_segm = txt |
| 258 |
else: |
| 259 |
txt_pt = txt[0] |
| 260 |
txt_segm = "" |
| 261 |
if len(branche["liste"]) > 0: |
| 262 |
l = [] |
| 263 |
for tige in branche["liste"]: |
| 264 |
l.append(parcourir_branche(tige, n + 1)) |
| 265 |
|
| 266 |
M = creer_point(n/(nbr_colonnes - 1), .5*(l[0][0].ordonnee+l[-1][0].ordonnee), txt_pt) |
| 267 |
self.feuille_actuelle.objets.add(M) |
| 268 |
for point, txt_s in l: |
| 269 |
s = creer_segment(M, point, txt_s) |
| 270 |
self.feuille_actuelle.objets.add(s) |
| 271 |
return M, txt_segm |
| 272 |
else: |
| 273 |
if ramifications > 1: |
| 274 |
y = 1 - ramification[0]/(ramifications - 1) |
| 275 |
else: |
| 276 |
y = .5 |
| 277 |
M = creer_point(n/(nbr_colonnes - 1), y, txt_pt) |
| 278 |
ramification[0] += 1 |
| 279 |
self.feuille_actuelle.objets.add(M) |
| 280 |
return M, txt_segm |
| 281 |
|
| 282 |
for branche in arbre: |
| 283 |
parcourir_branche(branche, 0) |
| 284 |
|
| 285 |
if legende is not None: |
| 286 |
decalage = -0.5 |
| 287 |
for i in legende: |
| 288 |
if i != "|": |
| 289 |
break |
| 290 |
decalage += .5 |
| 291 |
legende = legende.strip("|").split("|") |
| 292 |
for n in range(len(legende)): |
| 293 |
t = Texte(legende[n], (n + decalage)/(nbr_colonnes - 1), 1.1) |
| 294 |
self.feuille_actuelle.objets.add(t) |
| 295 |
|
| 296 |
self.feuille_actuelle.interprete.commande_executee() |
| 297 |
|
| 298 |
def info_proprietes(self, titre): |
| 299 |
QMessageBox.warning(self, titre, "Créez l'arbre au préalable.") |
| 300 |
|
| 301 |
def proprietes_sommets(self, event = None): |
| 302 |
objets = self.feuille_actuelle.objets.lister(type=Point) |
| 303 |
if not objets: |
| 304 |
self.info_proprietes('Aucun sommet.') |
| 305 |
return |
| 306 |
win = Proprietes(self, objets) |
| 307 |
win.show() |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
def proprietes_aretes(self, event = None): |
| 312 |
objets = self.feuille_actuelle.objets.lister(type=Segment) |
| 313 |
if not objets: |
| 314 |
self.info_proprietes('Aucune arête.') |
| 315 |
return |
| 316 |
win = Proprietes(self, objets) |
| 317 |
win.show() |
| 318 |
|
| 319 |
|
| 320 |
def proprietes_titres(self, event = None): |
| 321 |
objets = self.feuille_actuelle.objets.lister(type=Texte) |
| 322 |
if not objets: |
| 323 |
self.info_proprietes('Aucun titre.') |
| 324 |
return |
| 325 |
win = Proprietes(self, objets) |
| 326 |
win.show() |
| 327 |
|
| 328 |
|
| 329 |
def repeter_experiences_independantes(self): |
| 330 |
dlg = DialogRepetition(self) |
| 331 |
dlg.show() |
| 332 |
|
| 333 |
def _affiche(self): |
| 334 |
pass |
| 335 |
|
| 336 |
def assistant(self, event = None, liste = None): |
| 337 |
"""Crée un arbre en supposant les évènements de la liste tous indépendants entre eux. |
| 338 |
|
| 339 |
Exemple: |
| 340 |
self.assistant(liste = ["A:0.4", "B:0.7"]) |
| 341 |
|
| 342 |
On peut spécifier explicitement les évènements contraires: |
| 343 |
self.assistant(liste = [("G_1:0.4", "P_1:0.6"), ("G_2:0.5", "P_2:0.5")])""" |
| 344 |
|
| 345 |
|
| 346 |
lignes = [""] |
| 347 |
niveau = 0 |
| 348 |
for couple in liste: |
| 349 |
if isinstance(couple, str): |
| 350 |
evt1 = couple |
| 351 |
evt2 = None |
| 352 |
else: |
| 353 |
evt1, evt2 = couple |
| 354 |
|
| 355 |
niveau += 1 |
| 356 |
|
| 357 |
for i in range(len(lignes) - 1, -1, -1): |
| 358 |
if evt2 is None: |
| 359 |
if ":" in evt1: |
| 360 |
evt, proba = evt1.rsplit(":", 1) |
| 361 |
else: |
| 362 |
evt = evt1 |
| 363 |
proba = "" |
| 364 |
try: |
| 365 |
proba = str(1-float(proba)) |
| 366 |
except ValueError: |
| 367 |
proba = "1-" + proba |
| 368 |
evt2 = "&" + evt + ":" + proba |
| 369 |
|
| 370 |
lignes.insert(i, niveau*">" + evt2) |
| 371 |
lignes.insert(i, niveau*">" + evt1) |
| 372 |
|
| 373 |
print("test", "\n".join(lignes)) |
| 374 |
self.instructions.setPlainText("\n".join(lignes)) |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
def bernouilli(self, event = None, n = 3, evt1 = "", evt2 = None): |
| 379 |
self.assistant(liste = n*[(evt1, evt2)]) |