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/bn2scores.py
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
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 computing different scores from a BN.
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
import csv
|
|
46
|
+
import math
|
|
47
|
+
|
|
48
|
+
import pyagrum as gum
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def lines_count(filename):
|
|
52
|
+
"""count lines in a file"""
|
|
53
|
+
with open(filename) as f:
|
|
54
|
+
count = sum(1 for _ in f)
|
|
55
|
+
|
|
56
|
+
return count
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def checkCompatibility(bn, fields, csv_name):
|
|
60
|
+
"""
|
|
61
|
+
check if the variables of the bn are in the fields
|
|
62
|
+
|
|
63
|
+
Parameters
|
|
64
|
+
----------
|
|
65
|
+
bn: gum.BayesNet
|
|
66
|
+
the model
|
|
67
|
+
fields: Dict[str,int]
|
|
68
|
+
Dict of name,position in the file
|
|
69
|
+
csv_name: str
|
|
70
|
+
name of the csv file
|
|
71
|
+
|
|
72
|
+
Raises
|
|
73
|
+
------
|
|
74
|
+
gum.DatabaseError
|
|
75
|
+
if a BN variable is not in fields
|
|
76
|
+
|
|
77
|
+
Returns
|
|
78
|
+
-------
|
|
79
|
+
Dict[int,str]
|
|
80
|
+
return a dictionary of position for BN variables in fields
|
|
81
|
+
"""
|
|
82
|
+
res = {}
|
|
83
|
+
for field in bn.names():
|
|
84
|
+
if field not in fields:
|
|
85
|
+
raise gum.DatabaseError(f"** At least, field '{field}' is missing in {csv_name}")
|
|
86
|
+
res[bn.idFromName(field)] = fields[field]
|
|
87
|
+
|
|
88
|
+
return res
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def computeScores(bn_name, csv_name, visible=False, dialect=None):
|
|
92
|
+
"""
|
|
93
|
+
Compute scores (likelihood, aic, bic, mdl, etc.) from a bn w.r.t to a csv
|
|
94
|
+
|
|
95
|
+
Parameters
|
|
96
|
+
----------
|
|
97
|
+
bn_name : pyagrum.BayesNet | str
|
|
98
|
+
a gum.BayesianNetwork or a filename for a BN
|
|
99
|
+
csv_name : str
|
|
100
|
+
a filename for the CSV database
|
|
101
|
+
visible: bool
|
|
102
|
+
do we show the progress
|
|
103
|
+
dialect: csv.Dialect
|
|
104
|
+
if not provided, dialect will be inferred using csv.Sniffer().sniff(csvfile.read(1024))
|
|
105
|
+
|
|
106
|
+
Returns
|
|
107
|
+
-------
|
|
108
|
+
Tuple[float,Dict[str,float]]
|
|
109
|
+
percentDatabaseUsed,scores
|
|
110
|
+
"""
|
|
111
|
+
if isinstance(bn_name, str):
|
|
112
|
+
bn = gum.loadBN(bn_name)
|
|
113
|
+
else:
|
|
114
|
+
bn = bn_name
|
|
115
|
+
|
|
116
|
+
nbr_lines = lines_count(csv_name) - 1
|
|
117
|
+
|
|
118
|
+
if dialect is None:
|
|
119
|
+
with open(csv_name, "r") as csvfile:
|
|
120
|
+
k = 1024
|
|
121
|
+
buffer = ""
|
|
122
|
+
while True:
|
|
123
|
+
try:
|
|
124
|
+
buffer += csvfile.read(k)
|
|
125
|
+
dialect = csv.Sniffer().sniff(buffer)
|
|
126
|
+
break
|
|
127
|
+
except csv.Error:
|
|
128
|
+
if k == -1:
|
|
129
|
+
# we already tried with all the file
|
|
130
|
+
raise gum.DatabaseError("csv.Sniffer could not determine delimiter even with the whole file")
|
|
131
|
+
if len(buffer) > 16384:
|
|
132
|
+
# we try with all the file now
|
|
133
|
+
k = -1
|
|
134
|
+
|
|
135
|
+
nbr_insignificant = 0
|
|
136
|
+
num_ligne = 1
|
|
137
|
+
likelihood = 0.0
|
|
138
|
+
|
|
139
|
+
with open(csv_name, "r") as csvfile:
|
|
140
|
+
batchReader = csv.reader(csvfile, dialect)
|
|
141
|
+
|
|
142
|
+
titre = next(batchReader)
|
|
143
|
+
fields = {}
|
|
144
|
+
for i, nom in enumerate(titre):
|
|
145
|
+
fields[nom] = i
|
|
146
|
+
|
|
147
|
+
positions = checkCompatibility(bn, fields, csv_name)
|
|
148
|
+
|
|
149
|
+
inst = bn.completeInstantiation()
|
|
150
|
+
|
|
151
|
+
if visible:
|
|
152
|
+
# tqdm is optional
|
|
153
|
+
# pylint: disable=import-outside-toplevel
|
|
154
|
+
from tqdm import tqdm
|
|
155
|
+
|
|
156
|
+
pbar = tqdm(total=nbr_lines, desc=csv_name, bar_format="{desc}: {percentage:3.0f}%|{bar}|")
|
|
157
|
+
|
|
158
|
+
for data in batchReader:
|
|
159
|
+
num_ligne += 1
|
|
160
|
+
|
|
161
|
+
for i in positions.keys():
|
|
162
|
+
try:
|
|
163
|
+
name = bn.variable(i).name()
|
|
164
|
+
inst.chgVal(name, _getIdLabel(inst, name, data[positions[i]]))
|
|
165
|
+
except gum.OutOfBounds:
|
|
166
|
+
print(
|
|
167
|
+
f"Out of bounds for ({i},{positions[i]}) : unknown id or label '{data[positions[i]]}' for the variable {inst.variable(i)}"
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
p = bn.jointProbability(inst)
|
|
171
|
+
if p == 0.0:
|
|
172
|
+
print(str(num_ligne) + ":" + str(inst))
|
|
173
|
+
nbr_insignificant += 1
|
|
174
|
+
else:
|
|
175
|
+
likelihood += math.log(p, 2)
|
|
176
|
+
if visible:
|
|
177
|
+
pbar.update()
|
|
178
|
+
|
|
179
|
+
if visible:
|
|
180
|
+
pbar.close()
|
|
181
|
+
|
|
182
|
+
nbr_arcs = 1.0 * bn.sizeArcs()
|
|
183
|
+
dim = 1.0 * bn.dim()
|
|
184
|
+
|
|
185
|
+
aic = likelihood - dim
|
|
186
|
+
aicc = 2 * aic - 2 * dim * (dim + 1) / (nbr_lines - dim + 1) if (nbr_lines - dim + 1 > 0) else "undefined"
|
|
187
|
+
bic = likelihood - dim * math.log(nbr_lines, 2)
|
|
188
|
+
mdl = likelihood - nbr_arcs * math.log(nbr_lines, 2) - 32 * dim # 32=nbr bits for a params
|
|
189
|
+
|
|
190
|
+
return (
|
|
191
|
+
(nbr_lines - nbr_insignificant) * 100.0 / nbr_lines,
|
|
192
|
+
{"likelihood": likelihood, "aic": aic, "aicc": aicc, "bic": bic, "mdl": mdl},
|
|
193
|
+
)
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def _getIdLabel(inst, i, label):
|
|
197
|
+
"""
|
|
198
|
+
Return the idLabel. if label is an int, it already is an idLabel
|
|
199
|
+
|
|
200
|
+
Parameters
|
|
201
|
+
----------
|
|
202
|
+
inst : gum.Instantiation
|
|
203
|
+
the inst to find the idLabel
|
|
204
|
+
i: int
|
|
205
|
+
the id to find the variable
|
|
206
|
+
label: int|str
|
|
207
|
+
the label or id of label
|
|
208
|
+
|
|
209
|
+
Returns
|
|
210
|
+
-------
|
|
211
|
+
int
|
|
212
|
+
the id of label
|
|
213
|
+
"""
|
|
214
|
+
if isinstance(label, int): # an idLabel
|
|
215
|
+
return label
|
|
216
|
+
|
|
217
|
+
return inst.variable(i)[label]
|