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.
- pyagrum/__init__.py +165 -0
- pyagrum/_pyagrum.so +0 -0
- pyagrum/bnmixture/BNMInference.py +268 -0
- pyagrum/bnmixture/BNMLearning.py +376 -0
- pyagrum/bnmixture/BNMixture.py +464 -0
- pyagrum/bnmixture/__init__.py +60 -0
- pyagrum/bnmixture/notebook.py +1058 -0
- pyagrum/causal/_CausalFormula.py +280 -0
- pyagrum/causal/_CausalModel.py +436 -0
- pyagrum/causal/__init__.py +81 -0
- pyagrum/causal/_causalImpact.py +356 -0
- pyagrum/causal/_dSeparation.py +598 -0
- pyagrum/causal/_doAST.py +761 -0
- pyagrum/causal/_doCalculus.py +361 -0
- pyagrum/causal/_doorCriteria.py +374 -0
- pyagrum/causal/_exceptions.py +95 -0
- pyagrum/causal/_types.py +61 -0
- pyagrum/causal/causalEffectEstimation/_CausalEffectEstimation.py +1175 -0
- pyagrum/causal/causalEffectEstimation/_IVEstimators.py +718 -0
- pyagrum/causal/causalEffectEstimation/_RCTEstimators.py +132 -0
- pyagrum/causal/causalEffectEstimation/__init__.py +46 -0
- pyagrum/causal/causalEffectEstimation/_backdoorEstimators.py +774 -0
- pyagrum/causal/causalEffectEstimation/_causalBNEstimator.py +324 -0
- pyagrum/causal/causalEffectEstimation/_frontdoorEstimators.py +396 -0
- pyagrum/causal/causalEffectEstimation/_learners.py +118 -0
- pyagrum/causal/causalEffectEstimation/_utils.py +466 -0
- pyagrum/causal/notebook.py +172 -0
- pyagrum/clg/CLG.py +658 -0
- pyagrum/clg/GaussianVariable.py +111 -0
- pyagrum/clg/SEM.py +312 -0
- pyagrum/clg/__init__.py +63 -0
- pyagrum/clg/canonicalForm.py +408 -0
- pyagrum/clg/constants.py +54 -0
- pyagrum/clg/forwardSampling.py +202 -0
- pyagrum/clg/learning.py +776 -0
- pyagrum/clg/notebook.py +480 -0
- pyagrum/clg/variableElimination.py +271 -0
- pyagrum/common.py +60 -0
- pyagrum/config.py +319 -0
- pyagrum/ctbn/CIM.py +513 -0
- pyagrum/ctbn/CTBN.py +573 -0
- pyagrum/ctbn/CTBNGenerator.py +216 -0
- pyagrum/ctbn/CTBNInference.py +459 -0
- pyagrum/ctbn/CTBNLearner.py +161 -0
- pyagrum/ctbn/SamplesStats.py +671 -0
- pyagrum/ctbn/StatsIndepTest.py +355 -0
- pyagrum/ctbn/__init__.py +79 -0
- pyagrum/ctbn/constants.py +54 -0
- pyagrum/ctbn/notebook.py +264 -0
- pyagrum/defaults.ini +199 -0
- pyagrum/deprecated.py +95 -0
- pyagrum/explain/_ComputationCausal.py +75 -0
- pyagrum/explain/_ComputationConditional.py +48 -0
- pyagrum/explain/_ComputationMarginal.py +48 -0
- pyagrum/explain/_CustomShapleyCache.py +110 -0
- pyagrum/explain/_Explainer.py +176 -0
- pyagrum/explain/_Explanation.py +70 -0
- pyagrum/explain/_FIFOCache.py +54 -0
- pyagrum/explain/_ShallCausalValues.py +204 -0
- pyagrum/explain/_ShallConditionalValues.py +155 -0
- pyagrum/explain/_ShallMarginalValues.py +155 -0
- pyagrum/explain/_ShallValues.py +296 -0
- pyagrum/explain/_ShapCausalValues.py +208 -0
- pyagrum/explain/_ShapConditionalValues.py +126 -0
- pyagrum/explain/_ShapMarginalValues.py +191 -0
- pyagrum/explain/_ShapleyValues.py +298 -0
- pyagrum/explain/__init__.py +81 -0
- pyagrum/explain/_explGeneralizedMarkovBlanket.py +152 -0
- pyagrum/explain/_explIndependenceListForPairs.py +146 -0
- pyagrum/explain/_explInformationGraph.py +264 -0
- pyagrum/explain/notebook/__init__.py +54 -0
- pyagrum/explain/notebook/_bar.py +142 -0
- pyagrum/explain/notebook/_beeswarm.py +174 -0
- pyagrum/explain/notebook/_showShapValues.py +97 -0
- pyagrum/explain/notebook/_waterfall.py +220 -0
- pyagrum/explain/shapley.py +225 -0
- pyagrum/lib/__init__.py +46 -0
- pyagrum/lib/_colors.py +390 -0
- pyagrum/lib/bn2graph.py +299 -0
- pyagrum/lib/bn2roc.py +1026 -0
- pyagrum/lib/bn2scores.py +217 -0
- pyagrum/lib/bn_vs_bn.py +605 -0
- pyagrum/lib/cn2graph.py +305 -0
- pyagrum/lib/discreteTypeProcessor.py +1102 -0
- pyagrum/lib/discretizer.py +58 -0
- pyagrum/lib/dynamicBN.py +390 -0
- pyagrum/lib/explain.py +57 -0
- pyagrum/lib/export.py +84 -0
- pyagrum/lib/id2graph.py +258 -0
- pyagrum/lib/image.py +387 -0
- pyagrum/lib/ipython.py +307 -0
- pyagrum/lib/mrf2graph.py +471 -0
- pyagrum/lib/notebook.py +1821 -0
- pyagrum/lib/proba_histogram.py +552 -0
- pyagrum/lib/utils.py +138 -0
- pyagrum/pyagrum.py +31495 -0
- pyagrum/skbn/_MBCalcul.py +242 -0
- pyagrum/skbn/__init__.py +49 -0
- pyagrum/skbn/_learningMethods.py +282 -0
- pyagrum/skbn/_utils.py +297 -0
- pyagrum/skbn/bnclassifier.py +1014 -0
- pyagrum_nightly-2.3.1.9.dev202512261765915415.dist-info/LICENSE.md +12 -0
- pyagrum_nightly-2.3.1.9.dev202512261765915415.dist-info/LICENSES/LGPL-3.0-or-later.txt +304 -0
- pyagrum_nightly-2.3.1.9.dev202512261765915415.dist-info/LICENSES/MIT.txt +18 -0
- pyagrum_nightly-2.3.1.9.dev202512261765915415.dist-info/METADATA +145 -0
- pyagrum_nightly-2.3.1.9.dev202512261765915415.dist-info/RECORD +107 -0
- pyagrum_nightly-2.3.1.9.dev202512261765915415.dist-info/WHEEL +4 -0
pyagrum/lib/ipython.py
ADDED
|
@@ -0,0 +1,307 @@
|
|
|
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
|
+
tools for BN analysis in ipython (and spyder)
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
import IPython.display
|
|
46
|
+
import matplotlib as mpl
|
|
47
|
+
import matplotlib.pyplot as plt
|
|
48
|
+
import numpy as np
|
|
49
|
+
import pydot as dot
|
|
50
|
+
|
|
51
|
+
from IPython.display import Image, display
|
|
52
|
+
|
|
53
|
+
import pyagrum as gum
|
|
54
|
+
from pyagrum.lib.bn2graph import BN2dot
|
|
55
|
+
from pyagrum.lib.id2graph import ID2dot
|
|
56
|
+
from pyagrum.lib.mrf2graph import MRF2UGdot
|
|
57
|
+
from pyagrum.lib.mrf2graph import MRF2FactorGraphdot
|
|
58
|
+
from pyagrum.lib.bn_vs_bn import GraphicalBNComparator
|
|
59
|
+
from pyagrum.lib.proba_histogram import proba2histo
|
|
60
|
+
|
|
61
|
+
# check if an instance of ipython exists
|
|
62
|
+
try:
|
|
63
|
+
get_ipython
|
|
64
|
+
except NameError:
|
|
65
|
+
raise ImportError(
|
|
66
|
+
"[pyAgrum ERROR] pyagrum.lib.ipython has to be imported from an IPython's instance (mainly ipython's console)."
|
|
67
|
+
) from None
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def configuration():
|
|
71
|
+
"""
|
|
72
|
+
Display the collection of dependance and versions
|
|
73
|
+
"""
|
|
74
|
+
from collections import OrderedDict
|
|
75
|
+
import sys
|
|
76
|
+
import os
|
|
77
|
+
|
|
78
|
+
packages = OrderedDict()
|
|
79
|
+
packages["OS"] = "%s [%s]" % (os.name, sys.platform)
|
|
80
|
+
packages["Python"] = sys.version
|
|
81
|
+
packages["IPython"] = IPython.__version__
|
|
82
|
+
packages["MatPlotLib"] = mpl.__version__
|
|
83
|
+
packages["Numpy"] = np.__version__
|
|
84
|
+
packages["pyAgrum"] = gum.__version__
|
|
85
|
+
|
|
86
|
+
for name in packages:
|
|
87
|
+
print("%s : %s" % (name, packages[name]))
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def showGraph(gr, size=None):
|
|
91
|
+
"""
|
|
92
|
+
show a pydot graph in a notebook
|
|
93
|
+
|
|
94
|
+
Parameters
|
|
95
|
+
----------
|
|
96
|
+
gr: pydot.Dot
|
|
97
|
+
the graph to show
|
|
98
|
+
size: int|str
|
|
99
|
+
the size of the visualisation
|
|
100
|
+
"""
|
|
101
|
+
if size is None:
|
|
102
|
+
size = gum.config["notebook", "default_graph_size"]
|
|
103
|
+
|
|
104
|
+
gr.set_size(size)
|
|
105
|
+
display(Image(gr.create_png()))
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _from_dotstring(dotstring):
|
|
109
|
+
g = dot.graph_from_dot_data(dotstring)[0]
|
|
110
|
+
return g
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def showDot(dotstring, size=None):
|
|
114
|
+
"""
|
|
115
|
+
show a dot string as a graph
|
|
116
|
+
|
|
117
|
+
Parameters
|
|
118
|
+
----------
|
|
119
|
+
dotstring: str
|
|
120
|
+
the dot string
|
|
121
|
+
size: float | str
|
|
122
|
+
the size of the graphe
|
|
123
|
+
"""
|
|
124
|
+
showGraph(_from_dotstring(dotstring), size)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def showBNDiff(bn1, bn2, size=None):
|
|
128
|
+
"""show a graphical diff between the arcs of _bn1 (reference) with those of _bn2.
|
|
129
|
+
|
|
130
|
+
* full black line: the arc is common for both
|
|
131
|
+
* full red line: the arc is common but inverted in _bn2
|
|
132
|
+
* dotted black line: the arc is added in _bn2
|
|
133
|
+
* dotted red line: the arc is removed in _bn2
|
|
134
|
+
|
|
135
|
+
:param BayesNet bn1: referent model for the comparison
|
|
136
|
+
:param BayesNet bn2: bn compared to the referent model
|
|
137
|
+
:param size: size of the rendered graph
|
|
138
|
+
"""
|
|
139
|
+
if size is None:
|
|
140
|
+
size = gum.config["notebook", "default_graph_size"]
|
|
141
|
+
cmp = GraphicalBNComparator(bn1, bn2)
|
|
142
|
+
showGraph(cmp.dotDiff(), size)
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def showJunctionTree(bn, withNames=True, size=None):
|
|
146
|
+
"""
|
|
147
|
+
Show a junction tree
|
|
148
|
+
|
|
149
|
+
:param bn: the Bayesian network
|
|
150
|
+
:param boolean withNames: display the variable names or the node id in the clique
|
|
151
|
+
:param size: size of the rendered graph
|
|
152
|
+
"""
|
|
153
|
+
jtg = gum.JunctionTreeGenerator()
|
|
154
|
+
jt = jtg.junctionTree(bn)
|
|
155
|
+
if withNames:
|
|
156
|
+
return showDot(jt.toDotWithNames(bn), size)
|
|
157
|
+
else:
|
|
158
|
+
return showDot(jt.toDot(), size)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def showBN(bn, size=None, nodeColor=None, arcWidth=None, arcColor=None, cmap=None, cmapArc=None):
|
|
162
|
+
"""
|
|
163
|
+
show a Bayesian network
|
|
164
|
+
|
|
165
|
+
:param bn: the Bayesian network
|
|
166
|
+
:param size: size of the rendered graph
|
|
167
|
+
:param format: render as "png" or "svg"
|
|
168
|
+
:param vals: a nodeMap of values to be shown as color nodes
|
|
169
|
+
:param arcvals: a arcMap of values to be shown as bold arcs
|
|
170
|
+
:param cmap: color map to show the vals
|
|
171
|
+
"""
|
|
172
|
+
if size is None:
|
|
173
|
+
size = gum.config["notebook", "default_graph_size"]
|
|
174
|
+
|
|
175
|
+
if cmapArc is None:
|
|
176
|
+
cmapArc = cmap
|
|
177
|
+
|
|
178
|
+
return showGraph(BN2dot(bn, size, nodeColor, arcWidth, arcColor, cmap, cmapArc), size)
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def showProba(p, scale=1.0):
|
|
182
|
+
"""
|
|
183
|
+
Show a mono-dim Tensor
|
|
184
|
+
|
|
185
|
+
:param p: the mono-dim Tensor
|
|
186
|
+
:return:
|
|
187
|
+
"""
|
|
188
|
+
_ = proba2histo(p, scale)
|
|
189
|
+
# fig.patch.set_facecolor(gum.config["notebook", "figure_facecolor"])
|
|
190
|
+
IPython.display.set_matplotlib_formats(gum.config["notebook", "graph_format"])
|
|
191
|
+
plt.show()
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def showPosterior(bn, evs, target):
|
|
195
|
+
"""
|
|
196
|
+
shortcut for showProba(gum.getPosterior(bn,evs,target))
|
|
197
|
+
|
|
198
|
+
:param bn: the BayesNet
|
|
199
|
+
:param evs: map of evidence
|
|
200
|
+
:param target: name of target variable
|
|
201
|
+
"""
|
|
202
|
+
showProba(gum.getPosterior(bn, evs=evs, target=target))
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def showMRF(
|
|
206
|
+
mrf, view=None, size=None, nodeColor=None, factorColor=None, edgeWidth=None, edgeColor=None, cmap=None, cmapEdge=None
|
|
207
|
+
):
|
|
208
|
+
"""
|
|
209
|
+
show a Markov random field
|
|
210
|
+
|
|
211
|
+
:param mrf: the Markov random field
|
|
212
|
+
:param view: 'graph' | 'factorgraph’ | None (default)
|
|
213
|
+
:param size: size of the rendered graph
|
|
214
|
+
:param nodeColor: a nodeMap of values (between 0 and 1) to be shown as color of nodes (with special colors for 0 and 1)
|
|
215
|
+
:param factorColor: a function returning a value (beeween 0 and 1) to be shown as a color of factor. (used when view='factorgraph')
|
|
216
|
+
:param edgeWidth: a edgeMap of values to be shown as width of edges (used when view='graph')
|
|
217
|
+
:param edgeColor: a edgeMap of values (between 0 and 1) to be shown as color of edges (used when view='graph')
|
|
218
|
+
:param cmap: color map to show the colors
|
|
219
|
+
:param cmapEdge: color map to show the edge color if distinction is needed
|
|
220
|
+
:return: the graph
|
|
221
|
+
"""
|
|
222
|
+
if view is None:
|
|
223
|
+
view = gum.config["notebook", "default_markovrandomfield_view"]
|
|
224
|
+
|
|
225
|
+
if size is None:
|
|
226
|
+
size = gum.config["notebook", "default_graph_size"]
|
|
227
|
+
|
|
228
|
+
if cmapEdge is None:
|
|
229
|
+
cmapEdge = cmap
|
|
230
|
+
|
|
231
|
+
if view == "graph":
|
|
232
|
+
dottxt = MRF2UGdot(mrf, size, nodeColor, edgeWidth, edgeColor, cmap, cmapEdge)
|
|
233
|
+
else:
|
|
234
|
+
dottxt = MRF2FactorGraphdot(mrf, size, nodeColor, factorColor, cmapNode=cmap)
|
|
235
|
+
|
|
236
|
+
return showGraph(dottxt, size)
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def showInfluenceDiagram(diag, size=None):
|
|
240
|
+
"""
|
|
241
|
+
show an influence diagram as a graph
|
|
242
|
+
|
|
243
|
+
:param diag: the influence diagram
|
|
244
|
+
:param size: size of the rendered graph
|
|
245
|
+
:return: the representation of the influence diagram
|
|
246
|
+
"""
|
|
247
|
+
if size is None:
|
|
248
|
+
size = gum.config["influenceDiagram", "default_id_size"]
|
|
249
|
+
|
|
250
|
+
return showGraph(ID2dot(diag), size)
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def showInference(
|
|
254
|
+
model,
|
|
255
|
+
engine=None,
|
|
256
|
+
evs=None,
|
|
257
|
+
targets=None,
|
|
258
|
+
size=None,
|
|
259
|
+
nodeColor=None,
|
|
260
|
+
factorColor=None,
|
|
261
|
+
arcWidth=None,
|
|
262
|
+
arcColor=None,
|
|
263
|
+
cmap=None,
|
|
264
|
+
cmapArc=None,
|
|
265
|
+
graph=None,
|
|
266
|
+
view=None,
|
|
267
|
+
):
|
|
268
|
+
import warnings
|
|
269
|
+
|
|
270
|
+
warnings.warn(
|
|
271
|
+
"gum.lib.ipython does not provide `showInference` due to the use of svg format (not compatible with spyder)."
|
|
272
|
+
)
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
def showTensor(p):
|
|
276
|
+
print(p)
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
def show(model, size=None):
|
|
280
|
+
"""
|
|
281
|
+
propose a (visual) representation of a model in ipython console
|
|
282
|
+
|
|
283
|
+
:param GraphicalModel model: the model to show (pyagrum.BayesNet, pyagrum.MarkovRandomField, pyagrum.InfluenceDiagram or pyagrum.Tensor)
|
|
284
|
+
|
|
285
|
+
:param int size: optional size for the graphical model (no effect for Tensor)
|
|
286
|
+
"""
|
|
287
|
+
if isinstance(model, gum.BayesNet):
|
|
288
|
+
showBN(model, size)
|
|
289
|
+
elif isinstance(model, gum.MarkovRandomField):
|
|
290
|
+
showMRF(model, size)
|
|
291
|
+
elif isinstance(model, gum.InfluenceDiagram):
|
|
292
|
+
showInfluenceDiagram(model, size)
|
|
293
|
+
elif isinstance(model, gum.Tensor):
|
|
294
|
+
showTensor(model)
|
|
295
|
+
else:
|
|
296
|
+
raise gum.InvalidArgument("Argument model should be a PGM (BayesNet, MarkovRandomField or Influence Diagram")
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
# check if an instance of ipython exists
|
|
300
|
+
try:
|
|
301
|
+
get_ipython
|
|
302
|
+
except NameError:
|
|
303
|
+
import warnings
|
|
304
|
+
|
|
305
|
+
warnings.warn("""
|
|
306
|
+
** pyagrum.lib.notebook has to be import from an IPython's instance.
|
|
307
|
+
""")
|