pgmpy / pgmpy

Python Library for learning (Structure and Parameter), inference (Probabilistic and Causal), and simulations in Bayesian Networks.

Home Page:https://pgmpy.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

default attribute in Probability block

White-On opened this issue · comments

The default attribute in a Probability block in a BIF file making the file unreadable

I'm currently working on several libraries working around a Bayesian network to compare the performance. But, some library generates BIF files with a default attribute (which seems to be a valid attribute according to the reference you give here in the reference section: https://pgmpy.org/readwrite/bif.html

Attributes
Several attributes are defined at this point: property, type, table,
default and entry attributes (the entry attribute is not associated
with any keyword).
The attribute property can appear in all types of blocks. A property
is just a string of arbitrary text to be associated with a
block.

So I'm trying to make the default attribute as "table". In pgmpy/readwrite/BIF. py, a RegExp seems to be the key to make that possible at line 282. The original one was .*\\n[] *table .*\n.* and the first thing I've noticed is \\n may not lead to expected result and may need to be \n.

Another problem, If I change the word table with default, the file is correctly read without issue. But, as I want both table and default I end up trying those two RegExp: .*\n[] * (table|default) .*\n.* and .*\n[] *table .*\n.*|.*\n[] *default .*\n.*

Both will result in a correct reading of the BIF file, but went I try to get access to the model, I just end up with a ValueError.

In conclusion, I was wondering if there is a way for the default attribute to be considered as equal as the table attribute.

@White-On I think that should be possible. I can't think of a reason why get_model is throwing an error because of the change that you have described. Would it be possible to somehow share the changes that you have made (maybe a patch file or pushing to repo fork) so that I can have a look at the error?

This is the piece of code I've been testing on :
Code_NAjq2rWLHk

If nothing is done to the BIF reading code, the current error is the following:
Code_5nkgdj5cCI

The Bntest.bif is the following:
BnTest.zip
It is generated by a other library called PyAgrum and contain default attribute

as I replace all those default attribute by table the result is as follow :
Code_nZ975DvRvt
the BN is correct and what I expect as a result.

So by changing the RegExp tracking the attribute table I what hopping to get the same result. But as I'm changing the RegExp to be either .*\\n[] *table .*\n.*, .*\n[] * (table|default) .*\n.* or .*\n[] *table .*\n.*|.*\n[] *default .*\n.*, I only get this one error
Code_qjT8a5FdFE

Now the BIF file seems to be read but not the way intended. ( the error is no longer trigger by the BIFReader but by access to the model )

@White-On Thanks for the file and the code. To make the code work for default as well, we need to slightly modify the grammar definition for the probability tables as well. Specifically, this line 157 (https://github.com/pgmpy/pgmpy/blob/dev/pgmpy/readwrite/BIF.py#L157) would need to be changed to:

        probab_attributes = optional_expr | Suppress("table") | Suppress("default")

I think it would be nice to make these changes in pgmpy as well so that users can read files exported from PyAgrum. Would you like to create a PR with these changes?

The PR is on the way ! Hopefully, everything is done correctly and the ways it's intended. Looking forward to help/contribute on a future day. 😄