Visualizing state space

from phasic import Graph # ALWAYS import phasic first
import numpy as np
%config InlineBackend.figure_format = 'svg'

from vscodenb import set_vscode_theme
set_vscode_theme()
Overriding theme from NOTEBOOK_THEME environment variable. <phasic._DeviceListFilter object at 0x14f96bfd0>

def mesh(state):
    reachable = []
    if state.sum() < 1:
        return reachable
    for i in range(state.size):
        for j in range(state.size):
            if i == j: continue
            if i % 2: continue
            new_state = state.copy()
            if state[i] > 0 and state[j] <= len(state):
                new_state[i] -= 1
                new_state[j] += 1
                reachable.append([new_state, 1.0])
        if state[i] and state.sum() >= 1:
            new_state = state.copy()
            new_state[i] -= 1
            reachable.append([new_state, 1.0])
    return reachable

graph = Graph(mesh, ipv=[2, 2])
graph.plot()
Overriding theme from NOTEBOOK_THEME environment variable. <phasic._DeviceListFilter object at 0x14f96bfd0>

Change node separation from the default of 1:

graph.plot(nodesep=0.5)
Overriding theme from NOTEBOOK_THEME environment variable. <phasic._DeviceListFilter object at 0x14f96bfd0>

Also change the separation of nodes with different rank from the default of 1:

graph.plot(nodesep=0.5, ranksep=0.5)
Overriding theme from NOTEBOOK_THEME environment variable. <phasic._DeviceListFilter object at 0x14f96bfd0>

Plot node rank top to bottom (TB) rather than default left to right (LR):

graph.plot(rankdir="TB", ranksep=0.3)
Overriding theme from NOTEBOOK_THEME environment variable. <phasic._DeviceListFilter object at 0x14f96bfd0>

Width of edges, size of font, unicolor, and size of figure:

graph.plot(size=(7, 5), fontsize=30, rainbow=False, penwidth=5)
Overriding theme from NOTEBOOK_THEME environment variable. <phasic._DeviceListFilter object at 0x14f96bfd0>

Save to file

graph.plot(filename="graph_output.svg")
graph.plot(filename="graph_output.pdf")
graph.plot(filename="graph_output.png")
graph.plot(filename="graph_output.jpg")
[WARNING] phasic.plot: Dark mode: False
[WARNING] phasic.plot: Dark mode: False
[WARNING] phasic.plot: Dark mode: False
[WARNING] phasic.plot: Dark mode: False
[WARNING] phasic.plot: Dark mode: False
[WARNING] phasic.plot: Dark mode: False
[WARNING] phasic.plot: Dark mode: False
[WARNING] phasic.plot: Dark mode: False

Subgraphs

def fun(state):
    return f'First index\nvalue: {state[0]}'

graph.plot(by_state=fun)
Overriding theme from NOTEBOOK_THEME environment variable. <phasic._DeviceListFilter object at 0x14f96bfd0>

def fun(index):
    return index > 5

graph.plot(by_index=fun)
Overriding theme from NOTEBOOK_THEME environment variable. <phasic._DeviceListFilter object at 0x14f96bfd0>