readwrite

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 11, 2026 License: MIT Imports: 12 Imported by: 0

README

readwrite

Package readwrite provides readers and writers for probabilistic model file formats.

Import path: github.com/asymmetric-effort/datascience/lib/pgm/readwrite

Supported Formats

Format Read Write Description
BIF ReadBIF WriteBIF Bayesian Interchange Format
XMLBIF ReadXMLBIF WriteXMLBIF XML-based BIF format
NET ReadNET WriteNET Hugin NET format
UAI ReadUAI WriteUAI UAI competition format
XDSL ReadXDSL WriteXDSL GeNIe XDSL format
PomdpX ReadPomdpX - POMDP-X format (BN subset)
XBN ReadXBN - Microsoft XBN format
CSV ReadCSVStructure / ReadCSVWithCPDs WriteCSV Edge-list CSV and CPD CSV
JSON ReadJSON WriteJSON datascience JSON format with nodes, edges, states, CPDs
XML ReadXML WriteXML datascience-native XML format
Parquet - - Not yet implemented (placeholder)
XLSX - - Not yet implemented (placeholder)

All reader functions accept io.Reader and all writer functions accept io.Writer, making them composable with files, buffers, and network streams.

Usage Examples

BIF Format
import (
    "os"
    "github.com/asymmetric-effort/datascience/lib/pgm/readwrite"
)

// Read a BIF file
f, _ := os.Open("alarm.bif")
bn, _ := readwrite.ReadBIF(f)
f.Close()

// Write a BIF file
out, _ := os.Create("output.bif")
readwrite.WriteBIF(out, bn)
out.Close()
JSON Format
f, _ := os.Open("model.json")
bn, _ := readwrite.ReadJSON(f)
f.Close()

out, _ := os.Create("model.json")
readwrite.WriteJSON(out, bn)
out.Close()
CSV Edge-List
// CSV with "from,to" header
f, _ := os.Open("edges.csv")
bn, _ := readwrite.ReadCSVStructure(f) // structure only, no CPDs
f.Close()

// CSV with CPDs
f2, _ := os.Open("model.csv")
bn2, _ := readwrite.ReadCSVWithCPDs(f2)
f2.Close()
UAI Format
f, _ := os.Open("model.uai")
bn, _ := readwrite.ReadUAI(f)
f.Close()

out, _ := os.Create("model.uai")
readwrite.WriteUAI(out, bn)
out.Close()
XMLBIF Format
f, _ := os.Open("model.xmlbif")
bn, _ := readwrite.ReadXMLBIF(f)
f.Close()
NET Format (Hugin)
f, _ := os.Open("model.net")
bn, _ := readwrite.ReadNET(f)
f.Close()
XDSL Format (GeNIe)
f, _ := os.Open("model.xdsl")
bn, _ := readwrite.ReadXDSL(f)
f.Close()
XBN Format (Microsoft)
f, _ := os.Open("model.xbn")
bn, _ := readwrite.ReadXBN(f)
f.Close()
datascience Native XML
f, _ := os.Open("model.xml")
bn, _ := readwrite.ReadXML(f)
f.Close()

out, _ := os.Create("model.xml")
readwrite.WriteXML(out, bn)
out.Close()

Documentation

Overview

Package readwrite provides readers and writers for probabilistic model file formats: BIF, XMLBIF, NET, UAI, XDSL, PomdpX, XBN, CSV, JSON, and datascience-native XML.

Index

Constants

View Source
const MaxInputSize = 1 << 20 // 1 MB

MaxInputSize is the default maximum number of bytes accepted by any reader in this package. Inputs exceeding this limit are rejected to prevent denial-of-service via oversized or maliciously crafted files (e.g., XML bombs). Use the *WithLimit variants (e.g., ReadXMLBIFWithLimit) for larger models.

Variables

This section is empty.

Functions

func ReadBIF

func ReadBIF(r io.Reader) (*models.BayesianNetwork, error)

ReadBIF parses a BIF (Bayesian Interchange Format) file and returns a BayesianNetwork. It handles network, variable, and probability blocks. Lines containing // comments have the comment portion stripped.

func ReadCSVCPD

func ReadCSVCPD(r io.Reader) (*factors.TabularCPD, error)

ReadCSVCPD reads a CPD from a CSV table.

