other pyagrum.lib modules

bn2roc

The purpose of this module is to provide tools for building ROC and PR from Bayesian Network.

pyagrum.lib.bn2roc.animPR(bn, datasrc, target='Y', label='1')

Interactive selection of a threshold using TPR and FPR for BN and data

Parameters:
  • bn (pyagrum.BayesNet) – a Bayesian network

  • datasrc (str|DataFrame) – a csv filename or a pandas.DataFrame

  • target (str) – the target

  • label (str) – the target label

Return type:

Any

pyagrum.lib.bn2roc.animROC(bn, datasrc, target='Y', label='1')

Interactive selection of a threshold using TPR and FPR for BN and data

Parameters:
  • bn (pyagrum.BayesNet) – a Bayesian network

  • datasrc (str|DataFrame) – a csv filename or a pandas.DataFrame

  • target (str) – the target

  • label (str) – the target label

Return type:

Any

pyagrum.lib.bn2roc.getPRpoints(bn, datasrc, target, label, with_labels=True, significant_digits=10)

Compute the points of the PR curve

Parameters:
  • bn (pyagrum.BayesNet) – a Bayesian network

  • datasrc (str|DataFrame) – a csv filename or a pandas.DataFrame

  • target (str) – the target

  • label (str) – the target’s label

  • with_labels (bool) – whether we use label or id (especially for parameter label)

  • significant_digits (int) – number of significant digits when computing probabilities

Return type:

list[tuple[float, float]]

Returns:

list[tuple[float,float]]

the list of points (precision,recall)

pyagrum.lib.bn2roc.getROCpoints(bn, datasrc, target, label, with_labels=True, significant_digits=10)

Compute the points of the ROC curve

Parameters:
  • bn (pyagrum.BayesNet) – a Bayesian network

  • datasrc (str | DataFrame) – a csv filename or a DataFrame

  • target (str) – the target

  • label (str) – the target’s label

  • with_labels (bool) – whether we use label or id (especially for parameter label)

  • significant_digits (int) – number of significant digits when computing probabilities

Return type:

list[tuple[float, float]]

Returns:

list[tuple[int,int]]

the list of points (FalsePositifRate,TruePositifRate)

pyagrum.lib.bn2roc.showPR(bn, datasrc, target, label, *, beta=1, show_progress=True, show_fig=True, save_fig=False, with_labels=True, significant_digits=10)

Compute the ROC curve and save the result in the folder of the csv file.

Parameters:
  • bn (pyagrum.BayesNet) – a Bayesian network

  • datasrc (str|DataFrame) – a csv filename or a pandas.DataFrame

  • target (str) – the target

  • label (str) – the target label

  • show_progress (bool) – indicates if the progress bar must be printed

  • save_fig (bool) – save the result ?

  • show_fig (bool) – plot the resuls ?

  • with_labels (bool) – labels in csv ?

  • significant_digits (int) – number of significant digits when computing probabilities

  • beta (float)

Return type:

tuple[float, float, float, float]

pyagrum.lib.bn2roc.showROC(bn, datasrc, target, label, show_progress=True, show_fig=True, save_fig=False, with_labels=True, significant_digits=10)

Compute the ROC curve and save the result in the folder of the csv file.

Parameters:
  • bn (pyagrum.BayesNet) – a Bayesian network

  • datasrc (str|DataFrame) – a csv filename or a pandas.DataFrame

  • target (str) – the target

  • label (str) – the target label

  • show_progress (bool) – indicates if the progress bar must be printed

  • save_fig (bool) – save the result

  • show_fig (bool) – plot the resuls

  • with_labels (bool) – labels in csv

  • significant_digits (int) – number of significant digits when computing probabilities

Return type:

tuple[float, float, float, float]

pyagrum.lib.bn2roc.showROC_PR(bn, datasrc, target, label, *, beta=1, show_progress=True, show_fig=True, save_fig=False, with_labels=True, show_ROC=True, show_PR=True, significant_digits=10, bgcolor=None)

Compute the ROC curve and save the result in the folder of the csv file.

Parameters:
  • bn (pyagrum.BayesNet) – a Bayesian network

  • datasrc (str|DataFrame) – a csv filename or a pandas.DataFrame

  • target (str) – the target

  • label (str) – the target label

  • beta (float) – the value of beta for the F-beta score

  • show_progress (bool) – indicates if the progress bar must be printed

  • save_fig (bool) – save the result

  • show_fig (bool) – plot the resuls

  • with_labels (bool) – labels in csv

  • show_ROC (bool) – whether we show the ROC figure

  • show_PR (bool) – whether we show the PR figure

  • significant_digits (int) – number of significant digits when computing probabilities

  • bgcolor (Optional[str]) – HTML background color for the figure (default: None if transparent)

Returns:

(pointsROC, thresholdROC, pointsPR, thresholdPR)

Return type:

tuple

bn2scores

The purpose of this module is to provide tools for computing different scores from a BN.

pyagrum.lib.bn2scores.checkCompatibility(bn, fields, csv_name)

check if the variables of the bn are in the fields

Parameters:
  • bn (pyagrum.BayesNet) – the model

  • fields (dict[str,int]) – Dict of name,position in the file

  • csv_name (str) – name of the csv file

Raises:

pyagrum.DatabaseError – if a BN variable is not in fields

Returns:

return a dictionary of position for BN variables in fields

Return type:

dict[int,str]

