Input/Output Formats for Bayesian Networks
PyAgrum supports several formats for saving and loading graphical models, but not all formats preserve every feature of the models. Below is a guide to the supported formats and their capabilities.
Supported Formats
Section titled “Supported Formats”Warning
Only the native JSON formats (jgum and bgum), pickle, and BIFXML guarantee complete preservation of all graphical model features. Other formats may lose some of this information during serialization.
JGUM/BGUM format (Recommended)
Section titled “JGUM/BGUM format (Recommended)”JGUM and BGUM formats are JSON-based formats that ensure complete preservation of all features and attributes of the graphical model. They are the recommended formats for saving and loading Bayesian networks in PyAgrum.
- BGUM is a binary version of JGUM, which is more compact and faster to read/write (MessagePack format).
- JGUM is a text-based format that is human-readable and easy to debug.
import pyagrum as gum
## Save to JGUM/BGUMgum.config["core","default_jgumIndent"]=2 # Optional: set indentation for better readability (-1 = max compacity)gum.saveBN(bn, "model.jgum")gum.saveBN(bn, "binary_model.bgum")
## Load from JGUM/BGUMbn = gum.loadBN("model.jgum")bn2 = gum.loadBN("model.bgum") # bn==bn2BIFXML (complete preservation)
Section titled “BIFXML (complete preservation)”The BIFXML format is recommended for interoperability with other Bayesian network tools. It preserves all features and attributes of the model.
import pyagrum as gum
## Save to BIFXMLgum.saveBN(bn, "model.bifxml")
## Load from BIFXMLbn = gum.loadBN("model.bifxml")Pickle (Alternative for complete preservation)
Section titled “Pickle (Alternative for complete preservation)”The Python pickle format is also supported for complete preservation.
import pyagrum as gumimport pickle
## Save modelwith open('model.pkl', 'wb') as f: pickle.dump(bn, f)
## Load modelwith open('model.pkl', 'rb') as f: bn2 = pickle.load(f)
## or easilygum.saveBN(bn, "model.pkl")bn2 = gum.loadBN("model.pkl")Other Supported Formats
Section titled “Other Supported Formats”The following formats are supported but may not preserve all model features:
- Net (.net): Standard format for Bayesian networks but may lose some metadata.
- DSL (.dsl): GeNIe format with some limitations.
- UAI (.uai): Very simple format for Bayesian networks and Markov networks with limitations.
- BIF (.bif): Format for Bayesian networks with limitations.
- etc. (see
pyagrum.availableBNExts()for a complete list).
SEE ALSO
Section titled “SEE ALSO”JGUM / BGUM Format Reference : Complete JSON/binary format specification covering BN, InfluenceDiagram and MarkovRandomField.