Format:

  • Row 0 (header): first cell is the variable name, remaining cells are parent configuration labels (or empty for unconditional).
  • Row 1..N: first cell is the state name, remaining cells are probabilities.

For an unconditional CPD (no parents), the header has one cell and each data row has two cells (state, probability).

For a conditional CPD, parent configurations are encoded as "Parent1=state,Parent2=state" in the header cells.

func ReadCSVStructure

func ReadCSVStructure(r io.Reader) (*models.BayesianNetwork, error)

ReadCSVStructure reads an edge-list CSV with "from,to" columns and creates a BayesianNetwork containing only structure (nodes and edges, no CPDs). The first row must be a header with fields "from" and "to" (case-insensitive).

func ReadJSON

func ReadJSON(r io.Reader) (*models.BayesianNetwork, error)

ReadJSON parses a JSON file and returns a fully populated BayesianNetwork, including nodes, edges, states, and CPDs.

func ReadJSONStructure

func ReadJSONStructure(r io.Reader) (*models.BayesianNetwork, error)

ReadJSONStructure parses a JSON file and returns a BayesianNetwork with structure only (nodes, edges, states). CPDs in the JSON are ignored.

func ReadJSONStructureWithLimit added in v0.1.1

func ReadJSONStructureWithLimit(r io.Reader, maxBytes int) (*models.BayesianNetwork, error)

ReadJSONStructureWithLimit is like ReadJSONStructure but accepts a custom maximum input size in bytes. Use this for models larger than MaxInputSize (1 MB).

func ReadJSONWithLimit added in v0.1.1

func ReadJSONWithLimit(r io.Reader, maxBytes int) (*models.BayesianNetwork, error)

ReadJSONWithLimit is like ReadJSON but accepts a custom maximum input size in bytes. Use this for models larger than MaxInputSize (1 MB).

func ReadNET

func ReadNET(r io.Reader) (*models.BayesianNetwork, error)

ReadNET parses a Hugin NET format file and returns a BayesianNetwork. NET format uses: node X { states = ("s0" "s1"); } and potential (X | Y) { data = ((0.3 0.7)(0.8 0.2)); }

func ReadParquet

func ReadParquet(filename string) (*tabgo.DataFrame, error)

ReadParquet reads a DataFrame from a Parquet file. Parquet binary format support is not yet implemented. Use CSV instead.

func ReadPomdpX

func ReadPomdpX(r io.Reader) (*models.BayesianNetwork, error)

ReadPomdpX parses a PomdpX format file and returns a BayesianNetwork. Supports full Variable definitions with ValueEnum, InitialStateBelief for unconditional CPDs, and StateTransitionFunction for conditional CPDs with parent references via Entry instance/probability pairs.

func ReadPomdpXWithLimit added in v0.1.1

func ReadPomdpXWithLimit(r io.Reader, maxBytes int) (*models.BayesianNetwork, error)

ReadPomdpXWithLimit is like ReadPomdpX but accepts a custom maximum input size in bytes. Use this for models larger than MaxInputSize (1 MB).

func ReadUAI

func ReadUAI(r io.Reader) (*models.BayesianNetwork, error)

func ReadXBN

func ReadXBN(r io.Reader) (*models.BayesianNetwork, error)

ReadXBN parses a Microsoft XBN format file and returns a BayesianNetwork. Supports full NODELIST with STATENAME elements, ARCLIST with parent/child arcs, and DISTRIBS with DIST elements containing CONDSET/CONDELEM for parent references and DPIS/DPI with INDEXES attributes for conditional probability distributions.

func ReadXBNWithLimit added in v0.1.1

func ReadXBNWithLimit(r io.Reader, maxBytes int) (*models.BayesianNetwork, error)

ReadXBNWithLimit is like ReadXBN but accepts a custom maximum input size in bytes. Use this for models larger than MaxInputSize (1 MB).

func ReadXDSL

func ReadXDSL(r io.Reader) (*models.BayesianNetwork, error)

ReadXDSL parses a GeNIe XDSL format file and returns a BayesianNetwork.

func ReadXDSLWithLimit added in v0.1.1

func ReadXDSLWithLimit(r io.Reader, maxBytes int) (*models.BayesianNetwork, error)

ReadXDSLWithLimit is like ReadXDSL but accepts a custom maximum input size in bytes. Use this for models larger than MaxInputSize (1 MB).

func ReadXLSX