pyagrum.lib.bn2scores.computeScores(bn_name, csv_name, visible=False, dialect=None)

Compute scores (likelihood, aic, bic, mdl, etc.) from a bn w.r.t to a csv

Parameters:
  • bn_name (pyagrum.BayesNet | str) – a pyagrum.BayesianNetwork or a filename for a BN

  • csv_name (str) – a filename for the CSV database

  • visible (bool) – do we show the progress

  • dialect (csv.Dialect) – if not provided, dialect will be inferred using csv.Sniffer().sniff(csvfile.read(1024))

Returns:

percentDatabaseUsed,scores

Return type:

tuple[float,dict[str,float]]

pyagrum.lib.bn2scores.lines_count(filename)

count lines in a file

Parameters:

filename (str)

Return type:

int

bn_vs_bn

The purpose of this module is to provide tools for comaring different BNs.

class pyagrum.lib.bn_vs_bn.GraphicalBNComparator(bn1, bn2, delta=1e-06)

Bases: object

BNGraphicalComparator allows to compare in multiple way 2 BNs… The smallest assumption is that the names of the variables are the same in the 2 BNs. But some comparisons will have also to check the type and domainSize of the variables.

The bns have not exactly the same role : _bn1 is rather the referent model for the comparison whereas _bn2 is the compared one to the referent model.

Parameters:
dotDiff()

Return a pydot graph that compares the arcs of _bn1 (reference) with those of self._bn2. full black line: the arc is common for both full red line: the arc is common but inverted in _bn2 dotted black line: the arc is added in _bn2 dotted red line: the arc is removed in _bn2

Warning

if pydot is not installed, this function just returns None

Returns:

the result dot graph or None if pydot can not be imported

Return type:

pydot.Dot

equivalentBNs()

Check if the 2 BNs are equivalent :

  • same variables

  • same graphical structure

  • same parameters

Returns:

“OK” if bn are the same, a description of the error otherwise

Return type:

str

hamming()

Compute hamming and structural hamming distance.

Hamming distance is the difference of edges comparing the 2 skeletons (CPDAGs), and Structural Hamming distance is the difference comparing the CPDAGs including the arcs’ orientation.

Note

Delegates to pyagrum.StructuralMetrics.compare(BN, BN) which aligns nodes by variable name and compares the essential graphs (CPDAGs) in C++.

Returns:

A dictionary containing PURE_HAMMING and STRUCTURAL_HAMMING.

Return type:

dict[str,int]

scores()

Compute Precision, Recall, F-score, dist2opt and SID for self._bn2 compared to self._bn1.

precision and recall are computed considering BN1 as the reference.

Fscore is 2*(recall*precision)/(recall+precision) and is the weighted average of Precision and Recall.

dist2opt=square root of (1-precision)^2+(1-recall)^2 and represents the euclidian distance to the ideal point (precision=1, recall=1).

SID (Structural Intervention Distance, Peters & Bühlmann 2015) counts the number of ordered pairs (i, j) for which the parent-adjustment formula in self._bn2 gives a wrong intervention distribution relative to self._bn1.

Note

Delegates to pyagrum.StructuralMetrics (aGrUM C++) with nodes matched by variable name. Precision/recall/F-score/SHD are computed on the essential graphs of the two BNs; SID is computed on their DAGs (it is the only DAG-level metric here). Misoriented arcs are counted once (in fp, not in fn). For a DAG-level precision/recall/F-score, align NodeIds manually and call pyagrum.StructuralMetrics.compare(bn1.dag(), aligned_bn2.dag()) directly.

Returns:

A dictionary containing ‘count’, ‘precision’, ‘recall’, ‘fscore’, ‘dist2opt’, ‘sid’.

Return type:

dict[str,double]

skeletonScores()

Compute Precision, Recall, F-score and dist2opt for the skeleton of self._bn2 compared to self._bn1 (orientations are ignored).

precision and recall are computed considering BN1 as the reference.

Fscore is 2*(recall*precision)/(recall+precision) and is the weighted average of Precision and Recall.

dist2opt=square root of (1-precision)^2+(1-recall)^2 and represents the euclidian distance to the ideal point (precision=1, recall=1).

Note

Delegates to pyagrum.StructuralMetrics (aGrUM C++): comparison runs on the essential graphs of the two BNs, with nodes matched by variable name. For a DAG-level skeleton comparison, align NodeIds manually and call pyagrum.StructuralMetrics.compare(bn1.dag(), aligned_bn2.dag()) directly.

Returns:

A dictionnary containing ‘precision’, ‘recall’, ‘fscore’, ‘dist2opt’ and so on.

Return type:

dict[str,double]

pyagrum.lib.bn_vs_bn.graphDiff(bnref, bncmp, noStyle=False)

Return a pydot graph that compares the arcs of bnref to bncmp. graphDiff allows bncmp to have less nodes than bnref. (this is not the case in GraphicalBNComparator.dotDiff())

if noStyle is False use 4 styles (fixed in pyagrum.config) :
  • the arc is common for both

  • the arc is common but inverted in _bn2

  • the arc is added in _bn2

  • the arc is removed in _bn2

See graphDiffLegend() to add a legend to the graph. .. warning:: if pydot is not installed, this function just returns None

Returns:

the result dot graph or None if pydot can not be imported

Return type:

pydot.Dot

Parameters:

noStyle (bool)

pyagrum.lib.bn_vs_bn.graphDiffLegend()
Return type:

Dot | None