{ "cells": [ { "cell_type": "raw", "id": "0e4ef35e-e7c0-4123-b58e-018fb6b7554d", "metadata": { "jp-MarkdownHeadingCollapsed": true }, "source": [ "---\n", "title: Load and save graphical models in pyAgrum >2.3.2\n", "date: 2026-04-29\n", "categories: ['notebooks']\n", "authors: Pierre-Henri Wuillemin\n", "tags: snippets\n", "description: \"Comparisons of different input/output formats using pyAgrum\"\n", "---" ] }, { "cell_type": "markdown", "id": "17eba4a8", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "# Loading and saving graphical models" ] }, { "cell_type": "markdown", "id": "b307de73", "metadata": {}, "source": [ "pyAgrum can read and write graphical models in many file formats. This notebook gives an overview of which formats are available for each model type, what they preserve, and why the native **bgum** (binary) and **jgum** (JSON) formats are the best choice for pyAgrum workflows." ] }, { "cell_type": "code", "execution_count": 1, "id": "5f086777", "metadata": { "execution": { "iopub.execute_input": "2026-04-29T11:50:19.724059Z", "iopub.status.busy": "2026-04-29T11:50:19.723852Z", "iopub.status.idle": "2026-04-29T11:50:19.728685Z", "shell.execute_reply": "2026-04-29T11:50:19.727616Z", "shell.execute_reply.started": "2026-04-29T11:50:19.724027Z" }, "tags": [] }, "outputs": [], "source": [ "import os\n", "import tempfile\n", "import time\n", "\n", "import pyagrum as gum\n", "import pyagrum.lib.notebook as gnb" ] }, { "cell_type": "markdown", "id": "e346e51f", "metadata": {}, "source": [ "## Available formats\n", "\n", "The three main model types each have their own set of supported formats." ] }, { "cell_type": "code", "execution_count": 2, "id": "2a3d5c16", "metadata": { "execution": { "iopub.execute_input": "2026-04-29T11:50:19.729355Z", "iopub.status.busy": "2026-04-29T11:50:19.729217Z", "iopub.status.idle": "2026-04-29T11:50:19.733211Z", "shell.execute_reply": "2026-04-29T11:50:19.732443Z", "shell.execute_reply.started": "2026-04-29T11:50:19.729339Z" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "BayesNet formats : bif|dsl|net|bifxml|o3prm|uai|xdsl|pkl|jgum|bgum\n", "InfluenceDiagram : xmlbif|bifxml|xml|jgum|bgum|pkl\n", "MarkovRandomField : uai|jgum|bgum|pkl\n" ] } ], "source": [ "print(f\"BayesNet formats : {gum.availableBNExts()}\")\n", "print(f\"InfluenceDiagram : {gum.availableIDExts()}\")\n", "print(f\"MarkovRandomField : {gum.availableMRFExts()}\")" ] }, { "cell_type": "markdown", "id": "041921bb", "metadata": {}, "source": [ "The load/save API is uniform across model types:\n", "\n", "| Model | Load | Save |\n", "|-------|------|------|\n", "| `BayesNet` | `gum.loadBN(filename)` | `gum.saveBN(bn, filename)` |\n", "| `InfluenceDiagram` | `gum.loadID(filename)` | `gum.saveID(diag, filename)` |\n", "| `MarkovRandomField` | `gum.loadMRF(filename)` | `gum.saveMRF(mrf, filename)` |\n", "\n", "The format is selected automatically from the file extension." ] }, { "cell_type": "markdown", "id": "0a563688", "metadata": {}, "source": [ "## Bayesian networks\n", "\n", "### A tour of BN formats\n", "\n", "Let's load the classic Asia network and save/reload it in every available format, comparing file size and round-trip speed." ] }, { "cell_type": "code", "execution_count": 3, "id": "1157629f", "metadata": { "execution": { "iopub.execute_input": "2026-04-29T11:50:19.733862Z", "iopub.status.busy": "2026-04-29T11:50:19.733735Z", "iopub.status.idle": "2026-04-29T11:50:19.852592Z", "shell.execute_reply": "2026-04-29T11:50:19.852046Z", "shell.execute_reply.started": "2026-04-29T11:50:19.733849Z" }, "tags": [] }, "outputs": [ { "data": { "text/html": [ "\n", " \n", "