func ReadXLSX(filename string) (*models.BayesianNetwork, error)

ReadXLSX reads a BayesianNetwork from an XLSX file. XLSX support requires third-party ZIP/XML parsing libraries and is not yet implemented. Use JSON or CSV formats instead.

func ReadXMLBIF

func ReadXMLBIF(r io.Reader) (*models.BayesianNetwork, error)

ReadXMLBIF parses an XMLBIF format file and returns a BayesianNetwork.

func ReadXMLBIFWithLimit added in v0.1.1

func ReadXMLBIFWithLimit(r io.Reader, maxBytes int) (*models.BayesianNetwork, error)

ReadXMLBIFWithLimit is like ReadXMLBIF but accepts a custom maximum input size in bytes. Use this for models larger than MaxInputSize (1 MB).

func ReadXMLNative

func ReadXMLNative(r io.Reader) (*models.BayesianNetwork, error)

ReadXMLNative parses a datascience-native XML file and returns a BayesianNetwork.

func ReadXMLNativeWithLimit added in v0.1.1

func ReadXMLNativeWithLimit(r io.Reader, maxBytes int) (*models.BayesianNetwork, error)

ReadXMLNativeWithLimit is like ReadXMLNative but accepts a custom maximum input size in bytes. Use this for models larger than MaxInputSize (1 MB).

func WriteBIF

func WriteBIF(w io.Writer, bn *models.BayesianNetwork) error

WriteBIF serializes a BayesianNetwork to BIF (Bayesian Interchange Format). Variable blocks are written in the order returned by bn.Nodes(), followed by probability blocks in the same order.

func WriteCSVCPD

func WriteCSVCPD(w io.Writer, cpd *factors.TabularCPD) error

WriteCSVCPD writes a TabularCPD as a CSV table.

The output has a header row with the variable name in the first cell, followed by parent configuration labels (or a single empty-label column for unconditional). Each subsequent row is a child state followed by its probability values.

func WriteCSVStructure

func WriteCSVStructure(w io.Writer, bn *models.BayesianNetwork) error

WriteCSVStructure writes the edge list of a BayesianNetwork as CSV with columns "from,to".

func WriteJSON

func WriteJSON(w io.Writer, bn *models.BayesianNetwork) error

WriteJSON serializes a BayesianNetwork to JSON format.

func WriteNET

func WriteNET(w io.Writer, bn *models.BayesianNetwork) error

WriteNET serializes a BayesianNetwork to Hugin NET format.

func WriteParquet

func WriteParquet(filename string, df *tabgo.DataFrame) error

WriteParquet writes a DataFrame to a Parquet file. Parquet binary format support is not yet implemented. Use CSV instead.

func WritePomdpX

func WritePomdpX(w io.Writer, bn *models.BayesianNetwork) error

WritePomdpX serializes a BayesianNetwork to PomdpX format with full conditional probability table support. Unconditional distributions are written in InitialStateBelief; conditional distributions are written in StateTransitionFunction with Entry elements containing Instance tags.

func WriteUAI

func WriteUAI(w io.Writer, bn *models.BayesianNetwork) error

WriteUAI serializes a BayesianNetwork to UAI format.

func WriteXBN

func WriteXBN(w io.Writer, bn *models.BayesianNetwork) error

WriteXBN serializes a BayesianNetwork to Microsoft XBN format with full NODELIST (including STATENAME elements), ARCLIST, and DISTRIBS with CONDSET/CONDELEM and DPI elements with INDEXES attributes.

func WriteXDSL

func WriteXDSL(w io.Writer, bn *models.BayesianNetwork) error

WriteXDSL serializes a BayesianNetwork to GeNIe XDSL format.

func WriteXLSX

func WriteXLSX(filename string, bn *models.BayesianNetwork) error

WriteXLSX writes a BayesianNetwork to an XLSX file. XLSX support requires third-party ZIP/XML parsing libraries and is not yet implemented. Use JSON or CSV formats instead.

func WriteXMLBIF

func WriteXMLBIF(w io.Writer, bn *models.BayesianNetwork) error

WriteXMLBIF serializes a BayesianNetwork to XMLBIF format.

func WriteXMLNative

func WriteXMLNative(w io.Writer, bn *models.BayesianNetwork) error

WriteXMLNative serializes a BayesianNetwork to datascience-native XML format.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL