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
@@ -0,0 +1,305 @@
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 credal 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
+ import pyagrum.lib._colors as gumcols
56
+ from pyagrum.lib.proba_histogram import saveFigProbaMinMax
57
+
58
+
59
+ def CN2dot(
60
+ cn, size=None, nodeColor=None, arcWidth=None, arcLabel=None, arcColor=None, cmapNode=None, cmapArc=None, showMsg=None
61
+ ):
62
+ """
63
+ create a pydot representation of the Credal Network
64
+
65
+ Parameters
66
+ ----------
67
+ cn : pyagrum.CredalNet
68
+ the Credal network
69
+ size: str
70
+ size of the rendered graph
71
+ nodeColor: dict[int,float]
72
+ a nodeMap of values to be shown as color nodes (with special color for 0 and 1)
73
+ arcWidth: dict[Tuple(int,int),float]
74
+ an arcMap of values to be shown as bold arcs
75
+ arcLabel: dict[Tuple(int,int),float]
76
+ an arcMap of labels to be shown next to arcs
77
+ arcColor: dict[Tuple(int,int),float]
78
+ an arcMap of values (between 0 and 1) to be shown as color of arcs
79
+ cmapNode: matplotlib.color.colormap
80
+ color map to show the vals of Nodes
81
+ cmapArc: matplotlib.color.colormap
82
+ color map to show the vals of Arcs
83
+ showMsg : dict[int,str]
84
+ a nodeMap of values to be shown as tooltip
85
+
86
+ Returns
87
+ -------
88
+ pydot.Dot
89
+ the desired representation of the Credal Network
90
+ """
91
+ bn = cn.current_bn()
92
+ if cmapNode is None:
93
+ cmapNode = plt.get_cmap(gum.config["notebook", "default_node_cmap"])
94
+
95
+ if cmapArc is None:
96
+ cmapArc = plt.get_cmap(gum.config["notebook", "default_arc_cmap"])
97
+
98
+ # default
99
+ minarcs = 0
100
+ maxarcs = 100
101
+
102
+ if arcWidth is not None:
103
+ minarcs = min(arcWidth.values())
104
+ maxarcs = max(arcWidth.values())
105
+
106
+ graph = dot.Dot(graph_type="digraph", bgcolor="transparent")
107
+
108
+ for n in bn.names():
109
+ if nodeColor is None or n not in nodeColor:
110
+ bgcol = gum.config["credalnet", "default_node_bgcolor"]
111
+ fgcol = gum.config["credalnet", "default_node_fgcolor"]
112
+ res = ""
113
+ else:
114
+ bgcol = gumcols.proba2bgcolor(nodeColor[n], cmapNode)
115
+ fgcol = gumcols.proba2fgcolor(nodeColor[n], cmapNode)
116
+ res = f" : {nodeColor[n] if showMsg is None else showMsg[n]:2.5f}"
117
+
118
+ node = dot.Node(
119
+ '"' + n + '"',
120
+ style="filled",
121
+ shape="polygon",
122
+ sides="7",
123
+ peripheries="1",
124
+ fillcolor=bgcol,
125
+ fontcolor=fgcol,
126
+ tooltip=f'"({bn.idFromName(n)}) {n}{res}"',
127
+ )
128
+ graph.add_node(node)
129
+
130
+ for a in bn.arcs():
131
+ (n, j) = a
132
+ pw = 1
133
+ av = f"{n} → {j}"
134
+ col = gumcols.getBlackInTheme()
135
+ lb = ""
136
+
137
+ if arcWidth is not None and a in arcWidth:
138
+ if maxarcs != minarcs:
139
+ pw = 0.1 + 5 * (arcWidth[a] - minarcs) / (maxarcs - minarcs)
140
+ av = f"{n} → {j} : {arcWidth[a]}"
141
+
142
+ if arcColor is not None and a in arcColor:
143
+ col = gumcols.proba2color(arcColor[a], cmapArc)
144
+
145
+ if arcLabel is not None and a in arcLabel:
146
+ lb = arcLabel[a]
147
+
148
+ edge = dot.Edge(
149
+ '"' + bn.variable(a[0]).name() + '"',
150
+ '"' + bn.variable(a[1]).name() + '"',
151
+ label=lb,
152
+ fontsize="10",
153
+ penwidth=pw,
154
+ color=col,
155
+ tooltip=av,
156
+ )
157
+ graph.add_edge(edge)
158
+
159
+ if size is None:
160
+ size = gum.config["notebook", "default_graph_size"]
161
+
162
+ # dynamic member makes pylink unhappy
163
+ # pylint: disable=no-member
164
+ graph.set_size(size)
165
+ return graph
166
+
167
+
168
+ def CNinference2dot(
169
+ cn,
170
+ size=None,
171
+ engine=None,
172
+ evs=None,
173
+ targets=None,
174
+ nodeColor=None,
175
+ arcWidth=None,
176
+ arcColor=None,
177
+ cmapNode=None,
178
+ cmapArc=None,
179
+ dag=None,
180
+ ):
181
+ """
182
+ create a pydot representation of an inference in a BN
183
+
184
+ Parameters
185
+ ----------
186
+ cn : pyagrum.CredalNet
187
+ the credal network
188
+ size: str
189
+ size of the rendered graph
190
+ engine: pyagrum.Inference
191
+ inference algorithm used. If None, LazyPropagation will be used
192
+ evs: dict
193
+ map of evidence
194
+ targets: set
195
+ set of targets. If targets={} then each node is a target
196
+ nodeColor: dict
197
+ a nodeMap of values to be shown as color nodes (with special color for 0 and 1)
198
+ arcWidth: dict
199
+ a arcMap of values to be shown as bold arcs
200
+ arcColor: dict
201
+ a arcMap of values (between 0 and 1) to be shown as color of arcs
202
+ cmapNode: ColorMap
203
+ color map to show the vals of Nodes
204
+ cmapArc: ColorMap
205
+ color map to show the vals of Arcs
206
+ dag : pyagrum.DAG
207
+ only shows nodes that have their id in the dag (and not in the whole BN)
208
+
209
+ Returns
210
+ -------
211
+ the desired representation of the inference
212
+ """
213
+ if evs is None:
214
+ evs = {}
215
+ if targets is None:
216
+ targets = {}
217
+ bn = cn.current_bn()
218
+ if cmapNode is None:
219
+ cmapNode = plt.get_cmap(gum.config["notebook", "default_node_cmap"])
220
+
221
+ if cmapArc is None:
222
+ cmapArc = plt.get_cmap(gum.config["notebook", "default_arc_cmap"])
223
+
224
+ # by default
225
+ minarcs = 0
226
+ maxarcs = 100
227
+
228
+ if arcWidth is not None:
229
+ minarcs = min(arcWidth.values())
230
+ maxarcs = max(arcWidth.values())
231
+
232
+ startTime = time.time()
233
+ if engine is None:
234
+ ie = gum.CNMonteCarloSampling(cn)
235
+ else:
236
+ ie = engine
237
+ if len(evs) > 0:
238
+ ie.setEvidence(evs)
239
+ ie.makeInference()
240
+ stopTime = time.time()
241
+
242
+ temp_dir = mkdtemp("", "tmp", None) # with TemporaryDirectory() as temp_dir:
243
+
244
+ dotstr = 'digraph structs {\n fontcolor="' + gumcols.getBlackInTheme() + '";bgcolor="transparent";'
245
+
246
+ if gum.config.asBool["notebook", "show_inference_time"]:
247
+ dotstr += f' label="Inference in {1000 * (stopTime - startTime):6.2f}ms";\n'
248
+
249
+ fontname, fontsize = gumcols.fontFromMatplotlib()
250
+ dotstr += f' node [fillcolor="{gum.config["notebook", "default_node_bgcolor"]}", style=filled,color="{gum.config["notebook", "default_node_fgcolor"]}",fontname="{fontname}",fontsize="{fontsize}"];\n'
251
+ dotstr += f' edge [color="{gumcols.getBlackInTheme()}"];\n'
252
+
253
+ showdag = bn.dag() if dag is None else dag
254
+ for nid in showdag.nodes():
255
+ name = bn.variable(nid).name()
256
+
257
+ # defaults
258
+ bgcol = gum.config["notebook", "default_node_bgcolor"]
259
+ fgcol = gum.config["notebook", "default_node_fgcolor"]
260
+ if len(targets) == 0 or name in targets or nid in targets:
261
+ bgcol = gum.config["notebook", "figure_facecolor"]
262
+
263
+ if nodeColor is not None and (name in nodeColor or nid in nodeColor):
264
+ bgcol = gumcols.proba2bgcolor(nodeColor[name], cmapNode)
265
+ fgcol = gumcols.proba2fgcolor(nodeColor[name], cmapNode)
266
+
267
+ # 'hard' colour for evidence (?)
268
+ # if nid in ie.hardEvidenceNodes()|ie.softEvidenceNodes():
269
+ # bgcol = gum.config["notebook", "evidence_bgcolor"]
270
+ # fgcol = gum.config["notebook", "evidence_fgcolor"]
271
+
272
+ colorattribute = f'fillcolor="{bgcol}", fontcolor="{fgcol}", color="#000000"'
273
+ if len(targets) == 0 or name in targets or nid in targets:
274
+ filename = temp_dir + hashlib.md5(name.encode()).hexdigest() + "." + gum.config["notebook", "graph_format"]
275
+ saveFigProbaMinMax(ie.marginalMin(name), ie.marginalMax(name), filename, bgcolor=bgcol)
276
+ dotstr += f' "{name}" [shape=rectangle,image="{filename}",label="", {colorattribute}];\n'
277
+ else:
278
+ dotstr += f' "{name}" [shape=polygon,sides=7,peripheries=1,{colorattribute}]'
279
+
280
+ for a in showdag.arcs():
281
+ (n, j) = a
282
+ pw = 1
283
+ av = f"{n} → {j}"
284
+ col = gumcols.getBlackInTheme()
285
+
286
+ if arcWidth is not None and a in arcWidth:
287
+ if maxarcs != minarcs:
288
+ pw = 0.1 + 5 * (arcWidth[n, j] - minarcs) / (maxarcs - minarcs)
289
+ av = f"{n} → {j} : {arcWidth[a]}"
290
+
291
+ if arcColor is not None and a in arcColor:
292
+ col = gumcols.proba2color(arcColor[n, j], cmapArc)
293
+
294
+ dotstr += f' "{bn.variable(n).name()}"->"{bn.variable(j).name()}" [penwidth="{pw}",tooltip="{av}",color="{col}"];'
295
+
296
+ dotstr += "}"
297
+
298
+ g = dot.graph_from_dot_data(dotstr)[0]
299
+
300
+ if size is None:
301
+ size = gum.config["notebook", "default_graph_inference_size"]
302
+ g.set_size(size)
303
+ g.temp_dir = temp_dir
304
+
305
+ return g