pyAgrum-nightly 2.3.1.9.dev202512261765915415__cp310-abi3-macosx_10_15_x86_64.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.
Files changed (107) hide show
  1. pyagrum/__init__.py +165 -0
  2. pyagrum/_pyagrum.so +0 -0
  3. pyagrum/bnmixture/BNMInference.py +268 -0
  4. pyagrum/bnmixture/BNMLearning.py +376 -0
  5. pyagrum/bnmixture/BNMixture.py +464 -0
  6. pyagrum/bnmixture/__init__.py +60 -0
  7. pyagrum/bnmixture/notebook.py +1058 -0
  8. pyagrum/causal/_CausalFormula.py +280 -0
  9. pyagrum/causal/_CausalModel.py +436 -0
  10. pyagrum/causal/__init__.py +81 -0
  11. pyagrum/causal/_causalImpact.py +356 -0
  12. pyagrum/causal/_dSeparation.py +598 -0
  13. pyagrum/causal/_doAST.py +761 -0
  14. pyagrum/causal/_doCalculus.py +361 -0
  15. pyagrum/causal/_doorCriteria.py +374 -0
  16. pyagrum/causal/_exceptions.py +95 -0
  17. pyagrum/causal/_types.py +61 -0
  18. pyagrum/causal/causalEffectEstimation/_CausalEffectEstimation.py +1175 -0
  19. pyagrum/causal/causalEffectEstimation/_IVEstimators.py +718 -0
  20. pyagrum/causal/causalEffectEstimation/_RCTEstimators.py +132 -0
  21. pyagrum/causal/causalEffectEstimation/__init__.py +46 -0
  22. pyagrum/causal/causalEffectEstimation/_backdoorEstimators.py +774 -0
  23. pyagrum/causal/causalEffectEstimation/_causalBNEstimator.py +324 -0
  24. pyagrum/causal/causalEffectEstimation/_frontdoorEstimators.py +396 -0
  25. pyagrum/causal/causalEffectEstimation/_learners.py +118 -0
  26. pyagrum/causal/causalEffectEstimation/_utils.py +466 -0
  27. pyagrum/causal/notebook.py +172 -0
  28. pyagrum/clg/CLG.py +658 -0
  29. pyagrum/clg/GaussianVariable.py +111 -0
  30. pyagrum/clg/SEM.py +312 -0
  31. pyagrum/clg/__init__.py +63 -0
  32. pyagrum/clg/canonicalForm.py +408 -0
  33. pyagrum/clg/constants.py +54 -0
  34. pyagrum/clg/forwardSampling.py +202 -0
  35. pyagrum/clg/learning.py +776 -0
  36. pyagrum/clg/notebook.py +480 -0
  37. pyagrum/clg/variableElimination.py +271 -0
  38. pyagrum/common.py +60 -0
  39. pyagrum/config.py +319 -0
  40. pyagrum/ctbn/CIM.py +513 -0
  41. pyagrum/ctbn/CTBN.py +573 -0
  42. pyagrum/ctbn/CTBNGenerator.py +216 -0
  43. pyagrum/ctbn/CTBNInference.py +459 -0
  44. pyagrum/ctbn/CTBNLearner.py +161 -0
  45. pyagrum/ctbn/SamplesStats.py +671 -0
  46. pyagrum/ctbn/StatsIndepTest.py +355 -0
  47. pyagrum/ctbn/__init__.py +79 -0
  48. pyagrum/ctbn/constants.py +54 -0
  49. pyagrum/ctbn/notebook.py +264 -0
  50. pyagrum/defaults.ini +199 -0
  51. pyagrum/deprecated.py +95 -0
  52. pyagrum/explain/_ComputationCausal.py +75 -0
  53. pyagrum/explain/_ComputationConditional.py +48 -0
  54. pyagrum/explain/_ComputationMarginal.py +48 -0
  55. pyagrum/explain/_CustomShapleyCache.py +110 -0
  56. pyagrum/explain/_Explainer.py +176 -0
  57. pyagrum/explain/_Explanation.py +70 -0
  58. pyagrum/explain/_FIFOCache.py +54 -0
  59. pyagrum/explain/_ShallCausalValues.py +204 -0
  60. pyagrum/explain/_ShallConditionalValues.py +155 -0
  61. pyagrum/explain/_ShallMarginalValues.py +155 -0
  62. pyagrum/explain/_ShallValues.py +296 -0
  63. pyagrum/explain/_ShapCausalValues.py +208 -0
  64. pyagrum/explain/_ShapConditionalValues.py +126 -0
  65. pyagrum/explain/_ShapMarginalValues.py +191 -0
  66. pyagrum/explain/_ShapleyValues.py +298 -0
  67. pyagrum/explain/__init__.py +81 -0
  68. pyagrum/explain/_explGeneralizedMarkovBlanket.py +152 -0
  69. pyagrum/explain/_explIndependenceListForPairs.py +146 -0
  70. pyagrum/explain/_explInformationGraph.py +264 -0
  71. pyagrum/explain/notebook/__init__.py +54 -0
  72. pyagrum/explain/notebook/_bar.py +142 -0
  73. pyagrum/explain/notebook/_beeswarm.py +174 -0
  74. pyagrum/explain/notebook/_showShapValues.py +97 -0
  75. pyagrum/explain/notebook/_waterfall.py +220 -0
  76. pyagrum/explain/shapley.py +225 -0
  77. pyagrum/lib/__init__.py +46 -0
  78. pyagrum/lib/_colors.py +390 -0
  79. pyagrum/lib/bn2graph.py +299 -0
  80. pyagrum/lib/bn2roc.py +1026 -0
  81. pyagrum/lib/bn2scores.py +217 -0
  82. pyagrum/lib/bn_vs_bn.py +605 -0
  83. pyagrum/lib/cn2graph.py +305 -0
  84. pyagrum/lib/discreteTypeProcessor.py +1102 -0
  85. pyagrum/lib/discretizer.py +58 -0
  86. pyagrum/lib/dynamicBN.py +390 -0
  87. pyagrum/lib/explain.py +57 -0
  88. pyagrum/lib/export.py +84 -0
  89. pyagrum/lib/id2graph.py +258 -0
  90. pyagrum/lib/image.py +387 -0
  91. pyagrum/lib/ipython.py +307 -0
  92. pyagrum/lib/mrf2graph.py +471 -0
  93. pyagrum/lib/notebook.py +1821 -0
  94. pyagrum/lib/proba_histogram.py +552 -0
  95. pyagrum/lib/utils.py +138 -0
  96. pyagrum/pyagrum.py +31495 -0
  97. pyagrum/skbn/_MBCalcul.py +242 -0
  98. pyagrum/skbn/__init__.py +49 -0
  99. pyagrum/skbn/_learningMethods.py +282 -0
  100. pyagrum/skbn/_utils.py +297 -0
  101. pyagrum/skbn/bnclassifier.py +1014 -0
  102. pyagrum_nightly-2.3.1.9.dev202512261765915415.dist-info/LICENSE.md +12 -0
  103. pyagrum_nightly-2.3.1.9.dev202512261765915415.dist-info/LICENSES/LGPL-3.0-or-later.txt +304 -0
  104. pyagrum_nightly-2.3.1.9.dev202512261765915415.dist-info/LICENSES/MIT.txt +18 -0
  105. pyagrum_nightly-2.3.1.9.dev202512261765915415.dist-info/METADATA +145 -0
  106. pyagrum_nightly-2.3.1.9.dev202512261765915415.dist-info/RECORD +107 -0
  107. pyagrum_nightly-2.3.1.9.dev202512261765915415.dist-info/WHEEL +4 -0
