dbn

Support code for Dynamic Bayesian Networks (DBNs)

The only code defined here is intended for internal use in gtbook display. If you have a need to use it, you can import it as follows:

from gtbook.dbn import dbn_writer, has_positions

Create or amend a DotWriter to be use in show:


source

has_positions

 has_positions (writer)

Check if writer has positions for engine selection


source

dbn_writer

 dbn_writer (writer=None, hints:dict=None, positions:dict=None,
             boxes:set=None, factor_positions:dict=None,
             binary_edges=False, **kwargs)

Create a DotWriter depending on input arguments: If writer is supplied, we will add but not overwrite hints or positions.

# Check None cases
assert dbn_writer() is None
assert dbn_writer(exact=True) is None

# Check passthrough
writer = GraphvizFormatting()
test_eq(dbn_writer(writer), writer)
test_eq(has_positions(writer), False)

# Check boxes, and that they don't stomp
writer = dbn_writer(boxes={1, 2})
test_eq(writer.boxes, {1, 2})
writer = dbn_writer(writer, boxes={2, 3})
test_eq(writer.boxes, {1, 2, 3})
test_eq(has_positions(writer), False)

# Check hints, and that they don't stomp
writer = dbn_writer(hints={"A": 2})
test_eq(writer.positionHints, {"A": 2})
writer = dbn_writer(writer, hints={"A": 3})
test_eq(writer.positionHints, {"A": 2})
test_eq(has_positions(writer), True)

# Check positions, and that they don't stomp
key = 123
writer = dbn_writer(positions={key: (2, 0)})
test_eq(len(writer.variablePositions), 1)
writer = dbn_writer(writer, positions={key: (3, 0)})
test_eq(len(writer.variablePositions), 1)
test_eq(writer.variablePositions[key], (2, 0))
test_eq(has_positions(writer), True)

# Check factor positions, and that they don't stomp
i = 0
writer = dbn_writer(factor_positions={i: (2, 0)})
test_eq(len(writer.factorPositions), 1)
writer = dbn_writer(writer, factor_positions={i: (3, 0)})
test_eq(len(writer.factorPositions), 1)
test_eq(writer.factorPositions[i], (2, 0))
test_eq(has_positions(writer), True)