pyagrum/lib/_colors.py ADDED
@@ -0,0 +1,390 @@
1
+ ############################################################################
2
+ # This file is part of the aGrUM/pyAgrum library. #
3
+ # #
4
+ # Copyright (c) 2005-2025 by #
5
+ # - Pierre-Henri WUILLEMIN(_at_LIP6) #
6
+ # - Christophe GONZALES(_at_AMU) #
7
+ # #
8
+ # The aGrUM/pyAgrum library is free software; you can redistribute it #
9
+ # and/or modify it under the terms of either : #
10
+ # #
11
+ # - the GNU Lesser General Public License as published by #
12
+ # the Free Software Foundation, either version 3 of the License, #
13
+ # or (at your option) any later version, #
14
+ # - the MIT license (MIT), #
15
+ # - or both in dual license, as here. #
16
+ # #
17
+ # (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) #
18
+ # #
19
+ # This aGrUM/pyAgrum library is distributed in the hope that it will be #
20
+ # useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, #
21
+ # INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS #
22
+ # FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
23
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
24
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, #
25
+ # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR #
26
+ # OTHER DEALINGS IN THE SOFTWARE. #
27
+ # #
28
+ # See LICENCES for more details. #
29
+ # #
30
+ # SPDX-FileCopyrightText: Copyright 2005-2025 #
31
+ # - Pierre-Henri WUILLEMIN(_at_LIP6) #
32
+ # - Christophe GONZALES(_at_AMU) #
33
+ # SPDX-License-Identifier: LGPL-3.0-or-later OR MIT #
34
+ # #
35
+ # Contact : info_at_agrum_dot_org #
36
+ # homepage : http://agrum.gitlab.io #
37
+ # gitlab : https://gitlab.com/agrumery/agrum #
38
+ # #
39
+ ############################################################################
40
+
41
+ """
42
+ Color manipulations for pyagrum.lib module
43
+ """
44
+
45
+ from typing import List, Tuple
46
+
47
+ import matplotlib as mpl
48
+ import matplotlib.colors
49
+
50
+ import pyagrum as gum
51
+ from pyagrum.lib.utils import getBlackInTheme
52
+
53
+ HTMLCOLORS = {
54
+ "AliceBlue": "F0F8FF",
55
+ "antiquewhite": "FAEBD7",
56
+ "aqua": "00FFFF",
57
+ "aquamarine": "7FFFD4",
58
+ "azure": "F0FFFF",
59
+ "beige": "F5F5DC",
60
+ "bisque": "FFE4C4",
61
+ "black": "000000",
62
+ "blanchedalmond": "FFEBCD",
63
+ "blue": "0000FF",
64
+ "blueviolet": "8A2BE2",
65
+ "brown": "A52A2A",
66
+ "burlywood": "DEB887",
67
+ "cadetblue": "5F9EA0",
68
+ "chartreuse": "7FFF00",
69
+ "chocolate": "D2691E",
70
+ "coral": "FF7F50",
71
+ "cornflowerblue": "6495ED",
72
+ "cornsilk": "FFF8DC",
73
+ "crimson": "DC143C",
74
+ "cyan": "00FFFF",
75
+ "darkblue": "00008B",
76
+ "darkcyan": "008B8B",
77
+ "darkgoldenrod": "B8860B",
78
+ "darkgray": "A9A9A9",
79
+ "darkgrey": "A9A9A9",
80
+ "darkgreen": "006400",
81
+ "darkkhaki": "BDB76B",
82
+ "darkmagenta": "8B008B",
83
+ "darkolivegreen": "556B2F",
84
+ "darkorange": "FF8C00",
85
+ "darkorchid": "9932CC",
86
+ "darkred": "8B0000",
87
+ "darksalmon": "E9967A",
88
+ "darkseagreen": "8FBC8F",
89
+ "darkslateblue": "483D8B",
90
+ "darkslategray": "2F4F4F",
91
+ "darkslategrey": "2F4F4F",
92
+ "darkturquoise": "00CED1",
93
+ "darkviolet": "9400D3",
94
+ "deeppink": "FF1493",
95
+ "deepskyblue": "00BFFF",
96
+ "dimgray": "696969",
97
+ "dimgrey": "696969",
98
+ "dodgerblue": "1E90FF",
99
+ "firebrick": "B22222",
100
+ "floralwhite": "FFFAF0",
101
+ "forestgreen": "228B22",
102
+ "fuchsia": "FF00FF",
103
+ "gainsboro": "DCDCDC",
104
+ "ghostwhite": "F8F8FF",
105
+ "gold": "FFD700",
106
+ "goldenrod": "DAA520",
107
+ "gray": "808080",
108
+ "grey": "808080",
109
+ "green": "008000",
110
+ "greenyellow": "ADFF2F",
111
+ "honeydew": "F0FFF0",
112
+ "hotpink": "FF69B4",
113
+ "indianred ": "CD5C5C",
114
+ "indigo ": "4B0082",
115
+ "ivory": "FFFFF0",
116
+ "khaki": "F0E68C",
117
+ "lavender": "E6E6FA",
118
+ "lavenderblush": "FFF0F5",
119
+ "lawngreen": "7CFC00",
120
+ "lemonchiffon": "FFFACD",
121
+ "lightblue": "ADD8E6",
122
+ "lightcoral": "F08080",
123
+ "lightcyan": "E0FFFF",
124
+ "lightgoldenrodyellow": "FAFAD2",
125
+ "lightgray": "D3D3D3",
126
+ "lightgrey": "D3D3D3",
127
+ "lightgreen": "90EE90",
128
+ "lightpink": "FFB6C1",
129
+ "lightsalmon": "FFA07A",
130
+ "lightseagreen": "20B2AA",
131
+ "lightskyblue": "87CEFA",
132
+ "lightslategray": "778899",
133
+ "lightslategrey": "778899",
134
+ "lightsteelblue": "B0C4DE",
135
+ "lightyellow": "FFFFE0",
136
+ "lime": "00FF00",
137
+ "limegreen": "32CD32",
138
+ "linen": "FAF0E6",
139
+ "magenta": "FF00FF",
140
+ "maroon": "800000",
141
+ "mediumaquamarine": "66CDAA",
142
+ "mediumblue": "0000CD",
143
+ "mediumorchid": "BA55D3",
144
+ "mediumpurple": "9370DB",
145
+ "mediumseagreen": "3CB371",
146
+ "mediumslateblue": "7B68EE",
147
+ "mediumspringgreen": "00FA9A",
148
+ "mediumturquoise": "48D1CC",
149
+ "mediumvioletred": "C71585",
150
+ "midnightblue": "191970",
151
+ "mintcream": "F5FFFA",
152
+ "mistyrose": "FFE4E1",
153
+ "moccasin": "FFE4B5",
154
+ "navajowhite": "FFDEAD",
155
+ "navy": "000080",
156
+ "oldlace": "FDF5E6",
157
+ "olive": "808000",
158
+ "olivedrab": "6B8E23",
159
+ "orange": "FFA500",
160
+ "orangered": "FF4500",
161
+ "orchid": "DA70D6",
162
+ "palegoldenrod": "EEE8AA",
163
+ "palegreen": "98FB98",
164
+ "paleturquoise": "AFEEEE",
165
+ "palevioletred": "DB7093",
166
+ "papayawhip": "FFEFD5",
167
+ "peachpuff": "FFDAB9",
168
+ "peru": "CD853F",
169
+ "pink": "FFC0CB",
170
+ "plum": "DDA0DD",
171
+ "powderblue": "B0E0E6",
172
+ "purple": "800080",
173
+ "rebeccapurple": "663399",
174
+ "red": "FF0000",
175
+ "rosybrown": "BC8F8F",
176
+ "royalblue": "4169E1",
177
+ "saddlebrown": "8B4513",
178
+ "salmon": "FA8072",
179
+ "sandybrown": "F4A460",
180
+ "seagreen": "2E8B57",
181
+ "seashell": "FFF5EE",
182
+ "sienna": "A0522D",
183
+ "silver": "C0C0C0",
184
+ "skyblue": "87CEEB",
185
+ "slateblue": "6A5ACD",
186
+ "slategray": "708090",
187
+ "slategrey": "708090",
188
+ "snow": "FFFAFA",
189
+ "springgreen": "00FF7F",
190
+ "steelblue": "4682B4",
191
+ "tan": "D2B48C",
192
+ "teal": "008080",
193
+ "thistle": "D8BFD8",
194
+ "tomato": "FF6347",
195
+ "turquoise": "40E0D0",
196
+ "violet": "EE82EE",
197
+ "wheat": "F5DEB3",
198
+ "white": "FFFFFF",
199
+ "whitesmoke": "F5F5F5",
200
+ "yellow": "FFFF00",
201
+ "yellowgreen": "9ACD32",
202
+ }
203
+
204
+
205
+ def hex2rgb(vstr: str) -> List[int]:
206
+ """
207
+ from "#FFFFFF" to [255,255,255]
208
+ frorm 'DarkBlue' to [0,0,139]
209
+
210
+ Parameters
211
+ ----------
212
+ vstr: str
213
+ the rbg string or an html color
214
+ Returns
215
+ -------
216
+ List[int]
217
+ the list [r,g,b]
218
+ """
219
+ if vstr.startswith("#"):
220
+ value = vstr.lstrip("#")
221
+ else:
222
+ vl = vstr.lower()
223
+ if vl in HTMLCOLORS:
224
+ value = HTMLCOLORS[vl]
225
+ else:
226
+ raise ValueError(f"Unknown color {vstr}")
227
+
228
+ lv = len(value)
229
+ return [int(value[i : i + lv // 3], 16) for i in range(0, lv, lv // 3)]
230
+
231
+
232
+ def hextuple2rgb(vtuple: List[str]) -> List[int]:
233
+ """
234
+ from ("FF","FF","FF") to [255,255,255]
235
+
236
+ Parameters
237
+ ----------
238
+ vtuple : Tuple[str,str,str]
239
+ the Tuple of hexa values
240
+
241
+ Returns
242
+ -------
243
+ List[int,int,int]
244
+ the list [r,g,b]
245
+ """
246
+ return [int(v, 16) for v in vtuple]
247
+
248
+
249
+ def rgb2brightness(r: int, g: int, b: int) -> str:
250
+ """
251
+ Give the fgcol for a background (r,g,b).
252
+
253
+ Parameters
254
+ ----------
255
+ g: int[0,255]
256
+ r: int[0,255]
257
+ b: int[0,255]
258
+
259
+ Returns
260
+ -------
261
+ str
262
+ "white" or "black"
263
+ """
264
+ brightness = r * 0.299 + g * 0.587 + b * 0.114
265
+ return "white" if brightness <= 153 else "black"
266
+
267
+
268
+ def proba2hex(p: float, cmap: matplotlib.colors.Colormap, withSpecialColor: bool) -> Tuple[str, str, str]:
269
+ """
270
+ From a proba p and cmap gives the HTML rgb color
271
+
272
+ Parameters
273
+ ----------
274
+ p: float
275
+ the proba
276
+ cmap: matplotlib.colors.Colormap
277
+ the cmap
278
+ withSpecialColor: bool
279
+ do we have special colors for p=0 or 1 ?
280
+
281
+ Returns
282
+ -------
283
+ Tuple(str,str,str)
284
+ the hex values for r,g,b.
285
+ """
286
+ if withSpecialColor: # add special color for p=0 or p=1
287
+ if p == 0.0:
288
+ return "FF", "33", "33"
289
+ elif p == 1.0:
290
+ return "AA", "FF", "FF"
291
+
292
+ a, b, c, _ = cmap(p)
293
+ return f"{int(a * 256):02x}", f"{int(b * 256):02x}", f"{int(c * 256):02x}"
294
+
295
+
296
+ def proba2color(p: float, cmap: matplotlib.colors.Colormap) -> str:
297
+ """
298
+ From a proba p and cmap gives the HTML rgb color
299
+
300
+ Parameters
301
+ ----------
302
+ p: float
303
+ a value in [0,1]
304
+ cmap: matplotlib.colors.Colormap
305
+
306
+ Returns
307
+ -------
308
+ str
309
+ the html representation of the color
310
+ """
311
+ r, g, b = proba2hex(p, cmap, withSpecialColor=False)
312
+ return "#" + r + g + b
313
+
314
+
315
+ def proba2bgcolor(p: float, cmap: matplotlib.colors.Colormap) -> str:
316
+ """
317
+ From a proba p and cmap gives the HTML rgb color (with special colors for p=0 and p=1)
318
+
319
+ Parameters
320
+ ----------
321
+ p: float
322
+ a value in [0,1]
323
+ cmap: matplotlib.colors.Colormap
324
+
325
+ Returns
326
+ -------
327
+ str
328
+ the html representation of the background color
329
+ """
330
+ r, g, b = proba2hex(p, cmap, withSpecialColor=True)
331
+ return "#" + r + g + b
332
+
333
+
334
+ def proba2fgcolor(p: float, cmap: matplotlib.colors.Colormap) -> str:
335
+ """
336
+ From a proba p and cmap, returns the best choice for text color for the bgcolor(p,cmap).
337
+
338
+ Parameters
339
+ ----------
340
+ p: float
341
+ a value in [0,1]
342
+ cmap: matplotlib.colors.Colormap
343
+
344
+ Returns
345
+ -------
346
+ str
347
+ the html representation of the foreground color
348
+ """
349
+ a, b, c = hextuple2rgb(list(proba2hex(p, cmap, withSpecialColor=True)))
350
+ return rgb2brightness(a, b, c)
351
+
352
+
353
+ def fontFromMatplotlib():
354
+ """
355
+ Find the font name and the font size ysed by matplotlib
356
+
357
+ Returns
358
+ -------
359
+ fontname,size : font name and size from matplotlib
360
+ """
361
+ family = mpl.rcParams["font.family"][0]
362
+ if family == "sans-serif":
363
+ family = mpl.rcParams["font.sans-serif"][0]
364
+ return family, mpl.rcParams["font.size"]
365
+
366
+
367
+ def prepareDot(dotgraph, **kwargs):
368
+ if "size" in kwargs and kwargs["size"] is not None:
369
+ dotgraph.set_size(kwargs["size"])
370
+
371
+ # workaround for some badly parsed graph (pyparsing>=3.03)
372
+ dotgraph.del_node('"\\n"')
373
+ dotgraph.del_node('"\\n\\n"')
374
+
375
+ if dotgraph.get_rankdir() is None:
376
+ dotgraph.set_rankdir(gum.config["notebook", "graph_rankdir"])
377
+ if dotgraph.get_layout() is None:
378
+ dotgraph.set_layout(gum.config["notebook", "graph_layout"])
379
+
380
+ dotgraph.set_bgcolor("transparent")
381
+ for e in dotgraph.get_edges():
382
+ if e.get_color() is None:
383
+ e.set_color(getBlackInTheme())
384
+ for n in dotgraph.get_nodes():
385
+ if n.get_color() is None:
386
+ n.set_color(getBlackInTheme())
387
+ if n.get_fontcolor() is None:
388
+ n.set_fontcolor(getBlackInTheme())
389
+
390
+ return dotgraph
@@ -0,0 +1,299 @@
1
+ ############################################################################
2
+ # This file is part of the aGrUM/pyAgrum library. #
3
+ # #
4
+ # Copyright (c) 2005-2025 by #
5
+ # - Pierre-Henri WUILLEMIN(_at_LIP6) #
6
+ # - Christophe GONZALES(_at_AMU) #
7
+ # #
8
+ # The aGrUM/pyAgrum library is free software; you can redistribute it #
9
+ # and/or modify it under the terms of either : #
10
+ # #
11
+ # - the GNU Lesser General Public License as published by #
12
+ # the Free Software Foundation, either version 3 of the License, #
13
+ # or (at your option) any later version, #
14
+ # - the MIT license (MIT), #
15
+ # - or both in dual license, as here. #
16
+ # #
17
+ # (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) #
18
+ # #
19
+ # This aGrUM/pyAgrum library is distributed in the hope that it will be #
20
+ # useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, #
21
+ # INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS #
22
+ # FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
23
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
24
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, #
25
+ # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR #
26
+ # OTHER DEALINGS IN THE SOFTWARE. #
27
+ # #
28
+ # See LICENCES for more details. #
29
+ # #
30
+ # SPDX-FileCopyrightText: Copyright 2005-2025 #
31
+ # - Pierre-Henri WUILLEMIN(_at_LIP6) #
32
+ # - Christophe GONZALES(_at_AMU) #
33
+ # SPDX-License-Identifier: LGPL-3.0-or-later OR MIT #
34
+ # #
35
+ # Contact : info_at_agrum_dot_org #
36
+ # homepage : http://agrum.gitlab.io #
37
+ # gitlab : https://gitlab.com/agrumery/agrum #
38
+ # #
39
+ ############################################################################
40
+
41
+ """
42
+ The purpose of this module is to provide tools for mapping Bayesian Network (and inference) in dot language in order to
43
+ be displayed/saved as image.
44
+ """
45
+
46
+ import time
47
+ import hashlib
48
+
49
+ from tempfile import mkdtemp
50
+
51
+ import matplotlib.pyplot as plt
52
+ import pydot as dot
53
+
54
+ import pyagrum as gum
55
+
56
+ from pyagrum.lib import proba_histogram
57
+ import pyagrum.lib._colors as gumcols
58
+ from pyagrum.lib.utils import getBlackInTheme
59
+
60
+
61
+ def BN2dot(
62
+ bn, size=None, nodeColor=None, arcWidth=None, arcLabel=None, arcColor=None, cmapNode=None, cmapArc=None, showMsg=None
63
+ ):
64
+ """
65
+ create a pydot representation of the BN
66
+
67
+ Parameters
68
+ ----------
69
+ bn : pyagrum.BayesNet
70
+ the Bayesian network
71
+ size: str
72
+ size of the rendered graph
73
+ nodeColor: dict[Tuple(int,int),float]
74
+ a nodeMap of values to be shown as color nodes (with special color for 0 and 1)
75
+ arcWidth: dict[Tuple(int,int),float]
76
+ an arcMap of values to be shown as bold arcs
77
+ arcLabel: dict[Tuple(int,int),str]
78
+ an arcMap of labels to be shown next to arcs
79
+ arcColor: dict[Tuple(int,int),float]
80
+ an arcMap of values (between 0 and 1) to be shown as color of arcs
81
+ cmapNode: ColorMap
82
+ color map to show the vals of Nodes
83
+ cmapArc: ColorMap
84
+ color map to show the vals of Arcs
85
+ showMsg: dict
86
+ a nodeMap of values to be shown as tooltip
87
+
88
+ Returns
89
+ -------
90
+ pydot.Dot
91
+ the desired representation of the Bayesian network
92
+ """
93
+ if cmapNode is None:
94
+ cmapNode = plt.get_cmap(gum.config["notebook", "default_node_cmap"])
95
+
96
+ if cmapArc is None:
97
+ cmapArc = plt.get_cmap(gum.config["notebook", "default_arc_cmap"])
98
+
99
+ # default
100
+ maxarcs = 100
101
+ minarcs = 0
102
+
103
+ if arcWidth is not None:
104
+ minarcs = min(arcWidth.values())
105
+ maxarcs = max(arcWidth.values())
106
+
107
+ dotobj = dot.Dot(graph_type="digraph", bgcolor="transparent")
108
+
109
+ for n in bn.names():
110
+ if nodeColor is None or n not in nodeColor:
111
+ bgcol = gum.config["notebook", "default_node_bgcolor"]
112
+ fgcol = gum.config["notebook", "default_node_fgcolor"]
113
+ res = ""
114
+ else:
115
+ bgcol = gumcols.proba2bgcolor(nodeColor[n], cmapNode)
116
+ fgcol = gumcols.proba2fgcolor(nodeColor[n], cmapNode)
117
+ res = f" : {nodeColor[n] if showMsg is None else showMsg[n]:2.5f}"
118
+
119
+ node = dot.Node(
120
+ f'"{n}"', style="filled", fillcolor=bgcol, fontcolor=fgcol, tooltip=f'"({bn.idFromName(n)}) {n}{res}"'
121
+ )
122
+ dotobj.add_node(node)
123
+
124
+ for a in bn.arcs():
125
+ (n, j) = a
126
+ pw = 1
127
+ av = f"{n}&nbsp;&rarr;&nbsp;{j}"
128
+ col = getBlackInTheme()
129
+ lb = ""
130
+
131
+ if arcWidth is not None and a in arcWidth:
132
+ if maxarcs != minarcs:
133
+ pw = 0.1 + 5 * (arcWidth[a] - minarcs) / (maxarcs - minarcs)
134
+ av = f"{n}&nbsp;&rarr;&nbsp;{j} : {arcWidth[a]}"
135
+
136
+ if arcColor is not None and a in arcColor:
137
+ col = gumcols.proba2color(arcColor[a], cmapArc)
138
+
139
+ if arcLabel is not None and a in arcLabel:
140
+ lb = arcLabel[a]
141
+
142
+ edge = dot.Edge(
143
+ '"' + bn.variable(a[0]).name() + '"',
144
+ '"' + bn.variable(a[1]).name() + '"',
145
+ label=lb,
146
+ fontsize="10",
147
+ penwidth=pw,
148
+ color=col,
149
+ tooltip=av,
150
+ )
151
+ dotobj.add_edge(edge)
152
+
153
+ if size is None:
154
+ size = gum.config["notebook", "default_graph_size"]
155
+
156
+ # dynamic member makes pylink unhappy
157
+ # pylint: disable=no-member
158
+ dotobj.set_size(size)
159
+
160
+ return dotobj
161
+
162
+
163
+ def BNinference2dot(
164
+ bn,
165
+ size=None,
166
+ engine=None,
167
+ evs=None,
168
+ targets=None,
169
+ nodeColor=None,
170
+ arcWidth=None,
171
+ arcColor=None,
172
+ cmapNode=None,
173
+ cmapArc=None,
174
+ dag=None,
175
+ ):
176
+ """
177
+ create a pydot representation of an inference in a BN
178
+
179
+ Parameters
180
+ ----------
181
+ bn : pyagrum.BayesNet
182
+ the Bayesian network
183
+ size: str
184
+ size of the rendered graph
185
+ engine: pyagrum.Inference
186
+ inference algorithm used. If None, LazyPropagation will be used
187
+ evs: dict
188
+ map of evidence
189
+ targets: set
190
+ set of targets. If targets={} then each node is a target
191
+ nodeColor: dict
192
+ a nodeMap of values to be shown as color nodes (with special color for 0 and 1)
193
+ arcWidth: dict
194
+ a arcMap of values to be shown as bold arcs
195
+ arcColor: dict
196
+ a arcMap of values (between 0 and 1) to be shown as color of arcs
197
+ cmapNode: ColorMap
198
+ color map to show the vals of Nodes
199
+ cmapArc: ColorMap
200
+ color map to show the vals of Arcs
201
+ dag : pyagrum.DAG
202
+ only shows nodes that have their id in the dag (and not in the whole BN)
203
+
204
+ Returns
205
+ -------
206
+ the desired representation of the inference
207
+ """
208
+ if evs is None:
209
+ evs = {}
210
+ if targets is None:
211
+ targets = {}
212
+ if cmapNode is None:
213
+ cmapNode = plt.get_cmap(gum.config["notebook", "default_node_cmap"])
214
+
215
+ if cmapArc is None:
216
+ cmapArc = plt.get_cmap(gum.config["notebook", "default_arc_cmap"])
217
+
218
+ # defaukt
219
+ maxarcs = 100
220
+ minarcs = 0
221
+
222
+ if arcWidth is not None:
223
+ minarcs = min(arcWidth.values())
224
+ maxarcs = max(arcWidth.values())
225
+
226
+ startTime = time.time()
227
+ if engine is None:
228
+ ie = gum.LazyPropagation(bn)
229
+ else:
230
+ ie = engine
231
+ ie.setEvidence(evs)
232
+ ie.makeInference()
233
+ stopTime = time.time()
234
+
235
+ temp_dir = mkdtemp("", "tmp", None) # with TemporaryDirectory() as temp_dir:
236
+
237
+ dotstr = 'digraph structs {\n fontcolor="' + getBlackInTheme() + '";bgcolor="transparent";'
238
+
239
+ if gum.config.asBool["notebook", "show_inference_time"]:
240
+ dotstr += f' label="Inference in {1000 * (stopTime - startTime):6.2f}ms";\n'
241
+
242
+ fontname, fontsize = gumcols.fontFromMatplotlib()
243
+ dotstr += f' node [fillcolor="{gum.config["notebook", "default_node_bgcolor"]}", style=filled,color="{gum.config["notebook", "default_node_fgcolor"]}",fontname="{fontname}",fontsize="{fontsize}"];\n'
244
+ dotstr += f' edge [color="{getBlackInTheme()}"];\n'
245
+
246
+ showdag = bn.dag() if dag is None else dag
247
+
248
+ for nid in showdag.nodes():
249
+ name = bn.variable(nid).name()
250
+
251
+ # defaults
252
+ bgcol = gum.config["notebook", "default_node_bgcolor"]
253
+ fgcol = gum.config["notebook", "default_node_fgcolor"]
254
+ if len(targets) == 0 or name in targets or nid in targets:
255
+ bgcol = gum.config["notebook", "figure_facecolor"]
256
+
257
+ if nodeColor is not None and (name in nodeColor or nid in nodeColor):
258
+ bgcol = gumcols.proba2bgcolor(nodeColor[name], cmapNode)
259
+ fgcol = gumcols.proba2fgcolor(nodeColor[name], cmapNode)
260
+
261
+ # 'hard' colour for evidence (?)
262
+ if nid in ie.hardEvidenceNodes() | ie.softEvidenceNodes():
263
+ bgcol = gum.config["notebook", "evidence_bgcolor"]
264
+ fgcol = gum.config["notebook", "evidence_fgcolor"]
265
+
266
+ colorattribute = f'fillcolor="{bgcol}", fontcolor="{fgcol}", color="#000000"'
267
+ if len(targets) == 0 or name in targets or nid in targets:
268
+ filename = temp_dir + hashlib.md5(name.encode()).hexdigest() + "." + gum.config["notebook", "graph_format"]
269
+ proba_histogram.saveFigProba(ie.posterior(name), filename, bgcolor=bgcol)
270
+ dotstr += f' "{name}" [shape=rectangle,image="{filename}",label="", {colorattribute}];\n'
271
+ else:
272
+ dotstr += f' "{name}" [{colorattribute}]'
273
+
274
+ for a in showdag.arcs():
275
+ (n, j) = a
276
+ pw = 1
277
+ av = f"{n}&nbsp;&rarr;&nbsp;{j}"
278
+ col = getBlackInTheme()
279
+
280
+ if arcWidth is not None and a in arcWidth:
281
+ if maxarcs != minarcs:
282
+ pw = 0.1 + 5 * (arcWidth[a] - minarcs) / (maxarcs - minarcs)
283
+ av = f"{n}&nbsp;&rarr;&nbsp;{j} : {arcWidth[a]}"
284
+
285
+ if arcColor is not None and a in arcColor:
286
+ col = gumcols.proba2color(arcColor[a], cmapArc)
287
+
288
+ dotstr += f' "{bn.variable(n).name()}"->"{bn.variable(j).name()}" [penwidth="{pw}",tooltip="{av}",color="{col}"];'
289
+
290
+ dotstr += "}"
291
+
292
+ g = dot.graph_from_dot_data(dotstr)[0]
293
+
294
+ if size is None:
295
+ size = gum.config["notebook", "default_graph_inference_size"]
296
+ g.set_size(size)
297
+ g.temp_dir = temp_dir
298
+
299
+ return g